-// 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
* - shadow/hilight (s/h)\r
- * - "sonic mode" for midline palette changes\r
- * - accurate sprites (AS)\r
+ * - "sonic mode" for midline palette changes (8bit mode only)\r
+ * - accurate sprites (AS) [+ s/h]\r
*\r
* AS and s/h both use upper bits for both priority and shadow/hilight flags.\r
* "sonic mode" is autodetected, shadow/hilight is enabled by emulated game.\r
* AS is enabled by user and takes priority over "sonic mode".\r
+ *\r
+ * since renderer always draws line in 8bit mode, there are 2 spare bits:\r
+ * b \ mode: s/h as sonic\r
+ * 00 normal - pal index\r
+ * 01 shadow - pal index\r
+ * 10 hilight+op spr spr pal index\r
+ * 11 shadow +op spr - pal index\r
+ *\r
+ * not handled properly:\r
+ * - hilight op on shadow tile\r
+ * - AS + s/h (s/h sprite flag interferes with and cleared by AS code)\r
*/\r
\r
#include "pico_int.h"\r
int (*PicoScanBegin)(unsigned int num) = NULL;\r
int (*PicoScanEnd) (unsigned int num) = NULL;\r
\r
-#if OVERRIDE_HIGHCOL\r
static unsigned char DefHighCol[8+320+8];\r
-unsigned char *HighCol=DefHighCol;\r
-#else\r
-unsigned char HighCol[8+320+8];\r
-#endif\r
-unsigned short DefOutBuff[320*2];\r
-void *DrawLineDest=DefOutBuff; // pointer to dest buffer where to draw this line to\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
#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 = 0;\r
-int DrawScanline = 0;\r
+int rendstatus, rendstatus_old;\r
+int rendlines;\r
+int DrawScanline;\r
int PicoDrawMask = -1;\r
\r
static int skip_next_line=0;\r
// stuff available in asm:\r
#ifdef _ASM_DRAW_C\r
void DrawWindow(int tstart, int tend, int prio, int sh);\r
-void BackFill(int reg7, int sh);\r
void DrawAllSprites(unsigned char *sprited, int prio, int sh);\r
void DrawTilesFromCache(int *hc, int sh, int rlim);\r
void DrawSpritesSHi(unsigned char *sprited);\r
void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells);\r
-void FinalizeLineBGR444(int sh);\r
-void FinalizeLineRGB555(int sh);\r
+void FinalizeLineBGR444(int sh, int line);\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
// draw a sprite pixel, process operator colors\r
#define pix_sh(x) \\r
if (!t); \\r
- else if (t==0xe) pd[x]=(pd[x]&0x3f)|0x80; /* hilight */ \\r
- else if (t==0xf) pd[x]= pd[x] |0xc0; /* shadow */ \\r
+ else if (t>=0xe) pd[x]=(pd[x]&0x3f)|(t<<6); /* c0 shadow, 80 hilight */ \\r
else pd[x]=pal|t\r
\r
TileNormMaker(TileNormSH, pix_sh)\r
TileFlipMaker(TileFlipSH, pix_sh)\r
\r
-// draw a sprite pixel ignoring operator colors\r
-#define pix_sh_noop(x) \\r
- if (t && t < 0xe) \\r
- pd[x]=pal|t\r
+// draw a sprite pixel, mark operator colors\r
+#define pix_sh_markop(x) \\r
+ if (!t); \\r
+ else if (t>=0xe) pd[x]|=0x80; \\r
+ else pd[x]=pal|t\r
\r
-TileNormMaker(TileNormSH_noop, pix_sh_noop)\r
-TileFlipMaker(TileFlipSH_noop, pix_sh_noop)\r
+TileNormMaker(TileNormSH_markop, pix_sh_markop)\r
+TileFlipMaker(TileFlipSH_markop, pix_sh_markop)\r
\r
-// process operator pixels only, apply only on low pri tiles\r
+// process operator pixels only, apply only on low pri tiles and other op pixels\r
#define pix_sh_onlyop(x) \\r
- if (t==0xe && (pd[x]&0x40)) pd[x]=(pd[x]&0x3f)|0x80; /* hilight */ \\r
- else if (t==0xf && (pd[x]&0x40)) pd[x]= pd[x] |0xc0; /* shadow */\r
+ if (t>=0xe && (pd[x]&0xc0)) \\r
+ pd[x]=(pd[x]&0x3f)|(t<<6); /* c0 shadow, 80 hilight */ \\r
\r
TileNormMaker(TileNormSH_onlyop_lp, pix_sh_onlyop)\r
TileFlipMaker(TileFlipSH_onlyop_lp, pix_sh_onlyop)\r
\r
if (prio) {\r
int *zb = (int *)(HighCol+8+(tilex<<3));\r
- *zb++ &= 0x3f3f3f3f;\r
- *zb &= 0x3f3f3f3f;\r
+ *zb++ &= 0xbfbfbfbf;\r
+ *zb &= 0xbfbfbfbf;\r
} else {\r
pal |= 0x40;\r
}\r
static void DrawTilesFromCacheShPrep(void)\r
{\r
// as some layer has covered whole line with hi priority tiles,\r
- // we can process whole line and then act as if sh/hi mode was off.\r
+ // we can process whole line and then act as if sh/hi mode was off,\r
+ // but leave lo pri op sprite markers alone\r
int c = 320/4, *zb = (int *)(HighCol+8);\r
rendstatus |= PDRAW_SHHI_DONE;\r
while (c--)\r
{\r
- *zb++ &= 0x3f3f3f3f;\r
+ *zb++ &= 0xbfbfbfbf;\r
}\r
}\r
\r
addr+=(unsigned int)code>>25; // y offset into tile\r
dx=(code>>16)&0x1ff;\r
zb = HighCol+dx;\r
- *zb++ &= 0x3f; *zb++ &= 0x3f; *zb++ &= 0x3f; *zb++ &= 0x3f;\r
- *zb++ &= 0x3f; *zb++ &= 0x3f; *zb++ &= 0x3f; *zb++ &= 0x3f;\r
+ *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf;\r
+ *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf;\r
\r
pal=((code>>9)&0x30);\r
if (rlim-dx < 0) goto last_cut_tile;\r
pal|=sh<<6;\r
\r
if (sh && (code&0x6000) == 0x6000) {\r
- if(code&0x0800) fTileFunc=TileFlipSH_noop;\r
- else fTileFunc=TileNormSH_noop;\r
+ if(code&0x0800) fTileFunc=TileFlipSH_markop;\r
+ else fTileFunc=TileNormSH_markop;\r
} else {\r
if(code&0x0800) fTileFunc=TileFlip;\r
else fTileFunc=TileNorm;\r
\r
\r
#ifndef _ASM_DRAW_C\r
-// Index + 0 : hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size\r
-// Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8\r
+/*\r
+ * s/h drawing: lo_layers|40, lo_sprites|40 && mark_op,\r
+ * hi_layers&=~40, hi_sprites\r
+ *\r
+ * Index + 0 : hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size\r
+ * Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8\r
+ */\r
static void DrawSpritesSHi(unsigned char *sprited)\r
{\r
int (*fTileFunc)(int sx,int addr,int pal);\r
\r
/* nasty 1: remove 'sprite' flags */\r
{\r
- int c = 320/4, *zb = (int *)(HighCol+8);\r
+ int c = 320/4/4, *zb = (int *)(HighCol+8);\r
while (c--)\r
{\r
- *zb++ &= 0x7f7f7f7f;\r
+ *zb++ &= 0x7f7f7f7f; *zb++ &= 0x7f7f7f7f;\r
+ *zb++ &= 0x7f7f7f7f; *zb++ &= 0x7f7f7f7f;\r
}\r
}\r
\r
\r
// --------------------------------------------\r
\r
-static void BackFill(int reg7, int sh)\r
+void BackFill(int reg7, int sh)\r
{\r
unsigned int back;\r
\r
#ifndef _ASM_DRAW_C\r
void PicoDoHighPal555(int sh)\r
{\r
- unsigned short *pal=HighPal;\r
- int i, t;\r
+ unsigned int *spal, *dpal;\r
+ unsigned int t, i;\r
\r
Pico.m.dirtyPal = 0;\r
\r
- {\r
- unsigned int *spal=(void *)Pico.cram;\r
- unsigned int *dpal=(void *)HighPal;\r
- for (i = 0x3f/2; i >= 0; i--)\r
+ spal = (void *)Pico.cram;\r
+ dpal = (void *)HighPal;\r
+\r
+ for (i = 0; i < 0x40 / 2; i++) {\r
+ t = spal[i];\r
#ifdef USE_BGR555\r
- dpal[i] = ((spal[i]&0x000f000f)<< 1)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)<<4);\r
+ t = ((t & 0x000e000e)<< 1) | ((t & 0x00e000e0)<<3) | ((t & 0x0e000e00)<<4);\r
#else\r
- dpal[i] = ((spal[i]&0x000f000f)<<12)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)>>7);\r
+ t = ((t & 0x000e000e)<<12) | ((t & 0x00e000e0)<<3) | ((t & 0x0e000e00)>>7);\r
#endif\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
+ 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
\r
-static void FinalizeLineBGR444(int sh)\r
+#if 0\r
+static void FinalizeLineBGR444(int sh, int line)\r
{\r
unsigned short *pd=DrawLineDest;\r
unsigned char *ps=HighCol+8;\r
for(i = 0; i < len; i++)\r
pd[i] = pal[ps[i] & mask];\r
}\r
+#endif\r
\r
\r
-static void FinalizeLineRGB555(int sh)\r
+void FinalizeLine555(int sh, int line)\r
{\r
unsigned short *pd=DrawLineDest;\r
unsigned char *ps=HighCol+8;\r
}\r
#endif\r
\r
-static void FinalizeLine8bit(int sh)\r
+static void FinalizeLine8bit(int sh, int line)\r
{\r
- unsigned char *pd=DrawLineDest;\r
+ unsigned char *pd = DrawLineDest;\r
int len, rs = rendstatus;\r
static int dirty_count;\r
\r
- if (!sh && Pico.m.dirtyPal == 1 && DrawScanline < 222)\r
+ if (!sh && Pico.m.dirtyPal == 1)\r
{\r
// a hack for mid-frame palette changes\r
if (!(rs & PDRAW_SONIC_MODE))\r
if (Pico.video.reg[12]&1) {\r
len = 320;\r
} else {\r
- if (!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32;\r
+ if (!(PicoOpt & POPT_DIS_32C_BORDER))\r
+ pd += 32;\r
len = 256;\r
}\r
\r
}\r
}\r
\r
-static void (*FinalizeLine)(int sh) = FinalizeLineBGR444;\r
+static void (*FinalizeLine)(int sh, int line);\r
\r
// --------------------------------------------\r
\r
-static void DrawBlankedLine(void)\r
-{\r
- int sh=(Pico.video.reg[0xC]&8)>>3; // shadow/hilight?\r
-\r
- if (PicoScanBegin != NULL)\r
- PicoScanBegin(DrawScanline);\r
-\r
- BackFill(Pico.video.reg[7], sh);\r
-\r
- if (FinalizeLine != NULL)\r
- FinalizeLine(sh);\r
-\r
- if (PicoScanEnd != NULL)\r
- PicoScanEnd(DrawScanline);\r
-}\r
-\r
static int DrawDisplay(int sh)\r
{\r
unsigned char *sprited = &HighLnSpr[DrawScanline][0];\r
// MUST be called every frame\r
PICO_INTERNAL void PicoFrameStart(void)\r
{\r
+ int offs = 8, lines = 224;\r
+\r
// prepare to do this frame\r
rendstatus = 0;\r
- if ((Pico.video.reg[12]&6) == 6)\r
+ if ((Pico.video.reg[12] & 6) == 6)\r
rendstatus |= PDRAW_INTERLACE; // interlace mode\r
+ if (!(Pico.video.reg[12] & 1))\r
+ rendstatus |= PDRAW_32_COLS;\r
+ if (Pico.video.reg[1] & 8) {\r
+ offs = 0;\r
+ lines = 240;\r
+ }\r
+\r
+ if (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
+ }\r
+\r
+ HighCol = HighColBase + offs * HighColIncrement;\r
+ DrawLineDest = (char *)DrawLineDestBase + offs * DrawLineDestIncrement;\r
+ DrawScanline = 0;\r
+ skip_next_line = 0;\r
\r
- if (Pico.m.dirtyPal) Pico.m.dirtyPal = 2; // reset dirty if needed\r
+ if (PicoOpt & POPT_ALT_RENDERER)\r
+ return;\r
\r
- DrawScanline=0;\r
+ if (Pico.m.dirtyPal)\r
+ Pico.m.dirtyPal = 2; // reset dirty if needed\r
PrepareSprites(1);\r
- skip_next_line=0;\r
}\r
\r
-static void PicoLine(void)\r
+static void DrawBlankedLine(int line, int offs, int sh, int bgc)\r
+{\r
+ if (PicoScanBegin != NULL)\r
+ PicoScanBegin(line + offs);\r
+\r
+ BackFill(bgc, sh);\r
+\r
+ if (FinalizeLine != NULL)\r
+ FinalizeLine(sh, line);\r
+\r
+ if (PicoScanEnd != NULL)\r
+ PicoScanEnd(line + offs);\r
+\r
+ HighCol += HighColIncrement;\r
+ DrawLineDest = (char *)DrawLineDest + DrawLineDestIncrement;\r
+}\r
+\r
+static void PicoLine(int line, int offs, int sh, int bgc)\r
{\r
- int sh;\r
- if (skip_next_line>0) { skip_next_line--; return; } // skip rendering lines\r
+ int skip = 0;\r
\r
- sh=(Pico.video.reg[0xC]&8)>>3; // shadow/hilight?\r
+ if (skip_next_line > 0) {\r
+ skip_next_line--;\r
+ return;\r
+ }\r
\r
+ DrawScanline = line;\r
if (PicoScanBegin != NULL)\r
- skip_next_line = PicoScanBegin(DrawScanline);\r
+ skip = PicoScanBegin(line + offs);\r
+\r
+ if (skip) {\r
+ skip_next_line = skip - 1;\r
+ return;\r
+ }\r
\r
// Draw screen:\r
- BackFill(Pico.video.reg[7], sh);\r
+ BackFill(bgc, sh);\r
if (Pico.video.reg[1]&0x40)\r
DrawDisplay(sh);\r
\r
if (FinalizeLine != NULL)\r
- FinalizeLine(sh);\r
+ FinalizeLine(sh, line);\r
\r
if (PicoScanEnd != NULL)\r
- skip_next_line = PicoScanEnd(DrawScanline);\r
+ skip_next_line = PicoScanEnd(line + offs);\r
+\r
+ HighCol += HighColIncrement;\r
+ DrawLineDest = (char *)DrawLineDest + DrawLineDestIncrement;\r
}\r
\r
void PicoDrawSync(int to, int blank_last_line)\r
{\r
- for (; DrawScanline < to; DrawScanline++)\r
+ int line, offs = 0;\r
+ int sh = (Pico.video.reg[0xC] & 8) >> 3; // shadow/hilight?\r
+ int bgc = Pico.video.reg[7];\r
+\r
+ pprof_start(draw);\r
+\r
+ if (rendlines != 240)\r
+ offs = 8;\r
+\r
+ for (line = DrawScanline; line < to; line++)\r
{\r
-#if !CAN_HANDLE_240_LINES\r
- if (DrawScanline >= 224) break;\r
-#endif\r
- PicoLine();\r
+ PicoLine(line, offs, sh, bgc);\r
}\r
\r
-#if !CAN_HANDLE_240_LINES\r
- if (DrawScanline >= 224) { DrawScanline = 240; return; }\r
-#endif\r
-\r
// last line\r
- if (DrawScanline <= to)\r
+ if (line <= to)\r
{\r
if (blank_last_line)\r
- DrawBlankedLine();\r
- else PicoLine();\r
- DrawScanline++;\r
+ DrawBlankedLine(line, offs, sh, bgc);\r
+ else PicoLine(line, offs, sh, bgc);\r
+ line++;\r
+ }\r
+ DrawScanline = line;\r
+\r
+ pprof_end(draw);\r
+}\r
+\r
+// also works for fast renderer\r
+void PicoDrawUpdateHighPal(void)\r
+{\r
+ int sh = (Pico.video.reg[0xC] & 8) >> 3; // shadow/hilight?\r
+ if (PicoOpt & POPT_ALT_RENDERER)\r
+ sh = 0; // no s/h support\r
+\r
+ PicoDoHighPal555(sh);\r
+ if (rendstatus & PDRAW_SONIC_MODE) {\r
+ // FIXME?\r
+ memcpy(HighPal + 0x40, HighPal, 0x40*2);\r
+ memcpy(HighPal + 0x80, HighPal, 0x40*2);\r
}\r
}\r
\r
-void PicoDrawSetColorFormat(int which)\r
+void PicoDrawSetOutFormat(pdso_t which, int use_32x_line_mode)\r
{\r
switch (which)\r
{\r
- case 2: FinalizeLine = FinalizeLine8bit; break;\r
- case 1: FinalizeLine = FinalizeLineRGB555; break;\r
- case 0: FinalizeLine = FinalizeLineBGR444; break;\r
- default:FinalizeLine = NULL; break;\r
+ case PDF_8BIT:\r
+ FinalizeLine = FinalizeLine8bit;\r
+ break;\r
+\r
+ case PDF_RGB555:\r
+ if ((PicoAHW & PAHW_32X) && use_32x_line_mode)\r
+ FinalizeLine = FinalizeLine32xRGB555;\r
+ else\r
+ FinalizeLine = FinalizeLine555;\r
+ break;\r
+\r
+ default:\r
+ FinalizeLine = NULL;\r
+ break;\r
}\r
-#if OVERRIDE_HIGHCOL\r
- if (which) HighCol=DefHighCol;\r
-#endif\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
+ DrawLineDest = (unsigned char*)DrawLineDestBase + DrawScanline * increment;\r
}\r
\r
+void PicoDrawSetInternalBuf(void *dest, int increment)\r
+{\r
+ if (dest != NULL) {\r
+ HighColBase = dest;\r
+ HighColIncrement = increment;\r
+ HighCol = HighColBase + DrawScanline * increment;\r
+ }\r
+ else {\r
+ HighColBase = DefHighCol;\r
+ HighColIncrement = 0;\r
+ }\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 ((PicoAHW & PAHW_32X) && FinalizeLine != FinalizeLine32xRGB555) {\r
+ PicoScan32xBegin = begin;\r
+ PicoScan32xEnd = end;\r
+ }\r
+ else {\r
+ PicoScanBegin = begin;\r
+ PicoScanEnd = end;\r
+ }\r
+}\r