fix DrawSpritesHiAS
[picodrive.git] / pico / draw.c
index 168a6ea..680de3d 100644 (file)
@@ -1,11 +1,11 @@
-// This is part of Pico Library\r
-\r
-// (c) Copyright 2004 Dave, All rights reserved.\r
-// (c) Copyright 2006-2008 notaz, All rights reserved.\r
-// Free for non-commercial use.\r
-\r
-// For commercial use, separate licencing terms must be obtained.\r
-\r
+/*\r
+ * line renderer\r
+ * (c) Copyright Dave, 2004\r
+ * (C) notaz, 2006-2010\r
+ *\r
+ * This work is licensed under the terms of MAME license.\r
+ * See COPYING file in the top-level directory.\r
+ */\r
 /*\r
  * The renderer has 4 modes now:\r
  * - normal\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
+ * s/h uses 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
+ * b \ mode: s/h             sonic\r
+ * 00        normal          pal index\r
+ * 01        shadow          pal index\r
+ * 10        hilight+op 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
@@ -35,18 +34,20 @@ int (*PicoScanBegin)(unsigned int num) = NULL;
 int (*PicoScanEnd)  (unsigned int num) = NULL;\r
 \r
 static unsigned char DefHighCol[8+320+8];\r
-unsigned char *HighCol = DefHighCol;\r
 static unsigned char *HighColBase = DefHighCol;\r
 static int HighColIncrement;\r
 \r
 static unsigned int DefOutBuff[320*2/2];\r
-void *DrawLineDest = DefOutBuff; // pointer to dest buffer where to draw this line to\r
 void *DrawLineDestBase = DefOutBuff;\r
 int DrawLineDestIncrement;\r
 \r
 static int  HighCacheA[41+1];   // caches for high layers\r
 static int  HighCacheB[41+1];\r
-int  HighPreSpr[80*2+1]; // slightly preprocessed sprites\r
+static int  HighPreSpr[80*2+1]; // slightly preprocessed sprites\r
+\r
+#define LF_PLANE_1 (1 << 0)\r
+#define LF_SH      (1 << 1) // must be = 2\r
+#define LF_FORCE   (1 << 2)\r
 \r
 #define SPRL_HAVE_HI     0x80 // have hi priority sprites\r
 #define SPRL_HAVE_LO     0x40 // *lo*\r
@@ -54,15 +55,11 @@ int  HighPreSpr[80*2+1]; // slightly preprocessed sprites
 #define SPRL_LO_ABOVE_HI 0x10 // low priority sprites may be on top of hi\r
 unsigned char HighLnSpr[240][3 + MAX_LINE_SPRITES]; // sprite_count, ^flags, tile_count, [spritep]...\r
 \r
-int rendstatus, rendstatus_old;\r
+int rendstatus_old;\r
 int rendlines;\r
-int DrawScanline;\r
-int PicoDrawMask = -1;\r
 \r
 static int skip_next_line=0;\r
 \r
-//unsigned short ppt[] = { 0x0f11, 0x0ff1, 0x01f1, 0x011f, 0x01ff, 0x0f1f, 0x0f0e, 0x0e7c };\r
-\r
 struct TileStrip\r
 {\r
   int nametab; // Position in VRAM of name table (for this tile line)\r
@@ -75,12 +72,15 @@ struct TileStrip
 \r
 // stuff available in asm:\r
 #ifdef _ASM_DRAW_C\r
-void DrawWindow(int tstart, int tend, int prio, 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
-void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells);\r
-void FinalizeLineBGR444(int sh, int line);\r
+void DrawWindow(int tstart, int tend, int prio, int sh,\r
+                struct PicoEState *est);\r
+void DrawAllSprites(unsigned char *sprited, int prio, int sh,\r
+                    struct PicoEState *est);\r
+void DrawTilesFromCache(int *hc, int sh, int rlim,\r
+                    struct PicoEState *est);\r
+void DrawSpritesSHi(unsigned char *sprited, struct PicoEState *est);\r
+void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells,\r
+               struct PicoEState *est);\r
 void *blockcpy(void *dst, const void *src, size_t n);\r
 void blockcpy_or(void *dst, void *src, size_t n, int pat);\r
 #else\r
@@ -94,59 +94,49 @@ void blockcpy_or(void *dst, void *src, size_t n, int pat)
 #define blockcpy memcpy\r
 #endif\r
 \r
-\r
-#define TileNormMaker(funcname,pix_func)                     \\r
-static int funcname(int sx,int addr,int pal)                 \\r
+#define TileNormMaker_(pix_func)                             \\r
 {                                                            \\r
-  unsigned char *pd = HighCol+sx;                            \\r
-  unsigned int pack=0; unsigned int t=0;                     \\r
+  unsigned int t;                                            \\r
                                                              \\r
-  pack=*(unsigned int *)(Pico.vram+addr); /* Get 8 pixels */ \\r
-  if (pack)                                                  \\r
-  {                                                          \\r
-    t=(pack&0x0000f000)>>12; pix_func(0);                    \\r
-    t=(pack&0x00000f00)>> 8; pix_func(1);                    \\r
-    t=(pack&0x000000f0)>> 4; pix_func(2);                    \\r
-    t=(pack&0x0000000f)    ; pix_func(3);                    \\r
-    t=(pack&0xf0000000)>>28; pix_func(4);                    \\r
-    t=(pack&0x0f000000)>>24; pix_func(5);                    \\r
-    t=(pack&0x00f00000)>>20; pix_func(6);                    \\r
-    t=(pack&0x000f0000)>>16; pix_func(7);                    \\r
-    return 0;                                                \\r
-  }                                                          \\r
-                                                             \\r
-  return 1; /* Tile blank */                                 \\r
+  t = (pack&0x0000f000)>>12; pix_func(0);                    \\r
+  t = (pack&0x00000f00)>> 8; pix_func(1);                    \\r
+  t = (pack&0x000000f0)>> 4; pix_func(2);                    \\r
+  t = (pack&0x0000000f)    ; pix_func(3);                    \\r
+  t = (pack&0xf0000000)>>28; pix_func(4);                    \\r
+  t = (pack&0x0f000000)>>24; pix_func(5);                    \\r
+  t = (pack&0x00f00000)>>20; pix_func(6);                    \\r
+  t = (pack&0x000f0000)>>16; pix_func(7);                    \\r
 }\r
 \r
-\r
-#define TileFlipMaker(funcname,pix_func)                     \\r
-static int funcname(int sx,int addr,int pal)                 \\r
+#define TileFlipMaker_(pix_func)                             \\r
 {                                                            \\r
-  unsigned char *pd = HighCol+sx;                            \\r
-  unsigned int pack=0; unsigned int t=0;                     \\r
-                                                             \\r
-  pack=*(unsigned int *)(Pico.vram+addr); /* Get 8 pixels */ \\r
-  if (pack)                                                  \\r
-  {                                                          \\r
-    t=(pack&0x000f0000)>>16; pix_func(0);                    \\r
-    t=(pack&0x00f00000)>>20; pix_func(1);                    \\r
-    t=(pack&0x0f000000)>>24; pix_func(2);                    \\r
-    t=(pack&0xf0000000)>>28; pix_func(3);                    \\r
-    t=(pack&0x0000000f)    ; pix_func(4);                    \\r
-    t=(pack&0x000000f0)>> 4; pix_func(5);                    \\r
-    t=(pack&0x00000f00)>> 8; pix_func(6);                    \\r
-    t=(pack&0x0000f000)>>12; pix_func(7);                    \\r
-    return 0;                                                \\r
-  }                                                          \\r
+  unsigned int t;                                            \\r
                                                              \\r
-  return 1; /* Tile blank */                                 \\r
+  t = (pack&0x000f0000)>>16; pix_func(0);                    \\r
+  t = (pack&0x00f00000)>>20; pix_func(1);                    \\r
+  t = (pack&0x0f000000)>>24; pix_func(2);                    \\r
+  t = (pack&0xf0000000)>>28; pix_func(3);                    \\r
+  t = (pack&0x0000000f)    ; pix_func(4);                    \\r
+  t = (pack&0x000000f0)>> 4; pix_func(5);                    \\r
+  t = (pack&0x00000f00)>> 8; pix_func(6);                    \\r
+  t = (pack&0x0000f000)>>12; pix_func(7);                    \\r
 }\r
 \r
+#define TileNormMaker(funcname, pix_func) \\r
+static void funcname(unsigned char *pd, unsigned int pack, int pal) \\r
+TileNormMaker_(pix_func)\r
 \r
-#ifdef _ASM_DRAW_C_AMIPS\r
-int TileNorm(int sx,int addr,int pal);\r
-int TileFlip(int sx,int addr,int pal);\r
-#else\r
+#define TileFlipMaker(funcname, pix_func) \\r
+static void funcname(unsigned char *pd, unsigned int pack, int pal) \\r
+TileFlipMaker_(pix_func)\r
+\r
+#define TileNormMakerAS(funcname, pix_func) \\r
+static void funcname(unsigned char *pd, unsigned char *mb, unsigned int pack, int pal) \\r
+TileNormMaker_(pix_func)\r
+\r
+#define TileFlipMakerAS(funcname, pix_func) \\r
+static void funcname(unsigned char *pd, unsigned char *mb, unsigned int pack, int pal) \\r
+TileFlipMaker_(pix_func)\r
 \r
 #define pix_just_write(x) \\r
   if (t) pd[x]=pal|t\r
@@ -154,8 +144,6 @@ int TileFlip(int sx,int addr,int pal);
 TileNormMaker(TileNorm,pix_just_write)\r
 TileFlipMaker(TileFlip,pix_just_write)\r
 \r
-#endif\r
-\r
 #ifndef _ASM_DRAW_C\r
 \r
 // draw a sprite pixel, process operator colors\r
@@ -176,11 +164,15 @@ TileFlipMaker(TileFlipSH, pix_sh)
 TileNormMaker(TileNormSH_markop, pix_sh_markop)\r
 TileFlipMaker(TileFlipSH_markop, pix_sh_markop)\r
 \r
+#endif\r
+\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]&0xc0)) \\r
     pd[x]=(pd[x]&0x3f)|(t<<6); /* c0 shadow, 80 hilight */ \\r
 \r
+#ifndef _ASM_DRAW_C\r
+\r
 TileNormMaker(TileNormSH_onlyop_lp, pix_sh_onlyop)\r
 TileFlipMaker(TileFlipSH_onlyop_lp, pix_sh_onlyop)\r
 \r
@@ -188,37 +180,57 @@ TileFlipMaker(TileFlipSH_onlyop_lp, pix_sh_onlyop)
 \r
 // draw a sprite pixel (AS)\r
 #define pix_as(x) \\r
-  if (t && !(pd[x]&0x80)) pd[x]=pal|t\r
+  if (t & mb[x]) mb[x] = 0, pd[x] = pal | t\r
 \r
-TileNormMaker(TileNormAS, pix_as)\r
-TileFlipMaker(TileFlipAS, pix_as)\r
+TileNormMakerAS(TileNormAS, pix_as)\r
+TileFlipMakerAS(TileFlipAS, pix_as)\r
 \r
-// draw a sprite pixel, skip operator colors (AS)\r
-#define pix_sh_as_noop(x) \\r
-  if (t && t < 0xe && !(pd[x]&0x80)) pd[x]=pal|t\r
+// draw a sprite pixel, process operator colors (AS)\r
+#define pix_sh_as(x) \\r
+  if (t & mb[x]) { \\r
+    mb[x] = 0; \\r
+    if (t>=0xe) pd[x]=(pd[x]&0x3f)|(t<<6); /* c0 shadow, 80 hilight */ \\r
+    else pd[x] = pal | t; \\r
+  }\r
 \r
-TileNormMaker(TileNormAS_noop, pix_sh_as_noop)\r
-TileFlipMaker(TileFlipAS_noop, pix_sh_as_noop)\r
+TileNormMakerAS(TileNormSH_AS, pix_sh_as)\r
+TileFlipMakerAS(TileFlipSH_AS, pix_sh_as)\r
+\r
+#define pix_sh_as_onlyop(x) \\r
+  if (t & mb[x]) { \\r
+    mb[x] = 0; \\r
+    pix_sh_onlyop(x); \\r
+  }\r
+\r
+TileNormMakerAS(TileNormSH_AS_onlyop_lp, pix_sh_as_onlyop)\r
+TileFlipMakerAS(TileFlipSH_AS_onlyop_lp, pix_sh_as_onlyop)\r
 \r
 // mark pixel as sprite pixel (AS)\r
 #define pix_sh_as_onlymark(x) \\r
-  if (t) pd[x]|=0x80\r
+  if (t) mb[x] = 0\r
 \r
-TileNormMaker(TileNormAS_onlymark, pix_sh_as_onlymark)\r
-TileFlipMaker(TileFlipAS_onlymark, pix_sh_as_onlymark)\r
+TileNormMakerAS(TileNormAS_onlymark, pix_sh_as_onlymark)\r
+TileFlipMakerAS(TileFlipAS_onlymark, pix_sh_as_onlymark)\r
 \r
+// forced both layer draw (through debug reg)\r
+#define pix_and(x) \\r
+  pd[x] = (pd[x] & 0xc0) | (pd[x] & (pal | t))\r
+\r
+TileNormMaker(TileNorm_and, pix_and)\r
+TileFlipMaker(TileFlip_and, pix_and)\r
 \r
 // --------------------------------------------\r
 \r
 #ifndef _ASM_DRAW_C\r
-static void DrawStrip(struct TileStrip *ts, int plane_sh, int cellskip)\r
+static void DrawStrip(struct TileStrip *ts, int lflags, int cellskip)\r
 {\r
+  unsigned char *pd = Pico.est.HighCol;\r
   int tilex,dx,ty,code=0,addr=0,cells;\r
   int oldcode=-1,blank=-1; // The tile we know is blank\r
   int pal=0,sh;\r
 \r
   // Draw tiles across screen:\r
-  sh=(plane_sh<<5)&0x40;\r
+  sh = (lflags & LF_SH) << 5; // 0x40\r
   tilex=((-ts->hscroll)>>3)+cellskip;\r
   ty=(ts->line&7)<<1; // Y-Offset into tile\r
   dx=((ts->hscroll-1)&7)+1;\r
@@ -226,13 +238,14 @@ static void DrawStrip(struct TileStrip *ts, int plane_sh, int cellskip)
   if(dx != 8) cells++; // have hscroll, need to draw 1 cell more\r
   dx+=cellskip<<3;\r
 \r
-  for (; cells > 0; dx+=8,tilex++,cells--)\r
+  for (; cells > 0; dx+=8, tilex++, cells--)\r
   {\r
-    int zero=0;\r
+    unsigned int pack;\r
 \r
-    code=Pico.vram[ts->nametab+(tilex&ts->xmask)];\r
-    if (code==blank) continue;\r
-    if (code>>15) { // high priority tile\r
+    code = PicoMem.vram[ts->nametab + (tilex & ts->xmask)];\r
+    if (code == blank)\r
+      continue;\r
+    if ((code >> 15) | (lflags & LF_FORCE)) { // high priority tile\r
       int cval = code | (dx<<16) | (ty<<25);\r
       if(code&0x1000) cval^=7<<26;\r
       *ts->hc++ = cval; // cache it\r
@@ -249,41 +262,51 @@ static void DrawStrip(struct TileStrip *ts, int plane_sh, int cellskip)
       pal=((code>>9)&0x30)|sh;\r
     }\r
 \r
-    if (code&0x0800) zero=TileFlip(dx,addr,pal);\r
-    else             zero=TileNorm(dx,addr,pal);\r
+    pack = *(unsigned int *)(PicoMem.vram + addr);\r
+    if (!pack) {\r
+      blank = code;\r
+      continue;\r
+    }\r
 \r
-    if (zero) blank=code; // We know this tile is blank now\r
+    if (code & 0x0800) TileFlip(pd + dx, pack, pal);\r
+    else               TileNorm(pd + dx, pack, pal);\r
   }\r
 \r
   // terminate the cache list\r
   *ts->hc = 0;\r
   // if oldcode wasn't changed, it means all layer is hi priority\r
-  if (oldcode == -1) rendstatus |= PDRAW_PLANE_HI_PRIO;\r
+  if (oldcode == -1) Pico.est.rendstatus |= PDRAW_PLANE_HI_PRIO;\r
 }\r
 \r
 // this is messy\r
-void DrawStripVSRam(struct TileStrip *ts, int plane_sh, int cellskip)\r
+static void DrawStripVSRam(struct TileStrip *ts, int plane_sh, int cellskip)\r
 {\r
+  unsigned char *pd = Pico.est.HighCol;\r
   int tilex,dx,code=0,addr=0,cell=0;\r
   int oldcode=-1,blank=-1; // The tile we know is blank\r
-  int pal=0,scan=DrawScanline;\r
+  int pal=0,scan=Pico.est.DrawScanline;\r
 \r
   // Draw tiles across screen:\r
   tilex=(-ts->hscroll)>>3;\r
   dx=((ts->hscroll-1)&7)+1;\r
-  if(dx != 8) cell--; // have hscroll, start with negative cell\r
+  if (ts->hscroll & 0x0f) {\r
+    int adj = ((ts->hscroll ^ dx) >> 3) & 1;\r
+    cell -= adj + 1;\r
+    ts->cells -= adj;\r
+  }\r
   cell+=cellskip;\r
   tilex+=cellskip;\r
   dx+=cellskip<<3;\r
 \r
   for (; cell < ts->cells; dx+=8,tilex++,cell++)\r
   {\r
-    int zero=0,nametabadd,ty;\r
+    int nametabadd, ty;\r
+    unsigned int pack;\r
 \r
     //if((cell&1)==0)\r
     {\r
       int line,vscroll;\r
-      vscroll=Pico.vsram[(plane_sh&1)+(cell&~1)];\r
+      vscroll=PicoMem.vsram[(plane_sh&1)+(cell&~1)];\r
 \r
       // Find the line in the name table\r
       line=(vscroll+scan)&ts->line&0xffff; // ts->line is really ymask ..\r
@@ -291,7 +314,7 @@ void DrawStripVSRam(struct TileStrip *ts, int plane_sh, int cellskip)
       ty=(line&7)<<1; // Y-Offset into tile\r
     }\r
 \r
-    code=Pico.vram[ts->nametab+nametabadd+(tilex&ts->xmask)];\r
+    code=PicoMem.vram[ts->nametab+nametabadd+(tilex&ts->xmask)];\r
     if (code==blank) continue;\r
     if (code>>15) { // high priority tile\r
       int cval = code | (dx<<16) | (ty<<25);\r
@@ -309,15 +332,19 @@ void DrawStripVSRam(struct TileStrip *ts, int plane_sh, int cellskip)
       pal=((code>>9)&0x30)|((plane_sh<<5)&0x40);\r
     }\r
 \r
-    if (code&0x0800) zero=TileFlip(dx,addr,pal);\r
-    else             zero=TileNorm(dx,addr,pal);\r
+    pack = *(unsigned int *)(PicoMem.vram + addr);\r
+    if (!pack) {\r
+      blank = code;\r
+      continue;\r
+    }\r
 \r
-    if (zero) blank=code; // We know this tile is blank now\r
+    if (code & 0x0800) TileFlip(pd + dx, pack, pal);\r
+    else               TileNorm(pd + dx, pack, pal);\r
   }\r
 \r
   // terminate the cache list\r
   *ts->hc = 0;\r
-  if (oldcode == -1) rendstatus |= PDRAW_PLANE_HI_PRIO;\r
+  if (oldcode == -1) Pico.est.rendstatus |= PDRAW_PLANE_HI_PRIO;\r
 }\r
 #endif\r
 \r
@@ -326,6 +353,7 @@ static
 #endif\r
 void DrawStripInterlace(struct TileStrip *ts)\r
 {\r
+  unsigned char *pd = Pico.est.HighCol;\r
   int tilex=0,dx=0,ty=0,code=0,addr=0,cells;\r
   int oldcode=-1,blank=-1; // The tile we know is blank\r
   int pal=0;\r
@@ -339,9 +367,9 @@ void DrawStripInterlace(struct TileStrip *ts)
 \r
   for (; cells; dx+=8,tilex++,cells--)\r
   {\r
-    int zero=0;\r
+    unsigned int pack;\r
 \r
-    code=Pico.vram[ts->nametab+(tilex&ts->xmask)];\r
+    code = PicoMem.vram[ts->nametab + (tilex & ts->xmask)];\r
     if (code==blank) continue;\r
     if (code>>15) { // high priority tile\r
       int cval = (code&0xfc00) | (dx<<16) | (ty<<25);\r
@@ -361,10 +389,14 @@ void DrawStripInterlace(struct TileStrip *ts)
       pal=((code>>9)&0x30);\r
     }\r
 \r
-    if (code&0x0800) zero=TileFlip(dx,addr,pal);\r
-    else             zero=TileNorm(dx,addr,pal);\r
+    pack = *(unsigned int *)(PicoMem.vram + addr);\r
+    if (!pack) {\r
+      blank = code;\r
+      continue;\r
+    }\r
 \r
-    if (zero) blank=code; // We know this tile is blank now\r
+    if (code & 0x0800) TileFlip(pd + dx, pack, pal);\r
+    else               TileNorm(pd + dx, pack, pal);\r
   }\r
 \r
   // terminate the cache list\r
@@ -374,7 +406,8 @@ void DrawStripInterlace(struct TileStrip *ts)
 // --------------------------------------------\r
 \r
 #ifndef _ASM_DRAW_C\r
-static void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells)\r
+static void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells,\r
+  struct PicoEState *est)\r
 {\r
   struct PicoVideo *pvid=&Pico.video;\r
   const char shift[4]={5,6,5,7}; // 32,64 or 128 sized tilemaps (2 is invalid)\r
@@ -393,27 +426,30 @@ static void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells)
 \r
   ts.xmask=(1<<shift[width])-1; // X Mask in tiles (0x1f-0x7f)\r
   ymask=(height<<8)|0xff;       // Y Mask in pixels\r
-  if(width == 1)   ymask&=0x1ff;\r
-  else if(width>1) ymask =0x0ff;\r
+  switch (width) {\r
+    case 1: ymask &= 0x1ff; break;\r
+    case 2: ymask =  0x007; break;\r
+    case 3: ymask =  0x0ff; break;\r
+  }\r
 \r
   // Find name table:\r
   if (plane_sh&1) ts.nametab=(pvid->reg[4]&0x07)<<12; // B\r
   else            ts.nametab=(pvid->reg[2]&0x38)<< 9; // A\r
 \r
   htab=pvid->reg[13]<<9; // Horizontal scroll table address\r
-  if ( pvid->reg[11]&2)     htab+=DrawScanline<<1; // Offset by line\r
+  if ( pvid->reg[11]&2)     htab+=est->DrawScanline<<1; // Offset by line\r
   if ((pvid->reg[11]&1)==0) htab&=~0xf; // Offset by tile\r
   htab+=plane_sh&1; // A or B\r
 \r
   // Get horizontal scroll value, will be masked later\r
-  ts.hscroll=Pico.vram[htab&0x7fff];\r
+  ts.hscroll = PicoMem.vram[htab & 0x7fff];\r
 \r
   if((pvid->reg[12]&6) == 6) {\r
     // interlace mode 2\r
-    vscroll=Pico.vsram[plane_sh&1]; // Get vertical scroll value\r
+    vscroll = PicoMem.vsram[plane_sh & 1]; // Get vertical scroll value\r
 \r
     // Find the line in the name table\r
-    ts.line=(vscroll+(DrawScanline<<1))&((ymask<<1)|1);\r
+    ts.line=(vscroll+(est->DrawScanline<<1))&((ymask<<1)|1);\r
     ts.nametab+=(ts.line>>4)<<shift[width];\r
 \r
     DrawStripInterlace(&ts);\r
@@ -423,10 +459,10 @@ static void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells)
     ts.line=ymask|(shift[width]<<24); // save some stuff instead of line\r
     DrawStripVSRam(&ts, plane_sh, cellskip);\r
   } else {\r
-    vscroll=Pico.vsram[plane_sh&1]; // Get vertical scroll value\r
+    vscroll = PicoMem.vsram[plane_sh & 1]; // Get vertical scroll value\r
 \r
     // Find the line in the name table\r
-    ts.line=(vscroll+DrawScanline)&ymask;\r
+    ts.line=(vscroll+est->DrawScanline)&ymask;\r
     ts.nametab+=(ts.line>>3)<<shift[width];\r
 \r
     DrawStrip(&ts, plane_sh, cellskip);\r
@@ -437,9 +473,11 @@ static void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells)
 // --------------------------------------------\r
 \r
 // tstart & tend are tile pair numbers\r
-static void DrawWindow(int tstart, int tend, int prio, int sh) // int *hcache\r
+static void DrawWindow(int tstart, int tend, int prio, int sh,\r
+                       struct PicoEState *est)\r
 {\r
-  struct PicoVideo *pvid=&Pico.video;\r
+  unsigned char *pd = Pico.est.HighCol;\r
+  struct PicoVideo *pvid = &Pico.video;\r
   int tilex,ty,nametab,code=0;\r
   int blank=-1; // The tile we know is blank\r
 \r
@@ -447,71 +485,78 @@ static void DrawWindow(int tstart, int tend, int prio, int sh) // int *hcache
   if (pvid->reg[12]&1)\r
   {\r
     nametab=(pvid->reg[3]&0x3c)<<9; // 40-cell mode\r
-    nametab+=(DrawScanline>>3)<<6;\r
+    nametab+=(est->DrawScanline>>3)<<6;\r
   }\r
   else\r
   {\r
     nametab=(pvid->reg[3]&0x3e)<<9; // 32-cell mode\r
-    nametab+=(DrawScanline>>3)<<5;\r
+    nametab+=(est->DrawScanline>>3)<<5;\r
   }\r
 \r
   tilex=tstart<<1;\r
 \r
-  if (!(rendstatus & PDRAW_WND_DIFF_PRIO)) {\r
+  if (!(est->rendstatus & PDRAW_WND_DIFF_PRIO)) {\r
     // check the first tile code\r
-    code=Pico.vram[nametab+tilex];\r
+    code = PicoMem.vram[nametab + tilex];\r
     // if the whole window uses same priority (what is often the case), we may be able to skip this field\r
     if ((code>>15) != prio) return;\r
   }\r
 \r
   tend<<=1;\r
-  ty=(DrawScanline&7)<<1; // Y-Offset into tile\r
+  ty=(est->DrawScanline&7)<<1; // Y-Offset into tile\r
 \r
   // Draw tiles across screen:\r
   if (!sh)\r
   {\r
     for (; tilex < tend; tilex++)\r
     {\r
-      int addr=0,zero=0;\r
+      unsigned int pack;\r
+      int dx, addr;\r
       int pal;\r
 \r
-      code=Pico.vram[nametab+tilex];\r
+      code = PicoMem.vram[nametab + tilex];\r
       if (code==blank) continue;\r
       if ((code>>15) != prio) {\r
-        rendstatus |= PDRAW_WND_DIFF_PRIO;\r
+        est->rendstatus |= PDRAW_WND_DIFF_PRIO;\r
         continue;\r
       }\r
 \r
-      pal=((code>>9)&0x30);\r
-\r
       // Get tile address/2:\r
       addr=(code&0x7ff)<<4;\r
       if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip\r
 \r
-      if (code&0x0800) zero=TileFlip(8+(tilex<<3),addr,pal);\r
-      else             zero=TileNorm(8+(tilex<<3),addr,pal);\r
+      pack = *(unsigned int *)(PicoMem.vram + addr);\r
+      if (!pack) {\r
+        blank = code;\r
+        continue;\r
+      }\r
 \r
-      if (zero) blank=code; // We know this tile is blank now\r
+      pal = ((code >> 9) & 0x30);\r
+      dx = 8 + (tilex << 3);\r
+\r
+      if (code & 0x0800) TileFlip(pd + dx, pack, pal);\r
+      else               TileNorm(pd + dx, pack, pal);\r
     }\r
   }\r
   else\r
   {\r
     for (; tilex < tend; tilex++)\r
     {\r
-      int addr=0,zero=0;\r
+      unsigned int pack;\r
+      int dx, addr;\r
       int pal;\r
 \r
-      code=Pico.vram[nametab+tilex];\r
+      code = PicoMem.vram[nametab + tilex];\r
       if(code==blank) continue;\r
       if((code>>15) != prio) {\r
-        rendstatus |= PDRAW_WND_DIFF_PRIO;\r
+        est->rendstatus |= PDRAW_WND_DIFF_PRIO;\r
         continue;\r
       }\r
 \r
       pal=((code>>9)&0x30);\r
 \r
       if (prio) {\r
-        int *zb = (int *)(HighCol+8+(tilex<<3));\r
+        int *zb = (int *)(est->HighCol+8+(tilex<<3));\r
         *zb++ &= 0xbfbfbfbf;\r
         *zb   &= 0xbfbfbfbf;\r
       } else {\r
@@ -522,10 +567,16 @@ static void DrawWindow(int tstart, int tend, int prio, int sh) // int *hcache
       addr=(code&0x7ff)<<4;\r
       if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip\r
 \r
-      if (code&0x0800) zero=TileFlip(8+(tilex<<3),addr,pal);\r
-      else             zero=TileNorm(8+(tilex<<3),addr,pal);\r
+      pack = *(unsigned int *)(PicoMem.vram + addr);\r
+      if (!pack) {\r
+        blank = code;\r
+        continue;\r
+      }\r
+\r
+      dx = 8 + (tilex << 3);\r
 \r
-      if (zero) blank=code; // We know this tile is blank now\r
+      if (code & 0x0800) TileFlip(pd + dx, pack, pal);\r
+      else               TileNorm(pd + dx, pack, pal);\r
     }\r
   }\r
 }\r
@@ -537,24 +588,26 @@ static void DrawTilesFromCacheShPrep(void)
   // 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
   // but leave lo pri op sprite markers alone\r
-  int c = 320/4, *zb = (int *)(HighCol+8);\r
-  rendstatus |= PDRAW_SHHI_DONE;\r
+  int c = 320/4, *zb = (int *)(Pico.est.HighCol+8);\r
+  Pico.est.rendstatus |= PDRAW_SHHI_DONE;\r
   while (c--)\r
   {\r
     *zb++ &= 0xbfbfbfbf;\r
   }\r
 }\r
 \r
-static void DrawTilesFromCache(int *hc, int sh, int rlim)\r
+static void DrawTilesFromCache(int *hc, int sh, int rlim, struct PicoEState *est)\r
 {\r
+  unsigned char *pd = Pico.est.HighCol;\r
   int code, addr, dx;\r
+  unsigned int pack;\r
   int pal;\r
 \r
   // *ts->hc++ = code | (dx<<16) | (ty<<25); // cache it\r
 \r
-  if (sh && (rendstatus & (PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO)))\r
+  if (sh && (est->rendstatus & (PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO)))\r
   {\r
-    if (!(rendstatus & PDRAW_SHHI_DONE))\r
+    if (!(est->rendstatus & PDRAW_SHHI_DONE))\r
       DrawTilesFromCacheShPrep();\r
     sh = 0;\r
   }\r
@@ -563,48 +616,60 @@ static void DrawTilesFromCache(int *hc, int sh, int rlim)
   {\r
     short blank=-1; // The tile we know is blank\r
     while ((code=*hc++)) {\r
-      int zero;\r
-      if((short)code == blank) continue;\r
+      if (!(code & 0x8000) || (short)code == blank)\r
+        continue;\r
       // Get tile address/2:\r
-      addr=(code&0x7ff)<<4;\r
-      addr+=(unsigned int)code>>25; // y offset into tile\r
-      dx=(code>>16)&0x1ff;\r
+      addr = (code & 0x7ff) << 4;\r
+      addr += code >> 25; // y offset into tile\r
 \r
-      pal=((code>>9)&0x30);\r
-      if (rlim-dx < 0) goto last_cut_tile;\r
+      pack = *(unsigned int *)(PicoMem.vram + addr);\r
+      if (!pack) {\r
+        blank = (short)code;\r
+        continue;\r
+      }\r
 \r
-      if (code&0x0800) zero=TileFlip(dx,addr,pal);\r
-      else             zero=TileNorm(dx,addr,pal);\r
+      dx = (code >> 16) & 0x1ff;\r
+      pal = ((code >> 9) & 0x30);\r
+      if (rlim-dx < 0)\r
+        goto last_cut_tile;\r
 \r
-      if (zero) blank=(short)code;\r
+      if (code & 0x0800) TileFlip(pd + dx, pack, pal);\r
+      else               TileNorm(pd + dx, pack, pal);\r
     }\r
   }\r
   else\r
   {\r
     while ((code=*hc++)) {\r
       unsigned char *zb;\r
+\r
       // Get tile address/2:\r
       addr=(code&0x7ff)<<4;\r
       addr+=(unsigned int)code>>25; // y offset into tile\r
       dx=(code>>16)&0x1ff;\r
-      zb = HighCol+dx;\r
+      zb = est->HighCol+dx;\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
+      pack = *(unsigned int *)(PicoMem.vram + addr);\r
+      if (!pack)\r
+        continue;\r
 \r
-      if (code&0x0800) TileFlip(dx,addr,pal);\r
-      else             TileNorm(dx,addr,pal);\r
+      pal = ((code >> 9) & 0x30);\r
+      if (rlim - dx < 0)\r
+        goto last_cut_tile;\r
+\r
+      if (code & 0x0800) TileFlip(pd + dx, pack, pal);\r
+      else               TileNorm(pd + dx, pack, pal);\r
     }\r
   }\r
   return;\r
 \r
 last_cut_tile:\r
+  // for vertical window cutoff\r
   {\r
-    unsigned int t, pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels\r
-    unsigned char *pd = HighCol+dx;\r
-    if (!pack) return;\r
+    unsigned int t;\r
+\r
+    pd += dx;\r
     if (code&0x0800)\r
     {\r
       switch (rlim-dx+8)\r
@@ -643,12 +708,13 @@ last_cut_tile:
 \r
 static void DrawSprite(int *sprite, int sh)\r
 {\r
+  void (*fTileFunc)(unsigned char *pd, unsigned int pack, int pal);\r
+  unsigned char *pd = Pico.est.HighCol;\r
   int width=0,height=0;\r
   int row=0,code=0;\r
   int pal;\r
   int tile=0,delta=0;\r
   int sx, sy;\r
-  int (*fTileFunc)(int sx,int addr,int pal);\r
 \r
   // parse the sprite data\r
   sy=sprite[0];\r
@@ -658,7 +724,7 @@ static void DrawSprite(int *sprite, int sh)
   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
+  row=Pico.est.DrawScanline-sy; // Row of the sprite we are on\r
 \r
   if (code&0x1000) row=(height<<3)-1-row; // Flip Y\r
 \r
@@ -682,17 +748,42 @@ static void DrawSprite(int *sprite, int sh)
 \r
   for (; width; width--,sx+=8,tile+=delta)\r
   {\r
+    unsigned int pack;\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
+    pack = *(unsigned int *)(PicoMem.vram + (tile & 0x7fff));\r
+    fTileFunc(pd + sx, pack, pal);\r
   }\r
 }\r
 #endif\r
 \r
+static NOINLINE void DrawTilesFromCacheForced(const int *hc)\r
+{\r
+  unsigned char *pd = Pico.est.HighCol;\r
+  int code, addr, dx;\r
+  unsigned int pack;\r
+  int pal;\r
+\r
+  // *ts->hc++ = code | (dx<<16) | (ty<<25);\r
+  while ((code = *hc++)) {\r
+    // Get tile address/2:\r
+    addr = (code & 0x7ff) << 4;\r
+    addr += (code >> 25) & 0x0e; // y offset into tile\r
+\r
+    dx = (code >> 16) & 0x1ff;\r
+    pal = ((code >> 9) & 0x30);\r
+    pack = *(unsigned int *)(PicoMem.vram + addr);\r
+\r
+    if (code & 0x0800) TileFlip_and(pd + dx, pack, pal);\r
+    else               TileNorm_and(pd + dx, pack, pal);\r
+  }\r
+}\r
+\r
 static void DrawSpriteInterlace(unsigned int *sprite)\r
 {\r
+  unsigned char *pd = Pico.est.HighCol;\r
   int width=0,height=0;\r
   int row=0,code=0;\r
   int pal;\r
@@ -706,7 +797,7 @@ static void DrawSpriteInterlace(unsigned int *sprite)
   width=(height>>2)&3; height&=3;\r
   width++; height++; // Width and height in tiles\r
 \r
-  row=(DrawScanline<<1)-sy; // Row of the sprite we are on\r
+  row=(Pico.est.DrawScanline<<1)-sy; // Row of the sprite we are on\r
 \r
   code=sprite[1];\r
   sx=((code>>16)&0x1ff)-0x78; // X\r
@@ -725,20 +816,22 @@ static void DrawSpriteInterlace(unsigned int *sprite)
 \r
   for (; width; width--,sx+=8,tile+=delta)\r
   {\r
+    unsigned int pack;\r
+\r
     if(sx<=0)   continue;\r
     if(sx>=328) break; // Offscreen\r
 \r
-    tile&=0x7fff; // Clip tile address\r
-    if (code&0x0800) TileFlip(sx,tile,pal);\r
-    else             TileNorm(sx,tile,pal);\r
+    pack = *(unsigned int *)(PicoMem.vram + (tile & 0x7fff));\r
+    if (code & 0x0800) TileFlip(pd + sx, pack, pal);\r
+    else               TileNorm(pd + sx, pack, pal);\r
   }\r
 }\r
 \r
 \r
-static void DrawAllSpritesInterlace(int pri, int sh)\r
+static NOINLINE void DrawAllSpritesInterlace(int pri, int sh)\r
 {\r
   struct PicoVideo *pvid=&Pico.video;\r
-  int i,u,table,link=0,sline=DrawScanline<<1;\r
+  int i,u,table,link=0,sline=Pico.est.DrawScanline<<1;\r
   unsigned int *sprites[80]; // Sprite index\r
 \r
   table=pvid->reg[5]&0x7f;\r
@@ -750,7 +843,7 @@ static void DrawAllSpritesInterlace(int pri, int sh)
     unsigned int *sprite;\r
     int code, sx, sy, height;\r
 \r
-    sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
+    sprite=(unsigned int *)(PicoMem.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
 \r
     // get sprite info\r
     code = sprite[0];\r
@@ -790,9 +883,10 @@ static void DrawAllSpritesInterlace(int pri, int sh)
  * 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
+static void DrawSpritesSHi(unsigned char *sprited, const struct PicoEState *est)\r
 {\r
-  int (*fTileFunc)(int sx,int addr,int pal);\r
+  void (*fTileFunc)(unsigned char *pd, unsigned int pack, int pal);\r
+  unsigned char *pd = Pico.est.HighCol;\r
   unsigned char *p;\r
   int cnt;\r
 \r
@@ -808,7 +902,7 @@ static void DrawSpritesSHi(unsigned char *sprited)
     int offs, delta, width, height, row;\r
 \r
     offs = (p[cnt] & 0x7f) * 2;\r
-    sprite = HighPreSpr + offs;\r
+    sprite = est->HighPreSpr + offs;\r
     code = sprite[1];\r
     pal = (code>>9)&0x30;\r
 \r
@@ -835,7 +929,7 @@ static void DrawSpritesSHi(unsigned char *sprited)
     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
+    row=est->DrawScanline-sy; // Row of the sprite we are on\r
 \r
     if (code&0x1000) row=(height<<3)-1-row; // Flip Y\r
 \r
@@ -848,11 +942,13 @@ static void DrawSpritesSHi(unsigned char *sprited)
 \r
     for (; width; width--,sx+=8,tile+=delta)\r
     {\r
+      unsigned int pack;\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
+      pack = *(unsigned int *)(PicoMem.vram + (tile & 0x7fff));\r
+      fTileFunc(pd + sx, pack, pal);\r
     }\r
   }\r
 }\r
@@ -860,15 +956,17 @@ static void DrawSpritesSHi(unsigned char *sprited)
 \r
 static void DrawSpritesHiAS(unsigned char *sprited, int sh)\r
 {\r
-  int (*fTileFunc)(int sx,int addr,int pal);\r
+  void (*fTileFunc)(unsigned char *pd, unsigned char *mb,\r
+                    unsigned int pack, int pal);\r
+  unsigned char *pd = Pico.est.HighCol;\r
+  unsigned char mb[8+320+8];\r
   unsigned char *p;\r
-  int entry, cnt, sh_cnt = 0;\r
+  int entry, cnt;\r
 \r
   cnt = sprited[0] & 0x7f;\r
   if (cnt == 0) return;\r
 \r
-  rendstatus |= PDRAW_SPR_LO_ON_HI;\r
-\r
+  memset(mb, 0xff, sizeof(mb));\r
   p = &sprited[3];\r
 \r
   // Go through sprites:\r
@@ -882,22 +980,26 @@ static void DrawSpritesHiAS(unsigned char *sprited, int sh)
     code = sprite[1];\r
     pal = (code>>9)&0x30;\r
 \r
-    if (code & 0x8000) // hi priority\r
+    if (sh && pal == 0x30)\r
     {\r
-      if (sh && pal == 0x30)\r
+      if (code & 0x8000) // hi priority\r
       {\r
-        if (code&0x800) fTileFunc=TileFlipAS_noop;\r
-        else            fTileFunc=TileNormAS_noop;\r
+        if (code&0x800) fTileFunc = TileFlipSH_AS;\r
+        else            fTileFunc = TileNormSH_AS;\r
       } else {\r
-        if (code&0x800) fTileFunc=TileFlipAS;\r
-        else            fTileFunc=TileNormAS;\r
+        if (code&0x800) fTileFunc = TileFlipSH_AS_onlyop_lp;\r
+        else            fTileFunc = TileNormSH_AS_onlyop_lp;\r
       }\r
     } else {\r
-      if (code&0x800) fTileFunc=TileFlipAS_onlymark;\r
-      else            fTileFunc=TileNormAS_onlymark;\r
+      if (code & 0x8000) // hi priority\r
+      {\r
+        if (code&0x800) fTileFunc = TileFlipAS;\r
+        else            fTileFunc = TileNormAS;\r
+      } else {\r
+        if (code&0x800) fTileFunc = TileFlipAS_onlymark;\r
+        else            fTileFunc = TileNormAS_onlymark;\r
+      }\r
     }\r
-    if (sh && pal == 0x30)\r
-      p[sh_cnt++] = offs / 2; // re-save for sh/hi pass\r
 \r
     // parse remaining sprite data\r
     sy=sprite[0];\r
@@ -906,7 +1008,7 @@ static void DrawSpritesHiAS(unsigned char *sprited, int sh)
     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
+    row=Pico.est.DrawScanline-sy; // Row of the sprite we are on\r
 \r
     if (code&0x1000) row=(height<<3)-1-row; // Flip Y\r
 \r
@@ -917,32 +1019,17 @@ static void DrawSpritesHiAS(unsigned char *sprited, int sh)
     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
     {\r
+      unsigned int pack;\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
+      pack = *(unsigned int *)(PicoMem.vram + (tile & 0x7fff));\r
+      fTileFunc(pd + sx, mb + sx, pack, pal);\r
     }\r
   }\r
-\r
-  if (!sh || !(sprited[1]&SPRL_MAY_HAVE_OP)) return;\r
-\r
-  /* nasty 1: remove 'sprite' flags */\r
-  {\r
-    int c = 320/4/4, *zb = (int *)(HighCol+8);\r
-    while (c--)\r
-    {\r
-      *zb++ &= 0x7f7f7f7f; *zb++ &= 0x7f7f7f7f;\r
-      *zb++ &= 0x7f7f7f7f; *zb++ &= 0x7f7f7f7f;\r
-    }\r
-  }\r
-\r
-  /* nasty 2: sh operator pass */\r
-  sprited[0] = sh_cnt;\r
-  DrawSpritesSHi(sprited);\r
 }\r
 \r
 \r
@@ -952,9 +1039,10 @@ static void DrawSpritesHiAS(unsigned char *sprited, int sh)
 // Index + 0  :    hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size\r
 // Index + 4  :    xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8\r
 \r
-void PrepareSprites(int full)\r
+static NOINLINE void PrepareSprites(int full)\r
 {\r
-  struct PicoVideo *pvid=&Pico.video;\r
+  const struct PicoVideo *pvid=&Pico.video;\r
+  const struct PicoEState *est=&Pico.est;\r
   int u,link=0,sh;\r
   int table=0;\r
   int *pd = HighPreSpr;\r
@@ -963,7 +1051,7 @@ void PrepareSprites(int full)
 \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
+  if (PicoIn.opt & POPT_DIS_SPRITE_LIM)\r
     max_line_sprites = MAX_LINE_SPRITES;\r
 \r
   if (pvid->reg[1]&8) max_lines = 240;\r
@@ -982,7 +1070,7 @@ void PrepareSprites(int full)
       unsigned int *sprite;\r
       int code2, sx, sy, height;\r
 \r
-      sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
+      sprite=(unsigned int *)(PicoMem.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
 \r
       // parse sprite info\r
       code2 = sprite[1];\r
@@ -991,10 +1079,11 @@ void PrepareSprites(int full)
       sy = (pack << 16) >> 16;\r
       height = (pack >> 24) & 0xf;\r
 \r
-      if (sy < max_lines && sy + (height<<3) > DrawScanline && // sprite onscreen (y)?\r
+      if (sy < max_lines &&\r
+         sy + (height<<3) > est->DrawScanline && // sprite onscreen (y)?\r
           (sx > -24 || sx < max_width))                   // onscreen x\r
       {\r
-        int y = (sy >= DrawScanline) ? sy : DrawScanline;\r
+        int y = (sy >= est->DrawScanline) ? sy : est->DrawScanline;\r
         int entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80);\r
         for (; y < sy + (height<<3) && y < max_lines; y++)\r
         {\r
@@ -1034,7 +1123,7 @@ found:;
       unsigned int *sprite;\r
       int code, code2, sx, sy, hv, height, width;\r
 \r
-      sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
+      sprite=(unsigned int *)(PicoMem.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
 \r
       // parse sprite info\r
       code = sprite[0];\r
@@ -1047,7 +1136,7 @@ found:;
       sx = (code2>>16)&0x1ff;\r
       sx -= 0x78; // Get X coordinate + 8\r
 \r
-      if (sy < max_lines && sy + (height<<3) > DrawScanline) // sprite onscreen (y)?\r
+      if (sy < max_lines && sy + (height<<3) > est->DrawScanline) // sprite onscreen (y)?\r
       {\r
         int entry, y, sx_min, onscr_x, maybe_op = 0;\r
 \r
@@ -1057,7 +1146,7 @@ found:;
           maybe_op = SPRL_MAY_HAVE_OP;\r
 \r
         entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80);\r
-        y = (sy >= DrawScanline) ? sy : DrawScanline;\r
+        y = (sy >= est->DrawScanline) ? sy : est->DrawScanline;\r
         for (; y < sy + (height<<3) && y < max_lines; y++)\r
         {\r
          unsigned char *p = &HighLnSpr[y][0];\r
@@ -1110,18 +1199,12 @@ found:;
 }\r
 \r
 #ifndef _ASM_DRAW_C\r
-static void DrawAllSprites(unsigned char *sprited, int prio, int sh)\r
+static void DrawAllSprites(unsigned char *sprited, int prio, int sh,\r
+                           struct PicoEState *est)\r
 {\r
-  int rs = rendstatus;\r
   unsigned char *p;\r
   int cnt;\r
 \r
-  if (rs & (PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES)) {\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
-\r
   cnt = sprited[0] & 0x7f;\r
   if (cnt == 0) return;\r
 \r
@@ -1140,7 +1223,7 @@ static void DrawAllSprites(unsigned char *sprited, int prio, int sh)
 \r
 // --------------------------------------------\r
 \r
-void BackFill(int reg7, int sh)\r
+void BackFill(int reg7, int sh, struct PicoEState *est)\r
 {\r
   unsigned int back;\r
 \r
@@ -1150,119 +1233,78 @@ void BackFill(int reg7, int sh)
   back|=back<<8;\r
   back|=back<<16;\r
 \r
-  memset32((int *)(HighCol+8), back, 320/4);\r
+  memset32((int *)(est->HighCol+8), back, 320/4);\r
 }\r
 #endif\r
 \r
 // --------------------------------------------\r
 \r
-unsigned short HighPal[0x100];\r
-\r
 #ifndef _ASM_DRAW_C\r
-void PicoDoHighPal555(int sh)\r
+void PicoDoHighPal555(int sh, int line, struct PicoEState *est)\r
 {\r
   unsigned int *spal, *dpal;\r
-  unsigned short *pal=HighPal;\r
-  int i, t;\r
+  unsigned int t, i;\r
 \r
   Pico.m.dirtyPal = 0;\r
 \r
-  spal = (void *)Pico.cram;\r
-  dpal = (void *)HighPal;\r
+  spal = (void *)PicoMem.cram;\r
+  dpal = (void *)est->HighPal;\r
 \r
-  for (i = 0; i < 0x40; i++) {\r
-    unsigned int t = spal[i];\r
+  for (i = 0; i < 0x40 / 2; i++) {\r
+    t = spal[i];\r
 #ifdef USE_BGR555\r
     t = ((t & 0x000e000e)<< 1) | ((t & 0x00e000e0)<<3) | ((t & 0x0e000e00)<<4);\r
 #else\r
     t = ((t & 0x000e000e)<<12) | ((t & 0x00e000e0)<<3) | ((t & 0x0e000e00)>>7);\r
 #endif\r
-    t |= (t >> 3) & 0x18e318e3;\r
+    // treat it like it was 4-bit per channel, since in s/h mode it somewhat is that.\r
+    // otherwise intensity difference between this and s/h will be wrong\r
+    t |= (t >> 4) & 0x08610861; // 0x18e318e3\r
     dpal[i] = t;\r
   }\r
 \r
+  // norm: xxx0, sh: 0xxx, hi: 0xxx + 7\r
   if (sh)\r
   {\r
     // shadowed pixels\r
-    for (i = 0x3f; i >= 0; i--)\r
-      pal[0x40|i] = pal[0xc0|i] = (unsigned short)((pal[i]>>1)&0x738e);\r
+    for (i = 0; i < 0x40 / 2; i++)\r
+      dpal[0x40/2 | i] = dpal[0xc0/2 | i] = (dpal[i] >> 1) & 0x738e738e;\r
     // hilighted pixels\r
-    for (i = 0x3f; i >= 0; i--) {\r
-      t=pal[i]&0xe71c;t+=0x4208;if(t&0x20)t|=0x1c;if(t&0x800)t|=0x700;if(t&0x10000)t|=0xe000;t&=0xe71c;\r
-      pal[0x80|i]=(unsigned short)t;\r
-    }\r
-  }\r
-}\r
-\r
-#if 0\r
-static void FinalizeLineBGR444(int sh, int line)\r
-{\r
-  unsigned short *pd=DrawLineDest;\r
-  unsigned char  *ps=HighCol+8;\r
-  unsigned short *pal=Pico.cram;\r
-  int len, i, t, mask=0xff;\r
-\r
-  if (Pico.video.reg[12]&1) {\r
-    len = 320;\r
-  } else {\r
-    if(!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32;\r
-    len = 256;\r
-  }\r
-\r
-  if(sh) {\r
-    pal=HighPal;\r
-    if(Pico.m.dirtyPal) {\r
-      blockcpy(pal, Pico.cram, 0x40*2);\r
-      // shadowed pixels\r
-      for(i = 0x3f; i >= 0; i--)\r
-        pal[0x40|i] = pal[0xc0|i] = (unsigned short)((pal[i]>>1)&0x0777);\r
-      // hilighted pixels\r
-      for(i = 0x3f; i >= 0; i--) {\r
-        t=pal[i]&0xeee;t+=0x444;if(t&0x10)t|=0xe;if(t&0x100)t|=0xe0;if(t&0x1000)t|=0xe00;t&=0xeee;\r
-        pal[0x80|i]=(unsigned short)t;\r
-      }\r
-      Pico.m.dirtyPal = 0;\r
+    for (i = 0; i < 0x40 / 2; i++) {\r
+      t = ((dpal[i] >> 1) & 0x738e738e) + 0x738e738e; // 0x7bef7bef;\r
+      t |= (t >> 4) & 0x08610861;\r
+      dpal[0x80/2 | i] = t;\r
     }\r
   }\r
-\r
-  if (!sh && (rendstatus & PDRAW_SPR_LO_ON_HI))\r
-    mask=0x3f; // accurate sprites\r
-\r
-  for(i = 0; i < len; i++)\r
-    pd[i] = pal[ps[i] & mask];\r
 }\r
-#endif\r
 \r
-\r
-void FinalizeLine555(int sh, int line)\r
+void FinalizeLine555(int sh, int line, struct PicoEState *est)\r
 {\r
-  unsigned short *pd=DrawLineDest;\r
-  unsigned char  *ps=HighCol+8;\r
-  unsigned short *pal=HighPal;\r
+  unsigned short *pd=est->DrawLineDest;\r
+  unsigned char  *ps=est->HighCol+8;\r
+  unsigned short *pal=est->HighPal;\r
   int len;\r
 \r
   if (Pico.m.dirtyPal)\r
-    PicoDoHighPal555(sh);\r
+    PicoDoHighPal555(sh, line, est);\r
 \r
   if (Pico.video.reg[12]&1) {\r
     len = 320;\r
   } else {\r
-    if (!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32;\r
+    if (!(PicoIn.opt&POPT_DIS_32C_BORDER)) pd+=32;\r
     len = 256;\r
   }\r
 \r
   {\r
-#ifndef PSP\r
-    int i, mask=0xff;\r
-    if (!sh && (rendstatus & PDRAW_SPR_LO_ON_HI))\r
-      mask=0x3f; // accurate sprites, upper bits are priority stuff\r
+#if 1\r
+    int i;\r
 \r
     for (i = 0; i < len; i++)\r
-      pd[i] = pal[ps[i] & mask];\r
+      pd[i] = pal[ps[i]];\r
 #else\r
     extern void amips_clut(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);\r
     extern void amips_clut_6bit(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);\r
-    if (!sh && (rendstatus & PDRAW_SPR_LO_ON_HI))\r
+    if (!sh)\r
          amips_clut_6bit(pd, ps, pal, len);\r
     else amips_clut(pd, ps, pal, len);\r
 #endif\r
@@ -1270,10 +1312,10 @@ void FinalizeLine555(int sh, int line)
 }\r
 #endif\r
 \r
-static void FinalizeLine8bit(int sh, int line)\r
+static void FinalizeLine8bit(int sh, int line, struct PicoEState *est)\r
 {\r
-  unsigned char *pd = DrawLineDest;\r
-  int len, rs = rendstatus;\r
+  unsigned char *pd = est->DrawLineDest;\r
+  int len, rs = est->rendstatus;\r
   static int dirty_count;\r
 \r
   if (!sh && Pico.m.dirtyPal == 1)\r
@@ -1283,45 +1325,52 @@ static void FinalizeLine8bit(int sh, int line)
          dirty_count = 1;\r
     else dirty_count++;\r
     rs |= PDRAW_SONIC_MODE;\r
-    rendstatus = rs;\r
+    est->rendstatus = rs;\r
     if (dirty_count == 3) {\r
-      blockcpy(HighPal, Pico.cram, 0x40*2);\r
+      blockcpy(est->HighPal, PicoMem.cram, 0x40*2);\r
     } else if (dirty_count == 11) {\r
-      blockcpy(HighPal+0x40, Pico.cram, 0x40*2);\r
+      blockcpy(est->HighPal+0x40, PicoMem.cram, 0x40*2);\r
     }\r
   }\r
 \r
   if (Pico.video.reg[12]&1) {\r
     len = 320;\r
   } else {\r
-    if (!(PicoOpt & POPT_DIS_32C_BORDER))\r
+    if (!(PicoIn.opt & POPT_DIS_32C_BORDER))\r
       pd += 32;\r
     len = 256;\r
   }\r
 \r
   if (!sh && (rs & PDRAW_SONIC_MODE)) {\r
     if (dirty_count >= 11) {\r
-      blockcpy_or(pd, HighCol+8, len, 0x80);\r
+      blockcpy_or(pd, est->HighCol+8, len, 0x80);\r
     } else {\r
-      blockcpy_or(pd, HighCol+8, len, 0x40);\r
+      blockcpy_or(pd, est->HighCol+8, len, 0x40);\r
     }\r
   } else {\r
-    blockcpy(pd, HighCol+8, len);\r
+    blockcpy(pd, est->HighCol+8, len);\r
   }\r
 }\r
 \r
-static void (*FinalizeLine)(int sh, int line);\r
+static void (*FinalizeLine)(int sh, int line, struct PicoEState *est);\r
 \r
 // --------------------------------------------\r
 \r
 static int DrawDisplay(int sh)\r
 {\r
-  unsigned char *sprited = &HighLnSpr[DrawScanline][0];\r
+  struct PicoEState *est=&Pico.est;\r
+  unsigned char *sprited = &HighLnSpr[est->DrawScanline][0];\r
   struct PicoVideo *pvid=&Pico.video;\r
-  int win=0,edge=0,hvwind=0;\r
-  int maxw,maxcells;\r
+  int win=0, edge=0, hvwind=0, lflags;\r
+  int maxw, maxcells;\r
 \r
-  rendstatus &= ~(PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO);\r
+  if (est->rendstatus & (PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES)) {\r
+    // elprintf(EL_STATUS, "PrepareSprites(%i)", (est->rendstatus>>4)&1);\r
+    PrepareSprites(est->rendstatus & PDRAW_DIRTY_SPRITES);\r
+    est->rendstatus &= ~(PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES);\r
+  }\r
+\r
+  est->rendstatus &= ~(PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO);\r
 \r
   if (pvid->reg[12]&1) {\r
     maxw = 328; maxcells = 40;\r
@@ -1333,8 +1382,8 @@ static int DrawDisplay(int sh)
   win=pvid->reg[0x12];\r
   edge=(win&0x1f)<<3;\r
 \r
-  if (win&0x80) { if (DrawScanline>=edge) hvwind=1; }\r
-  else          { if (DrawScanline< edge) hvwind=1; }\r
+  if (win&0x80) { if (est->DrawScanline>=edge) hvwind=1; }\r
+  else          { if (est->DrawScanline< edge) hvwind=1; }\r
 \r
   if (!hvwind) // we might have a vertical window here\r
   {\r
@@ -1351,54 +1400,74 @@ static int DrawDisplay(int sh)
   }\r
 \r
   /* - layer B low - */\r
-  if (PicoDrawMask & PDRAW_LAYERB_ON)\r
-    DrawLayer(1|(sh<<1), HighCacheB, 0, maxcells);\r
+  if (!(pvid->debug_p & PVD_KILL_B)) {\r
+    lflags = LF_PLANE_1 | (sh << 1);\r
+    if (pvid->debug_p & PVD_FORCE_B)\r
+      lflags |= LF_FORCE;\r
+    DrawLayer(lflags, HighCacheB, 0, maxcells, est);\r
+  }\r
   /* - layer A low - */\r
-  if (!(PicoDrawMask & PDRAW_LAYERA_ON));\r
+  lflags = 0 | (sh << 1);\r
+  if (pvid->debug_p & PVD_FORCE_A)\r
+    lflags |= LF_FORCE;\r
+  if (pvid->debug_p & PVD_KILL_A)\r
+    ;\r
   else if (hvwind == 1)\r
-    DrawWindow(0, maxcells>>1, 0, sh);\r
+    DrawWindow(0, maxcells>>1, 0, sh, est);\r
   else if (hvwind == 2) {\r
-    DrawLayer(0|(sh<<1), HighCacheA, (win&0x80) ?    0 : edge<<1, (win&0x80) ?     edge<<1 : maxcells);\r
-    DrawWindow(                      (win&0x80) ? edge :       0, (win&0x80) ? maxcells>>1 : edge, 0, sh);\r
-  } else\r
-    DrawLayer(0|(sh<<1), HighCacheA, 0, maxcells);\r
+    DrawLayer(lflags, HighCacheA, (win&0x80) ?    0 : edge<<1, (win&0x80) ?     edge<<1 : maxcells, est);\r
+    DrawWindow(                   (win&0x80) ? edge :       0, (win&0x80) ? maxcells>>1 : edge, 0, sh, est);\r
+  }\r
+  else\r
+    DrawLayer(lflags, HighCacheA, 0, maxcells, est);\r
   /* - sprites low - */\r
-  if (!(PicoDrawMask & PDRAW_SPRITES_LOW_ON));\r
-  else if (rendstatus & PDRAW_INTERLACE)\r
+  if (pvid->debug_p & PVD_KILL_S_LO)\r
+    ;\r
+  else if (est->rendstatus & PDRAW_INTERLACE)\r
     DrawAllSpritesInterlace(0, sh);\r
   else if (sprited[1] & SPRL_HAVE_LO)\r
-    DrawAllSprites(sprited, 0, sh);\r
+    DrawAllSprites(sprited, 0, sh, est);\r
 \r
   /* - layer B hi - */\r
-  if ((PicoDrawMask & PDRAW_LAYERB_ON) && HighCacheB[0])\r
-    DrawTilesFromCache(HighCacheB, sh, maxw);\r
+  if (!(pvid->debug_p & PVD_KILL_B) && HighCacheB[0])\r
+    DrawTilesFromCache(HighCacheB, sh, maxw, est);\r
   /* - layer A hi - */\r
-  if (!(PicoDrawMask & PDRAW_LAYERA_ON));\r
+  if (pvid->debug_p & PVD_KILL_A)\r
+    ;\r
   else if (hvwind == 1)\r
-    DrawWindow(0, maxcells>>1, 1, sh);\r
+    DrawWindow(0, maxcells>>1, 1, sh, est);\r
   else if (hvwind == 2) {\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
+    if (HighCacheA[0])\r
+      DrawTilesFromCache(HighCacheA, sh, (win&0x80) ? edge<<4 : maxw, est);\r
+    DrawWindow((win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 1, sh, est);\r
   } else\r
-    if (HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, maxw);\r
+    if (HighCacheA[0])\r
+      DrawTilesFromCache(HighCacheA, sh, maxw, est);\r
   /* - sprites hi - */\r
-  if (!(PicoDrawMask & PDRAW_SPRITES_HI_ON));\r
-  else if (rendstatus & PDRAW_INTERLACE)\r
+  if (pvid->debug_p & PVD_KILL_S_HI)\r
+    ;\r
+  else if (est->rendstatus & PDRAW_INTERLACE)\r
     DrawAllSpritesInterlace(1, sh);\r
   // have sprites without layer pri bit ontop of sprites with that bit\r
-  else if ((sprited[1] & 0xd0) == 0xd0 && (PicoOpt & POPT_ACC_SPRITES))\r
+  else if ((sprited[1] & 0xd0) == 0xd0 && (PicoIn.opt & POPT_ACC_SPRITES))\r
     DrawSpritesHiAS(sprited, sh);\r
   else if (sh && (sprited[1] & SPRL_MAY_HAVE_OP))\r
-    DrawSpritesSHi(sprited);\r
+    DrawSpritesSHi(sprited, est);\r
   else if (sprited[1] & SPRL_HAVE_HI)\r
-    DrawAllSprites(sprited, 1, 0);\r
+    DrawAllSprites(sprited, 1, 0, est);\r
+\r
+  if (pvid->debug_p & PVD_FORCE_B)\r
+    DrawTilesFromCacheForced(HighCacheB);\r
+  else if (pvid->debug_p & PVD_FORCE_A)\r
+    DrawTilesFromCacheForced(HighCacheA);\r
 \r
 #if 0\r
   {\r
     int *c, a, b;\r
     for (a = 0, c = HighCacheA; *c; c++, a++);\r
     for (b = 0, c = HighCacheB; *c; c++, b++);\r
-    printf("%i:%03i: a=%i, b=%i\n", Pico.m.frame_count, DrawScanline, a, b);\r
+    printf("%i:%03i: a=%i, b=%i\n", Pico.m.frame_count,\r
+           Pico.est.DrawScanline, a, b);\r
   }\r
 #endif\r
 \r
@@ -1411,30 +1480,30 @@ PICO_INTERNAL void PicoFrameStart(void)
   int offs = 8, lines = 224;\r
 \r
   // prepare to do this frame\r
-  rendstatus = 0;\r
+  Pico.est.rendstatus = 0;\r
   if ((Pico.video.reg[12] & 6) == 6)\r
-    rendstatus |= PDRAW_INTERLACE; // interlace mode\r
+    Pico.est.rendstatus |= PDRAW_INTERLACE; // interlace mode\r
   if (!(Pico.video.reg[12] & 1))\r
-    rendstatus |= PDRAW_32_COLS;\r
+    Pico.est.rendstatus |= PDRAW_32_COLS;\r
   if (Pico.video.reg[1] & 8) {\r
     offs = 0;\r
     lines = 240;\r
   }\r
 \r
-  HighCol = HighColBase;\r
-  DrawLineDest = (char *)DrawLineDestBase + offs * DrawLineDestIncrement;\r
-  DrawScanline = 0;\r
-  skip_next_line = 0;\r
-\r
-  if (rendstatus != rendstatus_old || lines != rendlines) {\r
+  if (Pico.est.rendstatus != rendstatus_old || lines != rendlines) {\r
     rendlines = lines;\r
     // mode_change() might reset rendstatus_old by calling SetColorFormat\r
     emu_video_mode_change((lines == 240) ? 0 : 8,\r
       lines, (Pico.video.reg[12] & 1) ? 0 : 1);\r
-    rendstatus_old = rendstatus;\r
+    rendstatus_old = Pico.est.rendstatus;\r
   }\r
 \r
-  if (PicoOpt & POPT_ALT_RENDERER)\r
+  Pico.est.HighCol = HighColBase + offs * HighColIncrement;\r
+  Pico.est.DrawLineDest = (char *)DrawLineDestBase + offs * DrawLineDestIncrement;\r
+  Pico.est.DrawScanline = 0;\r
+  skip_next_line = 0;\r
+\r
+  if (PicoIn.opt & POPT_ALT_RENDERER)\r
     return;\r
 \r
   if (Pico.m.dirtyPal)\r
@@ -1447,13 +1516,16 @@ static void DrawBlankedLine(int line, int offs, int sh, int bgc)
   if (PicoScanBegin != NULL)\r
     PicoScanBegin(line + offs);\r
 \r
-  BackFill(bgc, sh);\r
+  BackFill(bgc, sh, &Pico.est);\r
 \r
   if (FinalizeLine != NULL)\r
-    FinalizeLine(sh, line);\r
+    FinalizeLine(sh, line, &Pico.est);\r
 \r
   if (PicoScanEnd != NULL)\r
     PicoScanEnd(line + offs);\r
+\r
+  Pico.est.HighCol += HighColIncrement;\r
+  Pico.est.DrawLineDest = (char *)Pico.est.DrawLineDest + DrawLineDestIncrement;\r
 }\r
 \r
 static void PicoLine(int line, int offs, int sh, int bgc)\r
@@ -1465,7 +1537,7 @@ static void PicoLine(int line, int offs, int sh, int bgc)
     return;\r
   }\r
 \r
-  DrawScanline = line;\r
+  Pico.est.DrawScanline = line;\r
   if (PicoScanBegin != NULL)\r
     skip = PicoScanBegin(line + offs);\r
 \r
@@ -1474,19 +1546,22 @@ static void PicoLine(int line, int offs, int sh, int bgc)
     return;\r
   }\r
 \r
+  if (Pico.video.debug_p & (PVD_FORCE_A | PVD_FORCE_B))\r
+    bgc = 0x3f;\r
+\r
   // Draw screen:\r
-  BackFill(bgc, sh);\r
+  BackFill(bgc, sh, &Pico.est);\r
   if (Pico.video.reg[1]&0x40)\r
     DrawDisplay(sh);\r
 \r
   if (FinalizeLine != NULL)\r
-    FinalizeLine(sh, line);\r
+    FinalizeLine(sh, line, &Pico.est);\r
 \r
   if (PicoScanEnd != NULL)\r
     skip_next_line = PicoScanEnd(line + offs);\r
 \r
-  HighCol += HighColIncrement;\r
-  DrawLineDest = (char *)DrawLineDest + DrawLineDestIncrement;\r
+  Pico.est.HighCol += HighColIncrement;\r
+  Pico.est.DrawLineDest = (char *)Pico.est.DrawLineDest + DrawLineDestIncrement;\r
 }\r
 \r
 void PicoDrawSync(int to, int blank_last_line)\r
@@ -1497,23 +1572,14 @@ void PicoDrawSync(int to, int blank_last_line)
 \r
   pprof_start(draw);\r
 \r
-  if (rendlines != 240)\r
+  if (rendlines != 240) {\r
     offs = 8;\r
-\r
-  for (line = DrawScanline; line < to; line++)\r
-  {\r
-#if !CAN_HANDLE_240_LINES\r
-    if (line >= 224) break;\r
-#endif\r
-    PicoLine(line, offs, sh, bgc);\r
+    if (to > 223)\r
+      to = 223;\r
   }\r
 \r
-#if !CAN_HANDLE_240_LINES\r
-  if (line >= 224) {\r
-    DrawScanline = 240;\r
-    return;\r
-  }\r
-#endif\r
+  for (line = Pico.est.DrawScanline; line < to; line++)\r
+    PicoLine(line, offs, sh, bgc);\r
 \r
   // last line\r
   if (line <= to)\r
@@ -1523,7 +1589,7 @@ void PicoDrawSync(int to, int blank_last_line)
     else PicoLine(line, offs, sh, bgc);\r
     line++;\r
   }\r
-  DrawScanline = line;\r
+  Pico.est.DrawScanline = line;\r
 \r
   pprof_end(draw);\r
 }\r
@@ -1531,19 +1597,20 @@ void PicoDrawSync(int to, int blank_last_line)
 // also works for fast renderer\r
 void PicoDrawUpdateHighPal(void)\r
 {\r
+  struct PicoEState *est = &Pico.est;\r
   int sh = (Pico.video.reg[0xC] & 8) >> 3; // shadow/hilight?\r
-  if (PicoOpt & POPT_ALT_RENDERER)\r
+  if (PicoIn.opt & POPT_ALT_RENDERER)\r
     sh = 0; // no s/h support\r
 \r
-  PicoDoHighPal555(sh);\r
-  if (rendstatus & PDRAW_SONIC_MODE) {\r
+  PicoDoHighPal555(sh, 0, &Pico.est);\r
+  if (est->rendstatus & PDRAW_SONIC_MODE) {\r
     // FIXME?\r
-    memcpy(HighPal + 0x40, HighPal, 0x40*2);\r
-    memcpy(HighPal + 0x80, HighPal, 0x40*2);\r
+    memcpy(est->HighPal + 0x40, est->HighPal, 0x40*2);\r
+    memcpy(est->HighPal + 0x80, est->HighPal, 0x40*2);\r
   }\r
 }\r
 \r
-void PicoDrawSetOutFormat(pdso_t which, int allow_32x)\r
+void PicoDrawSetOutFormat(pdso_t which, int use_32x_line_mode)\r
 {\r
   switch (which)\r
   {\r
@@ -1552,7 +1619,7 @@ void PicoDrawSetOutFormat(pdso_t which, int allow_32x)
       break;\r
 \r
     case PDF_RGB555:\r
-      if ((PicoAHW & PAHW_32X) && allow_32x)\r
+      if ((PicoIn.AHW & PAHW_32X) && use_32x_line_mode)\r
         FinalizeLine = FinalizeLine32xRGB555;\r
       else\r
         FinalizeLine = FinalizeLine555;\r
@@ -1562,14 +1629,17 @@ void PicoDrawSetOutFormat(pdso_t which, int allow_32x)
       FinalizeLine = NULL;\r
       break;\r
   }\r
+  PicoDrawSetOutFormat32x(which, use_32x_line_mode);\r
   PicoDrawSetOutputMode4(which);\r
   rendstatus_old = -1;\r
 }\r
 \r
+// note: may be called on the middle of frame\r
 void PicoDrawSetOutBuf(void *dest, int increment)\r
 {\r
   DrawLineDestBase = dest;\r
   DrawLineDestIncrement = increment;\r
+  Pico.est.DrawLineDest = (char *)DrawLineDestBase + Pico.est.DrawScanline * increment;\r
 }\r
 \r
 void PicoDrawSetInternalBuf(void *dest, int increment)\r
@@ -1577,6 +1647,7 @@ void PicoDrawSetInternalBuf(void *dest, int increment)
   if (dest != NULL) {\r
     HighColBase = dest;\r
     HighColIncrement = increment;\r
+    Pico.est.HighCol = HighColBase + Pico.est.DrawScanline * increment;\r
   }\r
   else {\r
     HighColBase = DefHighCol;\r
@@ -1584,3 +1655,29 @@ void PicoDrawSetInternalBuf(void *dest, int increment)
   }\r
 }\r
 \r
+void PicoDrawSetCallbacks(int (*begin)(unsigned int num), int (*end)(unsigned int num))\r
+{\r
+  PicoScanBegin = NULL;\r
+  PicoScanEnd = NULL;\r
+  PicoScan32xBegin = NULL;\r
+  PicoScan32xEnd = NULL;\r
+\r
+  if ((PicoIn.AHW & PAHW_32X) && FinalizeLine != FinalizeLine32xRGB555) {\r
+    PicoScan32xBegin = begin;\r
+    PicoScan32xEnd = end;\r
+  }\r
+  else {\r
+    PicoScanBegin = begin;\r
+    PicoScanEnd = end;\r
+  }\r
+}\r
+\r
+void PicoDrawInit(void)\r
+{\r
+  Pico.est.DrawLineDest = DefOutBuff;\r
+  Pico.est.HighCol = HighColBase;\r
+  Pico.est.HighPreSpr = HighPreSpr;\r
+  rendstatus_old = -1;\r
+}\r
+\r
+// vim:ts=2:sw=2:expandtab\r