diff --git a/src/include/os_dependent.h b/src/include/os_dependent.h index a2a3461..9300586 100644 --- a/src/include/os_dependent.h +++ b/src/include/os_dependent.h @@ -18,6 +18,7 @@ int graphics_init(); int graphics_drawpixel(long x, long y, long color); int graphics_blit(long x, long y, long w, long h); int graphics_drawline(long x, long y, long x1, long y1, long color); +void vsync(void); typedef struct Palette_t { diff --git a/src/main.c b/src/main.c index 59cfc63..7baa2bc 100755 --- a/src/main.c +++ b/src/main.c @@ -18,9 +18,9 @@ #include #include -#include #include #include +#include #include #include @@ -847,8 +847,6 @@ void Loop6502(quick6502_cpu *R) { quick6502_signal cpuSignal; // short skey; - long WaitTime; - static long delta = 0; cpuSignal = Q6502_NO_SIGNAL; @@ -875,47 +873,12 @@ void Loop6502(quick6502_cpu *R) frame++; SZHit = -1; IRQScanHit = -1; - /* Sync at 60FPS */ - /* Get current time in microseconds */ - gettimeofday(&timeEnd, NULL); - WaitTime = (timeEnd.tv_sec) - (timeStart.tv_sec); - WaitTime *= 1000000; - WaitTime += (timeEnd.tv_usec - timeStart.tv_usec); - -#if !ISPAL && ISNTSC - /* Calculate the waiting time, 16666 is the time of one frame in microseconds at a 60Hz rate) */ - WaitTime = 16666 - WaitTime + delta; -#elif ISPAL && !ISNTSC - WaitTime = 20000 - WaitTime + delta; -#endif - - /* If we press Page Up, we want to accelerate "time" */ -#ifndef RUN_COVERAGE if (!getKeyStatus('Y')) { - if ((WaitTime >= 0) && (WaitTime < 100000)) - { - usleep(WaitTime); - } + vsync(); } -#endif - /* Now get the time after sleep */ - gettimeofday(&timeStart, NULL); - - /* Now calculate How many microseconds we really spend in sleep and - calculate a delta for next iteration */ - delta = (timeStart.tv_sec) - (timeEnd.tv_sec); - delta *= 1000000; - delta += (timeStart.tv_usec - timeEnd.tv_usec); - delta = WaitTime - delta; - - /* To avoid strange time warp when stopping emulation or using acceleration a lot */ - if ((delta > 10000) || (delta < -10000)) - { - delta = 0; - } } /* There is Two dummy scanline */ diff --git a/src/os/unix/graphics.c b/src/os/unix/graphics.c index 09aada5..8659a5d 100644 --- a/src/os/unix/graphics.c +++ b/src/os/unix/graphics.c @@ -194,7 +194,7 @@ void initDisplay(GLWindow *g) glfwMakeContextCurrent(g->windows); setupGL(g, g->WIDTH, g->HEIGHT); - glfwSwapInterval(0); // Disable VSYNC + glfwSwapInterval(1); // We need vsync glfwGetWindowSize(g->windows, &w, &h); @@ -443,11 +443,18 @@ int graphics_drawline(long x, long y, long x1, long y1, long color) int graphics_blit(long x, long y, long w, long h) { - updateScreen(&mainWindow); + /* Just pool for events, no graphic thing to be done ATM */ + glfwPollEvents(); return 0; } int getKeyStatus(int key) { return mainWindow.keyArray[key].curState; +} + +/* Sync with 60Hz (or try to) */ +void vsync(void) +{ + updateScreen(&mainWindow); } \ No newline at end of file diff --git a/src/os/unix/graphics_dummy.c b/src/os/unix/graphics_dummy.c index 5483a49..e3dd364 100644 --- a/src/os/unix/graphics_dummy.c +++ b/src/os/unix/graphics_dummy.c @@ -11,6 +11,10 @@ #include #include +#include +#include + + #include #include @@ -130,3 +134,50 @@ int getKeyStatus(int key) { return 0; } + +/* Sync with 60Hz (or try to) */ +void vsync(void) +{ + long WaitTime; + static long delta = 0; + + /* Try to sync at 60FPS */ + /* Get current time in microseconds */ + gettimeofday(&timeEnd, NULL); + + WaitTime = (timeEnd.tv_sec) - (timeStart.tv_sec); + WaitTime *= 1000000; + WaitTime += (timeEnd.tv_usec - timeStart.tv_usec); + +#if !ISPAL && ISNTSC + /* Calculate the waiting time, 16666 is the time of one frame in microseconds at a 60Hz rate) */ + WaitTime = 16666 - WaitTime + delta; +#elif ISPAL && !ISNTSC + WaitTime = 20000 - WaitTime + delta; +#endif + +#ifndef RUN_COVERAGE + if ((WaitTime >= 0) && (WaitTime < 100000)) + { + usleep(WaitTime); + } +#endif + + /* Now get the time after sleep */ + gettimeofday(&timeStart, NULL); + + /* Now calculate How many microseconds we really spend in sleep and + calculate a delta for next iteration */ + delta = (timeStart.tv_sec) - (timeEnd.tv_sec); + delta *= 1000000; + delta += (timeStart.tv_usec - timeEnd.tv_usec); + delta = WaitTime - delta; + + console_printf(Console_Default, "Delta:%d\n", delta); + + /* To avoid strange time warp when stopping emulation or using acceleration a lot */ + if ((delta > 10000) || (delta < -10000)) + { + delta = 0; + } +} \ No newline at end of file