]> git.sesse.net Git - pistorm/blob - platforms/amiga/rtg/rtg.c
Add license information to source
[pistorm] / platforms / amiga / rtg / rtg.c
1 // SPDX-License-Identifier: MIT
2
3 #include <stdint.h>
4 #include <endian.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <time.h>
9 #include "rtg.h"
10 #include "config_file/config_file.h"
11
12 uint8_t rtg_u8[4];
13 uint16_t rtg_x[8], rtg_y[8];
14 uint16_t rtg_user[8];
15 uint16_t rtg_format;
16 uint32_t rtg_address[8];
17 uint32_t rtg_address_adj[8];
18 uint32_t rtg_rgb[8];
19
20 static uint8_t display_enabled = 0xFF;
21
22 uint16_t rtg_display_width, rtg_display_height;
23 uint16_t rtg_display_format;
24 uint16_t rtg_pitch, rtg_total_rows;
25 uint16_t rtg_offset_x, rtg_offset_y;
26
27 uint8_t *rtg_mem; // FIXME
28
29 uint32_t framebuffer_addr = 0;
30 uint32_t framebuffer_addr_adj = 0;
31
32 static void handle_rtg_command(uint32_t cmd);
33 //static struct timespec f1, f2;
34
35 uint8_t realtime_graphics_debug = 0;
36 extern int cpu_emulation_running;
37 /*
38 static const char *op_type_names[OP_TYPE_NUM] = {
39     "BYTE",
40     "WORD",
41     "LONGWORD",
42     "MEM",
43 };
44
45 static const char *rtg_format_names[RTGFMT_NUM] = {
46     "8BPP CLUT",
47     "16BPP RGB (565)",
48     "32BPP RGB (RGBA)",
49     "15BPP RGB (555)",
50 };
51 */
52 int init_rtg_data() {
53     rtg_mem = calloc(1, 40 * SIZE_MEGA);
54     if (!rtg_mem) {
55         printf("Failed to allocate RTG video memory.\n");
56         return 0;
57     }
58
59     return 1;
60 }
61
62 //extern uint8_t busy, rtg_on;
63 //void rtg_update_screen();
64
65 unsigned int rtg_read(uint32_t address, uint8_t mode) {
66     //printf("%s read from RTG: %.8X\n", op_type_names[mode], address);
67     if (address == RTG_COMMAND) {
68         return 0xFFCF;
69     }
70     if (address >= PIGFX_REG_SIZE) {
71         if (rtg_mem && (address - PIGFX_REG_SIZE) < PIGFX_UPPER) {
72             switch (mode) {
73                 case OP_TYPE_BYTE:
74                     return (rtg_mem[address - PIGFX_REG_SIZE]);
75                     break;
76                 case OP_TYPE_WORD:
77                     return be16toh(*(( uint16_t *) (&rtg_mem[address - PIGFX_REG_SIZE])));
78                     break;
79                 case OP_TYPE_LONGWORD:
80                     return be32toh(*(( uint32_t *) (&rtg_mem[address - PIGFX_REG_SIZE])));
81                     break;
82                 default:
83                     return 0;
84             }
85         }
86     }
87
88     return 0;
89 }
90
91 struct timespec diff(struct timespec start, struct timespec end)
92 {
93     struct timespec temp;
94     if ((end.tv_nsec-start.tv_nsec)<0) {
95         temp.tv_sec = end.tv_sec-start.tv_sec-1;
96         temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
97     } else {
98         temp.tv_sec = end.tv_sec-start.tv_sec;
99         temp.tv_nsec = end.tv_nsec-start.tv_nsec;
100     }
101     return temp;
102 }
103
104 #define CHKREG(a, b) case a: b = value; break;
105
106 void rtg_write(uint32_t address, uint32_t value, uint8_t mode) {
107     //printf("%s write to RTG: %.8X (%.8X)\n", op_type_names[mode], address, value);
108     if (address >= PIGFX_REG_SIZE) {
109         /*if ((address - PIGFX_REG_SIZE) < framebuffer_addr) {// || (address - PIGFX_REG_SIZE) > framebuffer_addr + ((rtg_display_width << rtg_display_format) * rtg_display_height)) {
110             printf("Write to RTG memory outside frame buffer %.8X (%.8X).\n", (address - PIGFX_REG_SIZE), framebuffer_addr);
111         }*/
112         if (rtg_mem && (address - PIGFX_REG_SIZE) < PIGFX_UPPER) {
113             switch (mode) {
114                 case OP_TYPE_BYTE:
115                     rtg_mem[address - PIGFX_REG_SIZE] = value;
116                     break;
117                 case OP_TYPE_WORD:
118                     *(( uint16_t *) (&rtg_mem[address - PIGFX_REG_SIZE])) = htobe16(value);
119                     break;
120                 case OP_TYPE_LONGWORD:
121                     *(( uint32_t *) (&rtg_mem[address - PIGFX_REG_SIZE])) = htobe32(value);
122                     break;
123                 default:
124                     return;
125             }
126         }
127     }
128     else {
129         switch (mode) {
130             case OP_TYPE_BYTE:
131                 switch (address) {
132                     CHKREG(RTG_U81, rtg_u8[0]);
133                     CHKREG(RTG_U82, rtg_u8[1]);
134                     CHKREG(RTG_U83, rtg_u8[2]);
135                     CHKREG(RTG_U84, rtg_u8[3]);
136                 }
137                 break;
138             case OP_TYPE_WORD:
139                 switch (address) {
140                     CHKREG(RTG_X1, rtg_x[0]);
141                     CHKREG(RTG_X2, rtg_x[1]);
142                     CHKREG(RTG_X3, rtg_x[2]);
143                     CHKREG(RTG_X4, rtg_x[3]);
144                     CHKREG(RTG_X5, rtg_x[4]);
145                     CHKREG(RTG_Y1, rtg_y[0]);
146                     CHKREG(RTG_Y2, rtg_y[1]);
147                     CHKREG(RTG_Y3, rtg_y[2]);
148                     CHKREG(RTG_Y4, rtg_y[3]);
149                     CHKREG(RTG_Y5, rtg_y[4]);
150                     CHKREG(RTG_U1, rtg_user[0]);
151                     CHKREG(RTG_U2, rtg_user[1]);
152                     CHKREG(RTG_FORMAT, rtg_format);
153                     case RTG_COMMAND:
154                         handle_rtg_command(value);
155                         break;
156                 }
157                 break;
158             case OP_TYPE_LONGWORD:
159                 switch (address) {
160                     case RTG_ADDR1:
161                         rtg_address[0] = value;
162                         rtg_address_adj[0] = value - (PIGFX_RTG_BASE + PIGFX_REG_SIZE);
163                         break;
164                     case RTG_ADDR2:
165                         rtg_address[1] = value;
166                         rtg_address_adj[1] = value - (PIGFX_RTG_BASE + PIGFX_REG_SIZE);
167                         break;
168                     CHKREG(RTG_ADDR3, rtg_address[2]);
169                     CHKREG(RTG_ADDR4, rtg_address[3]);
170                     CHKREG(RTG_RGB1, rtg_rgb[0]);
171                     CHKREG(RTG_RGB2, rtg_rgb[1]);
172                 }
173                 break;
174         }
175     }
176
177     return;
178 }
179
180 #define gdebug(a) if (realtime_graphics_debug) { printf(a); m68k_end_timeslice(); cpu_emulation_running = 0; }
181
182 static void handle_rtg_command(uint32_t cmd) {
183     //printf("Handling RTG command %d (%.8X)\n", cmd, cmd);
184     switch (cmd) {
185         case RTGCMD_SETGC:
186             rtg_display_format = rtg_format;
187             rtg_display_width = rtg_x[0];
188             rtg_display_height = rtg_y[0];
189             if (rtg_u8[0]) {
190                 //rtg_pitch = rtg_display_width << rtg_format;
191                 framebuffer_addr_adj = framebuffer_addr + (rtg_offset_x << rtg_display_format) + (rtg_offset_y * rtg_pitch);
192                 rtg_total_rows = rtg_y[1];
193             }
194             else {
195                 //rtg_pitch = rtg_display_width << rtg_format;
196                 framebuffer_addr_adj = framebuffer_addr + (rtg_offset_x << rtg_display_format) + (rtg_offset_y * rtg_pitch);
197                 rtg_total_rows = rtg_y[1];
198             }
199             //printf("Set RTG mode:\n");
200             //printf("%dx%d pixels\n", rtg_display_width, rtg_display_height);
201             //printf("Pixel format: %s\n", rtg_format_names[rtg_display_format]);
202             break;
203         case RTGCMD_SETPAN:
204             //printf("Command: SetPan.\n");
205             rtg_offset_x = rtg_x[1];
206             rtg_offset_y = rtg_y[1];
207             rtg_pitch = (rtg_x[0] << rtg_display_format);
208             framebuffer_addr = rtg_address[0] - (PIGFX_RTG_BASE + PIGFX_REG_SIZE);
209             framebuffer_addr_adj = framebuffer_addr + (rtg_offset_x << rtg_display_format) + (rtg_offset_y * rtg_pitch);
210             //printf("Set panning to $%.8X (%.8X)\n", framebuffer_addr, rtg_address[0]);
211             //printf("(Panned: $%.8X)\n", framebuffer_addr_adj);
212             //printf("Offset X/Y: %d/%d\n", rtg_offset_x, rtg_offset_y);
213             printf("Pitch: %d (%d bytes)\n", rtg_x[0], rtg_pitch);
214             break;
215         case RTGCMD_SETCLUT: {
216             //printf("Command: SetCLUT.\n");
217             //printf("Set palette entry %d to %d, %d, %d\n", rtg_u8[0], rtg_u8[1], rtg_u8[2], rtg_u8[3]);
218             //printf("Set palette entry %d to 32-bit palette color: %.8X\n", rtg_u8[0], rtg_rgb[0]);
219             rtg_set_clut_entry(rtg_u8[0], rtg_rgb[0]);
220             break;
221         }
222         case RTGCMD_SETDISPLAY:
223             //printf("RTG SetDisplay %s\n", (rtg_u8[1]) ? "enabled" : "disabled");
224             // I remeber wrongs.
225             //printf("Command: SetDisplay.\n");
226             break;
227         case RTGCMD_ENABLE:
228         case RTGCMD_SETSWITCH:
229             //printf("RTG SetSwitch %s\n", ((rtg_x[0]) & 0x01) ? "enabled" : "disabled");
230             //printf("LAL: %.4X\n", rtg_x[0]);
231             if (display_enabled != ((rtg_x[0]) & 0x01)) {
232                 display_enabled = ((rtg_x[0]) & 0x01);
233                 if (display_enabled) {
234                     rtg_init_display();
235                 }
236                 else
237                     rtg_shutdown_display();
238             }
239             break;
240         case RTGCMD_FILLRECT:
241             if (rtg_u8[0] == 0xFF || rtg_format != RTGFMT_8BIT) {
242                 rtg_fillrect_solid(rtg_x[0], rtg_y[0], rtg_x[1], rtg_y[1], rtg_rgb[0], rtg_x[2], rtg_format);
243                 gdebug("FillRect Solid\n");
244             }
245             else {
246                 rtg_fillrect(rtg_x[0], rtg_y[0], rtg_x[1], rtg_y[1], rtg_rgb[0], rtg_x[2], rtg_format, rtg_u8[0]);
247                 gdebug("FillRect Masked\n");
248             }
249             break;
250         case RTGCMD_INVERTRECT:
251             rtg_invertrect(rtg_x[0], rtg_y[0], rtg_x[1], rtg_y[1], rtg_x[2], rtg_format, rtg_u8[0]);
252             gdebug("InvertRect\n");
253             break;
254         case RTGCMD_BLITRECT:
255             if (rtg_u8[0] == 0xFF || rtg_format != RTGFMT_8BIT) {
256                 rtg_blitrect_solid(rtg_x[0], rtg_y[0], rtg_x[1], rtg_y[1], rtg_x[2], rtg_y[2], rtg_x[3], rtg_format);
257                 gdebug("BlitRect Solid\n");
258             }
259             else {
260                 rtg_blitrect(rtg_x[0], rtg_y[0], rtg_x[1], rtg_y[1], rtg_x[2], rtg_y[2], rtg_x[3], rtg_format, rtg_u8[0]);
261                 gdebug("BlitRect Masked\n");
262             }
263             break;
264         case RTGCMD_BLITRECT_NOMASK_COMPLETE:
265             rtg_blitrect_nomask_complete(rtg_x[0], rtg_y[0], rtg_x[1], rtg_y[1], rtg_x[2], rtg_y[2], rtg_x[3], rtg_x[4], rtg_address[0], rtg_address[1], rtg_format, rtg_u8[0]);
266             gdebug("BlitRectNoMaskComplete\n");
267             break;
268         case RTGCMD_BLITPATTERN:
269             rtg_blitpattern(rtg_x[0], rtg_y[0], rtg_x[1], rtg_y[1], rtg_address[0], rtg_rgb[0], rtg_rgb[1], rtg_x[3], rtg_format, rtg_x[2], rtg_y[2], rtg_u8[0], rtg_u8[1], rtg_u8[2]);
270             gdebug("BlitPattern\n");
271             return;
272         case RTGCMD_BLITTEMPLATE:
273             rtg_blittemplate(rtg_x[0], rtg_y[0], rtg_x[1], rtg_y[1], rtg_address[0], rtg_rgb[0], rtg_rgb[1], rtg_x[3], rtg_x[4], rtg_format, rtg_x[2], rtg_u8[0], rtg_u8[1]);
274             gdebug("BlitTemplate\n");
275             break;
276         case RTGCMD_DRAWLINE:
277             if (rtg_u8[0] == 0xFF && rtg_y[2] == 0xFFFF)
278                 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);
279             else
280                 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]);
281             gdebug("DrawLine\n");
282             break;
283         case RTGCMD_P2C:
284             rtg_p2c(rtg_x[0], rtg_y[0], rtg_x[1], rtg_y[1], rtg_x[2], rtg_y[2], rtg_u8[1], rtg_u8[2], rtg_u8[0], (rtg_user[0] >> 0x8), rtg_x[4], (uint8_t *)&rtg_mem[rtg_address_adj[1]]);
285             //rtg_p2c_broken(rtg_x[0], rtg_y[0], rtg_x[1], rtg_y[1], rtg_x[2], rtg_y[2], rtg_x[3], rtg_u8[0], rtg_u8[1], rtg_u8[2], rtg_user[0]);
286             gdebug("Planar2Chunky\n");
287             break;
288         case RTGCMD_P2D:
289             break;
290     }
291 }