5 #include "config_file/config_file.h"
7 #include "gpio/ps_protocol.h"
11 extern uint32_t rtg_address[8];
12 extern uint32_t rtg_address_adj[8];
13 extern uint8_t *rtg_mem; // FIXME
14 extern uint16_t rtg_display_format;
15 extern uint16_t rtg_user[8];
16 extern uint16_t rtg_x[8], rtg_y[8];
18 extern uint8_t realtime_graphics_debug;
20 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) {
21 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (x << format) + (y * pitch)];
24 for (int xs = 0; xs < w; xs++) {
25 dptr[xs] = color & 0xFF;
30 color = htobe16((color & 0xFFFF));
31 uint16_t *ptr = (uint16_t *)dptr;
32 for (int xs = 0; xs < w; xs++) {
38 color = htobe32(color);
39 uint32_t *ptr = (uint32_t *)dptr;
40 for (int xs = 0; xs < w; xs++) {
46 for (int ys = 1; ys < h; ys++) {
48 memcpy(dptr, (void *)(size_t)(dptr - pitch), (w << format));
52 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) {
53 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (x << format) + (y * pitch)];
55 for (int ys = 0; ys < h; ys++) {
56 for (int xs = 0; xs < w; xs++) {
57 SET_RTG_PIXEL_MASK(&dptr[xs], (color & 0xFF), format);
63 void rtg_invertrect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t pitch, uint16_t format, uint8_t mask) {
65 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (x << format) + (y * pitch)];
66 for (int ys = 0; ys < h; ys++) {
69 for (int xs = 0; xs < w; xs++) {
75 for (int xs = 0; xs < w; xs++) {
76 ((uint16_t *)dptr)[xs] = ~((uint16_t *)dptr)[xs];
81 for (int xs = 0; xs < w; xs++) {
82 ((uint32_t *)dptr)[xs] = ~((uint32_t *)dptr)[xs];
91 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) {
93 uint8_t *sptr = &rtg_mem[rtg_address_adj[0] + (x << format) + (y * pitch)];
94 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (dx << format) + (dy * pitch)];
96 uint32_t xdir = 1, pitchstep = pitch;
100 sptr += ((h - 1) * pitch);
101 dptr += ((h - 1) * pitch);
107 for (int ys = 0; ys < h; ys++) {
109 for (int xs = 0; xs < w; xs++) {
110 SET_RTG_PIXEL_MASK(&dptr[xs], sptr[xs], format);
114 for (int xs = w - 1; xs >= x; xs--) {
115 SET_RTG_PIXEL_MASK(&dptr[xs], sptr[xs], format);
123 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) {
124 uint8_t *sptr = &rtg_mem[rtg_address_adj[0] + (x << format) + (y * pitch)];
125 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (dx << format) + (dy * pitch)];
127 uint32_t xdir = 1, pitchstep = pitch;
131 sptr += ((h - 1) * pitch);
132 dptr += ((h - 1) * pitch);
138 for (int ys = 0; ys < h; ys++) {
140 memcpy(dptr, sptr, w << format);
142 memmove(dptr, sptr, w << format);
148 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) {
150 uint8_t *sptr = &rtg_mem[src_addr - (PIGFX_RTG_BASE + PIGFX_REG_SIZE) + (sx << format) + (sy * srcpitch)];
151 uint8_t *dptr = &rtg_mem[dst_addr - (PIGFX_RTG_BASE + PIGFX_REG_SIZE) + (dx << format) + (dy * dstpitch)];
153 uint32_t xdir = 1, src_pitchstep = srcpitch, dst_pitchstep = dstpitch;
154 uint8_t draw_mode = minterm;
155 uint32_t mask = 0xFF;
157 if (src_addr == dst_addr) {
159 src_pitchstep = -srcpitch;
160 sptr += ((h - 1) * srcpitch);
161 dst_pitchstep = -dstpitch;
162 dptr += ((h - 1) * dstpitch);
169 if (format == RTGFMT_RBG565)
171 if (format == RTGFMT_RGB32)
174 if (minterm == MINTERM_SRC) {
175 for (int ys = 0; ys < h; ys++) {
177 memcpy(dptr, sptr, w << format);
179 memmove(dptr, sptr, w << format);
180 sptr += src_pitchstep;
181 dptr += dst_pitchstep;
185 for (int ys = 0; ys < h; ys++) {
187 for (int xs = 0; xs < w; xs++) {
190 HANDLE_MINTERM_PIXEL(sptr[xs], dptr[xs], format);
193 HANDLE_MINTERM_PIXEL(((uint16_t *)sptr)[xs], ((uint16_t *)dptr)[xs], format);
196 HANDLE_MINTERM_PIXEL(((uint32_t *)sptr)[xs], ((uint32_t *)dptr)[xs], format);
202 for (int xs = w - 1; xs >= sx; xs--) {
205 HANDLE_MINTERM_PIXEL(sptr[xs], dptr[xs], format);
208 HANDLE_MINTERM_PIXEL(((uint16_t *)sptr)[xs], ((uint16_t *)dptr)[xs], format);
211 HANDLE_MINTERM_PIXEL(((uint32_t *)sptr)[xs], ((uint32_t *)dptr)[xs], format);
216 sptr += src_pitchstep;
217 dptr += src_pitchstep;
222 extern struct emulator_config *cfg;
224 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) {
227 uint8_t *dptr = &rtg_mem[rtg_address_adj[1] + (x << format) + (y * pitch)];
228 uint8_t *sptr = NULL;
229 uint8_t cur_bit = 0, base_bit = 0, cur_byte = 0;
230 uint8_t invert = (draw_mode & DRAWMODE_INVERSVID);
235 tmpl_x = offset_x / 8;
236 cur_bit = base_bit = (0x80 >> (offset_x % 8));
238 if (realtime_graphics_debug) {
239 printf("DEBUG: BlitTemplate - %d, %d (%dx%d)\n", x, y, w, h);
240 printf("Src: %.8X (%.8X)\n", src_addr, rtg_address_adj[0]);
241 printf("Dest: %.8X (%.8X)\n", rtg_address[1], rtg_address_adj[1]);
242 printf("pitch: %d t_pitch: %d format: %d\n", pitch, t_pitch, format);
243 printf("offset_x: %d mask: %.2X draw_mode: %d\n", offset_x, mask, draw_mode);
246 uint32_t fg_color[3] = {
248 htobe16((fgcol & 0xFFFF)),
251 uint32_t bg_color[3] = {
253 htobe16((bgcol & 0xFFFF)),
257 if (src_addr >= (PIGFX_RTG_BASE + PIGFX_REG_SIZE)) {
258 sptr = &rtg_mem[src_addr - (PIGFX_RTG_BASE + PIGFX_REG_SIZE)];
259 if (realtime_graphics_debug) {
260 printf("Origin: %.8X\n", rtg_address[2]);
261 printf("Grabbing data from RTG memory.\nData:\n");
262 for (int i = 0; i < h; i++) {
263 for (int j = 0; j < t_pitch; j++) {
264 printf("%.2X", sptr[j + (i * t_pitch)]);
269 printf("Data available at origin:\n");
270 for (int i = 0; i < h; i++) {
271 for (int j = 0; j < w; j++) {
272 printf("%.2X", read8(rtg_address[2] + j + (i * t_pitch)));
280 int i = get_mapped_item_by_address(cfg, src_addr);
282 sptr = &cfg->map_data[i][src_addr - cfg->map_offset[i]];
283 if (realtime_graphics_debug) {
284 printf("Grabbing data from maping %d - offset %.8X\nData:\n", i, src_addr - cfg->map_offset[i]);
285 for (int i = 0; i < h; i++) {
286 for (int j = 0; j < t_pitch; j++) {
287 printf("%.2X", sptr[j + (i * t_pitch)]);
294 printf("BlitTemplate: Failed to find mapped range for address %.8X\n", src_addr);
301 for (uint16_t ys = 0; ys < h; ys++) {
302 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
304 for (int xs = 0; xs < w; xs++) {
305 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
306 if (mask == 0xFF || format != RTGFMT_8BIT) {
307 SET_RTG_PIXELS(&dptr[xs << format], fg_color[format], format);
310 SET_RTG_PIXELS_MASK(&dptr[xs], fg_color[format], format);
315 while (cur_bit > 0 && xs < w) {
316 if (cur_byte & cur_bit) {
317 if (mask == 0xFF || format != RTGFMT_8BIT) {
318 SET_RTG_PIXEL(&dptr[xs << format], fg_color[format], format);
321 SET_RTG_PIXEL_MASK(&dptr[xs], fg_color[format], format);
336 for (uint16_t ys = 0; ys < h; ys++) {
337 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
339 for (int xs = 0; xs < w; xs++) {
340 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
341 if (mask == 0xFF || format != RTGFMT_8BIT) {
342 SET_RTG_PIXELS2_COND(&dptr[xs << format], fg_color[format], bg_color[format], format);
345 SET_RTG_PIXELS2_COND_MASK(&dptr[xs << format], fg_color[format], bg_color[format], format);
351 while (cur_bit > 0 && xs < w) {
352 if (mask == 0xFF || format != RTGFMT_8BIT) {
353 SET_RTG_PIXEL(&dptr[xs << format], (cur_byte & cur_bit) ? fg_color[format] : bg_color[format], format);
356 SET_RTG_PIXEL_MASK(&dptr[xs << format], (cur_byte & cur_bit) ? fg_color[format] : bg_color[format], format);
369 case DRAWMODE_COMPLEMENT:
370 for (uint16_t ys = 0; ys < h; ys++) {
371 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
373 for (int xs = 0; xs < w; xs++) {
374 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
375 INVERT_RTG_PIXELS(&dptr[xs << format], format)
379 while (cur_bit > 0 && xs < w) {
380 if (cur_byte & cur_bit) {
381 INVERT_RTG_PIXEL(&dptr[xs << format], format)
397 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) {
400 uint8_t *dptr = &rtg_mem[rtg_address_adj[1] + (x << format) + (y * pitch)];
401 uint8_t *sptr = NULL, *sptr_base = NULL;
402 uint8_t cur_bit = 0, base_bit = 0, cur_byte = 0;
403 uint8_t invert = (draw_mode & DRAWMODE_INVERSVID);
408 tmpl_x = (offset_x / 8) % 2;
409 cur_bit = base_bit = (0x80 >> (offset_x % 8));
411 uint32_t fg_color[3] = {
413 htobe16((fgcol & 0xFFFF)),
416 uint32_t bg_color[3] = {
418 htobe16((bgcol & 0xFFFF)),
423 if (src_addr >= (PIGFX_RTG_BASE + PIGFX_REG_SIZE))
424 sptr = &rtg_mem[src_addr - (PIGFX_RTG_BASE + PIGFX_REG_SIZE)];
426 int i = get_mapped_item_by_address(cfg, src_addr);
428 sptr = &cfg->map_data[i][src_addr - cfg->map_offset[i]];
431 printf("BlitPattern: Failed to find mapped range for address %.8X\n", src_addr);
437 sptr += (offset_y % loop_rows) * 2;
441 for (uint16_t ys = 0; ys < h; ys++) {
442 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
444 for (int xs = 0; xs < w; xs++) {
445 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
446 if (mask == 0xFF || format != RTGFMT_8BIT) {
447 SET_RTG_PIXELS(&dptr[xs << format], fg_color[format], format);
450 SET_RTG_PIXELS_MASK(&dptr[xs], fg_color[format], format);
455 while (cur_bit > 0 && xs < w) {
456 if (cur_byte & cur_bit) {
457 if (mask == 0xFF || format != RTGFMT_8BIT) {
458 SET_RTG_PIXEL(&dptr[xs << format], fg_color[format], format);
461 SET_RTG_PIXEL_MASK(&dptr[xs], fg_color[format], format);
476 for (uint16_t ys = 0; ys < h; ys++) {
477 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
479 for (int xs = 0; xs < w; xs++) {
480 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
481 if (mask == 0xFF || format != RTGFMT_8BIT) {
482 SET_RTG_PIXELS2_COND(&dptr[xs << format], fg_color[format], bg_color[format], format);
485 SET_RTG_PIXELS2_COND_MASK(&dptr[xs << format], fg_color[format], bg_color[format], format);
491 while (cur_bit > 0 && xs < w) {
492 if (mask == 0xFF || format != RTGFMT_8BIT) {
493 SET_RTG_PIXEL(&dptr[xs << format], (cur_byte & cur_bit) ? fg_color[format] : bg_color[format], format);
496 SET_RTG_PIXEL_MASK(&dptr[xs << format], (cur_byte & cur_bit) ? fg_color[format] : bg_color[format], format);
509 case DRAWMODE_COMPLEMENT:
510 for (uint16_t ys = 0; ys < h; ys++) {
511 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
513 for (int xs = 0; xs < w; xs++) {
514 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
515 INVERT_RTG_PIXELS(&dptr[xs << format], format)
519 while (cur_bit > 0 && xs < w) {
520 if (cur_byte & cur_bit) {
521 INVERT_RTG_PIXEL(&dptr[xs << format], format)
537 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) {
538 int16_t x1 = x1_, y1 = y1_;
539 int16_t x2 = x1_ + x2_, y2 = y1 + y2_;
541 uint32_t fg_color[3] = {
543 htobe16((fgcol & 0xFFFF)),
547 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (y1 * pitch)];
549 int32_t line_step = pitch;
552 int16_t dx, dy, dx_abs, dy_abs, ix, iy, x = x1;
566 SET_RTG_PIXEL(&dptr[x << format], fg_color[format], format);
568 if (dx_abs >= dy_abs) {
569 if (!len) len = dx_abs;
570 for (uint16_t i = 0; i < len; i++) {
578 SET_RTG_PIXEL(&dptr[x << format], fg_color[format], format);
582 if (!len) len = dy_abs;
583 for (uint16_t i = 0; i < len; i++) {
591 SET_RTG_PIXEL(&dptr[x << format], fg_color[format], format);
596 #define DRAW_LINE_PIXEL \
597 if (pattern & cur_bit) { \
598 if (invert) { INVERT_RTG_PIXEL(&dptr[x << format], format) } \
600 if (mask == 0xFF || format != RTGFMT_8BIT) { SET_RTG_PIXEL(&dptr[x << format], fg_color[format], format); } \
601 else { SET_RTG_PIXEL_MASK(&dptr[x << format], fg_color[format], format); } \
604 else if (draw_mode == DRAWMODE_JAM2) { \
605 if (invert) { INVERT_RTG_PIXEL(&dptr[x << format], format) } \
607 if (mask == 0xFF || format != RTGFMT_8BIT) { SET_RTG_PIXEL(&dptr[x << format], bg_color[format], format); } \
608 else { SET_RTG_PIXEL_MASK(&dptr[x << format], bg_color[format], format); } \
611 if ((cur_bit >>= 1) == 0) \
614 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) {
615 if (pattern_offset) {}
617 int16_t x1 = x1_, y1 = y1_;
618 int16_t x2 = x1_ + x2_, y2 = y1 + y2_;
619 uint16_t cur_bit = 0x8000;
620 //uint32_t color_mask = 0xFFFF0000;
623 uint32_t fg_color[3] = {
625 htobe16((fgcol & 0xFFFF)),
628 uint32_t bg_color[3] = {
630 htobe16((bgcol & 0xFFFF)),
634 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (y1 * pitch)];
636 int32_t line_step = pitch;
639 int16_t dx, dy, dx_abs, dy_abs, ix, iy, x = x1;
653 if (draw_mode & DRAWMODE_INVERSVID)
655 if (draw_mode & DRAWMODE_COMPLEMENT) {
662 if (dx_abs >= dy_abs) {
663 if (!len) len = dx_abs;
664 for (uint16_t i = 0; i < len; i++) {
676 if (!len) len = dy_abs;
677 for (uint16_t i = 0; i < len; i++) {
690 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) {
691 uint16_t pitch = rtg_x[3];
692 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (dy * pitch)];
694 uint8_t cur_bit, base_bit, base_byte;
695 uint16_t cur_byte = 0, u8_fg = 0;
696 //uint32_t color_mask = 0xFFFFFFFF;
698 uint32_t plane_size = src_line_pitch * h;
699 uint8_t *bmp_data = bmp_data_src;
701 cur_bit = base_bit = (0x80 >> (sx % 8));
702 cur_byte = base_byte = ((sx / 8) % src_line_pitch);
704 if (realtime_graphics_debug) {
705 printf("P2C: %d,%d - %d,%d (%dx%d) %d, %.2X\n", sx, sy, dx, dy, w, h, planes, layer_mask);
706 printf("Mask: %.2X Minterm: %.2X\n", mask, draw_mode);
707 printf("Pitch: %d Src Pitch: %d (!!!: %.4X)\n", pitch, src_line_pitch, rtg_user[0]);
708 printf("Curbyte: %d Curbit: %d\n", cur_byte, cur_bit);
709 printf("Plane size: %d Total size: %d (%X)\n", plane_size, plane_size * planes, plane_size * planes);
710 printf("Source: %.8X - %.8X\n", rtg_address[1], rtg_address_adj[1]);
711 printf("Target: %.8X - %.8X\n", rtg_address[0], rtg_address_adj[0]);
714 printf("Grabbing data from RTG memory.\nData:\n");
715 for (int i = 0; i < h; i++) {
716 for (int k = 0; k < planes; k++) {
717 for (int j = 0; j < src_line_pitch; j++) {
718 printf("%.2X", bmp_data_src[j + (i * src_line_pitch) + (plane_size * k)]);
726 for (int16_t line_y = 0; line_y < h; line_y++) {
727 for (int16_t x = dx; x < dx + w; x++) {
729 if (draw_mode & 0x01) {
730 DECODE_INVERTED_PLANAR_PIXEL(u8_fg)
733 DECODE_PLANAR_PIXEL(u8_fg)
736 if (mask == 0xFF && (draw_mode == MINTERM_SRC || draw_mode == MINTERM_NOTSRC)) {
741 HANDLE_MINTERM_PIXEL(u8_fg, dptr[x], rtg_display_format);
744 if ((cur_bit >>= 1) == 0) {
747 cur_byte %= src_line_pitch;
752 if ((line_y + sy + 1) % h)
753 bmp_data += src_line_pitch;
755 bmp_data = bmp_data_src;
757 cur_byte = base_byte;