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 *DrawLineDestBase = DefOutBuff;
\r
44 int DrawLineDestIncrement;
\r
46 static int HighCacheA[41+1]; // caches for high layers
\r
47 static int HighCacheB[41+1];
\r
48 static int HighPreSpr[80*2+1]; // slightly preprocessed sprites
\r
50 #define SPRL_HAVE_HI 0x80 // have hi priority sprites
\r
51 #define SPRL_HAVE_LO 0x40 // *lo*
\r
52 #define SPRL_MAY_HAVE_OP 0x20 // may have operator sprites on the line
\r
53 #define SPRL_LO_ABOVE_HI 0x10 // low priority sprites may be on top of hi
\r
54 unsigned char HighLnSpr[240][3 + MAX_LINE_SPRITES]; // sprite_count, ^flags, tile_count, [spritep]...
\r
58 int PicoDrawMask = -1;
\r
60 static int skip_next_line=0;
\r
62 //unsigned short ppt[] = { 0x0f11, 0x0ff1, 0x01f1, 0x011f, 0x01ff, 0x0f1f, 0x0f0e, 0x0e7c };
\r
66 int nametab; // Position in VRAM of name table (for this tile line)
\r
67 int line; // Line number in pixels 0x000-0x3ff within the virtual tilemap
\r
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
74 // stuff available in asm:
\r
76 void DrawWindow(int tstart, int tend, int prio, int sh,
\r
77 struct PicoEState *est);
\r
78 void DrawAllSprites(unsigned char *sprited, int prio, int sh,
\r
79 struct PicoEState *est);
\r
80 void DrawTilesFromCache(int *hc, int sh, int rlim,
\r
81 struct PicoEState *est);
\r
82 void DrawSpritesSHi(unsigned char *sprited, struct PicoEState *est);
\r
83 void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells,
\r
84 struct PicoEState *est);
\r
85 void *blockcpy(void *dst, const void *src, size_t n);
\r
86 void blockcpy_or(void *dst, void *src, size_t n, int pat);
\r
89 void blockcpy_or(void *dst, void *src, size_t n, int pat)
\r
91 unsigned char *pd = dst, *ps = src;
\r
93 *pd++ = (unsigned char) (*ps++ | pat);
\r
95 #define blockcpy memcpy
\r
99 #define TileNormMaker(funcname,pix_func) \
\r
100 static int funcname(int sx,int addr,int pal) \
\r
102 unsigned char *pd = Pico.est.HighCol+sx; \
\r
103 unsigned int pack=0; unsigned int t=0; \
\r
105 pack=*(unsigned int *)(Pico.vram+addr); /* Get 8 pixels */ \
\r
108 t=(pack&0x0000f000)>>12; pix_func(0); \
\r
109 t=(pack&0x00000f00)>> 8; pix_func(1); \
\r
110 t=(pack&0x000000f0)>> 4; pix_func(2); \
\r
111 t=(pack&0x0000000f) ; pix_func(3); \
\r
112 t=(pack&0xf0000000)>>28; pix_func(4); \
\r
113 t=(pack&0x0f000000)>>24; pix_func(5); \
\r
114 t=(pack&0x00f00000)>>20; pix_func(6); \
\r
115 t=(pack&0x000f0000)>>16; pix_func(7); \
\r
119 return 1; /* Tile blank */ \
\r
123 #define TileFlipMaker(funcname,pix_func) \
\r
124 static int funcname(int sx,int addr,int pal) \
\r
126 unsigned char *pd = Pico.est.HighCol+sx; \
\r
127 unsigned int pack=0; unsigned int t=0; \
\r
129 pack=*(unsigned int *)(Pico.vram+addr); /* Get 8 pixels */ \
\r
132 t=(pack&0x000f0000)>>16; pix_func(0); \
\r
133 t=(pack&0x00f00000)>>20; pix_func(1); \
\r
134 t=(pack&0x0f000000)>>24; pix_func(2); \
\r
135 t=(pack&0xf0000000)>>28; pix_func(3); \
\r
136 t=(pack&0x0000000f) ; pix_func(4); \
\r
137 t=(pack&0x000000f0)>> 4; pix_func(5); \
\r
138 t=(pack&0x00000f00)>> 8; pix_func(6); \
\r
139 t=(pack&0x0000f000)>>12; pix_func(7); \
\r
143 return 1; /* Tile blank */ \
\r
147 #ifdef _ASM_DRAW_C_AMIPS
\r
148 int TileNorm(int sx,int addr,int pal);
\r
149 int TileFlip(int sx,int addr,int pal);
\r
152 #define pix_just_write(x) \
\r
155 TileNormMaker(TileNorm,pix_just_write)
\r
156 TileFlipMaker(TileFlip,pix_just_write)
\r
160 #ifndef _ASM_DRAW_C
\r
162 // draw a sprite pixel, process operator colors
\r
163 #define pix_sh(x) \
\r
165 else if (t>=0xe) pd[x]=(pd[x]&0x3f)|(t<<6); /* c0 shadow, 80 hilight */ \
\r
168 TileNormMaker(TileNormSH, pix_sh)
\r
169 TileFlipMaker(TileFlipSH, pix_sh)
\r
171 // draw a sprite pixel, mark operator colors
\r
172 #define pix_sh_markop(x) \
\r
174 else if (t>=0xe) pd[x]|=0x80; \
\r
177 TileNormMaker(TileNormSH_markop, pix_sh_markop)
\r
178 TileFlipMaker(TileFlipSH_markop, pix_sh_markop)
\r
180 // process operator pixels only, apply only on low pri tiles and other op pixels
\r
181 #define pix_sh_onlyop(x) \
\r
182 if (t>=0xe && (pd[x]&0xc0)) \
\r
183 pd[x]=(pd[x]&0x3f)|(t<<6); /* c0 shadow, 80 hilight */ \
\r
185 TileNormMaker(TileNormSH_onlyop_lp, pix_sh_onlyop)
\r
186 TileFlipMaker(TileFlipSH_onlyop_lp, pix_sh_onlyop)
\r
190 // draw a sprite pixel (AS)
\r
191 #define pix_as(x) \
\r
192 if (t && !(pd[x]&0x80)) pd[x]=pal|t
\r
194 TileNormMaker(TileNormAS, pix_as)
\r
195 TileFlipMaker(TileFlipAS, pix_as)
\r
197 // draw a sprite pixel, skip operator colors (AS)
\r
198 #define pix_sh_as_noop(x) \
\r
199 if (t && t < 0xe && !(pd[x]&0x80)) pd[x]=pal|t
\r
201 TileNormMaker(TileNormAS_noop, pix_sh_as_noop)
\r
202 TileFlipMaker(TileFlipAS_noop, pix_sh_as_noop)
\r
204 // mark pixel as sprite pixel (AS)
\r
205 #define pix_sh_as_onlymark(x) \
\r
208 TileNormMaker(TileNormAS_onlymark, pix_sh_as_onlymark)
\r
209 TileFlipMaker(TileFlipAS_onlymark, pix_sh_as_onlymark)
\r
212 // --------------------------------------------
\r
214 #ifndef _ASM_DRAW_C
\r
215 static void DrawStrip(struct TileStrip *ts, int plane_sh, int cellskip)
\r
217 int tilex,dx,ty,code=0,addr=0,cells;
\r
218 int oldcode=-1,blank=-1; // The tile we know is blank
\r
221 // Draw tiles across screen:
\r
222 sh=(plane_sh<<5)&0x40;
\r
223 tilex=((-ts->hscroll)>>3)+cellskip;
\r
224 ty=(ts->line&7)<<1; // Y-Offset into tile
\r
225 dx=((ts->hscroll-1)&7)+1;
\r
226 cells = ts->cells - cellskip;
\r
227 if(dx != 8) cells++; // have hscroll, need to draw 1 cell more
\r
230 for (; cells > 0; dx+=8,tilex++,cells--)
\r
234 code=Pico.vram[ts->nametab+(tilex&ts->xmask)];
\r
235 if (code==blank) continue;
\r
236 if (code>>15) { // high priority tile
\r
237 int cval = code | (dx<<16) | (ty<<25);
\r
238 if(code&0x1000) cval^=7<<26;
\r
239 *ts->hc++ = cval; // cache it
\r
243 if (code!=oldcode) {
\r
245 // Get tile address/2:
\r
246 addr=(code&0x7ff)<<4;
\r
248 if (code&0x1000) addr^=0xe; // Y-flip
\r
250 pal=((code>>9)&0x30)|sh;
\r
253 if (code&0x0800) zero=TileFlip(dx,addr,pal);
\r
254 else zero=TileNorm(dx,addr,pal);
\r
256 if (zero) blank=code; // We know this tile is blank now
\r
259 // terminate the cache list
\r
261 // if oldcode wasn't changed, it means all layer is hi priority
\r
262 if (oldcode == -1) Pico.est.rendstatus |= PDRAW_PLANE_HI_PRIO;
\r
266 void DrawStripVSRam(struct TileStrip *ts, int plane_sh, int cellskip)
\r
268 int tilex,dx,code=0,addr=0,cell=0;
\r
269 int oldcode=-1,blank=-1; // The tile we know is blank
\r
270 int pal=0,scan=Pico.est.DrawScanline;
\r
272 // Draw tiles across screen:
\r
273 tilex=(-ts->hscroll)>>3;
\r
274 dx=((ts->hscroll-1)&7)+1;
\r
275 if(dx != 8) cell--; // have hscroll, start with negative cell
\r
280 for (; cell < ts->cells; dx+=8,tilex++,cell++)
\r
282 int zero=0,nametabadd,ty;
\r
287 vscroll=Pico.vsram[(plane_sh&1)+(cell&~1)];
\r
289 // Find the line in the name table
\r
290 line=(vscroll+scan)&ts->line&0xffff; // ts->line is really ymask ..
\r
291 nametabadd=(line>>3)<<(ts->line>>24); // .. and shift[width]
\r
292 ty=(line&7)<<1; // Y-Offset into tile
\r
295 code=Pico.vram[ts->nametab+nametabadd+(tilex&ts->xmask)];
\r
296 if (code==blank) continue;
\r
297 if (code>>15) { // high priority tile
\r
298 int cval = code | (dx<<16) | (ty<<25);
\r
299 if(code&0x1000) cval^=7<<26;
\r
300 *ts->hc++ = cval; // cache it
\r
304 if (code!=oldcode) {
\r
306 // Get tile address/2:
\r
307 addr=(code&0x7ff)<<4;
\r
308 if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
\r
310 pal=((code>>9)&0x30)|((plane_sh<<5)&0x40);
\r
313 if (code&0x0800) zero=TileFlip(dx,addr,pal);
\r
314 else zero=TileNorm(dx,addr,pal);
\r
316 if (zero) blank=code; // We know this tile is blank now
\r
319 // terminate the cache list
\r
321 if (oldcode == -1) Pico.est.rendstatus |= PDRAW_PLANE_HI_PRIO;
\r
325 #ifndef _ASM_DRAW_C
\r
328 void DrawStripInterlace(struct TileStrip *ts)
\r
330 int tilex=0,dx=0,ty=0,code=0,addr=0,cells;
\r
331 int oldcode=-1,blank=-1; // The tile we know is blank
\r
334 // Draw tiles across screen:
\r
335 tilex=(-ts->hscroll)>>3;
\r
336 ty=(ts->line&15)<<1; // Y-Offset into tile
\r
337 dx=((ts->hscroll-1)&7)+1;
\r
339 if(dx != 8) cells++; // have hscroll, need to draw 1 cell more
\r
341 for (; cells; dx+=8,tilex++,cells--)
\r
345 code=Pico.vram[ts->nametab+(tilex&ts->xmask)];
\r
346 if (code==blank) continue;
\r
347 if (code>>15) { // high priority tile
\r
348 int cval = (code&0xfc00) | (dx<<16) | (ty<<25);
\r
349 cval|=(code&0x3ff)<<1;
\r
350 if(code&0x1000) cval^=0xf<<26;
\r
351 *ts->hc++ = cval; // cache it
\r
355 if (code!=oldcode) {
\r
357 // Get tile address/2:
\r
358 addr=(code&0x7ff)<<5;
\r
359 if (code&0x1000) addr+=30-ty; else addr+=ty; // Y-flip
\r
361 // pal=Pico.cram+((code>>9)&0x30);
\r
362 pal=((code>>9)&0x30);
\r
365 if (code&0x0800) zero=TileFlip(dx,addr,pal);
\r
366 else zero=TileNorm(dx,addr,pal);
\r
368 if (zero) blank=code; // We know this tile is blank now
\r
371 // terminate the cache list
\r
375 // --------------------------------------------
\r
377 #ifndef _ASM_DRAW_C
\r
378 static void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells,
\r
379 struct PicoEState *est)
\r
381 struct PicoVideo *pvid=&Pico.video;
\r
382 const char shift[4]={5,6,5,7}; // 32,64 or 128 sized tilemaps (2 is invalid)
\r
383 struct TileStrip ts;
\r
384 int width, height, ymask;
\r
390 // Work out the TileStrip to draw
\r
392 // Work out the name table size: 32 64 or 128 tiles (0-3)
\r
393 width=pvid->reg[16];
\r
394 height=(width>>4)&3; width&=3;
\r
396 ts.xmask=(1<<shift[width])-1; // X Mask in tiles (0x1f-0x7f)
\r
397 ymask=(height<<8)|0xff; // Y Mask in pixels
\r
398 if(width == 1) ymask&=0x1ff;
\r
399 else if(width>1) ymask =0x0ff;
\r
401 // Find name table:
\r
402 if (plane_sh&1) ts.nametab=(pvid->reg[4]&0x07)<<12; // B
\r
403 else ts.nametab=(pvid->reg[2]&0x38)<< 9; // A
\r
405 htab=pvid->reg[13]<<9; // Horizontal scroll table address
\r
406 if ( pvid->reg[11]&2) htab+=est->DrawScanline<<1; // Offset by line
\r
407 if ((pvid->reg[11]&1)==0) htab&=~0xf; // Offset by tile
\r
408 htab+=plane_sh&1; // A or B
\r
410 // Get horizontal scroll value, will be masked later
\r
411 ts.hscroll=Pico.vram[htab&0x7fff];
\r
413 if((pvid->reg[12]&6) == 6) {
\r
414 // interlace mode 2
\r
415 vscroll=Pico.vsram[plane_sh&1]; // Get vertical scroll value
\r
417 // Find the line in the name table
\r
418 ts.line=(vscroll+(est->DrawScanline<<1))&((ymask<<1)|1);
\r
419 ts.nametab+=(ts.line>>4)<<shift[width];
\r
421 DrawStripInterlace(&ts);
\r
422 } else if( pvid->reg[11]&4) {
\r
423 // shit, we have 2-cell column based vscroll
\r
424 // luckily this doesn't happen too often
\r
425 ts.line=ymask|(shift[width]<<24); // save some stuff instead of line
\r
426 DrawStripVSRam(&ts, plane_sh, cellskip);
\r
428 vscroll=Pico.vsram[plane_sh&1]; // Get vertical scroll value
\r
430 // Find the line in the name table
\r
431 ts.line=(vscroll+est->DrawScanline)&ymask;
\r
432 ts.nametab+=(ts.line>>3)<<shift[width];
\r
434 DrawStrip(&ts, plane_sh, cellskip);
\r
439 // --------------------------------------------
\r
441 // tstart & tend are tile pair numbers
\r
442 static void DrawWindow(int tstart, int tend, int prio, int sh,
\r
443 struct PicoEState *est)
\r
445 struct PicoVideo *pvid=&Pico.video;
\r
446 int tilex,ty,nametab,code=0;
\r
447 int blank=-1; // The tile we know is blank
\r
449 // Find name table line:
\r
450 if (pvid->reg[12]&1)
\r
452 nametab=(pvid->reg[3]&0x3c)<<9; // 40-cell mode
\r
453 nametab+=(est->DrawScanline>>3)<<6;
\r
457 nametab=(pvid->reg[3]&0x3e)<<9; // 32-cell mode
\r
458 nametab+=(est->DrawScanline>>3)<<5;
\r
463 if (!(est->rendstatus & PDRAW_WND_DIFF_PRIO)) {
\r
464 // check the first tile code
\r
465 code=Pico.vram[nametab+tilex];
\r
466 // if the whole window uses same priority (what is often the case), we may be able to skip this field
\r
467 if ((code>>15) != prio) return;
\r
471 ty=(est->DrawScanline&7)<<1; // Y-Offset into tile
\r
473 // Draw tiles across screen:
\r
476 for (; tilex < tend; tilex++)
\r
481 code=Pico.vram[nametab+tilex];
\r
482 if (code==blank) continue;
\r
483 if ((code>>15) != prio) {
\r
484 est->rendstatus |= PDRAW_WND_DIFF_PRIO;
\r
488 pal=((code>>9)&0x30);
\r
490 // Get tile address/2:
\r
491 addr=(code&0x7ff)<<4;
\r
492 if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
\r
494 if (code&0x0800) zero=TileFlip(8+(tilex<<3),addr,pal);
\r
495 else zero=TileNorm(8+(tilex<<3),addr,pal);
\r
497 if (zero) blank=code; // We know this tile is blank now
\r
502 for (; tilex < tend; tilex++)
\r
507 code=Pico.vram[nametab+tilex];
\r
508 if(code==blank) continue;
\r
509 if((code>>15) != prio) {
\r
510 est->rendstatus |= PDRAW_WND_DIFF_PRIO;
\r
514 pal=((code>>9)&0x30);
\r
517 int *zb = (int *)(est->HighCol+8+(tilex<<3));
\r
518 *zb++ &= 0xbfbfbfbf;
\r
524 // Get tile address/2:
\r
525 addr=(code&0x7ff)<<4;
\r
526 if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
\r
528 if (code&0x0800) zero=TileFlip(8+(tilex<<3),addr,pal);
\r
529 else zero=TileNorm(8+(tilex<<3),addr,pal);
\r
531 if (zero) blank=code; // We know this tile is blank now
\r
536 // --------------------------------------------
\r
538 static void DrawTilesFromCacheShPrep(void)
\r
540 // as some layer has covered whole line with hi priority tiles,
\r
541 // we can process whole line and then act as if sh/hi mode was off,
\r
542 // but leave lo pri op sprite markers alone
\r
543 int c = 320/4, *zb = (int *)(Pico.est.HighCol+8);
\r
544 Pico.est.rendstatus |= PDRAW_SHHI_DONE;
\r
547 *zb++ &= 0xbfbfbfbf;
\r
551 static void DrawTilesFromCache(int *hc, int sh, int rlim, struct PicoEState *est)
\r
553 int code, addr, dx;
\r
556 // *ts->hc++ = code | (dx<<16) | (ty<<25); // cache it
\r
558 if (sh && (est->rendstatus & (PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO)))
\r
560 if (!(est->rendstatus & PDRAW_SHHI_DONE))
\r
561 DrawTilesFromCacheShPrep();
\r
567 short blank=-1; // The tile we know is blank
\r
568 while ((code=*hc++)) {
\r
570 if((short)code == blank) continue;
\r
571 // Get tile address/2:
\r
572 addr=(code&0x7ff)<<4;
\r
573 addr+=(unsigned int)code>>25; // y offset into tile
\r
574 dx=(code>>16)&0x1ff;
\r
576 pal=((code>>9)&0x30);
\r
577 if (rlim-dx < 0) goto last_cut_tile;
\r
579 if (code&0x0800) zero=TileFlip(dx,addr,pal);
\r
580 else zero=TileNorm(dx,addr,pal);
\r
582 if (zero) blank=(short)code;
\r
587 while ((code=*hc++)) {
\r
589 // Get tile address/2:
\r
590 addr=(code&0x7ff)<<4;
\r
591 addr+=(unsigned int)code>>25; // y offset into tile
\r
592 dx=(code>>16)&0x1ff;
\r
593 zb = est->HighCol+dx;
\r
594 *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf;
\r
595 *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf;
\r
597 pal=((code>>9)&0x30);
\r
598 if (rlim-dx < 0) goto last_cut_tile;
\r
600 if (code&0x0800) TileFlip(dx,addr,pal);
\r
601 else TileNorm(dx,addr,pal);
\r
608 unsigned int t, pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
609 unsigned char *pd = est->HighCol+dx;
\r
615 case 7: t=pack&0x00000f00; if (t) pd[6]=(unsigned char)(pal|(t>> 8)); // "break" is left out intentionally
\r
616 case 6: t=pack&0x000000f0; if (t) pd[5]=(unsigned char)(pal|(t>> 4));
\r
617 case 5: t=pack&0x0000000f; if (t) pd[4]=(unsigned char)(pal|(t ));
\r
618 case 4: t=pack&0xf0000000; if (t) pd[3]=(unsigned char)(pal|(t>>28));
\r
619 case 3: t=pack&0x0f000000; if (t) pd[2]=(unsigned char)(pal|(t>>24));
\r
620 case 2: t=pack&0x00f00000; if (t) pd[1]=(unsigned char)(pal|(t>>20));
\r
621 case 1: t=pack&0x000f0000; if (t) pd[0]=(unsigned char)(pal|(t>>16));
\r
629 case 7: t=pack&0x00f00000; if (t) pd[6]=(unsigned char)(pal|(t>>20));
\r
630 case 6: t=pack&0x0f000000; if (t) pd[5]=(unsigned char)(pal|(t>>24));
\r
631 case 5: t=pack&0xf0000000; if (t) pd[4]=(unsigned char)(pal|(t>>28));
\r
632 case 4: t=pack&0x0000000f; if (t) pd[3]=(unsigned char)(pal|(t ));
\r
633 case 3: t=pack&0x000000f0; if (t) pd[2]=(unsigned char)(pal|(t>> 4));
\r
634 case 2: t=pack&0x00000f00; if (t) pd[1]=(unsigned char)(pal|(t>> 8));
\r
635 case 1: t=pack&0x0000f000; if (t) pd[0]=(unsigned char)(pal|(t>>12));
\r
642 // --------------------------------------------
\r
644 // Index + 0 : hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size
\r
645 // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
\r
647 static void DrawSprite(int *sprite, int sh)
\r
649 int width=0,height=0;
\r
652 int tile=0,delta=0;
\r
654 int (*fTileFunc)(int sx,int addr,int pal);
\r
656 // parse the sprite data
\r
661 height=(sy>>24)&7; // Width and height in tiles
\r
662 sy=(sy<<16)>>16; // Y
\r
664 row=Pico.est.DrawScanline-sy; // Row of the sprite we are on
\r
666 if (code&0x1000) row=(height<<3)-1-row; // Flip Y
\r
668 tile=code + (row>>3); // Tile number increases going down
\r
669 delta=height; // Delta to increase tile by going right
\r
670 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
\r
672 tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address
\r
673 delta<<=4; // Delta of address
\r
675 pal=(code>>9)&0x30;
\r
678 if (sh && (code&0x6000) == 0x6000) {
\r
679 if(code&0x0800) fTileFunc=TileFlipSH_markop;
\r
680 else fTileFunc=TileNormSH_markop;
\r
682 if(code&0x0800) fTileFunc=TileFlip;
\r
683 else fTileFunc=TileNorm;
\r
686 for (; width; width--,sx+=8,tile+=delta)
\r
688 if(sx<=0) continue;
\r
689 if(sx>=328) break; // Offscreen
\r
691 tile&=0x7fff; // Clip tile address
\r
692 fTileFunc(sx,tile,pal);
\r
697 static void DrawSpriteInterlace(unsigned int *sprite)
\r
699 int width=0,height=0;
\r
702 int tile=0,delta=0;
\r
705 // parse the sprite data
\r
708 sy=(sy&0x3ff)-0x100; // Y
\r
709 width=(height>>2)&3; height&=3;
\r
710 width++; height++; // Width and height in tiles
\r
712 row=(Pico.est.DrawScanline<<1)-sy; // Row of the sprite we are on
\r
715 sx=((code>>16)&0x1ff)-0x78; // X
\r
717 if (code&0x1000) row^=(16<<height)-1; // Flip Y
\r
719 tile=code&0x3ff; // Tile number
\r
720 tile+=row>>4; // Tile number increases going down
\r
721 delta=height; // Delta to increase tile by going right
\r
722 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
\r
724 tile<<=5; tile+=(row&15)<<1; // Tile address
\r
726 delta<<=5; // Delta of address
\r
727 pal=((code>>9)&0x30); // Get palette pointer
\r
729 for (; width; width--,sx+=8,tile+=delta)
\r
731 if(sx<=0) continue;
\r
732 if(sx>=328) break; // Offscreen
\r
734 tile&=0x7fff; // Clip tile address
\r
735 if (code&0x0800) TileFlip(sx,tile,pal);
\r
736 else TileNorm(sx,tile,pal);
\r
741 static void DrawAllSpritesInterlace(int pri, int sh)
\r
743 struct PicoVideo *pvid=&Pico.video;
\r
744 int i,u,table,link=0,sline=Pico.est.DrawScanline<<1;
\r
745 unsigned int *sprites[80]; // Sprite index
\r
747 table=pvid->reg[5]&0x7f;
\r
748 if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode
\r
749 table<<=8; // Get sprite table address/2
\r
751 for (i=u=0; u < 80 && i < 21; u++)
\r
753 unsigned int *sprite;
\r
754 int code, sx, sy, height;
\r
756 sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite
\r
761 if(((sx>>15)&1) != pri) goto nextsprite; // wrong priority sprite
\r
763 // check if it is on this line
\r
764 sy = (code&0x3ff)-0x100;
\r
765 height = (((code>>24)&3)+1)<<4;
\r
766 if(sline < sy || sline >= sy+height) goto nextsprite; // no
\r
768 // check if sprite is not hidden offscreen
\r
769 sx = (sx>>16)&0x1ff;
\r
770 sx -= 0x78; // Get X coordinate + 8
\r
771 if(sx <= -8*3 || sx >= 328) goto nextsprite;
\r
773 // sprite is good, save it's pointer
\r
774 sprites[i++]=sprite;
\r
777 // Find next sprite
\r
778 link=(code>>16)&0x7f;
\r
779 if(!link) break; // End of sprites
\r
782 // Go through sprites backwards:
\r
783 for (i-- ;i>=0; i--)
\r
784 DrawSpriteInterlace(sprites[i]);
\r
788 #ifndef _ASM_DRAW_C
\r
790 * s/h drawing: lo_layers|40, lo_sprites|40 && mark_op,
\r
791 * hi_layers&=~40, hi_sprites
\r
793 * Index + 0 : hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size
\r
794 * Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
\r
796 static void DrawSpritesSHi(unsigned char *sprited, const struct PicoEState *est)
\r
798 int (*fTileFunc)(int sx,int addr,int pal);
\r
802 cnt = sprited[0] & 0x7f;
\r
803 if (cnt == 0) return;
\r
807 // Go through sprites backwards:
\r
808 for (cnt--; cnt >= 0; cnt--)
\r
810 int *sprite, code, pal, tile, sx, sy;
\r
811 int offs, delta, width, height, row;
\r
813 offs = (p[cnt] & 0x7f) * 2;
\r
814 sprite = est->HighPreSpr + offs;
\r
816 pal = (code>>9)&0x30;
\r
820 if (code & 0x8000) // hi priority
\r
822 if (code&0x800) fTileFunc=TileFlipSH;
\r
823 else fTileFunc=TileNormSH;
\r
825 if (code&0x800) fTileFunc=TileFlipSH_onlyop_lp;
\r
826 else fTileFunc=TileNormSH_onlyop_lp;
\r
829 if (!(code & 0x8000)) continue; // non-operator low sprite, already drawn
\r
830 if (code&0x800) fTileFunc=TileFlip;
\r
831 else fTileFunc=TileNorm;
\r
834 // parse remaining sprite data
\r
838 height=(sy>>24)&7; // Width and height in tiles
\r
839 sy=(sy<<16)>>16; // Y
\r
841 row=est->DrawScanline-sy; // Row of the sprite we are on
\r
843 if (code&0x1000) row=(height<<3)-1-row; // Flip Y
\r
845 tile=code + (row>>3); // Tile number increases going down
\r
846 delta=height; // Delta to increase tile by going right
\r
847 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
\r
849 tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address
\r
850 delta<<=4; // Delta of address
\r
852 for (; width; width--,sx+=8,tile+=delta)
\r
854 if(sx<=0) continue;
\r
855 if(sx>=328) break; // Offscreen
\r
857 tile&=0x7fff; // Clip tile address
\r
858 fTileFunc(sx,tile,pal);
\r
862 #endif // !_ASM_DRAW_C
\r
864 static void DrawSpritesHiAS(unsigned char *sprited, int sh)
\r
866 int (*fTileFunc)(int sx,int addr,int pal);
\r
868 int entry, cnt, sh_cnt = 0;
\r
870 cnt = sprited[0] & 0x7f;
\r
871 if (cnt == 0) return;
\r
873 Pico.est.rendstatus |= PDRAW_SPR_LO_ON_HI;
\r
877 // Go through sprites:
\r
878 for (entry = 0; entry < cnt; entry++)
\r
880 int *sprite, code, pal, tile, sx, sy;
\r
881 int offs, delta, width, height, row;
\r
883 offs = (p[entry] & 0x7f) * 2;
\r
884 sprite = HighPreSpr + offs;
\r
886 pal = (code>>9)&0x30;
\r
888 if (code & 0x8000) // hi priority
\r
890 if (sh && pal == 0x30)
\r
892 if (code&0x800) fTileFunc=TileFlipAS_noop;
\r
893 else fTileFunc=TileNormAS_noop;
\r
895 if (code&0x800) fTileFunc=TileFlipAS;
\r
896 else fTileFunc=TileNormAS;
\r
899 if (code&0x800) fTileFunc=TileFlipAS_onlymark;
\r
900 else fTileFunc=TileNormAS_onlymark;
\r
902 if (sh && pal == 0x30)
\r
903 p[sh_cnt++] = offs / 2; // re-save for sh/hi pass
\r
905 // parse remaining sprite data
\r
909 height=(sy>>24)&7; // Width and height in tiles
\r
910 sy=(sy<<16)>>16; // Y
\r
912 row=Pico.est.DrawScanline-sy; // Row of the sprite we are on
\r
914 if (code&0x1000) row=(height<<3)-1-row; // Flip Y
\r
916 tile=code + (row>>3); // Tile number increases going down
\r
917 delta=height; // Delta to increase tile by going right
\r
918 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
\r
920 tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address
\r
921 delta<<=4; // Delta of address
\r
924 for (; width; width--,sx+=8,tile+=delta)
\r
926 if(sx<=0) continue;
\r
927 if(sx>=328) break; // Offscreen
\r
929 tile&=0x7fff; // Clip tile address
\r
930 fTileFunc(sx,tile,pal);
\r
934 if (!sh || !(sprited[1]&SPRL_MAY_HAVE_OP)) return;
\r
936 /* nasty 1: remove 'sprite' flags */
\r
938 int c = 320/4/4, *zb = (int *)(Pico.est.HighCol+8);
\r
941 *zb++ &= 0x7f7f7f7f; *zb++ &= 0x7f7f7f7f;
\r
942 *zb++ &= 0x7f7f7f7f; *zb++ &= 0x7f7f7f7f;
\r
946 /* nasty 2: sh operator pass */
\r
947 sprited[0] = sh_cnt;
\r
948 DrawSpritesSHi(sprited, &Pico.est);
\r
952 // Index + 0 : ----hhvv -lllllll -------y yyyyyyyy
\r
953 // Index + 4 : -------x xxxxxxxx pccvhnnn nnnnnnnn
\r
955 // Index + 0 : hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size
\r
956 // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
\r
958 void PrepareSprites(int full)
\r
960 const struct PicoVideo *pvid=&Pico.video;
\r
961 const struct PicoEState *est=&Pico.est;
\r
964 int *pd = HighPreSpr;
\r
965 int max_lines = 224, max_sprites = 80, max_width = 328;
\r
966 int max_line_sprites = 20; // 20 sprites, 40 tiles
\r
968 if (!(Pico.video.reg[12]&1))
\r
969 max_sprites = 64, max_line_sprites = 16, max_width = 264;
\r
970 if (PicoOpt & POPT_DIS_SPRITE_LIM)
\r
971 max_line_sprites = MAX_LINE_SPRITES;
\r
973 if (pvid->reg[1]&8) max_lines = 240;
\r
974 sh = Pico.video.reg[0xC]&8; // shadow/hilight?
\r
976 table=pvid->reg[5]&0x7f;
\r
977 if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode
\r
978 table<<=8; // Get sprite table address/2
\r
983 // updates: tilecode, sx
\r
984 for (u=0; u < max_sprites && (pack = *pd); u++, pd+=2)
\r
986 unsigned int *sprite;
\r
987 int code2, sx, sy, height;
\r
989 sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite
\r
991 // parse sprite info
\r
993 sx = (code2>>16)&0x1ff;
\r
994 sx -= 0x78; // Get X coordinate + 8
\r
995 sy = (pack << 16) >> 16;
\r
996 height = (pack >> 24) & 0xf;
\r
998 if (sy < max_lines &&
\r
999 sy + (height<<3) > est->DrawScanline && // sprite onscreen (y)?
\r
1000 (sx > -24 || sx < max_width)) // onscreen x
\r
1002 int y = (sy >= est->DrawScanline) ? sy : est->DrawScanline;
\r
1003 int entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80);
\r
1004 for (; y < sy + (height<<3) && y < max_lines; y++)
\r
1007 cnt = HighLnSpr[y][0] & 0x7f;
\r
1008 if (cnt >= max_line_sprites) continue; // sprite limit?
\r
1010 for (i = 0; i < cnt; i++)
\r
1011 if (((HighLnSpr[y][3+i] ^ entry) & 0x7f) == 0) goto found;
\r
1013 // this sprite was previously missing
\r
1014 HighLnSpr[y][3+cnt] = entry;
\r
1015 HighLnSpr[y][0] = cnt + 1;
\r
1018 HighLnSpr[y][1] |= SPRL_HAVE_HI;
\r
1019 else HighLnSpr[y][1] |= SPRL_HAVE_LO;
\r
1023 code2 &= ~0xfe000000;
\r
1024 code2 -= 0x00780000; // Get X coordinate + 8 in upper 16 bits
\r
1027 // Find next sprite
\r
1028 link=(sprite[0]>>16)&0x7f;
\r
1029 if (!link) break; // End of sprites
\r
1034 for (u = 0; u < max_lines; u++)
\r
1035 *((int *)&HighLnSpr[u][0]) = 0;
\r
1037 for (u = 0; u < max_sprites; u++)
\r
1039 unsigned int *sprite;
\r
1040 int code, code2, sx, sy, hv, height, width;
\r
1042 sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite
\r
1044 // parse sprite info
\r
1046 sy = (code&0x1ff)-0x80;
\r
1047 hv = (code>>24)&0xf;
\r
1048 height = (hv&3)+1;
\r
1050 width = (hv>>2)+1;
\r
1051 code2 = sprite[1];
\r
1052 sx = (code2>>16)&0x1ff;
\r
1053 sx -= 0x78; // Get X coordinate + 8
\r
1055 if (sy < max_lines && sy + (height<<3) > est->DrawScanline) // sprite onscreen (y)?
\r
1057 int entry, y, sx_min, onscr_x, maybe_op = 0;
\r
1059 sx_min = 8-(width<<3);
\r
1060 onscr_x = sx_min < sx && sx < max_width;
\r
1061 if (sh && (code2 & 0x6000) == 0x6000)
\r
1062 maybe_op = SPRL_MAY_HAVE_OP;
\r
1064 entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80);
\r
1065 y = (sy >= est->DrawScanline) ? sy : est->DrawScanline;
\r
1066 for (; y < sy + (height<<3) && y < max_lines; y++)
\r
1068 unsigned char *p = &HighLnSpr[y][0];
\r
1070 if (cnt >= max_line_sprites) continue; // sprite limit?
\r
1072 if (p[2] >= max_line_sprites*2) { // tile limit?
\r
1078 if (sx == -0x78) {
\r
1080 p[0] |= 0x80; // masked, no more sprites for this line
\r
1083 // must keep the first sprite even if it's offscreen, for masking
\r
1084 if (cnt > 0 && !onscr_x) continue; // offscreen x
\r
1088 p[1] |= (entry & 0x80) ? SPRL_HAVE_HI : SPRL_HAVE_LO;
\r
1089 p[1] |= maybe_op; // there might be op sprites on this line
\r
1090 if (cnt > 0 && (code2 & 0x8000) && !(p[3+cnt-1]&0x80))
\r
1091 p[1] |= SPRL_LO_ABOVE_HI;
\r
1095 *pd++ = (width<<28)|(height<<24)|(hv<<16)|((unsigned short)sy);
\r
1096 *pd++ = (sx<<16)|((unsigned short)code2);
\r
1098 // Find next sprite
\r
1099 link=(code>>16)&0x7f;
\r
1100 if (!link) break; // End of sprites
\r
1105 for (u = 0; u < max_lines; u++)
\r
1108 printf("c%03i: %2i, %2i: ", u, HighLnSpr[u][0] & 0x7f, HighLnSpr[u][2]);
\r
1109 for (y = 0; y < HighLnSpr[u][0] & 0x7f; y++)
\r
1110 printf(" %i", HighLnSpr[u][y+3]);
\r
1117 #ifndef _ASM_DRAW_C
\r
1118 static void DrawAllSprites(unsigned char *sprited, int prio, int sh,
\r
1119 struct PicoEState *est)
\r
1121 int rs = est->rendstatus;
\r
1125 if (rs & (PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES)) {
\r
1126 //elprintf(EL_STATUS, "PrepareSprites(%i)", (rs>>4)&1);
\r
1127 PrepareSprites(rs & PDRAW_DIRTY_SPRITES);
\r
1128 est->rendstatus = rs & ~(PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES);
\r
1131 cnt = sprited[0] & 0x7f;
\r
1132 if (cnt == 0) return;
\r
1136 // Go through sprites backwards:
\r
1137 for (cnt--; cnt >= 0; cnt--)
\r
1140 if ((p[cnt] >> 7) != prio) continue;
\r
1141 offs = (p[cnt]&0x7f) * 2;
\r
1142 DrawSprite(HighPreSpr + offs, sh);
\r
1147 // --------------------------------------------
\r
1149 void BackFill(int reg7, int sh, struct PicoEState *est)
\r
1151 unsigned int back;
\r
1153 // Start with a blank scanline (background colour):
\r
1159 memset32((int *)(est->HighCol+8), back, 320/4);
\r
1163 // --------------------------------------------
\r
1165 #ifndef _ASM_DRAW_C
\r
1166 void PicoDoHighPal555(int sh, int line, struct PicoEState *est)
\r
1168 unsigned int *spal, *dpal;
\r
1169 unsigned int t, i;
\r
1171 Pico.m.dirtyPal = 0;
\r
1173 spal = (void *)Pico.cram;
\r
1174 dpal = (void *)est->HighPal;
\r
1176 for (i = 0; i < 0x40 / 2; i++) {
\r
1179 t = ((t & 0x000e000e)<< 1) | ((t & 0x00e000e0)<<3) | ((t & 0x0e000e00)<<4);
\r
1181 t = ((t & 0x000e000e)<<12) | ((t & 0x00e000e0)<<3) | ((t & 0x0e000e00)>>7);
\r
1183 // treat it like it was 4-bit per channel, since in s/h mode it somewhat is that.
\r
1184 // otherwise intensity difference between this and s/h will be wrong
\r
1185 t |= (t >> 4) & 0x08610861; // 0x18e318e3
\r
1189 // norm: xxx0, sh: 0xxx, hi: 0xxx + 7
\r
1192 // shadowed pixels
\r
1193 for (i = 0; i < 0x40 / 2; i++)
\r
1194 dpal[0x40/2 | i] = dpal[0xc0/2 | i] = (dpal[i] >> 1) & 0x738e738e;
\r
1195 // hilighted pixels
\r
1196 for (i = 0; i < 0x40 / 2; i++) {
\r
1197 t = ((dpal[i] >> 1) & 0x738e738e) + 0x738e738e; // 0x7bef7bef;
\r
1198 t |= (t >> 4) & 0x08610861;
\r
1199 dpal[0x80/2 | i] = t;
\r
1204 void FinalizeLine555(int sh, int line, struct PicoEState *est)
\r
1206 unsigned short *pd=est->DrawLineDest;
\r
1207 unsigned char *ps=est->HighCol+8;
\r
1208 unsigned short *pal=est->HighPal;
\r
1211 if (Pico.m.dirtyPal)
\r
1212 PicoDoHighPal555(sh, line, est);
\r
1214 if (Pico.video.reg[12]&1) {
\r
1217 if (!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32;
\r
1224 if (!sh && (est->rendstatus & PDRAW_SPR_LO_ON_HI))
\r
1225 mask=0x3f; // accurate sprites, upper bits are priority stuff
\r
1227 for (i = 0; i < len; i++)
\r
1228 pd[i] = pal[ps[i] & mask];
\r
1230 extern void amips_clut(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);
\r
1231 extern void amips_clut_6bit(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);
\r
1232 if (!sh && (est->rendstatus & PDRAW_SPR_LO_ON_HI))
\r
1233 amips_clut_6bit(pd, ps, pal, len);
\r
1234 else amips_clut(pd, ps, pal, len);
\r
1240 static void FinalizeLine8bit(int sh, int line, struct PicoEState *est)
\r
1242 unsigned char *pd = est->DrawLineDest;
\r
1243 int len, rs = est->rendstatus;
\r
1244 static int dirty_count;
\r
1246 if (!sh && Pico.m.dirtyPal == 1)
\r
1248 // a hack for mid-frame palette changes
\r
1249 if (!(rs & PDRAW_SONIC_MODE))
\r
1251 else dirty_count++;
\r
1252 rs |= PDRAW_SONIC_MODE;
\r
1253 est->rendstatus = rs;
\r
1254 if (dirty_count == 3) {
\r
1255 blockcpy(est->HighPal, Pico.cram, 0x40*2);
\r
1256 } else if (dirty_count == 11) {
\r
1257 blockcpy(est->HighPal+0x40, Pico.cram, 0x40*2);
\r
1261 if (Pico.video.reg[12]&1) {
\r
1264 if (!(PicoOpt & POPT_DIS_32C_BORDER))
\r
1269 if (!sh && (rs & PDRAW_SONIC_MODE)) {
\r
1270 if (dirty_count >= 11) {
\r
1271 blockcpy_or(pd, est->HighCol+8, len, 0x80);
\r
1273 blockcpy_or(pd, est->HighCol+8, len, 0x40);
\r
1276 blockcpy(pd, est->HighCol+8, len);
\r
1280 static void (*FinalizeLine)(int sh, int line, struct PicoEState *est);
\r
1282 // --------------------------------------------
\r
1284 static int DrawDisplay(int sh)
\r
1286 struct PicoEState *est=&Pico.est;
\r
1287 unsigned char *sprited = &HighLnSpr[est->DrawScanline][0];
\r
1288 struct PicoVideo *pvid=&Pico.video;
\r
1289 int win=0,edge=0,hvwind=0;
\r
1290 int maxw,maxcells;
\r
1292 est->rendstatus &= ~(PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO);
\r
1294 if (pvid->reg[12]&1) {
\r
1295 maxw = 328; maxcells = 40;
\r
1297 maxw = 264; maxcells = 32;
\r
1300 // Find out if the window is on this line:
\r
1301 win=pvid->reg[0x12];
\r
1302 edge=(win&0x1f)<<3;
\r
1304 if (win&0x80) { if (est->DrawScanline>=edge) hvwind=1; }
\r
1305 else { if (est->DrawScanline< edge) hvwind=1; }
\r
1307 if (!hvwind) // we might have a vertical window here
\r
1309 win=pvid->reg[0x11];
\r
1312 if (!edge) hvwind=1;
\r
1313 else if(edge < (maxcells>>1)) hvwind=2;
\r
1316 else if(edge < (maxcells>>1)) hvwind=2;
\r
1321 /* - layer B low - */
\r
1322 if (PicoDrawMask & PDRAW_LAYERB_ON)
\r
1323 DrawLayer(1|(sh<<1), HighCacheB, 0, maxcells, est);
\r
1324 /* - layer A low - */
\r
1325 if (!(PicoDrawMask & PDRAW_LAYERA_ON));
\r
1326 else if (hvwind == 1)
\r
1327 DrawWindow(0, maxcells>>1, 0, sh, est);
\r
1328 else if (hvwind == 2) {
\r
1329 DrawLayer(0|(sh<<1), HighCacheA, (win&0x80) ? 0 : edge<<1, (win&0x80) ? edge<<1 : maxcells, est);
\r
1330 DrawWindow( (win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 0, sh, est);
\r
1332 DrawLayer(0|(sh<<1), HighCacheA, 0, maxcells, est);
\r
1333 /* - sprites low - */
\r
1334 if (!(PicoDrawMask & PDRAW_SPRITES_LOW_ON));
\r
1335 else if (Pico.est.rendstatus & PDRAW_INTERLACE)
\r
1336 DrawAllSpritesInterlace(0, sh);
\r
1337 else if (sprited[1] & SPRL_HAVE_LO)
\r
1338 DrawAllSprites(sprited, 0, sh, est);
\r
1340 /* - layer B hi - */
\r
1341 if ((PicoDrawMask & PDRAW_LAYERB_ON) && HighCacheB[0])
\r
1342 DrawTilesFromCache(HighCacheB, sh, maxw, est);
\r
1343 /* - layer A hi - */
\r
1344 if (!(PicoDrawMask & PDRAW_LAYERA_ON));
\r
1345 else if (hvwind == 1)
\r
1346 DrawWindow(0, maxcells>>1, 1, sh, est);
\r
1347 else if (hvwind == 2) {
\r
1348 if (HighCacheA[0])
\r
1349 DrawTilesFromCache(HighCacheA, sh, (win&0x80) ? edge<<4 : maxw, est);
\r
1350 DrawWindow((win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 1, sh, est);
\r
1352 if (HighCacheA[0])
\r
1353 DrawTilesFromCache(HighCacheA, sh, maxw, est);
\r
1354 /* - sprites hi - */
\r
1355 if (!(PicoDrawMask & PDRAW_SPRITES_HI_ON));
\r
1356 else if (Pico.est.rendstatus & PDRAW_INTERLACE)
\r
1357 DrawAllSpritesInterlace(1, sh);
\r
1358 // have sprites without layer pri bit ontop of sprites with that bit
\r
1359 else if ((sprited[1] & 0xd0) == 0xd0 && (PicoOpt & POPT_ACC_SPRITES))
\r
1360 DrawSpritesHiAS(sprited, sh);
\r
1361 else if (sh && (sprited[1] & SPRL_MAY_HAVE_OP))
\r
1362 DrawSpritesSHi(sprited, est);
\r
1363 else if (sprited[1] & SPRL_HAVE_HI)
\r
1364 DrawAllSprites(sprited, 1, 0, est);
\r
1369 for (a = 0, c = HighCacheA; *c; c++, a++);
\r
1370 for (b = 0, c = HighCacheB; *c; c++, b++);
\r
1371 printf("%i:%03i: a=%i, b=%i\n", Pico.m.frame_count,
\r
1372 Pico.est.DrawScanline, a, b);
\r
1379 // MUST be called every frame
\r
1380 PICO_INTERNAL void PicoFrameStart(void)
\r
1382 int offs = 8, lines = 224;
\r
1384 // prepare to do this frame
\r
1385 Pico.est.rendstatus = 0;
\r
1386 if ((Pico.video.reg[12] & 6) == 6)
\r
1387 Pico.est.rendstatus |= PDRAW_INTERLACE; // interlace mode
\r
1388 if (!(Pico.video.reg[12] & 1))
\r
1389 Pico.est.rendstatus |= PDRAW_32_COLS;
\r
1390 if (Pico.video.reg[1] & 8) {
\r
1395 if (Pico.est.rendstatus != rendstatus_old || lines != rendlines) {
\r
1396 rendlines = lines;
\r
1397 // mode_change() might reset rendstatus_old by calling SetColorFormat
\r
1398 emu_video_mode_change((lines == 240) ? 0 : 8,
\r
1399 lines, (Pico.video.reg[12] & 1) ? 0 : 1);
\r
1400 rendstatus_old = Pico.est.rendstatus;
\r
1403 Pico.est.HighCol = HighColBase + offs * HighColIncrement;
\r
1404 Pico.est.DrawLineDest = (char *)DrawLineDestBase + offs * DrawLineDestIncrement;
\r
1405 Pico.est.DrawScanline = 0;
\r
1406 skip_next_line = 0;
\r
1408 if (PicoOpt & POPT_ALT_RENDERER)
\r
1411 if (Pico.m.dirtyPal)
\r
1412 Pico.m.dirtyPal = 2; // reset dirty if needed
\r
1413 PrepareSprites(1);
\r
1416 static void DrawBlankedLine(int line, int offs, int sh, int bgc)
\r
1418 if (PicoScanBegin != NULL)
\r
1419 PicoScanBegin(line + offs);
\r
1421 BackFill(bgc, sh, &Pico.est);
\r
1423 if (FinalizeLine != NULL)
\r
1424 FinalizeLine(sh, line, &Pico.est);
\r
1426 if (PicoScanEnd != NULL)
\r
1427 PicoScanEnd(line + offs);
\r
1429 Pico.est.HighCol += HighColIncrement;
\r
1430 Pico.est.DrawLineDest = (char *)Pico.est.DrawLineDest + DrawLineDestIncrement;
\r
1433 static void PicoLine(int line, int offs, int sh, int bgc)
\r
1437 if (skip_next_line > 0) {
\r
1442 Pico.est.DrawScanline = line;
\r
1443 if (PicoScanBegin != NULL)
\r
1444 skip = PicoScanBegin(line + offs);
\r
1447 skip_next_line = skip - 1;
\r
1452 BackFill(bgc, sh, &Pico.est);
\r
1453 if (Pico.video.reg[1]&0x40)
\r
1456 if (FinalizeLine != NULL)
\r
1457 FinalizeLine(sh, line, &Pico.est);
\r
1459 if (PicoScanEnd != NULL)
\r
1460 skip_next_line = PicoScanEnd(line + offs);
\r
1462 Pico.est.HighCol += HighColIncrement;
\r
1463 Pico.est.DrawLineDest = (char *)Pico.est.DrawLineDest + DrawLineDestIncrement;
\r
1466 void PicoDrawSync(int to, int blank_last_line)
\r
1468 int line, offs = 0;
\r
1469 int sh = (Pico.video.reg[0xC] & 8) >> 3; // shadow/hilight?
\r
1470 int bgc = Pico.video.reg[7];
\r
1472 pprof_start(draw);
\r
1474 if (rendlines != 240)
\r
1477 for (line = Pico.est.DrawScanline; line < to; line++)
\r
1479 PicoLine(line, offs, sh, bgc);
\r
1485 if (blank_last_line)
\r
1486 DrawBlankedLine(line, offs, sh, bgc);
\r
1487 else PicoLine(line, offs, sh, bgc);
\r
1490 Pico.est.DrawScanline = line;
\r
1495 // also works for fast renderer
\r
1496 void PicoDrawUpdateHighPal(void)
\r
1498 struct PicoEState *est = &Pico.est;
\r
1499 int sh = (Pico.video.reg[0xC] & 8) >> 3; // shadow/hilight?
\r
1500 if (PicoOpt & POPT_ALT_RENDERER)
\r
1501 sh = 0; // no s/h support
\r
1503 PicoDoHighPal555(sh, 0, &Pico.est);
\r
1504 if (est->rendstatus & PDRAW_SONIC_MODE) {
\r
1506 memcpy(est->HighPal + 0x40, est->HighPal, 0x40*2);
\r
1507 memcpy(est->HighPal + 0x80, est->HighPal, 0x40*2);
\r
1511 void PicoDrawSetOutFormat(pdso_t which, int use_32x_line_mode)
\r
1516 FinalizeLine = FinalizeLine8bit;
\r
1520 if ((PicoAHW & PAHW_32X) && use_32x_line_mode)
\r
1521 FinalizeLine = FinalizeLine32xRGB555;
\r
1523 FinalizeLine = FinalizeLine555;
\r
1527 FinalizeLine = NULL;
\r
1530 PicoDrawSetOutFormat32x(which, use_32x_line_mode);
\r
1531 PicoDrawSetOutputMode4(which);
\r
1532 rendstatus_old = -1;
\r
1535 // note: may be called on the middle of frame
\r
1536 void PicoDrawSetOutBuf(void *dest, int increment)
\r
1538 DrawLineDestBase = dest;
\r
1539 DrawLineDestIncrement = increment;
\r
1540 Pico.est.DrawLineDest = (unsigned char*)DrawLineDestBase + Pico.est.DrawScanline * increment;
\r
1543 void PicoDrawSetInternalBuf(void *dest, int increment)
\r
1545 if (dest != NULL) {
\r
1546 HighColBase = dest;
\r
1547 HighColIncrement = increment;
\r
1548 Pico.est.HighCol = HighColBase + Pico.est.DrawScanline * increment;
\r
1551 HighColBase = DefHighCol;
\r
1552 HighColIncrement = 0;
\r
1556 void PicoDrawSetCallbacks(int (*begin)(unsigned int num), int (*end)(unsigned int num))
\r
1558 PicoScanBegin = NULL;
\r
1559 PicoScanEnd = NULL;
\r
1560 PicoScan32xBegin = NULL;
\r
1561 PicoScan32xEnd = NULL;
\r
1563 if ((PicoAHW & PAHW_32X) && FinalizeLine != FinalizeLine32xRGB555) {
\r
1564 PicoScan32xBegin = begin;
\r
1565 PicoScan32xEnd = end;
\r
1568 PicoScanBegin = begin;
\r
1569 PicoScanEnd = end;
\r
1573 void PicoDrawInit(void)
\r
1575 Pico.est.DrawLineDest = DefOutBuff;
\r
1576 Pico.est.HighCol = HighColBase;
\r
1577 Pico.est.HighPreSpr = HighPreSpr;
\r
1578 rendstatus_old = -1;
\r
1581 // vim:ts=2:sw=2:expandtab
\r