]> git.sesse.net Git - pistorm/blobdiff - platforms/amiga/rtg/rtg.c
Rework RTG WaitVerticalSync a bit
[pistorm] / platforms / amiga / rtg / rtg.c
index 91e4e772695fbaa88ef9dfd552ae30ee218a0f74..80c75380281d520aa33a1cd687ca335a6e81b9dd 100644 (file)
@@ -23,7 +23,7 @@ uint32_t rtg_address[8];
 uint32_t rtg_address_adj[8];
 uint32_t rtg_rgb[8];
 
-static uint8_t display_enabled = 0xFF;
+uint8_t display_enabled = 0xFF;
 
 uint16_t rtg_display_width, rtg_display_height;
 uint16_t rtg_display_format;
@@ -41,7 +41,7 @@ static void handle_irtg_command(uint32_t cmd);
 uint8_t realtime_graphics_debug = 0;
 extern int cpu_emulation_running;
 extern struct emulator_config *cfg;
-extern uint8_t rtg_on;
+extern uint8_t rtg_on, rtg_enabled, rtg_output_in_vblank;
 
 //#define DEBUG_RTG
 
@@ -53,12 +53,12 @@ extern uint8_t rtg_on;
     "MEM",
 };*/
 
-/*static const char *rtg_format_names[RTGFMT_NUM] = {
+static const char *rtg_format_names[RTGFMT_NUM] = {
     "8BPP CLUT",
     "16BPP RGB (565)",
     "32BPP RGB (RGBA)",
     "15BPP RGB (555)",
-};*/
+};
 #define DEBUG printf
 #else
 #define DEBUG(...)
@@ -79,10 +79,10 @@ int init_rtg_data(struct emulator_config *cfg_) {
 void shutdown_rtg() {
     printf("[RTG] Shutting down RTG.\n");
     if (rtg_on) {
+        display_enabled = 0xFF;
         rtg_on = 0;
     }
     if (rtg_mem) {
-        
         free(rtg_mem);
         rtg_mem = NULL;
     }
@@ -92,11 +92,12 @@ unsigned int rtg_get_fb() {
     return PIGFX_RTG_BASE + PIGFX_REG_SIZE + framebuffer_addr_adj;
 }
 
+uint8_t wait_vblank = 0;
+uint32_t wait_rtg_frame = 0;
+extern uint32_t cur_rtg_frame;
+
 unsigned int rtg_read(uint32_t address, uint8_t mode) {
     //printf("%s read from RTG: %.8X\n", op_type_names[mode], address);
-    if (address == RTG_COMMAND) {
-        return 0xFFCF;
-    }
     if (address >= PIGFX_REG_SIZE) {
         if (rtg_mem && (address - PIGFX_REG_SIZE) < PIGFX_UPPER) {
             switch (mode) {
@@ -114,6 +115,33 @@ unsigned int rtg_read(uint32_t address, uint8_t mode) {
             }
         }
     }
+    switch (address) {
+        case RTG_COMMAND:
+            return rtg_enabled ? 0xFFCF : 0x0000;
+        case RTG_WAITVSYNC:
+            if (rtg_on) {
+                if (!wait_vblank && cur_rtg_frame != wait_rtg_frame) {
+                    wait_rtg_frame = cur_rtg_frame;
+                    if (wait_rtg_frame == 0) {
+                        wait_rtg_frame = cur_rtg_frame;
+                    }
+                    if (wait_rtg_frame == 0)
+                        printf("Wait RTG frame was zero!\n");
+                    wait_vblank = 1;
+                }
+                if (cur_rtg_frame != wait_rtg_frame && wait_vblank) {
+                    wait_vblank = 0;
+                    return 1;
+                }
+                else
+                    return 0;
+            }
+            // fallthrough
+        case RTG_INVBLANK:
+            return !rtg_on || rtg_output_in_vblank;
+        default:
+            break;
+    }
 
     return 0;
 }
@@ -483,6 +511,7 @@ static void handle_rtg_command(uint32_t cmd) {
   //printf("Handling RTG command %d (%.8X)\n", cmd, cmd);
     switch (cmd) {
         case RTGCMD_SETGC:
+            gdebug("SetGC\n");
             rtg_display_format = rtg_format;
             rtg_display_width = rtg_x[0];
             rtg_display_height = rtg_y[0];
@@ -496,9 +525,13 @@ static void handle_rtg_command(uint32_t cmd) {
                 framebuffer_addr_adj = framebuffer_addr + (rtg_offset_x << rtg_display_format) + (rtg_offset_y * rtg_pitch);
                 rtg_total_rows = rtg_y[1];
             }
-            //printf("Set RTG mode:\n");
-            //printf("%dx%d pixels\n", rtg_display_width, rtg_display_height);
-            //printf("Pixel format: %s\n", rtg_format_names[rtg_display_format]);
+            if (realtime_graphics_debug) {
+                printf("Set RTG mode:\n");
+                printf("%dx%d pixels\n", rtg_display_width, rtg_display_height);
+#ifdef DEBUG_RTG
+                printf("Pixel format: %s\n", rtg_format_names[rtg_display_format]);
+#endif
+            }
             break;
         case RTGCMD_SETPAN:
             //printf("Command: SetPan.\n");
@@ -516,19 +549,23 @@ static void handle_rtg_command(uint32_t cmd) {
             break;
         }
         case RTGCMD_SETDISPLAY:
-            //printf("RTG SetDisplay %s\n", (rtg_u8[1]) ? "enabled" : "disabled");
-            // I remeber wrongs.
-            //printf("Command: SetDisplay.\n");
+            gdebug("SetDisplay\n");
+            if (realtime_graphics_debug) {
+                printf("RTG SetDisplay %s\n", (rtg_u8[1]) ? "enabled" : "disabled");
+            }
             break;
         case RTGCMD_ENABLE:
         case RTGCMD_SETSWITCH:
-            //printf("RTG SetSwitch %s\n", ((rtg_x[0]) & 0x01) ? "enabled" : "disabled");
-            //printf("LAL: %.4X\n", rtg_x[0]);
-            if (display_enabled != ((rtg_x[0]) & 0x01)) {
-                display_enabled = ((rtg_x[0]) & 0x01);
-                if (display_enabled) {
+            gdebug("SetSwitch\n");
+            if (realtime_graphics_debug) {
+                printf("RTG SetSwitch %s\n", ((rtg_x[0]) & 0x01) ? "enabled" : "disabled");
+                printf("LAL: %.4X\n", rtg_x[0]);
+            }
+            display_enabled = ((rtg_x[0]) & 0x01);
+            if (display_enabled != rtg_on) {
+                rtg_on = display_enabled;
+                if (rtg_on)
                     rtg_init_display();
-                }
                 else
                     rtg_shutdown_display();
             }