sms wip: initial graphics support
[libpicofe.git] / gp2x / emu.c
index d83cd56..92e5b74 100644 (file)
@@ -5,8 +5,6 @@
 \r
 #include <stdio.h>\r
 #include <stdlib.h>\r
-#include <sys/time.h>\r
-#include <stdarg.h>\r
 \r
 #include "plat_gp2x.h"\r
 #include "soc.h"\r
 \r
 extern int crashed_940;\r
 \r
-static short __attribute__((aligned(4))) sndBuffer[2*44100/50];\r
-static struct timeval noticeMsgTime = { 0, 0 };        // when started showing\r
-static char noticeMsg[40];\r
+static short __attribute__((aligned(4))) sndBuffer[2*(44100+100)/50];\r
 static unsigned char PicoDraw2FB_[(8+320) * (8+240+8)];\r
 unsigned char *PicoDraw2FB = PicoDraw2FB_;\r
 static int osd_fps_x;\r
 \r
 extern void *gp2x_screens[4];\r
 \r
-void plat_status_msg(const char *format, ...)\r
-{\r
-       va_list vl;\r
-\r
-       va_start(vl, format);\r
-       vsnprintf(noticeMsg, sizeof(noticeMsg), format, vl);\r
-       va_end(vl);\r
-\r
-       gettimeofday(&noticeMsgTime, 0);\r
-}\r
-\r
 int plat_get_root_dir(char *dst, int len)\r
 {\r
        extern char **g_argv;\r
@@ -109,7 +94,7 @@ void pemu_prep_defconfig(void)
        if (soc == SOCID_MMSP2)\r
                defaultConfig.s_PicoOpt |= POPT_EXT_FM;\r
        else if (soc == SOCID_POLLUX)\r
-               defaultConfig.EmuOpt |= EOPT_WIZ_TEAR_FIX;\r
+               defaultConfig.EmuOpt |= EOPT_WIZ_TEAR_FIX|EOPT_SHOW_RTC;\r
 }\r
 \r
 static void (*osd_text)(int x, int y, const char *text);\r
@@ -220,7 +205,7 @@ static void draw_pico_ptr(void)
        if (!(Pico.video.reg[12]&1) && !(PicoOpt & POPT_DIS_32C_BORDER))\r
                x += 32;\r
 \r
-       if (EOPT_WIZ_TEAR_FIX) {\r
+       if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
                pitch = 240;\r
                p += (319 - x) * pitch + y;\r
        } else\r
@@ -289,7 +274,7 @@ static int EmuScanEnd8_rot(unsigned int num)
 int localPal[0x100];\r
 static void (*vidcpyM2)(void *dest, void *src, int m32col, int with_32c_border) = NULL;\r
 \r
-static void blit(const char *fps, const char *notice)\r
+void pemu_update_display(const char *fps, const char *notice)\r
 {\r
        int emu_opt = currentConfig.EmuOpt;\r
 \r
@@ -360,11 +345,42 @@ static void blit(const char *fps, const char *notice)
        gp2x_video_flip();\r
 }\r
 \r
-// clears whole screen or just the notice area (in all buffers)\r
-static void clearArea(int full)\r
+/* XXX */\r
+#ifdef __GP2X__\r
+unsigned int plat_get_ticks_ms(void)\r
+{\r
+       return gp2x_get_ticks_ms();\r
+}\r
+\r
+unsigned int plat_get_ticks_us(void)\r
+{\r
+       return gp2x_get_ticks_us();\r
+}\r
+#endif\r
+\r
+void plat_wait_till_us(unsigned int us_to)\r
+{\r
+       unsigned int now;\r
+\r
+       spend_cycles(1024);\r
+       now = plat_get_ticks_us();\r
+\r
+       while ((signed int)(us_to - now) > 512)\r
+       {\r
+               spend_cycles(1024);\r
+               now = plat_get_ticks_us();\r
+       }\r
+}\r
+\r
+void plat_video_wait_vsync(void)\r
+{\r
+       gp2x_video_wait_vsync();\r
+}\r
+\r
+void plat_status_msg_clear(void)\r
 {\r
        int is_8bit = (PicoOpt & POPT_ALT_RENDERER) || !(currentConfig.EmuOpt & EOPT_16BPP);\r
-       if (!full && (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX)) {\r
+       if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
                /* ugh.. */\r
                int i, u, *p;\r
                if (is_8bit) {\r
@@ -383,21 +399,17 @@ static void clearArea(int full)
                return;\r
        }\r
 \r
-       if (is_8bit) {\r
-               // 8-bit renderers\r
-               if (full) gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
-               else      gp2x_memset_all_buffers(320*232, 0xe0, 320*8);\r
-       } else {\r
-               // 16bit accurate renderer\r
-               if (full) gp2x_memset_all_buffers(0, 0, 320*240*2);\r
-               else      gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);\r
-       }\r
+       if (is_8bit)\r
+               gp2x_memset_all_buffers(320*232, 0xe0, 320*8);\r
+       else\r
+               gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);\r
 }\r
 \r
 void plat_status_msg_busy_next(const char *msg)\r
 {\r
-       clearArea(0);\r
-       blit("", msg);\r
+       plat_status_msg_clear();\r
+       pemu_update_display("", msg);\r
+       emu_status_msg("");\r
 \r
        /* assumption: msg_busy_next gets called only when\r
         * something slow is about to happen */\r
@@ -427,6 +439,7 @@ static void vidResetMode(void)
        }\r
        else if (currentConfig.EmuOpt & EOPT_16BPP) {\r
                PicoDrawSetColorFormat(1);\r
+               PicoDrawSetColorFormatMode4(1);\r
                if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {\r
                        gp2x_video_changemode(-16);\r
                        PicoScanBegin = EmuScanBegin16_rot;\r
@@ -494,11 +507,11 @@ void plat_video_toggle_renderer(int is_next, int is_menu)
        vidResetMode();\r
 \r
        if (PicoOpt & POPT_ALT_RENDERER) {\r
-               plat_status_msg(" 8bit fast renderer");\r
+               emu_status_msg(" 8bit fast renderer");\r
        } else if (currentConfig.EmuOpt & EOPT_16BPP) {\r
-               plat_status_msg("16bit accurate renderer");\r
+               emu_status_msg("16bit accurate renderer");\r
        } else {\r
-               plat_status_msg(" 8bit accurate renderer");\r
+               emu_status_msg(" 8bit accurate renderer");\r
        }\r
 }\r
 \r
@@ -560,11 +573,11 @@ void plat_update_volume(int has_changed, int is_up)
                        sndout_oss_setvol(vol, vol);\r
                        currentConfig.volume = vol;\r
                }\r
-               plat_status_msg("VOL: %02i", vol);\r
+               emu_status_msg("VOL: %02i", vol);\r
                prev_frame = Pico.m.frame_count;\r
        }\r
 \r
-       if (need_low_volume)\r
+       if (!need_low_volume)\r
                return;\r
 \r
        /* set the right mixer func */\r
@@ -576,48 +589,87 @@ void plat_update_volume(int has_changed, int is_up)
        }\r
 }\r
 \r
-\r
 static void updateSound(int len)\r
 {\r
-       if (PicoOpt&8) len<<=1;\r
+       len <<= 1;\r
+       if (PicoOpt & POPT_EN_STEREO)\r
+               len <<= 1;\r
+\r
+       if ((currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) && !sndout_oss_can_write(len))\r
+               return;\r
 \r
        /* avoid writing audio when lagging behind to prevent audio lag */\r
        if (PicoSkipFrame != 2)\r
-               sndout_oss_write(PsndOut, len<<1);\r
+               sndout_oss_write(PsndOut, len);\r
 }\r
 \r
 void pemu_sound_start(void)\r
 {\r
        static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;\r
-       int target_fps = Pico.m.pal ? 50 : 60;\r
 \r
        PsndOut = NULL;\r
 \r
        // prepare sound stuff\r
-       if (currentConfig.EmuOpt & 4)\r
+       if (currentConfig.EmuOpt & EOPT_EN_SOUND)\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
+               int is_stereo = (PicoOpt & POPT_EN_STEREO) ? 1 : 0;\r
+               int target_fps = Pico.m.pal ? 50 : 60;\r
+               int frame_samples, snd_excess_add;\r
+               int snd_rate_oss = PsndRate;\r
+               gp2x_soc_t soc;\r
+\r
+               soc = soc_detect();\r
+               if (soc == SOCID_POLLUX) {\r
+                       /* POLLUX pain: DPLL1 / mclk_div / bitclk_div / 4 */\r
+                       switch (PsndRate) {\r
+                       case 44100: PsndRate = 44171; break; // 44170.673077\r
+                       case 22050: PsndRate = 22086; break; // 22085.336538\r
+                       case 11025: PsndRate = 11043; break; // 11042.668269\r
+                       default: break;\r
+                       }\r
+               }\r
+\r
+               #define SOUND_RERATE_FLAGS (POPT_EN_FM|POPT_EN_PSG|POPT_EN_STEREO|POPT_EXT_FM|POPT_EN_MCD_CDDA)\r
+               if (PsndRate != PsndRate_old || Pico.m.pal != pal_old || ((PicoOpt & POPT_EXT_FM) && crashed_940) ||\r
+                               ((PicoOpt ^ PicoOpt_old) & SOUND_RERATE_FLAGS)) {\r
                        PsndRerate(Pico.m.frame_count ? 1 : 0);\r
                }\r
-               snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r
-               printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",\r
-                       PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);\r
-               sndout_oss_start(PsndRate, 16, (PicoOpt&8)>>3);\r
-               sndout_oss_setvol(currentConfig.volume, currentConfig.volume);\r
-               PicoWriteSound = updateSound;\r
-               plat_update_volume(0, 0);\r
+\r
                memset(sndBuffer, 0, sizeof(sndBuffer));\r
                PsndOut = sndBuffer;\r
+               PicoWriteSound = updateSound;\r
                PsndRate_old = PsndRate;\r
                PicoOpt_old  = PicoOpt;\r
                pal_old = Pico.m.pal;\r
+               plat_update_volume(0, 0);\r
+\r
+               frame_samples = PsndLen;\r
+               snd_excess_add = ((PsndRate - PsndLen * target_fps)<<16) / target_fps;\r
+               if (snd_excess_add != 0)\r
+                       frame_samples++;\r
+               if (soc == SOCID_POLLUX)\r
+                       frame_samples *= 2;     /* force larger buffer */\r
+\r
+               printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",\r
+                       PsndRate, PsndLen, snd_excess_add, is_stereo, Pico.m.pal);\r
+               sndout_oss_setvol(currentConfig.volume, currentConfig.volume);\r
+               sndout_oss_start(snd_rate_oss, frame_samples, is_stereo);\r
+\r
+               /* Wiz's sound hardware needs more prebuffer */\r
+               if (soc == SOCID_POLLUX)\r
+                       updateSound(frame_samples);\r
        }\r
 }\r
 \r
 void pemu_sound_stop(void)\r
 {\r
+       /* get back from Wiz pain */\r
+       switch (PsndRate) {\r
+               case 44171: PsndRate = 44100; break;\r
+               case 22086: PsndRate = 22050; break;\r
+               case 11043: PsndRate = 11025; break;\r
+               default: break;\r
+       }\r
 }\r
 \r
 void pemu_sound_wait(void)\r
@@ -626,14 +678,6 @@ void pemu_sound_wait(void)
 }\r
 \r
 \r
-static void SkipFrame(int do_audio)\r
-{\r
-       PicoSkipFrame=do_audio ? 1 : 2;\r
-       PicoFrame();\r
-       PicoSkipFrame=0;\r
-}\r
-\r
-\r
 void pemu_forced_frame(int opts)\r
 {\r
        int po_old = PicoOpt;\r
@@ -657,23 +701,27 @@ void plat_debug_cat(char *str)
 {\r
 }\r
 \r
-static void simpleWait(int thissec, int lim_time)\r
+void pemu_video_mode_change(int is_32col, int is_240_lines)\r
 {\r
-       struct timeval tval;\r
-\r
-       spend_cycles(1024);\r
-       gettimeofday(&tval, 0);\r
-       if (thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
-\r
-       while (tval.tv_usec < lim_time)\r
-       {\r
-               spend_cycles(1024);\r
-               gettimeofday(&tval, 0);\r
-               if (thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
+       int scalex = 320;\r
+       osd_fps_x = OSD_FPS_X;\r
+       if (is_32col && (PicoOpt & POPT_DIS_32C_BORDER)) {\r
+               scalex = 256;\r
+               osd_fps_x = OSD_FPS_X - 64;\r
        }\r
+       /* want vertical scaling and game is not in 240 line mode */\r
+       if (currentConfig.scaling == EOPT_SCALE_HW_HV && !is_240_lines)\r
+               gp2x_video_RGB_setscaling(8, scalex, 224);\r
+       else\r
+               gp2x_video_RGB_setscaling(0, scalex, 240);\r
+\r
+       // clear whole screen in all buffers\r
+       if ((PicoOpt & POPT_ALT_RENDERER) || !(currentConfig.EmuOpt & EOPT_16BPP))\r
+               gp2x_memset_all_buffers(0, 0xe0, 320*240);\r
+       else\r
+               gp2x_memset_all_buffers(0, 0, 320*240*2);\r
 }\r
 \r
-\r
 #if 0\r
 static void tga_dump(void)\r
 {\r
@@ -722,19 +770,13 @@ static void tga_dump(void)
 }\r
 #endif\r
 \r
-\r
-void pemu_loop(void)\r
+void pemu_loop_prep(void)\r
 {\r
        static int gp2x_old_clock = -1, EmuOpt_old = 0, pal_old = 0;\r
        static int gp2x_old_gamma = 100;\r
-       char fpsbuff[24]; // fps count c string\r
-       struct timeval tval; // timing\r
-       int pframes_done, pframes_shown, pthissec; // "period" frames, used for sync\r
-       int  frames_done,  frames_shown,  thissec; // actual frames\r
-       int oldmodes = 0, target_fps, target_frametime, lim_time, vsync_offset, i;\r
-       char *notice = 0;\r
+       gp2x_soc_t soc;\r
 \r
-       printf("entered emu_Loop()\n");\r
+       soc = soc_detect();\r
 \r
        if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_RAM_TIMINGS) {\r
                if (currentConfig.EmuOpt & EOPT_RAM_TIMINGS)\r
@@ -752,16 +794,16 @@ void pemu_loop(void)
                printf(" done\n");\r
        }\r
 \r
-       if (gp2x_old_gamma != currentConfig.gamma || (EmuOpt_old&0x1000) != (currentConfig.EmuOpt&0x1000)) {\r
-               set_lcd_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r
+       if (gp2x_old_gamma != currentConfig.gamma || ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_A_SN_GAMMA)) {\r
+               set_lcd_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt & EOPT_A_SN_GAMMA));\r
                gp2x_old_gamma = currentConfig.gamma;\r
                printf("updated gamma to %i, A_SN's curve: %i\n", currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r
        }\r
 \r
-       if (((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_PSYNC) || Pico.m.pal != pal_old) {\r
-               if (currentConfig.EmuOpt & EOPT_PSYNC)\r
+       if (((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_VSYNC) || Pico.m.pal != pal_old) {\r
+               if ((currentConfig.EmuOpt & EOPT_VSYNC) || soc == SOCID_POLLUX)\r
                        set_lcd_custom_rate(Pico.m.pal);\r
-               else\r
+               else if (EmuOpt_old & EOPT_VSYNC)\r
                        unset_lcd_custom_rate();\r
        }\r
 \r
@@ -770,206 +812,34 @@ void pemu_loop(void)
 \r
        EmuOpt_old = currentConfig.EmuOpt;\r
        pal_old = Pico.m.pal;\r
-       fpsbuff[0] = 0;\r
 \r
        // make sure we are in correct mode\r
        vidResetMode();\r
        scaling_update();\r
-       Pico.m.dirtyPal = 1;\r
-       oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;\r
-\r
-       // pal/ntsc might have changed, reset related stuff\r
-       target_fps = Pico.m.pal ? 50 : 60;\r
-       target_frametime = 1000000/target_fps;\r
-       reset_timing = 1;\r
 \r
        pemu_sound_start();\r
+}\r
 \r
-       // prepare CD buffer\r
-       if (PicoAHW & PAHW_MCD) PicoCDBufferInit();\r
-\r
-       // calc vsync offset to sync timing code with vsync\r
-       if (currentConfig.EmuOpt & EOPT_PSYNC) {\r
-               gettimeofday(&tval, 0);\r
-               gp2x_video_wait_vsync();\r
-               gettimeofday(&tval, 0);\r
-               vsync_offset = tval.tv_usec;\r
-               while (vsync_offset >= target_frametime)\r
-                       vsync_offset -= target_frametime;\r
-               if (!vsync_offset) vsync_offset++;\r
-               printf("vsync_offset: %i\n", vsync_offset);\r
-       } else\r
-               vsync_offset = 0;\r
-\r
-       frames_done = frames_shown = thissec =\r
-       pframes_done = pframes_shown = pthissec = 0;\r
-\r
-       // loop\r
-       while (engineState == PGS_Running)\r
-       {\r
-               int modes;\r
-\r
-               gettimeofday(&tval, 0);\r
-               if (reset_timing) {\r
-                       reset_timing = 0;\r
-                       pthissec = tval.tv_sec;\r
-                       pframes_shown = pframes_done = tval.tv_usec/target_frametime;\r
-               }\r
-\r
-               // show notice message?\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
-                               clearArea(0);\r
-                               notice = 0;\r
-                       } else {\r
-                               int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];\r
-                               if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }\r
-                               notice = noticeMsg;\r
-                       }\r
-               }\r
-\r
-               // check for mode changes\r
-               modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);\r
-               if (modes != oldmodes)\r
-               {\r
-                       int scalex = 320;\r
-                       osd_fps_x = OSD_FPS_X;\r
-                       if (!(modes & 4) && (PicoOpt & POPT_DIS_32C_BORDER)) {\r
-                               scalex = 256;\r
-                               osd_fps_x = OSD_FPS_X - 64;\r
-                       }\r
-                       /* want vertical scaling and game is not in 240 line mode */\r
-                       if (currentConfig.scaling == EOPT_SCALE_HW_HV && !(modes&8))\r
-                            gp2x_video_RGB_setscaling(8, scalex, 224);\r
-                       else gp2x_video_RGB_setscaling(0, scalex, 240);\r
-                       oldmodes = modes;\r
-                       clearArea(1);\r
-               }\r
-\r
-               // second changed?\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
-                               bench = 0;\r
-                               bench_fps_s = bench_fps;\r
-                               bf[bfp++ & 3] = bench_fps;\r
-                               bench_fps = 0;\r
-                       }\r
-                       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
-                               sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);\r
-                               if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }\r
-                       }\r
-#endif\r
-                       frames_shown = frames_done = 0;\r
-                       thissec = tval.tv_sec;\r
-               }\r
-#ifdef PFRAMES\r
-               sprintf(fpsbuff, "%i", Pico.m.frame_count);\r
-#endif\r
-\r
-               if (pthissec != tval.tv_sec)\r
-               {\r
-                       if (PsndOut == 0 && currentConfig.Frameskip >= 0) {\r
-                               pframes_done = pframes_shown = 0;\r
-                       } else {\r
-                               // it is quite common for this implementation to leave 1 fame unfinished\r
-                               // when second changes, but we don't want buffer to starve.\r
-                               if (PsndOut && pframes_done < target_fps && pframes_done > target_fps-5) {\r
-                                       emu_update_input();\r
-                                       SkipFrame(1); pframes_done++;\r
-                               }\r
-\r
-                               pframes_done  -= target_fps; if (pframes_done  < 0) pframes_done  = 0;\r
-                               pframes_shown -= target_fps; if (pframes_shown < 0) pframes_shown = 0;\r
-                               if (pframes_shown > pframes_done) pframes_shown = pframes_done;\r
-                       }\r
-                       pthissec = tval.tv_sec;\r
-               }\r
-\r
-               lim_time = (pframes_done+1) * target_frametime + vsync_offset;\r
-               if (currentConfig.Frameskip >= 0) // frameskip enabled\r
-               {\r
-                       for(i = 0; i < currentConfig.Frameskip; i++) {\r
-                               emu_update_input();\r
-                               SkipFrame(1); pframes_done++; frames_done++;\r
-                               if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled\r
-                                       gettimeofday(&tval, 0);\r
-                                       if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;\r
-                                       if (tval.tv_usec < lim_time) { // we are too fast\r
-                                               simpleWait(pthissec, lim_time);\r
-                                       }\r
-                               }\r
-                               lim_time += target_frametime;\r
-                       }\r
-               }\r
-               else if (tval.tv_usec > lim_time) // auto frameskip\r
-               {\r
-                       // no time left for this frame - skip\r
-                       if (tval.tv_usec - lim_time >= 300000) {\r
-                               /* something caused a slowdown for us (disk access? cache flush?)\r
-                                * try to recover by resetting timing... */\r
-                               reset_timing = 1;\r
-                               continue;\r
-                       }\r
-                       emu_update_input();\r
-                       SkipFrame(tval.tv_usec < lim_time+target_frametime*2); pframes_done++; frames_done++;\r
-                       continue;\r
-               }\r
-\r
-               emu_update_input();\r
-               PicoFrame();\r
-\r
-               // check time\r
-               gettimeofday(&tval, 0);\r
-               if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;\r
-\r
-               if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 300000) // slowdown detection\r
-                       reset_timing = 1;\r
-               else if (PsndOut != NULL || currentConfig.Frameskip < 0)\r
-               {\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 (!reset_timing && tval.tv_usec < lim_time)\r
-                       {\r
-                               // we are too fast\r
-                               if (vsync_offset) {\r
-                                       if (lim_time - tval.tv_usec > target_frametime/2)\r
-                                               simpleWait(pthissec, lim_time - target_frametime/4);\r
-                                       gp2x_video_wait_vsync();\r
-                               } else {\r
-                                       simpleWait(pthissec, lim_time);\r
-                               }\r
-                       }\r
-               }\r
-\r
-               blit(fpsbuff, notice);\r
-\r
-               pframes_done++; pframes_shown++;\r
-                frames_done++;  frames_shown++;\r
-       }\r
+void pemu_loop_end(void)\r
+{\r
+       int po_old = PicoOpt;\r
+       int eo_old = currentConfig.EmuOpt;\r
 \r
-       emu_set_fastforward(0);\r
+       pemu_sound_stop();\r
 \r
-       if (PicoAHW & PAHW_MCD)\r
-               PicoCDBufferFree();\r
+       /* do one more frame for menu bg */\r
+       PicoOpt &= ~POPT_ALT_RENDERER;\r
+       PicoOpt |= POPT_EN_SOFTSCALE|POPT_ACC_SPRITES;\r
+       currentConfig.EmuOpt |= EOPT_16BPP;\r
 \r
-       // save SRAM\r
-       if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) {\r
-               plat_status_msg_busy_first("Writing SRAM/BRAM...");\r
-               emu_save_load_game(0, 1);\r
-               SRam.changed = 0;\r
-       }\r
+       PicoScanBegin = EmuScanBegin16;\r
+       PicoScanEnd = NULL;\r
+       PicoDrawSetColorFormat(1);\r
+       Pico.m.dirtyPal = 1;\r
+       PicoFrame();\r
 \r
-       // do menu background to be sure it's right\r
-       pemu_forced_frame(POPT_EN_SOFTSCALE);\r
+       PicoOpt = po_old;\r
+       currentConfig.EmuOpt = eo_old;\r
 }\r
 \r
 const char *plat_get_credits(void)\r