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