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;
38 static const char *op_type_names[OP_TYPE_NUM] = {
45 static const char *rtg_format_names[RTGFMT_NUM] = {
53 rtg_mem = calloc(1, 40 * SIZE_MEGA);
55 printf("Failed to allocate RTG video memory.\n");
62 //extern uint8_t busy, rtg_on;
63 //void rtg_update_screen();
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) {
70 if (address >= PIGFX_REG_SIZE) {
71 if (rtg_mem && (address - PIGFX_REG_SIZE) < PIGFX_UPPER) {
74 return (rtg_mem[address - PIGFX_REG_SIZE]);
77 return be16toh(*(( uint16_t *) (&rtg_mem[address - PIGFX_REG_SIZE])));
79 case OP_TYPE_LONGWORD:
80 return be32toh(*(( uint32_t *) (&rtg_mem[address - PIGFX_REG_SIZE])));
91 struct timespec diff(struct timespec start, struct timespec end)
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;
98 temp.tv_sec = end.tv_sec-start.tv_sec;
99 temp.tv_nsec = end.tv_nsec-start.tv_nsec;
104 #define CHKREG(a, b) case a: b = value; break;
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);
112 if (rtg_mem && (address - PIGFX_REG_SIZE) < PIGFX_UPPER) {
115 rtg_mem[address - PIGFX_REG_SIZE] = value;
118 *(( uint16_t *) (&rtg_mem[address - PIGFX_REG_SIZE])) = htobe16(value);
120 case OP_TYPE_LONGWORD:
121 *(( uint32_t *) (&rtg_mem[address - PIGFX_REG_SIZE])) = htobe32(value);
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]);
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);
154 handle_rtg_command(value);
158 case OP_TYPE_LONGWORD:
161 rtg_address[0] = value;
162 rtg_address_adj[0] = value - (PIGFX_RTG_BASE + PIGFX_REG_SIZE);
165 rtg_address[1] = value;
166 rtg_address_adj[1] = value - (PIGFX_RTG_BASE + PIGFX_REG_SIZE);
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]);
180 #define gdebug(a) if (realtime_graphics_debug) { printf(a); m68k_end_timeslice(); cpu_emulation_running = 0; }
182 static void handle_rtg_command(uint32_t cmd) {
183 //printf("Handling RTG command %d (%.8X)\n", cmd, cmd);
186 rtg_display_format = rtg_format;
187 rtg_display_width = rtg_x[0];
188 rtg_display_height = rtg_y[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];
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];
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]);
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);
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]);
222 case RTGCMD_SETDISPLAY:
223 //printf("RTG SetDisplay %s\n", (rtg_u8[1]) ? "enabled" : "disabled");
225 //printf("Command: SetDisplay.\n");
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) {
237 rtg_shutdown_display();
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");
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");
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");
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");
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");
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");
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");
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");
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);
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");
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");