overdraw eliminated in AS mode
[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
e352c3af 147#ifndef _ASM_DRAW_C\r
148\r
07abbab1 149// draw a sprite pixel, process operator colors\r
150#define pix_sh(x) \\r
151 if (!t); \\r
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// draw a sprite pixel ignoring operator colors\r
160#define pix_sh_noop(x) \\r
161 if (t && t < 0xe) \\r
162 pd[x]=pal|t\r
cc68a136 163\r
07abbab1 164TileNormMaker(TileNormSH_noop, pix_sh_noop)\r
165TileFlipMaker(TileFlipSH_noop, pix_sh_noop)\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
e352c3af 175#endif\r
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
947fb5f9 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
7a7c6476 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
97a7f774 631static void DrawSprite(int *sprite, int sh)\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
ee4f03ae 657 delta<<=4; // Delta of address\r
07abbab1 658\r
659 pal=(code>>9)&0x30;\r
97a7f774 660 pal|=sh<<6;\r
e5fa9817 661\r
662 if (sh && (code&0x6000) == 0x6000) {\r
07abbab1 663 if(code&0x0800) fTileFunc=TileFlipSH_noop;\r
664 else fTileFunc=TileNormSH_noop;\r
e5fa9817 665 } else {\r
666 if(code&0x0800) fTileFunc=TileFlip;\r
667 else fTileFunc=TileNorm;\r
cc68a136 668 }\r
669\r
670 for (; width; width--,sx+=8,tile+=delta)\r
671 {\r
672 if(sx<=0) continue;\r
673 if(sx>=328) break; // Offscreen\r
674\r
675 tile&=0x7fff; // Clip tile address\r
e5fa9817 676 fTileFunc(sx,tile,pal);\r
cc68a136 677 }\r
678}\r
e5fa9817 679#endif\r
cc68a136 680\r
681static void DrawSpriteInterlace(unsigned int *sprite)\r
682{\r
683 int width=0,height=0;\r
684 int row=0,code=0;\r
685 int pal;\r
686 int tile=0,delta=0;\r
687 int sx, sy;\r
688\r
689 // parse the sprite data\r
690 sy=sprite[0];\r
691 height=sy>>24;\r
692 sy=(sy&0x3ff)-0x100; // Y\r
693 width=(height>>2)&3; height&=3;\r
694 width++; height++; // Width and height in tiles\r
695\r
b6d7ac70 696 row=(DrawScanline<<1)-sy; // Row of the sprite we are on\r
cc68a136 697\r
698 code=sprite[1];\r
699 sx=((code>>16)&0x1ff)-0x78; // X\r
700\r
701 if (code&0x1000) row^=(16<<height)-1; // Flip Y\r
702\r
703 tile=code&0x3ff; // Tile number\r
704 tile+=row>>4; // Tile number increases going down\r
705 delta=height; // Delta to increase tile by going right\r
706 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X\r
707\r
708 tile<<=5; tile+=(row&15)<<1; // Tile address\r
709\r
710 delta<<=5; // Delta of address\r
711 pal=((code>>9)&0x30); // Get palette pointer\r
712\r
713 for (; width; width--,sx+=8,tile+=delta)\r
714 {\r
715 if(sx<=0) continue;\r
716 if(sx>=328) break; // Offscreen\r
717\r
718 tile&=0x7fff; // Clip tile address\r
719 if (code&0x0800) TileFlip(sx,tile,pal);\r
720 else TileNorm(sx,tile,pal);\r
721 }\r
722}\r
723\r
724\r
ee4f03ae 725static void DrawAllSpritesInterlace(int pri, int sh)\r
cc68a136 726{\r
727 struct PicoVideo *pvid=&Pico.video;\r
b6d7ac70 728 int i,u,table,link=0,sline=DrawScanline<<1;\r
cc68a136 729 unsigned int *sprites[80]; // Sprite index\r
730\r
731 table=pvid->reg[5]&0x7f;\r
732 if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode\r
733 table<<=8; // Get sprite table address/2\r
734\r
735 for (i=u=0; u < 80 && i < 21; u++)\r
736 {\r
737 unsigned int *sprite;\r
738 int code, sx, sy, height;\r
739\r
740 sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
741\r
742 // get sprite info\r
743 code = sprite[0];\r
744 sx = sprite[1];\r
745 if(((sx>>15)&1) != pri) goto nextsprite; // wrong priority sprite\r
746\r
747 // check if it is on this line\r
748 sy = (code&0x3ff)-0x100;\r
749 height = (((code>>24)&3)+1)<<4;\r
750 if(sline < sy || sline >= sy+height) goto nextsprite; // no\r
751\r
752 // check if sprite is not hidden offscreen\r
753 sx = (sx>>16)&0x1ff;\r
754 sx -= 0x78; // Get X coordinate + 8\r
947fb5f9 755 if(sx <= -8*3 || sx >= 328) goto nextsprite;\r
cc68a136 756\r
757 // sprite is good, save it's pointer\r
758 sprites[i++]=sprite;\r
759\r
760 nextsprite:\r
761 // Find next sprite\r
762 link=(code>>16)&0x7f;\r
763 if(!link) break; // End of sprites\r
764 }\r
765\r
766 // Go through sprites backwards:\r
767 for (i-- ;i>=0; i--)\r
768 DrawSpriteInterlace(sprites[i]);\r
769}\r
770\r
771\r
772#ifndef _ASM_DRAW_C\r
ee4f03ae 773// Index + 0 : hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size\r
774// Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8\r
775static void DrawSpritesSHi(int prio, int sh)\r
cc68a136 776{\r
cc68a136 777 int (*fTileFunc)(int sx,int addr,int pal);\r
ee4f03ae 778 unsigned char *p;\r
779 int cnt;\r
780\r
781 cnt = HighLnSpr[DrawScanline][0] & 0x7f;\r
782 if (cnt == 0) return;\r
cc68a136 783\r
ee4f03ae 784 p = &HighLnSpr[DrawScanline][2];\r
cc68a136 785\r
ee4f03ae 786 // Go through sprites backwards:\r
787 for (cnt--; cnt >= 0; cnt--)\r
07abbab1 788 {\r
ee4f03ae 789 int *sprite, code, pal, tile, sx, sy;\r
790 int offs, delta, width, height, row;\r
791\r
792 offs = (p[cnt] & 0x7f) * 2;\r
793 sprite = HighPreSpr + offs;\r
794 code = sprite[1];\r
795 pal = (code>>9)&0x30;\r
cc68a136 796\r
07abbab1 797 if (sh && pal == 0x30)\r
798 {\r
ee4f03ae 799 if (code & 0x8000) // hi priority\r
07abbab1 800 {\r
ee4f03ae 801 if (code&0x800) fTileFunc=TileFlipSH;\r
802 else fTileFunc=TileNormSH;\r
07abbab1 803 } else {\r
ee4f03ae 804 if (code&0x800) fTileFunc=TileFlipSH_onlyop_lp;\r
805 else fTileFunc=TileNormSH_onlyop_lp;\r
07abbab1 806 }\r
cc68a136 807 } else {\r
ee4f03ae 808 if (!(code & 0x8000)) continue; // non-operator low sprite, already drawn\r
809 if (code&0x800) fTileFunc=TileFlip;\r
810 else fTileFunc=TileNorm;\r
cc68a136 811 }\r
812\r
ee4f03ae 813 // parse remaining sprite data\r
814 sy=sprite[0];\r
815 sx=code>>16; // X\r
816 width=sy>>28;\r
817 height=(sy>>24)&7; // Width and height in tiles\r
818 sy=(sy<<16)>>16; // Y\r
819\r
820 row=DrawScanline-sy; // Row of the sprite we are on\r
821\r
822 if (code&0x1000) row=(height<<3)-1-row; // Flip Y\r
823\r
824 tile=code + (row>>3); // Tile number increases going down\r
825 delta=height; // Delta to increase tile by going right\r
826 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X\r
827\r
828 tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address\r
829 delta<<=4; // Delta of address\r
830\r
cc68a136 831 for (; width; width--,sx+=8,tile+=delta)\r
832 {\r
833 if(sx<=0) continue;\r
834 if(sx>=328) break; // Offscreen\r
835\r
836 tile&=0x7fff; // Clip tile address\r
837 fTileFunc(sx,tile,pal);\r
838 }\r
839 }\r
840}\r
841#endif\r
842\r
ee4f03ae 843static void DrawSpritesHiAS(int prio, int sh)\r
e5fa9817 844{\r
e5fa9817 845 int (*fTileFunc)(int sx,int addr,int pal);\r
ee4f03ae 846 unsigned char *p;\r
847 int entry, cnt, sh_cnt = 0;\r
848\r
849 cnt = HighLnSpr[DrawScanline][0] & 0x7f;\r
850 if (cnt == 0) return;\r
e5fa9817 851\r
ee4f03ae 852 p = &HighLnSpr[DrawScanline][2];\r
e5fa9817 853\r
ee4f03ae 854 // Go through sprites:\r
855 for (entry = 0; entry < cnt; entry++)\r
07abbab1 856 {\r
ee4f03ae 857 int *sprite, code, pal, tile, sx, sy;\r
858 int offs, delta, width, height, row;\r
859\r
860 offs = (p[entry] & 0x7f) * 2;\r
861 sprite = HighPreSpr + offs;\r
862 code = sprite[1];\r
863 pal = (code>>9)&0x30;\r
864\r
865 if (code & 0x8000) // hi priority\r
07abbab1 866 {\r
867 if (sh && pal == 0x30)\r
868 {\r
ee4f03ae 869 if (code&0x800) fTileFunc=TileFlipAS_noop;\r
870 else fTileFunc=TileNormAS_noop;\r
07abbab1 871 } else {\r
ee4f03ae 872 if (code&0x800) fTileFunc=TileFlipAS;\r
873 else fTileFunc=TileNormAS;\r
07abbab1 874 }\r
e5fa9817 875 } else {\r
ee4f03ae 876 if (code&0x800) fTileFunc=TileFlipAS_onlymark;\r
877 else fTileFunc=TileNormAS_onlymark;\r
e5fa9817 878 }\r
411cba90 879 if (sh && pal == 0x30)\r
ee4f03ae 880 p[sh_cnt++] = offs / 2; // re-save for sh/hi pass\r
881\r
882 // parse remaining sprite data\r
883 sy=sprite[0];\r
884 sx=code>>16; // X\r
885 width=sy>>28;\r
886 height=(sy>>24)&7; // Width and height in tiles\r
887 sy=(sy<<16)>>16; // Y\r
888\r
889 row=DrawScanline-sy; // Row of the sprite we are on\r
890\r
891 if (code&0x1000) row=(height<<3)-1-row; // Flip Y\r
892\r
893 tile=code + (row>>3); // Tile number increases going down\r
894 delta=height; // Delta to increase tile by going right\r
895 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X\r
896\r
897 tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address\r
898 delta<<=4; // Delta of address\r
e5fa9817 899\r
07abbab1 900 pal |= 0x80;\r
901 for (; width; width--,sx+=8,tile+=delta)\r
902 {\r
903 if(sx<=0) continue;\r
904 if(sx>=328) break; // Offscreen\r
905\r
906 tile&=0x7fff; // Clip tile address\r
907 fTileFunc(sx,tile,pal);\r
908 }\r
909 }\r
910\r
911 if (!sh) return;\r
912\r
913 /* nasty 1: remove 'sprite' flags */\r
914 {\r
915 int c = 320/4, *zb = (int *)(HighCol+8);\r
916 while (c--)\r
917 {\r
918 *zb++ &= 0x7f7f7f7f;\r
919 }\r
920 }\r
921\r
ee4f03ae 922 /* nasty 2: sh operator pass */\r
923 HighLnSpr[DrawScanline][0] = sh_cnt;\r
924 DrawSpritesSHi(1, 1);\r
e5fa9817 925}\r
926\r
cc68a136 927\r
928// Index + 0 : ----hhvv -lllllll -------y yyyyyyyy\r
929// Index + 4 : -------x xxxxxxxx pccvhnnn nnnnnnnn\r
930// v\r
d9fc2fe1 931// Index + 0 : hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size\r
cc68a136 932// Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8\r
933\r
283fec1b 934void PrepareSprites(int full)\r
cc68a136 935{\r
936 struct PicoVideo *pvid=&Pico.video;\r
d9fc2fe1 937 int u,link=0;\r
cc68a136 938 int table=0;\r
939 int *pd = HighPreSpr;\r
947fb5f9 940 int max_lines = 224, max_sprites = 80, max_width = 328;\r
d9fc2fe1 941 int max_line_sprites = 20; // 20 sprites, 40 tiles\r
942\r
943 if (!(Pico.video.reg[12]&1))\r
947fb5f9 944 max_sprites = 64, max_line_sprites = 16, max_width = 264;\r
d9fc2fe1 945 if (PicoOpt & POPT_DIS_SPRITE_LIM)\r
946 max_line_sprites = MAX_LINE_SPRITES;\r
947\r
948 if (pvid->reg[1]&8) max_lines = 240;\r
cc68a136 949\r
950 table=pvid->reg[5]&0x7f;\r
951 if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode\r
952 table<<=8; // Get sprite table address/2\r
953\r
954 if (!full)\r
955 {\r
956 int pack;\r
957 // updates: tilecode, sx\r
d9fc2fe1 958 for (u=0; u < max_lines && (pack = *pd); u++, pd+=2)\r
cc68a136 959 {\r
960 unsigned int *sprite;\r
d9fc2fe1 961 int code2, sx, sy, height;\r
cc68a136 962\r
963 sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
964\r
965 // parse sprite info\r
cc68a136 966 code2 = sprite[1];\r
d9fc2fe1 967 sx = (code2>>16)&0x1ff;\r
968 sx -= 0x78; // Get X coordinate + 8\r
969 sy = (pack << 16) >> 16;\r
947fb5f9 970 height = (pack >> 24) & 0xf;\r
cc68a136 971\r
d9fc2fe1 972 if (sy < max_lines && sy + (height<<3) > DrawScanline && // sprite onscreen (y)?\r
947fb5f9 973 (sx > -24 || sx < max_width)) // onscreen x\r
d9fc2fe1 974 {\r
975 int y = (sy >= DrawScanline) ? sy : DrawScanline;\r
ee4f03ae 976 int entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80);\r
d9fc2fe1 977 for (; y < sy + (height<<3) && y < max_lines; y++)\r
978 {\r
947fb5f9 979 int i, cnt;\r
d9fc2fe1 980 cnt = HighLnSpr[y][0] & 0x7f;\r
981 if (cnt >= max_line_sprites) continue; // sprite limit?\r
d9fc2fe1 982\r
983 for (i = 0; i < cnt; i++)\r
ee4f03ae 984 if (((HighLnSpr[y][2+i] ^ entry) & 0x7f) == 0) goto found;\r
d9fc2fe1 985\r
986 // this sprite was previously missing\r
ee4f03ae 987 HighLnSpr[y][2+cnt] = entry;\r
d9fc2fe1 988 HighLnSpr[y][0] = cnt + 1;\r
989found:;\r
e352c3af 990 if (entry & 0x80)\r
991 rendstatus |= PDRAW_HAVE_HI_SPR;\r
992 else rendstatus |= PDRAW_HAVE_LO_SPR;\r
d9fc2fe1 993 }\r
cc68a136 994 }\r
995\r
d9fc2fe1 996 code2 &= ~0xfe000000;\r
997 code2 -= 0x00780000; // Get X coordinate + 8 in upper 16 bits\r
998 pd[1] = code2;\r
cc68a136 999\r
1000 // Find next sprite\r
d9fc2fe1 1001 link=(sprite[0]>>16)&0x7f;\r
1002 if (!link) break; // End of sprites\r
cc68a136 1003 }\r
cc68a136 1004 }\r
1005 else\r
1006 {\r
d9fc2fe1 1007 for (u = 0; u < max_lines; u++)\r
1008 *((int *)&HighLnSpr[u][0]) = 0;\r
1009\r
1010 for (u = 0; u < max_lines; u++)\r
cc68a136 1011 {\r
1012 unsigned int *sprite;\r
d9fc2fe1 1013 int code, code2, sx, sy, hv, height, width;\r
947fb5f9 1014 int sx_min;\r
cc68a136 1015\r
1016 sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
1017\r
1018 // parse sprite info\r
1019 code = sprite[0];\r
1020 sy = (code&0x1ff)-0x80;\r
1021 hv = (code>>24)&0xf;\r
1022 height = (hv&3)+1;\r
1023\r
cc68a136 1024 width = (hv>>2)+1;\r
1025 code2 = sprite[1];\r
1026 sx = (code2>>16)&0x1ff;\r
1027 sx -= 0x78; // Get X coordinate + 8\r
1028 sx_min = 8-(width<<3);\r
6d7acf9e 1029\r
d9fc2fe1 1030 if (sy < max_lines && sy + (height<<3) > DrawScanline) // sprite onscreen (y)?\r
1031 {\r
1032 int y = (sy >= DrawScanline) ? sy : DrawScanline;\r
ee4f03ae 1033 int entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80);\r
d9fc2fe1 1034 for (; y < sy + (height<<3) && y < max_lines; y++)\r
1035 {\r
1036 int cnt = HighLnSpr[y][0];\r
1037 if (cnt >= max_line_sprites) continue; // sprite limit?\r
1038\r
947fb5f9 1039 if (HighLnSpr[y][1] >= max_line_sprites*2) { // tile limit?\r
d9fc2fe1 1040 HighLnSpr[y][0] |= 0x80;\r
947fb5f9 1041 continue;\r
d9fc2fe1 1042 }\r
1043 HighLnSpr[y][1] += width;\r
1044\r
1045 if (sx == -0x78) {\r
1046 if (cnt > 0)\r
1047 HighLnSpr[y][0] |= 0x80; // masked, no more sprites for this line\r
947fb5f9 1048 continue;\r
d9fc2fe1 1049 }\r
1050 // must keep the first sprite even if it's offscreen, for masking\r
947fb5f9 1051 if (cnt > 0 && (sx <= sx_min || sx >= max_width)) continue; // offscreen x\r
d9fc2fe1 1052\r
ee4f03ae 1053 HighLnSpr[y][2+cnt] = entry;\r
d9fc2fe1 1054 HighLnSpr[y][0] = cnt + 1;\r
ee4f03ae 1055 if (entry & 0x80)\r
1056 rendstatus |= PDRAW_HAVE_HI_SPR;\r
1057 else rendstatus |= PDRAW_HAVE_LO_SPR;\r
d9fc2fe1 1058 }\r
cc68a136 1059 }\r
6d7acf9e 1060\r
d9fc2fe1 1061 *pd++ = (width<<28)|(height<<24)|(hv<<16)|((unsigned short)sy);\r
cc68a136 1062 *pd++ = (sx<<16)|((unsigned short)code2);\r
6d7acf9e 1063\r
cc68a136 1064 // Find next sprite\r
1065 link=(code>>16)&0x7f;\r
d9fc2fe1 1066 if (!link) break; // End of sprites\r
cc68a136 1067 }\r
d9fc2fe1 1068 *pd = 0;\r
1069\r
1070#if 0\r
1071 for (u = 0; u < max_lines; u++)\r
1072 {\r
1073 int y;\r
1074 printf("c%03i: %2i, %2i: ", u, HighLnSpr[u][0] & 0x7f, HighLnSpr[u][1]);\r
1075 for (y = 0; y < HighLnSpr[u][0] & 0x7f; y++)\r
1076 printf(" %i", HighLnSpr[u][y+2]);\r
1077 printf("\n");\r
1078 }\r
1079#endif\r
cc68a136 1080 }\r
1081}\r
1082\r
283fec1b 1083#ifndef _ASM_DRAW_C\r
ee4f03ae 1084static void DrawAllSprites(int prio, int sh)\r
cc68a136 1085{\r
d9fc2fe1 1086 int rs = rendstatus, scan = DrawScanline;\r
1087 unsigned char *p;\r
97a7f774 1088 int cnt;\r
cc68a136 1089\r
602133e1 1090 if (rs & (PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES)) {\r
d9fc2fe1 1091 //elprintf(EL_STATUS, "PrepareSprites(%i)", (rs>>4)&1);\r
602133e1 1092 PrepareSprites(rs & PDRAW_DIRTY_SPRITES);\r
1093 rendstatus = rs & ~(PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES);\r
cc68a136 1094 }\r
381eea9b 1095\r
d9fc2fe1 1096 cnt = HighLnSpr[scan][0] & 0x7f;\r
ee4f03ae 1097 if (cnt == 0) return;\r
cc68a136 1098\r
ee4f03ae 1099 p = &HighLnSpr[scan][2];\r
cc68a136 1100\r
ee4f03ae 1101 // Go through sprites backwards:\r
1102 for (cnt--; cnt >= 0; cnt--)\r
1103 {\r
1104 int offs;\r
97a7f774 1105 if ((p[cnt] >> 7) != prio) continue;\r
ee4f03ae 1106 offs = (p[cnt]&0x7f) * 2;\r
97a7f774 1107 DrawSprite(HighPreSpr + offs, sh);\r
cc68a136 1108 }\r
cc68a136 1109}\r
1110\r
1111\r
1112// --------------------------------------------\r
1113\r
cc68a136 1114static void BackFill(int reg7, int sh)\r
1115{\r
b542be46 1116 unsigned int back;\r
cc68a136 1117\r
1118 // Start with a blank scanline (background colour):\r
1119 back=reg7&0x3f;\r
1120 back|=sh<<6;\r
1121 back|=back<<8;\r
1122 back|=back<<16;\r
1123\r
b542be46 1124 memset32((int *)(HighCol+8), back, 320/4);\r
cc68a136 1125}\r
1126#endif\r
1127\r
1128// --------------------------------------------\r
1129\r
1130unsigned short HighPal[0x100];\r
1131\r
1132#ifndef _ASM_DRAW_C\r
1133static void FinalizeLineBGR444(int sh)\r
1134{\r
1135 unsigned short *pd=DrawLineDest;\r
1136 unsigned char *ps=HighCol+8;\r
1137 unsigned short *pal=Pico.cram;\r
e5fa9817 1138 int len, i, t, mask=0xff;\r
cc68a136 1139\r
1140 if (Pico.video.reg[12]&1) {\r
1141 len = 320;\r
1142 } else {\r
602133e1 1143 if(!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32;\r
cc68a136 1144 len = 256;\r
1145 }\r
1146\r
1147 if(sh) {\r
1148 pal=HighPal;\r
1149 if(Pico.m.dirtyPal) {\r
1150 blockcpy(pal, Pico.cram, 0x40*2);\r
1151 // shadowed pixels\r
1152 for(i = 0x3f; i >= 0; i--)\r
1153 pal[0x40|i] = pal[0xc0|i] = (unsigned short)((pal[i]>>1)&0x0777);\r
1154 // hilighted pixels\r
1155 for(i = 0x3f; i >= 0; i--) {\r
1156 t=pal[i]&0xeee;t+=0x444;if(t&0x10)t|=0xe;if(t&0x100)t|=0xe0;if(t&0x1000)t|=0xe00;t&=0xeee;\r
1157 pal[0x80|i]=(unsigned short)t;\r
1158 }\r
1159 Pico.m.dirtyPal = 0;\r
1160 }\r
1161 }\r
1162\r
e5fa9817 1163 if (!sh && (rendstatus & PDRAW_ACC_SPRITES))\r
1164 mask=0x3f; // accurate sprites\r
1165\r
cc68a136 1166 for(i = 0; i < len; i++)\r
e5fa9817 1167 pd[i] = pal[ps[i] & mask];\r
cc68a136 1168}\r
1169\r
1170\r
1171static void FinalizeLineRGB555(int sh)\r
1172{\r
1173 unsigned short *pd=DrawLineDest;\r
1174 unsigned char *ps=HighCol+8;\r
1175 unsigned short *pal=HighPal;\r
1176 int len, i, t, dirtyPal = Pico.m.dirtyPal;\r
1177\r
70357ce5 1178 if (dirtyPal)\r
1179 {\r
1180 unsigned int *spal=(void *)Pico.cram;\r
1181 unsigned int *dpal=(void *)HighPal;\r
1182 for (i = 0x3f/2; i >= 0; i--)\r
1183#ifdef USE_BGR555\r
1184 dpal[i] = ((spal[i]&0x000f000f)<< 1)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)<<4);\r
1185#else\r
1186 dpal[i] = ((spal[i]&0x000f000f)<<12)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)>>7);\r
1187#endif\r
cc68a136 1188 Pico.m.dirtyPal = 0;\r
1189 }\r
1190\r
70357ce5 1191 if (sh)\r
1192 {\r
1193 if (dirtyPal) {\r
cc68a136 1194 // shadowed pixels\r
70357ce5 1195 for (i = 0x3f; i >= 0; i--)\r
cc68a136 1196 pal[0x40|i] = pal[0xc0|i] = (unsigned short)((pal[i]>>1)&0x738e);\r
1197 // hilighted pixels\r
70357ce5 1198 for (i = 0x3f; i >= 0; i--) {\r
cc68a136 1199 t=pal[i]&0xe71c;t+=0x4208;if(t&0x20)t|=0x1c;if(t&0x800)t|=0x700;if(t&0x10000)t|=0xe000;t&=0xe71c;\r
1200 pal[0x80|i]=(unsigned short)t;\r
1201 }\r
1202 }\r
1203 }\r
1204\r
70357ce5 1205 if (Pico.video.reg[12]&1) {\r
1206 len = 320;\r
1207 } else {\r
602133e1 1208 if (!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32;\r
70357ce5 1209 len = 256;\r
1210 }\r
1211\r
e5fa9817 1212 {\r
70357ce5 1213#ifndef PSP\r
e5fa9817 1214 int mask=0xff;\r
1215 if (!sh && (rendstatus & PDRAW_ACC_SPRITES))\r
1216 mask=0x3f; // accurate sprites, upper bits are priority stuff\r
1217\r
1218 for (i = 0; i < len; i++)\r
1219 pd[i] = pal[ps[i] & mask];\r
70357ce5 1220#else\r
70357ce5 1221 extern void amips_clut(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);\r
e5fa9817 1222 extern void amips_clut_6bit(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);\r
1223 if (!sh && (rendstatus & PDRAW_ACC_SPRITES))\r
1224 amips_clut_6bit(pd, ps, pal, len);\r
1225 else amips_clut(pd, ps, pal, len);\r
70357ce5 1226#endif\r
e5fa9817 1227 }\r
cc68a136 1228}\r
1229#endif\r
1230\r
1231static void FinalizeLine8bit(int sh)\r
1232{\r
1233 unsigned char *pd=DrawLineDest;\r
1234 int len, rs = rendstatus;\r
1235 static int dirty_count;\r
1236\r
b6d7ac70 1237 if (!sh && !(rs & PDRAW_ACC_SPRITES) && Pico.m.dirtyPal == 1 && DrawScanline < 222)\r
602133e1 1238 {\r
cc68a136 1239 // a hack for mid-frame palette changes\r
602133e1 1240 if (!(rs & PDRAW_SONIC_MODE))\r
cc68a136 1241 dirty_count = 1;\r
1242 else dirty_count++;\r
602133e1 1243 rs |= PDRAW_SONIC_MODE;\r
cc68a136 1244 rendstatus = rs;\r
1245 if (dirty_count == 3) {\r
1246 blockcpy(HighPal, Pico.cram, 0x40*2);\r
1247 } else if (dirty_count == 11) {\r
1248 blockcpy(HighPal+0x40, Pico.cram, 0x40*2);\r
1249 }\r
1250 }\r
1251\r
1252 if (Pico.video.reg[12]&1) {\r
1253 len = 320;\r
1254 } else {\r
602133e1 1255 if (!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32;\r
cc68a136 1256 len = 256;\r
1257 }\r
1258\r
602133e1 1259 if (!sh && (rs & PDRAW_SONIC_MODE)) {\r
cc68a136 1260 if (dirty_count >= 11) {\r
1261 blockcpy_or(pd, HighCol+8, len, 0x80);\r
1262 } else {\r
1263 blockcpy_or(pd, HighCol+8, len, 0x40);\r
1264 }\r
1265 } else {\r
1266 blockcpy(pd, HighCol+8, len);\r
1267 }\r
1268}\r
1269\r
ea8c405f 1270static void (*FinalizeLine)(int sh) = FinalizeLineBGR444;\r
cc68a136 1271\r
b6d7ac70 1272// --------------------------------------------\r
1273\r
1274static void DrawBlankedLine(void)\r
602133e1 1275{\r
b6d7ac70 1276 int sh=(Pico.video.reg[0xC]&8)>>3; // shadow/hilight?\r
602133e1 1277\r
1278 if (PicoScanBegin != NULL)\r
b6d7ac70 1279 PicoScanBegin(DrawScanline);\r
602133e1 1280\r
1281 BackFill(Pico.video.reg[7], sh);\r
1282\r
1283 if (FinalizeLine != NULL)\r
1284 FinalizeLine(sh);\r
1285\r
1286 if (PicoScanEnd != NULL)\r
b6d7ac70 1287 PicoScanEnd(DrawScanline);\r
602133e1 1288}\r
1289\r
e5fa9817 1290static int DrawDisplay(int sh, int as)\r
cc68a136 1291{\r
1292 struct PicoVideo *pvid=&Pico.video;\r
1293 int win=0,edge=0,hvwind=0;\r
1294 int maxw, maxcells;\r
1295\r
602133e1 1296 rendstatus &= ~(PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO);\r
740da8c6 1297\r
cc68a136 1298 if(pvid->reg[12]&1) {\r
1299 maxw = 328; maxcells = 40;\r
1300 } else {\r
1301 maxw = 264; maxcells = 32;\r
1302 }\r
1303\r
1304 // Find out if the window is on this line:\r
1305 win=pvid->reg[0x12];\r
1306 edge=(win&0x1f)<<3;\r
1307\r
b6d7ac70 1308 if (win&0x80) { if (DrawScanline>=edge) hvwind=1; }\r
1309 else { if (DrawScanline< edge) hvwind=1; }\r
cc68a136 1310\r
7a7c6476 1311 if (!hvwind) { // we might have a vertical window here\r
cc68a136 1312 win=pvid->reg[0x11];\r
1313 edge=win&0x1f;\r
7a7c6476 1314 if (win&0x80) {\r
1315 if (!edge) hvwind=1;\r
cc68a136 1316 else if(edge < (maxcells>>1)) hvwind=2;\r
1317 } else {\r
7a7c6476 1318 if (!edge);\r
cc68a136 1319 else if(edge < (maxcells>>1)) hvwind=2;\r
1320 else hvwind=1;\r
1321 }\r
1322 }\r
1323\r
e5fa9817 1324 DrawLayer(1|((sh|as)<<1), HighCacheB, 0, maxcells);\r
7a7c6476 1325 if (hvwind == 1)\r
e5fa9817 1326 DrawWindow(0, maxcells>>1, 0, sh|as);\r
7a7c6476 1327 else if (hvwind == 2) {\r
cc68a136 1328 // ahh, we have vertical window\r
e5fa9817 1329 DrawLayer(0|((sh|as)<<1), HighCacheA, (win&0x80) ? 0 : edge<<1, (win&0x80) ? edge<<1 : maxcells);\r
1330 DrawWindow( (win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 0, sh|as);\r
cc68a136 1331 } else\r
e5fa9817 1332 DrawLayer(0|((sh|as)<<1), HighCacheA, 0, maxcells);\r
97a7f774 1333 if (rendstatus & PDRAW_HAVE_LO_SPR)\r
ee4f03ae 1334 DrawAllSpritesLoPri(0, sh);\r
cc68a136 1335\r
947fb5f9 1336 if (HighCacheB[0]) DrawTilesFromCache(HighCacheB, sh, maxw);\r
7a7c6476 1337 if (hvwind == 1)\r
cc68a136 1338 DrawWindow(0, maxcells>>1, 1, sh);\r
7a7c6476 1339 else if (hvwind == 2) {\r
947fb5f9 1340 if(HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, (win&0x80) ? edge<<4 : maxw);\r
cc68a136 1341 DrawWindow((win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 1, sh);\r
1342 } else\r
947fb5f9 1343 if (HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, maxw);\r
ee4f03ae 1344 if ((rendstatus & PDRAW_HAVE_HI_SPR) || sh)\r
1345 DrawAllSpritesHiPri(1, sh);\r
cc68a136 1346\r
740da8c6 1347#if 0\r
1348 {\r
1349 int *c, a, b;\r
1350 for (a = 0, c = HighCacheA; *c; c++, a++);\r
1351 for (b = 0, c = HighCacheB; *c; c++, b++);\r
b6d7ac70 1352 printf("%i:%03i: a=%i, b=%i\n", Pico.m.frame_count, DrawScanline, a, b);\r
740da8c6 1353 }\r
1354#endif\r
1355\r
cc68a136 1356 return 0;\r
1357}\r
1358\r
1359\r
eff55556 1360PICO_INTERNAL void PicoFrameStart(void)\r
cc68a136 1361{\r
1362 // prepare to do this frame\r
ee4f03ae 1363 int sh = Pico.video.reg[0xC] & 8; // shadow/hilight?\r
1364 rendstatus = 0;\r
1365 if (PicoOpt & POPT_ACC_SPRITES)\r
1366 rendstatus |= PDRAW_ACC_SPRITES;\r
1367\r
97a7f774 1368 if (sh)\r
e5fa9817 1369 {\r
1370 DrawAllSpritesLoPri = DrawAllSprites;\r
ee4f03ae 1371 DrawAllSpritesHiPri = DrawSpritesSHi;\r
1372 }\r
1373 else\r
1374 {\r
1375 DrawAllSpritesLoPri =\r
1376 DrawAllSpritesHiPri = DrawAllSprites;\r
e5fa9817 1377 }\r
ee4f03ae 1378 if (rendstatus & PDRAW_ACC_SPRITES)\r
1379 DrawAllSpritesHiPri = DrawSpritesHiAS;\r
97a7f774 1380 if ((Pico.video.reg[12]&6) == 6) {\r
1381 rendstatus |= PDRAW_INTERLACE; // interlace mode\r
1382 DrawAllSpritesLoPri =\r
1383 DrawAllSpritesHiPri = DrawAllSpritesInterlace;\r
1384 }\r
e5fa9817 1385\r
602133e1 1386 if (Pico.m.dirtyPal) Pico.m.dirtyPal = 2; // reset dirty if needed\r
cc68a136 1387\r
d9fc2fe1 1388 DrawScanline=0;\r
cc68a136 1389 PrepareSprites(1);\r
602133e1 1390 skip_next_line=0;\r
cc68a136 1391}\r
1392\r
b6d7ac70 1393static void PicoLine(void)\r
cc68a136 1394{\r
e5fa9817 1395 int sh, as = 0;\r
b6d7ac70 1396 if (skip_next_line>0) { skip_next_line--; return; } // skip rendering lines\r
cc68a136 1397\r
cc68a136 1398 sh=(Pico.video.reg[0xC]&8)>>3; // shadow/hilight?\r
e5fa9817 1399 if (rendstatus & PDRAW_ACC_SPRITES) as|=1; // accurate sprites\r
cc68a136 1400\r
602133e1 1401 if (PicoScanBegin != NULL)\r
b6d7ac70 1402 skip_next_line = PicoScanBegin(DrawScanline);\r
602133e1 1403\r
cc68a136 1404 // Draw screen:\r
e5fa9817 1405 BackFill(Pico.video.reg[7], sh|as);\r
cc68a136 1406 if (Pico.video.reg[1]&0x40)\r
e5fa9817 1407 DrawDisplay(sh, as);\r
cc68a136 1408\r
ea8c405f 1409 if (FinalizeLine != NULL)\r
1410 FinalizeLine(sh);\r
cc68a136 1411\r
602133e1 1412 if (PicoScanEnd != NULL)\r
b6d7ac70 1413 PicoScanEnd(DrawScanline);\r
cc68a136 1414}\r
1415\r
b6d7ac70 1416void PicoDrawSync(int to, int blank_last_line)\r
1417{\r
1418 for (; DrawScanline < to; DrawScanline++)\r
1419 {\r
1420#if !CAN_HANDLE_240_LINES\r
1421 if (DrawScanline >= 224) break;\r
1422#endif\r
1423 PicoLine();\r
1424 }\r
1425\r
1426#if !CAN_HANDLE_240_LINES\r
1427 if (DrawScanline >= 224) DrawScanline = 240, return;\r
1428#endif\r
1429\r
1430 // last line\r
1431 if (DrawScanline <= to)\r
1432 {\r
1433 if (blank_last_line)\r
1434 DrawBlankedLine();\r
1435 else PicoLine();\r
1436 DrawScanline++;\r
1437 }\r
1438}\r
cc68a136 1439\r
1440void PicoDrawSetColorFormat(int which)\r
1441{\r
ea8c405f 1442 switch (which)\r
1443 {\r
1444 case 2: FinalizeLine = FinalizeLine8bit; break;\r
1445 case 1: FinalizeLine = FinalizeLineRGB555; break;\r
1446 case 0: FinalizeLine = FinalizeLineBGR444; break;\r
1447 default:FinalizeLine = NULL; break;\r
1448 }\r
499a0be3 1449#if OVERRIDE_HIGHCOL\r
1450 if (which) HighCol=DefHighCol;\r
1451#endif\r
cc68a136 1452}\r
ea8c405f 1453\r