fix DrawSpritesHiAS
[picodrive.git] / pico / draw.c
CommitLineData
cff531af 1/*\r
2 * line renderer\r
3 * (c) Copyright Dave, 2004\r
4 * (C) notaz, 2006-2010\r
5 *\r
6 * This work is licensed under the terms of MAME license.\r
7 * See COPYING file in the top-level directory.\r
8 */\r
e5fa9817 9/*\r
10 * The renderer has 4 modes now:\r
11 * - normal\r
12 * - shadow/hilight (s/h)\r
40644bfe 13 * - "sonic mode" for midline palette changes (8bit mode only)\r
14 * - accurate sprites (AS) [+ s/h]\r
e5fa9817 15 *\r
ee3c39ef 16 * s/h uses upper bits for both priority and shadow/hilight flags.\r
e5fa9817 17 * "sonic mode" is autodetected, shadow/hilight is enabled by emulated game.\r
18 * AS is enabled by user and takes priority over "sonic mode".\r
40644bfe 19 *\r
bfa12428 20 * since renderer always draws line in 8bit mode, there are 2 spare bits:\r
ee3c39ef 21 * b \ mode: s/h sonic\r
22 * 00 normal pal index\r
23 * 01 shadow pal index\r
24 * 10 hilight+op spr pal index\r
25 * 11 shadow +op spr pal index\r
bfa12428 26 *\r
40644bfe 27 * not handled properly:\r
28 * - hilight op on shadow tile\r
e5fa9817 29 */\r
cc68a136 30\r
efcba75f 31#include "pico_int.h"\r
cc68a136 32\r
602133e1 33int (*PicoScanBegin)(unsigned int num) = NULL;\r
34int (*PicoScanEnd) (unsigned int num) = NULL;\r
cc68a136 35\r
ea8c405f 36static unsigned char DefHighCol[8+320+8];\r
5a681086 37static unsigned char *HighColBase = DefHighCol;\r
38static int HighColIncrement;\r
39\r
87b0845f 40static unsigned int DefOutBuff[320*2/2];\r
5a681086 41void *DrawLineDestBase = DefOutBuff;\r
42int DrawLineDestIncrement;\r
ea8c405f 43\r
cc68a136 44static int HighCacheA[41+1]; // caches for high layers\r
45static int HighCacheB[41+1];\r
99bdfd31 46static int HighPreSpr[80*2+1]; // slightly preprocessed sprites\r
381eea9b 47\r
e0bcb7a9 48#define LF_PLANE_1 (1 << 0)\r
49#define LF_SH (1 << 1) // must be = 2\r
50#define LF_FORCE (1 << 2)\r
51\r
fbc65db7 52#define SPRL_HAVE_HI 0x80 // have hi priority sprites\r
53#define SPRL_HAVE_LO 0x40 // *lo*\r
54#define SPRL_MAY_HAVE_OP 0x20 // may have operator sprites on the line\r
55#define SPRL_LO_ABOVE_HI 0x10 // low priority sprites may be on top of hi\r
56unsigned char HighLnSpr[240][3 + MAX_LINE_SPRITES]; // sprite_count, ^flags, tile_count, [spritep]...\r
d9fc2fe1 57\r
ea38612f 58int rendstatus_old;\r
ae87bffa 59int rendlines;\r
cc68a136 60\r
602133e1 61static int skip_next_line=0;\r
62\r
cc68a136 63struct TileStrip\r
64{\r
65 int nametab; // Position in VRAM of name table (for this tile line)\r
6d7acf9e 66 int line; // Line number in pixels 0x000-0x3ff within the virtual tilemap\r
cc68a136 67 int hscroll; // Horizontal scroll value in pixels for the line\r
68 int xmask; // X-Mask (0x1f - 0x7f) for horizontal wraparound in the tilemap\r
69 int *hc; // cache for high tile codes and their positions\r
70 int cells; // cells (tiles) to draw (32 col mode doesn't need to update whole 320)\r
71};\r
72\r
73// stuff available in asm:\r
74#ifdef _ASM_DRAW_C\r
ea38612f 75void DrawWindow(int tstart, int tend, int prio, int sh,\r
76 struct PicoEState *est);\r
77void DrawAllSprites(unsigned char *sprited, int prio, int sh,\r
78 struct PicoEState *est);\r
79void DrawTilesFromCache(int *hc, int sh, int rlim,\r
80 struct PicoEState *est);\r
81void DrawSpritesSHi(unsigned char *sprited, struct PicoEState *est);\r
82void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells,\r
83 struct PicoEState *est);\r
7b802576 84void *blockcpy(void *dst, const void *src, size_t n);\r
cc68a136 85void blockcpy_or(void *dst, void *src, size_t n, int pat);\r
86#else\r
87// utility\r
88void blockcpy_or(void *dst, void *src, size_t n, int pat)\r
89{\r
90 unsigned char *pd = dst, *ps = src;\r
91 for (; n; n--)\r
92 *pd++ = (unsigned char) (*ps++ | pat);\r
93}\r
7b802576 94#define blockcpy memcpy\r
cc68a136 95#endif\r
96\r
ee3c39ef 97#define TileNormMaker_(pix_func) \\r
07abbab1 98{ \\r
1a08dec0 99 unsigned int t; \\r
07abbab1 100 \\r
1a08dec0 101 t = (pack&0x0000f000)>>12; pix_func(0); \\r
102 t = (pack&0x00000f00)>> 8; pix_func(1); \\r
103 t = (pack&0x000000f0)>> 4; pix_func(2); \\r
104 t = (pack&0x0000000f) ; pix_func(3); \\r
105 t = (pack&0xf0000000)>>28; pix_func(4); \\r
106 t = (pack&0x0f000000)>>24; pix_func(5); \\r
107 t = (pack&0x00f00000)>>20; pix_func(6); \\r
108 t = (pack&0x000f0000)>>16; pix_func(7); \\r
cc68a136 109}\r
110\r
ee3c39ef 111#define TileFlipMaker_(pix_func) \\r
07abbab1 112{ \\r
1a08dec0 113 unsigned int t; \\r
07abbab1 114 \\r
1a08dec0 115 t = (pack&0x000f0000)>>16; pix_func(0); \\r
116 t = (pack&0x00f00000)>>20; pix_func(1); \\r
117 t = (pack&0x0f000000)>>24; pix_func(2); \\r
118 t = (pack&0xf0000000)>>28; pix_func(3); \\r
119 t = (pack&0x0000000f) ; pix_func(4); \\r
120 t = (pack&0x000000f0)>> 4; pix_func(5); \\r
121 t = (pack&0x00000f00)>> 8; pix_func(6); \\r
122 t = (pack&0x0000f000)>>12; pix_func(7); \\r
cc68a136 123}\r
cc68a136 124\r
ee3c39ef 125#define TileNormMaker(funcname, pix_func) \\r
126static void funcname(unsigned char *pd, unsigned int pack, int pal) \\r
127TileNormMaker_(pix_func)\r
128\r
129#define TileFlipMaker(funcname, pix_func) \\r
130static void funcname(unsigned char *pd, unsigned int pack, int pal) \\r
131TileFlipMaker_(pix_func)\r
132\r
133#define TileNormMakerAS(funcname, pix_func) \\r
134static void funcname(unsigned char *pd, unsigned char *mb, unsigned int pack, int pal) \\r
135TileNormMaker_(pix_func)\r
136\r
137#define TileFlipMakerAS(funcname, pix_func) \\r
138static void funcname(unsigned char *pd, unsigned char *mb, unsigned int pack, int pal) \\r
139TileFlipMaker_(pix_func)\r
cc68a136 140\r
07abbab1 141#define pix_just_write(x) \\r
142 if (t) pd[x]=pal|t\r
cc68a136 143\r
07abbab1 144TileNormMaker(TileNorm,pix_just_write)\r
145TileFlipMaker(TileFlip,pix_just_write)\r
cc68a136 146\r
e352c3af 147#ifndef _ASM_DRAW_C\r
148\r
07abbab1 149// draw a sprite pixel, process operator colors\r
150#define pix_sh(x) \\r
151 if (!t); \\r
40644bfe 152 else if (t>=0xe) pd[x]=(pd[x]&0x3f)|(t<<6); /* c0 shadow, 80 hilight */ \\r
07abbab1 153 else pd[x]=pal|t\r
e5fa9817 154\r
07abbab1 155TileNormMaker(TileNormSH, pix_sh)\r
156TileFlipMaker(TileFlipSH, pix_sh)\r
cc68a136 157\r
40644bfe 158// draw a sprite pixel, mark operator colors\r
159#define pix_sh_markop(x) \\r
160 if (!t); \\r
161 else if (t>=0xe) pd[x]|=0x80; \\r
162 else pd[x]=pal|t\r
cc68a136 163\r
40644bfe 164TileNormMaker(TileNormSH_markop, pix_sh_markop)\r
165TileFlipMaker(TileFlipSH_markop, pix_sh_markop)\r
6c254710 166\r
ee3c39ef 167#endif\r
168\r
40644bfe 169// process operator pixels only, apply only on low pri tiles and other op pixels\r
07abbab1 170#define pix_sh_onlyop(x) \\r
40644bfe 171 if (t>=0xe && (pd[x]&0xc0)) \\r
172 pd[x]=(pd[x]&0x3f)|(t<<6); /* c0 shadow, 80 hilight */ \\r
cc68a136 173\r
ee3c39ef 174#ifndef _ASM_DRAW_C\r
175\r
07abbab1 176TileNormMaker(TileNormSH_onlyop_lp, pix_sh_onlyop)\r
177TileFlipMaker(TileFlipSH_onlyop_lp, pix_sh_onlyop)\r
cc68a136 178\r
e352c3af 179#endif\r
180\r
07abbab1 181// draw a sprite pixel (AS)\r
182#define pix_as(x) \\r
ee3c39ef 183 if (t & mb[x]) mb[x] = 0, pd[x] = pal | t\r
cc68a136 184\r
ee3c39ef 185TileNormMakerAS(TileNormAS, pix_as)\r
186TileFlipMakerAS(TileFlipAS, pix_as)\r
cc68a136 187\r
ee3c39ef 188// draw a sprite pixel, process operator colors (AS)\r
189#define pix_sh_as(x) \\r
190 if (t & mb[x]) { \\r
191 mb[x] = 0; \\r
192 if (t>=0xe) pd[x]=(pd[x]&0x3f)|(t<<6); /* c0 shadow, 80 hilight */ \\r
193 else pd[x] = pal | t; \\r
194 }\r
195\r
196TileNormMakerAS(TileNormSH_AS, pix_sh_as)\r
197TileFlipMakerAS(TileFlipSH_AS, pix_sh_as)\r
198\r
199#define pix_sh_as_onlyop(x) \\r
200 if (t & mb[x]) { \\r
201 mb[x] = 0; \\r
202 pix_sh_onlyop(x); \\r
203 }\r
cc68a136 204\r
ee3c39ef 205TileNormMakerAS(TileNormSH_AS_onlyop_lp, pix_sh_as_onlyop)\r
206TileFlipMakerAS(TileFlipSH_AS_onlyop_lp, pix_sh_as_onlyop)\r
cc68a136 207\r
07abbab1 208// mark pixel as sprite pixel (AS)\r
209#define pix_sh_as_onlymark(x) \\r
ee3c39ef 210 if (t) mb[x] = 0\r
cc68a136 211\r
ee3c39ef 212TileNormMakerAS(TileNormAS_onlymark, pix_sh_as_onlymark)\r
213TileFlipMakerAS(TileFlipAS_onlymark, pix_sh_as_onlymark)\r
cc68a136 214\r
ee3c39ef 215// forced both layer draw (through debug reg)\r
e0bcb7a9 216#define pix_and(x) \\r
217 pd[x] = (pd[x] & 0xc0) | (pd[x] & (pal | t))\r
218\r
219TileNormMaker(TileNorm_and, pix_and)\r
220TileFlipMaker(TileFlip_and, pix_and)\r
e5fa9817 221\r
cc68a136 222// --------------------------------------------\r
223\r
224#ifndef _ASM_DRAW_C\r
e0bcb7a9 225static void DrawStrip(struct TileStrip *ts, int lflags, int cellskip)\r
cc68a136 226{\r
ee3c39ef 227 unsigned char *pd = Pico.est.HighCol;\r
83c093a4 228 int tilex,dx,ty,code=0,addr=0,cells;\r
cc68a136 229 int oldcode=-1,blank=-1; // The tile we know is blank\r
83c093a4 230 int pal=0,sh;\r
cc68a136 231\r
232 // Draw tiles across screen:\r
e0bcb7a9 233 sh = (lflags & LF_SH) << 5; // 0x40\r
83c093a4 234 tilex=((-ts->hscroll)>>3)+cellskip;\r
cc68a136 235 ty=(ts->line&7)<<1; // Y-Offset into tile\r
236 dx=((ts->hscroll-1)&7)+1;\r
83c093a4 237 cells = ts->cells - cellskip;\r
cc68a136 238 if(dx != 8) cells++; // have hscroll, need to draw 1 cell more\r
83c093a4 239 dx+=cellskip<<3;\r
cc68a136 240\r
e0bcb7a9 241 for (; cells > 0; dx+=8, tilex++, cells--)\r
cc68a136 242 {\r
1a08dec0 243 unsigned int pack;\r
cc68a136 244\r
88fd63ad 245 code = PicoMem.vram[ts->nametab + (tilex & ts->xmask)];\r
e0bcb7a9 246 if (code == blank)\r
247 continue;\r
248 if ((code >> 15) | (lflags & LF_FORCE)) { // high priority tile\r
cc68a136 249 int cval = code | (dx<<16) | (ty<<25);\r
250 if(code&0x1000) cval^=7<<26;\r
251 *ts->hc++ = cval; // cache it\r
252 continue;\r
253 }\r
254\r
255 if (code!=oldcode) {\r
256 oldcode = code;\r
257 // Get tile address/2:\r
258 addr=(code&0x7ff)<<4;\r
259 addr+=ty;\r
260 if (code&0x1000) addr^=0xe; // Y-flip\r
261\r
83c093a4 262 pal=((code>>9)&0x30)|sh;\r
cc68a136 263 }\r
264\r
88fd63ad 265 pack = *(unsigned int *)(PicoMem.vram + addr);\r
1a08dec0 266 if (!pack) {\r
267 blank = code;\r
268 continue;\r
269 }\r
cc68a136 270\r
ee3c39ef 271 if (code & 0x0800) TileFlip(pd + dx, pack, pal);\r
272 else TileNorm(pd + dx, pack, pal);\r
cc68a136 273 }\r
274\r
275 // terminate the cache list\r
276 *ts->hc = 0;\r
740da8c6 277 // if oldcode wasn't changed, it means all layer is hi priority\r
ea38612f 278 if (oldcode == -1) Pico.est.rendstatus |= PDRAW_PLANE_HI_PRIO;\r
cc68a136 279}\r
cc68a136 280\r
281// this is messy\r
1a08dec0 282static void DrawStripVSRam(struct TileStrip *ts, int plane_sh, int cellskip)\r
cc68a136 283{\r
ee3c39ef 284 unsigned char *pd = Pico.est.HighCol;\r
83c093a4 285 int tilex,dx,code=0,addr=0,cell=0;\r
cc68a136 286 int oldcode=-1,blank=-1; // The tile we know is blank\r
ea38612f 287 int pal=0,scan=Pico.est.DrawScanline;\r
cc68a136 288\r
289 // Draw tiles across screen:\r
290 tilex=(-ts->hscroll)>>3;\r
291 dx=((ts->hscroll-1)&7)+1;\r
cf07a88d 292 if (ts->hscroll & 0x0f) {\r
293 int adj = ((ts->hscroll ^ dx) >> 3) & 1;\r
294 cell -= adj + 1;\r
295 ts->cells -= adj;\r
296 }\r
83c093a4 297 cell+=cellskip;\r
298 tilex+=cellskip;\r
299 dx+=cellskip<<3;\r
cc68a136 300\r
301 for (; cell < ts->cells; dx+=8,tilex++,cell++)\r
302 {\r
1a08dec0 303 int nametabadd, ty;\r
304 unsigned int pack;\r
cc68a136 305\r
83c093a4 306 //if((cell&1)==0)\r
307 {\r
cc68a136 308 int line,vscroll;\r
88fd63ad 309 vscroll=PicoMem.vsram[(plane_sh&1)+(cell&~1)];\r
cc68a136 310\r
311 // Find the line in the name table\r
312 line=(vscroll+scan)&ts->line&0xffff; // ts->line is really ymask ..\r
313 nametabadd=(line>>3)<<(ts->line>>24); // .. and shift[width]\r
314 ty=(line&7)<<1; // Y-Offset into tile\r
315 }\r
316\r
88fd63ad 317 code=PicoMem.vram[ts->nametab+nametabadd+(tilex&ts->xmask)];\r
cc68a136 318 if (code==blank) continue;\r
319 if (code>>15) { // high priority tile\r
320 int cval = code | (dx<<16) | (ty<<25);\r
321 if(code&0x1000) cval^=7<<26;\r
322 *ts->hc++ = cval; // cache it\r
323 continue;\r
324 }\r
325\r
326 if (code!=oldcode) {\r
327 oldcode = code;\r
328 // Get tile address/2:\r
329 addr=(code&0x7ff)<<4;\r
330 if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip\r
331\r
83c093a4 332 pal=((code>>9)&0x30)|((plane_sh<<5)&0x40);\r
cc68a136 333 }\r
334\r
88fd63ad 335 pack = *(unsigned int *)(PicoMem.vram + addr);\r
1a08dec0 336 if (!pack) {\r
337 blank = code;\r
338 continue;\r
339 }\r
cc68a136 340\r
ee3c39ef 341 if (code & 0x0800) TileFlip(pd + dx, pack, pal);\r
342 else TileNorm(pd + dx, pack, pal);\r
cc68a136 343 }\r
344\r
345 // terminate the cache list\r
346 *ts->hc = 0;\r
ea38612f 347 if (oldcode == -1) Pico.est.rendstatus |= PDRAW_PLANE_HI_PRIO;\r
cc68a136 348}\r
6d7acf9e 349#endif\r
cc68a136 350\r
351#ifndef _ASM_DRAW_C\r
352static\r
353#endif\r
354void DrawStripInterlace(struct TileStrip *ts)\r
355{\r
ee3c39ef 356 unsigned char *pd = Pico.est.HighCol;\r
cc68a136 357 int tilex=0,dx=0,ty=0,code=0,addr=0,cells;\r
358 int oldcode=-1,blank=-1; // The tile we know is blank\r
359 int pal=0;\r
360\r
361 // Draw tiles across screen:\r
362 tilex=(-ts->hscroll)>>3;\r
363 ty=(ts->line&15)<<1; // Y-Offset into tile\r
364 dx=((ts->hscroll-1)&7)+1;\r
365 cells = ts->cells;\r
366 if(dx != 8) cells++; // have hscroll, need to draw 1 cell more\r
367\r
368 for (; cells; dx+=8,tilex++,cells--)\r
369 {\r
1a08dec0 370 unsigned int pack;\r
cc68a136 371\r
88fd63ad 372 code = PicoMem.vram[ts->nametab + (tilex & ts->xmask)];\r
cc68a136 373 if (code==blank) continue;\r
374 if (code>>15) { // high priority tile\r
375 int cval = (code&0xfc00) | (dx<<16) | (ty<<25);\r
376 cval|=(code&0x3ff)<<1;\r
377 if(code&0x1000) cval^=0xf<<26;\r
378 *ts->hc++ = cval; // cache it\r
379 continue;\r
380 }\r
381\r
382 if (code!=oldcode) {\r
383 oldcode = code;\r
384 // Get tile address/2:\r
385 addr=(code&0x7ff)<<5;\r
386 if (code&0x1000) addr+=30-ty; else addr+=ty; // Y-flip\r
387\r
388// pal=Pico.cram+((code>>9)&0x30);\r
389 pal=((code>>9)&0x30);\r
390 }\r
391\r
88fd63ad 392 pack = *(unsigned int *)(PicoMem.vram + addr);\r
1a08dec0 393 if (!pack) {\r
394 blank = code;\r
395 continue;\r
396 }\r
cc68a136 397\r
ee3c39ef 398 if (code & 0x0800) TileFlip(pd + dx, pack, pal);\r
399 else TileNorm(pd + dx, pack, pal);\r
cc68a136 400 }\r
401\r
402 // terminate the cache list\r
403 *ts->hc = 0;\r
404}\r
405\r
406// --------------------------------------------\r
407\r
408#ifndef _ASM_DRAW_C\r
ea38612f 409static void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells,\r
410 struct PicoEState *est)\r
cc68a136 411{\r
412 struct PicoVideo *pvid=&Pico.video;\r
413 const char shift[4]={5,6,5,7}; // 32,64 or 128 sized tilemaps (2 is invalid)\r
414 struct TileStrip ts;\r
415 int width, height, ymask;\r
416 int vscroll, htab;\r
417\r
418 ts.hc=hcache;\r
419 ts.cells=maxcells;\r
420\r
421 // Work out the TileStrip to draw\r
422\r
423 // Work out the name table size: 32 64 or 128 tiles (0-3)\r
424 width=pvid->reg[16];\r
425 height=(width>>4)&3; width&=3;\r
426\r
427 ts.xmask=(1<<shift[width])-1; // X Mask in tiles (0x1f-0x7f)\r
428 ymask=(height<<8)|0xff; // Y Mask in pixels\r
eced0190 429 switch (width) {\r
430 case 1: ymask &= 0x1ff; break;\r
431 case 2: ymask = 0x007; break;\r
432 case 3: ymask = 0x0ff; break;\r
433 }\r
cc68a136 434\r
435 // Find name table:\r
83c093a4 436 if (plane_sh&1) ts.nametab=(pvid->reg[4]&0x07)<<12; // B\r
437 else ts.nametab=(pvid->reg[2]&0x38)<< 9; // A\r
cc68a136 438\r
439 htab=pvid->reg[13]<<9; // Horizontal scroll table address\r
ea38612f 440 if ( pvid->reg[11]&2) htab+=est->DrawScanline<<1; // Offset by line\r
cc68a136 441 if ((pvid->reg[11]&1)==0) htab&=~0xf; // Offset by tile\r
83c093a4 442 htab+=plane_sh&1; // A or B\r
cc68a136 443\r
444 // Get horizontal scroll value, will be masked later\r
88fd63ad 445 ts.hscroll = PicoMem.vram[htab & 0x7fff];\r
cc68a136 446\r
447 if((pvid->reg[12]&6) == 6) {\r
448 // interlace mode 2\r
88fd63ad 449 vscroll = PicoMem.vsram[plane_sh & 1]; // Get vertical scroll value\r
cc68a136 450\r
451 // Find the line in the name table\r
ea38612f 452 ts.line=(vscroll+(est->DrawScanline<<1))&((ymask<<1)|1);\r
cc68a136 453 ts.nametab+=(ts.line>>4)<<shift[width];\r
454\r
455 DrawStripInterlace(&ts);\r
456 } else if( pvid->reg[11]&4) {\r
457 // shit, we have 2-cell column based vscroll\r
458 // luckily this doesn't happen too often\r
459 ts.line=ymask|(shift[width]<<24); // save some stuff instead of line\r
83c093a4 460 DrawStripVSRam(&ts, plane_sh, cellskip);\r
cc68a136 461 } else {\r
88fd63ad 462 vscroll = PicoMem.vsram[plane_sh & 1]; // Get vertical scroll value\r
cc68a136 463\r
464 // Find the line in the name table\r
ea38612f 465 ts.line=(vscroll+est->DrawScanline)&ymask;\r
cc68a136 466 ts.nametab+=(ts.line>>3)<<shift[width];\r
467\r
83c093a4 468 DrawStrip(&ts, plane_sh, cellskip);\r
cc68a136 469 }\r
470}\r
471\r
472\r
473// --------------------------------------------\r
474\r
475// tstart & tend are tile pair numbers\r
ea38612f 476static void DrawWindow(int tstart, int tend, int prio, int sh,\r
477 struct PicoEState *est)\r
cc68a136 478{\r
ee3c39ef 479 unsigned char *pd = Pico.est.HighCol;\r
480 struct PicoVideo *pvid = &Pico.video;\r
07abbab1 481 int tilex,ty,nametab,code=0;\r
cc68a136 482 int blank=-1; // The tile we know is blank\r
483\r
484 // Find name table line:\r
485 if (pvid->reg[12]&1)\r
486 {\r
487 nametab=(pvid->reg[3]&0x3c)<<9; // 40-cell mode\r
ea38612f 488 nametab+=(est->DrawScanline>>3)<<6;\r
cc68a136 489 }\r
490 else\r
491 {\r
492 nametab=(pvid->reg[3]&0x3e)<<9; // 32-cell mode\r
ea38612f 493 nametab+=(est->DrawScanline>>3)<<5;\r
cc68a136 494 }\r
495\r
496 tilex=tstart<<1;\r
cc68a136 497\r
ea38612f 498 if (!(est->rendstatus & PDRAW_WND_DIFF_PRIO)) {\r
cc68a136 499 // check the first tile code\r
88fd63ad 500 code = PicoMem.vram[nametab + tilex];\r
cc68a136 501 // if the whole window uses same priority (what is often the case), we may be able to skip this field\r
602133e1 502 if ((code>>15) != prio) return;\r
cc68a136 503 }\r
504\r
07abbab1 505 tend<<=1;\r
ea38612f 506 ty=(est->DrawScanline&7)<<1; // Y-Offset into tile\r
07abbab1 507\r
cc68a136 508 // Draw tiles across screen:\r
81fda4e8 509 if (!sh)\r
cc68a136 510 {\r
81fda4e8 511 for (; tilex < tend; tilex++)\r
512 {\r
1a08dec0 513 unsigned int pack;\r
514 int dx, addr;\r
81fda4e8 515 int pal;\r
516\r
88fd63ad 517 code = PicoMem.vram[nametab + tilex];\r
602133e1 518 if (code==blank) continue;\r
519 if ((code>>15) != prio) {\r
ea38612f 520 est->rendstatus |= PDRAW_WND_DIFF_PRIO;\r
81fda4e8 521 continue;\r
522 }\r
cc68a136 523\r
81fda4e8 524 // Get tile address/2:\r
525 addr=(code&0x7ff)<<4;\r
526 if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip\r
527\r
88fd63ad 528 pack = *(unsigned int *)(PicoMem.vram + addr);\r
1a08dec0 529 if (!pack) {\r
530 blank = code;\r
531 continue;\r
532 }\r
533\r
534 pal = ((code >> 9) & 0x30);\r
535 dx = 8 + (tilex << 3);\r
81fda4e8 536\r
ee3c39ef 537 if (code & 0x0800) TileFlip(pd + dx, pack, pal);\r
538 else TileNorm(pd + dx, pack, pal);\r
cc68a136 539 }\r
81fda4e8 540 }\r
541 else\r
542 {\r
543 for (; tilex < tend; tilex++)\r
544 {\r
1a08dec0 545 unsigned int pack;\r
546 int dx, addr;\r
07abbab1 547 int pal;\r
81fda4e8 548\r
88fd63ad 549 code = PicoMem.vram[nametab + tilex];\r
81fda4e8 550 if(code==blank) continue;\r
551 if((code>>15) != prio) {\r
ea38612f 552 est->rendstatus |= PDRAW_WND_DIFF_PRIO;\r
81fda4e8 553 continue;\r
554 }\r
cc68a136 555\r
81fda4e8 556 pal=((code>>9)&0x30);\r
cc68a136 557\r
07abbab1 558 if (prio) {\r
99bdfd31 559 int *zb = (int *)(est->HighCol+8+(tilex<<3));\r
bfa12428 560 *zb++ &= 0xbfbfbfbf;\r
561 *zb &= 0xbfbfbfbf;\r
cc68a136 562 } else {\r
563 pal |= 0x40;\r
564 }\r
cc68a136 565\r
81fda4e8 566 // Get tile address/2:\r
567 addr=(code&0x7ff)<<4;\r
568 if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip\r
cc68a136 569\r
88fd63ad 570 pack = *(unsigned int *)(PicoMem.vram + addr);\r
1a08dec0 571 if (!pack) {\r
572 blank = code;\r
573 continue;\r
574 }\r
575\r
576 dx = 8 + (tilex << 3);\r
cc68a136 577\r
ee3c39ef 578 if (code & 0x0800) TileFlip(pd + dx, pack, pal);\r
579 else TileNorm(pd + dx, pack, pal);\r
81fda4e8 580 }\r
cc68a136 581 }\r
cc68a136 582}\r
583\r
584// --------------------------------------------\r
585\r
81fda4e8 586static void DrawTilesFromCacheShPrep(void)\r
587{\r
602133e1 588 // as some layer has covered whole line with hi priority tiles,\r
40644bfe 589 // we can process whole line and then act as if sh/hi mode was off,\r
590 // but leave lo pri op sprite markers alone\r
99bdfd31 591 int c = 320/4, *zb = (int *)(Pico.est.HighCol+8);\r
ea38612f 592 Pico.est.rendstatus |= PDRAW_SHHI_DONE;\r
602133e1 593 while (c--)\r
81fda4e8 594 {\r
40644bfe 595 *zb++ &= 0xbfbfbfbf;\r
81fda4e8 596 }\r
597}\r
598\r
ea38612f 599static void DrawTilesFromCache(int *hc, int sh, int rlim, struct PicoEState *est)\r
cc68a136 600{\r
ee3c39ef 601 unsigned char *pd = Pico.est.HighCol;\r
740da8c6 602 int code, addr, dx;\r
1a08dec0 603 unsigned int pack;\r
cc68a136 604 int pal;\r
cc68a136 605\r
606 // *ts->hc++ = code | (dx<<16) | (ty<<25); // cache it\r
607\r
ea38612f 608 if (sh && (est->rendstatus & (PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO)))\r
740da8c6 609 {\r
ea38612f 610 if (!(est->rendstatus & PDRAW_SHHI_DONE))\r
602133e1 611 DrawTilesFromCacheShPrep();\r
740da8c6 612 sh = 0;\r
613 }\r
cc68a136 614\r
81fda4e8 615 if (!sh)\r
740da8c6 616 {\r
81fda4e8 617 short blank=-1; // The tile we know is blank\r
7a7c6476 618 while ((code=*hc++)) {\r
e0bcb7a9 619 if (!(code & 0x8000) || (short)code == blank)\r
620 continue;\r
740da8c6 621 // Get tile address/2:\r
e0bcb7a9 622 addr = (code & 0x7ff) << 4;\r
623 addr += code >> 25; // y offset into tile\r
740da8c6 624\r
88fd63ad 625 pack = *(unsigned int *)(PicoMem.vram + addr);\r
1a08dec0 626 if (!pack) {\r
627 blank = (short)code;\r
628 continue;\r
629 }\r
740da8c6 630\r
1a08dec0 631 dx = (code >> 16) & 0x1ff;\r
632 pal = ((code >> 9) & 0x30);\r
633 if (rlim-dx < 0)\r
634 goto last_cut_tile;\r
81fda4e8 635\r
ee3c39ef 636 if (code & 0x0800) TileFlip(pd + dx, pack, pal);\r
637 else TileNorm(pd + dx, pack, pal);\r
cc68a136 638 }\r
740da8c6 639 }\r
640 else\r
641 {\r
7a7c6476 642 while ((code=*hc++)) {\r
81fda4e8 643 unsigned char *zb;\r
1a08dec0 644\r
740da8c6 645 // Get tile address/2:\r
646 addr=(code&0x7ff)<<4;\r
647 addr+=(unsigned int)code>>25; // y offset into tile\r
648 dx=(code>>16)&0x1ff;\r
99bdfd31 649 zb = est->HighCol+dx;\r
40644bfe 650 *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf;\r
651 *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf;\r
cc68a136 652\r
88fd63ad 653 pack = *(unsigned int *)(PicoMem.vram + addr);\r
1a08dec0 654 if (!pack)\r
655 continue;\r
656\r
657 pal = ((code >> 9) & 0x30);\r
658 if (rlim - dx < 0)\r
659 goto last_cut_tile;\r
cc68a136 660\r
ee3c39ef 661 if (code & 0x0800) TileFlip(pd + dx, pack, pal);\r
662 else TileNorm(pd + dx, pack, pal);\r
7a7c6476 663 }\r
664 }\r
665 return;\r
666\r
667last_cut_tile:\r
1a08dec0 668 // for vertical window cutoff\r
7a7c6476 669 {\r
1a08dec0 670 unsigned int t;\r
671\r
ee3c39ef 672 pd += dx;\r
7a7c6476 673 if (code&0x0800)\r
674 {\r
675 switch (rlim-dx+8)\r
676 {\r
677 case 7: t=pack&0x00000f00; if (t) pd[6]=(unsigned char)(pal|(t>> 8)); // "break" is left out intentionally\r
678 case 6: t=pack&0x000000f0; if (t) pd[5]=(unsigned char)(pal|(t>> 4));\r
679 case 5: t=pack&0x0000000f; if (t) pd[4]=(unsigned char)(pal|(t ));\r
680 case 4: t=pack&0xf0000000; if (t) pd[3]=(unsigned char)(pal|(t>>28));\r
681 case 3: t=pack&0x0f000000; if (t) pd[2]=(unsigned char)(pal|(t>>24));\r
682 case 2: t=pack&0x00f00000; if (t) pd[1]=(unsigned char)(pal|(t>>20));\r
683 case 1: t=pack&0x000f0000; if (t) pd[0]=(unsigned char)(pal|(t>>16));\r
684 default: break;\r
685 }\r
686 }\r
687 else\r
688 {\r
689 switch (rlim-dx+8)\r
690 {\r
691 case 7: t=pack&0x00f00000; if (t) pd[6]=(unsigned char)(pal|(t>>20));\r
947fb5f9 692 case 6: t=pack&0x0f000000; if (t) pd[5]=(unsigned char)(pal|(t>>24));\r
693 case 5: t=pack&0xf0000000; if (t) pd[4]=(unsigned char)(pal|(t>>28));\r
694 case 4: t=pack&0x0000000f; if (t) pd[3]=(unsigned char)(pal|(t ));\r
695 case 3: t=pack&0x000000f0; if (t) pd[2]=(unsigned char)(pal|(t>> 4));\r
696 case 2: t=pack&0x00000f00; if (t) pd[1]=(unsigned char)(pal|(t>> 8));\r
697 case 1: t=pack&0x0000f000; if (t) pd[0]=(unsigned char)(pal|(t>>12));\r
698 default: break;\r
7a7c6476 699 }\r
740da8c6 700 }\r
cc68a136 701 }\r
702}\r
703\r
704// --------------------------------------------\r
705\r
706// Index + 0 : hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size\r
707// Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8\r
708\r
97a7f774 709static void DrawSprite(int *sprite, int sh)\r
cc68a136 710{\r
ee3c39ef 711 void (*fTileFunc)(unsigned char *pd, unsigned int pack, int pal);\r
712 unsigned char *pd = Pico.est.HighCol;\r
cc68a136 713 int width=0,height=0;\r
714 int row=0,code=0;\r
715 int pal;\r
716 int tile=0,delta=0;\r
717 int sx, sy;\r
cc68a136 718\r
719 // parse the sprite data\r
720 sy=sprite[0];\r
721 code=sprite[1];\r
722 sx=code>>16; // X\r
723 width=sy>>28;\r
724 height=(sy>>24)&7; // Width and height in tiles\r
725 sy=(sy<<16)>>16; // Y\r
726\r
ea38612f 727 row=Pico.est.DrawScanline-sy; // Row of the sprite we are on\r
cc68a136 728\r
729 if (code&0x1000) row=(height<<3)-1-row; // Flip Y\r
730\r
07abbab1 731 tile=code + (row>>3); // Tile number increases going down\r
cc68a136 732 delta=height; // Delta to increase tile by going right\r
733 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X\r
734\r
07abbab1 735 tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address\r
ee4f03ae 736 delta<<=4; // Delta of address\r
07abbab1 737\r
738 pal=(code>>9)&0x30;\r
97a7f774 739 pal|=sh<<6;\r
e5fa9817 740\r
741 if (sh && (code&0x6000) == 0x6000) {\r
40644bfe 742 if(code&0x0800) fTileFunc=TileFlipSH_markop;\r
743 else fTileFunc=TileNormSH_markop;\r
e5fa9817 744 } else {\r
745 if(code&0x0800) fTileFunc=TileFlip;\r
746 else fTileFunc=TileNorm;\r
cc68a136 747 }\r
748\r
749 for (; width; width--,sx+=8,tile+=delta)\r
750 {\r
1a08dec0 751 unsigned int pack;\r
752\r
cc68a136 753 if(sx<=0) continue;\r
754 if(sx>=328) break; // Offscreen\r
755\r
88fd63ad 756 pack = *(unsigned int *)(PicoMem.vram + (tile & 0x7fff));\r
ee3c39ef 757 fTileFunc(pd + sx, pack, pal);\r
cc68a136 758 }\r
759}\r
e5fa9817 760#endif\r
cc68a136 761\r
e0bcb7a9 762static NOINLINE void DrawTilesFromCacheForced(const int *hc)\r
763{\r
ee3c39ef 764 unsigned char *pd = Pico.est.HighCol;\r
e0bcb7a9 765 int code, addr, dx;\r
766 unsigned int pack;\r
767 int pal;\r
768\r
769 // *ts->hc++ = code | (dx<<16) | (ty<<25);\r
770 while ((code = *hc++)) {\r
771 // Get tile address/2:\r
772 addr = (code & 0x7ff) << 4;\r
773 addr += (code >> 25) & 0x0e; // y offset into tile\r
774\r
775 dx = (code >> 16) & 0x1ff;\r
776 pal = ((code >> 9) & 0x30);\r
88fd63ad 777 pack = *(unsigned int *)(PicoMem.vram + addr);\r
e0bcb7a9 778\r
ee3c39ef 779 if (code & 0x0800) TileFlip_and(pd + dx, pack, pal);\r
780 else TileNorm_and(pd + dx, pack, pal);\r
e0bcb7a9 781 }\r
782}\r
783\r
cc68a136 784static void DrawSpriteInterlace(unsigned int *sprite)\r
785{\r
ee3c39ef 786 unsigned char *pd = Pico.est.HighCol;\r
cc68a136 787 int width=0,height=0;\r
788 int row=0,code=0;\r
789 int pal;\r
790 int tile=0,delta=0;\r
791 int sx, sy;\r
792\r
793 // parse the sprite data\r
794 sy=sprite[0];\r
795 height=sy>>24;\r
796 sy=(sy&0x3ff)-0x100; // Y\r
797 width=(height>>2)&3; height&=3;\r
798 width++; height++; // Width and height in tiles\r
799\r
ea38612f 800 row=(Pico.est.DrawScanline<<1)-sy; // Row of the sprite we are on\r
cc68a136 801\r
802 code=sprite[1];\r
803 sx=((code>>16)&0x1ff)-0x78; // X\r
804\r
805 if (code&0x1000) row^=(16<<height)-1; // Flip Y\r
806\r
807 tile=code&0x3ff; // Tile number\r
808 tile+=row>>4; // Tile number increases going down\r
809 delta=height; // Delta to increase tile by going right\r
810 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X\r
811\r
812 tile<<=5; tile+=(row&15)<<1; // Tile address\r
813\r
814 delta<<=5; // Delta of address\r
815 pal=((code>>9)&0x30); // Get palette pointer\r
816\r
817 for (; width; width--,sx+=8,tile+=delta)\r
818 {\r
1a08dec0 819 unsigned int pack;\r
820\r
cc68a136 821 if(sx<=0) continue;\r
822 if(sx>=328) break; // Offscreen\r
823\r
88fd63ad 824 pack = *(unsigned int *)(PicoMem.vram + (tile & 0x7fff));\r
ee3c39ef 825 if (code & 0x0800) TileFlip(pd + sx, pack, pal);\r
826 else TileNorm(pd + sx, pack, pal);\r
cc68a136 827 }\r
828}\r
829\r
830\r
e0bcb7a9 831static NOINLINE void DrawAllSpritesInterlace(int pri, int sh)\r
cc68a136 832{\r
833 struct PicoVideo *pvid=&Pico.video;\r
ea38612f 834 int i,u,table,link=0,sline=Pico.est.DrawScanline<<1;\r
cc68a136 835 unsigned int *sprites[80]; // Sprite index\r
836\r
837 table=pvid->reg[5]&0x7f;\r
838 if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode\r
839 table<<=8; // Get sprite table address/2\r
840\r
841 for (i=u=0; u < 80 && i < 21; u++)\r
842 {\r
843 unsigned int *sprite;\r
844 int code, sx, sy, height;\r
845\r
88fd63ad 846 sprite=(unsigned int *)(PicoMem.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
cc68a136 847\r
848 // get sprite info\r
849 code = sprite[0];\r
850 sx = sprite[1];\r
851 if(((sx>>15)&1) != pri) goto nextsprite; // wrong priority sprite\r
852\r
853 // check if it is on this line\r
854 sy = (code&0x3ff)-0x100;\r
855 height = (((code>>24)&3)+1)<<4;\r
856 if(sline < sy || sline >= sy+height) goto nextsprite; // no\r
857\r
858 // check if sprite is not hidden offscreen\r
859 sx = (sx>>16)&0x1ff;\r
860 sx -= 0x78; // Get X coordinate + 8\r
947fb5f9 861 if(sx <= -8*3 || sx >= 328) goto nextsprite;\r
cc68a136 862\r
863 // sprite is good, save it's pointer\r
864 sprites[i++]=sprite;\r
865\r
866 nextsprite:\r
867 // Find next sprite\r
868 link=(code>>16)&0x7f;\r
869 if(!link) break; // End of sprites\r
870 }\r
871\r
872 // Go through sprites backwards:\r
873 for (i-- ;i>=0; i--)\r
874 DrawSpriteInterlace(sprites[i]);\r
875}\r
876\r
877\r
878#ifndef _ASM_DRAW_C\r
40644bfe 879/*\r
880 * s/h drawing: lo_layers|40, lo_sprites|40 && mark_op,\r
881 * hi_layers&=~40, hi_sprites\r
882 *\r
883 * Index + 0 : hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size\r
884 * Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8\r
885 */\r
ea38612f 886static void DrawSpritesSHi(unsigned char *sprited, const struct PicoEState *est)\r
cc68a136 887{\r
ee3c39ef 888 void (*fTileFunc)(unsigned char *pd, unsigned int pack, int pal);\r
889 unsigned char *pd = Pico.est.HighCol;\r
ee4f03ae 890 unsigned char *p;\r
891 int cnt;\r
892\r
fbc65db7 893 cnt = sprited[0] & 0x7f;\r
ee4f03ae 894 if (cnt == 0) return;\r
cc68a136 895\r
fbc65db7 896 p = &sprited[3];\r
cc68a136 897\r
ee4f03ae 898 // Go through sprites backwards:\r
899 for (cnt--; cnt >= 0; cnt--)\r
07abbab1 900 {\r
ee4f03ae 901 int *sprite, code, pal, tile, sx, sy;\r
902 int offs, delta, width, height, row;\r
903\r
904 offs = (p[cnt] & 0x7f) * 2;\r
99bdfd31 905 sprite = est->HighPreSpr + offs;\r
ee4f03ae 906 code = sprite[1];\r
907 pal = (code>>9)&0x30;\r
cc68a136 908\r
fbc65db7 909 if (pal == 0x30)\r
07abbab1 910 {\r
ee4f03ae 911 if (code & 0x8000) // hi priority\r
07abbab1 912 {\r
ee4f03ae 913 if (code&0x800) fTileFunc=TileFlipSH;\r
914 else fTileFunc=TileNormSH;\r
07abbab1 915 } else {\r
ee4f03ae 916 if (code&0x800) fTileFunc=TileFlipSH_onlyop_lp;\r
917 else fTileFunc=TileNormSH_onlyop_lp;\r
07abbab1 918 }\r
cc68a136 919 } else {\r
ee4f03ae 920 if (!(code & 0x8000)) continue; // non-operator low sprite, already drawn\r
921 if (code&0x800) fTileFunc=TileFlip;\r
922 else fTileFunc=TileNorm;\r
cc68a136 923 }\r
924\r
ee4f03ae 925 // parse remaining sprite data\r
926 sy=sprite[0];\r
927 sx=code>>16; // X\r
928 width=sy>>28;\r
929 height=(sy>>24)&7; // Width and height in tiles\r
930 sy=(sy<<16)>>16; // Y\r
931\r
ea38612f 932 row=est->DrawScanline-sy; // Row of the sprite we are on\r
ee4f03ae 933\r
934 if (code&0x1000) row=(height<<3)-1-row; // Flip Y\r
935\r
936 tile=code + (row>>3); // Tile number increases going down\r
937 delta=height; // Delta to increase tile by going right\r
938 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X\r
939\r
940 tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address\r
941 delta<<=4; // Delta of address\r
942\r
cc68a136 943 for (; width; width--,sx+=8,tile+=delta)\r
944 {\r
1a08dec0 945 unsigned int pack;\r
946\r
cc68a136 947 if(sx<=0) continue;\r
948 if(sx>=328) break; // Offscreen\r
949\r
88fd63ad 950 pack = *(unsigned int *)(PicoMem.vram + (tile & 0x7fff));\r
ee3c39ef 951 fTileFunc(pd + sx, pack, pal);\r
cc68a136 952 }\r
953 }\r
954}\r
f8af9634 955#endif // !_ASM_DRAW_C\r
cc68a136 956\r
fbc65db7 957static void DrawSpritesHiAS(unsigned char *sprited, int sh)\r
e5fa9817 958{\r
ee3c39ef 959 void (*fTileFunc)(unsigned char *pd, unsigned char *mb,\r
960 unsigned int pack, int pal);\r
961 unsigned char *pd = Pico.est.HighCol;\r
962 unsigned char mb[8+320+8];\r
ee4f03ae 963 unsigned char *p;\r
ee3c39ef 964 int entry, cnt;\r
ee4f03ae 965\r
fbc65db7 966 cnt = sprited[0] & 0x7f;\r
ee4f03ae 967 if (cnt == 0) return;\r
e5fa9817 968\r
ee3c39ef 969 memset(mb, 0xff, sizeof(mb));\r
fbc65db7 970 p = &sprited[3];\r
e5fa9817 971\r
ee4f03ae 972 // Go through sprites:\r
973 for (entry = 0; entry < cnt; entry++)\r
07abbab1 974 {\r
ee4f03ae 975 int *sprite, code, pal, tile, sx, sy;\r
976 int offs, delta, width, height, row;\r
977\r
978 offs = (p[entry] & 0x7f) * 2;\r
979 sprite = HighPreSpr + offs;\r
980 code = sprite[1];\r
981 pal = (code>>9)&0x30;\r
982\r
ee3c39ef 983 if (sh && pal == 0x30)\r
07abbab1 984 {\r
ee3c39ef 985 if (code & 0x8000) // hi priority\r
07abbab1 986 {\r
ee3c39ef 987 if (code&0x800) fTileFunc = TileFlipSH_AS;\r
988 else fTileFunc = TileNormSH_AS;\r
07abbab1 989 } else {\r
ee3c39ef 990 if (code&0x800) fTileFunc = TileFlipSH_AS_onlyop_lp;\r
991 else fTileFunc = TileNormSH_AS_onlyop_lp;\r
07abbab1 992 }\r
e5fa9817 993 } else {\r
ee3c39ef 994 if (code & 0x8000) // hi priority\r
995 {\r
996 if (code&0x800) fTileFunc = TileFlipAS;\r
997 else fTileFunc = TileNormAS;\r
998 } else {\r
999 if (code&0x800) fTileFunc = TileFlipAS_onlymark;\r
1000 else fTileFunc = TileNormAS_onlymark;\r
1001 }\r
e5fa9817 1002 }\r
ee4f03ae 1003\r
1004 // parse remaining sprite data\r
1005 sy=sprite[0];\r
1006 sx=code>>16; // X\r
1007 width=sy>>28;\r
1008 height=(sy>>24)&7; // Width and height in tiles\r
1009 sy=(sy<<16)>>16; // Y\r
1010\r
ea38612f 1011 row=Pico.est.DrawScanline-sy; // Row of the sprite we are on\r
ee4f03ae 1012\r
1013 if (code&0x1000) row=(height<<3)-1-row; // Flip Y\r
1014\r
1015 tile=code + (row>>3); // Tile number increases going down\r
1016 delta=height; // Delta to increase tile by going right\r
1017 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X\r
1018\r
1019 tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address\r
1020 delta<<=4; // Delta of address\r
e5fa9817 1021\r
07abbab1 1022 for (; width; width--,sx+=8,tile+=delta)\r
1023 {\r
1a08dec0 1024 unsigned int pack;\r
1025\r
07abbab1 1026 if(sx<=0) continue;\r
1027 if(sx>=328) break; // Offscreen\r
1028\r
88fd63ad 1029 pack = *(unsigned int *)(PicoMem.vram + (tile & 0x7fff));\r
ee3c39ef 1030 fTileFunc(pd + sx, mb + sx, pack, pal);\r
07abbab1 1031 }\r
1032 }\r
e5fa9817 1033}\r
1034\r
cc68a136 1035\r
1036// Index + 0 : ----hhvv -lllllll -------y yyyyyyyy\r
1037// Index + 4 : -------x xxxxxxxx pccvhnnn nnnnnnnn\r
1038// v\r
d9fc2fe1 1039// Index + 0 : hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size\r
cc68a136 1040// Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8\r
1041\r
6d8782a1 1042static NOINLINE void PrepareSprites(int full)\r
cc68a136 1043{\r
ea38612f 1044 const struct PicoVideo *pvid=&Pico.video;\r
1045 const struct PicoEState *est=&Pico.est;\r
fbc65db7 1046 int u,link=0,sh;\r
cc68a136 1047 int table=0;\r
1048 int *pd = HighPreSpr;\r
947fb5f9 1049 int max_lines = 224, max_sprites = 80, max_width = 328;\r
d9fc2fe1 1050 int max_line_sprites = 20; // 20 sprites, 40 tiles\r
1051\r
1052 if (!(Pico.video.reg[12]&1))\r
947fb5f9 1053 max_sprites = 64, max_line_sprites = 16, max_width = 264;\r
93f9619e 1054 if (PicoIn.opt & POPT_DIS_SPRITE_LIM)\r
d9fc2fe1 1055 max_line_sprites = MAX_LINE_SPRITES;\r
1056\r
1057 if (pvid->reg[1]&8) max_lines = 240;\r
fbc65db7 1058 sh = Pico.video.reg[0xC]&8; // shadow/hilight?\r
cc68a136 1059\r
1060 table=pvid->reg[5]&0x7f;\r
1061 if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode\r
1062 table<<=8; // Get sprite table address/2\r
1063\r
1064 if (!full)\r
1065 {\r
1066 int pack;\r
1067 // updates: tilecode, sx\r
4c436138 1068 for (u=0; u < max_sprites && (pack = *pd); u++, pd+=2)\r
cc68a136 1069 {\r
1070 unsigned int *sprite;\r
d9fc2fe1 1071 int code2, sx, sy, height;\r
cc68a136 1072\r
88fd63ad 1073 sprite=(unsigned int *)(PicoMem.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
cc68a136 1074\r
1075 // parse sprite info\r
cc68a136 1076 code2 = sprite[1];\r
d9fc2fe1 1077 sx = (code2>>16)&0x1ff;\r
1078 sx -= 0x78; // Get X coordinate + 8\r
1079 sy = (pack << 16) >> 16;\r
947fb5f9 1080 height = (pack >> 24) & 0xf;\r
cc68a136 1081\r
ea38612f 1082 if (sy < max_lines &&\r
1083 sy + (height<<3) > est->DrawScanline && // sprite onscreen (y)?\r
947fb5f9 1084 (sx > -24 || sx < max_width)) // onscreen x\r
d9fc2fe1 1085 {\r
ea38612f 1086 int y = (sy >= est->DrawScanline) ? sy : est->DrawScanline;\r
ee4f03ae 1087 int entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80);\r
d9fc2fe1 1088 for (; y < sy + (height<<3) && y < max_lines; y++)\r
1089 {\r
947fb5f9 1090 int i, cnt;\r
d9fc2fe1 1091 cnt = HighLnSpr[y][0] & 0x7f;\r
1092 if (cnt >= max_line_sprites) continue; // sprite limit?\r
d9fc2fe1 1093\r
1094 for (i = 0; i < cnt; i++)\r
fbc65db7 1095 if (((HighLnSpr[y][3+i] ^ entry) & 0x7f) == 0) goto found;\r
d9fc2fe1 1096\r
1097 // this sprite was previously missing\r
fbc65db7 1098 HighLnSpr[y][3+cnt] = entry;\r
d9fc2fe1 1099 HighLnSpr[y][0] = cnt + 1;\r
1100found:;\r
e352c3af 1101 if (entry & 0x80)\r
fbc65db7 1102 HighLnSpr[y][1] |= SPRL_HAVE_HI;\r
1103 else HighLnSpr[y][1] |= SPRL_HAVE_LO;\r
d9fc2fe1 1104 }\r
cc68a136 1105 }\r
1106\r
d9fc2fe1 1107 code2 &= ~0xfe000000;\r
1108 code2 -= 0x00780000; // Get X coordinate + 8 in upper 16 bits\r
1109 pd[1] = code2;\r
cc68a136 1110\r
1111 // Find next sprite\r
d9fc2fe1 1112 link=(sprite[0]>>16)&0x7f;\r
1113 if (!link) break; // End of sprites\r
cc68a136 1114 }\r
cc68a136 1115 }\r
1116 else\r
1117 {\r
d9fc2fe1 1118 for (u = 0; u < max_lines; u++)\r
1119 *((int *)&HighLnSpr[u][0]) = 0;\r
1120\r
4c436138 1121 for (u = 0; u < max_sprites; u++)\r
cc68a136 1122 {\r
1123 unsigned int *sprite;\r
d9fc2fe1 1124 int code, code2, sx, sy, hv, height, width;\r
cc68a136 1125\r
88fd63ad 1126 sprite=(unsigned int *)(PicoMem.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
cc68a136 1127\r
1128 // parse sprite info\r
1129 code = sprite[0];\r
1130 sy = (code&0x1ff)-0x80;\r
1131 hv = (code>>24)&0xf;\r
1132 height = (hv&3)+1;\r
1133\r
cc68a136 1134 width = (hv>>2)+1;\r
1135 code2 = sprite[1];\r
1136 sx = (code2>>16)&0x1ff;\r
1137 sx -= 0x78; // Get X coordinate + 8\r
6d7acf9e 1138\r
ea38612f 1139 if (sy < max_lines && sy + (height<<3) > est->DrawScanline) // sprite onscreen (y)?\r
d9fc2fe1 1140 {\r
fbc65db7 1141 int entry, y, sx_min, onscr_x, maybe_op = 0;\r
1142\r
1143 sx_min = 8-(width<<3);\r
1144 onscr_x = sx_min < sx && sx < max_width;\r
1145 if (sh && (code2 & 0x6000) == 0x6000)\r
1146 maybe_op = SPRL_MAY_HAVE_OP;\r
1147\r
fbc65db7 1148 entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80);\r
ea38612f 1149 y = (sy >= est->DrawScanline) ? sy : est->DrawScanline;\r
d9fc2fe1 1150 for (; y < sy + (height<<3) && y < max_lines; y++)\r
1151 {\r
0fc0e241 1152 unsigned char *p = &HighLnSpr[y][0];\r
1153 int cnt = p[0];\r
d9fc2fe1 1154 if (cnt >= max_line_sprites) continue; // sprite limit?\r
1155\r
0fc0e241 1156 if (p[2] >= max_line_sprites*2) { // tile limit?\r
1157 p[0] |= 0x80;\r
947fb5f9 1158 continue;\r
d9fc2fe1 1159 }\r
0fc0e241 1160 p[2] += width;\r
d9fc2fe1 1161\r
1162 if (sx == -0x78) {\r
1163 if (cnt > 0)\r
0fc0e241 1164 p[0] |= 0x80; // masked, no more sprites for this line\r
947fb5f9 1165 continue;\r
d9fc2fe1 1166 }\r
1167 // must keep the first sprite even if it's offscreen, for masking\r
fbc65db7 1168 if (cnt > 0 && !onscr_x) continue; // offscreen x\r
d9fc2fe1 1169\r
0fc0e241 1170 p[3+cnt] = entry;\r
1171 p[0] = cnt + 1;\r
1172 p[1] |= (entry & 0x80) ? SPRL_HAVE_HI : SPRL_HAVE_LO;\r
1173 p[1] |= maybe_op; // there might be op sprites on this line\r
1174 if (cnt > 0 && (code2 & 0x8000) && !(p[3+cnt-1]&0x80))\r
1175 p[1] |= SPRL_LO_ABOVE_HI;\r
d9fc2fe1 1176 }\r
cc68a136 1177 }\r
6d7acf9e 1178\r
d9fc2fe1 1179 *pd++ = (width<<28)|(height<<24)|(hv<<16)|((unsigned short)sy);\r
cc68a136 1180 *pd++ = (sx<<16)|((unsigned short)code2);\r
6d7acf9e 1181\r
cc68a136 1182 // Find next sprite\r
1183 link=(code>>16)&0x7f;\r
d9fc2fe1 1184 if (!link) break; // End of sprites\r
cc68a136 1185 }\r
d9fc2fe1 1186 *pd = 0;\r
1187\r
1188#if 0\r
1189 for (u = 0; u < max_lines; u++)\r
1190 {\r
1191 int y;\r
fbc65db7 1192 printf("c%03i: %2i, %2i: ", u, HighLnSpr[u][0] & 0x7f, HighLnSpr[u][2]);\r
d9fc2fe1 1193 for (y = 0; y < HighLnSpr[u][0] & 0x7f; y++)\r
fbc65db7 1194 printf(" %i", HighLnSpr[u][y+3]);\r
d9fc2fe1 1195 printf("\n");\r
1196 }\r
1197#endif\r
cc68a136 1198 }\r
1199}\r
1200\r
283fec1b 1201#ifndef _ASM_DRAW_C\r
ea38612f 1202static void DrawAllSprites(unsigned char *sprited, int prio, int sh,\r
1203 struct PicoEState *est)\r
cc68a136 1204{\r
d9fc2fe1 1205 unsigned char *p;\r
97a7f774 1206 int cnt;\r
cc68a136 1207\r
fbc65db7 1208 cnt = sprited[0] & 0x7f;\r
ee4f03ae 1209 if (cnt == 0) return;\r
cc68a136 1210\r
fbc65db7 1211 p = &sprited[3];\r
cc68a136 1212\r
ee4f03ae 1213 // Go through sprites backwards:\r
1214 for (cnt--; cnt >= 0; cnt--)\r
1215 {\r
1216 int offs;\r
97a7f774 1217 if ((p[cnt] >> 7) != prio) continue;\r
ee4f03ae 1218 offs = (p[cnt]&0x7f) * 2;\r
97a7f774 1219 DrawSprite(HighPreSpr + offs, sh);\r
cc68a136 1220 }\r
cc68a136 1221}\r
1222\r
1223\r
1224// --------------------------------------------\r
1225\r
99bdfd31 1226void BackFill(int reg7, int sh, struct PicoEState *est)\r
cc68a136 1227{\r
b542be46 1228 unsigned int back;\r
cc68a136 1229\r
1230 // Start with a blank scanline (background colour):\r
1231 back=reg7&0x3f;\r
1232 back|=sh<<6;\r
1233 back|=back<<8;\r
1234 back|=back<<16;\r
1235\r
99bdfd31 1236 memset32((int *)(est->HighCol+8), back, 320/4);\r
cc68a136 1237}\r
1238#endif\r
1239\r
1240// --------------------------------------------\r
1241\r
b2305d08 1242#ifndef _ASM_DRAW_C\r
ea38612f 1243void PicoDoHighPal555(int sh, int line, struct PicoEState *est)\r
e55f0cbb 1244{\r
19954be1 1245 unsigned int *spal, *dpal;\r
a39d8ba5 1246 unsigned int t, i;\r
e55f0cbb 1247\r
1248 Pico.m.dirtyPal = 0;\r
1249\r
88fd63ad 1250 spal = (void *)PicoMem.cram;\r
98a27142 1251 dpal = (void *)est->HighPal;\r
19954be1 1252\r
a39d8ba5 1253 for (i = 0; i < 0x40 / 2; i++) {\r
1254 t = spal[i];\r
e55f0cbb 1255#ifdef USE_BGR555\r
19954be1 1256 t = ((t & 0x000e000e)<< 1) | ((t & 0x00e000e0)<<3) | ((t & 0x0e000e00)<<4);\r
e55f0cbb 1257#else\r
19954be1 1258 t = ((t & 0x000e000e)<<12) | ((t & 0x00e000e0)<<3) | ((t & 0x0e000e00)>>7);\r
e55f0cbb 1259#endif\r
a39d8ba5 1260 // treat it like it was 4-bit per channel, since in s/h mode it somewhat is that.\r
1261 // otherwise intensity difference between this and s/h will be wrong\r
1262 t |= (t >> 4) & 0x08610861; // 0x18e318e3\r
19954be1 1263 dpal[i] = t;\r
e55f0cbb 1264 }\r
1265\r
a39d8ba5 1266 // norm: xxx0, sh: 0xxx, hi: 0xxx + 7\r
e55f0cbb 1267 if (sh)\r
1268 {\r
1269 // shadowed pixels\r
a39d8ba5 1270 for (i = 0; i < 0x40 / 2; i++)\r
1271 dpal[0x40/2 | i] = dpal[0xc0/2 | i] = (dpal[i] >> 1) & 0x738e738e;\r
e55f0cbb 1272 // hilighted pixels\r
a39d8ba5 1273 for (i = 0; i < 0x40 / 2; i++) {\r
1274 t = ((dpal[i] >> 1) & 0x738e738e) + 0x738e738e; // 0x7bef7bef;\r
1275 t |= (t >> 4) & 0x08610861;\r
1276 dpal[0x80/2 | i] = t;\r
e55f0cbb 1277 }\r
1278 }\r
1279}\r
1280\r
99bdfd31 1281void FinalizeLine555(int sh, int line, struct PicoEState *est)\r
cc68a136 1282{\r
99bdfd31 1283 unsigned short *pd=est->DrawLineDest;\r
1284 unsigned char *ps=est->HighCol+8;\r
98a27142 1285 unsigned short *pal=est->HighPal;\r
e55f0cbb 1286 int len;\r
cc68a136 1287\r
e55f0cbb 1288 if (Pico.m.dirtyPal)\r
ea38612f 1289 PicoDoHighPal555(sh, line, est);\r
cc68a136 1290\r
70357ce5 1291 if (Pico.video.reg[12]&1) {\r
1292 len = 320;\r
1293 } else {\r
93f9619e 1294 if (!(PicoIn.opt&POPT_DIS_32C_BORDER)) pd+=32;\r
70357ce5 1295 len = 256;\r
1296 }\r
1297\r
e5fa9817 1298 {\r
ee3c39ef 1299#if 1\r
1300 int i;\r
e5fa9817 1301\r
1302 for (i = 0; i < len; i++)\r
ee3c39ef 1303 pd[i] = pal[ps[i]];\r
70357ce5 1304#else\r
70357ce5 1305 extern void amips_clut(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);\r
e5fa9817 1306 extern void amips_clut_6bit(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);\r
ee3c39ef 1307 if (!sh)\r
e5fa9817 1308 amips_clut_6bit(pd, ps, pal, len);\r
1309 else amips_clut(pd, ps, pal, len);\r
70357ce5 1310#endif\r
e5fa9817 1311 }\r
cc68a136 1312}\r
1313#endif\r
1314\r
ea38612f 1315static void FinalizeLine8bit(int sh, int line, struct PicoEState *est)\r
cc68a136 1316{\r
99bdfd31 1317 unsigned char *pd = est->DrawLineDest;\r
ea38612f 1318 int len, rs = est->rendstatus;\r
cc68a136 1319 static int dirty_count;\r
1320\r
87b0845f 1321 if (!sh && Pico.m.dirtyPal == 1)\r
602133e1 1322 {\r
cc68a136 1323 // a hack for mid-frame palette changes\r
602133e1 1324 if (!(rs & PDRAW_SONIC_MODE))\r
cc68a136 1325 dirty_count = 1;\r
1326 else dirty_count++;\r
602133e1 1327 rs |= PDRAW_SONIC_MODE;\r
ea38612f 1328 est->rendstatus = rs;\r
cc68a136 1329 if (dirty_count == 3) {\r
88fd63ad 1330 blockcpy(est->HighPal, PicoMem.cram, 0x40*2);\r
cc68a136 1331 } else if (dirty_count == 11) {\r
88fd63ad 1332 blockcpy(est->HighPal+0x40, PicoMem.cram, 0x40*2);\r
cc68a136 1333 }\r
1334 }\r
1335\r
1336 if (Pico.video.reg[12]&1) {\r
1337 len = 320;\r
1338 } else {\r
93f9619e 1339 if (!(PicoIn.opt & POPT_DIS_32C_BORDER))\r
19954be1 1340 pd += 32;\r
cc68a136 1341 len = 256;\r
1342 }\r
1343\r
602133e1 1344 if (!sh && (rs & PDRAW_SONIC_MODE)) {\r
cc68a136 1345 if (dirty_count >= 11) {\r
99bdfd31 1346 blockcpy_or(pd, est->HighCol+8, len, 0x80);\r
cc68a136 1347 } else {\r
99bdfd31 1348 blockcpy_or(pd, est->HighCol+8, len, 0x40);\r
cc68a136 1349 }\r
1350 } else {\r
99bdfd31 1351 blockcpy(pd, est->HighCol+8, len);\r
cc68a136 1352 }\r
1353}\r
1354\r
ea38612f 1355static void (*FinalizeLine)(int sh, int line, struct PicoEState *est);\r
cc68a136 1356\r
b6d7ac70 1357// --------------------------------------------\r
1358\r
0fc0e241 1359static int DrawDisplay(int sh)\r
cc68a136 1360{\r
ea38612f 1361 struct PicoEState *est=&Pico.est;\r
1362 unsigned char *sprited = &HighLnSpr[est->DrawScanline][0];\r
cc68a136 1363 struct PicoVideo *pvid=&Pico.video;\r
e0bcb7a9 1364 int win=0, edge=0, hvwind=0, lflags;\r
1365 int maxw, maxcells;\r
cc68a136 1366\r
6d8782a1 1367 if (est->rendstatus & (PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES)) {\r
1368 // elprintf(EL_STATUS, "PrepareSprites(%i)", (est->rendstatus>>4)&1);\r
1369 PrepareSprites(est->rendstatus & PDRAW_DIRTY_SPRITES);\r
1370 est->rendstatus &= ~(PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES);\r
1371 }\r
1372\r
ea38612f 1373 est->rendstatus &= ~(PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO);\r
740da8c6 1374\r
fbc65db7 1375 if (pvid->reg[12]&1) {\r
cc68a136 1376 maxw = 328; maxcells = 40;\r
1377 } else {\r
1378 maxw = 264; maxcells = 32;\r
1379 }\r
1380\r
1381 // Find out if the window is on this line:\r
1382 win=pvid->reg[0x12];\r
1383 edge=(win&0x1f)<<3;\r
1384\r
ea38612f 1385 if (win&0x80) { if (est->DrawScanline>=edge) hvwind=1; }\r
1386 else { if (est->DrawScanline< edge) hvwind=1; }\r
cc68a136 1387\r
fbc65db7 1388 if (!hvwind) // we might have a vertical window here\r
1389 {\r
cc68a136 1390 win=pvid->reg[0x11];\r
1391 edge=win&0x1f;\r
7a7c6476 1392 if (win&0x80) {\r
1393 if (!edge) hvwind=1;\r
cc68a136 1394 else if(edge < (maxcells>>1)) hvwind=2;\r
1395 } else {\r
7a7c6476 1396 if (!edge);\r
cc68a136 1397 else if(edge < (maxcells>>1)) hvwind=2;\r
1398 else hvwind=1;\r
1399 }\r
1400 }\r
1401\r
fbc65db7 1402 /* - layer B low - */\r
e0bcb7a9 1403 if (!(pvid->debug_p & PVD_KILL_B)) {\r
1404 lflags = LF_PLANE_1 | (sh << 1);\r
1405 if (pvid->debug_p & PVD_FORCE_B)\r
1406 lflags |= LF_FORCE;\r
1407 DrawLayer(lflags, HighCacheB, 0, maxcells, est);\r
1408 }\r
fbc65db7 1409 /* - layer A low - */\r
e0bcb7a9 1410 lflags = 0 | (sh << 1);\r
1411 if (pvid->debug_p & PVD_FORCE_A)\r
1412 lflags |= LF_FORCE;\r
1413 if (pvid->debug_p & PVD_KILL_A)\r
1414 ;\r
fbc65db7 1415 else if (hvwind == 1)\r
ea38612f 1416 DrawWindow(0, maxcells>>1, 0, sh, est);\r
7a7c6476 1417 else if (hvwind == 2) {\r
e0bcb7a9 1418 DrawLayer(lflags, HighCacheA, (win&0x80) ? 0 : edge<<1, (win&0x80) ? edge<<1 : maxcells, est);\r
1419 DrawWindow( (win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 0, sh, est);\r
1420 }\r
1421 else\r
1422 DrawLayer(lflags, HighCacheA, 0, maxcells, est);\r
fbc65db7 1423 /* - sprites low - */\r
e0bcb7a9 1424 if (pvid->debug_p & PVD_KILL_S_LO)\r
1425 ;\r
6d8782a1 1426 else if (est->rendstatus & PDRAW_INTERLACE)\r
fbc65db7 1427 DrawAllSpritesInterlace(0, sh);\r
1428 else if (sprited[1] & SPRL_HAVE_LO)\r
ea38612f 1429 DrawAllSprites(sprited, 0, sh, est);\r
fbc65db7 1430\r
1431 /* - layer B hi - */\r
e0bcb7a9 1432 if (!(pvid->debug_p & PVD_KILL_B) && HighCacheB[0])\r
ea38612f 1433 DrawTilesFromCache(HighCacheB, sh, maxw, est);\r
fbc65db7 1434 /* - layer A hi - */\r
e0bcb7a9 1435 if (pvid->debug_p & PVD_KILL_A)\r
1436 ;\r
fbc65db7 1437 else if (hvwind == 1)\r
ea38612f 1438 DrawWindow(0, maxcells>>1, 1, sh, est);\r
7a7c6476 1439 else if (hvwind == 2) {\r
ea38612f 1440 if (HighCacheA[0])\r
1441 DrawTilesFromCache(HighCacheA, sh, (win&0x80) ? edge<<4 : maxw, est);\r
1442 DrawWindow((win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 1, sh, est);\r
cc68a136 1443 } else\r
ea38612f 1444 if (HighCacheA[0])\r
1445 DrawTilesFromCache(HighCacheA, sh, maxw, est);\r
fbc65db7 1446 /* - sprites hi - */\r
e0bcb7a9 1447 if (pvid->debug_p & PVD_KILL_S_HI)\r
1448 ;\r
6d8782a1 1449 else if (est->rendstatus & PDRAW_INTERLACE)\r
fbc65db7 1450 DrawAllSpritesInterlace(1, sh);\r
f8af9634 1451 // have sprites without layer pri bit ontop of sprites with that bit\r
93f9619e 1452 else if ((sprited[1] & 0xd0) == 0xd0 && (PicoIn.opt & POPT_ACC_SPRITES))\r
fbc65db7 1453 DrawSpritesHiAS(sprited, sh);\r
1454 else if (sh && (sprited[1] & SPRL_MAY_HAVE_OP))\r
ea38612f 1455 DrawSpritesSHi(sprited, est);\r
fbc65db7 1456 else if (sprited[1] & SPRL_HAVE_HI)\r
ea38612f 1457 DrawAllSprites(sprited, 1, 0, est);\r
cc68a136 1458\r
e0bcb7a9 1459 if (pvid->debug_p & PVD_FORCE_B)\r
1460 DrawTilesFromCacheForced(HighCacheB);\r
1461 else if (pvid->debug_p & PVD_FORCE_A)\r
1462 DrawTilesFromCacheForced(HighCacheA);\r
1463\r
740da8c6 1464#if 0\r
1465 {\r
1466 int *c, a, b;\r
1467 for (a = 0, c = HighCacheA; *c; c++, a++);\r
1468 for (b = 0, c = HighCacheB; *c; c++, b++);\r
ea38612f 1469 printf("%i:%03i: a=%i, b=%i\n", Pico.m.frame_count,\r
1470 Pico.est.DrawScanline, a, b);\r
740da8c6 1471 }\r
1472#endif\r
1473\r
cc68a136 1474 return 0;\r
1475}\r
1476\r
f8af9634 1477// MUST be called every frame\r
eff55556 1478PICO_INTERNAL void PicoFrameStart(void)\r
cc68a136 1479{\r
5a681086 1480 int offs = 8, lines = 224;\r
ae87bffa 1481\r
cc68a136 1482 // prepare to do this frame\r
ea38612f 1483 Pico.est.rendstatus = 0;\r
ae87bffa 1484 if ((Pico.video.reg[12] & 6) == 6)\r
ea38612f 1485 Pico.est.rendstatus |= PDRAW_INTERLACE; // interlace mode\r
ae87bffa 1486 if (!(Pico.video.reg[12] & 1))\r
ea38612f 1487 Pico.est.rendstatus |= PDRAW_32_COLS;\r
5a681086 1488 if (Pico.video.reg[1] & 8) {\r
1489 offs = 0;\r
ae87bffa 1490 lines = 240;\r
5a681086 1491 }\r
e5fa9817 1492\r
ea38612f 1493 if (Pico.est.rendstatus != rendstatus_old || lines != rendlines) {\r
ae87bffa 1494 rendlines = lines;\r
b60b8745 1495 // mode_change() might reset rendstatus_old by calling SetColorFormat\r
ae87bffa 1496 emu_video_mode_change((lines == 240) ? 0 : 8,\r
1497 lines, (Pico.video.reg[12] & 1) ? 0 : 1);\r
ea38612f 1498 rendstatus_old = Pico.est.rendstatus;\r
19954be1 1499 }\r
1500\r
99bdfd31 1501 Pico.est.HighCol = HighColBase + offs * HighColIncrement;\r
1502 Pico.est.DrawLineDest = (char *)DrawLineDestBase + offs * DrawLineDestIncrement;\r
ea38612f 1503 Pico.est.DrawScanline = 0;\r
b7d64dbd 1504 skip_next_line = 0;\r
1505\r
93f9619e 1506 if (PicoIn.opt & POPT_ALT_RENDERER)\r
19954be1 1507 return;\r
1508\r
87b0845f 1509 if (Pico.m.dirtyPal)\r
1510 Pico.m.dirtyPal = 2; // reset dirty if needed\r
cc68a136 1511 PrepareSprites(1);\r
cc68a136 1512}\r
1513\r
974fdb5b 1514static void DrawBlankedLine(int line, int offs, int sh, int bgc)\r
87b0845f 1515{\r
87b0845f 1516 if (PicoScanBegin != NULL)\r
1517 PicoScanBegin(line + offs);\r
1518\r
99bdfd31 1519 BackFill(bgc, sh, &Pico.est);\r
87b0845f 1520\r
1521 if (FinalizeLine != NULL)\r
ea38612f 1522 FinalizeLine(sh, line, &Pico.est);\r
87b0845f 1523\r
1524 if (PicoScanEnd != NULL)\r
1525 PicoScanEnd(line + offs);\r
b2670137 1526\r
99bdfd31 1527 Pico.est.HighCol += HighColIncrement;\r
1528 Pico.est.DrawLineDest = (char *)Pico.est.DrawLineDest + DrawLineDestIncrement;\r
87b0845f 1529}\r
1530\r
974fdb5b 1531static void PicoLine(int line, int offs, int sh, int bgc)\r
cc68a136 1532{\r
ae87bffa 1533 int skip = 0;\r
1534\r
87b0845f 1535 if (skip_next_line > 0) {\r
1536 skip_next_line--;\r
1537 return;\r
1538 }\r
cc68a136 1539\r
ea38612f 1540 Pico.est.DrawScanline = line;\r
602133e1 1541 if (PicoScanBegin != NULL)\r
ae87bffa 1542 skip = PicoScanBegin(line + offs);\r
1543\r
1544 if (skip) {\r
1545 skip_next_line = skip - 1;\r
1546 return;\r
1547 }\r
602133e1 1548\r
e0bcb7a9 1549 if (Pico.video.debug_p & (PVD_FORCE_A | PVD_FORCE_B))\r
1550 bgc = 0x3f;\r
1551\r
cc68a136 1552 // Draw screen:\r
99bdfd31 1553 BackFill(bgc, sh, &Pico.est);\r
cc68a136 1554 if (Pico.video.reg[1]&0x40)\r
0fc0e241 1555 DrawDisplay(sh);\r
cc68a136 1556\r
ea8c405f 1557 if (FinalizeLine != NULL)\r
ea38612f 1558 FinalizeLine(sh, line, &Pico.est);\r
cc68a136 1559\r
602133e1 1560 if (PicoScanEnd != NULL)\r
87b0845f 1561 skip_next_line = PicoScanEnd(line + offs);\r
5a681086 1562\r
99bdfd31 1563 Pico.est.HighCol += HighColIncrement;\r
1564 Pico.est.DrawLineDest = (char *)Pico.est.DrawLineDest + DrawLineDestIncrement;\r
cc68a136 1565}\r
1566\r
b6d7ac70 1567void PicoDrawSync(int to, int blank_last_line)\r
1568{\r
87b0845f 1569 int line, offs = 0;\r
974fdb5b 1570 int sh = (Pico.video.reg[0xC] & 8) >> 3; // shadow/hilight?\r
1571 int bgc = Pico.video.reg[7];\r
87b0845f 1572\r
f6c49d38 1573 pprof_start(draw);\r
1574\r
e42a47e2 1575 if (rendlines != 240) {\r
87b0845f 1576 offs = 8;\r
e42a47e2 1577 if (to > 223)\r
1578 to = 223;\r
1579 }\r
87b0845f 1580\r
ea38612f 1581 for (line = Pico.est.DrawScanline; line < to; line++)\r
974fdb5b 1582 PicoLine(line, offs, sh, bgc);\r
b6d7ac70 1583\r
b6d7ac70 1584 // last line\r
87b0845f 1585 if (line <= to)\r
b6d7ac70 1586 {\r
1587 if (blank_last_line)\r
974fdb5b 1588 DrawBlankedLine(line, offs, sh, bgc);\r
1589 else PicoLine(line, offs, sh, bgc);\r
87b0845f 1590 line++;\r
b6d7ac70 1591 }\r
ea38612f 1592 Pico.est.DrawScanline = line;\r
f6c49d38 1593\r
1594 pprof_end(draw);\r
b6d7ac70 1595}\r
cc68a136 1596\r
5a681086 1597// also works for fast renderer\r
1598void PicoDrawUpdateHighPal(void)\r
1599{\r
98a27142 1600 struct PicoEState *est = &Pico.est;\r
5a681086 1601 int sh = (Pico.video.reg[0xC] & 8) >> 3; // shadow/hilight?\r
93f9619e 1602 if (PicoIn.opt & POPT_ALT_RENDERER)\r
5a681086 1603 sh = 0; // no s/h support\r
1604\r
ea38612f 1605 PicoDoHighPal555(sh, 0, &Pico.est);\r
98a27142 1606 if (est->rendstatus & PDRAW_SONIC_MODE) {\r
5a681086 1607 // FIXME?\r
98a27142 1608 memcpy(est->HighPal + 0x40, est->HighPal, 0x40*2);\r
1609 memcpy(est->HighPal + 0x80, est->HighPal, 0x40*2);\r
5a681086 1610 }\r
1611}\r
1612\r
41946d70 1613void PicoDrawSetOutFormat(pdso_t which, int use_32x_line_mode)\r
cc68a136 1614{\r
ea8c405f 1615 switch (which)\r
1616 {\r
5a681086 1617 case PDF_8BIT:\r
1618 FinalizeLine = FinalizeLine8bit;\r
1619 break;\r
1620\r
1621 case PDF_RGB555:\r
93f9619e 1622 if ((PicoIn.AHW & PAHW_32X) && use_32x_line_mode)\r
5a681086 1623 FinalizeLine = FinalizeLine32xRGB555;\r
1624 else\r
1625 FinalizeLine = FinalizeLine555;\r
1626 break;\r
1627\r
1628 default:\r
1629 FinalizeLine = NULL;\r
1630 break;\r
ea8c405f 1631 }\r
41946d70 1632 PicoDrawSetOutFormat32x(which, use_32x_line_mode);\r
5a681086 1633 PicoDrawSetOutputMode4(which);\r
205bc456 1634 rendstatus_old = -1;\r
5a681086 1635}\r
1636\r
e51e5983 1637// note: may be called on the middle of frame\r
5a681086 1638void PicoDrawSetOutBuf(void *dest, int increment)\r
1639{\r
1640 DrawLineDestBase = dest;\r
1641 DrawLineDestIncrement = increment;\r
24aab4da 1642 Pico.est.DrawLineDest = (char *)DrawLineDestBase + Pico.est.DrawScanline * increment;\r
5a681086 1643}\r
1644\r
1645void PicoDrawSetInternalBuf(void *dest, int increment)\r
1646{\r
1647 if (dest != NULL) {\r
1648 HighColBase = dest;\r
1649 HighColIncrement = increment;\r
99bdfd31 1650 Pico.est.HighCol = HighColBase + Pico.est.DrawScanline * increment;\r
5a681086 1651 }\r
1652 else {\r
1653 HighColBase = DefHighCol;\r
1654 HighColIncrement = 0;\r
1655 }\r
cc68a136 1656}\r
ea8c405f 1657\r
f4750ee0 1658void PicoDrawSetCallbacks(int (*begin)(unsigned int num), int (*end)(unsigned int num))\r
1659{\r
1660 PicoScanBegin = NULL;\r
1661 PicoScanEnd = NULL;\r
1662 PicoScan32xBegin = NULL;\r
1663 PicoScan32xEnd = NULL;\r
1664\r
93f9619e 1665 if ((PicoIn.AHW & PAHW_32X) && FinalizeLine != FinalizeLine32xRGB555) {\r
f4750ee0 1666 PicoScan32xBegin = begin;\r
1667 PicoScan32xEnd = end;\r
1668 }\r
1669 else {\r
1670 PicoScanBegin = begin;\r
1671 PicoScanEnd = end;\r
1672 }\r
1673}\r
ea38612f 1674\r
99bdfd31 1675void PicoDrawInit(void)\r
1676{\r
1677 Pico.est.DrawLineDest = DefOutBuff;\r
1678 Pico.est.HighCol = HighColBase;\r
1679 Pico.est.HighPreSpr = HighPreSpr;\r
1680 rendstatus_old = -1;\r
1681}\r
1682\r
1683// vim:ts=2:sw=2:expandtab\r