]> git.sesse.net Git - pistorm/blob - platforms/amiga/rtg/rtg-gfx.c
Silence some compile warnings
[pistorm] / platforms / amiga / rtg / rtg-gfx.c
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "config_file/config_file.h"
6 #ifndef FAKESTORM
7 #include "gpio/ps_protocol.h"
8 #endif
9 #include "rtg.h"
10
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];
17
18 extern uint8_t realtime_graphics_debug;
19
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)];
22     switch(format) {
23         case RTGFMT_8BIT: {
24             for (int xs = 0; xs < w; xs++) {
25                 dptr[xs] = color & 0xFF;
26             }
27             break;
28         }
29         case RTGFMT_RBG565: {
30             color = htobe16((color & 0xFFFF));
31             uint16_t *ptr = (uint16_t *)dptr;
32             for (int xs = 0; xs < w; xs++) {
33                 ptr[xs] = color;
34             }
35             break;
36         }
37         case RTGFMT_RGB32: {
38             color = htobe32(color);
39             uint32_t *ptr = (uint32_t *)dptr;
40             for (int xs = 0; xs < w; xs++) {
41                 ptr[xs] = color;
42             }
43             break;
44         }
45     }
46     for (int ys = 1; ys < h; ys++) {
47         dptr += pitch;
48         memcpy(dptr, (void *)(size_t)(dptr - pitch), (w << format));
49     }
50 }
51
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)];
54
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);
58         }
59         dptr += pitch;
60     }
61 }
62
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) {
64     if (mask) {}
65     uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (x << format) + (y * pitch)];
66     for (int ys = 0; ys < h; ys++) {
67         switch(format) {
68             case RTGFMT_8BIT: {
69                 for (int xs = 0; xs < w; xs++) {
70                     dptr[xs] ^= mask;
71                 }
72                 break;
73             }
74             case RTGFMT_RBG565: {
75                 for (int xs = 0; xs < w; xs++) {
76                     ((uint16_t *)dptr)[xs] = ~((uint16_t *)dptr)[xs];
77                 }
78                 break;
79             }
80             case RTGFMT_RGB32: {
81                 for (int xs = 0; xs < w; xs++) {
82                     ((uint32_t *)dptr)[xs] = ~((uint32_t *)dptr)[xs];
83                 }
84                 break;
85             }
86         }
87         dptr += pitch;
88     }
89 }
90
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) {
92     if (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)];
95
96     uint32_t xdir = 1, pitchstep = pitch;
97
98     if (y < dy) {
99         pitchstep = -pitch;
100         sptr += ((h - 1) * pitch);
101         dptr += ((h - 1) * pitch);
102     }
103     if (x < dx) {
104         xdir = 0;
105     }
106
107     for (int ys = 0; ys < h; ys++) {
108         if (xdir) {
109             for (int xs = 0; xs < w; xs++) {
110                 SET_RTG_PIXEL_MASK(&dptr[xs], sptr[xs], format);
111             }
112         }
113         else {
114             for (int xs = w - 1; xs >= x; xs--) {
115                 SET_RTG_PIXEL_MASK(&dptr[xs], sptr[xs], format);
116             }
117         }
118         sptr += pitchstep;
119         dptr += pitchstep;
120     }
121 }
122
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)];
126
127     uint32_t xdir = 1, pitchstep = pitch;
128
129     if (y < dy) {
130         pitchstep = -pitch;
131         sptr += ((h - 1) * pitch);
132         dptr += ((h - 1) * pitch);
133     }
134     if (x < dx) {
135         xdir = 0;
136     }
137
138     for (int ys = 0; ys < h; ys++) {
139         if (xdir)
140             memcpy(dptr, sptr, w << format);
141         else
142             memmove(dptr, sptr, w << format);
143         sptr += pitchstep;
144         dptr += pitchstep;
145     }
146 }
147
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) {
149     if (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)];
152
153     uint32_t xdir = 1, src_pitchstep = srcpitch, dst_pitchstep = dstpitch;
154     uint8_t draw_mode = minterm;
155     uint32_t mask = 0xFF;
156
157     if (src_addr == dst_addr) {
158         if (sy < dy) {
159             src_pitchstep = -srcpitch;
160             sptr += ((h - 1) * srcpitch);
161             dst_pitchstep = -dstpitch;
162             dptr += ((h - 1) * dstpitch);
163         }
164         if (sx < dx) {
165             xdir = 0;
166         }
167     }
168
169     if (format == RTGFMT_RBG565)
170         mask = 0xFFFF;
171     if (format == RTGFMT_RGB32)
172         mask = 0xFFFFFFFF;
173
174     if (minterm == MINTERM_SRC) {
175         for (int ys = 0; ys < h; ys++) {
176             if (xdir)
177                 memcpy(dptr, sptr, w << format);
178             else
179                 memmove(dptr, sptr, w << format);
180             sptr += src_pitchstep;
181             dptr += dst_pitchstep;
182         }
183     }
184     else {
185         for (int ys = 0; ys < h; ys++) {
186             if (xdir) {
187                 for (int xs = 0; xs < w; xs++) {
188                     switch (format) {
189                         case RTGFMT_8BIT:
190                             HANDLE_MINTERM_PIXEL(sptr[xs], dptr[xs], format);
191                             break;
192                         case RTGFMT_RBG565:
193                             HANDLE_MINTERM_PIXEL(((uint16_t *)sptr)[xs], ((uint16_t *)dptr)[xs], format);
194                             break;
195                         case RTGFMT_RGB32:
196                             HANDLE_MINTERM_PIXEL(((uint32_t *)sptr)[xs], ((uint32_t *)dptr)[xs], format);
197                             break;
198                     }
199                 }
200             }
201             else {
202                 for (int xs = w - 1; xs >= sx; xs--) {
203                     switch (format) {
204                         case RTGFMT_8BIT:
205                             HANDLE_MINTERM_PIXEL(sptr[xs], dptr[xs], format);
206                             break;
207                         case RTGFMT_RBG565:
208                             HANDLE_MINTERM_PIXEL(((uint16_t *)sptr)[xs], ((uint16_t *)dptr)[xs], format);
209                             break;
210                         case RTGFMT_RGB32:
211                             HANDLE_MINTERM_PIXEL(((uint32_t *)sptr)[xs], ((uint32_t *)dptr)[xs], format);
212                             break;
213                     }
214                 }
215             }
216             sptr += src_pitchstep;
217             dptr += src_pitchstep;
218         }
219     }
220 }
221
222 extern struct emulator_config *cfg;
223
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) {
225     if (mask) {}
226
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);
231     uint16_t tmpl_x = 0;
232
233     draw_mode &= 0x03;
234
235         tmpl_x = offset_x / 8;
236     cur_bit = base_bit = (0x80 >> (offset_x % 8));
237
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);
244     }
245
246     uint32_t fg_color[3] = {
247         (fgcol & 0xFF),
248         htobe16((fgcol & 0xFFFF)),
249         htobe32(fgcol),
250     };
251     uint32_t bg_color[3] = {
252         (bgcol & 0xFF),
253         htobe16((bgcol & 0xFFFF)),
254         htobe32(bgcol),
255     };
256
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)]);
265                 }
266                 printf("\n");
267             }
268 #ifndef FAKESTORM
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)));
273                 }
274                 printf("\n");
275             }
276 #endif
277         }
278     }
279     else {
280         int i = get_mapped_item_by_address(cfg, src_addr);
281         if (i != -1) {
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 %.8lX\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)]);
288                     }
289                     printf("\n");
290                 }
291             }
292         }
293         else {
294             printf("BlitTemplate: Failed to find mapped range for address %.8X\n", src_addr);
295             return;
296         }
297     }
298
299     switch (draw_mode) {
300         case DRAWMODE_JAM1:
301             for (uint16_t ys = 0; ys < h; ys++) {
302                 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
303
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);
308                         }
309                         else {
310                             SET_RTG_PIXELS_MASK(&dptr[xs], fg_color[format], format);
311                         }
312                         xs += 7;
313                     }
314                     else {
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);
319                                 }
320                                 else {
321                                     SET_RTG_PIXEL_MASK(&dptr[xs], fg_color[format], format);
322                                 }
323                             }
324                             xs++;
325                             cur_bit >>= 1;
326                         }
327                         xs--;
328                         cur_bit = 0x80;
329                     }
330                     TEMPLATE_LOOPX;
331                 }
332                 TEMPLATE_LOOPY;
333             }
334             return;
335         case DRAWMODE_JAM2:
336             for (uint16_t ys = 0; ys < h; ys++) {
337                 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
338
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);
343                         }
344                         else {
345                             SET_RTG_PIXELS2_COND_MASK(&dptr[xs << format], fg_color[format], bg_color[format], format);
346                         }
347
348                         xs += 7;
349                     }
350                     else {
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);
354                             }
355                             else {
356                                 SET_RTG_PIXEL_MASK(&dptr[xs << format], (cur_byte & cur_bit) ? fg_color[format] : bg_color[format], format);
357                             }
358                             xs++;
359                             cur_bit >>= 1;
360                         }
361                         xs--;
362                         cur_bit = 0x80;
363                     }
364                     TEMPLATE_LOOPX;
365                 }
366                 TEMPLATE_LOOPY;
367             }
368             return;
369         case DRAWMODE_COMPLEMENT:
370             for (uint16_t ys = 0; ys < h; ys++) {
371                 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
372
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)
376                         xs += 7;
377                     }
378                     else {
379                         while (cur_bit > 0 && xs < w) {
380                             if (cur_byte & cur_bit) {
381                                 INVERT_RTG_PIXEL(&dptr[xs << format], format)
382                             }
383                             xs++;
384                             cur_bit >>= 1;
385                         }
386                         xs--;
387                         cur_bit = 0x80;
388                     }
389                     TEMPLATE_LOOPX;
390                 }
391                 TEMPLATE_LOOPY;
392             }
393             return;
394     }
395 }
396
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) {
398     if (mask) {}
399
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);
404     uint16_t tmpl_x = 0;
405
406     draw_mode &= 0x03;
407
408         tmpl_x = (offset_x / 8) % 2;
409     cur_bit = base_bit = (0x80 >> (offset_x % 8));
410
411     uint32_t fg_color[3] = {
412         (fgcol & 0xFF),
413         htobe16((fgcol & 0xFFFF)),
414         htobe32(fgcol),
415     };
416     uint32_t bg_color[3] = {
417         (bgcol & 0xFF),
418         htobe16((bgcol & 0xFFFF)),
419         htobe32(bgcol),
420     };
421
422
423     if (src_addr >= (PIGFX_RTG_BASE + PIGFX_REG_SIZE))
424         sptr = &rtg_mem[src_addr - (PIGFX_RTG_BASE + PIGFX_REG_SIZE)];
425     else {
426         int i = get_mapped_item_by_address(cfg, src_addr);
427         if (i != -1) {
428             sptr = &cfg->map_data[i][src_addr - cfg->map_offset[i]];
429         }
430         else {
431             printf("BlitPattern: Failed to find mapped range for address %.8X\n", src_addr);
432             return;
433         }
434     }
435
436     sptr_base = sptr;
437     sptr += (offset_y % loop_rows) * 2;
438
439     switch (draw_mode) {
440         case DRAWMODE_JAM1:
441             for (uint16_t ys = 0; ys < h; ys++) {
442                 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
443
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);
448                         }
449                         else {
450                             SET_RTG_PIXELS_MASK(&dptr[xs], fg_color[format], format);
451                         }
452                         xs += 7;
453                     }
454                     else {
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);
459                                 }
460                                 else {
461                                     SET_RTG_PIXEL_MASK(&dptr[xs], fg_color[format], format);
462                                 }
463                             }
464                             xs++;
465                             cur_bit >>= 1;
466                         }
467                         xs--;
468                         cur_bit = 0x80;
469                     }
470                     PATTERN_LOOPX;
471                 }
472                 PATTERN_LOOPY;
473             }
474             return;
475         case DRAWMODE_JAM2:
476             for (uint16_t ys = 0; ys < h; ys++) {
477                 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
478
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);
483                         }
484                         else {
485                             SET_RTG_PIXELS2_COND_MASK(&dptr[xs << format], fg_color[format], bg_color[format], format);
486                         }
487
488                         xs += 7;
489                     }
490                     else {
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);
494                             }
495                             else {
496                                 SET_RTG_PIXEL_MASK(&dptr[xs << format], (cur_byte & cur_bit) ? fg_color[format] : bg_color[format], format);
497                             }
498                             xs++;
499                             cur_bit >>= 1;
500                         }
501                         xs--;
502                         cur_bit = 0x80;
503                     }
504                     PATTERN_LOOPX;
505                 }
506                 PATTERN_LOOPY;
507             }
508             return;
509         case DRAWMODE_COMPLEMENT:
510             for (uint16_t ys = 0; ys < h; ys++) {
511                 cur_byte = (invert) ? sptr[tmpl_x] ^ 0xFF : sptr[tmpl_x];
512
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)
516                         xs += 7;
517                     }
518                     else {
519                         while (cur_bit > 0 && xs < w) {
520                             if (cur_byte & cur_bit) {
521                                 INVERT_RTG_PIXEL(&dptr[xs << format], format)
522                             }
523                             xs++;
524                             cur_bit >>= 1;
525                         }
526                         xs--;
527                         cur_bit = 0x80;
528                     }
529                     PATTERN_LOOPX;
530                 }
531                 PATTERN_LOOPY;
532             }
533             return;
534     }
535 }
536
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_;
540
541     uint32_t fg_color[3] = {
542         (fgcol & 0xFF),
543         htobe16((fgcol & 0xFFFF)),
544         htobe32(fgcol),
545     };
546
547     uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (y1 * pitch)];
548
549         int32_t line_step = pitch;
550         int8_t x_step = 1;
551
552         int16_t dx, dy, dx_abs, dy_abs, ix, iy, x = x1;
553
554         if (x2 < x1)
555                 x_step = -1;
556         if (y2 < y1)
557                 line_step = -pitch;
558
559         dx = x2 - x1;
560         dy = y2 - y1;
561         dx_abs = abs(dx);
562         dy_abs = abs(dy);
563         ix = dy_abs >> 1;
564         iy = dx_abs >> 1;
565
566     SET_RTG_PIXEL(&dptr[x << format], fg_color[format], format);
567
568         if (dx_abs >= dy_abs) {
569                 if (!len) len = dx_abs;
570                 for (uint16_t i = 0; i < len; i++) {
571                         iy += dy_abs;
572                         if (iy >= dx_abs) {
573                                 iy -= dx_abs;
574                                 dptr += line_step;
575                         }
576                         x += x_step;
577
578             SET_RTG_PIXEL(&dptr[x << format], fg_color[format], format);
579                 }
580         }
581         else {
582                 if (!len) len = dy_abs;
583                 for (uint16_t i = 0; i < len; i++) {
584                         ix += dx_abs;
585                         if (ix >= dy_abs) {
586                                 ix -= dy_abs;
587                                 x += x_step;
588                         }
589                         dptr += line_step;
590
591                         SET_RTG_PIXEL(&dptr[x << format], fg_color[format], format);
592                 }
593         }
594 }
595
596 #define DRAW_LINE_PIXEL \
597     if (pattern & cur_bit) { \
598         if (invert) { INVERT_RTG_PIXEL(&dptr[x << format], format) } \
599         else { \
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); } \
602         } \
603     } \
604     else if (draw_mode == DRAWMODE_JAM2) { \
605         if (invert) { INVERT_RTG_PIXEL(&dptr[x << format], format) } \
606         else { \
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); } \
609         } \
610     } \
611     if ((cur_bit >>= 1) == 0) \
612             cur_bit = 0x8000;
613
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) {}
616
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;
621     uint8_t invert = 0;
622
623     uint32_t fg_color[3] = {
624         (fgcol & 0xFF),
625         htobe16((fgcol & 0xFFFF)),
626         htobe32(fgcol),
627     };
628     uint32_t bg_color[3] = {
629         (bgcol & 0xFF),
630         htobe16((bgcol & 0xFFFF)),
631         htobe32(bgcol),
632     };
633
634     uint8_t *dptr = &rtg_mem[rtg_address_adj[0] + (y1 * pitch)];
635
636         int32_t line_step = pitch;
637         int8_t x_step = 1;
638
639         int16_t dx, dy, dx_abs, dy_abs, ix, iy, x = x1;
640
641         if (x2 < x1)
642                 x_step = -1;
643         if (y2 < y1)
644                 line_step = -pitch;
645
646         dx = x2 - x1;
647         dy = y2 - y1;
648         dx_abs = abs(dx);
649         dy_abs = abs(dy);
650         ix = dy_abs >> 1;
651         iy = dx_abs >> 1;
652
653     if (draw_mode & DRAWMODE_INVERSVID)
654         pattern = ~pattern;
655     if (draw_mode & DRAWMODE_COMPLEMENT) {
656         invert = 1;
657     }
658     draw_mode &= 0x01;
659
660     DRAW_LINE_PIXEL;
661
662         if (dx_abs >= dy_abs) {
663                 if (!len) len = dx_abs;
664                 for (uint16_t i = 0; i < len; i++) {
665                         iy += dy_abs;
666                         if (iy >= dx_abs) {
667                                 iy -= dx_abs;
668                                 dptr += line_step;
669                         }
670                         x += x_step;
671
672             DRAW_LINE_PIXEL;
673                 }
674         }
675         else {
676                 if (!len) len = dy_abs;
677                 for (uint16_t i = 0; i < len; i++) {
678                         ix += dx_abs;
679                         if (ix >= dy_abs) {
680                                 ix -= dy_abs;
681                                 x += x_step;
682                         }
683                         dptr += line_step;
684
685                         DRAW_LINE_PIXEL;
686                 }
687         }
688 }
689
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)];
693
694         uint8_t cur_bit, base_bit, base_byte;
695         uint16_t cur_byte = 0, u8_fg = 0;
696     //uint32_t color_mask = 0xFFFFFFFF;
697
698         uint32_t plane_size = src_line_pitch * h;
699         uint8_t *bmp_data = bmp_data_src;
700
701         cur_bit = base_bit = (0x80 >> (sx % 8));
702         cur_byte = base_byte = ((sx / 8) % src_line_pitch);
703
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]);
712         fflush(stdout);
713
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)]);
719                 }
720                 printf("  ");
721             }
722             printf("\n");
723         }
724     }
725
726         for (int16_t line_y = 0; line_y < h; line_y++) {
727                 for (int16_t x = dx; x < dx + w; x++) {
728                         u8_fg = 0;
729                         if (draw_mode & 0x01) {
730                                 DECODE_INVERTED_PLANAR_PIXEL(u8_fg)
731             }
732                         else {
733                                 DECODE_PLANAR_PIXEL(u8_fg)
734             }
735
736                         if (mask == 0xFF && (draw_mode == MINTERM_SRC || draw_mode == MINTERM_NOTSRC)) {
737                                 dptr[x] = u8_fg;
738                                 goto skip;
739                         }
740
741             HANDLE_MINTERM_PIXEL(u8_fg, dptr[x], rtg_display_format);
742
743                         skip:;
744                         if ((cur_bit >>= 1) == 0) {
745                                 cur_bit = 0x80;
746                                 cur_byte++;
747                                 cur_byte %= src_line_pitch;
748                         }
749
750                 }
751                 dptr += pitch;
752                 if ((line_y + sy + 1) % h)
753                         bmp_data += src_line_pitch;
754                 else
755                         bmp_data = bmp_data_src;
756                 cur_bit = base_bit;
757                 cur_byte = base_byte;
758         }
759 }