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