5 #include "../../../config_file/config_file.h"
7 #include "../../../gpio/gpio.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 = 1; ys < h; ys++) {
56 for (int xs = 0; xs < w; xs++) {
57 SET_RTG_PIXEL_MASK(&dptr[xs], (color & 0xFF), format);
62 void rtg_invertrect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t pitch, uint16_t format, uint8_t mask) {
64 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (x << format) + (y * pitch)];
65 for (int ys = 1; ys < h; ys++) {
68 for (int xs = 0; xs < w; xs++) {
74 for (int xs = 0; xs < w; xs++) {
75 ((uint16_t *)dptr)[xs] = ~((uint16_t *)dptr)[xs];
80 for (int xs = 0; xs < w; xs++) {
81 ((uint32_t *)dptr)[xs] = ~((uint32_t *)dptr)[xs];
90 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) {
92 uint8_t *sptr = &rtg_mem[rtg_address_adj[0] + (x << format) + (y * pitch)];
93 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (dx << format) + (dy * pitch)];
95 uint32_t xdir = 1, pitchstep = pitch;
99 sptr += ((h - 1) * pitch);
100 dptr += ((h - 1) * pitch);
106 for (int ys = 0; ys < h; ys++) {
108 for (int xs = 0; xs < w; xs++) {
109 SET_RTG_PIXEL_MASK(&dptr[xs], sptr[xs], format);
113 for (int xs = w - 1; xs >= x; xs--) {
114 SET_RTG_PIXEL_MASK(&dptr[xs], sptr[xs], format);
122 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) {
123 uint8_t *sptr = &rtg_mem[rtg_address_adj[0] + (x << format) + (y * pitch)];
124 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (dx << format) + (dy * pitch)];
126 uint32_t xdir = 1, pitchstep = pitch;
130 sptr += ((h - 1) * pitch);
131 dptr += ((h - 1) * pitch);
137 for (int ys = 0; ys < h; ys++) {
139 memcpy(dptr, sptr, w << format);
141 memmove(dptr, sptr, w << format);
147 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) {
149 uint8_t *sptr = &rtg_mem[src_addr - (PIGFX_RTG_BASE + PIGFX_REG_SIZE) + (sx << format) + (sy * srcpitch)];
150 uint8_t *dptr = &rtg_mem[dst_addr - (PIGFX_RTG_BASE + PIGFX_REG_SIZE) + (dx << format) + (dy * dstpitch)];
152 uint32_t xdir = 1, src_pitchstep = srcpitch, dst_pitchstep = dstpitch;
153 uint8_t draw_mode = minterm;
154 uint32_t mask = 0xFF;
156 if (src_addr == dst_addr) {
158 src_pitchstep = -srcpitch;
159 sptr += ((h - 1) * srcpitch);
160 dst_pitchstep = -dstpitch;
161 dptr += ((h - 1) * dstpitch);
168 if (format == RTGFMT_RBG565)
170 if (format == RTGFMT_RGB32)
173 if (minterm == MINTERM_SRC) {
174 for (int ys = 0; ys < h; ys++) {
176 memcpy(dptr, sptr, w << format);
178 memmove(dptr, sptr, w << format);
179 sptr += src_pitchstep;
180 dptr += dst_pitchstep;
184 for (int ys = 0; ys < h; ys++) {
186 for (int xs = 0; xs < w; xs++) {
189 HANDLE_MINTERM_PIXEL(sptr[xs], dptr[xs], format);
192 HANDLE_MINTERM_PIXEL(((uint16_t *)sptr)[xs], ((uint16_t *)dptr)[xs], format);
195 HANDLE_MINTERM_PIXEL(((uint32_t *)sptr)[xs], ((uint32_t *)dptr)[xs], format);
201 for (int xs = w - 1; xs >= sx; xs--) {
204 HANDLE_MINTERM_PIXEL(sptr[xs], dptr[xs], format);
207 HANDLE_MINTERM_PIXEL(((uint16_t *)sptr)[xs], ((uint16_t *)dptr)[xs], format);
210 HANDLE_MINTERM_PIXEL(((uint32_t *)sptr)[xs], ((uint32_t *)dptr)[xs], format);
215 sptr += src_pitchstep;
216 dptr += src_pitchstep;
221 extern struct emulator_config *cfg;
223 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) {
226 uint8_t *dptr = &rtg_mem[rtg_address_adj[1] + (x << format) + (y * pitch)];
227 uint8_t *sptr = NULL;
228 uint8_t cur_bit = 0, base_bit = 0, cur_byte = 0;
229 uint8_t invert = (draw_mode & DRAWMODE_INVERSVID);
234 tmpl_x = offset_x / 8;
235 cur_bit = base_bit = (0x80 >> (offset_x % 8));
237 if (realtime_graphics_debug) {
238 printf("DEBUG: BlitTemplate - %d, %d (%dx%d)\n", x, y, w, h);
239 printf("Src: %.8X (%.8X)\n", src_addr, rtg_address_adj[0]);
240 printf("Dest: %.8X (%.8X)\n", rtg_address[1], rtg_address_adj[1]);
241 printf("pitch: %d t_pitch: %d format: %d\n", pitch, t_pitch, format);
242 printf("offset_x: %d mask: %.2X draw_mode: %d\n", offset_x, mask, draw_mode);
245 uint32_t fg_color[3] = {
247 htobe16((fgcol & 0xFFFF)),
250 uint32_t bg_color[3] = {
252 htobe16((bgcol & 0xFFFF)),
256 if (src_addr >= (PIGFX_RTG_BASE + PIGFX_REG_SIZE)) {
257 sptr = &rtg_mem[src_addr - (PIGFX_RTG_BASE + PIGFX_REG_SIZE)];
258 if (realtime_graphics_debug) {
259 printf("Origin: %.8X\n", rtg_address[2]);
260 printf("Grabbing data from RTG memory.\nData:\n");
261 for (int i = 0; i < h; i++) {
262 for (int j = 0; j < t_pitch; j++) {
263 printf("%.2X", sptr[j + (i * t_pitch)]);
268 printf("Data available at origin:\n");
269 for (int i = 0; i < h; i++) {
270 for (int j = 0; j < w; j++) {
271 printf("%.2X", read8(rtg_address[2] + j + (i * t_pitch)));
279 int i = get_mapped_item_by_address(cfg, src_addr);
281 sptr = &cfg->map_data[i][src_addr - cfg->map_offset[i]];
282 if (realtime_graphics_debug) {
283 printf("Grabbing data from maping %d - offset %.8X\nData:\n", i, src_addr - cfg->map_offset[i]);
284 for (int i = 0; i < h; i++) {
285 for (int j = 0; j < t_pitch; j++) {
286 printf("%.2X", sptr[j + (i * t_pitch)]);
293 printf("BlitTemplate: Failed to find mapped range for address %.8X\n", src_addr);
300 for (uint16_t ys = 0; ys < h; ys++) {
301 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
303 for (int xs = 0; xs < w; xs++) {
304 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
305 if (mask == 0xFF || format != RTGFMT_8BIT) {
306 SET_RTG_PIXELS(&dptr[xs << format], fg_color[format], format);
309 SET_RTG_PIXELS_MASK(&dptr[xs], fg_color[format], format);
314 while (cur_bit > 0 && xs < w) {
315 if (cur_byte & cur_bit) {
316 if (mask == 0xFF || format != RTGFMT_8BIT) {
317 SET_RTG_PIXEL(&dptr[xs << format], fg_color[format], format);
320 SET_RTG_PIXEL_MASK(&dptr[xs], fg_color[format], format);
335 for (uint16_t ys = 0; ys < h; ys++) {
336 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
338 for (int xs = 0; xs < w; xs++) {
339 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
340 if (mask == 0xFF || format != RTGFMT_8BIT) {
341 SET_RTG_PIXELS2_COND(&dptr[xs << format], fg_color[format], bg_color[format], format);
344 SET_RTG_PIXELS2_COND_MASK(&dptr[xs << format], fg_color[format], bg_color[format], format);
350 while (cur_bit > 0 && xs < w) {
351 if (mask == 0xFF || format != RTGFMT_8BIT) {
352 SET_RTG_PIXEL(&dptr[xs << format], (cur_byte & cur_bit) ? fg_color[format] : bg_color[format], format);
355 SET_RTG_PIXEL_MASK(&dptr[xs << format], (cur_byte & cur_bit) ? fg_color[format] : bg_color[format], format);
368 case DRAWMODE_COMPLEMENT:
369 for (uint16_t ys = 0; ys < h; ys++) {
370 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
372 for (int xs = 0; xs < w; xs++) {
373 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
374 INVERT_RTG_PIXELS(&dptr[xs << format], format)
378 while (cur_bit > 0 && xs < w) {
379 if (cur_byte & cur_bit) {
380 INVERT_RTG_PIXEL(&dptr[xs << format], format)
396 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) {
399 uint8_t *dptr = &rtg_mem[rtg_address_adj[1] + (x << format) + (y * pitch)];
400 uint8_t *sptr = NULL, *sptr_base = NULL;
401 uint8_t cur_bit = 0, base_bit = 0, cur_byte = 0;
402 uint8_t invert = (draw_mode & DRAWMODE_INVERSVID);
407 tmpl_x = (offset_x / 8) % 2;
408 cur_bit = base_bit = (0x80 >> (offset_x % 8));
410 uint32_t fg_color[3] = {
412 htobe16((fgcol & 0xFFFF)),
415 uint32_t bg_color[3] = {
417 htobe16((bgcol & 0xFFFF)),
422 if (src_addr >= (PIGFX_RTG_BASE + PIGFX_REG_SIZE))
423 sptr = &rtg_mem[src_addr - (PIGFX_RTG_BASE + PIGFX_REG_SIZE)];
425 int i = get_mapped_item_by_address(cfg, src_addr);
427 sptr = &cfg->map_data[i][src_addr - cfg->map_offset[i]];
430 printf("BlitPattern: Failed to find mapped range for address %.8X\n", src_addr);
436 sptr += (offset_y % loop_rows) * 2;
440 for (uint16_t ys = 0; ys < h; ys++) {
441 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
443 for (int xs = 0; xs < w; xs++) {
444 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
445 if (mask == 0xFF || format != RTGFMT_8BIT) {
446 SET_RTG_PIXELS(&dptr[xs << format], fg_color[format], format);
449 SET_RTG_PIXELS_MASK(&dptr[xs], fg_color[format], format);
454 while (cur_bit > 0 && xs < w) {
455 if (cur_byte & cur_bit) {
456 if (mask == 0xFF || format != RTGFMT_8BIT) {
457 SET_RTG_PIXEL(&dptr[xs << format], fg_color[format], format);
460 SET_RTG_PIXEL_MASK(&dptr[xs], fg_color[format], format);
475 for (uint16_t ys = 0; ys < h; ys++) {
476 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
478 for (int xs = 0; xs < w; xs++) {
479 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
480 if (mask == 0xFF || format != RTGFMT_8BIT) {
481 SET_RTG_PIXELS2_COND(&dptr[xs << format], fg_color[format], bg_color[format], format);
484 SET_RTG_PIXELS2_COND_MASK(&dptr[xs << format], fg_color[format], bg_color[format], format);
490 while (cur_bit > 0 && xs < w) {
491 if (mask == 0xFF || format != RTGFMT_8BIT) {
492 SET_RTG_PIXEL(&dptr[xs << format], (cur_byte & cur_bit) ? fg_color[format] : bg_color[format], format);
495 SET_RTG_PIXEL_MASK(&dptr[xs << format], (cur_byte & cur_bit) ? fg_color[format] : bg_color[format], format);
508 case DRAWMODE_COMPLEMENT:
509 for (uint16_t ys = 0; ys < h; ys++) {
510 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
512 for (int xs = 0; xs < w; xs++) {
513 if (w >= 8 && cur_bit == 0x80 && xs < w - 8) {
514 INVERT_RTG_PIXELS(&dptr[xs << format], format)
518 while (cur_bit > 0 && xs < w) {
519 if (cur_byte & cur_bit) {
520 INVERT_RTG_PIXEL(&dptr[xs << format], format)
536 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) {
537 int16_t x1 = x1_, y1 = y1_;
538 int16_t x2 = x1_ + x2_, y2 = y1 + y2_;
540 uint32_t fg_color[3] = {
542 htobe16((fgcol & 0xFFFF)),
546 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (y1 * pitch)];
548 int32_t line_step = pitch;
551 int16_t dx, dy, dx_abs, dy_abs, ix, iy, x = x1;
565 SET_RTG_PIXEL(&dptr[x << format], fg_color[format], format);
567 if (dx_abs >= dy_abs) {
568 if (!len) len = dx_abs;
569 for (uint16_t i = 0; i < len; i++) {
577 SET_RTG_PIXEL(&dptr[x << format], fg_color[format], format);
581 if (!len) len = dy_abs;
582 for (uint16_t i = 0; i < len; i++) {
590 SET_RTG_PIXEL(&dptr[x << format], fg_color[format], format);
595 #define DRAW_LINE_PIXEL \
596 if (pattern & cur_bit) { \
597 if (invert) { INVERT_RTG_PIXEL(&dptr[x << format], format) } \
599 if (mask == 0xFF || format != RTGFMT_8BIT) { SET_RTG_PIXEL(&dptr[x << format], fg_color[format], format); } \
600 else { SET_RTG_PIXEL_MASK(&dptr[x << format], fg_color[format], format); } \
603 else if (draw_mode == DRAWMODE_JAM2) { \
604 if (invert) { INVERT_RTG_PIXEL(&dptr[x << format], format) } \
606 if (mask == 0xFF || format != RTGFMT_8BIT) { SET_RTG_PIXEL(&dptr[x << format], bg_color[format], format); } \
607 else { SET_RTG_PIXEL_MASK(&dptr[x << format], bg_color[format], format); } \
610 if ((cur_bit >>= 1) == 0) \
613 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) {
614 if (pattern_offset) {}
616 int16_t x1 = x1_, y1 = y1_;
617 int16_t x2 = x1_ + x2_, y2 = y1 + y2_;
618 uint16_t cur_bit = 0x8000;
619 //uint32_t color_mask = 0xFFFF0000;
622 uint32_t fg_color[3] = {
624 htobe16((fgcol & 0xFFFF)),
627 uint32_t bg_color[3] = {
629 htobe16((bgcol & 0xFFFF)),
633 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (y1 * pitch)];
635 int32_t line_step = pitch;
638 int16_t dx, dy, dx_abs, dy_abs, ix, iy, x = x1;
652 if (draw_mode & DRAWMODE_INVERSVID)
654 if (draw_mode & DRAWMODE_COMPLEMENT) {
661 if (dx_abs >= dy_abs) {
662 if (!len) len = dx_abs;
663 for (uint16_t i = 0; i < len; i++) {
675 if (!len) len = dy_abs;
676 for (uint16_t i = 0; i < len; i++) {
689 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) {
690 uint16_t pitch = rtg_x[3];
691 uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (dy * pitch)];
693 uint8_t cur_bit, base_bit, base_byte;
694 uint16_t cur_byte = 0, u8_fg = 0;
695 //uint32_t color_mask = 0xFFFFFFFF;
697 uint32_t plane_size = src_line_pitch * h;
698 uint8_t *bmp_data = bmp_data_src;
700 cur_bit = base_bit = (0x80 >> (sx % 8));
701 cur_byte = base_byte = ((sx / 8) % src_line_pitch);
703 if (realtime_graphics_debug) {
704 printf("P2C: %d,%d - %d,%d (%dx%d) %d, %.2X\n", sx, sy, dx, dy, w, h, planes, layer_mask);
705 printf("Mask: %.2X Minterm: %.2X\n", mask, draw_mode);
706 printf("Pitch: %d Src Pitch: %d (!!!: %.4X)\n", pitch, src_line_pitch, rtg_user[0]);
707 printf("Curbyte: %d Curbit: %d\n", cur_byte, cur_bit);
708 printf("Plane size: %d Total size: %d (%X)\n", plane_size, plane_size * planes, plane_size * planes);
709 printf("Source: %.8X - %.8X\n", rtg_address[1], rtg_address_adj[1]);
710 printf("Target: %.8X - %.8X\n", rtg_address[0], rtg_address_adj[0]);
713 printf("Grabbing data from RTG memory.\nData:\n");
714 for (int i = 0; i < h; i++) {
715 for (int k = 0; k < planes; k++) {
716 for (int j = 0; j < src_line_pitch; j++) {
717 printf("%.2X", bmp_data_src[j + (i * src_line_pitch) + (plane_size * k)]);
725 for (int16_t line_y = 0; line_y < h; line_y++) {
726 for (int16_t x = dx; x < dx + w; x++) {
728 if (draw_mode & 0x01) {
729 DECODE_INVERTED_PLANAR_PIXEL(u8_fg)
732 DECODE_PLANAR_PIXEL(u8_fg)
735 if (mask == 0xFF && (draw_mode == MINTERM_SRC || draw_mode == MINTERM_NOTSRC)) {
740 HANDLE_MINTERM_PIXEL(u8_fg, dptr[x], rtg_display_format);
743 if ((cur_bit >>= 1) == 0) {
746 cur_byte %= src_line_pitch;
751 if ((line_y + sy + 1) % h)
752 bmp_data += src_line_pitch;
754 bmp_data = bmp_data_src;
756 cur_byte = base_byte;