]> git.sesse.net Git - pistorm/blobdiff - platforms/amiga/rtg/rtg.c
Dysfunctional SDL2 RTG output
[pistorm] / platforms / amiga / rtg / rtg.c
index e26cabfbc040a85cd43216f1de93a917b60973a4..4fce959f0c86a4202b454b66c1eb6d9b65b0852c 100644 (file)
@@ -1,11 +1,13 @@
 #include <stdint.h>
 #include <endian.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
 #include "rtg.h"
 #include "../../../config_file/config_file.h"
 
 static uint16_t palette[256];
-static uint8_t rtg_mem[64 * SIZE_MEGA]; // FIXME
 
 static uint8_t rtg_u8[4];
 static uint16_t rtg_x[3], rtg_y[3];
@@ -13,16 +15,19 @@ static uint16_t rtg_format;
 static uint32_t rtg_address[2];
 static uint32_t rtg_rgb[2];
 
-static uint8_t rtg_enabled;
+static uint8_t display_enabled;
 
 uint16_t rtg_display_width, rtg_display_height;
 uint16_t rtg_display_format;
 uint16_t rtg_pitch, rtg_total_rows;
 uint16_t rtg_offset_x, rtg_offset_y;
 
+uint8_t *rtg_mem; // FIXME
+
 uint32_t framebuffer_addr;
 
 static void handle_rtg_command(uint32_t cmd);
+static struct timespec f1, f2;
 
 static const char *op_type_names[OP_TYPE_NUM] = {
     "BYTE",
@@ -38,6 +43,19 @@ static const char *rtg_format_names[RTGFMT_NUM] = {
     "15BPP RGB (555)",
 };
 
+int init_rtg_data() {
+    rtg_mem = calloc(1, 32 * SIZE_MEGA);
+    if (!rtg_mem) {
+        printf("Failed to allocate RTG video memory.\n");
+        return 0;
+    }
+
+    return 1;
+}
+
+extern uint8_t busy, rtg_on;
+void rtg_update_screen();
+
 unsigned int rtg_read(uint32_t address, uint8_t mode) {
     //printf("%s read from RTG: %.8X\n", op_type_names[mode], address);
     if (address >= PIGFX_REG_SIZE) {
@@ -61,6 +79,19 @@ unsigned int rtg_read(uint32_t address, uint8_t mode) {
     return 0;
 }
 
+struct timespec diff(struct timespec start, struct timespec end)
+{
+    struct timespec temp;
+    if ((end.tv_nsec-start.tv_nsec)<0) {
+        temp.tv_sec = end.tv_sec-start.tv_sec-1;
+        temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
+    } else {
+        temp.tv_sec = end.tv_sec-start.tv_sec;
+        temp.tv_nsec = end.tv_nsec-start.tv_nsec;
+    }
+    return temp;
+}
+
 void rtg_write(uint32_t address, uint32_t value, uint8_t mode) {
     //printf("%s write to RTG: %.8X (%.8X)\n", op_type_names[mode], address, value);
     if (address >= PIGFX_REG_SIZE) {
@@ -138,6 +169,14 @@ void rtg_write(uint32_t address, uint32_t value, uint8_t mode) {
         }
     }
 
+    if (rtg_on) {
+        clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &f2);
+        if (diff(f1,f2).tv_nsec / 1000000.0 > 100.00) {
+            rtg_update_screen();
+            f1 = f2;
+        }
+    }
+
     return;
 }
 
@@ -162,7 +201,7 @@ static void handle_rtg_command(uint32_t cmd) {
             break;
         case RTGCMD_SETPAN:
             //printf("Command: SetPan.\n");
-            framebuffer_addr = rtg_address[0];
+            framebuffer_addr = rtg_address[0] - (PIGFX_RTG_BASE + PIGFX_REG_SIZE);
             rtg_offset_x = rtg_x[1];
             rtg_offset_y = rtg_y[1];
             rtg_pitch = (rtg_x[0] << rtg_display_format);
@@ -173,25 +212,53 @@ static void handle_rtg_command(uint32_t cmd) {
         case RTGCMD_SETCLUT: {
             //printf("Command: SetCLUT.\n");
             //printf("Set palette entry %d to %d, %d, %d\n", rtg_u8[0], rtg_u8[1], rtg_u8[2], rtg_u8[3]);
-            int r = (int)((float)rtg_u8[1] / 255.0f * 31.0f);
+            /*int r = (int)((float)rtg_u8[1] / 255.0f * 31.0f);
             int g = (int)((float)rtg_u8[2] / 255.0f * 63.0f);
             int b = (int)((float)rtg_u8[3] / 255.0f * 31.0f);
-            palette[rtg_u8[0]] = ((r & 0x1F) << 11) | ((g & 0x3F) << 6) | ((b & 0x1F) << 6);
+            palette[rtg_u8[0]] = ((r & 0x1F) << 11) | ((g & 0x3F) << 6) | (b & 0x1F);*/
+            rtg_set_clut_entry(rtg_u8[0], rtg_u8[1], rtg_u8[2], rtg_u8[3]);
             break;
         }
         case RTGCMD_SETDISPLAY:
+            // I remeber wrongs.
             //printf("Command: SetDisplay.\n");
-            if (rtg_enabled != rtg_u8[1]) {
-                //printf("RTG Display %s\n", (rtg_u8[1]) ? "enabled" : "disabled");
-                rtg_enabled = rtg_u8[1];
-                //if (rtg_enabled)
-                    //printf("%dx%d pixels\n", rtg_display_width, rtg_display_height);
-            }
             break;
         case RTGCMD_ENABLE:
         case RTGCMD_SETSWITCH:
-            // Implementing this command only matters if the Pi is to pass through the analog (or digital)
-            // native video, otherwise this does nothing.
+            if (display_enabled != rtg_u8[1]) {
+                //printf("RTG Display %s\n", (rtg_u8[1]) ? "enabled" : "disabled");
+                display_enabled = rtg_u8[1];
+                if (display_enabled) {
+                    rtg_init_display();
+                    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &f1);
+                }
+                else
+                    rtg_shutdown_display();
+            }
+            break;
+        case RTGCMD_FILLRECT:
+            rtg_fillrect(rtg_x[0], rtg_y[0], rtg_x[1], rtg_y[1], rtg_rgb[0], rtg_x[2], rtg_format, 0xFF);
+            break;
+    }
+}
+
+void rtg_fillrect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint32_t color, uint16_t pitch, uint16_t format, uint8_t mask) {
+    if (mask) {}
+    switch(format) {
+        case RTGFMT_8BIT:
+            break;
+        case RTGFMT_RBG565: {
+            uint16_t *ptr = (uint16_t *)&rtg_mem[framebuffer_addr + (x << format) + (y * pitch)];
+            for (int xs = 0; xs < w; xs++) {
+                ptr[xs] = (color >> 16);
+            }
+            for (int ys = 1; ys < h; ys++) {
+                ptr += (rtg_pitch >> format);
+                memcpy(ptr, (void *)(size_t)(ptr - (rtg_pitch >> format)), rtg_pitch);
+            }
+            break;
+        }
+        case RTGFMT_RGB32:
             break;
     }
 }