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