line draw deferment implemented
[picodrive.git] / Pico / VideoPort.c
index 227367f..849389d 100644 (file)
@@ -323,14 +323,32 @@ static void CommandChange(void)
   if (cmd&0x80) CommandDma();\r
 }\r
 \r
+static __inline void DrawSync(int blank_on)\r
+{\r
+  if (Pico.m.scanline < 224 && !(PicoOpt & POPT_ALT_RENDERER) &&\r
+      !PicoSkipFrame && DrawScanline <= Pico.m.scanline) {\r
+    elprintf(EL_ANOMALY, "sync");\r
+    PicoDrawSync(Pico.m.scanline, blank_on);\r
+  }\r
+}\r
+\r
 PICO_INTERNAL_ASM void PicoVideoWrite(unsigned int a,unsigned short d)\r
 {\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
 \r
   if (a==0x00) // Data port 0 or 2\r
   {\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
+       )\r
+      DrawSync(0);\r
+\r
     if (pvid->pending) {\r
       CommandChange();\r
       pvid->pending=0;\r
@@ -344,7 +362,7 @@ PICO_INTERNAL_ASM void PicoVideoWrite(unsigned int a,unsigned short d)
     else\r
     {\r
       // preliminary FIFO emulation for Chaos Engine, The (E)\r
-      if(!(pvid->status&8) && (pvid->reg[1]&0x40) && Pico.m.scanline!=-1 && !(PicoOpt&POPT_DIS_VDP_FIFO)) // active display, accurate mode?\r
+      if (!(pvid->status&8) && (pvid->reg[1]&0x40) && Pico.m.scanline!=-1 && !(PicoOpt&POPT_DIS_VDP_FIFO)) // active display, accurate mode?\r
       {\r
         pvid->status&=~0x200; // FIFO no longer empty\r
         pvid->lwrite_cnt++;\r
@@ -362,8 +380,9 @@ PICO_INTERNAL_ASM void PicoVideoWrite(unsigned int a,unsigned short d)
 \r
   if (a==0x04) // Control (command) port 4 or 6\r
   {\r
-    if(pvid->pending)\r
+    if (pvid->pending)\r
     {\r
+      if (d & 0x80) DrawSync(0); // only need sync for DMA\r
       // Low word of command:\r
       pvid->command&=0xffff0000;\r
       pvid->command|=d;\r
@@ -377,24 +396,26 @@ PICO_INTERNAL_ASM void PicoVideoWrite(unsigned int a,unsigned short d)
         // Register write:\r
         int num=(d>>8)&0x1f;\r
         int dold=pvid->reg[num];\r
+        int update_irq = 0, blank_on = 0;\r
         pvid->type=0; // register writes clear command (else no Sega logo in Golden Axe II)\r
         if (num > 0x0a && !(pvid->reg[1]&4)) {\r
           elprintf(EL_ANOMALY, "%02x written to reg %02x in SMS mode @ %06x", d, num, SekPc);\r
           return;\r
-        } else\r
-          pvid->reg[num]=(unsigned char)d;\r
+        }\r
 \r
         switch (num)\r
         {\r
           case 0x00:\r
             elprintf(EL_INTSW, "hint_onoff: %i->%i [%i] pend=%i @ %06x", (dold&0x10)>>4,\r
                     (d&0x10)>>4, SekCyclesDone(), (pvid->pending_ints&0x10)>>4, SekPc);\r
-            goto update_irq;\r
+            update_irq = 1;\r
+            break;\r
           case 0x01:\r
             elprintf(EL_INTSW, "vint_onoff: %i->%i [%i] pend=%i @ %06x", (dold&0x20)>>5,\r
                     (d&0x20)>>5, SekCyclesDone(), (pvid->pending_ints&0x20)>>5, SekPc);\r
-            if (!(d&0x40) && SekCyclesLeft > 390) rendstatus |= PDRAW_EARLY_BLANK;\r
-            goto update_irq;\r
+            if (!(d&0x40) && SekCyclesLeft > 390) blank_on = 1;\r
+            update_irq = 1;\r
+            break;\r
           case 0x05:\r
             if (d^dold) rendstatus |= PDRAW_SPRITES_MOVED;\r
             break;\r
@@ -403,21 +424,19 @@ PICO_INTERNAL_ASM void PicoVideoWrite(unsigned int a,unsigned short d)
             if ((d^dold)&8) Pico.m.dirtyPal = 2;\r
             break;\r
         }\r
-        return;\r
+        DrawSync(blank_on);\r
+        pvid->reg[num]=(unsigned char)d;\r
+        if (!update_irq) return;\r
 \r
-update_irq:;\r
 #ifndef EMU_CORE_DEBUG\r
-        // update IRQ level (Lemmings, Wiz 'n' Liz intro, ... )\r
-        // may break if done improperly:\r
-        // International Superstar Soccer Deluxe (crash), Street Racer (logos), Burning Force (gfx),\r
-        // Fatal Rewind (crash), Sesame Street Counting Cafe\r
+        // update IRQ level\r
         if (!SekShouldInterrupt) // hack\r
         {\r
           int lines, pints, irq=0;\r
           lines = (pvid->reg[1] & 0x20) | (pvid->reg[0] & 0x10);\r
           pints = (pvid->pending_ints&lines);\r
-               if(pints & 0x20) irq = 6;\r
-          else if(pints & 0x10) irq = 4;\r
+               if (pints & 0x20) irq = 6;\r
+          else if (pints & 0x10) irq = 4;\r
           SekInterrupt(irq); // update line\r
 \r
           if (irq && Pico.m.scanline!=-1) SekEndRun(24); // make it delayed\r
@@ -437,20 +456,17 @@ update_irq:;
 \r
 PICO_INTERNAL_ASM unsigned int PicoVideoRead(unsigned int a)\r
 {\r
-  unsigned int d=0;\r
-\r
   a&=0x1c;\r
 \r
-\r
   if (a==0x00) // data port\r
   {\r
-    d=VideoRead();\r
-    goto end;\r
+    return VideoRead();\r
   }\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 (!(pv->reg[1]&0x40))        d|=0x0008; // set V-Blank if display is disabled\r
@@ -462,7 +478,7 @@ PICO_INTERNAL_ASM unsigned int PicoVideoRead(unsigned int a)
     pv->pending=0; // ctrl port reads clear write-pending flag (Charles MacDonald)\r
 \r
     elprintf(EL_SR, "SR read: %04x @ %06x", d, SekPc);\r
-    goto end;\r
+    return d;\r
   }\r
 \r
   // H-counter info (based on Generator):\r
@@ -482,9 +498,10 @@ PICO_INTERNAL_ASM unsigned int PicoVideoRead(unsigned int a)
   // check: Sonic 3D Blast bonus, Cannon Fodder, Chase HQ II, 3 Ninjas kick back, Road Rash 3, Skitchin', Wheel of Fortune\r
   if ((a&0x1c)==0x08)\r
   {\r
-    unsigned int hc;\r
+    unsigned int hc, d;\r
 \r
-    if(Pico.m.scanline != -1) {\r
+    if (Pico.m.scanline != -1)\r
+    {\r
       int lineCycles=(488-SekCyclesLeft)&0x1ff;\r
       d=Pico.m.scanline; // V-Counter\r
 \r
@@ -500,9 +517,9 @@ PICO_INTERNAL_ASM unsigned int PicoVideoRead(unsigned int a)
     }\r
 \r
     if(Pico.m.pal) {\r
-      if(d >= 0x103) d-=56; // based on Gens\r
+      if (d >= 0x103) d-=56; // based on Gens\r
     } else {\r
-      if(d >= 0xEB)  d-=6;\r
+      if (d >= 0xEB)  d-=6;\r
     }\r
 \r
     if((Pico.video.reg[12]&6) == 6) {\r
@@ -514,10 +531,8 @@ PICO_INTERNAL_ASM unsigned int PicoVideoRead(unsigned int a)
     elprintf(EL_HVCNT, "hv: %02x %02x (%i) @ %06x", hc, d, SekCyclesDone(), SekPc);\r
     d&=0xff; d<<=8;\r
     d|=hc;\r
-    goto end;\r
+    return d;\r
   }\r
 \r
-end:\r
-\r
-  return d;\r
+  return 0;\r
 }\r