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