GP2X: low volume and fast forward
[libpicofe.git] / gp2x / emu.c
index 3f2ed3f..a658044 100644 (file)
@@ -25,6 +25,7 @@
 \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
@@ -432,9 +433,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 +517,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 +535,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 +547,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 +615,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 +741,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 +754,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 +786,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 +809,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 +832,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 +845,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 +875,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 +968,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 +986,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