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