1 // SPDX-License-Identifier: MIT
10 #include "config_file/config_file.h"
13 uint16_t rtg_x[8], rtg_y[8];
16 uint32_t rtg_address[8];
17 uint32_t rtg_address_adj[8];
20 static uint8_t display_enabled = 0xFF;
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;
27 uint8_t *rtg_mem; // FIXME
29 uint32_t framebuffer_addr = 0;
30 uint32_t framebuffer_addr_adj = 0;
32 static void handle_rtg_command(uint32_t cmd);
33 //static struct timespec f1, f2;
35 uint8_t realtime_graphics_debug = 0;
36 extern int cpu_emulation_running;
37 extern uint8_t rtg_on;
40 static const char *op_type_names[OP_TYPE_NUM] = {
47 static const char *rtg_format_names[RTGFMT_NUM] = {
55 rtg_mem = calloc(1, 40 * SIZE_MEGA);
57 printf("Failed to allocate RTG video memory.\n");
65 printf("[RTG] Shutting down RTG.\n");
75 //void rtg_update_screen();
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) {
82 if (address >= PIGFX_REG_SIZE) {
83 if (rtg_mem && (address - PIGFX_REG_SIZE) < PIGFX_UPPER) {
86 return (rtg_mem[address - PIGFX_REG_SIZE]);
89 return be16toh(*(( uint16_t *) (&rtg_mem[address - PIGFX_REG_SIZE])));
91 case OP_TYPE_LONGWORD:
92 return be32toh(*(( uint32_t *) (&rtg_mem[address - PIGFX_REG_SIZE])));
103 struct timespec diff(struct timespec start, struct timespec end)
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;
110 temp.tv_sec = end.tv_sec-start.tv_sec;
111 temp.tv_nsec = end.tv_nsec-start.tv_nsec;
116 #define CHKREG(a, b) case a: b = value; break;
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);
124 if (rtg_mem && (address - PIGFX_REG_SIZE) < PIGFX_UPPER) {
127 rtg_mem[address - PIGFX_REG_SIZE] = value;
130 *(( uint16_t *) (&rtg_mem[address - PIGFX_REG_SIZE])) = htobe16(value);
132 case OP_TYPE_LONGWORD:
133 *(( uint32_t *) (&rtg_mem[address - PIGFX_REG_SIZE])) = htobe32(value);
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]);
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);
166 handle_rtg_command(value);
170 case OP_TYPE_LONGWORD:
173 rtg_address[0] = value;
174 rtg_address_adj[0] = value - (PIGFX_RTG_BASE + PIGFX_REG_SIZE);
177 rtg_address[1] = value;
178 rtg_address_adj[1] = value - (PIGFX_RTG_BASE + PIGFX_REG_SIZE);
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]);
192 #define gdebug(a) if (realtime_graphics_debug) { printf(a); m68k_end_timeslice(); cpu_emulation_running = 0; }
194 static void handle_rtg_command(uint32_t cmd) {
195 //printf("Handling RTG command %d (%.8X)\n", cmd, cmd);
198 rtg_display_format = rtg_format;
199 rtg_display_width = rtg_x[0];
200 rtg_display_height = rtg_y[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];
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];
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]);
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);
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]);
234 case RTGCMD_SETDISPLAY:
235 //printf("RTG SetDisplay %s\n", (rtg_u8[1]) ? "enabled" : "disabled");
237 //printf("Command: SetDisplay.\n");
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) {
249 rtg_shutdown_display();
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");
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");
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");
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");
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");
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");
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");
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");
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);
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");
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");