Browse Source

Remove all the complicated (even if working) stuff to use vsync instead to sync NTSC at 60Hz!

Godzil 4 years ago
parent
commit
9fb3a09f05
4 changed files with 63 additions and 41 deletions
  1. 1 0
      src/include/os_dependent.h
  2. 2 39
      src/main.c
  3. 9 2
      src/os/unix/graphics.c
  4. 51 0
      src/os/unix/graphics_dummy.c

+ 1 - 0
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
 {

+ 2 - 39
src/main.c

@@ -18,9 +18,9 @@
 #include <fcntl.h>
 #include <unistd.h>
 
-#include <sys/mman.h>
 #include <sys/time.h>
 #include <time.h>
+#include <sys/mman.h>
 #include <ctype.h>
 
 #include <GLFW/glfw3.h>
@@ -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 */

+ 9 - 2
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);
 }

+ 51 - 0
src/os/unix/graphics_dummy.c

@@ -11,6 +11,10 @@
 #include <string.h>
 #include <stdint.h>
 
+#include <sys/time.h>
+#include <time.h>
+
+
 #include <os_dependent.h>
 
 #include <GLFW/glfw3.h>
@@ -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;
+    }
+}