3 * (c) Copyright Dave, 2004
\r
4 * (C) notaz, 2006-2010
\r
6 * This work is licensed under the terms of MAME license.
\r
7 * See COPYING file in the top-level directory.
\r
10 * The renderer has 4 modes now:
\r
12 * - shadow/hilight (s/h)
\r
13 * - "sonic mode" for midline palette changes (8bit mode only)
\r
14 * - accurate sprites (AS) [+ s/h]
\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
20 * since renderer always draws line in 8bit mode, there are 2 spare bits:
\r
21 * b \ mode: s/h as sonic
\r
22 * 00 normal - pal index
\r
23 * 01 shadow - pal index
\r
24 * 10 hilight+op spr spr pal index
\r
25 * 11 shadow +op spr - pal index
\r
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
32 #include "pico_int.h"
\r
34 int (*PicoScanBegin)(unsigned int num) = NULL;
\r
35 int (*PicoScanEnd) (unsigned int num) = NULL;
\r
37 static unsigned char DefHighCol[8+320+8];
\r
38 unsigned char *HighCol = DefHighCol;
\r
39 static unsigned char *HighColBase = DefHighCol;
\r
40 static int HighColIncrement;
\r
42 static unsigned int DefOutBuff[320*2/2];
\r
43 void *DrawLineDest = DefOutBuff; // pointer to dest buffer where to draw this line to
\r
44 void *DrawLineDestBase = DefOutBuff;
\r
45 int DrawLineDestIncrement;
\r
47 static int HighCacheA[41+1]; // caches for high layers
\r
48 static int HighCacheB[41+1];
\r
49 int HighPreSpr[80*2+1]; // slightly preprocessed sprites
\r
51 #define SPRL_HAVE_HI 0x80 // have hi priority sprites
\r
52 #define SPRL_HAVE_LO 0x40 // *lo*
\r
53 #define SPRL_MAY_HAVE_OP 0x20 // may have operator sprites on the line
\r
54 #define SPRL_LO_ABOVE_HI 0x10 // low priority sprites may be on top of hi
\r
55 unsigned char HighLnSpr[240][3 + MAX_LINE_SPRITES]; // sprite_count, ^flags, tile_count, [spritep]...
\r
57 int rendstatus, rendstatus_old;
\r
60 int PicoDrawMask = -1;
\r
62 static int skip_next_line=0;
\r
64 //unsigned short ppt[] = { 0x0f11, 0x0ff1, 0x01f1, 0x011f, 0x01ff, 0x0f1f, 0x0f0e, 0x0e7c };
\r
68 int nametab; // Position in VRAM of name table (for this tile line)
\r
69 int line; // Line number in pixels 0x000-0x3ff within the virtual tilemap
\r
70 int hscroll; // Horizontal scroll value in pixels for the line
\r
71 int xmask; // X-Mask (0x1f - 0x7f) for horizontal wraparound in the tilemap
\r
72 int *hc; // cache for high tile codes and their positions
\r
73 int cells; // cells (tiles) to draw (32 col mode doesn't need to update whole 320)
\r
76 // stuff available in asm:
\r
78 void DrawWindow(int tstart, int tend, int prio, int sh);
\r
79 void DrawAllSprites(unsigned char *sprited, int prio, int sh);
\r
80 void DrawTilesFromCache(int *hc, int sh, int rlim);
\r
81 void DrawSpritesSHi(unsigned char *sprited);
\r
82 void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells);
\r
83 void FinalizeLineBGR444(int sh, int line);
\r
84 void *blockcpy(void *dst, const void *src, size_t n);
\r
85 void blockcpy_or(void *dst, void *src, size_t n, int pat);
\r
88 void blockcpy_or(void *dst, void *src, size_t n, int pat)
\r
90 unsigned char *pd = dst, *ps = src;
\r
92 *pd++ = (unsigned char) (*ps++ | pat);
\r
94 #define blockcpy memcpy
\r
98 #define TileNormMaker(funcname,pix_func) \
\r
99 static int funcname(int sx,int addr,int pal) \
\r
101 unsigned char *pd = HighCol+sx; \
\r
102 unsigned int pack=0; unsigned int t=0; \
\r
104 pack=*(unsigned int *)(Pico.vram+addr); /* Get 8 pixels */ \
\r
107 t=(pack&0x0000f000)>>12; pix_func(0); \
\r
108 t=(pack&0x00000f00)>> 8; pix_func(1); \
\r
109 t=(pack&0x000000f0)>> 4; pix_func(2); \
\r
110 t=(pack&0x0000000f) ; pix_func(3); \
\r
111 t=(pack&0xf0000000)>>28; pix_func(4); \
\r
112 t=(pack&0x0f000000)>>24; pix_func(5); \
\r
113 t=(pack&0x00f00000)>>20; pix_func(6); \
\r
114 t=(pack&0x000f0000)>>16; pix_func(7); \
\r
118 return 1; /* Tile blank */ \
\r
122 #define TileFlipMaker(funcname,pix_func) \
\r
123 static int funcname(int sx,int addr,int pal) \
\r
125 unsigned char *pd = HighCol+sx; \
\r
126 unsigned int pack=0; unsigned int t=0; \
\r
128 pack=*(unsigned int *)(Pico.vram+addr); /* Get 8 pixels */ \
\r
131 t=(pack&0x000f0000)>>16; pix_func(0); \
\r
132 t=(pack&0x00f00000)>>20; pix_func(1); \
\r
133 t=(pack&0x0f000000)>>24; pix_func(2); \
\r
134 t=(pack&0xf0000000)>>28; pix_func(3); \
\r
135 t=(pack&0x0000000f) ; pix_func(4); \
\r
136 t=(pack&0x000000f0)>> 4; pix_func(5); \
\r
137 t=(pack&0x00000f00)>> 8; pix_func(6); \
\r
138 t=(pack&0x0000f000)>>12; pix_func(7); \
\r
142 return 1; /* Tile blank */ \
\r
146 #ifdef _ASM_DRAW_C_AMIPS
\r
147 int TileNorm(int sx,int addr,int pal);
\r
148 int TileFlip(int sx,int addr,int pal);
\r
151 #define pix_just_write(x) \
\r
154 TileNormMaker(TileNorm,pix_just_write)
\r
155 TileFlipMaker(TileFlip,pix_just_write)
\r
159 #ifndef _ASM_DRAW_C
\r
161 // draw a sprite pixel, process operator colors
\r
162 #define pix_sh(x) \
\r
164 else if (t>=0xe) pd[x]=(pd[x]&0x3f)|(t<<6); /* c0 shadow, 80 hilight */ \
\r
167 TileNormMaker(TileNormSH, pix_sh)
\r
168 TileFlipMaker(TileFlipSH, pix_sh)
\r
170 // draw a sprite pixel, mark operator colors
\r
171 #define pix_sh_markop(x) \
\r
173 else if (t>=0xe) pd[x]|=0x80; \
\r
176 TileNormMaker(TileNormSH_markop, pix_sh_markop)
\r
177 TileFlipMaker(TileFlipSH_markop, pix_sh_markop)
\r
179 // process operator pixels only, apply only on low pri tiles and other op pixels
\r
180 #define pix_sh_onlyop(x) \
\r
181 if (t>=0xe && (pd[x]&0xc0)) \
\r
182 pd[x]=(pd[x]&0x3f)|(t<<6); /* c0 shadow, 80 hilight */ \
\r
184 TileNormMaker(TileNormSH_onlyop_lp, pix_sh_onlyop)
\r
185 TileFlipMaker(TileFlipSH_onlyop_lp, pix_sh_onlyop)
\r
189 // draw a sprite pixel (AS)
\r
190 #define pix_as(x) \
\r
191 if (t && !(pd[x]&0x80)) pd[x]=pal|t
\r
193 TileNormMaker(TileNormAS, pix_as)
\r
194 TileFlipMaker(TileFlipAS, pix_as)
\r
196 // draw a sprite pixel, skip operator colors (AS)
\r
197 #define pix_sh_as_noop(x) \
\r
198 if (t && t < 0xe && !(pd[x]&0x80)) pd[x]=pal|t
\r
200 TileNormMaker(TileNormAS_noop, pix_sh_as_noop)
\r
201 TileFlipMaker(TileFlipAS_noop, pix_sh_as_noop)
\r
203 // mark pixel as sprite pixel (AS)
\r
204 #define pix_sh_as_onlymark(x) \
\r
207 TileNormMaker(TileNormAS_onlymark, pix_sh_as_onlymark)
\r
208 TileFlipMaker(TileFlipAS_onlymark, pix_sh_as_onlymark)
\r
211 // --------------------------------------------
\r
213 #ifndef _ASM_DRAW_C
\r
214 static void DrawStrip(struct TileStrip *ts, int plane_sh, int cellskip)
\r
216 int tilex,dx,ty,code=0,addr=0,cells;
\r
217 int oldcode=-1,blank=-1; // The tile we know is blank
\r
220 // Draw tiles across screen:
\r
221 sh=(plane_sh<<5)&0x40;
\r
222 tilex=((-ts->hscroll)>>3)+cellskip;
\r
223 ty=(ts->line&7)<<1; // Y-Offset into tile
\r
224 dx=((ts->hscroll-1)&7)+1;
\r
225 cells = ts->cells - cellskip;
\r
226 if(dx != 8) cells++; // have hscroll, need to draw 1 cell more
\r
229 for (; cells > 0; dx+=8,tilex++,cells--)
\r
233 code=Pico.vram[ts->nametab+(tilex&ts->xmask)];
\r
234 if (code==blank) continue;
\r
235 if (code>>15) { // high priority tile
\r
236 int cval = code | (dx<<16) | (ty<<25);
\r
237 if(code&0x1000) cval^=7<<26;
\r
238 *ts->hc++ = cval; // cache it
\r
242 if (code!=oldcode) {
\r
244 // Get tile address/2:
\r
245 addr=(code&0x7ff)<<4;
\r
247 if (code&0x1000) addr^=0xe; // Y-flip
\r
249 pal=((code>>9)&0x30)|sh;
\r
252 if (code&0x0800) zero=TileFlip(dx,addr,pal);
\r
253 else zero=TileNorm(dx,addr,pal);
\r
255 if (zero) blank=code; // We know this tile is blank now
\r
258 // terminate the cache list
\r
260 // if oldcode wasn't changed, it means all layer is hi priority
\r
261 if (oldcode == -1) rendstatus |= PDRAW_PLANE_HI_PRIO;
\r
265 void DrawStripVSRam(struct TileStrip *ts, int plane_sh, int cellskip)
\r
267 int tilex,dx,code=0,addr=0,cell=0;
\r
268 int oldcode=-1,blank=-1; // The tile we know is blank
\r
269 int pal=0,scan=DrawScanline;
\r
271 // Draw tiles across screen:
\r
272 tilex=(-ts->hscroll)>>3;
\r
273 dx=((ts->hscroll-1)&7)+1;
\r
274 if(dx != 8) cell--; // have hscroll, start with negative cell
\r
279 for (; cell < ts->cells; dx+=8,tilex++,cell++)
\r
281 int zero=0,nametabadd,ty;
\r
286 vscroll=Pico.vsram[(plane_sh&1)+(cell&~1)];
\r
288 // Find the line in the name table
\r
289 line=(vscroll+scan)&ts->line&0xffff; // ts->line is really ymask ..
\r
290 nametabadd=(line>>3)<<(ts->line>>24); // .. and shift[width]
\r
291 ty=(line&7)<<1; // Y-Offset into tile
\r
294 code=Pico.vram[ts->nametab+nametabadd+(tilex&ts->xmask)];
\r
295 if (code==blank) continue;
\r
296 if (code>>15) { // high priority tile
\r
297 int cval = code | (dx<<16) | (ty<<25);
\r
298 if(code&0x1000) cval^=7<<26;
\r
299 *ts->hc++ = cval; // cache it
\r
303 if (code!=oldcode) {
\r
305 // Get tile address/2:
\r
306 addr=(code&0x7ff)<<4;
\r
307 if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
\r
309 pal=((code>>9)&0x30)|((plane_sh<<5)&0x40);
\r
312 if (code&0x0800) zero=TileFlip(dx,addr,pal);
\r
313 else zero=TileNorm(dx,addr,pal);
\r
315 if (zero) blank=code; // We know this tile is blank now
\r
318 // terminate the cache list
\r
320 if (oldcode == -1) rendstatus |= PDRAW_PLANE_HI_PRIO;
\r
324 #ifndef _ASM_DRAW_C
\r
327 void DrawStripInterlace(struct TileStrip *ts)
\r
329 int tilex=0,dx=0,ty=0,code=0,addr=0,cells;
\r
330 int oldcode=-1,blank=-1; // The tile we know is blank
\r
333 // Draw tiles across screen:
\r
334 tilex=(-ts->hscroll)>>3;
\r
335 ty=(ts->line&15)<<1; // Y-Offset into tile
\r
336 dx=((ts->hscroll-1)&7)+1;
\r
338 if(dx != 8) cells++; // have hscroll, need to draw 1 cell more
\r
340 for (; cells; dx+=8,tilex++,cells--)
\r
344 code=Pico.vram[ts->nametab+(tilex&ts->xmask)];
\r
345 if (code==blank) continue;
\r
346 if (code>>15) { // high priority tile
\r
347 int cval = (code&0xfc00) | (dx<<16) | (ty<<25);
\r
348 cval|=(code&0x3ff)<<1;
\r
349 if(code&0x1000) cval^=0xf<<26;
\r
350 *ts->hc++ = cval; // cache it
\r
354 if (code!=oldcode) {
\r
356 // Get tile address/2:
\r
357 addr=(code&0x7ff)<<5;
\r
358 if (code&0x1000) addr+=30-ty; else addr+=ty; // Y-flip
\r
360 // pal=Pico.cram+((code>>9)&0x30);
\r
361 pal=((code>>9)&0x30);
\r
364 if (code&0x0800) zero=TileFlip(dx,addr,pal);
\r
365 else zero=TileNorm(dx,addr,pal);
\r
367 if (zero) blank=code; // We know this tile is blank now
\r
370 // terminate the cache list
\r
374 // --------------------------------------------
\r
376 #ifndef _ASM_DRAW_C
\r
377 static void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells)
\r
379 struct PicoVideo *pvid=&Pico.video;
\r
380 const char shift[4]={5,6,5,7}; // 32,64 or 128 sized tilemaps (2 is invalid)
\r
381 struct TileStrip ts;
\r
382 int width, height, ymask;
\r
388 // Work out the TileStrip to draw
\r
390 // Work out the name table size: 32 64 or 128 tiles (0-3)
\r
391 width=pvid->reg[16];
\r
392 height=(width>>4)&3; width&=3;
\r
394 ts.xmask=(1<<shift[width])-1; // X Mask in tiles (0x1f-0x7f)
\r
395 ymask=(height<<8)|0xff; // Y Mask in pixels
\r
396 if(width == 1) ymask&=0x1ff;
\r
397 else if(width>1) ymask =0x0ff;
\r
399 // Find name table:
\r
400 if (plane_sh&1) ts.nametab=(pvid->reg[4]&0x07)<<12; // B
\r
401 else ts.nametab=(pvid->reg[2]&0x38)<< 9; // A
\r
403 htab=pvid->reg[13]<<9; // Horizontal scroll table address
\r
404 if ( pvid->reg[11]&2) htab+=DrawScanline<<1; // Offset by line
\r
405 if ((pvid->reg[11]&1)==0) htab&=~0xf; // Offset by tile
\r
406 htab+=plane_sh&1; // A or B
\r
408 // Get horizontal scroll value, will be masked later
\r
409 ts.hscroll=Pico.vram[htab&0x7fff];
\r
411 if((pvid->reg[12]&6) == 6) {
\r
412 // interlace mode 2
\r
413 vscroll=Pico.vsram[plane_sh&1]; // Get vertical scroll value
\r
415 // Find the line in the name table
\r
416 ts.line=(vscroll+(DrawScanline<<1))&((ymask<<1)|1);
\r
417 ts.nametab+=(ts.line>>4)<<shift[width];
\r
419 DrawStripInterlace(&ts);
\r
420 } else if( pvid->reg[11]&4) {
\r
421 // shit, we have 2-cell column based vscroll
\r
422 // luckily this doesn't happen too often
\r
423 ts.line=ymask|(shift[width]<<24); // save some stuff instead of line
\r
424 DrawStripVSRam(&ts, plane_sh, cellskip);
\r
426 vscroll=Pico.vsram[plane_sh&1]; // Get vertical scroll value
\r
428 // Find the line in the name table
\r
429 ts.line=(vscroll+DrawScanline)&ymask;
\r
430 ts.nametab+=(ts.line>>3)<<shift[width];
\r
432 DrawStrip(&ts, plane_sh, cellskip);
\r
437 // --------------------------------------------
\r
439 // tstart & tend are tile pair numbers
\r
440 static void DrawWindow(int tstart, int tend, int prio, int sh) // int *hcache
\r
442 struct PicoVideo *pvid=&Pico.video;
\r
443 int tilex,ty,nametab,code=0;
\r
444 int blank=-1; // The tile we know is blank
\r
446 // Find name table line:
\r
447 if (pvid->reg[12]&1)
\r
449 nametab=(pvid->reg[3]&0x3c)<<9; // 40-cell mode
\r
450 nametab+=(DrawScanline>>3)<<6;
\r
454 nametab=(pvid->reg[3]&0x3e)<<9; // 32-cell mode
\r
455 nametab+=(DrawScanline>>3)<<5;
\r
460 if (!(rendstatus & PDRAW_WND_DIFF_PRIO)) {
\r
461 // check the first tile code
\r
462 code=Pico.vram[nametab+tilex];
\r
463 // if the whole window uses same priority (what is often the case), we may be able to skip this field
\r
464 if ((code>>15) != prio) return;
\r
468 ty=(DrawScanline&7)<<1; // Y-Offset into tile
\r
470 // Draw tiles across screen:
\r
473 for (; tilex < tend; tilex++)
\r
478 code=Pico.vram[nametab+tilex];
\r
479 if (code==blank) continue;
\r
480 if ((code>>15) != prio) {
\r
481 rendstatus |= PDRAW_WND_DIFF_PRIO;
\r
485 pal=((code>>9)&0x30);
\r
487 // Get tile address/2:
\r
488 addr=(code&0x7ff)<<4;
\r
489 if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
\r
491 if (code&0x0800) zero=TileFlip(8+(tilex<<3),addr,pal);
\r
492 else zero=TileNorm(8+(tilex<<3),addr,pal);
\r
494 if (zero) blank=code; // We know this tile is blank now
\r
499 for (; tilex < tend; tilex++)
\r
504 code=Pico.vram[nametab+tilex];
\r
505 if(code==blank) continue;
\r
506 if((code>>15) != prio) {
\r
507 rendstatus |= PDRAW_WND_DIFF_PRIO;
\r
511 pal=((code>>9)&0x30);
\r
514 int *zb = (int *)(HighCol+8+(tilex<<3));
\r
515 *zb++ &= 0xbfbfbfbf;
\r
521 // Get tile address/2:
\r
522 addr=(code&0x7ff)<<4;
\r
523 if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
\r
525 if (code&0x0800) zero=TileFlip(8+(tilex<<3),addr,pal);
\r
526 else zero=TileNorm(8+(tilex<<3),addr,pal);
\r
528 if (zero) blank=code; // We know this tile is blank now
\r
533 // --------------------------------------------
\r
535 static void DrawTilesFromCacheShPrep(void)
\r
537 // as some layer has covered whole line with hi priority tiles,
\r
538 // we can process whole line and then act as if sh/hi mode was off,
\r
539 // but leave lo pri op sprite markers alone
\r
540 int c = 320/4, *zb = (int *)(HighCol+8);
\r
541 rendstatus |= PDRAW_SHHI_DONE;
\r
544 *zb++ &= 0xbfbfbfbf;
\r
548 static void DrawTilesFromCache(int *hc, int sh, int rlim)
\r
550 int code, addr, dx;
\r
553 // *ts->hc++ = code | (dx<<16) | (ty<<25); // cache it
\r
555 if (sh && (rendstatus & (PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO)))
\r
557 if (!(rendstatus & PDRAW_SHHI_DONE))
\r
558 DrawTilesFromCacheShPrep();
\r
564 short blank=-1; // The tile we know is blank
\r
565 while ((code=*hc++)) {
\r
567 if((short)code == blank) continue;
\r
568 // Get tile address/2:
\r
569 addr=(code&0x7ff)<<4;
\r
570 addr+=(unsigned int)code>>25; // y offset into tile
\r
571 dx=(code>>16)&0x1ff;
\r
573 pal=((code>>9)&0x30);
\r
574 if (rlim-dx < 0) goto last_cut_tile;
\r
576 if (code&0x0800) zero=TileFlip(dx,addr,pal);
\r
577 else zero=TileNorm(dx,addr,pal);
\r
579 if (zero) blank=(short)code;
\r
584 while ((code=*hc++)) {
\r
586 // Get tile address/2:
\r
587 addr=(code&0x7ff)<<4;
\r
588 addr+=(unsigned int)code>>25; // y offset into tile
\r
589 dx=(code>>16)&0x1ff;
\r
591 *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf;
\r
592 *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf;
\r
594 pal=((code>>9)&0x30);
\r
595 if (rlim-dx < 0) goto last_cut_tile;
\r
597 if (code&0x0800) TileFlip(dx,addr,pal);
\r
598 else TileNorm(dx,addr,pal);
\r
605 unsigned int t, pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
606 unsigned char *pd = HighCol+dx;
\r
612 case 7: t=pack&0x00000f00; if (t) pd[6]=(unsigned char)(pal|(t>> 8)); // "break" is left out intentionally
\r
613 case 6: t=pack&0x000000f0; if (t) pd[5]=(unsigned char)(pal|(t>> 4));
\r
614 case 5: t=pack&0x0000000f; if (t) pd[4]=(unsigned char)(pal|(t ));
\r
615 case 4: t=pack&0xf0000000; if (t) pd[3]=(unsigned char)(pal|(t>>28));
\r
616 case 3: t=pack&0x0f000000; if (t) pd[2]=(unsigned char)(pal|(t>>24));
\r
617 case 2: t=pack&0x00f00000; if (t) pd[1]=(unsigned char)(pal|(t>>20));
\r
618 case 1: t=pack&0x000f0000; if (t) pd[0]=(unsigned char)(pal|(t>>16));
\r
626 case 7: t=pack&0x00f00000; if (t) pd[6]=(unsigned char)(pal|(t>>20));
\r
627 case 6: t=pack&0x0f000000; if (t) pd[5]=(unsigned char)(pal|(t>>24));
\r
628 case 5: t=pack&0xf0000000; if (t) pd[4]=(unsigned char)(pal|(t>>28));
\r
629 case 4: t=pack&0x0000000f; if (t) pd[3]=(unsigned char)(pal|(t ));
\r
630 case 3: t=pack&0x000000f0; if (t) pd[2]=(unsigned char)(pal|(t>> 4));
\r
631 case 2: t=pack&0x00000f00; if (t) pd[1]=(unsigned char)(pal|(t>> 8));
\r
632 case 1: t=pack&0x0000f000; if (t) pd[0]=(unsigned char)(pal|(t>>12));
\r
639 // --------------------------------------------
\r
641 // Index + 0 : hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size
\r
642 // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
\r
644 static void DrawSprite(int *sprite, int sh)
\r
646 int width=0,height=0;
\r
649 int tile=0,delta=0;
\r
651 int (*fTileFunc)(int sx,int addr,int pal);
\r
653 // parse the sprite data
\r
658 height=(sy>>24)&7; // Width and height in tiles
\r
659 sy=(sy<<16)>>16; // Y
\r
661 row=DrawScanline-sy; // Row of the sprite we are on
\r
663 if (code&0x1000) row=(height<<3)-1-row; // Flip Y
\r
665 tile=code + (row>>3); // Tile number increases going down
\r
666 delta=height; // Delta to increase tile by going right
\r
667 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
\r
669 tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address
\r
670 delta<<=4; // Delta of address
\r
672 pal=(code>>9)&0x30;
\r
675 if (sh && (code&0x6000) == 0x6000) {
\r
676 if(code&0x0800) fTileFunc=TileFlipSH_markop;
\r
677 else fTileFunc=TileNormSH_markop;
\r
679 if(code&0x0800) fTileFunc=TileFlip;
\r
680 else fTileFunc=TileNorm;
\r
683 for (; width; width--,sx+=8,tile+=delta)
\r
685 if(sx<=0) continue;
\r
686 if(sx>=328) break; // Offscreen
\r
688 tile&=0x7fff; // Clip tile address
\r
689 fTileFunc(sx,tile,pal);
\r
694 static void DrawSpriteInterlace(unsigned int *sprite)
\r
696 int width=0,height=0;
\r
699 int tile=0,delta=0;
\r
702 // parse the sprite data
\r
705 sy=(sy&0x3ff)-0x100; // Y
\r
706 width=(height>>2)&3; height&=3;
\r
707 width++; height++; // Width and height in tiles
\r
709 row=(DrawScanline<<1)-sy; // Row of the sprite we are on
\r
712 sx=((code>>16)&0x1ff)-0x78; // X
\r
714 if (code&0x1000) row^=(16<<height)-1; // Flip Y
\r
716 tile=code&0x3ff; // Tile number
\r
717 tile+=row>>4; // Tile number increases going down
\r
718 delta=height; // Delta to increase tile by going right
\r
719 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
\r
721 tile<<=5; tile+=(row&15)<<1; // Tile address
\r
723 delta<<=5; // Delta of address
\r
724 pal=((code>>9)&0x30); // Get palette pointer
\r
726 for (; width; width--,sx+=8,tile+=delta)
\r
728 if(sx<=0) continue;
\r
729 if(sx>=328) break; // Offscreen
\r
731 tile&=0x7fff; // Clip tile address
\r
732 if (code&0x0800) TileFlip(sx,tile,pal);
\r
733 else TileNorm(sx,tile,pal);
\r
738 static void DrawAllSpritesInterlace(int pri, int sh)
\r
740 struct PicoVideo *pvid=&Pico.video;
\r
741 int i,u,table,link=0,sline=DrawScanline<<1;
\r
742 unsigned int *sprites[80]; // Sprite index
\r
744 table=pvid->reg[5]&0x7f;
\r
745 if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode
\r
746 table<<=8; // Get sprite table address/2
\r
748 for (i=u=0; u < 80 && i < 21; u++)
\r
750 unsigned int *sprite;
\r
751 int code, sx, sy, height;
\r
753 sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite
\r
758 if(((sx>>15)&1) != pri) goto nextsprite; // wrong priority sprite
\r
760 // check if it is on this line
\r
761 sy = (code&0x3ff)-0x100;
\r
762 height = (((code>>24)&3)+1)<<4;
\r
763 if(sline < sy || sline >= sy+height) goto nextsprite; // no
\r
765 // check if sprite is not hidden offscreen
\r
766 sx = (sx>>16)&0x1ff;
\r
767 sx -= 0x78; // Get X coordinate + 8
\r
768 if(sx <= -8*3 || sx >= 328) goto nextsprite;
\r
770 // sprite is good, save it's pointer
\r
771 sprites[i++]=sprite;
\r
774 // Find next sprite
\r
775 link=(code>>16)&0x7f;
\r
776 if(!link) break; // End of sprites
\r
779 // Go through sprites backwards:
\r
780 for (i-- ;i>=0; i--)
\r
781 DrawSpriteInterlace(sprites[i]);
\r
785 #ifndef _ASM_DRAW_C
\r
787 * s/h drawing: lo_layers|40, lo_sprites|40 && mark_op,
\r
788 * hi_layers&=~40, hi_sprites
\r
790 * Index + 0 : hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size
\r
791 * Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
\r
793 static void DrawSpritesSHi(unsigned char *sprited)
\r
795 int (*fTileFunc)(int sx,int addr,int pal);
\r
799 cnt = sprited[0] & 0x7f;
\r
800 if (cnt == 0) return;
\r
804 // Go through sprites backwards:
\r
805 for (cnt--; cnt >= 0; cnt--)
\r
807 int *sprite, code, pal, tile, sx, sy;
\r
808 int offs, delta, width, height, row;
\r
810 offs = (p[cnt] & 0x7f) * 2;
\r
811 sprite = HighPreSpr + offs;
\r
813 pal = (code>>9)&0x30;
\r
817 if (code & 0x8000) // hi priority
\r
819 if (code&0x800) fTileFunc=TileFlipSH;
\r
820 else fTileFunc=TileNormSH;
\r
822 if (code&0x800) fTileFunc=TileFlipSH_onlyop_lp;
\r
823 else fTileFunc=TileNormSH_onlyop_lp;
\r
826 if (!(code & 0x8000)) continue; // non-operator low sprite, already drawn
\r
827 if (code&0x800) fTileFunc=TileFlip;
\r
828 else fTileFunc=TileNorm;
\r
831 // parse remaining sprite data
\r
835 height=(sy>>24)&7; // Width and height in tiles
\r
836 sy=(sy<<16)>>16; // Y
\r
838 row=DrawScanline-sy; // Row of the sprite we are on
\r
840 if (code&0x1000) row=(height<<3)-1-row; // Flip Y
\r
842 tile=code + (row>>3); // Tile number increases going down
\r
843 delta=height; // Delta to increase tile by going right
\r
844 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
\r
846 tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address
\r
847 delta<<=4; // Delta of address
\r
849 for (; width; width--,sx+=8,tile+=delta)
\r
851 if(sx<=0) continue;
\r
852 if(sx>=328) break; // Offscreen
\r
854 tile&=0x7fff; // Clip tile address
\r
855 fTileFunc(sx,tile,pal);
\r
859 #endif // !_ASM_DRAW_C
\r
861 static void DrawSpritesHiAS(unsigned char *sprited, int sh)
\r
863 int (*fTileFunc)(int sx,int addr,int pal);
\r
865 int entry, cnt, sh_cnt = 0;
\r
867 cnt = sprited[0] & 0x7f;
\r
868 if (cnt == 0) return;
\r
870 rendstatus |= PDRAW_SPR_LO_ON_HI;
\r
874 // Go through sprites:
\r
875 for (entry = 0; entry < cnt; entry++)
\r
877 int *sprite, code, pal, tile, sx, sy;
\r
878 int offs, delta, width, height, row;
\r
880 offs = (p[entry] & 0x7f) * 2;
\r
881 sprite = HighPreSpr + offs;
\r
883 pal = (code>>9)&0x30;
\r
885 if (code & 0x8000) // hi priority
\r
887 if (sh && pal == 0x30)
\r
889 if (code&0x800) fTileFunc=TileFlipAS_noop;
\r
890 else fTileFunc=TileNormAS_noop;
\r
892 if (code&0x800) fTileFunc=TileFlipAS;
\r
893 else fTileFunc=TileNormAS;
\r
896 if (code&0x800) fTileFunc=TileFlipAS_onlymark;
\r
897 else fTileFunc=TileNormAS_onlymark;
\r
899 if (sh && pal == 0x30)
\r
900 p[sh_cnt++] = offs / 2; // re-save for sh/hi pass
\r
902 // parse remaining sprite data
\r
906 height=(sy>>24)&7; // Width and height in tiles
\r
907 sy=(sy<<16)>>16; // Y
\r
909 row=DrawScanline-sy; // Row of the sprite we are on
\r
911 if (code&0x1000) row=(height<<3)-1-row; // Flip Y
\r
913 tile=code + (row>>3); // Tile number increases going down
\r
914 delta=height; // Delta to increase tile by going right
\r
915 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
\r
917 tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address
\r
918 delta<<=4; // Delta of address
\r
921 for (; width; width--,sx+=8,tile+=delta)
\r
923 if(sx<=0) continue;
\r
924 if(sx>=328) break; // Offscreen
\r
926 tile&=0x7fff; // Clip tile address
\r
927 fTileFunc(sx,tile,pal);
\r
931 if (!sh || !(sprited[1]&SPRL_MAY_HAVE_OP)) return;
\r
933 /* nasty 1: remove 'sprite' flags */
\r
935 int c = 320/4/4, *zb = (int *)(HighCol+8);
\r
938 *zb++ &= 0x7f7f7f7f; *zb++ &= 0x7f7f7f7f;
\r
939 *zb++ &= 0x7f7f7f7f; *zb++ &= 0x7f7f7f7f;
\r
943 /* nasty 2: sh operator pass */
\r
944 sprited[0] = sh_cnt;
\r
945 DrawSpritesSHi(sprited);
\r
949 // Index + 0 : ----hhvv -lllllll -------y yyyyyyyy
\r
950 // Index + 4 : -------x xxxxxxxx pccvhnnn nnnnnnnn
\r
952 // Index + 0 : hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size
\r
953 // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
\r
955 void PrepareSprites(int full)
\r
957 struct PicoVideo *pvid=&Pico.video;
\r
960 int *pd = HighPreSpr;
\r
961 int max_lines = 224, max_sprites = 80, max_width = 328;
\r
962 int max_line_sprites = 20; // 20 sprites, 40 tiles
\r
964 if (!(Pico.video.reg[12]&1))
\r
965 max_sprites = 64, max_line_sprites = 16, max_width = 264;
\r
966 if (PicoOpt & POPT_DIS_SPRITE_LIM)
\r
967 max_line_sprites = MAX_LINE_SPRITES;
\r
969 if (pvid->reg[1]&8) max_lines = 240;
\r
970 sh = Pico.video.reg[0xC]&8; // shadow/hilight?
\r
972 table=pvid->reg[5]&0x7f;
\r
973 if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode
\r
974 table<<=8; // Get sprite table address/2
\r
979 // updates: tilecode, sx
\r
980 for (u=0; u < max_sprites && (pack = *pd); u++, pd+=2)
\r
982 unsigned int *sprite;
\r
983 int code2, sx, sy, height;
\r
985 sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite
\r
987 // parse sprite info
\r
989 sx = (code2>>16)&0x1ff;
\r
990 sx -= 0x78; // Get X coordinate + 8
\r
991 sy = (pack << 16) >> 16;
\r
992 height = (pack >> 24) & 0xf;
\r
994 if (sy < max_lines && sy + (height<<3) > DrawScanline && // sprite onscreen (y)?
\r
995 (sx > -24 || sx < max_width)) // onscreen x
\r
997 int y = (sy >= DrawScanline) ? sy : DrawScanline;
\r
998 int entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80);
\r
999 for (; y < sy + (height<<3) && y < max_lines; y++)
\r
1002 cnt = HighLnSpr[y][0] & 0x7f;
\r
1003 if (cnt >= max_line_sprites) continue; // sprite limit?
\r
1005 for (i = 0; i < cnt; i++)
\r
1006 if (((HighLnSpr[y][3+i] ^ entry) & 0x7f) == 0) goto found;
\r
1008 // this sprite was previously missing
\r
1009 HighLnSpr[y][3+cnt] = entry;
\r
1010 HighLnSpr[y][0] = cnt + 1;
\r
1013 HighLnSpr[y][1] |= SPRL_HAVE_HI;
\r
1014 else HighLnSpr[y][1] |= SPRL_HAVE_LO;
\r
1018 code2 &= ~0xfe000000;
\r
1019 code2 -= 0x00780000; // Get X coordinate + 8 in upper 16 bits
\r
1022 // Find next sprite
\r
1023 link=(sprite[0]>>16)&0x7f;
\r
1024 if (!link) break; // End of sprites
\r
1029 for (u = 0; u < max_lines; u++)
\r
1030 *((int *)&HighLnSpr[u][0]) = 0;
\r
1032 for (u = 0; u < max_sprites; u++)
\r
1034 unsigned int *sprite;
\r
1035 int code, code2, sx, sy, hv, height, width;
\r
1037 sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite
\r
1039 // parse sprite info
\r
1041 sy = (code&0x1ff)-0x80;
\r
1042 hv = (code>>24)&0xf;
\r
1043 height = (hv&3)+1;
\r
1045 width = (hv>>2)+1;
\r
1046 code2 = sprite[1];
\r
1047 sx = (code2>>16)&0x1ff;
\r
1048 sx -= 0x78; // Get X coordinate + 8
\r
1050 if (sy < max_lines && sy + (height<<3) > DrawScanline) // sprite onscreen (y)?
\r
1052 int entry, y, sx_min, onscr_x, maybe_op = 0;
\r
1054 sx_min = 8-(width<<3);
\r
1055 onscr_x = sx_min < sx && sx < max_width;
\r
1056 if (sh && (code2 & 0x6000) == 0x6000)
\r
1057 maybe_op = SPRL_MAY_HAVE_OP;
\r
1059 entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80);
\r
1060 y = (sy >= DrawScanline) ? sy : DrawScanline;
\r
1061 for (; y < sy + (height<<3) && y < max_lines; y++)
\r
1063 unsigned char *p = &HighLnSpr[y][0];
\r
1065 if (cnt >= max_line_sprites) continue; // sprite limit?
\r
1067 if (p[2] >= max_line_sprites*2) { // tile limit?
\r
1073 if (sx == -0x78) {
\r
1075 p[0] |= 0x80; // masked, no more sprites for this line
\r
1078 // must keep the first sprite even if it's offscreen, for masking
\r
1079 if (cnt > 0 && !onscr_x) continue; // offscreen x
\r
1083 p[1] |= (entry & 0x80) ? SPRL_HAVE_HI : SPRL_HAVE_LO;
\r
1084 p[1] |= maybe_op; // there might be op sprites on this line
\r
1085 if (cnt > 0 && (code2 & 0x8000) && !(p[3+cnt-1]&0x80))
\r
1086 p[1] |= SPRL_LO_ABOVE_HI;
\r
1090 *pd++ = (width<<28)|(height<<24)|(hv<<16)|((unsigned short)sy);
\r
1091 *pd++ = (sx<<16)|((unsigned short)code2);
\r
1093 // Find next sprite
\r
1094 link=(code>>16)&0x7f;
\r
1095 if (!link) break; // End of sprites
\r
1100 for (u = 0; u < max_lines; u++)
\r
1103 printf("c%03i: %2i, %2i: ", u, HighLnSpr[u][0] & 0x7f, HighLnSpr[u][2]);
\r
1104 for (y = 0; y < HighLnSpr[u][0] & 0x7f; y++)
\r
1105 printf(" %i", HighLnSpr[u][y+3]);
\r
1112 #ifndef _ASM_DRAW_C
\r
1113 static void DrawAllSprites(unsigned char *sprited, int prio, int sh)
\r
1115 int rs = rendstatus;
\r
1119 if (rs & (PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES)) {
\r
1120 //elprintf(EL_STATUS, "PrepareSprites(%i)", (rs>>4)&1);
\r
1121 PrepareSprites(rs & PDRAW_DIRTY_SPRITES);
\r
1122 rendstatus = rs & ~(PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES);
\r
1125 cnt = sprited[0] & 0x7f;
\r
1126 if (cnt == 0) return;
\r
1130 // Go through sprites backwards:
\r
1131 for (cnt--; cnt >= 0; cnt--)
\r
1134 if ((p[cnt] >> 7) != prio) continue;
\r
1135 offs = (p[cnt]&0x7f) * 2;
\r
1136 DrawSprite(HighPreSpr + offs, sh);
\r
1141 // --------------------------------------------
\r
1143 void BackFill(int reg7, int sh)
\r
1145 unsigned int back;
\r
1147 // Start with a blank scanline (background colour):
\r
1153 memset32((int *)(HighCol+8), back, 320/4);
\r
1157 // --------------------------------------------
\r
1159 unsigned short HighPal[0x100];
\r
1161 #ifndef _ASM_DRAW_C
\r
1162 void PicoDoHighPal555(int sh)
\r
1164 unsigned int *spal, *dpal;
\r
1165 unsigned int t, i;
\r
1167 Pico.m.dirtyPal = 0;
\r
1169 spal = (void *)Pico.cram;
\r
1170 dpal = (void *)HighPal;
\r
1172 for (i = 0; i < 0x40 / 2; i++) {
\r
1175 t = ((t & 0x000e000e)<< 1) | ((t & 0x00e000e0)<<3) | ((t & 0x0e000e00)<<4);
\r
1177 t = ((t & 0x000e000e)<<12) | ((t & 0x00e000e0)<<3) | ((t & 0x0e000e00)>>7);
\r
1179 // treat it like it was 4-bit per channel, since in s/h mode it somewhat is that.
\r
1180 // otherwise intensity difference between this and s/h will be wrong
\r
1181 t |= (t >> 4) & 0x08610861; // 0x18e318e3
\r
1185 // norm: xxx0, sh: 0xxx, hi: 0xxx + 7
\r
1188 // shadowed pixels
\r
1189 for (i = 0; i < 0x40 / 2; i++)
\r
1190 dpal[0x40/2 | i] = dpal[0xc0/2 | i] = (dpal[i] >> 1) & 0x738e738e;
\r
1191 // hilighted pixels
\r
1192 for (i = 0; i < 0x40 / 2; i++) {
\r
1193 t = ((dpal[i] >> 1) & 0x738e738e) + 0x738e738e; // 0x7bef7bef;
\r
1194 t |= (t >> 4) & 0x08610861;
\r
1195 dpal[0x80/2 | i] = t;
\r
1201 static void FinalizeLineBGR444(int sh, int line)
\r
1203 unsigned short *pd=DrawLineDest;
\r
1204 unsigned char *ps=HighCol+8;
\r
1205 unsigned short *pal=Pico.cram;
\r
1206 int len, i, t, mask=0xff;
\r
1208 if (Pico.video.reg[12]&1) {
\r
1211 if(!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32;
\r
1217 if(Pico.m.dirtyPal) {
\r
1218 blockcpy(pal, Pico.cram, 0x40*2);
\r
1219 // shadowed pixels
\r
1220 for(i = 0x3f; i >= 0; i--)
\r
1221 pal[0x40|i] = pal[0xc0|i] = (unsigned short)((pal[i]>>1)&0x0777);
\r
1222 // hilighted pixels
\r
1223 for(i = 0x3f; i >= 0; i--) {
\r
1224 t=pal[i]&0xeee;t+=0x444;if(t&0x10)t|=0xe;if(t&0x100)t|=0xe0;if(t&0x1000)t|=0xe00;t&=0xeee;
\r
1225 pal[0x80|i]=(unsigned short)t;
\r
1227 Pico.m.dirtyPal = 0;
\r
1231 if (!sh && (rendstatus & PDRAW_SPR_LO_ON_HI))
\r
1232 mask=0x3f; // accurate sprites
\r
1234 for(i = 0; i < len; i++)
\r
1235 pd[i] = pal[ps[i] & mask];
\r
1240 void FinalizeLine555(int sh, int line)
\r
1242 unsigned short *pd=DrawLineDest;
\r
1243 unsigned char *ps=HighCol+8;
\r
1244 unsigned short *pal=HighPal;
\r
1247 if (Pico.m.dirtyPal)
\r
1248 PicoDoHighPal555(sh);
\r
1250 if (Pico.video.reg[12]&1) {
\r
1253 if (!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32;
\r
1260 if (!sh && (rendstatus & PDRAW_SPR_LO_ON_HI))
\r
1261 mask=0x3f; // accurate sprites, upper bits are priority stuff
\r
1263 for (i = 0; i < len; i++)
\r
1264 pd[i] = pal[ps[i] & mask];
\r
1266 extern void amips_clut(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);
\r
1267 extern void amips_clut_6bit(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);
\r
1268 if (!sh && (rendstatus & PDRAW_SPR_LO_ON_HI))
\r
1269 amips_clut_6bit(pd, ps, pal, len);
\r
1270 else amips_clut(pd, ps, pal, len);
\r
1276 static void FinalizeLine8bit(int sh, int line)
\r
1278 unsigned char *pd = DrawLineDest;
\r
1279 int len, rs = rendstatus;
\r
1280 static int dirty_count;
\r
1282 if (!sh && Pico.m.dirtyPal == 1)
\r
1284 // a hack for mid-frame palette changes
\r
1285 if (!(rs & PDRAW_SONIC_MODE))
\r
1287 else dirty_count++;
\r
1288 rs |= PDRAW_SONIC_MODE;
\r
1290 if (dirty_count == 3) {
\r
1291 blockcpy(HighPal, Pico.cram, 0x40*2);
\r
1292 } else if (dirty_count == 11) {
\r
1293 blockcpy(HighPal+0x40, Pico.cram, 0x40*2);
\r
1297 if (Pico.video.reg[12]&1) {
\r
1300 if (!(PicoOpt & POPT_DIS_32C_BORDER))
\r
1305 if (!sh && (rs & PDRAW_SONIC_MODE)) {
\r
1306 if (dirty_count >= 11) {
\r
1307 blockcpy_or(pd, HighCol+8, len, 0x80);
\r
1309 blockcpy_or(pd, HighCol+8, len, 0x40);
\r
1312 blockcpy(pd, HighCol+8, len);
\r
1316 static void (*FinalizeLine)(int sh, int line);
\r
1318 // --------------------------------------------
\r
1320 static int DrawDisplay(int sh)
\r
1322 unsigned char *sprited = &HighLnSpr[DrawScanline][0];
\r
1323 struct PicoVideo *pvid=&Pico.video;
\r
1324 int win=0,edge=0,hvwind=0;
\r
1325 int maxw,maxcells;
\r
1327 rendstatus &= ~(PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO);
\r
1329 if (pvid->reg[12]&1) {
\r
1330 maxw = 328; maxcells = 40;
\r
1332 maxw = 264; maxcells = 32;
\r
1335 // Find out if the window is on this line:
\r
1336 win=pvid->reg[0x12];
\r
1337 edge=(win&0x1f)<<3;
\r
1339 if (win&0x80) { if (DrawScanline>=edge) hvwind=1; }
\r
1340 else { if (DrawScanline< edge) hvwind=1; }
\r
1342 if (!hvwind) // we might have a vertical window here
\r
1344 win=pvid->reg[0x11];
\r
1347 if (!edge) hvwind=1;
\r
1348 else if(edge < (maxcells>>1)) hvwind=2;
\r
1351 else if(edge < (maxcells>>1)) hvwind=2;
\r
1356 /* - layer B low - */
\r
1357 if (PicoDrawMask & PDRAW_LAYERB_ON)
\r
1358 DrawLayer(1|(sh<<1), HighCacheB, 0, maxcells);
\r
1359 /* - layer A low - */
\r
1360 if (!(PicoDrawMask & PDRAW_LAYERA_ON));
\r
1361 else if (hvwind == 1)
\r
1362 DrawWindow(0, maxcells>>1, 0, sh);
\r
1363 else if (hvwind == 2) {
\r
1364 DrawLayer(0|(sh<<1), HighCacheA, (win&0x80) ? 0 : edge<<1, (win&0x80) ? edge<<1 : maxcells);
\r
1365 DrawWindow( (win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 0, sh);
\r
1367 DrawLayer(0|(sh<<1), HighCacheA, 0, maxcells);
\r
1368 /* - sprites low - */
\r
1369 if (!(PicoDrawMask & PDRAW_SPRITES_LOW_ON));
\r
1370 else if (rendstatus & PDRAW_INTERLACE)
\r
1371 DrawAllSpritesInterlace(0, sh);
\r
1372 else if (sprited[1] & SPRL_HAVE_LO)
\r
1373 DrawAllSprites(sprited, 0, sh);
\r
1375 /* - layer B hi - */
\r
1376 if ((PicoDrawMask & PDRAW_LAYERB_ON) && HighCacheB[0])
\r
1377 DrawTilesFromCache(HighCacheB, sh, maxw);
\r
1378 /* - layer A hi - */
\r
1379 if (!(PicoDrawMask & PDRAW_LAYERA_ON));
\r
1380 else if (hvwind == 1)
\r
1381 DrawWindow(0, maxcells>>1, 1, sh);
\r
1382 else if (hvwind == 2) {
\r
1383 if (HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, (win&0x80) ? edge<<4 : maxw);
\r
1384 DrawWindow((win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 1, sh);
\r
1386 if (HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, maxw);
\r
1387 /* - sprites hi - */
\r
1388 if (!(PicoDrawMask & PDRAW_SPRITES_HI_ON));
\r
1389 else if (rendstatus & PDRAW_INTERLACE)
\r
1390 DrawAllSpritesInterlace(1, sh);
\r
1391 // have sprites without layer pri bit ontop of sprites with that bit
\r
1392 else if ((sprited[1] & 0xd0) == 0xd0 && (PicoOpt & POPT_ACC_SPRITES))
\r
1393 DrawSpritesHiAS(sprited, sh);
\r
1394 else if (sh && (sprited[1] & SPRL_MAY_HAVE_OP))
\r
1395 DrawSpritesSHi(sprited);
\r
1396 else if (sprited[1] & SPRL_HAVE_HI)
\r
1397 DrawAllSprites(sprited, 1, 0);
\r
1402 for (a = 0, c = HighCacheA; *c; c++, a++);
\r
1403 for (b = 0, c = HighCacheB; *c; c++, b++);
\r
1404 printf("%i:%03i: a=%i, b=%i\n", Pico.m.frame_count, DrawScanline, a, b);
\r
1411 // MUST be called every frame
\r
1412 PICO_INTERNAL void PicoFrameStart(void)
\r
1414 int offs = 8, lines = 224;
\r
1416 // prepare to do this frame
\r
1418 if ((Pico.video.reg[12] & 6) == 6)
\r
1419 rendstatus |= PDRAW_INTERLACE; // interlace mode
\r
1420 if (!(Pico.video.reg[12] & 1))
\r
1421 rendstatus |= PDRAW_32_COLS;
\r
1422 if (Pico.video.reg[1] & 8) {
\r
1427 if (rendstatus != rendstatus_old || lines != rendlines) {
\r
1428 rendlines = lines;
\r
1429 // mode_change() might reset rendstatus_old by calling SetColorFormat
\r
1430 emu_video_mode_change((lines == 240) ? 0 : 8,
\r
1431 lines, (Pico.video.reg[12] & 1) ? 0 : 1);
\r
1432 rendstatus_old = rendstatus;
\r
1435 HighCol = HighColBase + offs * HighColIncrement;
\r
1436 DrawLineDest = (char *)DrawLineDestBase + offs * DrawLineDestIncrement;
\r
1438 skip_next_line = 0;
\r
1440 if (PicoOpt & POPT_ALT_RENDERER)
\r
1443 if (Pico.m.dirtyPal)
\r
1444 Pico.m.dirtyPal = 2; // reset dirty if needed
\r
1445 PrepareSprites(1);
\r
1448 static void DrawBlankedLine(int line, int offs, int sh, int bgc)
\r
1450 if (PicoScanBegin != NULL)
\r
1451 PicoScanBegin(line + offs);
\r
1453 BackFill(bgc, sh);
\r
1455 if (FinalizeLine != NULL)
\r
1456 FinalizeLine(sh, line);
\r
1458 if (PicoScanEnd != NULL)
\r
1459 PicoScanEnd(line + offs);
\r
1461 HighCol += HighColIncrement;
\r
1462 DrawLineDest = (char *)DrawLineDest + DrawLineDestIncrement;
\r
1465 static void PicoLine(int line, int offs, int sh, int bgc)
\r
1469 if (skip_next_line > 0) {
\r
1474 DrawScanline = line;
\r
1475 if (PicoScanBegin != NULL)
\r
1476 skip = PicoScanBegin(line + offs);
\r
1479 skip_next_line = skip - 1;
\r
1484 BackFill(bgc, sh);
\r
1485 if (Pico.video.reg[1]&0x40)
\r
1488 if (FinalizeLine != NULL)
\r
1489 FinalizeLine(sh, line);
\r
1491 if (PicoScanEnd != NULL)
\r
1492 skip_next_line = PicoScanEnd(line + offs);
\r
1494 HighCol += HighColIncrement;
\r
1495 DrawLineDest = (char *)DrawLineDest + DrawLineDestIncrement;
\r
1498 void PicoDrawSync(int to, int blank_last_line)
\r
1500 int line, offs = 0;
\r
1501 int sh = (Pico.video.reg[0xC] & 8) >> 3; // shadow/hilight?
\r
1502 int bgc = Pico.video.reg[7];
\r
1504 pprof_start(draw);
\r
1506 if (rendlines != 240)
\r
1509 for (line = DrawScanline; line < to; line++)
\r
1511 PicoLine(line, offs, sh, bgc);
\r
1517 if (blank_last_line)
\r
1518 DrawBlankedLine(line, offs, sh, bgc);
\r
1519 else PicoLine(line, offs, sh, bgc);
\r
1522 DrawScanline = line;
\r
1527 // also works for fast renderer
\r
1528 void PicoDrawUpdateHighPal(void)
\r
1530 int sh = (Pico.video.reg[0xC] & 8) >> 3; // shadow/hilight?
\r
1531 if (PicoOpt & POPT_ALT_RENDERER)
\r
1532 sh = 0; // no s/h support
\r
1534 PicoDoHighPal555(sh);
\r
1535 if (rendstatus & PDRAW_SONIC_MODE) {
\r
1537 memcpy(HighPal + 0x40, HighPal, 0x40*2);
\r
1538 memcpy(HighPal + 0x80, HighPal, 0x40*2);
\r
1542 void PicoDrawSetOutFormat(pdso_t which, int use_32x_line_mode)
\r
1547 FinalizeLine = FinalizeLine8bit;
\r
1551 if ((PicoAHW & PAHW_32X) && use_32x_line_mode)
\r
1552 FinalizeLine = FinalizeLine32xRGB555;
\r
1554 FinalizeLine = FinalizeLine555;
\r
1558 FinalizeLine = NULL;
\r
1561 PicoDrawSetOutFormat32x(which, use_32x_line_mode);
\r
1562 PicoDrawSetOutputMode4(which);
\r
1563 rendstatus_old = -1;
\r
1566 // note: may be called on the middle of frame
\r
1567 void PicoDrawSetOutBuf(void *dest, int increment)
\r
1569 DrawLineDestBase = dest;
\r
1570 DrawLineDestIncrement = increment;
\r
1571 DrawLineDest = (unsigned char*)DrawLineDestBase + DrawScanline * increment;
\r
1574 void PicoDrawSetInternalBuf(void *dest, int increment)
\r
1576 if (dest != NULL) {
\r
1577 HighColBase = dest;
\r
1578 HighColIncrement = increment;
\r
1579 HighCol = HighColBase + DrawScanline * increment;
\r
1582 HighColBase = DefHighCol;
\r
1583 HighColIncrement = 0;
\r
1587 void PicoDrawSetCallbacks(int (*begin)(unsigned int num), int (*end)(unsigned int num))
\r
1589 PicoScanBegin = NULL;
\r
1590 PicoScanEnd = NULL;
\r
1591 PicoScan32xBegin = NULL;
\r
1592 PicoScan32xEnd = NULL;
\r
1594 if ((PicoAHW & PAHW_32X) && FinalizeLine != FinalizeLine32xRGB555) {
\r
1595 PicoScan32xBegin = begin;
\r
1596 PicoScan32xEnd = end;
\r
1599 PicoScanBegin = begin;
\r
1600 PicoScanEnd = end;
\r