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