bugfixes, new config system and messed code for it
[libpicofe.git] / gp2x / emu.c
index 3f2ed3f..74bcde8 100644 (file)
 #include "../common/arm_utils.h"\r
 #include "../common/fonts.h"\r
 #include "../common/emu.h"\r
+#include "../common/config.h"\r
 #include "cpuctrl.h"\r
 \r
 #include <Pico/PicoInt.h>\r
 #include <Pico/Patch.h>\r
+#include <Pico/sound/mix.h>\r
 #include <zlib/zlib.h>\r
 \r
 //#define PFRAMES\r
@@ -159,20 +161,10 @@ void emu_Deinit(void)
        }\r
 \r
        if (!(currentConfig.EmuOpt & 0x20)) {\r
-               FILE *f = fopen(PicoConfigFile, "r+b");\r
-               if (!f) emu_WriteConfig(0);\r
-               else {\r
-                       // if we already have config, reload it, except last ROM\r
-                       fseek(f, sizeof(currentConfig.lastRomFile), SEEK_SET);\r
-                       fread(&currentConfig.EmuOpt, 1, sizeof(currentConfig) - sizeof(currentConfig.lastRomFile), f);\r
-                       fseek(f, 0, SEEK_SET);\r
-                       fwrite(&currentConfig, 1, sizeof(currentConfig), f);\r
-                       fflush(f);\r
-                       fclose(f);\r
+               config_writelrom(PicoConfigFile);\r
 #ifndef NO_SYNC\r
-                       sync();\r
+               sync();\r
 #endif\r
-               }\r
        }\r
 \r
        free(PicoDraw2FB);\r
@@ -184,34 +176,43 @@ void emu_Deinit(void)
                set_gamma(100, 0);\r
 }\r
 \r
+void emu_prepareDefaultConfig(void)\r
+{\r
+       memset(&defaultConfig, 0, sizeof(defaultConfig));\r
+       defaultConfig.EmuOpt    = 0x1d | 0x00700; // | <- ram_tmng, confirm_save, cd_leds\r
+       defaultConfig.s_PicoOpt = 0x0f | 0x20e00; // | <- use_940, cd_pcm, cd_cdda, svp drc\r
+       defaultConfig.s_PsndRate = 44100;\r
+       defaultConfig.s_PicoRegion = 0; // auto\r
+       defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP\r
+       defaultConfig.s_PicoCDBuffers = 64;\r
+       defaultConfig.Frameskip = -1; // auto\r
+       defaultConfig.CPUclock = 200;\r
+       defaultConfig.volume = 50;\r
+       defaultConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU\r
+       defaultConfig.KeyBinds[ 4] = 1<<1;\r
+       defaultConfig.KeyBinds[ 2] = 1<<2;\r
+       defaultConfig.KeyBinds[ 6] = 1<<3;\r
+       defaultConfig.KeyBinds[14] = 1<<4;\r
+       defaultConfig.KeyBinds[13] = 1<<5;\r
+       defaultConfig.KeyBinds[12] = 1<<6;\r
+       defaultConfig.KeyBinds[ 8] = 1<<7;\r
+       defaultConfig.KeyBinds[15] = 1<<26; // switch rend\r
+       defaultConfig.KeyBinds[10] = 1<<27; // save state\r
+       defaultConfig.KeyBinds[11] = 1<<28; // load state\r
+       defaultConfig.KeyBinds[23] = 1<<29; // vol up\r
+       defaultConfig.KeyBinds[22] = 1<<30; // vol down\r
+       defaultConfig.gamma = 100;\r
+       defaultConfig.scaling = 0;\r
+}\r
+\r
 void emu_setDefaultConfig(void)\r
 {\r
-       memset(&currentConfig, 0, sizeof(currentConfig));\r
-       currentConfig.lastRomFile[0] = 0;\r
-       currentConfig.EmuOpt  = 0x1f | 0x600; // | confirm_save, cd_leds\r
-       currentConfig.PicoOpt = 0x0f | 0xe00; // | use_940, cd_pcm, cd_cdda\r
-       currentConfig.PsndRate = 22050; // 44100;\r
-       currentConfig.PicoRegion = 0; // auto\r
-       currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP\r
-       currentConfig.Frameskip = -1; // auto\r
-       currentConfig.CPUclock = 200;\r
-       currentConfig.volume = 50;\r
-       currentConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU\r
-       currentConfig.KeyBinds[ 4] = 1<<1;\r
-       currentConfig.KeyBinds[ 2] = 1<<2;\r
-       currentConfig.KeyBinds[ 6] = 1<<3;\r
-       currentConfig.KeyBinds[14] = 1<<4;\r
-       currentConfig.KeyBinds[13] = 1<<5;\r
-       currentConfig.KeyBinds[12] = 1<<6;\r
-       currentConfig.KeyBinds[ 8] = 1<<7;\r
-       currentConfig.KeyBinds[15] = 1<<26; // switch rend\r
-       currentConfig.KeyBinds[10] = 1<<27; // save state\r
-       currentConfig.KeyBinds[11] = 1<<28; // load state\r
-       currentConfig.KeyBinds[23] = 1<<29; // vol up\r
-       currentConfig.KeyBinds[22] = 1<<30; // vol down\r
-       currentConfig.gamma = 100;\r
-       currentConfig.PicoCDBuffers = 64;\r
-       currentConfig.scaling = 0;\r
+       memcpy(&currentConfig, &defaultConfig, sizeof(currentConfig));\r
+       PicoOpt = currentConfig.s_PicoOpt;\r
+       PsndRate = currentConfig.s_PsndRate;\r
+       PicoRegionOverride = currentConfig.s_PicoRegion;\r
+       PicoAutoRgnOrder = currentConfig.s_PicoAutoRgnOrder;\r
+       PicoCDBuffers = currentConfig.s_PicoCDBuffers;\r
 }\r
 \r
 void osd_text(int x, int y, const char *text)\r
@@ -290,7 +291,8 @@ static void blit(const char *fps, const char *notice)
 {\r
        int emu_opt = currentConfig.EmuOpt;\r
 \r
-       if (PicoOpt&0x10) {\r
+       if (PicoOpt&0x10)\r
+       {\r
                // 8bit fast renderer\r
                if (Pico.m.dirtyPal) {\r
                        Pico.m.dirtyPal = 0;\r
@@ -298,8 +300,14 @@ static void blit(const char *fps, const char *notice)
                        // feed new palette to our device\r
                        gp2x_video_setpalette(localPal, 0x40);\r
                }\r
+               // a hack for VR\r
+               if (PicoRead16Hook == PicoSVPRead16)\r
+                       memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);\r
+               // do actual copy\r
                vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);\r
-       } else if (!(emu_opt&0x80)) {\r
+       }\r
+       else if (!(emu_opt&0x80))\r
+       {\r
                // 8bit accurate renderer\r
                if (Pico.m.dirtyPal) {\r
                        Pico.m.dirtyPal = 0;\r
@@ -432,9 +440,69 @@ static void emu_msg_tray_open(void)
        gettimeofday(&noticeMsgTime, 0);\r
 }\r
 \r
+static void update_volume(int has_changed, int is_up)\r
+{\r
+       static int prev_frame = 0, wait_frames = 0;\r
+       int vol = currentConfig.volume;\r
+\r
+       if (has_changed)\r
+       {\r
+               if (vol < 5 && (PicoOpt&8) && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)\r
+                       wait_frames++;\r
+               else {\r
+                       if (is_up) {\r
+                               if (vol < 99) vol++;\r
+                       } else {\r
+                               if (vol >  0) vol--;\r
+                       }\r
+                       wait_frames = 0;\r
+                       gp2x_sound_volume(vol, vol);\r
+                       currentConfig.volume = vol;\r
+               }\r
+               sprintf(noticeMsg, "VOL: %02i", vol);\r
+               gettimeofday(&noticeMsgTime, 0);\r
+               prev_frame = Pico.m.frame_count;\r
+       }\r
+\r
+       // set the right mixer func\r
+       if (!(PicoOpt&8)) return; // just use defaults for mono\r
+       if (vol >= 5)\r
+               PsndMix_32_to_16l = mix_32_to_16l_stereo;\r
+       else {\r
+               mix_32_to_16l_level = 5 - vol;\r
+               PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl;\r
+       }\r
+}\r
+\r
+static void change_fast_forward(int set_on)\r
+{\r
+       static void *set_PsndOut = NULL;\r
+       static int set_Frameskip, set_EmuOpt, is_on = 0;\r
+\r
+       if (set_on && !is_on) {\r
+               set_PsndOut = PsndOut;\r
+               set_Frameskip = currentConfig.Frameskip;\r
+               set_EmuOpt = currentConfig.EmuOpt;\r
+               PsndOut = NULL;\r
+               currentConfig.Frameskip = 8;\r
+               currentConfig.EmuOpt &= ~4;\r
+               is_on = 1;\r
+       }\r
+       else if (!set_on && is_on) {\r
+               PsndOut = set_PsndOut;\r
+               currentConfig.Frameskip = set_Frameskip;\r
+               currentConfig.EmuOpt = set_EmuOpt;\r
+               PsndRerate(1);\r
+               update_volume(0, 0);\r
+               reset_timing = 1;\r
+               is_on = 0;\r
+       }\r
+}\r
+\r
 static void RunEvents(unsigned int which)\r
 {\r
-       if(which & 0x1800) { // save or load (but not both)\r
+       if (which & 0x1800) // save or load (but not both)\r
+       {\r
                int do_it = 1;\r
                if ( emu_checkSaveFile(state_slot) &&\r
                                (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) ||   // load\r
@@ -456,7 +524,8 @@ static void RunEvents(unsigned int which)
 \r
                reset_timing = 1;\r
        }\r
-       if(which & 0x0400) { // switch renderer\r
+       if (which & 0x0400) // switch renderer\r
+       {\r
                if      (  PicoOpt&0x10)             { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }\r
                else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;\r
                else   currentConfig.EmuOpt &= ~0x80;\r
@@ -473,7 +542,8 @@ static void RunEvents(unsigned int which)
 \r
                gettimeofday(&noticeMsgTime, 0);\r
        }\r
-       if(which & 0x0300) {\r
+       if (which & 0x0300)\r
+       {\r
                if(which&0x0200) {\r
                        state_slot -= 1;\r
                        if(state_slot < 0) state_slot = 9;\r
@@ -484,7 +554,7 @@ static void RunEvents(unsigned int which)
                sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");\r
                gettimeofday(&noticeMsgTime, 0);\r
        }\r
-       if(which & 0x0080) {\r
+       if (which & 0x0080) {\r
                engineState = PGS_Menu;\r
        }\r
 }\r
@@ -552,18 +622,11 @@ static void updateKeys(void)
        events = (allActions[0] | allActions[1]) >> 16;\r
 \r
        // volume is treated in special way and triggered every frame\r
-       if(events & 0x6000) {\r
-               int vol = currentConfig.volume;\r
-               if (events & 0x2000) {\r
-                       if (vol < 99) vol++;\r
-               } else {\r
-                       if (vol >  0) vol--;\r
-               }\r
-               gp2x_sound_volume(vol, vol);\r
-               sprintf(noticeMsg, "VOL: %02i", vol);\r
-               gettimeofday(&noticeMsgTime, 0);\r
-               currentConfig.volume = vol;\r
-       }\r
+       if (events & 0x6000)\r
+               update_volume(1, events & 0x2000);\r
+\r
+       if ((events ^ prevEvents) & 0x40)\r
+               change_fast_forward(events & 0x40);\r
 \r
        events &= ~prevEvents;\r
        if (events) RunEvents(events);\r
@@ -685,15 +748,11 @@ void emu_Loop(void)
        reset_timing = 1;\r
 \r
        // prepare sound stuff\r
-       if(currentConfig.EmuOpt & 4) {\r
+       if (currentConfig.EmuOpt & 4)\r
+       {\r
                int snd_excess_add;\r
                if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old ||\r
                                ((PicoOpt&0x200) && crashed_940)) {\r
-                       /* if 940 is turned off, we need it to be put back to sleep */\r
-                       if (!(PicoOpt&0x200) && ((PicoOpt^PicoOpt_old)&0x200)) {\r
-                               Reset940(1, 2);\r
-                               Pause940(1);\r
-                       }\r
                        PsndRerate(Pico.m.frame_count ? 1 : 0);\r
                }\r
                snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r
@@ -702,13 +761,14 @@ void emu_Loop(void)
                gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);\r
                gp2x_sound_volume(currentConfig.volume, currentConfig.volume);\r
                PicoWriteSound = updateSound;\r
+               update_volume(0, 0);\r
                memset(sndBuffer, 0, sizeof(sndBuffer));\r
                PsndOut = sndBuffer;\r
                PsndRate_old = PsndRate;\r
                PicoOpt_old  = PicoOpt;\r
                pal_old = Pico.m.pal;\r
        } else {\r
-               PsndOut = 0;\r
+               PsndOut = NULL;\r
        }\r
 \r
        // prepare CD buffer\r
@@ -733,14 +793,15 @@ void emu_Loop(void)
                int modes;\r
 \r
                gettimeofday(&tval, 0);\r
-               if(reset_timing) {\r
+               if (reset_timing) {\r
                        reset_timing = 0;\r
                        thissec = tval.tv_sec;\r
                        frames_shown = frames_done = tval.tv_usec/target_frametime;\r
                }\r
 \r
                // show notice message?\r
-               if(noticeMsgTime.tv_sec) {\r
+               if (noticeMsgTime.tv_sec)\r
+               {\r
                        static int noticeMsgSum;\r
                        if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec\r
                                noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;\r
@@ -755,7 +816,8 @@ void emu_Loop(void)
 \r
                // check for mode changes\r
                modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);\r
-               if (modes != oldmodes) {\r
+               if (modes != oldmodes)\r
+               {\r
                        int scalex = 320;\r
                        osd_fps_x = OSD_FPS_X;\r
                        if (modes & 4) {\r
@@ -777,10 +839,11 @@ void emu_Loop(void)
                }\r
 \r
                // second changed?\r
-               if(thissec != tval.tv_sec) {\r
+               if (thissec != tval.tv_sec)\r
+               {\r
 #ifdef BENCHMARK\r
                        static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];\r
-                       if(++bench == 10) {\r
+                       if (++bench == 10) {\r
                                bench = 0;\r
                                bench_fps_s = bench_fps;\r
                                bf[bfp++ & 3] = bench_fps;\r
@@ -789,12 +852,13 @@ void emu_Loop(void)
                        bench_fps += frames_shown;\r
                        sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);\r
 #else\r
-                       if(currentConfig.EmuOpt & 2)\r
+                       if (currentConfig.EmuOpt & 2)\r
                                sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);\r
+                       if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }\r
 #endif\r
                        thissec = tval.tv_sec;\r
 \r
-                       if(PsndOut == 0 && currentConfig.Frameskip >= 0) {\r
+                       if (PsndOut == 0 && currentConfig.Frameskip >= 0) {\r
                                frames_done = frames_shown = 0;\r
                        } else {\r
                                // it is quite common for this implementation to leave 1 fame unfinished\r
@@ -818,7 +882,7 @@ void emu_Loop(void)
                        for(i = 0; i < currentConfig.Frameskip; i++) {\r
                                updateKeys();\r
                                SkipFrame(1); frames_done++;\r
-                               if (PsndOut) { // do framelimitting if sound is enabled\r
+                               if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled\r
                                        gettimeofday(&tval, 0);\r
                                        if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
                                        if(tval.tv_usec < lim_time) { // we are too fast\r
@@ -911,7 +975,7 @@ if (Pico.m.frame_count == 31563) {
                {\r
                        // sleep or vsync if we are still too fast\r
                        // usleep sleeps for ~20ms minimum, so it is not a solution here\r
-                       if(tval.tv_usec < lim_time)\r
+                       if (!reset_timing && tval.tv_usec < lim_time)\r
                        {\r
                                // we are too fast\r
                                if (vsync_offset) {\r
@@ -929,6 +993,7 @@ if (Pico.m.frame_count == 31563) {
                frames_done++; frames_shown++;\r
        }\r
 \r
+       change_fast_forward(0);\r
 \r
        if (PicoMCD & 1) PicoCDBufferFree();\r
 \r