perfect vsync
authornotaz <notasas@gmail.com>
Wed, 8 Aug 2007 19:49:11 +0000 (19:49 +0000)
committernotaz <notasas@gmail.com>
Wed, 8 Aug 2007 19:49:11 +0000 (19:49 +0000)
git-svn-id: file:///home/notaz/opt/svn/PicoDrive@218 be3aeb3a-fb24-0410-a615-afba39da0efa

platform/gp2x/emu.c
platform/gp2x/emu.h
platform/gp2x/gp2x.c
platform/gp2x/menu.c

index eca0dc4..36d956a 100644 (file)
@@ -1063,11 +1063,11 @@ static void simpleWait(int thissec, int lim_time)
 void emu_Loop(void)\r
 {\r
        static int gp2x_old_clock = 200;\r
-       static int PsndRate_old = 0, PicoOpt_old = 0, PsndLen_real = 0, pal_old = 0;\r
+       static int PsndRate_old = 0, PicoOpt_old = 0, EmuOpt_old = 0, PsndLen_real = 0, pal_old = 0;\r
        char fpsbuff[24]; // fps count c string\r
        struct timeval tval; // timing\r
        int thissec = 0, frames_done = 0, frames_shown = 0, oldmodes = 0;\r
-       int target_fps, target_frametime, lim_time, i;\r
+       int target_fps, target_frametime, lim_time, vsync_offset, i;\r
        char *notice = 0;\r
 \r
        printf("entered emu_Loop()\n");\r
@@ -1085,6 +1085,13 @@ void emu_Loop(void)
                printf("updated gamma to %i\n", currentConfig.gamma);\r
        }\r
 \r
+       if ((EmuOpt_old&0x2000) != (currentConfig.EmuOpt&0x2000)) {\r
+               if (currentConfig.EmuOpt&0x2000)\r
+                    set_LCD_custom_rate(Pico.m.pal ? LCDR_100 : LCDR_120);\r
+               else unset_LCD_custom_rate();\r
+       }\r
+\r
+       EmuOpt_old = currentConfig.EmuOpt;\r
        fpsbuff[0] = 0;\r
 \r
        // make sure we are in correct mode\r
@@ -1128,6 +1135,19 @@ void emu_Loop(void)
        // prepare CD buffer\r
        if (PicoMCD & 1) PicoCDBufferInit();\r
 \r
+       // calc vsync offset to sync timing code with vsync\r
+       if (currentConfig.EmuOpt&0x2000) {\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
        // loop?\r
        while (engineState == PGS_Running)\r
        {\r
@@ -1210,10 +1230,8 @@ void emu_Loop(void)
                                if (frames_shown > frames_done) frames_shown = frames_done;\r
                        }\r
                }\r
-#if 0\r
-               sprintf(fpsbuff, "%05i", Pico.m.frame_count);\r
-#endif\r
-               lim_time = (frames_done+1) * target_frametime;\r
+\r
+               lim_time = (frames_done+1) * target_frametime + vsync_offset;\r
                if(currentConfig.Frameskip >= 0) { // frameskip enabled\r
                        for(i = 0; i < currentConfig.Frameskip; i++) {\r
                                updateKeys();\r
@@ -1309,12 +1327,18 @@ if (Pico.m.frame_count == 31563) {
                        reset_timing = 1;\r
                else if (PsndOut != NULL || currentConfig.Frameskip < 0)\r
                {\r
-                       // sleep if we are still too fast\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
                        {\r
                                // we are too fast\r
-                               simpleWait(thissec, lim_time);\r
+                               if (vsync_offset) {\r
+                                       if (lim_time - tval.tv_usec > target_frametime/2)\r
+                                               simpleWait(thissec, lim_time - target_frametime/4);\r
+                                       gp2x_video_wait_vsync();\r
+                               } else {\r
+                                       simpleWait(thissec, lim_time);\r
+                               }\r
                        }\r
                }\r
 \r
index 75a0322..17e855b 100644 (file)
@@ -21,7 +21,7 @@ typedef struct {
        int EmuOpt;             // LSb->MSb: use_sram, show_fps, enable_sound, gzip_saves,\r
                                        // squidgehack, no_save_cfg_on_exit, <unused>, 16_bit_mode\r
                                        // craigix_ram, confirm_save, show_cd_leds, confirm_load\r
-                                       //\r
+                                       // A_SNs_gamma, perfect_vsync\r
        int PicoOpt;  // used for config saving only, see Pico.h\r
        int PsndRate; // ditto\r
        int PicoRegion; // ditto\r
index f138be6..d6a701c 100644 (file)
@@ -34,6 +34,7 @@
 \r
 #include "gp2x.h"\r
 #include "usbjoy.h"\r
+#include "asmutils.h"\r
 \r
 volatile unsigned short *gp2x_memregs;\r
 //static\r
@@ -149,11 +150,10 @@ void gp2x_video_RGB_setscaling(int ln_offs, int W, int H)
 }\r
 \r
 \r
-/* LCD updates @ 80Hz? */\r
 void gp2x_video_wait_vsync(void)\r
 {\r
-       gp2x_memregs[0x2846>>1] = 0x20|2; //(gp2x_memregs[0x2846>>1] | 0x20) & ~2;\r
-       while(!(gp2x_memregs[0x2846>>1] & 2));// usleep(1);\r
+       unsigned short v = gp2x_memregs[0x1182>>1];\r
+       while (!((v ^ gp2x_memregs[0x1182>>1]) & 0x10)) spend_cycles(1024);\r
 }\r
 \r
 \r
index 3386a95..0b8264e 100644 (file)
@@ -597,7 +597,8 @@ static void draw_savestate_menu(int menu_sel, int is_loading)
 \r
 static int savestate_menu_loop(int is_loading)\r
 {\r
-       int menu_sel = 10, menu_sel_max = 10;\r
+       static int menu_sel = 10;\r
+       int menu_sel_max = 10;\r
        unsigned long inp = 0;\r
 \r
        state_check_slots();\r
@@ -820,7 +821,8 @@ static void draw_cd_menu_options(int menu_sel, char *b_us, char *b_eu, char *b_j
 \r
 static void cd_menu_loop_options(void)\r
 {\r
-       int menu_sel = 0, menu_sel_max = 10;\r
+       static int menu_sel = 0;\r
+       int menu_sel_max = 10;\r
        unsigned long inp = 0;\r
        char bios_us[32], bios_eu[32], bios_jp[32], *bios, *p;\r
 \r
@@ -897,7 +899,7 @@ static void cd_menu_loop_options(void)
 \r
 static void draw_amenu_options(int menu_sel)\r
 {\r
-       int tl_x = 25, tl_y = 60, y;\r
+       int tl_x = 25, tl_y = 50, y;\r
        char *mms = mmuhack_status ? "active)  " : "inactive)";\r
 \r
        y = tl_y;\r
@@ -905,14 +907,16 @@ static void draw_amenu_options(int menu_sel)
        gp2x_pd_clone_buffer2();\r
 \r
        gp2x_text_out8(tl_x, y,       "Gamma correction           %i.%02i", currentConfig.gamma / 100, currentConfig.gamma%100); // 0\r
-       gp2x_text_out8(tl_x, (y+=10), "Emulate Z80                %s", (currentConfig.PicoOpt&0x004)?"ON":"OFF"); // 1\r
-       gp2x_text_out8(tl_x, (y+=10), "Emulate YM2612 (FM)        %s", (currentConfig.PicoOpt&0x001)?"ON":"OFF"); // 2\r
-       gp2x_text_out8(tl_x, (y+=10), "Emulate SN76496 (PSG)      %s", (currentConfig.PicoOpt&0x002)?"ON":"OFF"); // 3\r
-       gp2x_text_out8(tl_x, (y+=10), "gzip savestates            %s", (currentConfig.EmuOpt &0x008)?"ON":"OFF"); // 4\r
-       gp2x_text_out8(tl_x, (y+=10), "Don't save last used ROM   %s", (currentConfig.EmuOpt &0x020)?"ON":"OFF"); // 5\r
+       gp2x_text_out8(tl_x, (y+=10), "A_SN's gamma curve         %s", (currentConfig.EmuOpt &0x1000)?"ON":"OFF");\r
+       gp2x_text_out8(tl_x, (y+=10), "Perfecf vsync              %s", (currentConfig.EmuOpt &0x2000)?"ON":"OFF");\r
+       gp2x_text_out8(tl_x, (y+=10), "Emulate Z80                %s", (currentConfig.PicoOpt&0x0004)?"ON":"OFF");\r
+       gp2x_text_out8(tl_x, (y+=10), "Emulate YM2612 (FM)        %s", (currentConfig.PicoOpt&0x0001)?"ON":"OFF");\r
+       gp2x_text_out8(tl_x, (y+=10), "Emulate SN76496 (PSG)      %s", (currentConfig.PicoOpt&0x0002)?"ON":"OFF"); // 5\r
+       gp2x_text_out8(tl_x, (y+=10), "gzip savestates            %s", (currentConfig.EmuOpt &0x0008)?"ON":"OFF");\r
+       gp2x_text_out8(tl_x, (y+=10), "Don't save last used ROM   %s", (currentConfig.EmuOpt &0x0020)?"ON":"OFF");\r
        gp2x_text_out8(tl_x, (y+=10), "needs restart:");\r
-       gp2x_text_out8(tl_x, (y+=10), "craigix's RAM timings      %s", (currentConfig.EmuOpt &0x100)?"ON":"OFF"); // 7\r
-       gp2x_text_out8(tl_x, (y+=10), "squidgehack (now %s %s",   mms, (currentConfig.EmuOpt &0x010)?"ON":"OFF"); // 8\r
+       gp2x_text_out8(tl_x, (y+=10), "craigix's RAM timings      %s", (currentConfig.EmuOpt &0x0100)?"ON":"OFF");\r
+       gp2x_text_out8(tl_x, (y+=10), "squidgehack (now %s %s",   mms, (currentConfig.EmuOpt &0x0010)?"ON":"OFF"); // 10\r
        gp2x_text_out8(tl_x, (y+=10), "Done");\r
 \r
        // draw cursor\r
@@ -923,7 +927,8 @@ static void draw_amenu_options(int menu_sel)
 \r
 static void amenu_loop_options(void)\r
 {\r
-       int menu_sel = 0, menu_sel_max = 9;\r
+       static int menu_sel = 0;\r
+       int menu_sel_max = 11;\r
        unsigned long inp = 0;\r
 \r
        for(;;)\r
@@ -934,14 +939,16 @@ static void amenu_loop_options(void)
                if(inp & GP2X_DOWN) { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }\r
                if((inp& GP2X_B)||(inp&GP2X_LEFT)||(inp&GP2X_RIGHT)) { // toggleable options\r
                        switch (menu_sel) {\r
-                               case  1: currentConfig.PicoOpt^=0x004; break;\r
-                               case  2: currentConfig.PicoOpt^=0x001; break;\r
-                               case  3: currentConfig.PicoOpt^=0x002; break;\r
-                               case  4: currentConfig.EmuOpt ^=0x008; break;\r
-                               case  5: currentConfig.EmuOpt ^=0x020; break;\r
-                               case  7: currentConfig.EmuOpt ^=0x100; break;\r
-                               case  8: currentConfig.EmuOpt ^=0x010; break;\r
-                               case  9: return;\r
+                               case  1: currentConfig.EmuOpt ^=0x1000; break;\r
+                               case  2: currentConfig.EmuOpt ^=0x2000; break;\r
+                               case  3: currentConfig.PicoOpt^=0x0004; break;\r
+                               case  4: currentConfig.PicoOpt^=0x0001; break;\r
+                               case  5: currentConfig.PicoOpt^=0x0002; break;\r
+                               case  6: currentConfig.EmuOpt ^=0x0008; break;\r
+                               case  7: currentConfig.EmuOpt ^=0x0020; break;\r
+                               case  9: currentConfig.EmuOpt ^=0x0100; break;\r
+                               case 10: currentConfig.EmuOpt ^=0x0010; break;\r
+                               case 11: return;\r
                        }\r
                }\r
                if(inp & (GP2X_X|GP2X_A)) return;\r
@@ -1098,7 +1105,8 @@ static void menu_options_save(void)
 \r
 static int menu_loop_options(void)\r
 {\r
-       int menu_sel = 0, menu_sel_max = 17;\r
+       static int menu_sel = 0;\r
+       int menu_sel_max = 17;\r
        unsigned long inp = 0;\r
 \r
        if (rom_data) menu_sel_max++;\r