move emu loop to common; redo timing; add pollux timer
[libpicofe.git] / gp2x / emu.c
index f17e53a..372ce56 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
 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 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
@@ -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,16 @@ 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
 \r
        /* assumption: msg_busy_next gets called only when\r
         * something slow is about to happen */\r
@@ -494,11 +505,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,7 +571,7 @@ 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
@@ -576,10 +587,10 @@ 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
+       if (PicoOpt & POPT_EN_STEREO)\r
+               len <<= 1;\r
 \r
        /* avoid writing audio when lagging behind to prevent audio lag */\r
        if (PicoSkipFrame != 2)\r
@@ -626,14 +637,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 +660,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 +729,10 @@ 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
-\r
-       printf("entered emu_Loop()\n");\r
 \r
        if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_RAM_TIMINGS) {\r
                if (currentConfig.EmuOpt & EOPT_RAM_TIMINGS)\r
@@ -770,206 +768,16 @@ 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
-\r
-       emu_set_fastforward(0);\r
-\r
-       if (PicoAHW & PAHW_MCD)\r
-               PicoCDBufferFree();\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
-\r
-       // do menu background to be sure it's right\r
-       pemu_forced_frame(POPT_EN_SOFTSCALE);\r
+void pemu_loop_end(void)\r
+{\r
 }\r
 \r
 const char *plat_get_credits(void)\r