1 // This is part of Pico Library
\r
3 // (c) Copyright 2004 Dave, All rights reserved.
\r
4 // (c) Copyright 2006-2008 notaz, All rights reserved.
\r
5 // Free for non-commercial use.
\r
7 // For commercial use, separate licencing terms must be obtained.
\r
10 * The renderer has 4 modes now:
\r
12 * - shadow/hilight (s/h)
\r
13 * - "sonic mode" for midline palette changes
\r
14 * - accurate sprites (AS)
\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
21 #include "PicoInt.h"
\r
23 int (*PicoScanBegin)(unsigned int num) = NULL;
\r
24 int (*PicoScanEnd) (unsigned int num) = NULL;
\r
26 #if OVERRIDE_HIGHCOL
\r
27 static unsigned char DefHighCol[8+320+8];
\r
28 unsigned char *HighCol=DefHighCol;
\r
30 unsigned char HighCol[8+320+8];
\r
32 unsigned short DefOutBuff[320*2];
\r
33 void *DrawLineDest=DefOutBuff; // pointer to dest buffer where to draw this line to
\r
35 static int HighCacheA[41+1]; // caches for high layers
\r
36 static int HighCacheB[41+1];
\r
37 int HighPreSpr[80*2+1]; // slightly preprocessed sprites
\r
39 #define MAX_LINE_SPRITES 29
\r
40 #define SPRL_HAVE_HI 0x80 // have hi priority sprites
\r
41 #define SPRL_HAVE_LO 0x40 // *lo*
\r
42 #define SPRL_MAY_HAVE_OP 0x20 // may have operator sprites on the line
\r
43 #define SPRL_LO_ABOVE_HI 0x10 // low priority sprites may be on top of hi
\r
44 unsigned char HighLnSpr[240][3 + MAX_LINE_SPRITES]; // sprite_count, ^flags, tile_count, [spritep]...
\r
47 int DrawScanline = 0;
\r
48 int PicoDrawMask = -1;
\r
50 static int skip_next_line=0;
\r
52 //unsigned short ppt[] = { 0x0f11, 0x0ff1, 0x01f1, 0x011f, 0x01ff, 0x0f1f, 0x0f0e, 0x0e7c };
\r
56 int nametab; // Position in VRAM of name table (for this tile line)
\r
57 int line; // Line number in pixels 0x000-0x3ff within the virtual tilemap
\r
58 int hscroll; // Horizontal scroll value in pixels for the line
\r
59 int xmask; // X-Mask (0x1f - 0x7f) for horizontal wraparound in the tilemap
\r
60 int *hc; // cache for high tile codes and their positions
\r
61 int cells; // cells (tiles) to draw (32 col mode doesn't need to update whole 320)
\r
64 // stuff available in asm:
\r
66 void DrawWindow(int tstart, int tend, int prio, int sh);
\r
67 void BackFill(int reg7, int sh);
\r
68 void DrawAllSprites(unsigned char *sprited, int prio, int sh);
\r
69 void DrawTilesFromCache(int *hc, int sh, int rlim);
\r
70 void DrawSpritesSHi(unsigned char *sprited);
\r
71 void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells);
\r
72 void FinalizeLineBGR444(int sh);
\r
73 void FinalizeLineRGB555(int sh);
\r
74 void *blockcpy(void *dst, const void *src, size_t n);
\r
75 void blockcpy_or(void *dst, void *src, size_t n, int pat);
\r
78 void blockcpy_or(void *dst, void *src, size_t n, int pat)
\r
80 unsigned char *pd = dst, *ps = src;
\r
82 *pd++ = (unsigned char) (*ps++ | pat);
\r
84 #define blockcpy memcpy
\r
88 #define TileNormMaker(funcname,pix_func) \
\r
89 static int funcname(int sx,int addr,int pal) \
\r
91 unsigned char *pd = HighCol+sx; \
\r
92 unsigned int pack=0; unsigned int t=0; \
\r
94 pack=*(unsigned int *)(Pico.vram+addr); /* Get 8 pixels */ \
\r
97 t=(pack&0x0000f000)>>12; pix_func(0); \
\r
98 t=(pack&0x00000f00)>> 8; pix_func(1); \
\r
99 t=(pack&0x000000f0)>> 4; pix_func(2); \
\r
100 t=(pack&0x0000000f) ; pix_func(3); \
\r
101 t=(pack&0xf0000000)>>28; pix_func(4); \
\r
102 t=(pack&0x0f000000)>>24; pix_func(5); \
\r
103 t=(pack&0x00f00000)>>20; pix_func(6); \
\r
104 t=(pack&0x000f0000)>>16; pix_func(7); \
\r
108 return 1; /* Tile blank */ \
\r
112 #define TileFlipMaker(funcname,pix_func) \
\r
113 static int funcname(int sx,int addr,int pal) \
\r
115 unsigned char *pd = HighCol+sx; \
\r
116 unsigned int pack=0; unsigned int t=0; \
\r
118 pack=*(unsigned int *)(Pico.vram+addr); /* Get 8 pixels */ \
\r
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
132 return 1; /* Tile blank */ \
\r
136 #ifdef _ASM_DRAW_C_AMIPS
\r
137 int TileNorm(int sx,int addr,int pal);
\r
138 int TileFlip(int sx,int addr,int pal);
\r
141 #define pix_just_write(x) \
\r
144 TileNormMaker(TileNorm,pix_just_write)
\r
145 TileFlipMaker(TileFlip,pix_just_write)
\r
149 #ifndef _ASM_DRAW_C
\r
151 // draw a sprite pixel, process operator colors
\r
152 #define pix_sh(x) \
\r
154 else if (t==0xe) pd[x]=(pd[x]&0x3f)|0x80; /* hilight */ \
\r
155 else if (t==0xf) pd[x]= pd[x] |0xc0; /* shadow */ \
\r
158 TileNormMaker(TileNormSH, pix_sh)
\r
159 TileFlipMaker(TileFlipSH, pix_sh)
\r
161 // draw a sprite pixel ignoring operator colors
\r
162 #define pix_sh_noop(x) \
\r
163 if (t && t < 0xe) \
\r
166 TileNormMaker(TileNormSH_noop, pix_sh_noop)
\r
167 TileFlipMaker(TileFlipSH_noop, pix_sh_noop)
\r
169 // process operator pixels only, apply only on low pri tiles
\r
170 #define pix_sh_onlyop(x) \
\r
171 if (t==0xe && (pd[x]&0x40)) pd[x]=(pd[x]&0x3f)|0x80; /* hilight */ \
\r
172 else if (t==0xf && (pd[x]&0x40)) pd[x]= pd[x] |0xc0; /* shadow */
\r
174 TileNormMaker(TileNormSH_onlyop_lp, pix_sh_onlyop)
\r
175 TileFlipMaker(TileFlipSH_onlyop_lp, pix_sh_onlyop)
\r
179 // draw a sprite pixel (AS)
\r
180 #define pix_as(x) \
\r
181 if (t && !(pd[x]&0x80)) pd[x]=pal|t
\r
183 TileNormMaker(TileNormAS, pix_as)
\r
184 TileFlipMaker(TileFlipAS, pix_as)
\r
186 // draw a sprite pixel, skip operator colors (AS)
\r
187 #define pix_sh_as_noop(x) \
\r
188 if (t && t < 0xe && !(pd[x]&0x80)) pd[x]=pal|t
\r
190 TileNormMaker(TileNormAS_noop, pix_sh_as_noop)
\r
191 TileFlipMaker(TileFlipAS_noop, pix_sh_as_noop)
\r
193 // mark pixel as sprite pixel (AS)
\r
194 #define pix_sh_as_onlymark(x) \
\r
197 TileNormMaker(TileNormAS_onlymark, pix_sh_as_onlymark)
\r
198 TileFlipMaker(TileFlipAS_onlymark, pix_sh_as_onlymark)
\r
201 // --------------------------------------------
\r
203 #ifndef _ASM_DRAW_C
\r
204 static void DrawStrip(struct TileStrip *ts, int plane_sh, int cellskip)
\r
206 int tilex,dx,ty,code=0,addr=0,cells;
\r
207 int oldcode=-1,blank=-1; // The tile we know is blank
\r
210 // Draw tiles across screen:
\r
211 sh=(plane_sh<<5)&0x40;
\r
212 tilex=((-ts->hscroll)>>3)+cellskip;
\r
213 ty=(ts->line&7)<<1; // Y-Offset into tile
\r
214 dx=((ts->hscroll-1)&7)+1;
\r
215 cells = ts->cells - cellskip;
\r
216 if(dx != 8) cells++; // have hscroll, need to draw 1 cell more
\r
219 for (; cells > 0; dx+=8,tilex++,cells--)
\r
223 code=Pico.vram[ts->nametab+(tilex&ts->xmask)];
\r
224 if (code==blank) continue;
\r
225 if (code>>15) { // high priority tile
\r
226 int cval = code | (dx<<16) | (ty<<25);
\r
227 if(code&0x1000) cval^=7<<26;
\r
228 *ts->hc++ = cval; // cache it
\r
232 if (code!=oldcode) {
\r
234 // Get tile address/2:
\r
235 addr=(code&0x7ff)<<4;
\r
237 if (code&0x1000) addr^=0xe; // Y-flip
\r
239 pal=((code>>9)&0x30)|sh;
\r
242 if (code&0x0800) zero=TileFlip(dx,addr,pal);
\r
243 else zero=TileNorm(dx,addr,pal);
\r
245 if (zero) blank=code; // We know this tile is blank now
\r
248 // terminate the cache list
\r
250 // if oldcode wasn't changed, it means all layer is hi priority
\r
251 if (oldcode == -1) rendstatus |= PDRAW_PLANE_HI_PRIO;
\r
255 void DrawStripVSRam(struct TileStrip *ts, int plane_sh, int cellskip)
\r
257 int tilex,dx,code=0,addr=0,cell=0;
\r
258 int oldcode=-1,blank=-1; // The tile we know is blank
\r
259 int pal=0,scan=DrawScanline;
\r
261 // Draw tiles across screen:
\r
262 tilex=(-ts->hscroll)>>3;
\r
263 dx=((ts->hscroll-1)&7)+1;
\r
264 if(dx != 8) cell--; // have hscroll, start with negative cell
\r
269 for (; cell < ts->cells; dx+=8,tilex++,cell++)
\r
271 int zero=0,nametabadd,ty;
\r
276 vscroll=Pico.vsram[(plane_sh&1)+(cell&~1)];
\r
278 // Find the line in the name table
\r
279 line=(vscroll+scan)&ts->line&0xffff; // ts->line is really ymask ..
\r
280 nametabadd=(line>>3)<<(ts->line>>24); // .. and shift[width]
\r
281 ty=(line&7)<<1; // Y-Offset into tile
\r
284 code=Pico.vram[ts->nametab+nametabadd+(tilex&ts->xmask)];
\r
285 if (code==blank) continue;
\r
286 if (code>>15) { // high priority tile
\r
287 int cval = code | (dx<<16) | (ty<<25);
\r
288 if(code&0x1000) cval^=7<<26;
\r
289 *ts->hc++ = cval; // cache it
\r
293 if (code!=oldcode) {
\r
295 // Get tile address/2:
\r
296 addr=(code&0x7ff)<<4;
\r
297 if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
\r
299 pal=((code>>9)&0x30)|((plane_sh<<5)&0x40);
\r
302 if (code&0x0800) zero=TileFlip(dx,addr,pal);
\r
303 else zero=TileNorm(dx,addr,pal);
\r
305 if (zero) blank=code; // We know this tile is blank now
\r
308 // terminate the cache list
\r
310 if (oldcode == -1) rendstatus |= PDRAW_PLANE_HI_PRIO;
\r
314 #ifndef _ASM_DRAW_C
\r
317 void DrawStripInterlace(struct TileStrip *ts)
\r
319 int tilex=0,dx=0,ty=0,code=0,addr=0,cells;
\r
320 int oldcode=-1,blank=-1; // The tile we know is blank
\r
323 // Draw tiles across screen:
\r
324 tilex=(-ts->hscroll)>>3;
\r
325 ty=(ts->line&15)<<1; // Y-Offset into tile
\r
326 dx=((ts->hscroll-1)&7)+1;
\r
328 if(dx != 8) cells++; // have hscroll, need to draw 1 cell more
\r
330 for (; cells; dx+=8,tilex++,cells--)
\r
334 code=Pico.vram[ts->nametab+(tilex&ts->xmask)];
\r
335 if (code==blank) continue;
\r
336 if (code>>15) { // high priority tile
\r
337 int cval = (code&0xfc00) | (dx<<16) | (ty<<25);
\r
338 cval|=(code&0x3ff)<<1;
\r
339 if(code&0x1000) cval^=0xf<<26;
\r
340 *ts->hc++ = cval; // cache it
\r
344 if (code!=oldcode) {
\r
346 // Get tile address/2:
\r
347 addr=(code&0x7ff)<<5;
\r
348 if (code&0x1000) addr+=30-ty; else addr+=ty; // Y-flip
\r
350 // pal=Pico.cram+((code>>9)&0x30);
\r
351 pal=((code>>9)&0x30);
\r
354 if (code&0x0800) zero=TileFlip(dx,addr,pal);
\r
355 else zero=TileNorm(dx,addr,pal);
\r
357 if (zero) blank=code; // We know this tile is blank now
\r
360 // terminate the cache list
\r
364 // --------------------------------------------
\r
366 #ifndef _ASM_DRAW_C
\r
367 static void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells)
\r
369 struct PicoVideo *pvid=&Pico.video;
\r
370 const char shift[4]={5,6,5,7}; // 32,64 or 128 sized tilemaps (2 is invalid)
\r
371 struct TileStrip ts;
\r
372 int width, height, ymask;
\r
378 // Work out the TileStrip to draw
\r
380 // Work out the name table size: 32 64 or 128 tiles (0-3)
\r
381 width=pvid->reg[16];
\r
382 height=(width>>4)&3; width&=3;
\r
384 ts.xmask=(1<<shift[width])-1; // X Mask in tiles (0x1f-0x7f)
\r
385 ymask=(height<<8)|0xff; // Y Mask in pixels
\r
386 if(width == 1) ymask&=0x1ff;
\r
387 else if(width>1) ymask =0x0ff;
\r
389 // Find name table:
\r
390 if (plane_sh&1) ts.nametab=(pvid->reg[4]&0x07)<<12; // B
\r
391 else ts.nametab=(pvid->reg[2]&0x38)<< 9; // A
\r
393 htab=pvid->reg[13]<<9; // Horizontal scroll table address
\r
394 if ( pvid->reg[11]&2) htab+=DrawScanline<<1; // Offset by line
\r
395 if ((pvid->reg[11]&1)==0) htab&=~0xf; // Offset by tile
\r
396 htab+=plane_sh&1; // A or B
\r
398 // Get horizontal scroll value, will be masked later
\r
399 ts.hscroll=Pico.vram[htab&0x7fff];
\r
401 if((pvid->reg[12]&6) == 6) {
\r
402 // interlace mode 2
\r
403 vscroll=Pico.vsram[plane_sh&1]; // Get vertical scroll value
\r
405 // Find the line in the name table
\r
406 ts.line=(vscroll+(DrawScanline<<1))&((ymask<<1)|1);
\r
407 ts.nametab+=(ts.line>>4)<<shift[width];
\r
409 DrawStripInterlace(&ts);
\r
410 } else if( pvid->reg[11]&4) {
\r
411 // shit, we have 2-cell column based vscroll
\r
412 // luckily this doesn't happen too often
\r
413 ts.line=ymask|(shift[width]<<24); // save some stuff instead of line
\r
414 DrawStripVSRam(&ts, plane_sh, cellskip);
\r
416 vscroll=Pico.vsram[plane_sh&1]; // Get vertical scroll value
\r
418 // Find the line in the name table
\r
419 ts.line=(vscroll+DrawScanline)&ymask;
\r
420 ts.nametab+=(ts.line>>3)<<shift[width];
\r
422 DrawStrip(&ts, plane_sh, cellskip);
\r
427 // --------------------------------------------
\r
429 // tstart & tend are tile pair numbers
\r
430 static void DrawWindow(int tstart, int tend, int prio, int sh) // int *hcache
\r
432 struct PicoVideo *pvid=&Pico.video;
\r
433 int tilex,ty,nametab,code=0;
\r
434 int blank=-1; // The tile we know is blank
\r
436 // Find name table line:
\r
437 if (pvid->reg[12]&1)
\r
439 nametab=(pvid->reg[3]&0x3c)<<9; // 40-cell mode
\r
440 nametab+=(DrawScanline>>3)<<6;
\r
444 nametab=(pvid->reg[3]&0x3e)<<9; // 32-cell mode
\r
445 nametab+=(DrawScanline>>3)<<5;
\r
450 if (!(rendstatus & PDRAW_WND_DIFF_PRIO)) {
\r
451 // check the first tile code
\r
452 code=Pico.vram[nametab+tilex];
\r
453 // if the whole window uses same priority (what is often the case), we may be able to skip this field
\r
454 if ((code>>15) != prio) return;
\r
458 ty=(DrawScanline&7)<<1; // Y-Offset into tile
\r
460 // Draw tiles across screen:
\r
463 for (; tilex < tend; tilex++)
\r
468 code=Pico.vram[nametab+tilex];
\r
469 if (code==blank) continue;
\r
470 if ((code>>15) != prio) {
\r
471 rendstatus |= PDRAW_WND_DIFF_PRIO;
\r
475 pal=((code>>9)&0x30);
\r
477 // Get tile address/2:
\r
478 addr=(code&0x7ff)<<4;
\r
479 if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
\r
481 if (code&0x0800) zero=TileFlip(8+(tilex<<3),addr,pal);
\r
482 else zero=TileNorm(8+(tilex<<3),addr,pal);
\r
484 if (zero) blank=code; // We know this tile is blank now
\r
489 for (; tilex < tend; tilex++)
\r
494 code=Pico.vram[nametab+tilex];
\r
495 if(code==blank) continue;
\r
496 if((code>>15) != prio) {
\r
497 rendstatus |= PDRAW_WND_DIFF_PRIO;
\r
501 pal=((code>>9)&0x30);
\r
504 int *zb = (int *)(HighCol+8+(tilex<<3));
\r
505 *zb++ &= 0x3f3f3f3f;
\r
511 // Get tile address/2:
\r
512 addr=(code&0x7ff)<<4;
\r
513 if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
\r
515 if (code&0x0800) zero=TileFlip(8+(tilex<<3),addr,pal);
\r
516 else zero=TileNorm(8+(tilex<<3),addr,pal);
\r
518 if (zero) blank=code; // We know this tile is blank now
\r
523 // --------------------------------------------
\r
525 static void DrawTilesFromCacheShPrep(void)
\r
527 // as some layer has covered whole line with hi priority tiles,
\r
528 // we can process whole line and then act as if sh/hi mode was off.
\r
529 int c = 320/4, *zb = (int *)(HighCol+8);
\r
530 rendstatus |= PDRAW_SHHI_DONE;
\r
533 *zb++ &= 0x3f3f3f3f;
\r
537 static void DrawTilesFromCache(int *hc, int sh, int rlim)
\r
539 int code, addr, dx;
\r
542 // *ts->hc++ = code | (dx<<16) | (ty<<25); // cache it
\r
544 if (sh && (rendstatus & (PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO)))
\r
546 if (!(rendstatus & PDRAW_SHHI_DONE))
\r
547 DrawTilesFromCacheShPrep();
\r
553 short blank=-1; // The tile we know is blank
\r
554 while ((code=*hc++)) {
\r
556 if((short)code == blank) continue;
\r
557 // Get tile address/2:
\r
558 addr=(code&0x7ff)<<4;
\r
559 addr+=(unsigned int)code>>25; // y offset into tile
\r
560 dx=(code>>16)&0x1ff;
\r
562 pal=((code>>9)&0x30);
\r
563 if (rlim-dx < 0) goto last_cut_tile;
\r
565 if (code&0x0800) zero=TileFlip(dx,addr,pal);
\r
566 else zero=TileNorm(dx,addr,pal);
\r
568 if (zero) blank=(short)code;
\r
573 while ((code=*hc++)) {
\r
575 // Get tile address/2:
\r
576 addr=(code&0x7ff)<<4;
\r
577 addr+=(unsigned int)code>>25; // y offset into tile
\r
578 dx=(code>>16)&0x1ff;
\r
580 *zb++ &= 0x3f; *zb++ &= 0x3f; *zb++ &= 0x3f; *zb++ &= 0x3f;
\r
581 *zb++ &= 0x3f; *zb++ &= 0x3f; *zb++ &= 0x3f; *zb++ &= 0x3f;
\r
583 pal=((code>>9)&0x30);
\r
584 if (rlim-dx < 0) goto last_cut_tile;
\r
586 if (code&0x0800) TileFlip(dx,addr,pal);
\r
587 else TileNorm(dx,addr,pal);
\r
594 unsigned int t, pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
595 unsigned char *pd = HighCol+dx;
\r
601 case 7: t=pack&0x00000f00; if (t) pd[6]=(unsigned char)(pal|(t>> 8)); // "break" is left out intentionally
\r
602 case 6: t=pack&0x000000f0; if (t) pd[5]=(unsigned char)(pal|(t>> 4));
\r
603 case 5: t=pack&0x0000000f; if (t) pd[4]=(unsigned char)(pal|(t ));
\r
604 case 4: t=pack&0xf0000000; if (t) pd[3]=(unsigned char)(pal|(t>>28));
\r
605 case 3: t=pack&0x0f000000; if (t) pd[2]=(unsigned char)(pal|(t>>24));
\r
606 case 2: t=pack&0x00f00000; if (t) pd[1]=(unsigned char)(pal|(t>>20));
\r
607 case 1: t=pack&0x000f0000; if (t) pd[0]=(unsigned char)(pal|(t>>16));
\r
615 case 7: t=pack&0x00f00000; if (t) pd[6]=(unsigned char)(pal|(t>>20));
\r
616 case 6: t=pack&0x0f000000; if (t) pd[5]=(unsigned char)(pal|(t>>24));
\r
617 case 5: t=pack&0xf0000000; if (t) pd[4]=(unsigned char)(pal|(t>>28));
\r
618 case 4: t=pack&0x0000000f; if (t) pd[3]=(unsigned char)(pal|(t ));
\r
619 case 3: t=pack&0x000000f0; if (t) pd[2]=(unsigned char)(pal|(t>> 4));
\r
620 case 2: t=pack&0x00000f00; if (t) pd[1]=(unsigned char)(pal|(t>> 8));
\r
621 case 1: t=pack&0x0000f000; if (t) pd[0]=(unsigned char)(pal|(t>>12));
\r
628 // --------------------------------------------
\r
630 // Index + 0 : hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size
\r
631 // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
\r
633 static void DrawSprite(int *sprite, int sh)
\r
635 int width=0,height=0;
\r
638 int tile=0,delta=0;
\r
640 int (*fTileFunc)(int sx,int addr,int pal);
\r
642 // parse the sprite data
\r
647 height=(sy>>24)&7; // Width and height in tiles
\r
648 sy=(sy<<16)>>16; // Y
\r
650 row=DrawScanline-sy; // Row of the sprite we are on
\r
652 if (code&0x1000) row=(height<<3)-1-row; // Flip Y
\r
654 tile=code + (row>>3); // Tile number increases going down
\r
655 delta=height; // Delta to increase tile by going right
\r
656 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
\r
658 tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address
\r
659 delta<<=4; // Delta of address
\r
661 pal=(code>>9)&0x30;
\r
664 if (sh && (code&0x6000) == 0x6000) {
\r
665 if(code&0x0800) fTileFunc=TileFlipSH_noop;
\r
666 else fTileFunc=TileNormSH_noop;
\r
668 if(code&0x0800) fTileFunc=TileFlip;
\r
669 else fTileFunc=TileNorm;
\r
672 for (; width; width--,sx+=8,tile+=delta)
\r
674 if(sx<=0) continue;
\r
675 if(sx>=328) break; // Offscreen
\r
677 tile&=0x7fff; // Clip tile address
\r
678 fTileFunc(sx,tile,pal);
\r
683 static void DrawSpriteInterlace(unsigned int *sprite)
\r
685 int width=0,height=0;
\r
688 int tile=0,delta=0;
\r
691 // parse the sprite data
\r
694 sy=(sy&0x3ff)-0x100; // Y
\r
695 width=(height>>2)&3; height&=3;
\r
696 width++; height++; // Width and height in tiles
\r
698 row=(DrawScanline<<1)-sy; // Row of the sprite we are on
\r
701 sx=((code>>16)&0x1ff)-0x78; // X
\r
703 if (code&0x1000) row^=(16<<height)-1; // Flip Y
\r
705 tile=code&0x3ff; // Tile number
\r
706 tile+=row>>4; // Tile number increases going down
\r
707 delta=height; // Delta to increase tile by going right
\r
708 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
\r
710 tile<<=5; tile+=(row&15)<<1; // Tile address
\r
712 delta<<=5; // Delta of address
\r
713 pal=((code>>9)&0x30); // Get palette pointer
\r
715 for (; width; width--,sx+=8,tile+=delta)
\r
717 if(sx<=0) continue;
\r
718 if(sx>=328) break; // Offscreen
\r
720 tile&=0x7fff; // Clip tile address
\r
721 if (code&0x0800) TileFlip(sx,tile,pal);
\r
722 else TileNorm(sx,tile,pal);
\r
727 static void DrawAllSpritesInterlace(int pri, int sh)
\r
729 struct PicoVideo *pvid=&Pico.video;
\r
730 int i,u,table,link=0,sline=DrawScanline<<1;
\r
731 unsigned int *sprites[80]; // Sprite index
\r
733 table=pvid->reg[5]&0x7f;
\r
734 if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode
\r
735 table<<=8; // Get sprite table address/2
\r
737 for (i=u=0; u < 80 && i < 21; u++)
\r
739 unsigned int *sprite;
\r
740 int code, sx, sy, height;
\r
742 sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite
\r
747 if(((sx>>15)&1) != pri) goto nextsprite; // wrong priority sprite
\r
749 // check if it is on this line
\r
750 sy = (code&0x3ff)-0x100;
\r
751 height = (((code>>24)&3)+1)<<4;
\r
752 if(sline < sy || sline >= sy+height) goto nextsprite; // no
\r
754 // check if sprite is not hidden offscreen
\r
755 sx = (sx>>16)&0x1ff;
\r
756 sx -= 0x78; // Get X coordinate + 8
\r
757 if(sx <= -8*3 || sx >= 328) goto nextsprite;
\r
759 // sprite is good, save it's pointer
\r
760 sprites[i++]=sprite;
\r
763 // Find next sprite
\r
764 link=(code>>16)&0x7f;
\r
765 if(!link) break; // End of sprites
\r
768 // Go through sprites backwards:
\r
769 for (i-- ;i>=0; i--)
\r
770 DrawSpriteInterlace(sprites[i]);
\r
774 #ifndef _ASM_DRAW_C
\r
775 // Index + 0 : hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size
\r
776 // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
\r
777 static void DrawSpritesSHi(unsigned char *sprited)
\r
779 int (*fTileFunc)(int sx,int addr,int pal);
\r
783 cnt = sprited[0] & 0x7f;
\r
784 if (cnt == 0) return;
\r
788 // Go through sprites backwards:
\r
789 for (cnt--; cnt >= 0; cnt--)
\r
791 int *sprite, code, pal, tile, sx, sy;
\r
792 int offs, delta, width, height, row;
\r
794 offs = (p[cnt] & 0x7f) * 2;
\r
795 sprite = HighPreSpr + offs;
\r
797 pal = (code>>9)&0x30;
\r
801 if (code & 0x8000) // hi priority
\r
803 if (code&0x800) fTileFunc=TileFlipSH;
\r
804 else fTileFunc=TileNormSH;
\r
806 if (code&0x800) fTileFunc=TileFlipSH_onlyop_lp;
\r
807 else fTileFunc=TileNormSH_onlyop_lp;
\r
810 if (!(code & 0x8000)) continue; // non-operator low sprite, already drawn
\r
811 if (code&0x800) fTileFunc=TileFlip;
\r
812 else fTileFunc=TileNorm;
\r
815 // parse remaining sprite data
\r
819 height=(sy>>24)&7; // Width and height in tiles
\r
820 sy=(sy<<16)>>16; // Y
\r
822 row=DrawScanline-sy; // Row of the sprite we are on
\r
824 if (code&0x1000) row=(height<<3)-1-row; // Flip Y
\r
826 tile=code + (row>>3); // Tile number increases going down
\r
827 delta=height; // Delta to increase tile by going right
\r
828 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
\r
830 tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address
\r
831 delta<<=4; // Delta of address
\r
833 for (; width; width--,sx+=8,tile+=delta)
\r
835 if(sx<=0) continue;
\r
836 if(sx>=328) break; // Offscreen
\r
838 tile&=0x7fff; // Clip tile address
\r
839 fTileFunc(sx,tile,pal);
\r
845 static void DrawSpritesHiAS(unsigned char *sprited, int sh)
\r
847 int (*fTileFunc)(int sx,int addr,int pal);
\r
849 int entry, cnt, sh_cnt = 0;
\r
851 cnt = sprited[0] & 0x7f;
\r
852 if (cnt == 0) return;
\r
856 // Go through sprites:
\r
857 for (entry = 0; entry < cnt; entry++)
\r
859 int *sprite, code, pal, tile, sx, sy;
\r
860 int offs, delta, width, height, row;
\r
862 offs = (p[entry] & 0x7f) * 2;
\r
863 sprite = HighPreSpr + offs;
\r
865 pal = (code>>9)&0x30;
\r
867 if (code & 0x8000) // hi priority
\r
869 if (sh && pal == 0x30)
\r
871 if (code&0x800) fTileFunc=TileFlipAS_noop;
\r
872 else fTileFunc=TileNormAS_noop;
\r
874 if (code&0x800) fTileFunc=TileFlipAS;
\r
875 else fTileFunc=TileNormAS;
\r
878 if (code&0x800) fTileFunc=TileFlipAS_onlymark;
\r
879 else fTileFunc=TileNormAS_onlymark;
\r
881 if (sh && pal == 0x30)
\r
882 p[sh_cnt++] = offs / 2; // re-save for sh/hi pass
\r
884 // parse remaining sprite data
\r
888 height=(sy>>24)&7; // Width and height in tiles
\r
889 sy=(sy<<16)>>16; // Y
\r
891 row=DrawScanline-sy; // Row of the sprite we are on
\r
893 if (code&0x1000) row=(height<<3)-1-row; // Flip Y
\r
895 tile=code + (row>>3); // Tile number increases going down
\r
896 delta=height; // Delta to increase tile by going right
\r
897 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
\r
899 tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address
\r
900 delta<<=4; // Delta of address
\r
903 for (; width; width--,sx+=8,tile+=delta)
\r
905 if(sx<=0) continue;
\r
906 if(sx>=328) break; // Offscreen
\r
908 tile&=0x7fff; // Clip tile address
\r
909 fTileFunc(sx,tile,pal);
\r
913 if (!sh || !(sprited[1]&SPRL_MAY_HAVE_OP)) return;
\r
915 /* nasty 1: remove 'sprite' flags */
\r
917 int c = 320/4, *zb = (int *)(HighCol+8);
\r
920 *zb++ &= 0x7f7f7f7f;
\r
924 /* nasty 2: sh operator pass */
\r
925 sprited[0] = sh_cnt;
\r
926 DrawSpritesSHi(sprited);
\r
930 // Index + 0 : ----hhvv -lllllll -------y yyyyyyyy
\r
931 // Index + 4 : -------x xxxxxxxx pccvhnnn nnnnnnnn
\r
933 // Index + 0 : hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size
\r
934 // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
\r
936 void PrepareSprites(int full)
\r
938 struct PicoVideo *pvid=&Pico.video;
\r
941 int *pd = HighPreSpr;
\r
942 int max_lines = 224, max_sprites = 80, max_width = 328;
\r
943 int max_line_sprites = 20; // 20 sprites, 40 tiles
\r
945 if (!(Pico.video.reg[12]&1))
\r
946 max_sprites = 64, max_line_sprites = 16, max_width = 264;
\r
947 if (PicoOpt & POPT_DIS_SPRITE_LIM)
\r
948 max_line_sprites = MAX_LINE_SPRITES;
\r
950 if (pvid->reg[1]&8) max_lines = 240;
\r
951 sh = Pico.video.reg[0xC]&8; // shadow/hilight?
\r
953 table=pvid->reg[5]&0x7f;
\r
954 if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode
\r
955 table<<=8; // Get sprite table address/2
\r
960 // updates: tilecode, sx
\r
961 for (u=0; u < max_lines && (pack = *pd); u++, pd+=2)
\r
963 unsigned int *sprite;
\r
964 int code2, sx, sy, height;
\r
966 sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite
\r
968 // parse sprite info
\r
970 sx = (code2>>16)&0x1ff;
\r
971 sx -= 0x78; // Get X coordinate + 8
\r
972 sy = (pack << 16) >> 16;
\r
973 height = (pack >> 24) & 0xf;
\r
975 if (sy < max_lines && sy + (height<<3) > DrawScanline && // sprite onscreen (y)?
\r
976 (sx > -24 || sx < max_width)) // onscreen x
\r
978 int y = (sy >= DrawScanline) ? sy : DrawScanline;
\r
979 int entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80);
\r
980 for (; y < sy + (height<<3) && y < max_lines; y++)
\r
983 cnt = HighLnSpr[y][0] & 0x7f;
\r
984 if (cnt >= max_line_sprites) continue; // sprite limit?
\r
986 for (i = 0; i < cnt; i++)
\r
987 if (((HighLnSpr[y][3+i] ^ entry) & 0x7f) == 0) goto found;
\r
989 // this sprite was previously missing
\r
990 HighLnSpr[y][3+cnt] = entry;
\r
991 HighLnSpr[y][0] = cnt + 1;
\r
994 HighLnSpr[y][1] |= SPRL_HAVE_HI;
\r
995 else HighLnSpr[y][1] |= SPRL_HAVE_LO;
\r
999 code2 &= ~0xfe000000;
\r
1000 code2 -= 0x00780000; // Get X coordinate + 8 in upper 16 bits
\r
1003 // Find next sprite
\r
1004 link=(sprite[0]>>16)&0x7f;
\r
1005 if (!link) break; // End of sprites
\r
1010 for (u = 0; u < max_lines; u++)
\r
1011 *((int *)&HighLnSpr[u][0]) = 0;
\r
1013 for (u = 0; u < max_lines; u++)
\r
1015 unsigned int *sprite;
\r
1016 int code, code2, sx, sy, hv, height, width;
\r
1018 sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite
\r
1020 // parse sprite info
\r
1022 sy = (code&0x1ff)-0x80;
\r
1023 hv = (code>>24)&0xf;
\r
1024 height = (hv&3)+1;
\r
1026 width = (hv>>2)+1;
\r
1027 code2 = sprite[1];
\r
1028 sx = (code2>>16)&0x1ff;
\r
1029 sx -= 0x78; // Get X coordinate + 8
\r
1031 if (sy < max_lines && sy + (height<<3) > DrawScanline) // sprite onscreen (y)?
\r
1033 int entry, y, sx_min, onscr_x, maybe_op = 0;
\r
1035 sx_min = 8-(width<<3);
\r
1036 onscr_x = sx_min < sx && sx < max_width;
\r
1037 if (sh && (code2 & 0x6000) == 0x6000)
\r
1038 maybe_op = SPRL_MAY_HAVE_OP;
\r
1040 entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80);
\r
1041 y = (sy >= DrawScanline) ? sy : DrawScanline;
\r
1042 for (; y < sy + (height<<3) && y < max_lines; y++)
\r
1044 unsigned char *p = &HighLnSpr[y][0];
\r
1046 if (cnt >= max_line_sprites) continue; // sprite limit?
\r
1048 if (p[2] >= max_line_sprites*2) { // tile limit?
\r
1054 if (sx == -0x78) {
\r
1056 p[0] |= 0x80; // masked, no more sprites for this line
\r
1059 // must keep the first sprite even if it's offscreen, for masking
\r
1060 if (cnt > 0 && !onscr_x) continue; // offscreen x
\r
1064 p[1] |= (entry & 0x80) ? SPRL_HAVE_HI : SPRL_HAVE_LO;
\r
1065 p[1] |= maybe_op; // there might be op sprites on this line
\r
1066 if (cnt > 0 && (code2 & 0x8000) && !(p[3+cnt-1]&0x80))
\r
1067 p[1] |= SPRL_LO_ABOVE_HI;
\r
1071 *pd++ = (width<<28)|(height<<24)|(hv<<16)|((unsigned short)sy);
\r
1072 *pd++ = (sx<<16)|((unsigned short)code2);
\r
1074 // Find next sprite
\r
1075 link=(code>>16)&0x7f;
\r
1076 if (!link) break; // End of sprites
\r
1081 for (u = 0; u < max_lines; u++)
\r
1084 printf("c%03i: %2i, %2i: ", u, HighLnSpr[u][0] & 0x7f, HighLnSpr[u][2]);
\r
1085 for (y = 0; y < HighLnSpr[u][0] & 0x7f; y++)
\r
1086 printf(" %i", HighLnSpr[u][y+3]);
\r
1093 #ifndef _ASM_DRAW_C
\r
1094 static void DrawAllSprites(unsigned char *sprited, int prio, int sh)
\r
1096 int rs = rendstatus;
\r
1100 if (rs & (PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES)) {
\r
1101 //elprintf(EL_STATUS, "PrepareSprites(%i)", (rs>>4)&1);
\r
1102 PrepareSprites(rs & PDRAW_DIRTY_SPRITES);
\r
1103 rendstatus = rs & ~(PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES);
\r
1106 cnt = sprited[0] & 0x7f;
\r
1107 if (cnt == 0) return;
\r
1111 // Go through sprites backwards:
\r
1112 for (cnt--; cnt >= 0; cnt--)
\r
1115 if ((p[cnt] >> 7) != prio) continue;
\r
1116 offs = (p[cnt]&0x7f) * 2;
\r
1117 DrawSprite(HighPreSpr + offs, sh);
\r
1122 // --------------------------------------------
\r
1124 static void BackFill(int reg7, int sh)
\r
1126 unsigned int back;
\r
1128 // Start with a blank scanline (background colour):
\r
1134 memset32((int *)(HighCol+8), back, 320/4);
\r
1138 // --------------------------------------------
\r
1140 unsigned short HighPal[0x100];
\r
1142 #ifndef _ASM_DRAW_C
\r
1143 static void FinalizeLineBGR444(int sh)
\r
1145 unsigned short *pd=DrawLineDest;
\r
1146 unsigned char *ps=HighCol+8;
\r
1147 unsigned short *pal=Pico.cram;
\r
1148 int len, i, t, mask=0xff;
\r
1150 if (Pico.video.reg[12]&1) {
\r
1153 if(!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32;
\r
1159 if(Pico.m.dirtyPal) {
\r
1160 blockcpy(pal, Pico.cram, 0x40*2);
\r
1161 // shadowed pixels
\r
1162 for(i = 0x3f; i >= 0; i--)
\r
1163 pal[0x40|i] = pal[0xc0|i] = (unsigned short)((pal[i]>>1)&0x0777);
\r
1164 // hilighted pixels
\r
1165 for(i = 0x3f; i >= 0; i--) {
\r
1166 t=pal[i]&0xeee;t+=0x444;if(t&0x10)t|=0xe;if(t&0x100)t|=0xe0;if(t&0x1000)t|=0xe00;t&=0xeee;
\r
1167 pal[0x80|i]=(unsigned short)t;
\r
1169 Pico.m.dirtyPal = 0;
\r
1173 if (!sh && (rendstatus & PDRAW_ACC_SPRITES))
\r
1174 mask=0x3f; // accurate sprites
\r
1176 for(i = 0; i < len; i++)
\r
1177 pd[i] = pal[ps[i] & mask];
\r
1181 static void FinalizeLineRGB555(int sh)
\r
1183 unsigned short *pd=DrawLineDest;
\r
1184 unsigned char *ps=HighCol+8;
\r
1185 unsigned short *pal=HighPal;
\r
1186 int len, i, t, dirtyPal = Pico.m.dirtyPal;
\r
1190 unsigned int *spal=(void *)Pico.cram;
\r
1191 unsigned int *dpal=(void *)HighPal;
\r
1192 for (i = 0x3f/2; i >= 0; i--)
\r
1194 dpal[i] = ((spal[i]&0x000f000f)<< 1)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)<<4);
\r
1196 dpal[i] = ((spal[i]&0x000f000f)<<12)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)>>7);
\r
1198 Pico.m.dirtyPal = 0;
\r
1204 // shadowed pixels
\r
1205 for (i = 0x3f; i >= 0; i--)
\r
1206 pal[0x40|i] = pal[0xc0|i] = (unsigned short)((pal[i]>>1)&0x738e);
\r
1207 // hilighted pixels
\r
1208 for (i = 0x3f; i >= 0; i--) {
\r
1209 t=pal[i]&0xe71c;t+=0x4208;if(t&0x20)t|=0x1c;if(t&0x800)t|=0x700;if(t&0x10000)t|=0xe000;t&=0xe71c;
\r
1210 pal[0x80|i]=(unsigned short)t;
\r
1215 if (Pico.video.reg[12]&1) {
\r
1218 if (!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32;
\r
1225 if (!sh && (rendstatus & PDRAW_ACC_SPRITES))
\r
1226 mask=0x3f; // accurate sprites, upper bits are priority stuff
\r
1228 for (i = 0; i < len; i++)
\r
1229 pd[i] = pal[ps[i] & mask];
\r
1231 extern void amips_clut(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);
\r
1232 extern void amips_clut_6bit(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);
\r
1233 if (!sh && (rendstatus & PDRAW_ACC_SPRITES))
\r
1234 amips_clut_6bit(pd, ps, pal, len);
\r
1235 else amips_clut(pd, ps, pal, len);
\r
1241 static void FinalizeLine8bit(int sh)
\r
1243 unsigned char *pd=DrawLineDest;
\r
1244 int len, rs = rendstatus;
\r
1245 static int dirty_count;
\r
1247 if (!sh && !(rs & PDRAW_ACC_SPRITES) && Pico.m.dirtyPal == 1 && DrawScanline < 222)
\r
1249 // a hack for mid-frame palette changes
\r
1250 if (!(rs & PDRAW_SONIC_MODE))
\r
1252 else dirty_count++;
\r
1253 rs |= PDRAW_SONIC_MODE;
\r
1255 if (dirty_count == 3) {
\r
1256 blockcpy(HighPal, Pico.cram, 0x40*2);
\r
1257 } else if (dirty_count == 11) {
\r
1258 blockcpy(HighPal+0x40, Pico.cram, 0x40*2);
\r
1262 if (Pico.video.reg[12]&1) {
\r
1265 if (!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32;
\r
1269 if (!sh && (rs & PDRAW_SONIC_MODE)) {
\r
1270 if (dirty_count >= 11) {
\r
1271 blockcpy_or(pd, HighCol+8, len, 0x80);
\r
1273 blockcpy_or(pd, HighCol+8, len, 0x40);
\r
1276 blockcpy(pd, HighCol+8, len);
\r
1280 static void (*FinalizeLine)(int sh) = FinalizeLineBGR444;
\r
1282 // --------------------------------------------
\r
1284 static void DrawBlankedLine(void)
\r
1286 int sh=(Pico.video.reg[0xC]&8)>>3; // shadow/hilight?
\r
1288 if (PicoScanBegin != NULL)
\r
1289 PicoScanBegin(DrawScanline);
\r
1291 BackFill(Pico.video.reg[7], sh);
\r
1293 if (FinalizeLine != NULL)
\r
1296 if (PicoScanEnd != NULL)
\r
1297 PicoScanEnd(DrawScanline);
\r
1300 static int DrawDisplay(int sh)
\r
1302 unsigned char *sprited = &HighLnSpr[DrawScanline][0];
\r
1303 struct PicoVideo *pvid=&Pico.video;
\r
1304 int win=0,edge=0,hvwind=0;
\r
1305 int maxw,maxcells;
\r
1307 rendstatus &= ~(PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO);
\r
1309 if (pvid->reg[12]&1) {
\r
1310 maxw = 328; maxcells = 40;
\r
1312 maxw = 264; maxcells = 32;
\r
1315 // Find out if the window is on this line:
\r
1316 win=pvid->reg[0x12];
\r
1317 edge=(win&0x1f)<<3;
\r
1319 if (win&0x80) { if (DrawScanline>=edge) hvwind=1; }
\r
1320 else { if (DrawScanline< edge) hvwind=1; }
\r
1322 if (!hvwind) // we might have a vertical window here
\r
1324 win=pvid->reg[0x11];
\r
1327 if (!edge) hvwind=1;
\r
1328 else if(edge < (maxcells>>1)) hvwind=2;
\r
1331 else if(edge < (maxcells>>1)) hvwind=2;
\r
1336 /* - layer B low - */
\r
1337 if (PicoDrawMask & PDRAW_LAYERB_ON)
\r
1338 DrawLayer(1|(sh<<1), HighCacheB, 0, maxcells);
\r
1339 /* - layer A low - */
\r
1340 if (!(PicoDrawMask & PDRAW_LAYERA_ON));
\r
1341 else if (hvwind == 1)
\r
1342 DrawWindow(0, maxcells>>1, 0, sh);
\r
1343 else if (hvwind == 2) {
\r
1344 DrawLayer(0|(sh<<1), HighCacheA, (win&0x80) ? 0 : edge<<1, (win&0x80) ? edge<<1 : maxcells);
\r
1345 DrawWindow( (win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 0, sh);
\r
1347 DrawLayer(0|(sh<<1), HighCacheA, 0, maxcells);
\r
1348 /* - sprites low - */
\r
1349 if (!(PicoDrawMask & PDRAW_SPRITES_LOW_ON));
\r
1350 else if (rendstatus & PDRAW_INTERLACE)
\r
1351 DrawAllSpritesInterlace(0, sh);
\r
1352 else if (sprited[1] & SPRL_HAVE_LO)
\r
1353 DrawAllSprites(sprited, 0, sh);
\r
1355 /* - layer B hi - */
\r
1356 if ((PicoDrawMask & PDRAW_LAYERB_ON) && HighCacheB[0])
\r
1357 DrawTilesFromCache(HighCacheB, sh, maxw);
\r
1358 /* - layer A hi - */
\r
1359 if (!(PicoDrawMask & PDRAW_LAYERA_ON));
\r
1360 else if (hvwind == 1)
\r
1361 DrawWindow(0, maxcells>>1, 1, sh);
\r
1362 else if (hvwind == 2) {
\r
1363 if (HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, (win&0x80) ? edge<<4 : maxw);
\r
1364 DrawWindow((win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 1, sh);
\r
1366 if (HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, maxw);
\r
1367 /* - sprites hi - */
\r
1368 if (!(PicoDrawMask & PDRAW_SPRITES_HI_ON));
\r
1369 else if (rendstatus & PDRAW_INTERLACE)
\r
1370 DrawAllSpritesInterlace(1, sh);
\r
1371 // AS on and have both lo/hi sprites and lo before hi sprites?
\r
1372 else if ((sprited[1] & 0xd0) == 0xd0 && (rendstatus & PDRAW_ACC_SPRITES))
\r
1373 DrawSpritesHiAS(sprited, sh);
\r
1374 else if (sh && (sprited[1] & SPRL_MAY_HAVE_OP))
\r
1375 DrawSpritesSHi(sprited);
\r
1376 else if (sprited[1] & SPRL_HAVE_HI)
\r
1377 DrawAllSprites(sprited, 1, 0);
\r
1382 for (a = 0, c = HighCacheA; *c; c++, a++);
\r
1383 for (b = 0, c = HighCacheB; *c; c++, b++);
\r
1384 printf("%i:%03i: a=%i, b=%i\n", Pico.m.frame_count, DrawScanline, a, b);
\r
1392 PICO_INTERNAL void PicoFrameStart(void)
\r
1394 // prepare to do this frame
\r
1396 if (PicoOpt & POPT_ACC_SPRITES)
\r
1397 rendstatus |= PDRAW_ACC_SPRITES;
\r
1398 if ((Pico.video.reg[12]&6) == 6)
\r
1399 rendstatus |= PDRAW_INTERLACE; // interlace mode
\r
1401 if (Pico.m.dirtyPal) Pico.m.dirtyPal = 2; // reset dirty if needed
\r
1404 PrepareSprites(1);
\r
1408 static void PicoLine(void)
\r
1411 if (skip_next_line>0) { skip_next_line--; return; } // skip rendering lines
\r
1413 sh=(Pico.video.reg[0xC]&8)>>3; // shadow/hilight?
\r
1415 if (PicoScanBegin != NULL)
\r
1416 skip_next_line = PicoScanBegin(DrawScanline);
\r
1419 BackFill(Pico.video.reg[7], sh);
\r
1420 if (Pico.video.reg[1]&0x40)
\r
1423 if (FinalizeLine != NULL)
\r
1426 if (PicoScanEnd != NULL)
\r
1427 PicoScanEnd(DrawScanline);
\r
1430 void PicoDrawSync(int to, int blank_last_line)
\r
1432 for (; DrawScanline < to; DrawScanline++)
\r
1434 #if !CAN_HANDLE_240_LINES
\r
1435 if (DrawScanline >= 224) break;
\r
1440 #if !CAN_HANDLE_240_LINES
\r
1441 if (DrawScanline >= 224) DrawScanline = 240, return;
\r
1445 if (DrawScanline <= to)
\r
1447 if (blank_last_line)
\r
1448 DrawBlankedLine();
\r
1454 void PicoDrawSetColorFormat(int which)
\r
1458 case 2: FinalizeLine = FinalizeLine8bit; break;
\r
1459 case 1: FinalizeLine = FinalizeLineRGB555; break;
\r
1460 case 0: FinalizeLine = FinalizeLineBGR444; break;
\r
1461 default:FinalizeLine = NULL; break;
\r
1463 #if OVERRIDE_HIGHCOL
\r
1464 if (which) HighCol=DefHighCol;
\r
1468 /* debug and fun */
\r
1469 static void set16(unsigned short *p, unsigned short d, int cnt)
\r
1475 void PicoDrawShowSpriteStats(unsigned short *screen)
\r
1477 int lines, i, u, step;
\r
1478 unsigned short *dest;
\r
1481 memset(screen, 0, 320*240*2);
\r
1482 step = (320-4*4-1) / MAX_LINE_SPRITES;
\r
1484 if (!Pico.m.pal || !(Pico.video.reg[1]&8))
\r
1485 lines = 224, screen += 320*8;
\r
1487 for (i = 0; i < lines; i++)
\r
1489 dest = screen + 320*i;
\r
1490 p = &HighLnSpr[i][0];
\r
1493 for (u = 0; u < (p[0] & 0x7f); u++) {
\r
1494 set16(dest, (p[3+u] & 0x80) ? 0xe700 : 0x0700, step);
\r
1499 dest = screen + 320*i + 320-4*4;
\r
1500 if (p[1] & SPRL_HAVE_LO) set16(dest+4*0, 0x0700, 4);
\r
1501 if (p[1] & SPRL_HAVE_HI) set16(dest+4*1, 0xe700, 4);
\r
1502 if (p[1] & SPRL_MAY_HAVE_OP) set16(dest+4*2, 0x001e, 4);
\r
1503 if (p[1] & SPRL_LO_ABOVE_HI) set16(dest+4*3, 0xf000, 4);
\r
1507 for (i = step*5; i <= 320-4*4-1; i += step*5) {
\r
1508 for (u = 0; u < lines; u++)
\r
1509 screen[i + u*320] = 0x182;
\r
1513 void PicoDrawShowPalette(unsigned short *screen)
\r
1515 unsigned int *spal=(void *)Pico.cram;
\r
1516 unsigned int *dpal=(void *)HighPal;
\r
1519 memset(screen, 0, 320*240*2);
\r
1521 for (i = 0x3f/2; i >= 0; i--)
\r
1523 dpal[i] = ((spal[i]&0x000f000f)<< 1)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)<<4);
\r
1525 dpal[i] = ((spal[i]&0x000f000f)<<12)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)>>7);
\r
1527 for (i = 0x3f; i >= 0; i--)
\r
1528 HighPal[0x40|i] = (unsigned short)((HighPal[i]>>1)&0x738e);
\r
1529 for (i = 0x3f; i >= 0; i--) {
\r
1530 int t=HighPal[i]&0xe71c;t+=0x4208;if(t&0x20)t|=0x1c;if(t&0x800)t|=0x700;if(t&0x10000)t|=0xe000;t&=0xe71c;
\r
1531 HighPal[0x80|i] = (unsigned short)t;
\r
1534 screen += 16*320+8;
\r
1535 for (y = 0; y < 8*4; y++)
\r
1536 for (x = 0; x < 8*16; x++)
\r
1537 screen[x + y*320] = HighPal[x/8 + (y/8)*16];
\r
1540 for (y = 0; y < 8*4; y++)
\r
1541 for (x = 0; x < 8*16; x++)
\r
1542 screen[x + y*320] = HighPal[(x/8 + (y/8)*16) | 0x40];
\r
1545 for (y = 0; y < 8*4; y++)
\r
1546 for (x = 0; x < 8*16; x++)
\r
1547 screen[x + y*320] = HighPal[(x/8 + (y/8)*16) | 0x80];
\r
1549 Pico.m.dirtyPal = 1;
\r