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 uint32_t framebuffer_addr;
21 extern uint32_t framebuffer_addr_adj;
23 extern uint8_t realtime_graphics_debug;
25 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) {
26 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (x << format) + (y * pitch)];
29 for (int xs = 0; xs < w; xs++) {
30 dptr[xs] = color & 0xFF;
35 color = htobe16((color & 0xFFFF));
36 uint16_t *ptr = (uint16_t *)dptr;
37 for (int xs = 0; xs < w; xs++) {
43 color = htobe32(color);
44 uint32_t *ptr = (uint32_t *)dptr;
45 for (int xs = 0; xs < w; xs++) {
51 for (int ys = 1; ys < h; ys++) {
53 memcpy(dptr, (void *)(size_t)(dptr - pitch), (w << format));
57 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) {
58 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (x << format) + (y * pitch)];
60 for (int ys = 0; ys < h; ys++) {
61 for (int xs = 0; xs < w; xs++) {
62 SET_RTG_PIXEL_MASK(&dptr[xs << format], (color & 0xFF), format);
68 void rtg_invertrect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t pitch, uint16_t format, uint8_t mask) {
70 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (x << format) + (y * pitch)];
71 for (int ys = 0; ys < h; ys++) {
74 for (int xs = 0; xs < w; xs++) {
80 for (int xs = 0; xs < w; xs++) {
81 ((uint16_t *)dptr)[xs] = ~((uint16_t *)dptr)[xs];
86 for (int xs = 0; xs < w; xs++) {
87 ((uint32_t *)dptr)[xs] = ~((uint32_t *)dptr)[xs];
96 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) {
98 uint8_t *sptr = &rtg_mem[rtg_address_adj[0] + (x << format) + (y * pitch)];
99 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (dx << format) + (dy * pitch)];
101 uint32_t xdir = 1, pitchstep = pitch;
105 sptr += ((h - 1) * pitch);
106 dptr += ((h - 1) * pitch);
112 for (int ys = 0; ys < h; ys++) {
114 for (int xs = 0; xs < w; xs++) {
115 SET_RTG_PIXEL_MASK(&dptr[xs], sptr[xs], format);
119 for (int xs = w - 1; xs >= x; xs--) {
120 SET_RTG_PIXEL_MASK(&dptr[xs], sptr[xs], format);
128 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) {
129 uint8_t *sptr = &rtg_mem[rtg_address_adj[0] + (x << format) + (y * pitch)];
130 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (dx << format) + (dy * pitch)];
132 uint32_t xdir = 1, pitchstep = pitch;
136 sptr += ((h - 1) * pitch);
137 dptr += ((h - 1) * pitch);
143 for (int ys = 0; ys < h; ys++) {
145 memcpy(dptr, sptr, w << format);
147 memmove(dptr, sptr, w << format);
153 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) {
155 uint8_t *sptr = &rtg_mem[src_addr - (PIGFX_RTG_BASE + PIGFX_REG_SIZE) + (sx << format) + (sy * srcpitch)];
156 uint8_t *dptr = &rtg_mem[dst_addr - (PIGFX_RTG_BASE + PIGFX_REG_SIZE) + (dx << format) + (dy * dstpitch)];
158 uint32_t xdir = 1, src_pitchstep = srcpitch, dst_pitchstep = dstpitch;
159 uint8_t draw_mode = minterm;
160 uint32_t mask = 0xFF;
162 if (src_addr == dst_addr) {
164 src_pitchstep = -srcpitch;
165 sptr += ((h - 1) * srcpitch);
166 dst_pitchstep = -dstpitch;
167 dptr += ((h - 1) * dstpitch);
174 if (format == RTGFMT_RBG565)
176 if (format == RTGFMT_RGB32)
179 if (minterm == MINTERM_SRC) {
180 for (int ys = 0; ys < h; ys++) {
182 memcpy(dptr, sptr, w << format);
184 memmove(dptr, sptr, w << format);
185 sptr += src_pitchstep;
186 dptr += dst_pitchstep;
190 for (int ys = 0; ys < h; ys++) {
192 for (int xs = 0; xs < w; xs++) {
195 HANDLE_MINTERM_PIXEL(sptr[xs], dptr[xs], format);
198 HANDLE_MINTERM_PIXEL(((uint16_t *)sptr)[xs], ((uint16_t *)dptr)[xs], format);
201 HANDLE_MINTERM_PIXEL(((uint32_t *)sptr)[xs], ((uint32_t *)dptr)[xs], format);
207 for (int xs = w - 1; xs >= sx; xs--) {
210 HANDLE_MINTERM_PIXEL(sptr[xs], dptr[xs], format);
213 HANDLE_MINTERM_PIXEL(((uint16_t *)sptr)[xs], ((uint16_t *)dptr)[xs], format);
216 HANDLE_MINTERM_PIXEL(((uint32_t *)sptr)[xs], ((uint32_t *)dptr)[xs], format);
221 sptr += src_pitchstep;
222 dptr += src_pitchstep;
227 extern struct emulator_config *cfg;
229 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) {
230 uint8_t *dptr = &rtg_mem[rtg_address_adj[1] + (x << format) + (y * pitch)];
231 uint8_t *sptr = NULL;
232 uint8_t cur_bit = 0, base_bit = 0, cur_byte = 0;
233 uint8_t invert = (draw_mode & DRAWMODE_INVERSVID);
238 tmpl_x = offset_x / 8;
239 cur_bit = base_bit = (0x80 >> (offset_x % 8));
241 if (realtime_graphics_debug) {
242 printf("DEBUG: BlitTemplate - %d, %d (%dx%d)\n", x, y, w, h);
243 printf("Src: %.8X\n", src_addr);
244 printf("Dest: %.8X (%.8X)\n", rtg_address[1], rtg_address_adj[1]);
245 printf("pitch: %d t_pitch: %d format: %d\n", pitch, t_pitch, format);
246 printf("offset_x: %d mask: %.2X draw_mode: %d\n", offset_x, mask, draw_mode);
249 uint32_t fg_color[3] = {
251 htobe16((fgcol & 0xFFFF)),
254 uint32_t bg_color[3] = {
256 htobe16((bgcol & 0xFFFF)),
260 sptr = get_mapped_data_pointer_by_address(cfg, src_addr);
262 if (realtime_graphics_debug) {
263 printf("BlitTemplate pattern data NOT available in mapped range, source address: $%.8X\n", src_addr);
266 if (realtime_graphics_debug) {
267 printf("BlitTemplate pattern data available in mapped range at $%.8X\n", src_addr);
273 for (uint16_t ys = 0; ys < h; ys++) {
274 for (int xs = 0; xs < w; xs++) {
276 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
277 if (mask == 0xFF || format != RTGFMT_8BIT) {
278 SET_RTG_PIXELS(&dptr[xs << format], fg_color[format], format);
281 SET_RTG_PIXELS_MASK(&dptr[xs], fg_color[format], format);
286 while (cur_bit > 0 && xs < w) {
287 if (cur_byte & cur_bit) {
288 if (mask == 0xFF || format != RTGFMT_8BIT) {
289 SET_RTG_PIXEL(&dptr[xs << format], fg_color[format], format);
292 SET_RTG_PIXEL_MASK(&dptr[xs], fg_color[format], format);
306 for (uint16_t ys = 0; ys < h; ys++) {
307 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
309 for (int xs = 0; xs < w; xs++) {
311 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
312 if (mask == 0xFF || format != RTGFMT_8BIT) {
313 SET_RTG_PIXELS2_COND(&dptr[xs << format], fg_color[format], bg_color[format], format);
316 SET_RTG_PIXELS2_COND_MASK(&dptr[xs << format], fg_color[format], bg_color[format], format);
322 while (cur_bit > 0 && xs < w) {
323 if (mask == 0xFF || format != RTGFMT_8BIT) {
324 SET_RTG_PIXEL(&dptr[xs << format], (cur_byte & cur_bit) ? fg_color[format] : bg_color[format], format);
327 SET_RTG_PIXEL_MASK(&dptr[xs << format], (cur_byte & cur_bit) ? fg_color[format] : bg_color[format], format);
339 case DRAWMODE_COMPLEMENT:
340 for (uint16_t ys = 0; ys < h; ys++) {
341 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
343 for (int xs = 0; xs < w; xs++) {
345 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
346 INVERT_RTG_PIXELS(&dptr[xs << format], format)
350 while (cur_bit > 0 && xs < w) {
351 if (cur_byte & cur_bit) {
352 INVERT_RTG_PIXEL(&dptr[xs << format], format)
367 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) {
370 uint8_t *dptr = &rtg_mem[rtg_address_adj[1] + (x << format) + (y * pitch)];
371 uint8_t *sptr = NULL, *sptr_base = NULL;
372 uint8_t cur_bit = 0, base_bit = 0, cur_byte = 0;
373 uint8_t invert = (draw_mode & DRAWMODE_INVERSVID);
378 tmpl_x = (offset_x / 8) % 2;
379 cur_bit = base_bit = (0x80 >> (offset_x % 8));
381 uint32_t fg_color[3] = {
383 htobe16((fgcol & 0xFFFF)),
386 uint32_t bg_color[3] = {
388 htobe16((bgcol & 0xFFFF)),
393 if (src_addr >= (PIGFX_RTG_BASE + PIGFX_REG_SIZE))
394 sptr = &rtg_mem[src_addr - (PIGFX_RTG_BASE + PIGFX_REG_SIZE)];
396 int i = get_mapped_item_by_address(cfg, src_addr);
398 sptr = &cfg->map_data[i][src_addr - cfg->map_offset[i]];
401 printf("BlitPattern: Failed to find mapped range for address %.8X\n", src_addr);
407 sptr += (offset_y % loop_rows) * 2;
411 for (uint16_t ys = 0; ys < h; ys++) {
412 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
414 for (int xs = 0; xs < w; xs++) {
415 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
416 if (mask == 0xFF || format != RTGFMT_8BIT) {
417 SET_RTG_PIXELS(&dptr[xs << format], fg_color[format], format);
420 SET_RTG_PIXELS_MASK(&dptr[xs], fg_color[format], format);
425 while (cur_bit > 0 && xs < w) {
426 if (cur_byte & cur_bit) {
427 if (mask == 0xFF || format != RTGFMT_8BIT) {
428 SET_RTG_PIXEL(&dptr[xs << format], fg_color[format], format);
431 SET_RTG_PIXEL_MASK(&dptr[xs], fg_color[format], format);
446 for (uint16_t ys = 0; ys < h; ys++) {
447 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
449 for (int xs = 0; xs < w; xs++) {
450 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
451 if (mask == 0xFF || format != RTGFMT_8BIT) {
452 SET_RTG_PIXELS2_COND(&dptr[xs << format], fg_color[format], bg_color[format], format);
455 SET_RTG_PIXELS2_COND_MASK(&dptr[xs << format], fg_color[format], bg_color[format], format);
461 while (cur_bit > 0 && xs < w) {
462 if (mask == 0xFF || format != RTGFMT_8BIT) {
463 SET_RTG_PIXEL(&dptr[xs << format], (cur_byte & cur_bit) ? fg_color[format] : bg_color[format], format);
466 SET_RTG_PIXEL_MASK(&dptr[xs << format], (cur_byte & cur_bit) ? fg_color[format] : bg_color[format], format);
479 case DRAWMODE_COMPLEMENT:
480 for (uint16_t ys = 0; ys < h; ys++) {
481 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
483 for (int xs = 0; xs < w; xs++) {
484 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
485 INVERT_RTG_PIXELS(&dptr[xs << format], format)
489 while (cur_bit > 0 && xs < w) {
490 if (cur_byte & cur_bit) {
491 INVERT_RTG_PIXEL(&dptr[xs << format], format)
507 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) {
508 int16_t x1 = x1_, y1 = y1_;
509 int16_t x2 = x1_ + x2_, y2 = y1 + y2_;
511 uint32_t fg_color[3] = {
513 htobe16((fgcol & 0xFFFF)),
517 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (y1 * pitch)];
519 int32_t line_step = pitch;
522 int16_t dx, dy, dx_abs, dy_abs, ix, iy, x = x1;
536 SET_RTG_PIXEL(&dptr[x << format], fg_color[format], format);
538 if (dx_abs >= dy_abs) {
539 if (!len) len = dx_abs;
540 for (uint16_t i = 0; i < len; i++) {
548 SET_RTG_PIXEL(&dptr[x << format], fg_color[format], format);
552 if (!len) len = dy_abs;
553 for (uint16_t i = 0; i < len; i++) {
561 SET_RTG_PIXEL(&dptr[x << format], fg_color[format], format);
566 #define DRAW_LINE_PIXEL \
567 if (pattern & cur_bit) { \
568 if (invert) { INVERT_RTG_PIXEL(&dptr[x << format], format) } \
570 if (mask == 0xFF || format != RTGFMT_8BIT) { SET_RTG_PIXEL(&dptr[x << format], fg_color[format], format); } \
571 else { SET_RTG_PIXEL_MASK(&dptr[x << format], fg_color[format], format); } \
574 else if (draw_mode == DRAWMODE_JAM2) { \
575 if (invert) { INVERT_RTG_PIXEL(&dptr[x << format], format) } \
577 if (mask == 0xFF || format != RTGFMT_8BIT) { SET_RTG_PIXEL(&dptr[x << format], bg_color[format], format); } \
578 else { SET_RTG_PIXEL_MASK(&dptr[x << format], bg_color[format], format); } \
581 if ((cur_bit >>= 1) == 0) \
584 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) {
585 if (pattern_offset) {}
587 int16_t x1 = x1_, y1 = y1_;
588 int16_t x2 = x1_ + x2_, y2 = y1 + y2_;
589 uint16_t cur_bit = 0x8000;
590 //uint32_t color_mask = 0xFFFF0000;
593 uint32_t fg_color[3] = {
595 htobe16((fgcol & 0xFFFF)),
598 uint32_t bg_color[3] = {
600 htobe16((bgcol & 0xFFFF)),
604 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (y1 * pitch)];
606 int32_t line_step = pitch;
609 int16_t dx, dy, dx_abs, dy_abs, ix, iy, x = x1;
623 if (draw_mode & DRAWMODE_INVERSVID)
625 if (draw_mode & DRAWMODE_COMPLEMENT) {
632 if (dx_abs >= dy_abs) {
633 if (!len) len = dx_abs;
634 for (uint16_t i = 0; i < len; i++) {
646 if (!len) len = dy_abs;
647 for (uint16_t i = 0; i < len; i++) {
660 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) {
661 uint16_t pitch = rtg_x[3];
662 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (dy * pitch)];
664 uint8_t cur_bit, base_bit, base_byte;
665 uint16_t cur_byte = 0, u8_fg = 0;
666 //uint32_t color_mask = 0xFFFFFFFF;
668 uint32_t plane_size = src_line_pitch * h;
669 uint8_t *bmp_data = bmp_data_src;
671 cur_bit = base_bit = (0x80 >> (sx % 8));
672 cur_byte = base_byte = ((sx / 8) % src_line_pitch);
674 if (realtime_graphics_debug) {
675 printf("P2C: %d,%d - %d,%d (%dx%d) %d, %.2X\n", sx, sy, dx, dy, w, h, planes, layer_mask);
676 printf("Mask: %.2X Minterm: %.2X\n", mask, draw_mode);
677 printf("Pitch: %d Src Pitch: %d (!!!: %.4X)\n", pitch, src_line_pitch, rtg_user[0]);
678 printf("Curbyte: %d Curbit: %d\n", cur_byte, cur_bit);
679 printf("Plane size: %d Total size: %d (%X)\n", plane_size, plane_size * planes, plane_size * planes);
680 printf("Source: %.8X - %.8X\n", rtg_address[1], rtg_address_adj[1]);
681 printf("Target: %.8X - %.8X\n", rtg_address[0], rtg_address_adj[0]);
684 printf("Grabbing data from RTG memory.\nData:\n");
685 for (int i = 0; i < h; i++) {
686 for (int k = 0; k < planes; k++) {
687 for (int j = 0; j < src_line_pitch; j++) {
688 printf("%.2X", bmp_data_src[j + (i * src_line_pitch) + (plane_size * k)]);
696 for (int16_t line_y = 0; line_y < h; line_y++) {
697 for (int16_t x = dx; x < dx + w; x++) {
699 if (draw_mode & 0x01) {
700 DECODE_INVERTED_PLANAR_PIXEL(u8_fg)
703 DECODE_PLANAR_PIXEL(u8_fg)
706 if (mask == 0xFF && (draw_mode == MINTERM_SRC || draw_mode == MINTERM_NOTSRC)) {
711 HANDLE_MINTERM_PIXEL(u8_fg, dptr[x], rtg_display_format);
714 if ((cur_bit >>= 1) == 0) {
717 cur_byte %= src_line_pitch;
721 if ((line_y + sy + 1) % h)
722 bmp_data += src_line_pitch;
724 bmp_data = bmp_data_src;
726 cur_byte = base_byte;
730 void rtg_p2d (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) {
731 uint16_t pitch = rtg_x[3];
732 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (dy * pitch)];
734 uint8_t cur_bit, base_bit, base_byte;
735 uint16_t cur_byte = 0, u8_fg = 0;
736 //uint32_t color_mask = 0xFFFFFFFF;
738 uint32_t plane_size = src_line_pitch * h;
739 uint8_t *bmp_data = bmp_data_src;
741 cur_bit = base_bit = (0x80 >> (sx % 8));
742 cur_byte = base_byte = ((sx / 8) % src_line_pitch);
744 if (realtime_graphics_debug) {
745 printf("P2D: %d,%d - %d,%d (%dx%d) %d, %.2X\n", sx, sy, dx, dy, w, h, planes, layer_mask);
746 printf("Mask: %.2X Minterm: %.2X\n", mask, draw_mode);
747 printf("Pitch: %d Src Pitch: %d (!!!: %.4X)\n", pitch, src_line_pitch, rtg_user[0]);
748 printf("Curbyte: %d Curbit: %d\n", cur_byte, cur_bit);
749 printf("Plane size: %d Total size: %d (%X)\n", plane_size, plane_size * planes, plane_size * planes);
750 printf("Source: %.8X - %.8X\n", rtg_address[1], rtg_address_adj[1]);
751 printf("Target: %.8X - %.8X\n", rtg_address[0], rtg_address_adj[0]);
754 printf("Grabbing data from RTG memory.\nData:\n");
755 for (int i = 0; i < h; i++) {
756 for (int k = 0; k < planes; k++) {
757 for (int j = 0; j < src_line_pitch; j++) {
758 printf("%.2X", bmp_data_src[j + (i * src_line_pitch) + (plane_size * k)]);
766 uint32_t *clut = (uint32_t *)bmp_data_src;
767 bmp_data += (256 * 4);
768 bmp_data_src += (256 * 4);
770 for (int16_t line_y = 0; line_y < h; line_y++) {
771 for (int16_t x = dx; x < dx + w; x++) {
773 if (draw_mode & 0x01) {
774 DECODE_INVERTED_PLANAR_PIXEL(u8_fg)
777 DECODE_PLANAR_PIXEL(u8_fg)
780 uint32_t fg_color = clut[u8_fg];
782 if (mask == 0xFF && (draw_mode == MINTERM_SRC || draw_mode == MINTERM_NOTSRC)) {
783 switch (rtg_display_format) {
785 ((uint16_t *)dptr)[x] = (fg_color >> 16);
788 ((uint32_t *)dptr)[x] = fg_color;
795 if ((cur_bit >>= 1) == 0) {
798 cur_byte %= src_line_pitch;
802 if ((line_y + sy + 1) % h)
803 bmp_data += src_line_pitch;
805 bmp_data = bmp_data_src;
807 cur_byte = base_byte;