1 // SPDX-License-Identifier: MIT
7 #include "config_file/config_file.h"
9 #include "gpio/ps_protocol.h"
13 extern uint32_t rtg_address[8];
14 extern uint32_t rtg_address_adj[8];
15 extern uint8_t *rtg_mem; // FIXME
16 extern uint16_t rtg_display_format;
17 extern uint16_t rtg_user[8];
18 extern uint16_t rtg_x[8], rtg_y[8];
20 extern uint8_t realtime_graphics_debug;
22 void rtg_fillrect_solid(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint32_t color, uint16_t pitch, uint16_t format) {
23 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (x << format) + (y * pitch)];
26 for (int xs = 0; xs < w; xs++) {
27 dptr[xs] = color & 0xFF;
32 color = htobe16((color & 0xFFFF));
33 uint16_t *ptr = (uint16_t *)dptr;
34 for (int xs = 0; xs < w; xs++) {
40 color = htobe32(color);
41 uint32_t *ptr = (uint32_t *)dptr;
42 for (int xs = 0; xs < w; xs++) {
48 for (int ys = 1; ys < h; ys++) {
50 memcpy(dptr, (void *)(size_t)(dptr - pitch), (w << format));
54 void rtg_fillrect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint32_t color, uint16_t pitch, uint16_t format, uint8_t mask) {
55 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (x << format) + (y * pitch)];
57 for (int ys = 0; ys < h; ys++) {
58 for (int xs = 0; xs < w; xs++) {
59 SET_RTG_PIXEL_MASK(&dptr[xs], (color & 0xFF), format);
65 void rtg_invertrect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t pitch, uint16_t format, uint8_t mask) {
67 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (x << format) + (y * pitch)];
68 for (int ys = 0; ys < h; ys++) {
71 for (int xs = 0; xs < w; xs++) {
77 for (int xs = 0; xs < w; xs++) {
78 ((uint16_t *)dptr)[xs] = ~((uint16_t *)dptr)[xs];
83 for (int xs = 0; xs < w; xs++) {
84 ((uint32_t *)dptr)[xs] = ~((uint32_t *)dptr)[xs];
93 void rtg_blitrect(uint16_t x, uint16_t y, uint16_t dx, uint16_t dy, uint16_t w, uint16_t h, uint16_t pitch, uint16_t format, uint8_t mask) {
95 uint8_t *sptr = &rtg_mem[rtg_address_adj[0] + (x << format) + (y * pitch)];
96 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (dx << format) + (dy * pitch)];
98 uint32_t xdir = 1, pitchstep = pitch;
102 sptr += ((h - 1) * pitch);
103 dptr += ((h - 1) * pitch);
109 for (int ys = 0; ys < h; ys++) {
111 for (int xs = 0; xs < w; xs++) {
112 SET_RTG_PIXEL_MASK(&dptr[xs], sptr[xs], format);
116 for (int xs = w - 1; xs >= x; xs--) {
117 SET_RTG_PIXEL_MASK(&dptr[xs], sptr[xs], format);
125 void rtg_blitrect_solid(uint16_t x, uint16_t y, uint16_t dx, uint16_t dy, uint16_t w, uint16_t h, uint16_t pitch, uint16_t format) {
126 uint8_t *sptr = &rtg_mem[rtg_address_adj[0] + (x << format) + (y * pitch)];
127 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (dx << format) + (dy * pitch)];
129 uint32_t xdir = 1, pitchstep = pitch;
133 sptr += ((h - 1) * pitch);
134 dptr += ((h - 1) * pitch);
140 for (int ys = 0; ys < h; ys++) {
142 memcpy(dptr, sptr, w << format);
144 memmove(dptr, sptr, w << format);
150 void rtg_blitrect_nomask_complete(uint16_t sx, uint16_t sy, uint16_t dx, uint16_t dy, uint16_t w, uint16_t h, uint16_t srcpitch, uint16_t dstpitch, uint32_t src_addr, uint32_t dst_addr, uint16_t format, uint8_t minterm) {
152 uint8_t *sptr = &rtg_mem[src_addr - (PIGFX_RTG_BASE + PIGFX_REG_SIZE) + (sx << format) + (sy * srcpitch)];
153 uint8_t *dptr = &rtg_mem[dst_addr - (PIGFX_RTG_BASE + PIGFX_REG_SIZE) + (dx << format) + (dy * dstpitch)];
155 uint32_t xdir = 1, src_pitchstep = srcpitch, dst_pitchstep = dstpitch;
156 uint8_t draw_mode = minterm;
157 uint32_t mask = 0xFF;
159 if (src_addr == dst_addr) {
161 src_pitchstep = -srcpitch;
162 sptr += ((h - 1) * srcpitch);
163 dst_pitchstep = -dstpitch;
164 dptr += ((h - 1) * dstpitch);
171 if (format == RTGFMT_RBG565)
173 if (format == RTGFMT_RGB32)
176 if (minterm == MINTERM_SRC) {
177 for (int ys = 0; ys < h; ys++) {
179 memcpy(dptr, sptr, w << format);
181 memmove(dptr, sptr, w << format);
182 sptr += src_pitchstep;
183 dptr += dst_pitchstep;
187 for (int ys = 0; ys < h; ys++) {
189 for (int xs = 0; xs < w; xs++) {
192 HANDLE_MINTERM_PIXEL(sptr[xs], dptr[xs], format);
195 HANDLE_MINTERM_PIXEL(((uint16_t *)sptr)[xs], ((uint16_t *)dptr)[xs], format);
198 HANDLE_MINTERM_PIXEL(((uint32_t *)sptr)[xs], ((uint32_t *)dptr)[xs], format);
204 for (int xs = w - 1; xs >= sx; xs--) {
207 HANDLE_MINTERM_PIXEL(sptr[xs], dptr[xs], format);
210 HANDLE_MINTERM_PIXEL(((uint16_t *)sptr)[xs], ((uint16_t *)dptr)[xs], format);
213 HANDLE_MINTERM_PIXEL(((uint32_t *)sptr)[xs], ((uint32_t *)dptr)[xs], format);
218 sptr += src_pitchstep;
219 dptr += src_pitchstep;
224 extern struct emulator_config *cfg;
226 void rtg_blittemplate(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint32_t src_addr, uint32_t fgcol, uint32_t bgcol, uint16_t pitch, uint16_t t_pitch, uint16_t format, uint16_t offset_x, uint8_t mask, uint8_t draw_mode) {
229 uint8_t *dptr = &rtg_mem[rtg_address_adj[1] + (x << format) + (y * pitch)];
230 uint8_t *sptr = NULL;
231 uint8_t cur_bit = 0, base_bit = 0, cur_byte = 0;
232 uint8_t invert = (draw_mode & DRAWMODE_INVERSVID);
237 tmpl_x = offset_x / 8;
238 cur_bit = base_bit = (0x80 >> (offset_x % 8));
240 if (realtime_graphics_debug) {
241 printf("DEBUG: BlitTemplate - %d, %d (%dx%d)\n", x, y, w, h);
242 printf("Src: %.8X (%.8X)\n", src_addr, rtg_address_adj[0]);
243 printf("Dest: %.8X (%.8X)\n", rtg_address[1], rtg_address_adj[1]);
244 printf("pitch: %d t_pitch: %d format: %d\n", pitch, t_pitch, format);
245 printf("offset_x: %d mask: %.2X draw_mode: %d\n", offset_x, mask, draw_mode);
248 uint32_t fg_color[3] = {
250 htobe16((fgcol & 0xFFFF)),
253 uint32_t bg_color[3] = {
255 htobe16((bgcol & 0xFFFF)),
259 if (src_addr >= (PIGFX_RTG_BASE + PIGFX_REG_SIZE)) {
260 sptr = &rtg_mem[src_addr - (PIGFX_RTG_BASE + PIGFX_REG_SIZE)];
261 if (realtime_graphics_debug) {
262 printf("Origin: %.8X\n", rtg_address[2]);
263 printf("Grabbing data from RTG memory.\nData:\n");
264 for (int i = 0; i < h; i++) {
265 for (int j = 0; j < t_pitch; j++) {
266 printf("%.2X", sptr[j + (i * t_pitch)]);
271 printf("Data available at origin:\n");
272 for (int i = 0; i < h; i++) {
273 for (int j = 0; j < w; j++) {
274 printf("%.2X", read8(rtg_address[2] + j + (i * t_pitch)));
282 int i = get_mapped_item_by_address(cfg, src_addr);
284 sptr = &cfg->map_data[i][src_addr - cfg->map_offset[i]];
285 if (realtime_graphics_debug) {
286 printf("Grabbing data from maping %d - offset %.8lX\nData:\n", i, src_addr - cfg->map_offset[i]);
287 for (int i = 0; i < h; i++) {
288 for (int j = 0; j < t_pitch; j++) {
289 printf("%.2X", sptr[j + (i * t_pitch)]);
296 printf("BlitTemplate: Failed to find mapped range for address %.8X\n", src_addr);
303 for (uint16_t ys = 0; ys < h; ys++) {
304 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
306 for (int xs = 0; xs < w; xs++) {
307 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
308 if (mask == 0xFF || format != RTGFMT_8BIT) {
309 SET_RTG_PIXELS(&dptr[xs << format], fg_color[format], format);
312 SET_RTG_PIXELS_MASK(&dptr[xs], fg_color[format], format);
317 while (cur_bit > 0 && xs < w) {
318 if (cur_byte & cur_bit) {
319 if (mask == 0xFF || format != RTGFMT_8BIT) {
320 SET_RTG_PIXEL(&dptr[xs << format], fg_color[format], format);
323 SET_RTG_PIXEL_MASK(&dptr[xs], fg_color[format], format);
338 for (uint16_t ys = 0; ys < h; ys++) {
339 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
341 for (int xs = 0; xs < w; xs++) {
342 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
343 if (mask == 0xFF || format != RTGFMT_8BIT) {
344 SET_RTG_PIXELS2_COND(&dptr[xs << format], fg_color[format], bg_color[format], format);
347 SET_RTG_PIXELS2_COND_MASK(&dptr[xs << format], fg_color[format], bg_color[format], format);
353 while (cur_bit > 0 && xs < w) {
354 if (mask == 0xFF || format != RTGFMT_8BIT) {
355 SET_RTG_PIXEL(&dptr[xs << format], (cur_byte & cur_bit) ? fg_color[format] : bg_color[format], format);
358 SET_RTG_PIXEL_MASK(&dptr[xs << format], (cur_byte & cur_bit) ? fg_color[format] : bg_color[format], format);
371 case DRAWMODE_COMPLEMENT:
372 for (uint16_t ys = 0; ys < h; ys++) {
373 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
375 for (int xs = 0; xs < w; xs++) {
376 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
377 INVERT_RTG_PIXELS(&dptr[xs << format], format)
381 while (cur_bit > 0 && xs < w) {
382 if (cur_byte & cur_bit) {
383 INVERT_RTG_PIXEL(&dptr[xs << format], format)
399 void rtg_blitpattern(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint32_t src_addr, uint32_t fgcol, uint32_t bgcol, uint16_t pitch, uint16_t format, uint16_t offset_x, uint16_t offset_y, uint8_t mask, uint8_t draw_mode, uint8_t loop_rows) {
402 uint8_t *dptr = &rtg_mem[rtg_address_adj[1] + (x << format) + (y * pitch)];
403 uint8_t *sptr = NULL, *sptr_base = NULL;
404 uint8_t cur_bit = 0, base_bit = 0, cur_byte = 0;
405 uint8_t invert = (draw_mode & DRAWMODE_INVERSVID);
410 tmpl_x = (offset_x / 8) % 2;
411 cur_bit = base_bit = (0x80 >> (offset_x % 8));
413 uint32_t fg_color[3] = {
415 htobe16((fgcol & 0xFFFF)),
418 uint32_t bg_color[3] = {
420 htobe16((bgcol & 0xFFFF)),
425 if (src_addr >= (PIGFX_RTG_BASE + PIGFX_REG_SIZE))
426 sptr = &rtg_mem[src_addr - (PIGFX_RTG_BASE + PIGFX_REG_SIZE)];
428 int i = get_mapped_item_by_address(cfg, src_addr);
430 sptr = &cfg->map_data[i][src_addr - cfg->map_offset[i]];
433 printf("BlitPattern: Failed to find mapped range for address %.8X\n", src_addr);
439 sptr += (offset_y % loop_rows) * 2;
443 for (uint16_t ys = 0; ys < h; ys++) {
444 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
446 for (int xs = 0; xs < w; xs++) {
447 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
448 if (mask == 0xFF || format != RTGFMT_8BIT) {
449 SET_RTG_PIXELS(&dptr[xs << format], fg_color[format], format);
452 SET_RTG_PIXELS_MASK(&dptr[xs], fg_color[format], format);
457 while (cur_bit > 0 && xs < w) {
458 if (cur_byte & cur_bit) {
459 if (mask == 0xFF || format != RTGFMT_8BIT) {
460 SET_RTG_PIXEL(&dptr[xs << format], fg_color[format], format);
463 SET_RTG_PIXEL_MASK(&dptr[xs], fg_color[format], format);
478 for (uint16_t ys = 0; ys < h; ys++) {
479 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
481 for (int xs = 0; xs < w; xs++) {
482 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
483 if (mask == 0xFF || format != RTGFMT_8BIT) {
484 SET_RTG_PIXELS2_COND(&dptr[xs << format], fg_color[format], bg_color[format], format);
487 SET_RTG_PIXELS2_COND_MASK(&dptr[xs << format], fg_color[format], bg_color[format], format);
493 while (cur_bit > 0 && xs < w) {
494 if (mask == 0xFF || format != RTGFMT_8BIT) {
495 SET_RTG_PIXEL(&dptr[xs << format], (cur_byte & cur_bit) ? fg_color[format] : bg_color[format], format);
498 SET_RTG_PIXEL_MASK(&dptr[xs << format], (cur_byte & cur_bit) ? fg_color[format] : bg_color[format], format);
511 case DRAWMODE_COMPLEMENT:
512 for (uint16_t ys = 0; ys < h; ys++) {
513 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
515 for (int xs = 0; xs < w; xs++) {
516 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
517 INVERT_RTG_PIXELS(&dptr[xs << format], format)
521 while (cur_bit > 0 && xs < w) {
522 if (cur_byte & cur_bit) {
523 INVERT_RTG_PIXEL(&dptr[xs << format], format)
539 void rtg_drawline_solid(int16_t x1_, int16_t y1_, int16_t x2_, int16_t y2_, uint16_t len, uint32_t fgcol, uint16_t pitch, uint16_t format) {
540 int16_t x1 = x1_, y1 = y1_;
541 int16_t x2 = x1_ + x2_, y2 = y1 + y2_;
543 uint32_t fg_color[3] = {
545 htobe16((fgcol & 0xFFFF)),
549 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (y1 * pitch)];
551 int32_t line_step = pitch;
554 int16_t dx, dy, dx_abs, dy_abs, ix, iy, x = x1;
568 SET_RTG_PIXEL(&dptr[x << format], fg_color[format], format);
570 if (dx_abs >= dy_abs) {
571 if (!len) len = dx_abs;
572 for (uint16_t i = 0; i < len; i++) {
580 SET_RTG_PIXEL(&dptr[x << format], fg_color[format], format);
584 if (!len) len = dy_abs;
585 for (uint16_t i = 0; i < len; i++) {
593 SET_RTG_PIXEL(&dptr[x << format], fg_color[format], format);
598 #define DRAW_LINE_PIXEL \
599 if (pattern & cur_bit) { \
600 if (invert) { INVERT_RTG_PIXEL(&dptr[x << format], format) } \
602 if (mask == 0xFF || format != RTGFMT_8BIT) { SET_RTG_PIXEL(&dptr[x << format], fg_color[format], format); } \
603 else { SET_RTG_PIXEL_MASK(&dptr[x << format], fg_color[format], format); } \
606 else if (draw_mode == DRAWMODE_JAM2) { \
607 if (invert) { INVERT_RTG_PIXEL(&dptr[x << format], format) } \
609 if (mask == 0xFF || format != RTGFMT_8BIT) { SET_RTG_PIXEL(&dptr[x << format], bg_color[format], format); } \
610 else { SET_RTG_PIXEL_MASK(&dptr[x << format], bg_color[format], format); } \
613 if ((cur_bit >>= 1) == 0) \
616 void rtg_drawline (int16_t x1_, int16_t y1_, int16_t x2_, int16_t y2_, uint16_t len, uint16_t pattern, uint16_t pattern_offset, uint32_t fgcol, uint32_t bgcol, uint16_t pitch, uint16_t format, uint8_t mask, uint8_t draw_mode) {
617 if (pattern_offset) {}
619 int16_t x1 = x1_, y1 = y1_;
620 int16_t x2 = x1_ + x2_, y2 = y1 + y2_;
621 uint16_t cur_bit = 0x8000;
622 //uint32_t color_mask = 0xFFFF0000;
625 uint32_t fg_color[3] = {
627 htobe16((fgcol & 0xFFFF)),
630 uint32_t bg_color[3] = {
632 htobe16((bgcol & 0xFFFF)),
636 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (y1 * pitch)];
638 int32_t line_step = pitch;
641 int16_t dx, dy, dx_abs, dy_abs, ix, iy, x = x1;
655 if (draw_mode & DRAWMODE_INVERSVID)
657 if (draw_mode & DRAWMODE_COMPLEMENT) {
664 if (dx_abs >= dy_abs) {
665 if (!len) len = dx_abs;
666 for (uint16_t i = 0; i < len; i++) {
678 if (!len) len = dy_abs;
679 for (uint16_t i = 0; i < len; i++) {
692 void rtg_p2c (int16_t sx, int16_t sy, int16_t dx, int16_t dy, int16_t w, int16_t h, uint8_t draw_mode, uint8_t planes, uint8_t mask, uint8_t layer_mask, uint16_t src_line_pitch, uint8_t *bmp_data_src) {
693 uint16_t pitch = rtg_x[3];
694 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (dy * pitch)];
696 uint8_t cur_bit, base_bit, base_byte;
697 uint16_t cur_byte = 0, u8_fg = 0;
698 //uint32_t color_mask = 0xFFFFFFFF;
700 uint32_t plane_size = src_line_pitch * h;
701 uint8_t *bmp_data = bmp_data_src;
703 cur_bit = base_bit = (0x80 >> (sx % 8));
704 cur_byte = base_byte = ((sx / 8) % src_line_pitch);
706 if (realtime_graphics_debug) {
707 printf("P2C: %d,%d - %d,%d (%dx%d) %d, %.2X\n", sx, sy, dx, dy, w, h, planes, layer_mask);
708 printf("Mask: %.2X Minterm: %.2X\n", mask, draw_mode);
709 printf("Pitch: %d Src Pitch: %d (!!!: %.4X)\n", pitch, src_line_pitch, rtg_user[0]);
710 printf("Curbyte: %d Curbit: %d\n", cur_byte, cur_bit);
711 printf("Plane size: %d Total size: %d (%X)\n", plane_size, plane_size * planes, plane_size * planes);
712 printf("Source: %.8X - %.8X\n", rtg_address[1], rtg_address_adj[1]);
713 printf("Target: %.8X - %.8X\n", rtg_address[0], rtg_address_adj[0]);
716 printf("Grabbing data from RTG memory.\nData:\n");
717 for (int i = 0; i < h; i++) {
718 for (int k = 0; k < planes; k++) {
719 for (int j = 0; j < src_line_pitch; j++) {
720 printf("%.2X", bmp_data_src[j + (i * src_line_pitch) + (plane_size * k)]);
728 for (int16_t line_y = 0; line_y < h; line_y++) {
729 for (int16_t x = dx; x < dx + w; x++) {
731 if (draw_mode & 0x01) {
732 DECODE_INVERTED_PLANAR_PIXEL(u8_fg)
735 DECODE_PLANAR_PIXEL(u8_fg)
738 if (mask == 0xFF && (draw_mode == MINTERM_SRC || draw_mode == MINTERM_NOTSRC)) {
743 HANDLE_MINTERM_PIXEL(u8_fg, dptr[x], rtg_display_format);
746 if ((cur_bit >>= 1) == 0) {
749 cur_byte %= src_line_pitch;
754 if ((line_y + sy + 1) % h)
755 bmp_data += src_line_pitch;
757 bmp_data = bmp_data_src;
759 cur_byte = base_byte;