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