1 // PiStorm RTG driver, VBCC edition.
2 // Based in part on the ZZ9000 RTG driver.
4 #include <proto/exec.h>
5 #include <proto/expansion.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>
17 #include "boardinfo.h"
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;
23 #define CARD_OFFSET 0x70000000
24 #define CARD_REGSIZE 0x00010000
26 #define CARD_MEMSIZE 0x02000000
28 // "Register" offsets for sending data to the RTG.
64 const unsigned short rgbf_to_rtg[16] = {
70 RTGFMT_RGB555, // 0x05
75 RTGFMT_RBG565, // 0x0A
76 RTGFMT_RGB555, // 0x0B
78 RTGFMT_RGB555, // 0x0D
84 struct Library libNode;
86 struct ExecBase* sysBase;
87 struct ExpansionBase* expansionBase;
90 int FindCard(__REGA0(struct BoardInfo* b));
91 int InitCard(__REGA0(struct BoardInfo* b));
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));
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));
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));
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));
113 void WaitVerticalSync (__REGA0(struct BoardInfo *b), __REGD0(BOOL toggle));
115 static ULONG LibStart(void) {
119 static const char LibraryName[] = "PiRTG.card";
120 static const char LibraryID[] = "$VER: PiRTG.card 0.01\r\n";
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));
130 static const APTR FuncTab[] = {
149 static struct InitTable InitTab = {
150 (ULONG) sizeof(struct GFXBase),
156 static const struct Resident ROMTag = {
169 #define CLOCK_HZ 100000000
171 static struct GFXBase *_gfxbase;
172 const char *gfxname = "PiStorm RTG";
175 __saveds struct GFXBase* InitLib(__REGA6(struct ExecBase *sysbase),
176 __REGA0(BPTR seglist),
177 __REGD0(struct GFXBase *exb))
183 __saveds struct GFXBase* OpenLib(__REGA6(struct GFXBase *gfxbase))
185 gfxbase->libNode.lib_OpenCnt++;
186 gfxbase->libNode.lib_Flags &= ~LIBF_DELEXP;
191 BPTR __saveds CloseLib(__REGA6(struct GFXBase *gfxbase))
193 gfxbase->libNode.lib_OpenCnt--;
195 if (!gfxbase->libNode.lib_OpenCnt) {
196 if (gfxbase->libNode.lib_Flags & LIBF_DELEXP) {
197 return (ExpungeLib(gfxbase));
203 BPTR __saveds ExpungeLib(__REGA6(struct GFXBase *exb))
206 struct ExecBase *SysBase = *(struct ExecBase **)4L;
208 if(!exb->libNode.lib_OpenCnt) {
209 ULONG negsize, possize, fullsize;
210 UBYTE *negptr = (UBYTE *)exb;
212 seglist = exb->segList;
214 Remove((struct Node *)exb);
216 negsize = exb->libNode.lib_NegSize;
217 possize = exb->libNode.lib_PosSize;
218 fullsize = negsize + possize;
221 FreeMem(negptr, fullsize);
225 exb->libNode.lib_Flags |= LIBF_DELEXP;
229 ULONG ExtFuncLib(void)
234 static LONG zorro_version = 0;
236 static struct GFXData *gfxdata;
237 //MNTZZ9KRegs* registers;
239 #define LOADLIB(a, b) if ((a = (struct a*)OpenLibrary(b,0L))==NULL) { \
240 KPrintF("Failed to load %s.\n", b); \
244 static BYTE card_already_found;
245 static BYTE card_initialized;
247 int FindCard(__REGA0(struct BoardInfo* b)) {
248 //if (card_already_found)
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;
256 LOADLIB(ExpansionBase, "expansion.library");
257 LOADLIB(DOSBase, "dos.library");
258 LOADLIB(IntuitionBase, "intuition.library");
260 b->MemorySize = CARD_MEMSIZE;
261 b->RegisterBase = (void *)CARD_OFFSET;
262 b->MemoryBase = (void *)(CARD_OFFSET + CARD_REGSIZE);
268 #define VGASPLIT (1 << 6)
269 #define FLICKERFIXER (1 << 12)
270 #define INDISPLAYCHAIN (1 << 20)
271 #define DIRECTACCESS (1 << 26)
273 int InitCard(__REGA0(struct BoardInfo* b)) {
274 //if (!card_initialized)
275 // card_initialized = 1;
280 struct ExecBase *SysBase = *(struct ExecBase **)4L;
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;
289 b->Flags = BIF_INDISPLAYCHAIN | BIF_GRANTDIRECTACCESS;
290 b->RGBFormats = 1 | 2 | 512 | 1024 | 2048;
291 b->SoftSpriteFlags = 0;
292 b->BitsPerCannon = 8;
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;
302 b->MemoryClock = CLOCK_HZ;
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;
316 b->ResolvePixelClock = (void *)ResolvePixelClock;
317 b->GetPixelClock = (void *)GetPixelClock;
318 b->SetClock = (void *)SetClock;
320 b->SetMemoryMode = (void *)SetMemoryMode;
321 b->SetWriteMask = (void *)SetWriteMask;
322 b->SetClearMask = (void *)SetClearMask;
323 b->SetReadPlane = (void *)SetReadPlane;
325 b->WaitVerticalSync = (void *)WaitVerticalSync;
326 //b->SetInterrupt = (void *)NULL;
328 //b->WaitBlitter = (void *)NULL;
330 //b->ScrollPlanar = (void *)NULL;
331 //b->UpdatePlanar = (void *)NULL;
333 //b->BlitPlanar2Chunky = (void *)NULL;
334 //b->BlitPlanar2Direct = (void *)NULL;
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;
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;
358 //b->SetSprite = (void *)NULL;
359 //b->SetSpritePosition = (void *)NULL;
360 //b->SetSpriteImage = (void *)NULL;
361 //b->SetSpriteColor = (void *)NULL;
363 //b->CreateFeature = (void *)NULL;
364 //b->SetFeatureAttrs = (void *)NULL;
365 //b->DeleteFeature = (void *)NULL;
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.
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);
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;
395 old_setswitch = setswitch;
398 if (old_setswitch != enabled) {
399 //WRITEBYTE(RTG_U81, (unsigned char)enabled);
400 //WRITESHORT(RTG_COMMAND, RTGCMD_SETSWITCH);
403 return old_setswitch;
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.
413 b->XOffset = x_offset;
414 b->YOffset = y_offset;
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);
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.
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);
436 UWORD CalculateBytesPerRow (__REGA0(struct BoardInfo *b), __REGD0(UWORD width), __REGD7(RGBFTYPE format)) {
437 WRITESHORT(RTGCMD_SETPAN, 0x0006);
446 case 0x05: case 0x0A: case 0x0B: case 0x0D:
448 case 0x08: case 0x09:
453 APTR CalculateMemory (__REGA0(struct BoardInfo *b), __REGA1(unsigned int addr), __REGD7(RGBFTYPE format)) {
454 WRITESHORT(RTGCMD_SETPAN, 0x0007);
458 if (addr > (unsigned int)b->MemoryBase && addr < (((unsigned int)b->MemoryBase) + b->MemorySize)) {
459 addr = ((addr + 0x1000) & 0xFFFFF000);
465 ULONG GetCompatibleFormats (__REGA0(struct BoardInfo *b), __REGD7(RGBFTYPE format)) {
466 // It is of course compatible with all the formats ever.
470 static int display_enabled = 0;
471 BOOL SetDisplay (__REGA0(struct BoardInfo *b), __REGD0(BOOL enabled)) {
472 WRITESHORT(RTGCMD_SETPAN, 0x0009);
476 // Enables or disables the display.
477 WRITEBYTE(RTG_U82, (unsigned char)enabled);
478 WRITESHORT(RTG_COMMAND, RTGCMD_SETDISPLAY);
480 if (display_enabled != enabled) {
481 display_enabled = enabled;
482 return !display_enabled;
484 return display_enabled;
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;
495 ULONG GetPixelClock (__REGA0(struct BoardInfo *b), __REGA1(struct ModeInfo *mode_info), __REGD0(ULONG index), __REGD7(RGBFTYPE format)) {
496 // Just return 100MHz.
500 // None of these five really have to do anything.
501 void SetClock (__REGA0(struct BoardInfo *b)) {
504 void SetMemoryMode (__REGA0(struct BoardInfo *b), __REGD7(RGBFTYPE format)) {
507 void SetWriteMask (__REGA0(struct BoardInfo *b), __REGD0(UBYTE mask)) {
510 void SetClearMask (__REGA0(struct BoardInfo *b), __REGD0(UBYTE mask)) {
513 void SetReadPlane (__REGA0(struct BoardInfo *b), __REGD0(UBYTE plane)) {
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.