sprite cache removed
[picodrive.git] / Pico / Draw.c
index 0c5f178..aea679f 100644 (file)
@@ -34,20 +34,20 @@ void *DrawLineDest=DefOutBuff; // pointer to dest buffer where to draw this line
 \r
 static int  HighCacheA[41+1];   // caches for high layers\r
 static int  HighCacheB[41+1];\r
-static int  HighCacheS[80+1];   // and sprites\r
-static int  HighPreSpr[80*2+1]; // slightly preprocessed sprites\r
-int *HighCacheS_ptr;\r
+int  HighPreSpr[80*2+1]; // slightly preprocessed sprites\r
+\r
+#define MAX_LINE_SPRITES 30\r
+unsigned char HighLnSpr[240][2 + MAX_LINE_SPRITES]; // sprite_count, tile_count, [spritep]...\r
 \r
 int rendstatus = 0;\r
 int DrawScanline = 0;\r
 \r
-static int SpriteBlocks;\r
 static int skip_next_line=0;\r
 \r
 //unsigned short ppt[] = { 0x0f11, 0x0ff1, 0x01f1, 0x011f, 0x01ff, 0x0f1f, 0x0f0e, 0x0e7c };\r
 \r
-static void (*DrawAllSpritesLoPri)(int *hcache, int maxwidth, int prio, int sh) = NULL;\r
-static void (*DrawAllSpritesHiPri)(int *hcache, int maxwidth, int prio, int sh) = NULL;\r
+static void (*DrawAllSpritesLoPri)(int prio, int sh) = NULL;\r
+static void (*DrawAllSpritesHiPri)(int prio, int sh) = NULL;\r
 \r
 struct TileStrip\r
 {\r
@@ -63,9 +63,9 @@ struct TileStrip
 #ifdef _ASM_DRAW_C\r
 void DrawWindow(int tstart, int tend, int prio, int sh);\r
 void BackFill(int reg7, int sh);\r
-void DrawSprite(int *sprite, int sh, int as);\r
+void DrawAllSprites(int prio, int sh);\r
 void DrawTilesFromCache(int *hc, int sh, int rlim);\r
-void DrawSpritesFromCache(int *hc, int maxwidth, int prio, int sh);\r
+void DrawSpritesSHi(int prio, int sh);\r
 void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells);\r
 void FinalizeLineBGR444(int sh);\r
 void FinalizeLineRGB555(int sh);\r
@@ -609,13 +609,13 @@ last_cut_tile:
       switch (rlim-dx+8)\r
       {\r
         case 7: t=pack&0x00f00000; if (t) pd[6]=(unsigned char)(pal|(t>>20));\r
-       case 6: t=pack&0x0f000000; if (t) pd[5]=(unsigned char)(pal|(t>>24));\r
-       case 5: t=pack&0xf0000000; if (t) pd[4]=(unsigned char)(pal|(t>>28));\r
-       case 4: t=pack&0x0000000f; if (t) pd[3]=(unsigned char)(pal|(t    ));\r
-       case 3: t=pack&0x000000f0; if (t) pd[2]=(unsigned char)(pal|(t>> 4));\r
-       case 2: t=pack&0x00000f00; if (t) pd[1]=(unsigned char)(pal|(t>> 8));\r
-       case 1: t=pack&0x0000f000; if (t) pd[0]=(unsigned char)(pal|(t>>12));\r
-       default: break;\r
+        case 6: t=pack&0x0f000000; if (t) pd[5]=(unsigned char)(pal|(t>>24));\r
+        case 5: t=pack&0xf0000000; if (t) pd[4]=(unsigned char)(pal|(t>>28));\r
+        case 4: t=pack&0x0000000f; if (t) pd[3]=(unsigned char)(pal|(t    ));\r
+        case 3: t=pack&0x000000f0; if (t) pd[2]=(unsigned char)(pal|(t>> 4));\r
+        case 2: t=pack&0x00000f00; if (t) pd[1]=(unsigned char)(pal|(t>> 8));\r
+        case 1: t=pack&0x0000f000; if (t) pd[0]=(unsigned char)(pal|(t>>12));\r
+        default: break;\r
       }\r
     }\r
   }\r
@@ -652,17 +652,9 @@ static void DrawSprite(int *sprite, int sh, int as)
   if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X\r
 \r
   tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address\r
+  delta<<=4; // Delta of address\r
 \r
   pal=(code>>9)&0x30;\r
-\r
-  // assume there will be no sprites with both normal and operator pixels..\r
-  if ((code&0x8000) || (sh && pal == 0x30) || as) {\r
-    *HighCacheS_ptr++ = ((code&0x8000)<<16)|(tile<<16)|((code&0x0800)<<5)|((sx<<6)&0x0000ffc0)|pal|((sprite[0]>>16)&0xf);\r
-    // we need all for accurate sprites, cached will be used to do proper priorities\r
-    if (!as && (code&0x8000)) return;\r
-  }\r
-\r
-  delta<<=4; // Delta of address\r
   pal|=((sh|as)<<6);\r
 \r
   if (sh && (code&0x6000) == 0x6000) {\r
@@ -728,7 +720,7 @@ static void DrawSpriteInterlace(unsigned int *sprite)
 }\r
 \r
 \r
-static void DrawAllSpritesInterlace(int *hcache, int maxwidth, int pri, int sh)\r
+static void DrawAllSpritesInterlace(int pri, int sh)\r
 {\r
   struct PicoVideo *pvid=&Pico.video;\r
   int i,u,table,link=0,sline=DrawScanline<<1;\r
@@ -758,7 +750,7 @@ static void DrawAllSpritesInterlace(int *hcache, int maxwidth, int pri, int sh)
     // check if sprite is not hidden offscreen\r
     sx = (sx>>16)&0x1ff;\r
     sx -= 0x78; // Get X coordinate + 8\r
-    if(sx <= -8*3 || sx >= maxwidth) goto nextsprite;\r
+    if(sx <= -8*3 || sx >= 328) goto nextsprite;\r
 \r
     // sprite is good, save it's pointer\r
     sprites[i++]=sprite;\r
@@ -772,46 +764,68 @@ static void DrawAllSpritesInterlace(int *hcache, int maxwidth, int pri, int sh)
   // Go through sprites backwards:\r
   for (i-- ;i>=0; i--)\r
     DrawSpriteInterlace(sprites[i]);\r
-\r
-  HighCacheS[0] = 1; // just to fool main code\r
 }\r
 \r
 \r
 #ifndef _ASM_DRAW_C\r
-static void DrawSpritesFromCache(int *hc, int maxwidth, int prio, int sh)\r
+// Index + 0  :    hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size\r
+// Index + 4  :    xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8\r
+static void DrawSpritesSHi(int prio, int sh)\r
 {\r
-  int code, tile, sx, delta, width;\r
-  int pal;\r
   int (*fTileFunc)(int sx,int addr,int pal);\r
+  unsigned char *p;\r
+  int cnt;\r
+\r
+  cnt = HighLnSpr[DrawScanline][0] & 0x7f;\r
+  if (cnt == 0) return;\r
 \r
-  // prio[31]:tile[30:17]:flipx[16]:sx[15:6]:pal[5:4]:delta_width[3:0]\r
+  p = &HighLnSpr[DrawScanline][2];\r
 \r
-  while ((code=*hc++))\r
+  // Go through sprites backwards:\r
+  for (cnt--; cnt >= 0; cnt--)\r
   {\r
-    pal=(code&0x30);\r
-    delta=code&0xf;\r
-    width=delta>>2; delta&=3;\r
-    width++; delta++; // Width and height in tiles\r
-    if (code&0x10000) delta=-delta; // Flip X\r
-    delta<<=4;\r
-    tile=((unsigned int)code>>17)<<1; // also has prio\r
-    sx=(code<<16)>>22; // sx can be negative (start offscreen), so sign extend\r
+    int *sprite, code, pal, tile, sx, sy;\r
+    int offs, delta, width, height, row;\r
+\r
+    offs = (p[cnt] & 0x7f) * 2;\r
+    sprite = HighPreSpr + offs;\r
+    code = sprite[1];\r
+    pal = (code>>9)&0x30;\r
 \r
     if (sh && pal == 0x30)\r
     {\r
-      if (code & 0x80000000) // hi priority\r
+      if (code & 0x8000) // hi priority\r
       {\r
-        if(code&0x10000) fTileFunc=TileFlipSH;\r
-        else             fTileFunc=TileNormSH;\r
+        if (code&0x800) fTileFunc=TileFlipSH;\r
+        else            fTileFunc=TileNormSH;\r
       } else {\r
-        if(code&0x10000) fTileFunc=TileFlipSH_onlyop_lp;\r
-        else             fTileFunc=TileNormSH_onlyop_lp;\r
+        if (code&0x800) fTileFunc=TileFlipSH_onlyop_lp;\r
+        else            fTileFunc=TileNormSH_onlyop_lp;\r
       }\r
     } else {\r
-      if(code&0x10000) fTileFunc=TileFlip;\r
-      else             fTileFunc=TileNorm;\r
+      if (!(code & 0x8000)) continue; // non-operator low sprite, already drawn\r
+      if (code&0x800) fTileFunc=TileFlip;\r
+      else            fTileFunc=TileNorm;\r
     }\r
 \r
+    // parse remaining sprite data\r
+    sy=sprite[0];\r
+    sx=code>>16; // X\r
+    width=sy>>28;\r
+    height=(sy>>24)&7; // Width and height in tiles\r
+    sy=(sy<<16)>>16; // Y\r
+\r
+    row=DrawScanline-sy; // Row of the sprite we are on\r
+\r
+    if (code&0x1000) row=(height<<3)-1-row; // Flip Y\r
+\r
+    tile=code + (row>>3); // Tile number increases going down\r
+    delta=height; // Delta to increase tile by going right\r
+    if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X\r
+\r
+    tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address\r
+    delta<<=4; // Delta of address\r
+\r
     for (; width; width--,sx+=8,tile+=delta)\r
     {\r
       if(sx<=0)   continue;\r
@@ -824,44 +838,62 @@ static void DrawSpritesFromCache(int *hc, int maxwidth, int prio, int sh)
 }\r
 #endif\r
 \r
-static void DrawSpritesFromCacheAS(int *hc, int maxwidth, int prio, int sh)\r
+static void DrawSpritesHiAS(int prio, int sh)\r
 {\r
-  int code, tile, sx, delta, width;\r
-  int pal, *hce, *hco;\r
   int (*fTileFunc)(int sx,int addr,int pal);\r
+  unsigned char *p;\r
+  int entry, cnt, sh_cnt = 0;\r
 \r
-  // prio[31]:tile[30:17]:flipx[16]:sx[15:6]:pal[5:4]:delta_width[3:0]\r
+  cnt = HighLnSpr[DrawScanline][0] & 0x7f;\r
+  if (cnt == 0) return;\r
 \r
-  /* walk the sprite cache backwards.. */\r
-  hco = hce = HighCacheS_ptr;\r
-  while (hce > hc)\r
+  p = &HighLnSpr[DrawScanline][2];\r
+\r
+  // Go through sprites:\r
+  for (entry = 0; entry < cnt; entry++)\r
   {\r
-    code=*(--hce);\r
-    pal=(code&0x30);\r
-    delta=code&0xf;\r
-    width=delta>>2; delta&=3;\r
-    width++; delta++; // Width and height in tiles\r
-    if (code&0x10000) delta=-delta; // Flip X\r
-    delta<<=4;\r
-    tile=((unsigned int)code>>17)<<1;\r
-    sx=(code<<16)>>22; // sx can be negative (start offscreen), so sign extend\r
-\r
-    if (code & 0x80000000) // hi priority\r
+    int *sprite, code, pal, tile, sx, sy;\r
+    int offs, delta, width, height, row;\r
+\r
+    offs = (p[entry] & 0x7f) * 2;\r
+    sprite = HighPreSpr + offs;\r
+    code = sprite[1];\r
+    pal = (code>>9)&0x30;\r
+\r
+    if (code & 0x8000) // hi priority\r
     {\r
       if (sh && pal == 0x30)\r
       {\r
-        if(code&0x10000) fTileFunc=TileFlipAS_noop;\r
-        else             fTileFunc=TileNormAS_noop;\r
+        if (code&0x800) fTileFunc=TileFlipAS_noop;\r
+        else            fTileFunc=TileNormAS_noop;\r
       } else {\r
-        if(code&0x10000) fTileFunc=TileFlipAS;\r
-        else             fTileFunc=TileNormAS;\r
+        if (code&0x800) fTileFunc=TileFlipAS;\r
+        else            fTileFunc=TileNormAS;\r
       }\r
     } else {\r
-      if(code&0x10000) fTileFunc=TileFlipAS_onlymark;\r
-      else             fTileFunc=TileNormAS_onlymark;\r
+      if (code&0x800) fTileFunc=TileFlipAS_onlymark;\r
+      else            fTileFunc=TileNormAS_onlymark;\r
     }\r
     if (sh && pal == 0x30)\r
-      *(--hco) = code; /* save for later */\r
+      p[sh_cnt++] = offs / 2; // re-save for sh/hi pass\r
+\r
+    // parse remaining sprite data\r
+    sy=sprite[0];\r
+    sx=code>>16; // X\r
+    width=sy>>28;\r
+    height=(sy>>24)&7; // Width and height in tiles\r
+    sy=(sy<<16)>>16; // Y\r
+\r
+    row=DrawScanline-sy; // Row of the sprite we are on\r
+\r
+    if (code&0x1000) row=(height<<3)-1-row; // Flip Y\r
+\r
+    tile=code + (row>>3); // Tile number increases going down\r
+    delta=height; // Delta to increase tile by going right\r
+    if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X\r
+\r
+    tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address\r
+    delta<<=4; // Delta of address\r
 \r
     pal |= 0x80;\r
     for (; width; width--,sx+=8,tile+=delta)\r
@@ -885,54 +917,33 @@ static void DrawSpritesFromCacheAS(int *hc, int maxwidth, int prio, int sh)
     }\r
   }\r
 \r
-  /* nasty 2: loop once more and do operator colors */\r
-  while ((code=*hco++))\r
-  {\r
-    pal=(code&0x30);\r
-    if (pal != 0x30) continue;\r
-    delta=code&0xf;\r
-    width=delta>>2; delta&=3;\r
-    width++; delta++;\r
-    if (code&0x10000) delta=-delta; // Flip X\r
-    delta<<=4;\r
-    tile=((unsigned int)code>>17)<<1;\r
-    sx=(code<<16)>>22;\r
-\r
-    if (code & 0x80000000)\r
-    {\r
-      if(code&0x10000) fTileFunc=TileFlipSH;\r
-      else             fTileFunc=TileNormSH;\r
-    } else {\r
-      if(code&0x10000) fTileFunc=TileFlipSH_onlyop_lp;\r
-      else             fTileFunc=TileNormSH_onlyop_lp;\r
-    }\r
-\r
-    pal |= 0x80;\r
-    for (; width; width--,sx+=8,tile+=delta)\r
-    {\r
-      if(sx<=0)   continue;\r
-      if(sx>=328) break; // Offscreen\r
-\r
-      tile&=0x7fff; // Clip tile address\r
-      fTileFunc(sx,tile,pal);\r
-    }\r
-  }\r
+  /* nasty 2: sh operator pass */\r
+  HighLnSpr[DrawScanline][0] = sh_cnt;\r
+  DrawSpritesSHi(1, 1);\r
 }\r
 \r
 \r
-\r
 // Index + 0  :    ----hhvv -lllllll -------y yyyyyyyy\r
 // Index + 4  :    -------x xxxxxxxx pccvhnnn nnnnnnnn\r
 // v\r
-// Index + 0  :    hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size\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 PrepareSprites(int full)\r
+void PrepareSprites(int full)\r
 {\r
   struct PicoVideo *pvid=&Pico.video;\r
-  int u=0,link=0,sblocks=0;\r
+  int u,link=0;\r
   int table=0;\r
   int *pd = HighPreSpr;\r
+  int max_lines = 224, max_sprites = 80, max_width = 328;\r
+  int max_line_sprites = 20; // 20 sprites, 40 tiles\r
+\r
+  if (!(Pico.video.reg[12]&1))\r
+    max_sprites = 64, max_line_sprites = 16, max_width = 264;\r
+  if (PicoOpt & POPT_DIS_SPRITE_LIM)\r
+    max_line_sprites = MAX_LINE_SPRITES;\r
+\r
+  if (pvid->reg[1]&8) max_lines = 240;\r
 \r
   table=pvid->reg[5]&0x7f;\r
   if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode\r
@@ -942,41 +953,60 @@ static void PrepareSprites(int full)
   {\r
     int pack;\r
     // updates: tilecode, sx\r
-    for (u=0; u < 80 && (pack = *pd); u++, pd+=2)\r
+    for (u=0; u < max_lines && (pack = *pd); u++, pd+=2)\r
     {\r
       unsigned int *sprite;\r
-      int code, code2, sx, sy, skip=0;\r
+      int code2, sx, sy, height;\r
 \r
       sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
 \r
       // parse sprite info\r
-      code  = sprite[0];\r
       code2 = sprite[1];\r
-      code2 &= ~0xfe000000;\r
-      code2 -=  0x00780000; // Get X coordinate + 8 in upper 16 bits\r
-      sx = code2>>16;\r
+      sx = (code2>>16)&0x1ff;\r
+      sx -= 0x78; // Get X coordinate + 8\r
+      sy = (pack << 16) >> 16;\r
+      height = (pack >> 24) & 0xf;\r
 \r
-      if((sx <= 8-((pack>>28)<<3) && sx >= -0x76) || sx >= 328) skip=1<<23;\r
-      else if ((sy = (pack<<16)>>16) < 240 && sy > -32) {\r
-        int sbl = (2<<(pack>>28))-1;\r
-        sblocks |= sbl<<(sy>>3);\r
+      if (sy < max_lines && sy + (height<<3) > DrawScanline && // sprite onscreen (y)?\r
+          (sx > -24 || sx < max_width))                   // onscreen x\r
+      {\r
+        int y = (sy >= DrawScanline) ? sy : DrawScanline;\r
+        int entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80);\r
+        for (; y < sy + (height<<3) && y < max_lines; y++)\r
+        {\r
+          int i, cnt;\r
+          cnt = HighLnSpr[y][0] & 0x7f;\r
+          if (cnt >= max_line_sprites) continue;              // sprite limit?\r
+\r
+          for (i = 0; i < cnt; i++)\r
+            if (((HighLnSpr[y][2+i] ^ entry) & 0x7f) == 0) goto found;\r
+\r
+          // this sprite was previously missing\r
+          HighLnSpr[y][2+cnt] = entry;\r
+          HighLnSpr[y][0] = cnt + 1;\r
+found:;\r
+        }\r
       }\r
 \r
-      *pd = (pack&~(1<<23))|skip;\r
-      *(pd+1) = code2;\r
+      code2 &= ~0xfe000000;\r
+      code2 -=  0x00780000; // Get X coordinate + 8 in upper 16 bits\r
+      pd[1] = code2;\r
 \r
       // Find next sprite\r
-      link=(code>>16)&0x7f;\r
-      if(!link) break; // End of sprites\r
+      link=(sprite[0]>>16)&0x7f;\r
+      if (!link) break; // End of sprites\r
     }\r
-    SpriteBlocks |= sblocks;\r
   }\r
   else\r
   {\r
-    for (; u < 80; u++)\r
+    for (u = 0; u < max_lines; u++)\r
+      *((int *)&HighLnSpr[u][0]) = 0;\r
+\r
+    for (u = 0; u < max_lines; u++)\r
     {\r
       unsigned int *sprite;\r
-      int code, code2, sx, sy, hv, height, width, skip=0, sx_min;\r
+      int code, code2, sx, sy, hv, height, width;\r
+      int sx_min;\r
 \r
       sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
 \r
@@ -986,110 +1016,97 @@ static void PrepareSprites(int full)
       hv = (code>>24)&0xf;\r
       height = (hv&3)+1;\r
 \r
-      if (sy > 240 || sy + (height<<3) <= 0) skip|=1<<22; // sprite offscreen (completely, y)\r
-\r
       width  = (hv>>2)+1;\r
       code2 = sprite[1];\r
       sx = (code2>>16)&0x1ff;\r
       sx -= 0x78; // Get X coordinate + 8\r
       sx_min = 8-(width<<3);\r
 \r
-      if ((sx <= sx_min && sx >= -0x76) || sx >= 328) skip|=1<<23; // offscreen x\r
-      else if (sx > sx_min && !skip) {\r
-        int sbl = (2<<height)-1;\r
-        int shi = sy>>3;\r
-        if(shi < 0) shi=0; // negative sy\r
-        sblocks |= sbl<<shi;\r
+      if (sy < max_lines && sy + (height<<3) > DrawScanline) // sprite onscreen (y)?\r
+      {\r
+        int y = (sy >= DrawScanline) ? sy : DrawScanline;\r
+        int entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80);\r
+        for (; y < sy + (height<<3) && y < max_lines; y++)\r
+        {\r
+          int cnt = HighLnSpr[y][0];\r
+          if (cnt >= max_line_sprites) continue;              // sprite limit?\r
+\r
+          if (HighLnSpr[y][1] >= max_line_sprites*2) {        // tile limit?\r
+            HighLnSpr[y][0] |= 0x80;\r
+            continue;\r
+          }\r
+          HighLnSpr[y][1] += width;\r
+\r
+          if (sx == -0x78) {\r
+            if (cnt > 0)\r
+              HighLnSpr[y][0] |= 0x80; // masked, no more sprites for this line\r
+            continue;\r
+          }\r
+          // must keep the first sprite even if it's offscreen, for masking\r
+          if (cnt > 0 && (sx <= sx_min || sx >= max_width)) continue; // offscreen x\r
+\r
+          HighLnSpr[y][2+cnt] = entry;\r
+          HighLnSpr[y][0] = cnt + 1;\r
+          if (entry & 0x80)\r
+               rendstatus |= PDRAW_HAVE_HI_SPR;\r
+          else rendstatus |= PDRAW_HAVE_LO_SPR;\r
+        }\r
       }\r
 \r
-      *pd++ = (width<<28)|(height<<24)|skip|(hv<<16)|((unsigned short)sy);\r
+      *pd++ = (width<<28)|(height<<24)|(hv<<16)|((unsigned short)sy);\r
       *pd++ = (sx<<16)|((unsigned short)code2);\r
 \r
       // Find next sprite\r
       link=(code>>16)&0x7f;\r
-      if(!link) break; // End of sprites\r
+      if (!link) break; // End of sprites\r
+    }\r
+    *pd = 0;\r
+\r
+#if 0\r
+    for (u = 0; u < max_lines; u++)\r
+    {\r
+      int y;\r
+      printf("c%03i: %2i, %2i: ", u, HighLnSpr[u][0] & 0x7f, HighLnSpr[u][1]);\r
+      for (y = 0; y < HighLnSpr[u][0] & 0x7f; y++)\r
+        printf(" %i", HighLnSpr[u][y+2]);\r
+      printf("\n");\r
     }\r
-    SpriteBlocks = sblocks;\r
-    *pd = 0; // terminate\r
+#endif\r
   }\r
 }\r
 \r
-static void DrawAllSprites(int *hcache, int maxwidth, int prio, int sh)\r
+#ifndef _ASM_DRAW_C\r
+static void DrawAllSprites(int prio, int sh)\r
 {\r
-  int i,u,n;\r
-  int sx1seen = 0; // sprite with x coord 1 or 0 seen\r
-  int ntiles = 0; // tile counter for sprite limit emulation\r
-  int *sprites[40]; // Sprites to draw in fast mode\r
-  int max_line_sprites = 20; // 20 sprites, 40 tiles\r
-  int *ps, pack, rs = rendstatus, scan = DrawScanline;\r
+  int rs = rendstatus, scan = DrawScanline;\r
+  unsigned char *p;\r
+  int cnt, as;\r
 \r
   if (rs & (PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES)) {\r
-    //dprintf("PrepareSprites(%i) [%i]", (rs>>4)&1, scan);\r
+    //elprintf(EL_STATUS, "PrepareSprites(%i)", (rs>>4)&1);\r
     PrepareSprites(rs & PDRAW_DIRTY_SPRITES);\r
     rendstatus = rs & ~(PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES);\r
   }\r
-  if (!(SpriteBlocks & (1<<(scan>>3)))) { *hcache = 0; return; }\r
-\r
-  if (PicoOpt & POPT_DIS_SPRITE_LIM)\r
-    max_line_sprites = 80;\r
 \r
-  HighCacheS_ptr = hcache;\r
-  ps = HighPreSpr;\r
+  cnt = HighLnSpr[scan][0] & 0x7f;\r
+  if (cnt == 0) return;\r
 \r
-  // Index + 0  :    hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size\r
-  // Index + 4  :    xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8\r
+  p = &HighLnSpr[scan][2];\r
+  as = (rs & PDRAW_ACC_SPRITES) ? 1 : 0;\r
 \r
-  for (i=u=n=0; (pack = *ps) && n < max_line_sprites; ps+=2, u++)\r
+  // Go through sprites backwards:\r
+  for (cnt--; cnt >= 0; cnt--)\r
   {\r
-    int sx, sy, row, pack2;\r
-\r
-    if (pack & 0x00400000) continue;\r
-\r
-    // get sprite info\r
-    pack2 = *(ps+1);\r
-    sx =  pack2>>16;\r
-    sy = (pack <<16)>>16;\r
-    row = scan-sy;\r
-\r
-    // elprintf(EL_ANOMALY, "x: %4i y: %4i p %i %ix%i", sx, sy, (pack2>>15)&1, (pack>>28)<<3, (pack>>21)&0x38);\r
-\r
-    if (sx == -0x77) sx1seen|=1; // for masking mode 2\r
-\r
-    // check if it is on this line\r
-    if (row < 0 || row >= ((pack>>21)&0x38)) continue; // no\r
-\r
-    // masking sprite?\r
-    if (sx == -0x78) {\r
-      if (n > 0) break; // masked\r
-      continue;\r
-    }\r
-\r
-    n++; // number of sprites on this line (both visible and hidden, except of x=0)\r
-\r
-    // sprite limit\r
-    ntiles += pack>>28;\r
-    if (ntiles > max_line_sprites*2) break;\r
-\r
-    if (pack & 0x00800000) continue;\r
-\r
-    // sprite is good, save it's pointer\r
-    sprites[i++]=ps;\r
+    int offs;\r
+    if ((p[cnt] >> 7) != prio && !as) continue;\r
+    offs = (p[cnt]&0x7f) * 2;\r
+    DrawSprite(HighPreSpr + offs, sh, as);\r
   }\r
-\r
-  n = (rs & PDRAW_ACC_SPRITES) ? 1 : 0;\r
-\r
-  // Go through sprites backwards:\r
-  for (i--; i>=0; i--)\r
-    DrawSprite(sprites[i],sh,n);\r
-\r
-  // terminate cache list\r
-  *HighCacheS_ptr = 0;\r
 }\r
 \r
 \r
 // --------------------------------------------\r
 \r
-#ifndef _ASM_DRAW_C\r
 static void BackFill(int reg7, int sh)\r
 {\r
   unsigned int back;\r
@@ -1309,17 +1326,19 @@ static int DrawDisplay(int sh, int as)
     DrawWindow(                           (win&0x80) ? edge :       0, (win&0x80) ? maxcells>>1 : edge, 0, sh|as);\r
   } else\r
     DrawLayer(0|((sh|as)<<1), HighCacheA, 0, maxcells);\r
-  DrawAllSpritesLoPri(HighCacheS, maxw, 0, sh);\r
+  if ((rendstatus & PDRAW_HAVE_LO_SPR) || as)\r
+    DrawAllSpritesLoPri(0, sh);\r
 \r
-  if (HighCacheB[0]) DrawTilesFromCache(HighCacheB, sh, 328);\r
+  if (HighCacheB[0]) DrawTilesFromCache(HighCacheB, sh, maxw);\r
   if (hvwind == 1)\r
     DrawWindow(0, maxcells>>1, 1, sh);\r
   else if (hvwind == 2) {\r
-    if(HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, (win&0x80) ? edge<<4 : 328);\r
+    if(HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, (win&0x80) ? edge<<4 : maxw);\r
     DrawWindow((win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 1, sh);\r
   } else\r
-    if (HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, 328);\r
-  if (HighCacheS[0]) DrawAllSpritesHiPri(HighCacheS, maxw, 1, sh);\r
+    if (HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, maxw);\r
+  if ((rendstatus & PDRAW_HAVE_HI_SPR) || sh)\r
+    DrawAllSpritesHiPri(1, sh);\r
 \r
 #if 0\r
   {\r
@@ -1337,23 +1356,34 @@ static int DrawDisplay(int sh, int as)
 PICO_INTERNAL void PicoFrameStart(void)\r
 {\r
   // prepare to do this frame\r
-  rendstatus = (PicoOpt&0x80)>>5;    // accurate sprites, clear everything else\r
+  int sh = Pico.video.reg[0xC] & 8; // shadow/hilight?\r
+  rendstatus = 0;\r
+  if (PicoOpt & POPT_ACC_SPRITES)\r
+    rendstatus |= PDRAW_ACC_SPRITES;\r
+\r
   if ((Pico.video.reg[12]&6) == 6) {\r
     rendstatus |= PDRAW_INTERLACE; // interlace mode\r
-    DrawAllSpritesLoPri = DrawAllSpritesInterlace;\r
+    DrawAllSpritesLoPri =\r
     DrawAllSpritesHiPri = DrawAllSpritesInterlace;\r
   }\r
-  else\r
+  else if (sh)\r
   {\r
     DrawAllSpritesLoPri = DrawAllSprites;\r
-    DrawAllSpritesHiPri = rendstatus ? DrawSpritesFromCacheAS : DrawSpritesFromCache;\r
+    DrawAllSpritesHiPri = DrawSpritesSHi;\r
   }\r
+  else\r
+  {\r
+    DrawAllSpritesLoPri =\r
+    DrawAllSpritesHiPri = DrawAllSprites;\r
+  }\r
+  if (rendstatus & PDRAW_ACC_SPRITES)\r
+    DrawAllSpritesHiPri = DrawSpritesHiAS;\r
 \r
   if (Pico.m.dirtyPal) Pico.m.dirtyPal = 2; // reset dirty if needed\r
 \r
+  DrawScanline=0;\r
   PrepareSprites(1);\r
   skip_next_line=0;\r
-  DrawScanline=0;\r
 }\r
 \r
 static void PicoLine(void)\r