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