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