Added 0.030 of PicoDrive and moved license files into root
[cyclone68000.git] / Pico / VideoPort.cpp
diff --git a/Pico/VideoPort.cpp b/Pico/VideoPort.cpp
new file mode 100644 (file)
index 0000000..322e6c2
--- /dev/null
@@ -0,0 +1,222 @@
+\r
+#include "PicoInt.h"\r
+\r
+static inline void AutoIncrement()\r
+{\r
+  Pico.video.addr=(unsigned short)(Pico.video.addr+Pico.video.reg[0xf]);\r
+}\r
+\r
+static void VideoWrite(unsigned int d)\r
+{\r
+  unsigned int a=0;\r
+  unsigned short sd=(unsigned short)d;\r
+\r
+  a=Pico.video.addr;\r
+  if (a&1) d=((d<<8)&0xff00)|(d>>8); // If address is odd, bytes are swapped\r
+  a>>=1;\r
+\r
+  switch (Pico.video.type)\r
+  {\r
+    case 1: Pico.vram [a&0x7fff]=sd; break;\r
+    case 3: Pico.cram [a&0x003f]=sd; Pico.m.dirtyPal=1; break;\r
+    case 5: Pico.vsram[a&0x003f]=sd; break;\r
+  }\r
+  \r
+  AutoIncrement();\r
+}\r
+\r
+static unsigned int VideoRead()\r
+{\r
+  unsigned int a=0,d=0;\r
+\r
+  a=Pico.video.addr; a>>=1;\r
+\r
+  switch (Pico.video.type)\r
+  {\r
+    case 0: d=Pico.vram [a&0x7fff]; break;\r
+    case 8: d=Pico.cram [a&0x003f]; break;\r
+    case 4: d=Pico.vsram[a&0x003f]; break;\r
+  }\r
+  \r
+  AutoIncrement();\r
+  return d;\r
+}\r
+\r
+static int GetDmaSource()\r
+{\r
+  struct PicoVideo *pvid=&Pico.video;\r
+  int source=0;\r
+  source =pvid->reg[0x15]<<1;\r
+  source|=pvid->reg[0x16]<<9;\r
+  source|=pvid->reg[0x17]<<17;\r
+  return source;\r
+}\r
+\r
+static int GetDmaLength()\r
+{\r
+  struct PicoVideo *pvid=&Pico.video;\r
+  int len=0;\r
+  // 16-bit words to transfer:\r
+  len =pvid->reg[0x13];\r
+  len|=pvid->reg[0x14]<<8;\r
+  return len;\r
+}\r
+\r
+static void DmaSlow(int source,int len)\r
+{\r
+  int i=0,max=0;\r
+\r
+  if (source>=0x800000 && source<0xe00000) return; // Invalid source address\r
+\r
+  /// Clip Cram DMA size (Todds Adventures in Slime World):\r
+  if (Pico.video.type==3) { max=0x80-Pico.video.addr; if (len>max) len=max; }\r
+\r
+  for (i=0;i<len;i++)\r
+  {\r
+    VideoWrite(PicoRead16(source));\r
+    source+=2;\r
+  }\r
+}\r
+\r
+static void DmaCopy(int source,int len)\r
+{\r
+  int i=0;\r
+\r
+  len>>=1; // Length specifies number of bytes\r
+\r
+  for (i=0;i<len;i++)\r
+  {\r
+    VideoWrite(Pico.vram[source&0x7fff]);\r
+    source+=2;\r
+  }\r
+}\r
+\r
+static void DmaFill(int data)\r
+{\r
+  int len=0,i=0;\r
+  \r
+  len=GetDmaLength();\r
+\r
+  for (i=0;i<len+1;i++) VideoWrite(data);\r
+}\r
+\r
+static void CommandDma()\r
+{\r
+  struct PicoVideo *pvid=&Pico.video;\r
+  int len=0,method=0,source=0;\r
+\r
+  if ((pvid->reg[1]&0x10)==0) return; // DMA not enabled\r
+\r
+  len=GetDmaLength();\r
+\r
+  method=pvid->reg[0x17]>>6;\r
+  source=GetDmaSource();\r
+  if (method< 2) DmaSlow(source,len); // 68000 to VDP\r
+  if (method==3) DmaCopy(source,len); // VRAM Copy\r
+}\r
+\r
+static void CommandChange()\r
+{\r
+  struct PicoVideo *pvid=&Pico.video;\r
+  unsigned int cmd=0,addr=0;\r
+\r
+  cmd=pvid->command;\r
+\r
+  // Get type of transfer 0xc0000030 (v/c/vsram read/write)\r
+  pvid->type=(unsigned char)(((cmd>>2)&0xc)|(cmd>>30));\r
+\r
+  // Get address 0x3fff0003\r
+  addr =(cmd>>16)&0x3fff;\r
+  addr|=(cmd<<14)&0xc000;\r
+  pvid->addr=(unsigned short)addr;\r
+\r
+  // Check for dma:\r
+  if (cmd&0x80) CommandDma();\r
+}\r
+\r
+void PicoVideoWrite(unsigned int a,unsigned int d)\r
+{\r
+  struct PicoVideo *pvid=&Pico.video;\r
+\r
+  a&=0x1c;\r
+  d=(unsigned short)d;\r
+\r
+  if (a==0x00) // Data port 0 or 2\r
+  {    \r
+    if (pvid->pending) CommandChange();\r
+    pvid->pending=0;\r
+\r
+    // If a DMA fill has been set up, do it\r
+    if ((pvid->command&0x80) && (pvid->reg[1]&0x10) && (pvid->reg[0x17]>>6)==2)\r
+    {\r
+      DmaFill(d);\r
+    }\r
+    else\r
+    {\r
+      VideoWrite(d);\r
+    }\r
+    return;\r
+  }\r
+\r
+  if (a==0x04) // Command port 4 or 6\r
+  {\r
+    if (pvid->pending)\r
+    {\r
+      // Low word of command:\r
+      pvid->command&=0xffff0000;\r
+      pvid->command|=d;\r
+      pvid->pending=0;\r
+      CommandChange();\r
+      return;\r
+    }\r
+\r
+    if ((d&0xc000)==0x8000)\r
+    {\r
+      // Register write:\r
+      int num=(d>>8)&0x1f;\r
+      pvid->reg[num]=(unsigned char)d;\r
+      return;\r
+    }\r
+\r
+    // High word of command:\r
+    pvid->command&=0x0000ffff;\r
+    pvid->command|=d<<16;\r
+    pvid->pending=1;\r
+  }\r
+}\r
+\r
+unsigned int PicoVideoRead(unsigned int a)\r
+{\r
+  unsigned int d=0;\r
+  \r
+  a&=0x1c;\r
+\r
+  if (a==0x00) { d=VideoRead(); goto end; }\r
+\r
+  if (a==0x04)\r
+  {\r
+    d=Pico.video.status;\r
+\r
+    // Toggle fifo full empty:\r
+    if (Pico.m.rotate&4) d|=0x3520; else d|=0x3620;\r
+    if (Pico.m.rotate&2) d|=0x0004; // Toggle in/out of H-Blank\r
+    Pico.m.rotate++;\r
+\r
+    if (Pico.m.pal) d|=1; // PAL screen\r
+\r
+    goto end;\r
+  }\r
+\r
+  if ((a&0x1c)==0x08)\r
+  {\r
+    if (Pico.m.scanline>-64) d=Pico.m.scanline; // HV-Counter\r
+    else                     d=Pico.m.rotate++; // Fudge\r
+\r
+    d&=0xff; d<<=8;\r
+    goto end;\r
+  }\r
+\r
+end:\r
+\r
+  return d;\r
+}\r