rework sr
[picodrive.git] / pico / videoport.c
index 0af4bbc..b5e3f86 100644 (file)
@@ -11,7 +11,6 @@
 #define NEED_DMA_SOURCE\r
 #include "memory.h"\r
 \r
-int line_base_cycles;\r
 extern const unsigned char  hcounts_32[];\r
 extern const unsigned char  hcounts_40[];\r
 \r
@@ -33,23 +32,24 @@ static NOINLINE void VideoWrite128(u32 a, u16 d)
 {\r
   // nasty\r
   a = ((a & 2) >> 1) | ((a & 0x400) >> 9) | (a & 0x3FC) | ((a & 0x1F800) >> 1);\r
-  ((u8 *)Pico.vram)[a] = d;\r
+  ((u8 *)PicoMem.vram)[a] = d;\r
 }\r
 \r
 static void VideoWrite(u16 d)\r
 {\r
-  unsigned int a=Pico.video.addr;\r
+  unsigned int a = Pico.video.addr;\r
 \r
   switch (Pico.video.type)\r
   {\r
-    case 1: if(a&1) d=(u16)((d<<8)|(d>>8)); // If address is odd, bytes are swapped (which game needs this?)\r
-            Pico.vram [(a>>1)&0x7fff]=d;\r
+    case 1: if (a & 1)\r
+              d = (u16)((d << 8) | (d >> 8));\r
+            PicoMem.vram [(a >> 1) & 0x7fff] = d;\r
             if (a - ((unsigned)(Pico.video.reg[5]&0x7f) << 9) < 0x400)\r
               Pico.est.rendstatus |= PDRAW_DIRTY_SPRITES;\r
             break;\r
     case 3: Pico.m.dirtyPal = 1;\r
-            Pico.cram [(a>>1)&0x003f]=d; break; // wraps (Desert Strike)\r
-    case 5: Pico.vsram[(a>>1)&0x003f]=d; break;\r
+            PicoMem.cram [(a >> 1) & 0x3f] = d; break;\r
+    case 5: PicoMem.vsram[(a >> 1) & 0x3f] = d; break;\r
     case 0x81:\r
       a |= Pico.video.addr_u << 16;\r
       VideoWrite128(a, d);\r
@@ -68,9 +68,9 @@ static unsigned int VideoRead(void)
 \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
+    case 0: d=PicoMem.vram [a & 0x7fff]; break;\r
+    case 8: d=PicoMem.cram [a & 0x003f]; break;\r
+    case 4: d=PicoMem.vsram[a & 0x003f]; break;\r
     default:elprintf(EL_ANOMALY, "VDP read with bad type %i", Pico.video.type); break;\r
   }\r
 \r
@@ -106,7 +106,7 @@ static void DmaSlow(int len, unsigned int source)
   SekCyclesBurnRun(CheckDMA());\r
 \r
   if ((source & 0xe00000) == 0xe00000) { // Ram\r
-    base = (u16 *)Pico.ram;\r
+    base = (u16 *)PicoMem.ram;\r
     mask = 0xffff;\r
   }\r
   else if (PicoAHW & PAHW_MCD)\r
@@ -154,7 +154,7 @@ static void DmaSlow(int len, unsigned int source)
   switch (Pico.video.type)\r
   {\r
     case 1: // vram\r
-      r = Pico.vram;\r
+      r = PicoMem.vram;\r
       if (inc == 2 && !(a & 1) && a + len * 2 < 0x10000\r
           && !(((source + len - 1) ^ source) & ~mask))\r
       {\r
@@ -178,7 +178,7 @@ static void DmaSlow(int len, unsigned int source)
 \r
     case 3: // cram\r
       Pico.m.dirtyPal = 1;\r
-      r = Pico.cram;\r
+      r = PicoMem.cram;\r
       for (; len; len--)\r
       {\r
         r[(a / 2) & 0x3f] = base[source++ & mask];\r
@@ -188,7 +188,7 @@ static void DmaSlow(int len, unsigned int source)
       break;\r
 \r
     case 5: // vsram\r
-      r = Pico.vsram;\r
+      r = PicoMem.vsram;\r
       for (; len; len--)\r
       {\r
         r[(a / 2) & 0x3f] = base[source++ & mask];\r
@@ -219,11 +219,11 @@ static void DmaSlow(int len, unsigned int source)
 \r
 static void DmaCopy(int len)\r
 {\r
-  u16 a=Pico.video.addr;\r
-  unsigned char *vr = (unsigned char *) Pico.vram;\r
-  unsigned char inc=Pico.video.reg[0xf];\r
+  u16 a = Pico.video.addr;\r
+  u8 *vr = (u8 *)PicoMem.vram;\r
+  u8 inc = Pico.video.reg[0xf];\r
   int source;\r
-  elprintf(EL_VDPDMA, "DmaCopy len %i [%i]", len, SekCyclesDone());\r
+  elprintf(EL_VDPDMA, "DmaCopy len %i [%u]", len, SekCyclesDone());\r
 \r
   Pico.m.dma_xfers += len;\r
   if (Pico.m.dma_xfers < len)\r
@@ -246,15 +246,15 @@ static void DmaCopy(int len)
 \r
 static NOINLINE void DmaFill(int data)\r
 {\r
-  unsigned short a=Pico.video.addr;\r
-  unsigned char *vr=(unsigned char *) Pico.vram;\r
-  unsigned char high = (unsigned char) (data >> 8);\r
-  unsigned char inc=Pico.video.reg[0xf];\r
+  u16 a = Pico.video.addr;\r
+  u8 *vr = (u8 *)PicoMem.vram;\r
+  u8 high = (u8)(data >> 8);\r
+  u8 inc = Pico.video.reg[0xf];\r
   int source;\r
   int len, l;\r
 \r
   len = GetDmaLength();\r
-  elprintf(EL_VDPDMA, "DmaFill len %i inc %i [%i]", len, inc, SekCyclesDone());\r
+  elprintf(EL_VDPDMA, "DmaFill len %i inc %i [%u]", len, inc, SekCyclesDone());\r
 \r
   Pico.m.dma_xfers += len;\r
   if (Pico.m.dma_xfers < len) // lame 16bit var\r
@@ -357,17 +357,17 @@ PICO_INTERNAL_ASM void PicoVideoWrite(unsigned int a,unsigned short d)
 {\r
   struct PicoVideo *pvid=&Pico.video;\r
 \r
-  //if (Pico.m.scanline < 224)\r
-  //  elprintf(EL_STATUS, "PicoVideoWrite [%06x] %04x", a, d);\r
-  a&=0x1c;\r
+  //elprintf(EL_STATUS, "PicoVideoWrite [%06x] %04x [%u] @ %06x",\r
+  //  a, d, SekCyclesDone(), SekPc);\r
 \r
+  a &= 0x1c;\r
   switch (a)\r
   {\r
   case 0x00: // Data port 0 or 2\r
     // try avoiding the sync..\r
     if (Pico.m.scanline < 224 && (pvid->reg[1]&0x40) &&\r
         !(!pvid->pending &&\r
-          ((pvid->command & 0xc00000f0) == 0x40000010 && Pico.vsram[pvid->addr>>1] == d))\r
+          ((pvid->command & 0xc00000f0) == 0x40000010 && PicoMem.vsram[pvid->addr>>1] == d))\r
        )\r
       DrawSync(0);\r
 \r
@@ -376,17 +376,14 @@ PICO_INTERNAL_ASM void PicoVideoWrite(unsigned int a,unsigned short d)
       pvid->pending=0;\r
     }\r
 \r
-    // preliminary FIFO emulation for Chaos Engine, The (E)\r
-    if (!(pvid->status&8) && (pvid->reg[1]&0x40) && !(PicoOpt&POPT_DIS_VDP_FIFO)) // active display?\r
+    if (!(pvid->status & SR_VB) && !(PicoOpt&POPT_DIS_VDP_FIFO))\r
     {\r
-      pvid->status&=~0x200; // FIFO no longer empty\r
-      pvid->lwrite_cnt++;\r
-      if (pvid->lwrite_cnt >= 4) pvid->status|=0x100; // FIFO full\r
-      if (pvid->lwrite_cnt >  4) {\r
-        SekCyclesBurnRun(32); // penalty // 488/12-8\r
-      }\r
-      elprintf(EL_ASVDP, "VDP data write: %04x [%06x] {%i} #%i @ %06x", d, Pico.video.addr,\r
-               Pico.video.type, pvid->lwrite_cnt, SekPc);\r
+      int use = pvid->type == 1 ? 2 : 1;\r
+      pvid->lwrite_cnt -= use;\r
+      if (pvid->lwrite_cnt < 0)\r
+        SekCyclesLeft = 0;\r
+      elprintf(EL_ASVDP, "VDP data write: [%04x] %04x [%u] {%i} #%i @ %06x",\r
+        Pico.video.addr, d, SekCyclesDone(), Pico.video.type, pvid->lwrite_cnt, SekPc);\r
     }\r
     VideoWrite(d);\r
 \r
@@ -423,19 +420,22 @@ PICO_INTERNAL_ASM void PicoVideoWrite(unsigned int a,unsigned short d)
           return;\r
         }\r
 \r
-        if (num == 1 && !(d&0x40) && SekCyclesDone() - line_base_cycles <= 488-390)\r
+        if (num == 1 && !(d&0x40) && SekCyclesDone() - Pico.t.m68c_line_start <= 488-390)\r
           blank_on = 1;\r
         DrawSync(blank_on);\r
         pvid->reg[num]=(unsigned char)d;\r
         switch (num)\r
         {\r
           case 0x00:\r
-            elprintf(EL_INTSW, "hint_onoff: %i->%i [%i] pend=%i @ %06x", (dold&0x10)>>4,\r
+            elprintf(EL_INTSW, "hint_onoff: %i->%i [%u] pend=%i @ %06x", (dold&0x10)>>4,\r
                     (d&0x10)>>4, SekCyclesDone(), (pvid->pending_ints&0x10)>>4, SekPc);\r
             goto update_irq;\r
           case 0x01:\r
-            elprintf(EL_INTSW, "vint_onoff: %i->%i [%i] pend=%i @ %06x", (dold&0x20)>>5,\r
+            elprintf(EL_INTSW, "vint_onoff: %i->%i [%u] pend=%i @ %06x", (dold&0x20)>>5,\r
                     (d&0x20)>>5, SekCyclesDone(), (pvid->pending_ints&0x20)>>5, SekPc);\r
+            if (!(pvid->status & PVS_VB2))\r
+              pvid->status &= ~SR_VB;\r
+            pvid->status |= ((d >> 3) ^ SR_VB) & SR_VB; // forced blanking\r
             goto update_irq;\r
           case 0x05:\r
             //elprintf(EL_STATUS, "spritep moved to %04x", (unsigned)(Pico.video.reg[5]&0x7f) << 9);\r
@@ -453,14 +453,15 @@ update_irq:
         // update IRQ level\r
         if (!SekShouldInterrupt()) // hack\r
         {\r
-          int lines, pints, irq=0;\r
+          int lines, pints, irq = 0;\r
           lines = (pvid->reg[1] & 0x20) | (pvid->reg[0] & 0x10);\r
-          pints = (pvid->pending_ints&lines);\r
+          pints = pvid->pending_ints & lines;\r
                if (pints & 0x20) irq = 6;\r
           else if (pints & 0x10) irq = 4;\r
           SekInterrupt(irq); // update line\r
 \r
-          if (irq) SekEndRun(24); // make it delayed\r
+          // this is broken because cost of current insn isn't known here\r
+          if (irq) SekEndRun(21); // make it delayed\r
         }\r
 #endif\r
       }\r
@@ -504,26 +505,26 @@ update_irq:
   }\r
 }\r
 \r
-PICO_INTERNAL_ASM unsigned int PicoVideoRead(unsigned int a)\r
+static u32 SrLow(const struct PicoVideo *pv)\r
 {\r
-  a&=0x1c;\r
-\r
-  if (a==0x04) // control port\r
-  {\r
-    struct PicoVideo *pv=&Pico.video;\r
-    unsigned int d;\r
-    d=pv->status;\r
-    //if (PicoOpt&POPT_ALT_RENDERER) d|=0x0020; // sprite collision (Shadow of the Beast)\r
-    if (SekCyclesDone() - line_base_cycles >= 488-88)\r
-      d|=0x0004; // H-Blank (Sonic3 vs)\r
+  unsigned int c, d = pv->status;\r
 \r
-    d |= ((pv->reg[1]&0x40)^0x40) >> 3;  // set V-Blank if display is disabled\r
-    d |= (pv->pending_ints&0x20)<<2;     // V-int pending?\r
-    if (d&0x100) pv->status&=~0x100; // FIFO no longer full\r
+  c = SekCyclesDone() - Pico.t.m68c_line_start - 39;\r
+  if (c < 92)\r
+    d |= SR_HB;\r
+  return d;\r
+}\r
 \r
-    pv->pending = 0; // ctrl port reads clear write-pending flag (Charles MacDonald)\r
+PICO_INTERNAL_ASM unsigned int PicoVideoRead(unsigned int a)\r
+{\r
+  a &= 0x1c;\r
 \r
-    elprintf(EL_SR, "SR read: %04x @ %06x", d, SekPc);\r
+  if (a == 0x04) // control port\r
+  {\r
+    struct PicoVideo *pv = &Pico.video;\r
+    unsigned int d = SrLow(pv);\r
+    pv->pending = 0;\r
+    elprintf(EL_SR, "SR read: %04x [%u] @ %06x", d, SekCyclesDone(), SekPc);\r
     return d;\r
   }\r
 \r
@@ -546,12 +547,12 @@ PICO_INTERNAL_ASM unsigned int PicoVideoRead(unsigned int a)
   {\r
     unsigned int d;\r
 \r
-    d = (SekCyclesDone() - line_base_cycles) & 0x1ff; // FIXME\r
+    d = (SekCyclesDone() - Pico.t.m68c_line_start) & 0x1ff; // FIXME\r
     if (Pico.video.reg[12]&1)\r
          d = hcounts_40[d];\r
     else d = hcounts_32[d];\r
 \r
-    elprintf(EL_HVCNT, "hv: %02x %02x (%i) @ %06x", d, Pico.video.v_counter, SekCyclesDone(), SekPc);\r
+    elprintf(EL_HVCNT, "hv: %02x %02x [%u] @ %06x", d, Pico.video.v_counter, SekCyclesDone(), SekPc);\r
     return d | (Pico.video.v_counter << 8);\r
   }\r
 \r
@@ -573,12 +574,9 @@ unsigned char PicoVideoRead8DataL(void)
   return VideoRead();\r
 }\r
 \r
-// FIXME: broken mess\r
 unsigned char PicoVideoRead8CtlH(void)\r
 {\r
   u8 d = (u8)(Pico.video.status >> 8);\r
-  if (d & 1)\r
-    Pico.video.status &= ~0x100; // FIFO no longer full\r
   Pico.video.pending = 0;\r
   elprintf(EL_SR, "SR read (h): %02x @ %06x", d, SekPc);\r
   return d;\r
@@ -586,11 +584,7 @@ unsigned char PicoVideoRead8CtlH(void)
 \r
 unsigned char PicoVideoRead8CtlL(void)\r
 {\r
-  u8 d = (u8)Pico.video.status;\r
-  //if (PicoOpt&POPT_ALT_RENDERER) d|=0x0020; // sprite collision (Shadow of the Beast)\r
-  d |= ((Pico.video.reg[1]&0x40)^0x40) >> 3;  // set V-Blank if display is disabled\r
-  d |= (Pico.video.pending_ints&0x20)<<2;     // V-int pending?\r
-  if (SekCyclesDone() - line_base_cycles >= 488-88) d |= 4;    // H-Blank\r
+  u8 d = SrLow(&Pico.video);\r
   Pico.video.pending = 0;\r
   elprintf(EL_SR, "SR read (l): %02x @ %06x", d, SekPc);\r
   return d;\r
@@ -598,18 +592,18 @@ unsigned char PicoVideoRead8CtlL(void)
 \r
 unsigned char PicoVideoRead8HV_H(void)\r
 {\r
-  elprintf(EL_HVCNT, "vcounter: %02x (%i) @ %06x", Pico.video.v_counter, SekCyclesDone(), SekPc);\r
+  elprintf(EL_HVCNT, "vcounter: %02x [%u] @ %06x", Pico.video.v_counter, SekCyclesDone(), SekPc);\r
   return Pico.video.v_counter;\r
 }\r
 \r
 // FIXME: broken\r
 unsigned char PicoVideoRead8HV_L(void)\r
 {\r
-  u32 d = (SekCyclesDone() - line_base_cycles) & 0x1ff; // FIXME\r
+  u32 d = (SekCyclesDone() - Pico.t.m68c_line_start) & 0x1ff; // FIXME\r
   if (Pico.video.reg[12]&1)\r
        d = hcounts_40[d];\r
   else d = hcounts_32[d];\r
-  elprintf(EL_HVCNT, "hcounter: %02x (%i) @ %06x", d, SekCyclesDone(), SekPc);\r
+  elprintf(EL_HVCNT, "hcounter: %02x [%u] @ %06x", d, SekCyclesDone(), SekPc);\r
   return d;\r
 }\r
 \r