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