]> git.sesse.net Git - pistorm/blob - platforms/amiga/rtg/rtg_driver_amiga/pigfx.c
Fix up P2C/P2D, fix 256th color missing in 8bpp modes
[pistorm] / platforms / amiga / rtg / rtg_driver_amiga / pigfx.c
1 // SPDX-License-Identifier: MIT
2
3 // PiStorm RTG driver, VBCC edition.
4 // Based in part on the ZZ9000 RTG driver.
5
6 #include <proto/exec.h>
7 #include <proto/expansion.h>
8 #include <proto/dos.h>
9 #include <proto/intuition.h>
10 #include <exec/types.h>
11 #include <exec/memory.h>
12 #include <exec/libraries.h>
13 #include <exec/execbase.h>
14 #include <exec/resident.h>
15 #include <exec/initializers.h>
16 #include <clib/debug_protos.h>
17 #include <string.h>
18 #include <stdint.h>
19 #include "boardinfo.h"
20 #include "rtg_enums.h"
21
22 #define WRITESHORT(cmd, val) *(unsigned short *)((unsigned long)(b->RegisterBase)+cmd) = val;
23 #define WRITELONG(cmd, val) *(unsigned long *)((unsigned long)(b->RegisterBase)+cmd) = val;
24 #define WRITEBYTE(cmd, val) *(unsigned char *)((unsigned long)(b->RegisterBase)+cmd) = val;
25
26 #define CHECKRTG *((unsigned short *)(CARD_OFFSET))
27
28 #define CARD_OFFSET   0x70000000
29 #define CARD_REGSIZE  0x00010000
30 #define CARD_MEMSIZE  0x02000000 // 32MB "VRAM"
31 #define CARD_SCRATCH  0x72010000
32
33 #define CHIP_RAM_SIZE 0x00200000 // Chip RAM offset, 2MB
34
35 const unsigned short rgbf_to_rtg[16] = {
36   RTGFMT_8BIT,      // 0x00
37   RTGFMT_8BIT,      // 0x01
38   0,                // 0x02
39   0,                // 0x03
40   0,                // 0x04
41   RTGFMT_RGB555,    // 0x05
42   0,                // 0x06
43   0,                // 0x07
44   RTGFMT_RGB32,     // 0x08
45   RTGFMT_RGB32,     // 0x09
46   RTGFMT_RBG565,    // 0x0A
47   RTGFMT_RGB555,    // 0x0B
48   0,                // 0x0C
49   RTGFMT_RGB555,    // 0x0D
50   0,                // 0x0E
51   0,                // 0x0F
52 };
53
54 struct GFXBase {
55   struct Library libNode;
56   BPTR segList;
57   struct ExecBase* sysBase;
58   struct ExpansionBase* expansionBase;
59 };
60
61 int FindCard(__REGA0(struct BoardInfo* b));
62 int InitCard(__REGA0(struct BoardInfo* b));
63
64 void SetDAC (__REGA0(struct BoardInfo *b), __REGD7(RGBFTYPE format));
65 void SetGC (__REGA0(struct BoardInfo *b), __REGA1(struct ModeInfo *mode_info), __REGD0(BOOL border));
66 void SetColorArray (__REGA0(struct BoardInfo *b), __REGD0(UWORD start), __REGD1(UWORD num));
67 void SetPanning (__REGA0(struct BoardInfo *b), __REGA1(UBYTE *addr), __REGD0(UWORD width), __REGD1(WORD x_offset), __REGD2(WORD y_offset), __REGD7(RGBFTYPE format));
68 UWORD SetSwitch (__REGA0(struct BoardInfo *b), __REGD0(UWORD enabled));
69 UWORD SetDisplay (__REGA0(struct BoardInfo *b), __REGD0(UWORD enabled));
70
71 UWORD CalculateBytesPerRow (__REGA0(struct BoardInfo *b), __REGD0(UWORD width), __REGD7(RGBFTYPE format));
72 APTR CalculateMemory (__REGA0(struct BoardInfo *b), __REGA1(unsigned long addr), __REGD7(RGBFTYPE format));
73 ULONG GetCompatibleFormats (__REGA0(struct BoardInfo *b), __REGD7(RGBFTYPE format));
74
75 LONG ResolvePixelClock (__REGA0(struct BoardInfo *b), __REGA1(struct ModeInfo *mode_info), __REGD0(ULONG pixel_clock), __REGD7(RGBFTYPE format));
76 ULONG GetPixelClock (__REGA0(struct BoardInfo *b), __REGA1(struct ModeInfo *mode_info), __REGD0(ULONG index), __REGD7(RGBFTYPE format));
77 void SetClock (__REGA0(struct BoardInfo *b));
78
79 void SetMemoryMode (__REGA0(struct BoardInfo *b), __REGD7(RGBFTYPE format));
80 void SetWriteMask (__REGA0(struct BoardInfo *b), __REGD0(UBYTE mask));
81 void SetClearMask (__REGA0(struct BoardInfo *b), __REGD0(UBYTE mask));
82 void SetReadPlane (__REGA0(struct BoardInfo *b), __REGD0(UBYTE plane));
83
84 void WaitVerticalSync (__REGA0(struct BoardInfo *b), __REGD0(BOOL toggle));
85
86 void FillRect (__REGA0(struct BoardInfo *b), __REGA1(struct RenderInfo *r), __REGD0(WORD x), __REGD1(WORD y), __REGD2(WORD w), __REGD3(WORD h), __REGD4(ULONG color), __REGD5(UBYTE mask), __REGD7(RGBFTYPE format));
87 void InvertRect (__REGA0(struct BoardInfo *b), __REGA1(struct RenderInfo *r), __REGD0(WORD x), __REGD1(WORD y), __REGD2(WORD w), __REGD3(WORD h), __REGD4(UBYTE mask), __REGD7(RGBFTYPE format));
88 void BlitRect (__REGA0(struct BoardInfo *b), __REGA1(struct RenderInfo *r), __REGD0(WORD x), __REGD1(WORD y), __REGD2(WORD dx), __REGD3(WORD dy), __REGD4(WORD w), __REGD5(WORD h), __REGD6(UBYTE mask), __REGD7(RGBFTYPE format));
89 void BlitRectNoMaskComplete (__REGA0(struct BoardInfo *b), __REGA1(struct RenderInfo *rs), __REGA2(struct RenderInfo *rt), __REGD0(WORD x), __REGD1(WORD y), __REGD2(WORD dx), __REGD3(WORD dy), __REGD4(WORD w), __REGD5(WORD h), __REGD6(UBYTE minterm), __REGD7(RGBFTYPE format));
90 void BlitTemplate (__REGA0(struct BoardInfo *b), __REGA1(struct RenderInfo *r), __REGA2(struct Template *t), __REGD0(WORD x), __REGD1(WORD y), __REGD2(WORD w), __REGD3(WORD h), __REGD4(UBYTE mask), __REGD7(RGBFTYPE format));
91 void BlitPattern (__REGA0(struct BoardInfo *b), __REGA1(struct RenderInfo *r), __REGA2(struct Pattern *p), __REGD0(WORD x), __REGD1(WORD y), __REGD2(WORD w), __REGD3(WORD h), __REGD4(UBYTE mask), __REGD7(RGBFTYPE format));
92 void DrawLine (__REGA0(struct BoardInfo *b), __REGA1(struct RenderInfo *r), __REGA2(struct Line *l), __REGD0(UBYTE mask), __REGD7(RGBFTYPE format));
93
94 void BlitPlanar2Chunky (__REGA0(struct BoardInfo *b), __REGA1(struct BitMap *bm), __REGA2(struct RenderInfo *r), __REGD0(SHORT x), __REGD1(SHORT y), __REGD2(SHORT dx), __REGD3(SHORT dy), __REGD4(SHORT w), __REGD5(SHORT h), __REGD6(UBYTE minterm), __REGD7(UBYTE mask));
95 void BlitPlanar2Direct (__REGA0(struct BoardInfo *b), __REGA1(struct BitMap *bmp), __REGA2(struct RenderInfo *r), __REGA3(struct ColorIndexMapping *clut), __REGD0(SHORT x), __REGD1(SHORT y), __REGD2(SHORT dx), __REGD3(SHORT dy), __REGD4(SHORT w), __REGD5(SHORT h), __REGD6(UBYTE minterm), __REGD7(UBYTE mask));
96
97 static ULONG LibStart(void) {
98   return(-1);
99 }
100
101 static const char LibraryName[] = "PiRTG.card";
102 static const char LibraryID[]   = "$VER: PiRTG.card 0.01\r\n";
103
104 __saveds struct GFXBase* OpenLib(__REGA6(struct GFXBase *gfxbase));
105 BPTR __saveds CloseLib(__REGA6(struct GFXBase *gfxbase));
106 BPTR __saveds ExpungeLib(__REGA6(struct GFXBase *exb));
107 ULONG ExtFuncLib(void);
108 __saveds struct GFXBase* InitLib(__REGA6(struct ExecBase *sysbase),
109                                  __REGA0(BPTR seglist),
110                                  __REGD0(struct GFXBase *exb));
111
112 static const APTR FuncTab[] = {
113   (APTR)OpenLib,
114   (APTR)CloseLib,
115   (APTR)ExpungeLib,
116   (APTR)ExtFuncLib,
117
118   (APTR)FindCard,
119   (APTR)InitCard,
120   (APTR)((LONG)-1)
121 };
122
123 struct InitTable
124 {
125   ULONG LibBaseSize;
126   APTR  FunctionTable;
127   APTR  DataTable;
128   APTR  InitLibTable;
129 };
130
131 static struct InitTable InitTab = {
132   (ULONG) sizeof(struct GFXBase),
133   (APTR) FuncTab,
134   (APTR) NULL,
135   (APTR) InitLib
136 };
137
138 static const struct Resident ROMTag = {
139         RTC_MATCHWORD,
140   &ROMTag,
141   &ROMTag + 1,
142   RTF_AUTOINIT,
143         83,
144   NT_LIBRARY,
145   0,
146   (char *)LibraryName,
147   (char *)LibraryID,
148   (APTR)&InitTab
149 };
150
151 #define CLOCK_HZ 100000000
152
153 static struct GFXBase *_gfxbase;
154 const char *gfxname = "PiStorm RTG";
155 char dummies[128];
156
157 __saveds struct GFXBase* InitLib(__REGA6(struct ExecBase *sysbase),
158                                           __REGA0(BPTR seglist),
159                                           __REGD0(struct GFXBase *exb))
160 {
161   _gfxbase = exb;
162   return _gfxbase;
163 }
164
165 __saveds struct GFXBase* OpenLib(__REGA6(struct GFXBase *gfxbase))
166 {
167   gfxbase->libNode.lib_OpenCnt++;
168   gfxbase->libNode.lib_Flags &= ~LIBF_DELEXP;
169
170   return gfxbase;
171 }
172
173 BPTR __saveds CloseLib(__REGA6(struct GFXBase *gfxbase))
174 {
175   gfxbase->libNode.lib_OpenCnt--;
176
177   if (!gfxbase->libNode.lib_OpenCnt) {
178     if (gfxbase->libNode.lib_Flags & LIBF_DELEXP) {
179       return (ExpungeLib(gfxbase));
180     }
181   }
182   return 0;
183 }
184
185 BPTR __saveds ExpungeLib(__REGA6(struct GFXBase *exb))
186 {
187   BPTR seglist;
188   struct ExecBase *SysBase = *(struct ExecBase **)4L;
189
190   if(!exb->libNode.lib_OpenCnt) {
191     ULONG negsize, possize, fullsize;
192     UBYTE *negptr = (UBYTE *)exb;
193
194     seglist = exb->segList;
195
196     Remove((struct Node *)exb);
197
198     negsize  = exb->libNode.lib_NegSize;
199     possize  = exb->libNode.lib_PosSize;
200     fullsize = negsize + possize;
201     negptr  -= negsize;
202
203     FreeMem(negptr, fullsize);
204     return(seglist);
205   }
206
207   exb->libNode.lib_Flags |= LIBF_DELEXP;
208   return 0;
209 }
210
211 ULONG ExtFuncLib(void)
212 {
213   return 0;
214 }
215
216 static LONG zorro_version = 0;
217
218 static struct GFXData *gfxdata;
219 //MNTZZ9KRegs* registers;
220
221 #define LOADLIB(a, b) if ((a = (struct a*)OpenLibrary(b,0L))==NULL) { \
222     KPrintF("Failed to load %s.\n", b); \
223     return 0; \
224   } \
225
226 static BYTE card_already_found;
227 static BYTE card_initialized;
228
229 int FindCard(__REGA0(struct BoardInfo* b)) {
230   //if (card_already_found)
231 //    return 1;
232   uint16_t card_check = CHECKRTG;
233   if (card_check != 0xFFCF) {
234     // RTG not enabled
235     return 0;
236   }
237
238   struct ConfigDev* cd = NULL;
239   struct ExpansionBase *ExpansionBase = NULL;
240   struct DOSBase *DOSBase = NULL;
241   struct IntuitionBase *IntuitionBase = NULL;
242   struct ExecBase *SysBase = *(struct ExecBase **)4L;
243
244   LOADLIB(ExpansionBase, "expansion.library");
245   LOADLIB(DOSBase, "dos.library");
246   LOADLIB(IntuitionBase, "intuition.library");
247
248   b->MemorySize = CARD_MEMSIZE;
249   b->RegisterBase = (void *)CARD_OFFSET;
250   b->MemoryBase = (void *)(CARD_OFFSET + CARD_REGSIZE);
251
252   return 1;
253 }
254
255 #define HWSPRITE 1
256 #define VGASPLIT (1 << 6)
257 #define FLICKERFIXER (1 << 12)
258 #define INDISPLAYCHAIN (1 << 20)
259 #define DIRECTACCESS (1 << 26)
260
261 int InitCard(__REGA0(struct BoardInfo* b)) {
262   //if (!card_initialized)
263 //    card_initialized = 1;
264 //  else
265     //return 1;
266
267   int max, i;
268   struct ExecBase *SysBase = *(struct ExecBase **)4L;
269
270   b->CardBase = (struct CardBase *)_gfxbase;
271   b->ExecBase = SysBase;
272   b->BoardName = "PiStorm RTG";
273   b->BoardType = BT_MNT_ZZ9000;
274   b->PaletteChipType = PCT_MNT_ZZ9000;
275   b->GraphicsControllerType = GCT_MNT_ZZ9000;
276
277   b->Flags = BIF_INDISPLAYCHAIN | BIF_GRANTDIRECTACCESS;// | BIF_HARDWARESPRITE;
278   b->RGBFormats = 1 | 2 | 512 | 1024 | 2048;
279   b->SoftSpriteFlags = 0;
280   b->BitsPerCannon = 8;
281
282   for(i = 0; i < MAXMODES; i++) {
283     b->MaxHorValue[i] = 8192;
284     b->MaxVerValue[i] = 8192;
285     b->MaxHorResolution[i] = 8192;
286     b->MaxVerResolution[i] = 8192;
287     b->PixelClockCount[i] = 1;
288   }
289
290   b->MemoryClock = CLOCK_HZ;
291
292   //b->AllocCardMem = (void *)NULL;
293   //b->FreeCardMem = (void *)NULL;
294   b->SetSwitch = (void *)SetSwitch;
295   b->SetColorArray = (void *)SetColorArray;
296   b->SetDAC = (void *)SetDAC;
297   b->SetGC = (void *)SetGC;
298   b->SetPanning = (void *)SetPanning;
299   b->CalculateBytesPerRow = (void *)CalculateBytesPerRow;
300   b->CalculateMemory = (void *)CalculateMemory;
301   b->GetCompatibleFormats = (void *)GetCompatibleFormats;
302   b->SetDisplay = (void *)SetDisplay;
303
304   b->ResolvePixelClock = (void *)ResolvePixelClock;
305   b->GetPixelClock = (void *)GetPixelClock;
306   b->SetClock = (void *)SetClock;
307
308   b->SetMemoryMode = (void *)SetMemoryMode;
309   b->SetWriteMask = (void *)SetWriteMask;
310   b->SetClearMask = (void *)SetClearMask;
311   b->SetReadPlane = (void *)SetReadPlane;
312
313   b->WaitVerticalSync = (void *)WaitVerticalSync;
314   //b->SetInterrupt = (void *)NULL;
315
316   //b->WaitBlitter = (void *)NULL;
317
318   //b->ScrollPlanar = (void *)NULL;
319   //b->UpdatePlanar = (void *)NULL;
320
321   b->BlitPlanar2Chunky = (void *)BlitPlanar2Chunky;
322   b->BlitPlanar2Direct = (void *)BlitPlanar2Direct;
323
324   b->FillRect = (void *)FillRect;
325   b->InvertRect = (void *)InvertRect;
326   b->BlitRect = (void *)BlitRect;
327   b->BlitTemplate = (void *)BlitTemplate;
328   b->BlitPattern = (void *)BlitPattern;
329   b->DrawLine = (void *)DrawLine;
330   b->BlitRectNoMaskComplete = (void *)BlitRectNoMaskComplete;
331   //b->EnableSoftSprite = (void *)NULL;
332
333   //b->AllocCardMemAbs = (void *)NULL;
334   //b->SetSplitPosition = (void *)NULL;
335   //b->ReInitMemory = (void *)NULL;
336   //b->WriteYUVRect = (void *)NULL;
337   //b->GetVSyncState = (void *)NULL;
338   //b->GetVBeamPos = (void *)NULL;
339   //b->SetDPMSLevel = (void *)NULL;
340   //b->ResetChip = (void *)NULL;
341   //b->GetFeatureAttrs = (void *)NULL;
342   //b->AllocBitMap = (void *)NULL;
343   //b->FreeBitMap = (void *)NULL;
344   //b->GetBitMapAttr = (void *)NULL;
345
346   //b->SetSprite = (void *)NULL;
347   //b->SetSpritePosition = (void *)NULL;
348   //b->SetSpriteImage = (void *)NULL;
349   //b->SetSpriteColor = (void *)NULL;
350
351   //b->CreateFeature = (void *)NULL;
352   //b->SetFeatureAttrs = (void *)NULL;
353   //b->DeleteFeature = (void *)NULL;
354
355   return 1;
356 }
357
358 void SetDAC (__REGA0(struct BoardInfo *b), __REGD7(RGBFTYPE format)) {
359   // Used to set the color format of the video card's RAMDAC.
360   // This needs no handling, since the PiStorm doesn't really have a RAMDAC or a video card chipset.
361 }
362
363 void SetGC (__REGA0(struct BoardInfo *b), __REGA1(struct ModeInfo *mode_info), __REGD0(BOOL border)) {
364   b->ModeInfo = mode_info;
365   // Send width, height and format to the RaspberryPi Targetable Graphics.
366   WRITESHORT(RTG_X1, mode_info->Width);
367   WRITESHORT(RTG_Y1, mode_info->Height);
368   WRITESHORT(RTG_FORMAT, rgbf_to_rtg[b->RGBFormat]);
369   WRITESHORT(RTG_COMMAND, RTGCMD_SETGC);
370 }
371
372 int setswitch = -1;
373 UWORD SetSwitch (__REGA0(struct BoardInfo *b), __REGD0(UWORD enabled)) {
374   if (setswitch != enabled) {
375     setswitch = enabled;
376   }
377   
378   WRITEBYTE(RTG_U81, setswitch);
379   WRITESHORT(RTG_X1, setswitch);
380   WRITESHORT(RTG_COMMAND, RTGCMD_SETSWITCH);
381
382   return 1 - enabled;
383 }
384
385 void SetPanning (__REGA0(struct BoardInfo *b), __REGA1(UBYTE *addr), __REGD0(UWORD width), __REGD1(WORD x_offset), __REGD2(WORD y_offset), __REGD7(RGBFTYPE format)) {
386   // Set the panning offset, or the offset used for the current display area on the Pi.
387   // The address needs to have CARD_BASE subtracted from it to be used as an offset on the Pi side.
388   if (!b)
389     return;
390
391   b->XOffset = x_offset;
392   b->YOffset = y_offset;
393
394   WRITELONG(RTG_ADDR1, (unsigned long)addr);
395   WRITESHORT(RTG_X1, width);
396   WRITESHORT(RTG_X2, b->XOffset);
397   WRITESHORT(RTG_Y2, b->YOffset);
398   WRITESHORT(RTG_COMMAND, RTGCMD_SETPAN);
399 }
400
401 void SetColorArray (__REGA0(struct BoardInfo *b), __REGD0(UWORD start), __REGD1(UWORD num)) {
402   // Sets the color components of X color components for 8-bit paletted display modes.
403   if (!b->CLUT)
404     return;
405   
406   int j = start + num;
407   
408   for(int i = start; i < j; i++) {
409     //WRITEBYTE(RTG_U82, (unsigned char)b->CLUT[i].Red);
410     //WRITEBYTE(RTG_U83, (unsigned char)b->CLUT[i].Green);
411     //WRITEBYTE(RTG_U84, (unsigned char)b->CLUT[i].Blue);
412     unsigned long xrgb = 0 | (b->CLUT[i].Red << 16) | (b->CLUT[i].Green << 8) | (b->CLUT[i].Blue);
413     WRITEBYTE(RTG_U81, (unsigned char)i);
414     WRITELONG(RTG_RGB1, xrgb);
415     WRITESHORT(RTG_COMMAND, RTGCMD_SETCLUT);
416   }
417 }
418
419 UWORD CalculateBytesPerRow (__REGA0(struct BoardInfo *b), __REGD0(UWORD width), __REGD7(RGBFTYPE format)) {
420   if (!b)
421     return 0;
422
423   UWORD pitch = width;
424
425   switch(format) {
426     default:
427       return pitch;
428     case 0x05: case 0x0A: case 0x0B: case 0x0D:
429       return (width * 2);
430     case 0x08: case 0x09:
431       return (width * 4);
432   }
433 }
434
435 APTR CalculateMemory (__REGA0(struct BoardInfo *b), __REGA1(unsigned long addr), __REGD7(RGBFTYPE format)) {
436   /*if (!b)
437     return (APTR)addr;
438
439   if (addr > (unsigned int)b->MemoryBase && addr < (((unsigned int)b->MemoryBase) + b->MemorySize)) {
440     addr = ((addr + 0x1000) & 0xFFFFF000);
441   }*/
442
443   return (APTR)addr;
444 }
445
446 ULONG GetCompatibleFormats (__REGA0(struct BoardInfo *b), __REGD7(RGBFTYPE format)) {
447   // It is of course compatible with all the formats ever.
448   return 0xFFFFFFFF;
449 }
450
451 static int display_enabled = 0;
452 UWORD SetDisplay (__REGA0(struct BoardInfo *b), __REGD0(UWORD enabled)) {
453   // Enables or disables the display.
454   WRITEBYTE(RTG_U82, (unsigned char)enabled);
455   WRITESHORT(RTG_COMMAND, RTGCMD_SETDISPLAY);
456
457   return 1;
458 }
459
460 LONG ResolvePixelClock (__REGA0(struct BoardInfo *b), __REGA1(struct ModeInfo *mode_info), __REGD0(ULONG pixel_clock), __REGD7(RGBFTYPE format)) {
461   mode_info->PixelClock = CLOCK_HZ;
462   mode_info->pll1.Clock = 0;
463   mode_info->pll2.ClockDivide = 1;
464
465   return 0;
466 }
467
468 ULONG GetPixelClock (__REGA0(struct BoardInfo *b), __REGA1(struct ModeInfo *mode_info), __REGD0(ULONG index), __REGD7(RGBFTYPE format)) {
469   // Just return 100MHz.
470   return CLOCK_HZ;
471 }
472
473 // None of these five really have to do anything.
474 void SetClock (__REGA0(struct BoardInfo *b)) {
475 }
476
477 void SetMemoryMode (__REGA0(struct BoardInfo *b), __REGD7(RGBFTYPE format)) {
478 }
479
480 void SetWriteMask (__REGA0(struct BoardInfo *b), __REGD0(UBYTE mask)) {
481 }
482
483 void SetClearMask (__REGA0(struct BoardInfo *b), __REGD0(UBYTE mask)) {
484 }
485
486 void SetReadPlane (__REGA0(struct BoardInfo *b), __REGD0(UBYTE plane)) {
487 }
488
489 void WaitVerticalSync (__REGA0(struct BoardInfo *b), __REGD0(BOOL toggle)) {
490   // I don't know why this one has a bool in D0, but it isn't used for anything.
491 }
492
493 void FillRect (__REGA0(struct BoardInfo *b), __REGA1(struct RenderInfo *r), __REGD0(WORD x), __REGD1(WORD y), __REGD2(WORD w), __REGD3(WORD h), __REGD4(ULONG color), __REGD5(UBYTE mask), __REGD7(RGBFTYPE format)) {
494   if (!r)
495     return;
496
497   WRITELONG(RTG_ADDR1, (unsigned long)r->Memory);
498   
499   WRITESHORT(RTG_FORMAT, rgbf_to_rtg[format]);
500   WRITESHORT(RTG_X1, x);
501   WRITESHORT(RTG_X2, w);
502   WRITESHORT(RTG_Y1, y);
503   WRITESHORT(RTG_Y2, h);
504   WRITELONG(RTG_RGB1, color);
505   WRITESHORT(RTG_X3, r->BytesPerRow);
506   WRITEBYTE(RTG_U81, mask);
507   WRITESHORT(RTG_COMMAND, RTGCMD_FILLRECT);
508 }
509
510 void InvertRect (__REGA0(struct BoardInfo *b), __REGA1(struct RenderInfo *r), __REGD0(WORD x), __REGD1(WORD y), __REGD2(WORD w), __REGD3(WORD h), __REGD4(UBYTE mask), __REGD7(RGBFTYPE format)) {
511   if (!r)
512     return;
513   
514   if (mask != 0xFF) {
515     b->InvertRectDefault(b, r, x, y, w, h, mask, format);
516     return;
517   }
518
519   WRITELONG(RTG_ADDR1, (unsigned long)r->Memory);
520   
521   WRITESHORT(RTG_FORMAT, rgbf_to_rtg[format]);
522   WRITESHORT(RTG_X1, x);
523   WRITESHORT(RTG_X2, w);
524   WRITESHORT(RTG_Y1, y);
525   WRITESHORT(RTG_Y2, h);
526   WRITESHORT(RTG_X3, r->BytesPerRow);
527   WRITEBYTE(RTG_U81, mask);
528   WRITESHORT(RTG_COMMAND, RTGCMD_INVERTRECT);
529 }
530
531 void BlitRect (__REGA0(struct BoardInfo *b), __REGA1(struct RenderInfo *r), __REGD0(WORD x), __REGD1(WORD y), __REGD2(WORD dx), __REGD3(WORD dy), __REGD4(WORD w), __REGD5(WORD h), __REGD6(UBYTE mask), __REGD7(RGBFTYPE format)) {
532   if (!r)
533     return;
534
535   WRITELONG(RTG_ADDR1, (unsigned long)r->Memory);
536
537   WRITESHORT(RTG_FORMAT, rgbf_to_rtg[format]);
538   WRITESHORT(RTG_X1, x);
539   WRITESHORT(RTG_X2, dx);
540   WRITESHORT(RTG_X3, w);
541   WRITESHORT(RTG_Y1, y);
542   WRITESHORT(RTG_Y2, dy);
543   WRITESHORT(RTG_Y3, h);
544   WRITESHORT(RTG_X4, r->BytesPerRow);
545   WRITEBYTE(RTG_U81, mask);
546   WRITESHORT(RTG_COMMAND, RTGCMD_BLITRECT);
547 }
548
549 void BlitRectNoMaskComplete (__REGA0(struct BoardInfo *b), __REGA1(struct RenderInfo *rs), __REGA2(struct RenderInfo *rt), __REGD0(WORD x), __REGD1(WORD y), __REGD2(WORD dx), __REGD3(WORD dy), __REGD4(WORD w), __REGD5(WORD h), __REGD6(UBYTE minterm), __REGD7(RGBFTYPE format)) {
550   if (!rs || !rt)
551     return;
552
553   WRITESHORT(RTG_FORMAT, rgbf_to_rtg[format]);
554   WRITELONG(RTG_ADDR1, (unsigned long)rs->Memory);
555   WRITELONG(RTG_ADDR2, (unsigned long)rt->Memory);
556   WRITESHORT(RTG_X1, x);
557   WRITESHORT(RTG_X2, dx);
558   WRITESHORT(RTG_X3, w);
559   WRITESHORT(RTG_Y1, y);
560   WRITESHORT(RTG_Y2, dy);
561   WRITESHORT(RTG_Y3, h);
562   WRITESHORT(RTG_X4, rs->BytesPerRow);
563   WRITESHORT(RTG_X5, rt->BytesPerRow);
564   WRITEBYTE(RTG_U81, minterm);
565   WRITESHORT(RTG_COMMAND, RTGCMD_BLITRECT_NOMASK_COMPLETE);
566 }
567
568 void BlitTemplate (__REGA0(struct BoardInfo *b), __REGA1(struct RenderInfo *r), __REGA2(struct Template *t), __REGD0(WORD x), __REGD1(WORD y), __REGD2(WORD w), __REGD3(WORD h), __REGD4(UBYTE mask), __REGD7(RGBFTYPE format)) {
569   if (!r || !t) return;
570   if (w < 1 || h < 1) return;
571
572   WRITELONG(RTG_ADDR2, (unsigned long)r->Memory);
573
574   WRITESHORT(RTG_FORMAT, rgbf_to_rtg[format]);
575   WRITESHORT(RTG_X1, x);
576   WRITESHORT(RTG_X2, w);
577   WRITESHORT(RTG_X3, t->XOffset);
578   WRITESHORT(RTG_Y1, y);
579   WRITESHORT(RTG_Y2, h);
580   WRITESHORT(RTG_Y3, 0);
581
582   if ((unsigned long)t->Memory > CHIP_RAM_SIZE) {
583     WRITELONG(RTG_ADDR1, (unsigned long)t->Memory);
584   }
585   else {
586     unsigned long dest = CARD_SCRATCH;
587     memcpy((unsigned char *)dest, t->Memory, (t->BytesPerRow * h));
588     WRITELONG(RTG_ADDR1, (unsigned long)dest);
589     WRITELONG(RTG_ADDR3, (unsigned long)t->Memory);
590   }
591
592   WRITELONG(RTG_RGB1, t->FgPen);
593   WRITELONG(RTG_RGB2, t->BgPen);
594
595   WRITESHORT(RTG_X4, r->BytesPerRow);
596   WRITESHORT(RTG_X5, t->BytesPerRow);
597
598   WRITEBYTE(RTG_U81, mask);
599   WRITEBYTE(RTG_U82, t->DrawMode);
600   WRITESHORT(RTG_COMMAND, RTGCMD_BLITTEMPLATE);
601 }
602
603 void BlitPattern (__REGA0(struct BoardInfo *b), __REGA1(struct RenderInfo *r), __REGA2(struct Pattern *p), __REGD0(WORD x), __REGD1(WORD y), __REGD2(WORD w), __REGD3(WORD h), __REGD4(UBYTE mask), __REGD7(RGBFTYPE format)) {
604   if (!r || !p) return;
605   if (w < 1 || h < 1) return;
606
607   WRITELONG(RTG_ADDR2, (unsigned long)r->Memory);
608
609   WRITESHORT(RTG_FORMAT, rgbf_to_rtg[format]);
610   WRITESHORT(RTG_X1, x);
611   WRITESHORT(RTG_X2, w);
612   WRITESHORT(RTG_X3, p->XOffset);
613   WRITESHORT(RTG_Y1, y);
614   WRITESHORT(RTG_Y2, h);
615   WRITESHORT(RTG_Y3, p->YOffset);
616
617   if ((unsigned long)p->Memory > CHIP_RAM_SIZE) {
618     WRITELONG(RTG_ADDR1, (unsigned long)p->Memory);
619   }
620   else {
621     unsigned long dest = CARD_SCRATCH;
622     memcpy((unsigned char *)dest, p->Memory, (2 * (1 << p->Size)));
623     WRITELONG(RTG_ADDR1, (unsigned long)dest);
624   }
625
626   WRITELONG(RTG_RGB1, p->FgPen);
627   WRITELONG(RTG_RGB2, p->BgPen);
628
629   WRITESHORT(RTG_X4, r->BytesPerRow);
630   WRITESHORT(RTG_X5, (1 << p->Size));
631
632   WRITEBYTE(RTG_U81, mask);
633   WRITEBYTE(RTG_U82, p->DrawMode);
634   WRITEBYTE(RTG_U83, (1 << p->Size));
635   WRITESHORT(RTG_COMMAND, RTGCMD_BLITPATTERN);
636 }
637
638 void DrawLine (__REGA0(struct BoardInfo *b), __REGA1(struct RenderInfo *r), __REGA2(struct Line *l), __REGD0(UBYTE mask), __REGD7(RGBFTYPE format)) {
639   if (!r || !b) return;
640
641   WRITELONG(RTG_ADDR1, (unsigned long)r->Memory);
642
643   WRITELONG(RTG_RGB1, l->FgPen);
644   WRITELONG(RTG_RGB2, l->BgPen);
645
646   WRITESHORT(RTG_FORMAT, rgbf_to_rtg[format]);
647
648   WRITESHORT(RTG_X1, l->X);
649   WRITESHORT(RTG_X2, l->dX);
650   WRITESHORT(RTG_Y1, l->Y);
651   WRITESHORT(RTG_Y2, l->dY);
652
653   WRITESHORT(RTG_X3, l->Length);
654   WRITESHORT(RTG_Y3, l->LinePtrn);
655
656   WRITESHORT(RTG_X4, r->BytesPerRow);
657   WRITESHORT(RTG_X5, l->PatternShift);
658
659   WRITEBYTE(RTG_U81, mask);
660   WRITEBYTE(RTG_U82, l->DrawMode);
661   WRITEBYTE(RTG_U83, l->pad);
662
663   WRITESHORT(RTG_COMMAND, RTGCMD_DRAWLINE);
664 }
665
666 void BlitPlanar2Chunky (__REGA0(struct BoardInfo *b), __REGA1(struct BitMap *bm), __REGA2(struct RenderInfo *r), __REGD0(SHORT x), __REGD1(SHORT y), __REGD2(SHORT dx), __REGD3(SHORT dy), __REGD4(SHORT w), __REGD5(SHORT h), __REGD6(UBYTE minterm), __REGD7(UBYTE mask)) {
667   if (!b || !r)
668     return;
669
670   uint32_t plane_size = bm->BytesPerRow * bm->Rows;
671
672   uint32_t template_addr = CARD_SCRATCH;
673
674   uint16_t plane_mask = mask;
675   uint8_t ff_mask = 0x00;
676   uint8_t cur_plane = 0x01;
677
678   uint16_t line_size = (w >> 3) + 2;
679   uint32_t output_plane_size = line_size * h;
680   uint16_t x_offset = (x >> 3);
681
682   WRITELONG(RTG_ADDR1, (unsigned long)r->Memory);
683   WRITELONG(RTG_ADDR2, template_addr);
684   WRITESHORT(RTG_X4, r->BytesPerRow);
685   WRITESHORT(RTG_X5, line_size);
686   WRITESHORT(RTG_FORMAT, rgbf_to_rtg[r->RGBFormat]);
687
688   WRITEBYTE(RTG_U81, mask);
689   WRITEBYTE(RTG_U82, minterm);
690
691   for (int16_t i = 0; i < bm->Depth; i++) {
692     uint16_t x_offset = (x >> 3);
693     if ((uint32_t)bm->Planes[i] == 0xFFFFFFFF) {
694       uint8_t* dest = (uint8_t*)((uint32_t)template_addr);
695       memset(dest, 0xFF, output_plane_size);
696     }
697     else if (bm->Planes[i] != NULL) {
698       uint8_t* bmp_mem = (uint8_t*)bm->Planes[i] + (y * bm->BytesPerRow) + x_offset;
699       uint8_t* dest = (uint8_t*)((uint32_t)template_addr);
700       for (int16_t y_line = 0; y_line < h; y_line++) {
701         memcpy(dest, bmp_mem, line_size);
702         dest += line_size;
703         bmp_mem += bm->BytesPerRow;
704       }
705     }
706     else {
707       plane_mask &= (cur_plane ^ 0xFF);
708     }
709     cur_plane <<= 1;
710     template_addr += output_plane_size;
711   }
712
713   WRITESHORT(RTG_X1, (x & 0x07));
714   WRITESHORT(RTG_X2, dx);
715   WRITESHORT(RTG_X3, w);
716   WRITESHORT(RTG_Y1, 0);
717   WRITESHORT(RTG_Y2, dy);
718   WRITESHORT(RTG_Y3, h);
719
720   WRITESHORT(RTG_U1, (plane_mask << 8 | ff_mask));
721   WRITEBYTE(RTG_U83, bm->Depth);
722
723   WRITESHORT(RTG_COMMAND, RTGCMD_P2C);
724 }
725
726 void BlitPlanar2Direct (__REGA0(struct BoardInfo *b), __REGA1(struct BitMap *bm), __REGA2(struct RenderInfo *r), __REGA3(struct ColorIndexMapping *clut), __REGD0(SHORT x), __REGD1(SHORT y), __REGD2(SHORT dx), __REGD3(SHORT dy), __REGD4(SHORT w), __REGD5(SHORT h), __REGD6(UBYTE minterm), __REGD7(UBYTE mask)) {
727   if (!b || !r)
728     return;
729
730   uint32_t plane_size = bm->BytesPerRow * bm->Rows;
731
732   uint32_t template_addr = CARD_SCRATCH;
733
734   uint16_t plane_mask = mask;
735   uint8_t ff_mask = 0x00;
736   uint8_t cur_plane = 0x01;
737
738   uint16_t line_size = (w >> 3) + 2;
739   uint32_t output_plane_size = line_size * h;
740   uint16_t x_offset = (x >> 3);
741
742   WRITELONG(RTG_ADDR1, (unsigned long)r->Memory);
743   WRITELONG(RTG_ADDR2, template_addr);
744   WRITESHORT(RTG_X4, r->BytesPerRow);
745   WRITESHORT(RTG_X5, line_size);
746   WRITESHORT(RTG_FORMAT, rgbf_to_rtg[r->RGBFormat]);
747
748   WRITEBYTE(RTG_U81, mask);
749   WRITEBYTE(RTG_U82, minterm);
750
751   memcpy((uint8_t*)((uint32_t)template_addr), clut->Colors, (256 << 2));
752   template_addr += (256 << 2);
753
754   for (int16_t i = 0; i < bm->Depth; i++) {
755     uint16_t x_offset = (x >> 3);
756     if ((uint32_t)bm->Planes[i] == 0xFFFFFFFF) {
757       uint8_t* dest = (uint8_t*)((uint32_t)template_addr);
758       memset(dest, 0xFF, output_plane_size);
759     }
760     else if (bm->Planes[i] != NULL) {
761       uint8_t* bmp_mem = (uint8_t*)bm->Planes[i] + (y * bm->BytesPerRow) + x_offset;
762       uint8_t* dest = (uint8_t*)((uint32_t)template_addr);
763       for (int16_t y_line = 0; y_line < h; y_line++) {
764         memcpy(dest, bmp_mem, line_size);
765         dest += line_size;
766         bmp_mem += bm->BytesPerRow;
767       }
768     }
769     else {
770       plane_mask &= (cur_plane ^ 0xFF);
771     }
772     cur_plane <<= 1;
773     template_addr += output_plane_size;
774   }
775
776   WRITESHORT(RTG_X1, (x & 0x07));
777   WRITESHORT(RTG_X2, dx);
778   WRITESHORT(RTG_X3, w);
779   WRITESHORT(RTG_Y1, 0);
780   WRITESHORT(RTG_Y2, dy);
781   WRITESHORT(RTG_Y3, h);
782
783   WRITESHORT(RTG_U1, (plane_mask << 8 | ff_mask));
784   WRITEBYTE(RTG_U83, bm->Depth);
785
786   WRITESHORT(RTG_COMMAND, RTGCMD_P2D);
787 }