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