]> git.sesse.net Git - pistorm/blobdiff - platforms/amiga/rtg/rtg.c
iRTG initial implementation
[pistorm] / platforms / amiga / rtg / rtg.c
index 564dd50adb708480b59175a2d29ed1d2bf819dad..45d8e5761e48af98866a046a3c5b6be5a29db658 100644 (file)
@@ -6,9 +6,12 @@
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
+#include "irtg_structs.h"
 #include "config_file/config_file.h"
 #include "rtg.h"
 
+#include "m68k.h"
+
 uint8_t rtg_u8[4];
 uint16_t rtg_x[8], rtg_y[8];
 uint16_t rtg_user[8];
@@ -30,28 +33,34 @@ uint32_t framebuffer_addr = 0;
 uint32_t framebuffer_addr_adj = 0;
 
 static void handle_rtg_command(uint32_t cmd);
-//static struct timespec f1, f2;
+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;
 
-/*
-static const char *op_type_names[OP_TYPE_NUM] = {
+//#define DEBUG_RTG
+
+#ifdef DEBUG_RTG
+/*static const char *op_type_names[OP_TYPE_NUM] = {
     "BYTE",
     "WORD",
     "LONGWORD",
     "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(...)
+#endif
+
 int init_rtg_data(struct emulator_config *cfg_) {
     rtg_mem = calloc(1, 40 * SIZE_MEGA);
     if (!rtg_mem) {
@@ -142,8 +151,9 @@ void rtg_write(uint32_t address, uint32_t value, uint8_t mode) {
                     return;
             }
         }
-    }
-    else {
+    } else if (address == RTG_DEBUGME) {
+        printf("RTG DEBUGME WRITE: %d.\n", value);
+    } else {
         switch (mode) {
             case OP_TYPE_BYTE:
                 switch (address) {
@@ -171,6 +181,9 @@ void rtg_write(uint32_t address, uint32_t value, uint8_t mode) {
                     case RTG_COMMAND:
                         handle_rtg_command(value);
                         break;
+                    case IRTG_COMMAND:
+                        handle_irtg_command(value);
+                        break;
                 }
                 break;
             case OP_TYPE_LONGWORD:
@@ -196,6 +209,157 @@ void rtg_write(uint32_t address, uint32_t value, uint8_t mode) {
 }
 
 #define gdebug(a) if (realtime_graphics_debug) { printf(a); m68k_end_timeslice(); cpu_emulation_running = 0; }
+#define M68KR(a) m68k_get_reg(NULL, a)
+#define RGBF_D7 rgbf_to_rtg[M68KR(M68K_REG_D7)]
+#define CMD_PITCH be16toh(r->BytesPerRow)
+
+static struct P96RenderInfo *r;
+static struct P96BoardInfo *b;
+static struct P96Line *ln;
+static uint8_t cmd_mask;
+
+static void handle_irtg_command(uint32_t cmd) {
+    b = (struct P96BoardInfo *)get_mapped_data_pointer_by_address(cfg, M68KR(M68K_REG_A0));
+    r = (struct P96RenderInfo *)get_mapped_data_pointer_by_address(cfg, M68KR(M68K_REG_A1));
+
+    switch (cmd) {
+        case RTGCMD_SETPAN: {
+            // A0: struct BoardInfo *b, A1: UBYTE *addr, D0 UWORD width, D1: WORD x_offset, D2: WORD y_offset, D7: RGBFTYPE format
+#ifdef DEBUG_RTG
+            if (realtime_graphics_debug) {
+                printf("iSetPanning begin\n");
+                printf("IRTGCmd SetPanning\n");
+                printf("IRTGCmd x: %d y: %d w: %d (%d)\n", M68KR(M68K_REG_D1), M68KR(M68K_REG_D2), M68KR(M68K_REG_D0) << RGBF_D7, M68KR(M68K_REG_D0));
+                printf("BoardInfo: %.8X Addr: %.8X\n", M68KR(M68K_REG_A0), M68KR(M68K_REG_A1));
+                printf("BoardInfo Xoffs: %d Yoffs: %d\n", be16toh(b->XOffset), be16toh(b->YOffset));
+            }
+#endif
+            if (!b) break;
+
+            b->XOffset = (int16_t)htobe16(M68KR(M68K_REG_D1));
+            b->YOffset = (int16_t)htobe16(M68KR(M68K_REG_D2));
+
+            rtg_offset_x = M68KR(M68K_REG_D1);
+            rtg_offset_y = M68KR(M68K_REG_D2);
+            rtg_pitch = (M68KR(M68K_REG_D0) << RGBF_D7);
+            framebuffer_addr = M68KR(M68K_REG_A1) - (PIGFX_RTG_BASE + PIGFX_REG_SIZE);
+            framebuffer_addr_adj = framebuffer_addr + (rtg_offset_x << RGBF_D7) + (rtg_offset_y * rtg_pitch);
+
+#ifdef DEBUG_RTG
+            if (realtime_graphics_debug) {
+                printf("RTG OffsetX/Y: %d/%d\n", rtg_offset_x, rtg_offset_y);
+                printf("RTG Pitch: %d\n", rtg_pitch);
+                printf("RTG FBAddr/Adj: %.8X (%.8X)/%.8X\n", framebuffer_addr, M68KR(M68K_REG_A1), framebuffer_addr_adj);
+                printf("iSetPanning End\n");
+            }
+#endif
+
+            break;
+        }
+        case RTGCMD_DRAWLINE: {
+            // A0: struct BoardInfo *b, A1: RenderInfo *r A2: struct Line *l, D0: UBYTE mask, D7: RGBFTYPE format
+            gdebug("iDrawLine begin\n");
+            ln = (struct P96Line *)get_mapped_data_pointer_by_address(cfg, M68KR(M68K_REG_A2));
+
+            if (!ln || !r) break;
+
+            cmd_mask = (uint8_t)M68KR(M68K_REG_D0);
+            rtg_address_adj[0] = be32toh(r->_p_Memory) - (PIGFX_RTG_BASE + PIGFX_REG_SIZE);
+
+            if (cmd_mask == 0xFF && be16toh(ln->LinePtrn) == 0xFFFF) {
+                rtg_drawline_solid(be16toh(ln->X), be16toh(ln->Y), be16toh(ln->dX), be16toh(ln->dY),
+                                   be16toh(ln->Length), be32toh(ln->FgPen), CMD_PITCH, RGBF_D7);
+            } else {
+                rtg_drawline(be16toh(ln->X), be16toh(ln->Y), be16toh(ln->dX), be16toh(ln->dY),
+                             be16toh(ln->Length), be16toh(ln->LinePtrn), be16toh(ln->PatternShift),
+                             be32toh(ln->FgPen), be32toh(ln->BgPen), CMD_PITCH,
+                             RGBF_D7, cmd_mask, ln->DrawMode);
+            }
+            gdebug("iDrawLine end\n");
+            break;
+        }
+        case RTGCMD_FILLRECT: {
+            // A0: BoardInfo *b, A1: RenderInfo *r
+            // D0 WORD x, D1: WORD y, D2: WORD w, D3: WORD h
+            // D4: ULONG color, D5: UBYTE mask, D7: RGBFTYPE format
+            gdebug("iFillRect begin\n");
+#ifdef DEBUG_RTG
+            if (realtime_graphics_debug) {
+                DEBUG("X1/X2: %d/%d-> X2/Y2: %d/%d\n", (int16_t)M68KR(M68K_REG_D0), (int16_t)M68KR(M68K_REG_D1), (int16_t)M68KR(M68K_REG_D2), (int16_t)M68KR(M68K_REG_D3));
+                DEBUG("R: %.8X B: %.8X\n", M68KR(M68K_REG_A0), M68KR(M68K_REG_A1));
+            }
+#endif
+
+            if (!b || !r) break;
+
+            cmd_mask = (uint8_t)M68KR(M68K_REG_D5);
+            rtg_address_adj[0] = be32toh(r->_p_Memory) - (PIGFX_RTG_BASE + PIGFX_REG_SIZE);
+
+            if (cmd_mask == 0xFF) {
+                rtg_fillrect_solid((int16_t)M68KR(M68K_REG_D0), (int16_t)M68KR(M68K_REG_D1), (int16_t)M68KR(M68K_REG_D2), (int16_t)M68KR(M68K_REG_D3),
+                                   M68KR(M68K_REG_D4), CMD_PITCH, RGBF_D7);
+            } else {
+                rtg_fillrect((int16_t)M68KR(M68K_REG_D0), (int16_t)M68KR(M68K_REG_D1), (int16_t)M68KR(M68K_REG_D2), (int16_t)M68KR(M68K_REG_D3),
+                             M68KR(M68K_REG_D4), CMD_PITCH, RGBF_D7, cmd_mask);
+            }
+            gdebug("iFillRect end\n");
+            break;
+        }
+        case RTGCMD_INVERTRECT: {
+            // A0: BoardInfo *b, A1: RenderInfo *r
+            // D0 WORD x, D1: WORD y, D2: WORD w, D3: WORD h
+            // D4: UBYTE mask, D7: RGBFTYPE format
+            gdebug("iInvertRect begin\n");
+            if (!b || !r) break;
+
+            cmd_mask = (uint8_t)M68KR(M68K_REG_D4);
+            rtg_address_adj[0] = be32toh(r->_p_Memory) - (PIGFX_RTG_BASE + PIGFX_REG_SIZE);
+
+            rtg_invertrect((int16_t)M68KR(M68K_REG_D0), (int16_t)M68KR(M68K_REG_D1), (int16_t)M68KR(M68K_REG_D2), (int16_t)M68KR(M68K_REG_D3), CMD_PITCH, RGBF_D7, cmd_mask);
+            gdebug("iInvertRect end\n");
+            break;
+        }
+        case RTGCMD_BLITRECT: {
+            // A0: BoardInfo *b, A1: RenderInfo *r)
+            // D0: WORD x, D1: WORD y, D2: WORD dx, D3: WORD dy, D4: WORD w, D5: WORD h,
+            // D6: UBYTE mask, D7: RGBFTYPE format
+            gdebug("iBlitRect begin\n");
+
+            cmd_mask = (uint8_t)M68KR(M68K_REG_D6);
+            rtg_address_adj[0] = be32toh(r->_p_Memory) - (PIGFX_RTG_BASE + PIGFX_REG_SIZE);
+
+            if (cmd_mask == 0xFF) {
+                rtg_blitrect_solid(M68KR(M68K_REG_D0), M68KR(M68K_REG_D1), M68KR(M68K_REG_D2), M68KR(M68K_REG_D3), M68KR(M68K_REG_D4), M68KR(M68K_REG_D5), CMD_PITCH, RGBF_D7);
+            } else {
+                rtg_blitrect(M68KR(M68K_REG_D0), M68KR(M68K_REG_D1), M68KR(M68K_REG_D2), M68KR(M68K_REG_D3), M68KR(M68K_REG_D4), M68KR(M68K_REG_D5), CMD_PITCH, RGBF_D7, cmd_mask);
+            }
+
+            gdebug("iBlitRect end\n");
+            break;
+        }
+        case RTGCMD_BLITRECT_NOMASK_COMPLETE: {
+            // A0: BoardInfo *b, A1: RenderInfo *rs, A2: RenderInfo *rt,
+            // D0: WORD x, D1: WORD y, D2: WORD dx, D3: WORD dy, D4: WORD w, D5: WORD h,
+            // D6: UBYTE minterm, D7: RGBFTYPE format
+            gdebug("iBlitRectNoMaskComplete begin\n");
+
+            uint8_t minterm = (uint8_t)M68KR(M68K_REG_D6);
+            struct P96RenderInfo *rt = (struct P96RenderInfo *)get_mapped_data_pointer_by_address(cfg, M68KR(M68K_REG_A2));
+
+            uint32_t src_addr = be32toh(r->_p_Memory);
+            uint32_t dst_addr = be32toh(rt->_p_Memory);
+
+            rtg_blitrect_nomask_complete(M68KR(M68K_REG_D0), M68KR(M68K_REG_D1), M68KR(M68K_REG_D2), M68KR(M68K_REG_D3), M68KR(M68K_REG_D4), M68KR(M68K_REG_D5),
+                                         CMD_PITCH, be16toh(rt->BytesPerRow), src_addr, dst_addr, RGBF_D7, minterm);
+
+            gdebug("iBlitRectNoMaskComplete end\n");
+            break;
+        }
+        default:
+            printf("[!!!IRTG] Unnkonw/unhandled iRTG command %d.\n", cmd);
+            break;
+    }
+}
 
 static void handle_rtg_command(uint32_t cmd) {
   //printf("Handling RTG command %d (%.8X)\n", cmd, cmd);
@@ -225,10 +389,6 @@ static void handle_rtg_command(uint32_t cmd) {
             rtg_pitch = (rtg_x[0] << rtg_display_format);
             framebuffer_addr = rtg_address[0] - (PIGFX_RTG_BASE + PIGFX_REG_SIZE);
             framebuffer_addr_adj = framebuffer_addr + (rtg_offset_x << rtg_display_format) + (rtg_offset_y * rtg_pitch);
-            //printf("Set panning to $%.8X (%.8X)\n", framebuffer_addr, rtg_address[0]);
-            //printf("(Panned: $%.8X)\n", framebuffer_addr_adj);
-            //printf("Offset X/Y: %d/%d\n", rtg_offset_x, rtg_offset_y);
-            //printf("Pitch: %d (%d bytes)\n", rtg_x[0], rtg_pitch);
             break;
         case RTGCMD_SETCLUT: {
             //printf("Command: SetCLUT.\n");
@@ -295,7 +455,7 @@ static void handle_rtg_command(uint32_t cmd) {
             if (rtg_u8[0] == 0xFF && rtg_y[2] == 0xFFFF)
                 rtg_drawline_solid(rtg_x[0], rtg_y[0], rtg_x[1], rtg_y[1], rtg_x[2], rtg_rgb[0], rtg_x[3], rtg_format);
             else
-                rtg_drawline(rtg_x[0], rtg_y[0], rtg_x[1], rtg_y[1], rtg_x[2], rtg_y[2], rtg_x[4], rtg_rgb[0], rtg_rgb[1],  rtg_x[3], rtg_format, rtg_u8[0], rtg_u8[1]);
+                rtg_drawline(rtg_x[0], rtg_y[0], rtg_x[1], rtg_y[1], rtg_x[2], rtg_y[2], rtg_x[4], rtg_rgb[0], rtg_rgb[1], rtg_x[3], rtg_format, rtg_u8[0], rtg_u8[1]);
             gdebug("DrawLine\n");
             break;
         case RTGCMD_P2C: