]> git.sesse.net Git - pistorm/blob - platforms/amiga/rtg/rtg_driver_amiga/pigfx.c
Fix all the colors, fix some other stuff.
[pistorm] / platforms / amiga / rtg / rtg_driver_amiga / pigfx.c
1 // PiStorm RTG driver, VBCC edition.
2 // Based in part on the ZZ9000 RTG driver.
3
4 #include <proto/exec.h>
5 #include <proto/expansion.h>
6 #include <proto/dos.h>
7 #include <proto/intuition.h>
8 #include <exec/types.h>
9 #include <exec/memory.h>
10 #include <exec/libraries.h>
11 #include <exec/execbase.h>
12 #include <exec/resident.h>
13 #include <exec/initializers.h>
14 #include <clib/debug_protos.h>
15 #include <string.h>
16 #include <stdio.h>
17 #include "boardinfo.h"
18
19 #define WRITESHORT(cmd, val) *(unsigned short *)((unsigned int)(b->RegisterBase)+cmd) = val;
20 #define WRITELONG(cmd, val) *(unsigned int *)((unsigned int)(b->RegisterBase)+cmd) = val;
21 #define WRITEBYTE(cmd, val) *(unsigned char *)((unsigned int)(b->RegisterBase)+cmd) = val;
22
23 #define CARD_OFFSET  0x70000000
24 #define CARD_REGSIZE 0x00010000
25 // 32MB "VRAM"
26 #define CARD_MEMSIZE 0x02000000
27
28 // "Register" offsets for sending data to the RTG.
29 enum pi_regs {
30   RTG_COMMAND = 0x00,
31   RTG_X1      = 0x02,
32   RTG_X2      = 0x04,
33   RTG_X3      = 0x06,
34   RTG_Y1      = 0x08,
35   RTG_Y2      = 0x0A,
36   RTG_Y3      = 0x0C,
37   RTG_FORMAT  = 0x0E,
38   RTG_RGB1    = 0x10,
39   RTG_RGB2    = 0x14,
40   RTG_ADDR1   = 0x18,
41   RTG_ADDR2   = 0x1C,
42   RTG_U81     = 0x20,
43   RTG_U82     = 0x21,
44   RTG_U83     = 0x22,
45   RTG_U84     = 0x23,
46 };
47
48 enum rtg_cmds {
49   RTGCMD_SETGC,
50   RTGCMD_SETPAN,
51   RTGCMD_SETCLUT,
52   RTGCMD_ENABLE,
53   RTGCMD_SETDISPLAY,
54   RTGCMD_SETSWITCH,
55   RTGCMD_FILLRECT,
56 };
57
58 enum rtg_formats {
59   RTGFMT_8BIT,
60   RTGFMT_RBG565,
61   RTGFMT_RGB32,
62   RTGFMT_RGB555,
63 };
64
65 const unsigned short rgbf_to_rtg[16] = {
66   RTGFMT_8BIT,      // 0x00
67   RTGFMT_8BIT,      // 0x01
68   0,                // 0x02
69   0,                // 0x03
70   0,                // 0x04
71   RTGFMT_RGB555,    // 0x05
72   0,                // 0x06
73   0,                // 0x07
74   RTGFMT_RGB32,     // 0x08
75   RTGFMT_RGB32,     // 0x09
76   RTGFMT_RBG565,    // 0x0A
77   RTGFMT_RGB555,    // 0x0B
78   0,                // 0x0C
79   RTGFMT_RGB555,    // 0x0D
80   0,                // 0x0E
81   0,                // 0x0F
82 };
83
84 struct GFXBase {
85   struct Library libNode;
86   BPTR segList;
87   struct ExecBase* sysBase;
88   struct ExpansionBase* expansionBase;
89 };
90
91 int FindCard(__REGA0(struct BoardInfo* b));
92 int InitCard(__REGA0(struct BoardInfo* b));
93
94 void SetDAC (__REGA0(struct BoardInfo *b), __REGD7(RGBFTYPE format));
95 void SetGC (__REGA0(struct BoardInfo *b), __REGA1(struct ModeInfo *mode_info), __REGD0(BOOL border));
96 void SetColorArray (__REGA0(struct BoardInfo *b), __REGD0(UWORD start), __REGD1(UWORD num));
97 void SetPanning (__REGA0(struct BoardInfo *b), __REGA1(UBYTE *addr), __REGD0(UWORD width), __REGD1(WORD x_offset), __REGD2(WORD y_offset), __REGD7(RGBFTYPE format));
98 UWORD SetSwitch (__REGA0(struct BoardInfo *b), __REGD0(UWORD enabled));
99 UWORD SetDisplay (__REGA0(struct BoardInfo *b), __REGD0(UWORD enabled));
100
101 UWORD CalculateBytesPerRow (__REGA0(struct BoardInfo *b), __REGD0(UWORD width), __REGD7(RGBFTYPE format));
102 APTR CalculateMemory (__REGA0(struct BoardInfo *b), __REGA1(unsigned int addr), __REGD7(RGBFTYPE format));
103 ULONG GetCompatibleFormats (__REGA0(struct BoardInfo *b), __REGD7(RGBFTYPE format));
104
105 LONG ResolvePixelClock (__REGA0(struct BoardInfo *b), __REGA1(struct ModeInfo *mode_info), __REGD0(ULONG pixel_clock), __REGD7(RGBFTYPE format));
106 ULONG GetPixelClock (__REGA0(struct BoardInfo *b), __REGA1(struct ModeInfo *mode_info), __REGD0(ULONG index), __REGD7(RGBFTYPE format));
107 void SetClock (__REGA0(struct BoardInfo *b));
108
109 void SetMemoryMode (__REGA0(struct BoardInfo *b), __REGD7(RGBFTYPE format));
110 void SetWriteMask (__REGA0(struct BoardInfo *b), __REGD0(UBYTE mask));
111 void SetClearMask (__REGA0(struct BoardInfo *b), __REGD0(UBYTE mask));
112 void SetReadPlane (__REGA0(struct BoardInfo *b), __REGD0(UBYTE plane));
113
114 void WaitVerticalSync (__REGA0(struct BoardInfo *b), __REGD0(BOOL toggle));
115
116 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));
117
118 static ULONG LibStart(void) {
119   return(-1);
120 }
121
122 static const char LibraryName[] = "PiRTG.card";
123 static const char LibraryID[]   = "$VER: PiRTG.card 0.01\r\n";
124
125 __saveds struct GFXBase* OpenLib(__REGA6(struct GFXBase *gfxbase));
126 BPTR __saveds CloseLib(__REGA6(struct GFXBase *gfxbase));
127 BPTR __saveds ExpungeLib(__REGA6(struct GFXBase *exb));
128 ULONG ExtFuncLib(void);
129 __saveds struct GFXBase* InitLib(__REGA6(struct ExecBase *sysbase),
130                                  __REGA0(BPTR seglist),
131                                  __REGD0(struct GFXBase *exb));
132
133 static const APTR FuncTab[] = {
134   (APTR)OpenLib,
135   (APTR)CloseLib,
136   (APTR)ExpungeLib,
137   (APTR)ExtFuncLib,
138
139   (APTR)FindCard,
140   (APTR)InitCard,
141   (APTR)((LONG)-1)
142 };
143
144 struct InitTable
145 {
146   ULONG LibBaseSize;
147   APTR  FunctionTable;
148   APTR  DataTable;
149   APTR  InitLibTable;
150 };
151
152 static struct InitTable InitTab = {
153   (ULONG) sizeof(struct GFXBase),
154   (APTR) FuncTab,
155   (APTR) NULL,
156   (APTR) InitLib
157 };
158
159 static const struct Resident ROMTag = {
160         RTC_MATCHWORD,
161   &ROMTag,
162   &ROMTag + 1,
163   RTF_AUTOINIT,
164         83,
165   NT_LIBRARY,
166   0,
167   (char *)LibraryName,
168   (char *)LibraryID,
169   (APTR)&InitTab
170 };
171
172 #define CLOCK_HZ 100000000
173
174 static struct GFXBase *_gfxbase;
175 const char *gfxname = "PiStorm RTG";
176 char dummies[128];
177
178 __saveds struct GFXBase* InitLib(__REGA6(struct ExecBase *sysbase),
179                                           __REGA0(BPTR seglist),
180                                           __REGD0(struct GFXBase *exb))
181 {
182   _gfxbase = exb;
183   return _gfxbase;
184 }
185
186 __saveds struct GFXBase* OpenLib(__REGA6(struct GFXBase *gfxbase))
187 {
188   gfxbase->libNode.lib_OpenCnt++;
189   gfxbase->libNode.lib_Flags &= ~LIBF_DELEXP;
190
191   return gfxbase;
192 }
193
194 BPTR __saveds CloseLib(__REGA6(struct GFXBase *gfxbase))
195 {
196   gfxbase->libNode.lib_OpenCnt--;
197
198   if (!gfxbase->libNode.lib_OpenCnt) {
199     if (gfxbase->libNode.lib_Flags & LIBF_DELEXP) {
200       return (ExpungeLib(gfxbase));
201     }
202   }
203   return 0;
204 }
205
206 BPTR __saveds ExpungeLib(__REGA6(struct GFXBase *exb))
207 {
208   BPTR seglist;
209   struct ExecBase *SysBase = *(struct ExecBase **)4L;
210
211   if(!exb->libNode.lib_OpenCnt) {
212     ULONG negsize, possize, fullsize;
213     UBYTE *negptr = (UBYTE *)exb;
214
215     seglist = exb->segList;
216
217     Remove((struct Node *)exb);
218
219     negsize  = exb->libNode.lib_NegSize;
220     possize  = exb->libNode.lib_PosSize;
221     fullsize = negsize + possize;
222     negptr  -= negsize;
223
224     FreeMem(negptr, fullsize);
225     return(seglist);
226   }
227
228   exb->libNode.lib_Flags |= LIBF_DELEXP;
229   return 0;
230 }
231
232 ULONG ExtFuncLib(void)
233 {
234   return 0;
235 }
236
237 static LONG zorro_version = 0;
238
239 static struct GFXData *gfxdata;
240 //MNTZZ9KRegs* registers;
241
242 #define LOADLIB(a, b) if ((a = (struct a*)OpenLibrary(b,0L))==NULL) { \
243     KPrintF("Failed to load %s.\n", b); \
244     return 0; \
245   } \
246
247 static BYTE card_already_found;
248 static BYTE card_initialized;
249
250 int FindCard(__REGA0(struct BoardInfo* b)) {
251   //if (card_already_found)
252 //    return 1;
253   struct ConfigDev* cd = NULL;
254   struct ExpansionBase *ExpansionBase = NULL;
255   struct DOSBase *DOSBase = NULL;
256   struct IntuitionBase *IntuitionBase = NULL;
257   struct ExecBase *SysBase = *(struct ExecBase **)4L;
258
259   LOADLIB(ExpansionBase, "expansion.library");
260   LOADLIB(DOSBase, "dos.library");
261   LOADLIB(IntuitionBase, "intuition.library");
262
263   b->MemorySize = CARD_MEMSIZE;
264   b->RegisterBase = (void *)CARD_OFFSET;
265   b->MemoryBase = (void *)(CARD_OFFSET + CARD_REGSIZE);
266
267   return 1;
268 }
269
270 #define HWSPRITE 1
271 #define VGASPLIT (1 << 6)
272 #define FLICKERFIXER (1 << 12)
273 #define INDISPLAYCHAIN (1 << 20)
274 #define DIRECTACCESS (1 << 26)
275
276 int InitCard(__REGA0(struct BoardInfo* b)) {
277   //if (!card_initialized)
278 //    card_initialized = 1;
279 //  else
280     //return 1;
281
282   int max, i;
283   struct ExecBase *SysBase = *(struct ExecBase **)4L;
284
285   b->CardBase = (struct CardBase *)_gfxbase;
286   b->ExecBase = SysBase;
287   b->BoardName = "PiStorm RTG";
288   b->BoardType = BT_MNT_ZZ9000;
289   b->PaletteChipType = PCT_MNT_ZZ9000;
290   b->GraphicsControllerType = GCT_MNT_ZZ9000;
291
292   b->Flags = BIF_INDISPLAYCHAIN | BIF_GRANTDIRECTACCESS;
293   b->RGBFormats = 1 | 2 | 512 | 1024 | 2048;
294   b->SoftSpriteFlags = 0;
295   b->BitsPerCannon = 8;
296
297   for(i = 0; i < MAXMODES; i++) {
298     b->MaxHorValue[i] = 1920;
299     b->MaxVerValue[i] = 1080;
300     b->MaxHorResolution[i] = 1920;
301     b->MaxVerResolution[i] = 1080;
302     b->PixelClockCount[i] = 1;
303   }
304
305   b->MemoryClock = CLOCK_HZ;
306
307   //b->AllocCardMem = (void *)NULL;
308   //b->FreeCardMem = (void *)NULL;
309   b->SetSwitch = (void *)SetSwitch;
310   b->SetColorArray = (void *)SetColorArray;
311   b->SetDAC = (void *)SetDAC;
312   b->SetGC = (void *)SetGC;
313   b->SetPanning = (void *)SetPanning;
314   b->CalculateBytesPerRow = (void *)CalculateBytesPerRow;
315   b->CalculateMemory = (void *)CalculateMemory;
316   b->GetCompatibleFormats = (void *)GetCompatibleFormats;
317   b->SetDisplay = (void *)SetDisplay;
318
319   b->ResolvePixelClock = (void *)ResolvePixelClock;
320   b->GetPixelClock = (void *)GetPixelClock;
321   b->SetClock = (void *)SetClock;
322
323   b->SetMemoryMode = (void *)SetMemoryMode;
324   b->SetWriteMask = (void *)SetWriteMask;
325   b->SetClearMask = (void *)SetClearMask;
326   b->SetReadPlane = (void *)SetReadPlane;
327
328   b->WaitVerticalSync = (void *)WaitVerticalSync;
329   //b->SetInterrupt = (void *)NULL;
330
331   //b->WaitBlitter = (void *)NULL;
332
333   //b->ScrollPlanar = (void *)NULL;
334   //b->UpdatePlanar = (void *)NULL;
335
336   //b->BlitPlanar2Chunky = (void *)NULL;
337   //b->BlitPlanar2Direct = (void *)NULL;
338
339   b->FillRect = (void *)FillRect;
340   //b->InvertRect = (void *)NULL;
341   //b->BlitRect = (void *)NULL;
342   //b->BlitTemplate = (void *)NULL;
343   //b->BlitPattern = (void *)NULL;
344   //b->DrawLine = (void *)NULL;
345   //b->BlitRectNoMaskComplete = (void *)NULL;
346   //b->EnableSoftSprite = (void *)NULL;
347
348   //b->AllocCardMemAbs = (void *)NULL;
349   //b->SetSplitPosition = (void *)NULL;
350   //b->ReInitMemory = (void *)NULL;
351   //b->WriteYUVRect = (void *)NULL;
352   //b->GetVSyncState = (void *)NULL;
353   //b->GetVBeamPos = (void *)NULL;
354   //b->SetDPMSLevel = (void *)NULL;
355   //b->ResetChip = (void *)NULL;
356   //b->GetFeatureAttrs = (void *)NULL;
357   //b->AllocBitMap = (void *)NULL;
358   //b->FreeBitMap = (void *)NULL;
359   //b->GetBitMapAttr = (void *)NULL;
360
361   //b->SetSprite = (void *)NULL;
362   //b->SetSpritePosition = (void *)NULL;
363   //b->SetSpriteImage = (void *)NULL;
364   //b->SetSpriteColor = (void *)NULL;
365
366   //b->CreateFeature = (void *)NULL;
367   //b->SetFeatureAttrs = (void *)NULL;
368   //b->DeleteFeature = (void *)NULL;
369
370   return 1;
371 }
372
373 void SetDAC (__REGA0(struct BoardInfo *b), __REGD7(RGBFTYPE format)) {
374   // Used to set the color format of the video card's RAMDAC.
375   // This needs no handling, since the PiStorm doesn't really have a RAMDAC or a video card chipset.
376 }
377
378 void SetGC (__REGA0(struct BoardInfo *b), __REGA1(struct ModeInfo *mode_info), __REGD0(BOOL border)) {
379   b->ModeInfo = mode_info;
380   // Send width, height and format to the RaspberryPi Targetable Graphics.
381   WRITESHORT(RTG_X1, mode_info->Width);
382   WRITESHORT(RTG_Y1, mode_info->Height);
383   WRITESHORT(RTG_FORMAT, rgbf_to_rtg[b->RGBFormat]);
384   WRITESHORT(RTG_COMMAND, RTGCMD_SETGC);
385 }
386
387 int setswitch = -1;
388 UWORD SetSwitch (__REGA0(struct BoardInfo *b), __REGD0(UWORD enabled)) {
389   if (setswitch != enabled) {
390     setswitch = enabled;
391   }
392   
393   WRITEBYTE(RTG_U81, setswitch);
394   WRITESHORT(RTG_X1, setswitch);
395   WRITESHORT(RTG_COMMAND, RTGCMD_SETSWITCH);
396
397   return 1 - enabled;
398 }
399
400 void SetPanning (__REGA0(struct BoardInfo *b), __REGA1(UBYTE *addr), __REGD0(UWORD width), __REGD1(WORD x_offset), __REGD2(WORD y_offset), __REGD7(RGBFTYPE format)) {
401   // Set the panning offset, or the offset used for the current display area on the Pi.
402   // The address needs to have CARD_BASE subtracted from it to be used as an offset on the Pi side.
403   if (!b)
404     return;
405
406   b->XOffset = x_offset;
407   b->YOffset = y_offset;
408
409   WRITELONG(RTG_ADDR1, (unsigned int)addr);
410   WRITESHORT(RTG_X1, width);
411   WRITESHORT(RTG_X2, b->XOffset);
412   WRITESHORT(RTG_Y2, b->YOffset);
413   WRITESHORT(RTG_COMMAND, RTGCMD_SETPAN);
414 }
415
416 void SetColorArray (__REGA0(struct BoardInfo *b), __REGD0(UWORD start), __REGD1(UWORD num)) {
417   // Sets the color components of X color components for 8-bit paletted display modes.
418   if (!b->CLUT)
419     return;
420   
421   int j = start + num;
422   
423   for(int i = start; i < j; i++) {
424     //WRITEBYTE(RTG_U82, (unsigned char)b->CLUT[i].Red);
425     //WRITEBYTE(RTG_U83, (unsigned char)b->CLUT[i].Green);
426     //WRITEBYTE(RTG_U84, (unsigned char)b->CLUT[i].Blue);
427     unsigned int xrgb = 0 | (b->CLUT[i].Red << 16) | (b->CLUT[i].Green << 8) | (b->CLUT[i].Blue);
428     WRITEBYTE(RTG_U81, (unsigned char)i);
429     WRITELONG(RTG_RGB1, xrgb);
430     WRITESHORT(RTG_COMMAND, RTGCMD_SETCLUT);
431   }
432 }
433
434 UWORD CalculateBytesPerRow (__REGA0(struct BoardInfo *b), __REGD0(UWORD width), __REGD7(RGBFTYPE format)) {
435   if (!b)
436     return 0;
437
438   UWORD pitch = width;
439
440   switch(format) {
441     default:
442       return pitch;
443     case 0x05: case 0x0A: case 0x0B: case 0x0D:
444       return (width * 2);
445     case 0x08: case 0x09:
446       return (width * 4);
447   }
448 }
449
450 APTR CalculateMemory (__REGA0(struct BoardInfo *b), __REGA1(unsigned int addr), __REGD7(RGBFTYPE format)) {
451   /*if (!b)
452     return (APTR)addr;
453
454   if (addr > (unsigned int)b->MemoryBase && addr < (((unsigned int)b->MemoryBase) + b->MemorySize)) {
455     addr = ((addr + 0x1000) & 0xFFFFF000);
456   }*/
457
458   return (APTR)addr;
459 }
460
461 ULONG GetCompatibleFormats (__REGA0(struct BoardInfo *b), __REGD7(RGBFTYPE format)) {
462   // It is of course compatible with all the formats ever.
463   return 0xFFFFFFFF;
464 }
465
466 static int display_enabled = 0;
467 UWORD SetDisplay (__REGA0(struct BoardInfo *b), __REGD0(UWORD enabled)) {
468   // Enables or disables the display.
469   WRITEBYTE(RTG_U82, (unsigned char)enabled);
470   WRITESHORT(RTG_COMMAND, RTGCMD_SETDISPLAY);
471
472   return 1;
473 }
474
475 LONG ResolvePixelClock (__REGA0(struct BoardInfo *b), __REGA1(struct ModeInfo *mode_info), __REGD0(ULONG pixel_clock), __REGD7(RGBFTYPE format)) {
476   mode_info->PixelClock = CLOCK_HZ;
477   mode_info->pll1.Clock = 0;
478   mode_info->pll2.ClockDivide = 1;
479
480   return 0;
481 }
482
483 ULONG GetPixelClock (__REGA0(struct BoardInfo *b), __REGA1(struct ModeInfo *mode_info), __REGD0(ULONG index), __REGD7(RGBFTYPE format)) {
484   // Just return 100MHz.
485   return CLOCK_HZ;
486 }
487
488 // None of these five really have to do anything.
489 void SetClock (__REGA0(struct BoardInfo *b)) {
490 }
491
492 void SetMemoryMode (__REGA0(struct BoardInfo *b), __REGD7(RGBFTYPE format)) {
493 }
494
495 void SetWriteMask (__REGA0(struct BoardInfo *b), __REGD0(UBYTE mask)) {
496 }
497
498 void SetClearMask (__REGA0(struct BoardInfo *b), __REGD0(UBYTE mask)) {
499 }
500
501 void SetReadPlane (__REGA0(struct BoardInfo *b), __REGD0(UBYTE plane)) {
502 }
503
504 void WaitVerticalSync (__REGA0(struct BoardInfo *b), __REGD0(BOOL toggle)) {
505   // I don't know why this one has a bool in D0, but it isn't used for anything.
506 }
507
508 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)) {
509   if (!r)
510     return;
511   if (mask != 0xFF)
512     b->FillRectDefault(b, r, x, y, w, h, color, mask, format);
513   
514   WRITESHORT(RTG_FORMAT, rgbf_to_rtg[format]);
515   WRITESHORT(RTG_X1, x);
516   WRITESHORT(RTG_X2, w);
517   WRITESHORT(RTG_Y1, y);
518   WRITESHORT(RTG_Y2, h);
519   WRITELONG(RTG_RGB1, color);
520   WRITESHORT(RTG_X3, r->BytesPerRow);
521   WRITESHORT(RTG_COMMAND, RTGCMD_FILLRECT);
522 }