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