more sms wip, better ROM detect, line callback change
[picodrive.git] / pico / draw.c
index 551b3d6..747f428 100644 (file)
  * The renderer has 4 modes now:\r
  * - normal\r
  * - shadow/hilight (s/h)\r
- * - "sonic mode" for midline palette changes\r
- * - accurate sprites (AS)\r
+ * - "sonic mode" for midline palette changes (8bit mode only)\r
+ * - accurate sprites (AS) [+ s/h]\r
  *\r
  * AS and s/h both use upper bits for both priority and shadow/hilight flags.\r
  * "sonic mode" is autodetected, shadow/hilight is enabled by emulated game.\r
  * AS is enabled by user and takes priority over "sonic mode".\r
+ *\r
+ * since renderer always draws line in 8bit mode, there are 2 spare bits:\r
+ * b \ mode: s/h             as        sonic\r
+ * 00        normal          -         -\r
+ * 01        shadow          -         pal index\r
+ * 10        hilight+op spr  spr       pal index\r
+ * 11        shadow +op spr  -         pal index\r
+ *\r
+ * not handled properly:\r
+ * - hilight op on shadow tile\r
+ * - AS + s/h (s/h sprite flag interferes with and cleared by AS code)\r
  */\r
 \r
 #include "pico_int.h"\r
@@ -29,7 +40,7 @@ unsigned char *HighCol=DefHighCol;
 #else\r
 unsigned char  HighCol[8+320+8];\r
 #endif\r
-unsigned short DefOutBuff[320*2];\r
+static unsigned int DefOutBuff[320*2/2];\r
 void *DrawLineDest=DefOutBuff; // pointer to dest buffer where to draw this line to\r
 \r
 static int  HighCacheA[41+1];   // caches for high layers\r
@@ -63,7 +74,6 @@ struct TileStrip
 // stuff available in asm:\r
 #ifdef _ASM_DRAW_C\r
 void DrawWindow(int tstart, int tend, int prio, int sh);\r
-void BackFill(int reg7, int sh);\r
 void DrawAllSprites(unsigned char *sprited, int prio, int sh);\r
 void DrawTilesFromCache(int *hc, int sh, int rlim);\r
 void DrawSpritesSHi(unsigned char *sprited);\r
@@ -150,25 +160,25 @@ TileFlipMaker(TileFlip,pix_just_write)
 // draw a sprite pixel, process operator colors\r
 #define pix_sh(x) \\r
   if (!t); \\r
-  else if (t==0xe) pd[x]=(pd[x]&0x3f)|0x80; /* hilight */ \\r
-  else if (t==0xf) pd[x]= pd[x]      |0xc0; /* shadow  */ \\r
+  else if (t>=0xe) pd[x]=(pd[x]&0x3f)|(t<<6); /* c0 shadow, 80 hilight */ \\r
   else pd[x]=pal|t\r
 \r
 TileNormMaker(TileNormSH, pix_sh)\r
 TileFlipMaker(TileFlipSH, pix_sh)\r
 \r
-// draw a sprite pixel ignoring operator colors\r
-#define pix_sh_noop(x) \\r
-  if (t && t < 0xe) \\r
-    pd[x]=pal|t\r
+// draw a sprite pixel, mark operator colors\r
+#define pix_sh_markop(x) \\r
+  if (!t); \\r
+  else if (t>=0xe) pd[x]|=0x80; \\r
+  else pd[x]=pal|t\r
 \r
-TileNormMaker(TileNormSH_noop, pix_sh_noop)\r
-TileFlipMaker(TileFlipSH_noop, pix_sh_noop)\r
+TileNormMaker(TileNormSH_markop, pix_sh_markop)\r
+TileFlipMaker(TileFlipSH_markop, pix_sh_markop)\r
 \r
-// process operator pixels only, apply only on low pri tiles\r
+// process operator pixels only, apply only on low pri tiles and other op pixels\r
 #define pix_sh_onlyop(x) \\r
-  if      (t==0xe && (pd[x]&0x40)) pd[x]=(pd[x]&0x3f)|0x80; /* hilight */ \\r
-  else if (t==0xf && (pd[x]&0x40)) pd[x]= pd[x]      |0xc0; /* shadow  */\r
+  if (t>=0xe && (pd[x]&0xc0)) \\r
+    pd[x]=(pd[x]&0x3f)|(t<<6); /* c0 shadow, 80 hilight */ \\r
 \r
 TileNormMaker(TileNormSH_onlyop_lp, pix_sh_onlyop)\r
 TileFlipMaker(TileFlipSH_onlyop_lp, pix_sh_onlyop)\r
@@ -501,8 +511,8 @@ static void DrawWindow(int tstart, int tend, int prio, int sh) // int *hcache
 \r
       if (prio) {\r
         int *zb = (int *)(HighCol+8+(tilex<<3));\r
-        *zb++ &= 0x3f3f3f3f;\r
-        *zb   &= 0x3f3f3f3f;\r
+        *zb++ &= 0xbfbfbfbf;\r
+        *zb   &= 0xbfbfbfbf;\r
       } else {\r
         pal |= 0x40;\r
       }\r
@@ -524,12 +534,13 @@ static void DrawWindow(int tstart, int tend, int prio, int sh) // int *hcache
 static void DrawTilesFromCacheShPrep(void)\r
 {\r
   // as some layer has covered whole line with hi priority tiles,\r
-  // we can process whole line and then act as if sh/hi mode was off.\r
+  // we can process whole line and then act as if sh/hi mode was off,\r
+  // but leave lo pri op sprite markers alone\r
   int c = 320/4, *zb = (int *)(HighCol+8);\r
   rendstatus |= PDRAW_SHHI_DONE;\r
   while (c--)\r
   {\r
-    *zb++ &= 0x3f3f3f3f;\r
+    *zb++ &= 0xbfbfbfbf;\r
   }\r
 }\r
 \r
@@ -576,8 +587,8 @@ static void DrawTilesFromCache(int *hc, int sh, int rlim)
       addr+=(unsigned int)code>>25; // y offset into tile\r
       dx=(code>>16)&0x1ff;\r
       zb = HighCol+dx;\r
-      *zb++ &= 0x3f; *zb++ &= 0x3f; *zb++ &= 0x3f; *zb++ &= 0x3f;\r
-      *zb++ &= 0x3f; *zb++ &= 0x3f; *zb++ &= 0x3f; *zb++ &= 0x3f;\r
+      *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf;\r
+      *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf;\r
 \r
       pal=((code>>9)&0x30);\r
       if (rlim-dx < 0) goto last_cut_tile;\r
@@ -661,8 +672,8 @@ static void DrawSprite(int *sprite, int sh)
   pal|=sh<<6;\r
 \r
   if (sh && (code&0x6000) == 0x6000) {\r
-    if(code&0x0800) fTileFunc=TileFlipSH_noop;\r
-    else            fTileFunc=TileNormSH_noop;\r
+    if(code&0x0800) fTileFunc=TileFlipSH_markop;\r
+    else            fTileFunc=TileNormSH_markop;\r
   } else {\r
     if(code&0x0800) fTileFunc=TileFlip;\r
     else            fTileFunc=TileNorm;\r
@@ -771,8 +782,13 @@ static void DrawAllSpritesInterlace(int pri, int sh)
 \r
 \r
 #ifndef _ASM_DRAW_C\r
-// Index + 0  :    hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size\r
-// Index + 4  :    xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8\r
+/*\r
+ * s/h drawing: lo_layers|40, lo_sprites|40 && mark_op,\r
+ *        hi_layers&=~40, hi_sprites\r
+ *\r
+ * Index + 0  :    hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size\r
+ * Index + 4  :    xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8\r
+ */\r
 static void DrawSpritesSHi(unsigned char *sprited)\r
 {\r
   int (*fTileFunc)(int sx,int addr,int pal);\r
@@ -915,10 +931,11 @@ static void DrawSpritesHiAS(unsigned char *sprited, int sh)
 \r
   /* nasty 1: remove 'sprite' flags */\r
   {\r
-    int c = 320/4, *zb = (int *)(HighCol+8);\r
+    int c = 320/4/4, *zb = (int *)(HighCol+8);\r
     while (c--)\r
     {\r
-      *zb++ &= 0x7f7f7f7f;\r
+      *zb++ &= 0x7f7f7f7f; *zb++ &= 0x7f7f7f7f;\r
+      *zb++ &= 0x7f7f7f7f; *zb++ &= 0x7f7f7f7f;\r
     }\r
   }\r
 \r
@@ -1122,7 +1139,7 @@ static void DrawAllSprites(unsigned char *sprited, int prio, int sh)
 \r
 // --------------------------------------------\r
 \r
-static void BackFill(int reg7, int sh)\r
+void BackFill(int reg7, int sh)\r
 {\r
   unsigned int back;\r
 \r
@@ -1252,7 +1269,7 @@ static void FinalizeLine8bit(int sh)
   int len, rs = rendstatus;\r
   static int dirty_count;\r
 \r
-  if (!sh && Pico.m.dirtyPal == 1 && DrawScanline < 222)\r
+  if (!sh && Pico.m.dirtyPal == 1)\r
   {\r
     // a hack for mid-frame palette changes\r
     if (!(rs & PDRAW_SONIC_MODE))\r
@@ -1285,26 +1302,10 @@ static void FinalizeLine8bit(int sh)
   }\r
 }\r
 \r
-static void (*FinalizeLine)(int sh) = FinalizeLineBGR444;\r
+static void (*FinalizeLine)(int sh);\r
 \r
 // --------------------------------------------\r
 \r
-static void DrawBlankedLine(void)\r
-{\r
-  int sh=(Pico.video.reg[0xC]&8)>>3; // shadow/hilight?\r
-\r
-  if (PicoScanBegin != NULL)\r
-    PicoScanBegin(DrawScanline);\r
-\r
-  BackFill(Pico.video.reg[7], sh);\r
-\r
-  if (FinalizeLine != NULL)\r
-    FinalizeLine(sh);\r
-\r
-  if (PicoScanEnd != NULL)\r
-    PicoScanEnd(DrawScanline);\r
-}\r
-\r
 static int DrawDisplay(int sh)\r
 {\r
   unsigned char *sprited = &HighLnSpr[DrawScanline][0];\r
@@ -1403,23 +1404,46 @@ PICO_INTERNAL void PicoFrameStart(void)
   rendstatus = 0;\r
   if ((Pico.video.reg[12]&6) == 6)\r
     rendstatus |= PDRAW_INTERLACE; // interlace mode\r
+  if (Pico.video.reg[1] & 8)\r
+    rendstatus |= PDRAW_240LINES;\r
 \r
-  if (Pico.m.dirtyPal) Pico.m.dirtyPal = 2; // reset dirty if needed\r
+  if (Pico.m.dirtyPal)\r
+    Pico.m.dirtyPal = 2; // reset dirty if needed\r
 \r
   DrawScanline=0;\r
   PrepareSprites(1);\r
   skip_next_line=0;\r
 }\r
 \r
-static void PicoLine(void)\r
+static void DrawBlankedLine(int line, int offs)\r
+{\r
+  int sh = (Pico.video.reg[0xC]&8)>>3; // shadow/hilight?\r
+\r
+  if (PicoScanBegin != NULL)\r
+    PicoScanBegin(line + offs);\r
+\r
+  BackFill(Pico.video.reg[7], sh);\r
+\r
+  if (FinalizeLine != NULL)\r
+    FinalizeLine(sh);\r
+\r
+  if (PicoScanEnd != NULL)\r
+    PicoScanEnd(line + offs);\r
+}\r
+\r
+static void PicoLine(int line, int offs)\r
 {\r
   int sh;\r
-  if (skip_next_line>0) { skip_next_line--; return; } // skip rendering lines\r
+  if (skip_next_line > 0) {\r
+    skip_next_line--;\r
+    return;\r
+  }\r
 \r
   sh=(Pico.video.reg[0xC]&8)>>3; // shadow/hilight?\r
 \r
+  DrawScanline = line;\r
   if (PicoScanBegin != NULL)\r
-    skip_next_line = PicoScanBegin(DrawScanline);\r
+    skip_next_line = PicoScanBegin(line + offs);\r
 \r
   // Draw screen:\r
   BackFill(Pico.video.reg[7], sh);\r
@@ -1430,31 +1454,40 @@ static void PicoLine(void)
     FinalizeLine(sh);\r
 \r
   if (PicoScanEnd != NULL)\r
-    skip_next_line = PicoScanEnd(DrawScanline);\r
+    skip_next_line = PicoScanEnd(line + offs);\r
 }\r
 \r
 void PicoDrawSync(int to, int blank_last_line)\r
 {\r
-  for (; DrawScanline < to; DrawScanline++)\r
+  int line, offs = 0;\r
+\r
+  if (!(rendstatus & PDRAW_240LINES))\r
+    offs = 8;\r
+\r
+  for (line = DrawScanline; line < to; line++)\r
   {\r
 #if !CAN_HANDLE_240_LINES\r
-    if (DrawScanline >= 224) break;\r
+    if (line >= 224) break;\r
 #endif\r
-    PicoLine();\r
+    PicoLine(line, offs);\r
   }\r
 \r
 #if !CAN_HANDLE_240_LINES\r
-  if (DrawScanline >= 224) { DrawScanline = 240; return; }\r
+  if (line >= 224) {\r
+    DrawScanline = 240;\r
+    return;\r
+  }\r
 #endif\r
 \r
   // last line\r
-  if (DrawScanline <= to)\r
+  if (line <= to)\r
   {\r
     if (blank_last_line)\r
-         DrawBlankedLine();\r
-    else PicoLine();\r
-    DrawScanline++;\r
+         DrawBlankedLine(line, offs);\r
+    else PicoLine(line, offs);\r
+    line++;\r
   }\r
+  DrawScanline = line;\r
 }\r
 \r
 void PicoDrawSetColorFormat(int which)\r
@@ -1466,8 +1499,10 @@ void PicoDrawSetColorFormat(int which)
     case 0: FinalizeLine = FinalizeLineBGR444; break;\r
     default:FinalizeLine = NULL; break;\r
   }\r
+  PicoDrawSetColorFormatMode4(which);\r
 #if OVERRIDE_HIGHCOL\r
-  if (which) HighCol=DefHighCol;\r
+  if (which)\r
+    HighCol=DefHighCol;\r
 #endif\r
 }\r
 \r