fix DrawSpritesHiAS
[picodrive.git] / pico / draw.c
index 7b4dd99..680de3d 100644 (file)
  * - "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          -         pal index\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
@@ -46,6 +45,10 @@ static int  HighCacheA[41+1];   // caches for high layers
 static int  HighCacheB[41+1];\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
 #define SPRL_MAY_HAVE_OP 0x20 // may have operator sprites on the line\r
@@ -54,12 +57,9 @@ unsigned char HighLnSpr[240][3 + MAX_LINE_SPRITES]; // sprite_count, ^flags, til
 \r
 int rendstatus_old;\r
 int rendlines;\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
@@ -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 = Pico.est.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 = Pico.est.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
+TileNormMakerAS(TileNormSH_AS, pix_sh_as)\r
+TileFlipMakerAS(TileFlipSH_AS, pix_sh_as)\r
 \r
-TileNormMaker(TileNormAS_noop, pix_sh_as_noop)\r
-TileFlipMaker(TileFlipAS_noop, pix_sh_as_noop)\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
+TileNormMakerAS(TileNormAS_onlymark, pix_sh_as_onlymark)\r
+TileFlipMakerAS(TileFlipAS_onlymark, pix_sh_as_onlymark)\r
 \r
-TileNormMaker(TileNormAS_onlymark, pix_sh_as_onlymark)\r
-TileFlipMaker(TileFlipAS_onlymark, pix_sh_as_onlymark)\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,10 +262,14 @@ 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
@@ -262,8 +279,9 @@ static void DrawStrip(struct TileStrip *ts, int plane_sh, int cellskip)
 }\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=Pico.est.DrawScanline;\r
@@ -271,19 +289,24 @@ void DrawStripVSRam(struct TileStrip *ts, int plane_sh, int cellskip)
   // 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,10 +332,14 @@ 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
@@ -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
@@ -394,8 +426,11 @@ 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
@@ -407,11 +442,11 @@ static void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells,
   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+(est->DrawScanline<<1))&((ymask<<1)|1);\r
@@ -424,7 +459,7 @@ 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+est->DrawScanline)&ymask;\r
@@ -441,7 +476,8 @@ static void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells,
 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
@@ -461,7 +497,7 @@ static void DrawWindow(int tstart, int tend, int prio, int sh,
 \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
@@ -474,36 +510,43 @@ static void DrawWindow(int tstart, int tend, int prio, int sh,
   {\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
         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
+      pal = ((code >> 9) & 0x30);\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
   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
         est->rendstatus |= PDRAW_WND_DIFF_PRIO;\r
@@ -524,10 +567,16 @@ static void DrawWindow(int tstart, int tend, int prio, int sh,
       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
@@ -549,7 +598,9 @@ static void DrawTilesFromCacheShPrep(void)
 \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
@@ -565,26 +616,32 @@ static void DrawTilesFromCache(int *hc, int sh, int rlim, struct PicoEState *est
   {\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
@@ -593,20 +650,26 @@ static void DrawTilesFromCache(int *hc, int sh, int rlim, struct PicoEState *est
       *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
+      pal = ((code >> 9) & 0x30);\r
+      if (rlim - dx < 0)\r
+        goto last_cut_tile;\r
 \r
-      if (code&0x0800) TileFlip(dx,addr,pal);\r
-      else             TileNorm(dx,addr,pal);\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 = est->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
@@ -645,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
@@ -684,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
@@ -727,17 +816,19 @@ 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=Pico.est.DrawScanline<<1;\r
@@ -752,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
@@ -794,7 +885,8 @@ static void DrawAllSpritesInterlace(int pri, int sh)
  */\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
@@ -850,11 +942,13 @@ static void DrawSpritesSHi(unsigned char *sprited, const struct PicoEState *est)
 \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
@@ -862,15 +956,17 @@ static void DrawSpritesSHi(unsigned char *sprited, const struct PicoEState *est)
 \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
-  Pico.est.rendstatus |= PDRAW_SPR_LO_ON_HI;\r
-\r
+  memset(mb, 0xff, sizeof(mb));\r
   p = &sprited[3];\r
 \r
   // Go through sprites:\r
@@ -884,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
@@ -919,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
-    }\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 *)(Pico.est.HighCol+8);\r
-    while (c--)\r
-    {\r
-      *zb++ &= 0x7f7f7f7f; *zb++ &= 0x7f7f7f7f;\r
-      *zb++ &= 0x7f7f7f7f; *zb++ &= 0x7f7f7f7f;\r
+      pack = *(unsigned int *)(PicoMem.vram + (tile & 0x7fff));\r
+      fTileFunc(pd + sx, mb + sx, pack, pal);\r
     }\r
   }\r
-\r
-  /* nasty 2: sh operator pass */\r
-  sprited[0] = sh_cnt;\r
-  DrawSpritesSHi(sprited, &Pico.est);\r
 }\r
 \r
 \r
@@ -966,7 +1051,7 @@ static NOINLINE 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
@@ -985,7 +1070,7 @@ static NOINLINE 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
@@ -1038,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
@@ -1162,7 +1247,7 @@ void PicoDoHighPal555(int sh, int line, struct PicoEState *est)
 \r
   Pico.m.dirtyPal = 0;\r
 \r
-  spal = (void *)Pico.cram;\r
+  spal = (void *)PicoMem.cram;\r
   dpal = (void *)est->HighPal;\r
 \r
   for (i = 0; i < 0x40 / 2; i++) {\r
@@ -1206,22 +1291,20 @@ void FinalizeLine555(int sh, int line, struct PicoEState *est)
   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 && (est->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 && (est->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
@@ -1244,16 +1327,16 @@ static void FinalizeLine8bit(int sh, int line, struct PicoEState *est)
     rs |= PDRAW_SONIC_MODE;\r
     est->rendstatus = rs;\r
     if (dirty_count == 3) {\r
-      blockcpy(est->HighPal, Pico.cram, 0x40*2);\r
+      blockcpy(est->HighPal, PicoMem.cram, 0x40*2);\r
     } else if (dirty_count == 11) {\r
-      blockcpy(est->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
@@ -1278,8 +1361,8 @@ static int DrawDisplay(int sh)
   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
   if (est->rendstatus & (PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES)) {\r
     // elprintf(EL_STATUS, "PrepareSprites(%i)", (est->rendstatus>>4)&1);\r
@@ -1317,29 +1400,40 @@ static int DrawDisplay(int sh)
   }\r
 \r
   /* - layer B low - */\r
-  if (PicoDrawMask & PDRAW_LAYERB_ON)\r
-    DrawLayer(1|(sh<<1), HighCacheB, 0, maxcells, est);\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, est);\r
   else if (hvwind == 2) {\r
-    DrawLayer(0|(sh<<1), 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
-  } else\r
-    DrawLayer(0|(sh<<1), HighCacheA, 0, maxcells, est);\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
+  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, est);\r
 \r
   /* - layer B hi - */\r
-  if ((PicoDrawMask & PDRAW_LAYERB_ON) && HighCacheB[0])\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, est);\r
   else if (hvwind == 2) {\r
@@ -1350,17 +1444,23 @@ static int DrawDisplay(int sh)
     if (HighCacheA[0])\r
       DrawTilesFromCache(HighCacheA, sh, maxw, est);\r
   /* - sprites hi - */\r
-  if (!(PicoDrawMask & PDRAW_SPRITES_HI_ON));\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, est);\r
   else if (sprited[1] & SPRL_HAVE_HI)\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
@@ -1403,7 +1503,7 @@ PICO_INTERNAL void PicoFrameStart(void)
   Pico.est.DrawScanline = 0;\r
   skip_next_line = 0;\r
 \r
-  if (PicoOpt & POPT_ALT_RENDERER)\r
+  if (PicoIn.opt & POPT_ALT_RENDERER)\r
     return;\r
 \r
   if (Pico.m.dirtyPal)\r
@@ -1446,6 +1546,9 @@ 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, &Pico.est);\r
   if (Pico.video.reg[1]&0x40)\r
@@ -1469,13 +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
+    if (to > 223)\r
+      to = 223;\r
+  }\r
 \r
   for (line = Pico.est.DrawScanline; line < to; line++)\r
-  {\r
     PicoLine(line, offs, sh, bgc);\r
-  }\r
 \r
   // last line\r
   if (line <= to)\r
@@ -1495,7 +1599,7 @@ void PicoDrawUpdateHighPal(void)
 {\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, 0, &Pico.est);\r
@@ -1515,7 +1619,7 @@ void PicoDrawSetOutFormat(pdso_t which, int use_32x_line_mode)
       break;\r
 \r
     case PDF_RGB555:\r
-      if ((PicoAHW & PAHW_32X) && use_32x_line_mode)\r
+      if ((PicoIn.AHW & PAHW_32X) && use_32x_line_mode)\r
         FinalizeLine = FinalizeLine32xRGB555;\r
       else\r
         FinalizeLine = FinalizeLine555;\r
@@ -1535,7 +1639,7 @@ void PicoDrawSetOutBuf(void *dest, int increment)
 {\r
   DrawLineDestBase = dest;\r
   DrawLineDestIncrement = increment;\r
-  Pico.est.DrawLineDest = DrawLineDestBase + Pico.est.DrawScanline * increment;\r
+  Pico.est.DrawLineDest = (char *)DrawLineDestBase + Pico.est.DrawScanline * increment;\r
 }\r
 \r
 void PicoDrawSetInternalBuf(void *dest, int increment)\r
@@ -1558,7 +1662,7 @@ void PicoDrawSetCallbacks(int (*begin)(unsigned int num), int (*end)(unsigned in
   PicoScan32xBegin = NULL;\r
   PicoScan32xEnd = NULL;\r
 \r
-  if ((PicoAHW & PAHW_32X) && FinalizeLine != FinalizeLine32xRGB555) {\r
+  if ((PicoIn.AHW & PAHW_32X) && FinalizeLine != FinalizeLine32xRGB555) {\r
     PicoScan32xBegin = begin;\r
     PicoScan32xEnd = end;\r
   }\r