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 static int HighCacheS[80+1]; // and sprites
\r
38 static int HighPreSpr[80*2+1]; // slightly preprocessed sprites
\r
41 int Scanline = 0; // Scanline
\r
43 static int SpriteBlocks;
\r
44 static int skip_next_line=0;
\r
46 //unsigned short ppt[] = { 0x0f11, 0x0ff1, 0x01f1, 0x011f, 0x01ff, 0x0f1f, 0x0f0e, 0x0e7c };
\r
48 static void (*DrawAllSpritesLoPri)(int *hcache, int maxwidth, int prio, int sh) = NULL;
\r
49 static void (*DrawAllSpritesHiPri)(int *hcache, int maxwidth, int prio, int sh) = NULL;
\r
53 int nametab; // Position in VRAM of name table (for this tile line)
\r
54 int line; // Line number in pixels 0x000-0x3ff within the virtual tilemap
\r
55 int hscroll; // Horizontal scroll value in pixels for the line
\r
56 int xmask; // X-Mask (0x1f - 0x7f) for horizontal wraparound in the tilemap
\r
57 int *hc; // cache for high tile codes and their positions
\r
58 int cells; // cells (tiles) to draw (32 col mode doesn't need to update whole 320)
\r
61 // stuff available in asm:
\r
63 void DrawWindow(int tstart, int tend, int prio, int sh);
\r
64 void BackFill(int reg7, int sh);
\r
65 void DrawSprite(int *sprite, int **hc, int sh, int as);
\r
66 void DrawTilesFromCache(int *hc, int sh, int rlim);
\r
67 void DrawSpritesFromCache(int *hc, int maxwidth, int prio, int sh);
\r
68 void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells);
\r
69 void FinalizeLineBGR444(int sh);
\r
70 void FinalizeLineRGB555(int sh);
\r
71 void *blockcpy(void *dst, const void *src, size_t n);
\r
72 void blockcpy_or(void *dst, void *src, size_t n, int pat);
\r
75 void blockcpy_or(void *dst, void *src, size_t n, int pat)
\r
77 unsigned char *pd = dst, *ps = src;
\r
79 *pd++ = (unsigned char) (*ps++ | pat);
\r
81 #define blockcpy memcpy
\r
85 #ifdef _ASM_DRAW_C_AMIPS
\r
86 int TileNorm(int sx,int addr,int pal);
\r
87 int TileFlip(int sx,int addr,int pal);
\r
89 static int TileNorm(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; if (t) pd[0]=(unsigned char)(pal|(t>>12));
\r
98 t=pack&0x00000f00; if (t) pd[1]=(unsigned char)(pal|(t>> 8));
\r
99 t=pack&0x000000f0; if (t) pd[2]=(unsigned char)(pal|(t>> 4));
\r
100 t=pack&0x0000000f; if (t) pd[3]=(unsigned char)(pal|(t ));
\r
101 t=pack&0xf0000000; if (t) pd[4]=(unsigned char)(pal|(t>>28));
\r
102 t=pack&0x0f000000; if (t) pd[5]=(unsigned char)(pal|(t>>24));
\r
103 t=pack&0x00f00000; if (t) pd[6]=(unsigned char)(pal|(t>>20));
\r
104 t=pack&0x000f0000; if (t) pd[7]=(unsigned char)(pal|(t>>16));
\r
108 return 1; // Tile blank
\r
111 static int TileFlip(int sx,int addr,int pal)
\r
113 unsigned char *pd = HighCol+sx;
\r
114 unsigned int pack=0; unsigned int t=0;
\r
116 pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
119 t=pack&0x000f0000; if (t) pd[0]=(unsigned char)(pal|(t>>16));
\r
120 t=pack&0x00f00000; if (t) pd[1]=(unsigned char)(pal|(t>>20));
\r
121 t=pack&0x0f000000; if (t) pd[2]=(unsigned char)(pal|(t>>24));
\r
122 t=pack&0xf0000000; if (t) pd[3]=(unsigned char)(pal|(t>>28));
\r
123 t=pack&0x0000000f; if (t) pd[4]=(unsigned char)(pal|(t ));
\r
124 t=pack&0x000000f0; if (t) pd[5]=(unsigned char)(pal|(t>> 4));
\r
125 t=pack&0x00000f00; if (t) pd[6]=(unsigned char)(pal|(t>> 8));
\r
126 t=pack&0x0000f000; if (t) pd[7]=(unsigned char)(pal|(t>>12));
\r
129 return 1; // Tile blank
\r
133 // tile renderers for hacky operator sprite support
\r
134 #define sh_pix(x) \
\r
136 else if(t==0xe) pd[x]=(unsigned char)((pd[x]&0x3f)|0x80); /* hilight */ \
\r
137 else if(t==0xf) pd[x]=(unsigned char)( pd[x] |0xc0); /* shadow */ \
\r
138 else pd[x]=(unsigned char)(pal|t)
\r
140 #ifndef _ASM_DRAW_C
\r
141 static int TileNormSH(int sx,int addr,int pal)
\r
143 unsigned int pack=0; unsigned int t=0;
\r
144 unsigned char *pd = HighCol+sx;
\r
146 pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
149 t=(pack&0x0000f000)>>12; sh_pix(0);
\r
150 t=(pack&0x00000f00)>> 8; sh_pix(1);
\r
151 t=(pack&0x000000f0)>> 4; sh_pix(2);
\r
152 t=(pack&0x0000000f) ; sh_pix(3);
\r
153 t=(pack&0xf0000000)>>28; sh_pix(4);
\r
154 t=(pack&0x0f000000)>>24; sh_pix(5);
\r
155 t=(pack&0x00f00000)>>20; sh_pix(6);
\r
156 t=(pack&0x000f0000)>>16; sh_pix(7);
\r
160 return 1; // Tile blank
\r
163 static int TileFlipSH(int sx,int addr,int pal)
\r
165 unsigned int pack=0; unsigned int t=0;
\r
166 unsigned char *pd = HighCol+sx;
\r
168 pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
171 t=(pack&0x000f0000)>>16; sh_pix(0);
\r
172 t=(pack&0x00f00000)>>20; sh_pix(1);
\r
173 t=(pack&0x0f000000)>>24; sh_pix(2);
\r
174 t=(pack&0xf0000000)>>28; sh_pix(3);
\r
175 t=(pack&0x0000000f) ; sh_pix(4);
\r
176 t=(pack&0x000000f0)>> 4; sh_pix(5);
\r
177 t=(pack&0x00000f00)>> 8; sh_pix(6);
\r
178 t=(pack&0x0000f000)>>12; sh_pix(7);
\r
181 return 1; // Tile blank
\r
185 #define tilepixelAS(mask,index,shift) \
\r
186 if (!(pd[index]&0xc0)) { t=pack&mask; if (t) pd[index]=(pal|(t>>shift)); }
\r
188 static int TileNormAS(int sx,int addr,int pal)
\r
190 unsigned char *pd = HighCol+sx;
\r
191 unsigned int pack=0; unsigned int t=0;
\r
193 pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
196 tilepixelAS(0x0000f000, 0, 12);
\r
197 tilepixelAS(0x00000f00, 1, 8);
\r
198 tilepixelAS(0x000000f0, 2, 4);
\r
199 tilepixelAS(0x0000000f, 3, 0);
\r
200 tilepixelAS(0xf0000000, 4, 28);
\r
201 tilepixelAS(0x0f000000, 5, 24);
\r
202 tilepixelAS(0x00f00000, 6, 20);
\r
203 tilepixelAS(0x000f0000, 7, 16);
\r
207 return 1; // Tile blank
\r
210 static int TileFlipAS(int sx,int addr,int pal)
\r
212 unsigned char *pd = HighCol+sx;
\r
213 unsigned int pack=0; unsigned int t=0;
\r
215 pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
218 tilepixelAS(0x000f0000, 0, 16);
\r
219 tilepixelAS(0x00f00000, 1, 20);
\r
220 tilepixelAS(0x0f000000, 2, 24);
\r
221 tilepixelAS(0xf0000000, 3, 28);
\r
222 tilepixelAS(0x0000000f, 4, 0);
\r
223 tilepixelAS(0x000000f0, 5, 4);
\r
224 tilepixelAS(0x00000f00, 6, 8);
\r
225 tilepixelAS(0x0000f000, 7, 12);
\r
228 return 1; // Tile blank
\r
231 // there is a problem with transparent hi pri tiles (on layer), it will clear high bits
\r
232 // and sprite tiles will be drawn needlessly. Hopefully that won't happen much..
\r
233 #define sh_pixAS(x) \
\r
235 else if(t==0xe) pd[x]=(unsigned char)((pd[x]&0x3f)|0x80); /* hilight */ \
\r
236 else if(t==0xf) pd[x]=(unsigned char)( pd[x] |0xc0); /* shadow */ \
\r
237 else if(!(pd[x]&0xc0)) pd[x]=(unsigned char)(pal|t)
\r
239 static int TileNormSHAS(int sx,int addr,int pal)
\r
241 unsigned int pack=0; unsigned int t=0;
\r
242 unsigned char *pd = HighCol+sx;
\r
244 pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
247 t=(pack&0x0000f000)>>12; sh_pixAS(0);
\r
248 t=(pack&0x00000f00)>> 8; sh_pixAS(1);
\r
249 t=(pack&0x000000f0)>> 4; sh_pixAS(2);
\r
250 t=(pack&0x0000000f) ; sh_pixAS(3);
\r
251 t=(pack&0xf0000000)>>28; sh_pixAS(4);
\r
252 t=(pack&0x0f000000)>>24; sh_pixAS(5);
\r
253 t=(pack&0x00f00000)>>20; sh_pixAS(6);
\r
254 t=(pack&0x000f0000)>>16; sh_pixAS(7);
\r
258 return 1; // Tile blank
\r
261 static int TileFlipSHAS(int sx,int addr,int pal)
\r
263 unsigned int pack=0; unsigned int t=0;
\r
264 unsigned char *pd = HighCol+sx;
\r
266 pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
269 t=(pack&0x000f0000)>>16; sh_pixAS(0);
\r
270 t=(pack&0x00f00000)>>20; sh_pixAS(1);
\r
271 t=(pack&0x0f000000)>>24; sh_pixAS(2);
\r
272 t=(pack&0xf0000000)>>28; sh_pixAS(3);
\r
273 t=(pack&0x0000000f) ; sh_pixAS(4);
\r
274 t=(pack&0x000000f0)>> 4; sh_pixAS(5);
\r
275 t=(pack&0x00000f00)>> 8; sh_pixAS(6);
\r
276 t=(pack&0x0000f000)>>12; sh_pixAS(7);
\r
279 return 1; // Tile blank
\r
283 // --------------------------------------------
\r
285 #ifndef _ASM_DRAW_C
\r
286 static void DrawStrip(struct TileStrip *ts, int plane_sh, int cellskip)
\r
288 int tilex,dx,ty,code=0,addr=0,cells;
\r
289 int oldcode=-1,blank=-1; // The tile we know is blank
\r
292 // Draw tiles across screen:
\r
293 sh=(plane_sh<<5)&0x40;
\r
294 tilex=((-ts->hscroll)>>3)+cellskip;
\r
295 ty=(ts->line&7)<<1; // Y-Offset into tile
\r
296 dx=((ts->hscroll-1)&7)+1;
\r
297 cells = ts->cells - cellskip;
\r
298 if(dx != 8) cells++; // have hscroll, need to draw 1 cell more
\r
301 for (; cells > 0; dx+=8,tilex++,cells--)
\r
305 code=Pico.vram[ts->nametab+(tilex&ts->xmask)];
\r
306 if (code==blank) continue;
\r
307 if (code>>15) { // high priority tile
\r
308 int cval = code | (dx<<16) | (ty<<25);
\r
309 if(code&0x1000) cval^=7<<26;
\r
310 *ts->hc++ = cval; // cache it
\r
314 if (code!=oldcode) {
\r
316 // Get tile address/2:
\r
317 addr=(code&0x7ff)<<4;
\r
319 if (code&0x1000) addr^=0xe; // Y-flip
\r
321 pal=((code>>9)&0x30)|sh;
\r
324 if (code&0x0800) zero=TileFlip(dx,addr,pal);
\r
325 else zero=TileNorm(dx,addr,pal);
\r
327 if (zero) blank=code; // We know this tile is blank now
\r
330 // terminate the cache list
\r
332 // if oldcode wasn't changed, it means all layer is hi priority
\r
333 if (oldcode == -1) rendstatus |= PDRAW_PLANE_HI_PRIO;
\r
337 void DrawStripVSRam(struct TileStrip *ts, int plane_sh, int cellskip)
\r
339 int tilex,dx,code=0,addr=0,cell=0;
\r
340 int oldcode=-1,blank=-1; // The tile we know is blank
\r
341 int pal=0,scan=Scanline;
\r
343 // Draw tiles across screen:
\r
344 tilex=(-ts->hscroll)>>3;
\r
345 dx=((ts->hscroll-1)&7)+1;
\r
346 if(dx != 8) cell--; // have hscroll, start with negative cell
\r
351 for (; cell < ts->cells; dx+=8,tilex++,cell++)
\r
353 int zero=0,nametabadd,ty;
\r
358 vscroll=Pico.vsram[(plane_sh&1)+(cell&~1)];
\r
360 // Find the line in the name table
\r
361 line=(vscroll+scan)&ts->line&0xffff; // ts->line is really ymask ..
\r
362 nametabadd=(line>>3)<<(ts->line>>24); // .. and shift[width]
\r
363 ty=(line&7)<<1; // Y-Offset into tile
\r
366 code=Pico.vram[ts->nametab+nametabadd+(tilex&ts->xmask)];
\r
367 if (code==blank) continue;
\r
368 if (code>>15) { // high priority tile
\r
369 int cval = code | (dx<<16) | (ty<<25);
\r
370 if(code&0x1000) cval^=7<<26;
\r
371 *ts->hc++ = cval; // cache it
\r
375 if (code!=oldcode) {
\r
377 // Get tile address/2:
\r
378 addr=(code&0x7ff)<<4;
\r
379 if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
\r
381 pal=((code>>9)&0x30)|((plane_sh<<5)&0x40);
\r
384 if (code&0x0800) zero=TileFlip(dx,addr,pal);
\r
385 else zero=TileNorm(dx,addr,pal);
\r
387 if (zero) blank=code; // We know this tile is blank now
\r
390 // terminate the cache list
\r
392 if (oldcode == -1) rendstatus |= PDRAW_PLANE_HI_PRIO;
\r
396 #ifndef _ASM_DRAW_C
\r
399 void DrawStripInterlace(struct TileStrip *ts)
\r
401 int tilex=0,dx=0,ty=0,code=0,addr=0,cells;
\r
402 int oldcode=-1,blank=-1; // The tile we know is blank
\r
405 // Draw tiles across screen:
\r
406 tilex=(-ts->hscroll)>>3;
\r
407 ty=(ts->line&15)<<1; // Y-Offset into tile
\r
408 dx=((ts->hscroll-1)&7)+1;
\r
410 if(dx != 8) cells++; // have hscroll, need to draw 1 cell more
\r
412 for (; cells; dx+=8,tilex++,cells--)
\r
416 code=Pico.vram[ts->nametab+(tilex&ts->xmask)];
\r
417 if (code==blank) continue;
\r
418 if (code>>15) { // high priority tile
\r
419 int cval = (code&0xfc00) | (dx<<16) | (ty<<25);
\r
420 cval|=(code&0x3ff)<<1;
\r
421 if(code&0x1000) cval^=0xf<<26;
\r
422 *ts->hc++ = cval; // cache it
\r
426 if (code!=oldcode) {
\r
428 // Get tile address/2:
\r
429 addr=(code&0x7ff)<<5;
\r
430 if (code&0x1000) addr+=30-ty; else addr+=ty; // Y-flip
\r
432 // pal=Pico.cram+((code>>9)&0x30);
\r
433 pal=((code>>9)&0x30);
\r
436 if (code&0x0800) zero=TileFlip(dx,addr,pal);
\r
437 else zero=TileNorm(dx,addr,pal);
\r
439 if (zero) blank=code; // We know this tile is blank now
\r
442 // terminate the cache list
\r
446 // --------------------------------------------
\r
448 #ifndef _ASM_DRAW_C
\r
449 static void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells)
\r
451 struct PicoVideo *pvid=&Pico.video;
\r
452 const char shift[4]={5,6,5,7}; // 32,64 or 128 sized tilemaps (2 is invalid)
\r
453 struct TileStrip ts;
\r
454 int width, height, ymask;
\r
460 // Work out the TileStrip to draw
\r
462 // Work out the name table size: 32 64 or 128 tiles (0-3)
\r
463 width=pvid->reg[16];
\r
464 height=(width>>4)&3; width&=3;
\r
466 ts.xmask=(1<<shift[width])-1; // X Mask in tiles (0x1f-0x7f)
\r
467 ymask=(height<<8)|0xff; // Y Mask in pixels
\r
468 if(width == 1) ymask&=0x1ff;
\r
469 else if(width>1) ymask =0x0ff;
\r
471 // Find name table:
\r
472 if (plane_sh&1) ts.nametab=(pvid->reg[4]&0x07)<<12; // B
\r
473 else ts.nametab=(pvid->reg[2]&0x38)<< 9; // A
\r
475 htab=pvid->reg[13]<<9; // Horizontal scroll table address
\r
476 if ( pvid->reg[11]&2) htab+=Scanline<<1; // Offset by line
\r
477 if ((pvid->reg[11]&1)==0) htab&=~0xf; // Offset by tile
\r
478 htab+=plane_sh&1; // A or B
\r
480 // Get horizontal scroll value, will be masked later
\r
481 ts.hscroll=Pico.vram[htab&0x7fff];
\r
483 if((pvid->reg[12]&6) == 6) {
\r
484 // interlace mode 2
\r
485 vscroll=Pico.vsram[plane_sh&1]; // Get vertical scroll value
\r
487 // Find the line in the name table
\r
488 ts.line=(vscroll+(Scanline<<1))&((ymask<<1)|1);
\r
489 ts.nametab+=(ts.line>>4)<<shift[width];
\r
491 DrawStripInterlace(&ts);
\r
492 } else if( pvid->reg[11]&4) {
\r
493 // shit, we have 2-cell column based vscroll
\r
494 // luckily this doesn't happen too often
\r
495 ts.line=ymask|(shift[width]<<24); // save some stuff instead of line
\r
496 DrawStripVSRam(&ts, plane_sh, cellskip);
\r
498 vscroll=Pico.vsram[plane_sh&1]; // Get vertical scroll value
\r
500 // Find the line in the name table
\r
501 ts.line=(vscroll+Scanline)&ymask;
\r
502 ts.nametab+=(ts.line>>3)<<shift[width];
\r
504 DrawStrip(&ts, plane_sh, cellskip);
\r
509 // --------------------------------------------
\r
511 // tstart & tend are tile pair numbers
\r
512 static void DrawWindow(int tstart, int tend, int prio, int sh) // int *hcache
\r
514 struct PicoVideo *pvid=&Pico.video;
\r
515 int tilex=0,ty=0,nametab,code=0;
\r
516 int blank=-1; // The tile we know is blank
\r
518 // Find name table line:
\r
519 if (pvid->reg[12]&1)
\r
521 nametab=(pvid->reg[3]&0x3c)<<9; // 40-cell mode
\r
522 nametab+=(Scanline>>3)<<6;
\r
526 nametab=(pvid->reg[3]&0x3e)<<9; // 32-cell mode
\r
527 nametab+=(Scanline>>3)<<5;
\r
533 ty=(Scanline&7)<<1; // Y-Offset into tile
\r
535 if (!(rendstatus & PDRAW_WND_DIFF_PRIO)) {
\r
536 // check the first tile code
\r
537 code=Pico.vram[nametab+tilex];
\r
538 // if the whole window uses same priority (what is often the case), we may be able to skip this field
\r
539 if ((code>>15) != prio) return;
\r
542 // Draw tiles across screen:
\r
545 for (; tilex < tend; tilex++)
\r
550 code=Pico.vram[nametab+tilex];
\r
551 if (code==blank) continue;
\r
552 if ((code>>15) != prio) {
\r
553 rendstatus |= PDRAW_WND_DIFF_PRIO;
\r
557 pal=((code>>9)&0x30);
\r
559 // Get tile address/2:
\r
560 addr=(code&0x7ff)<<4;
\r
561 if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
\r
563 if (code&0x0800) zero=TileFlip(8+(tilex<<3),addr,pal);
\r
564 else zero=TileNorm(8+(tilex<<3),addr,pal);
\r
566 if (zero) blank=code; // We know this tile is blank now
\r
571 for (; tilex < tend; tilex++)
\r
576 code=Pico.vram[nametab+tilex];
\r
577 if(code==blank) continue;
\r
578 if((code>>15) != prio) {
\r
579 rendstatus |= PDRAW_WND_DIFF_PRIO;
\r
583 pal=((code>>9)&0x30);
\r
585 zb = (int *)(HighCol+8+(tilex<<3));
\r
588 if(!(tmp&0x00000080)) tmp&=~0x000000c0; if(!(tmp&0x00008000)) tmp&=~0x0000c000;
\r
589 if(!(tmp&0x00800000)) tmp&=~0x00c00000; if(!(tmp&0x80000000)) tmp&=~0xc0000000;
\r
590 *zb++=tmp; tmp = *zb;
\r
591 if(!(tmp&0x00000080)) tmp&=~0x000000c0; if(!(tmp&0x00008000)) tmp&=~0x0000c000;
\r
592 if(!(tmp&0x00800000)) tmp&=~0x00c00000; if(!(tmp&0x80000000)) tmp&=~0xc0000000;
\r
598 // Get tile address/2:
\r
599 addr=(code&0x7ff)<<4;
\r
600 if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
\r
602 if (code&0x0800) zero=TileFlip(8+(tilex<<3),addr,pal);
\r
603 else zero=TileNorm(8+(tilex<<3),addr,pal);
\r
605 if (zero) blank=code; // We know this tile is blank now
\r
610 // --------------------------------------------
\r
612 static void DrawTilesFromCacheShPrep(void)
\r
614 // as some layer has covered whole line with hi priority tiles,
\r
615 // we can process whole line and then act as if sh/hi mode was off.
\r
616 int c = 320/4, *zb = (int *)(HighCol+8);
\r
617 rendstatus |= PDRAW_SHHI_DONE;
\r
621 if (!(tmp & 0x80808080)) *zb=tmp&0x3f3f3f3f;
\r
623 if(!(tmp&0x00000080)) tmp&=~0x000000c0; if(!(tmp&0x00008000)) tmp&=~0x0000c000;
\r
624 if(!(tmp&0x00800000)) tmp&=~0x00c00000; if(!(tmp&0x80000000)) tmp&=~0xc0000000;
\r
631 static void DrawTilesFromCache(int *hc, int sh, int rlim)
\r
633 int code, addr, dx;
\r
636 // *ts->hc++ = code | (dx<<16) | (ty<<25); // cache it
\r
638 if (sh && (rendstatus & (PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO)))
\r
640 if (!(rendstatus & PDRAW_SHHI_DONE))
\r
641 DrawTilesFromCacheShPrep();
\r
647 short blank=-1; // The tile we know is blank
\r
648 while ((code=*hc++)) {
\r
650 if((short)code == blank) continue;
\r
651 // Get tile address/2:
\r
652 addr=(code&0x7ff)<<4;
\r
653 addr+=(unsigned int)code>>25; // y offset into tile
\r
654 dx=(code>>16)&0x1ff;
\r
656 pal=((code>>9)&0x30);
\r
657 if (rlim-dx < 0) goto last_cut_tile;
\r
659 if (code&0x0800) zero=TileFlip(dx,addr,pal);
\r
660 else zero=TileNorm(dx,addr,pal);
\r
662 if (zero) blank=(short)code;
\r
667 while ((code=*hc++)) {
\r
669 // Get tile address/2:
\r
670 addr=(code&0x7ff)<<4;
\r
671 addr+=(unsigned int)code>>25; // y offset into tile
\r
672 dx=(code>>16)&0x1ff;
\r
674 if(!(*zb&0x80)) *zb&=0x3f; zb++; if(!(*zb&0x80)) *zb&=0x3f; zb++;
\r
675 if(!(*zb&0x80)) *zb&=0x3f; zb++; if(!(*zb&0x80)) *zb&=0x3f; zb++;
\r
676 if(!(*zb&0x80)) *zb&=0x3f; zb++; if(!(*zb&0x80)) *zb&=0x3f; zb++;
\r
677 if(!(*zb&0x80)) *zb&=0x3f; zb++; if(!(*zb&0x80)) *zb&=0x3f; zb++;
\r
679 pal=((code>>9)&0x30);
\r
680 if (rlim-dx < 0) goto last_cut_tile;
\r
682 if (code&0x0800) TileFlip(dx,addr,pal);
\r
683 else TileNorm(dx,addr,pal);
\r
690 unsigned int t, pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
691 unsigned char *pd = HighCol+dx;
\r
697 case 7: t=pack&0x00000f00; if (t) pd[6]=(unsigned char)(pal|(t>> 8)); // "break" is left out intentionally
\r
698 case 6: t=pack&0x000000f0; if (t) pd[5]=(unsigned char)(pal|(t>> 4));
\r
699 case 5: t=pack&0x0000000f; if (t) pd[4]=(unsigned char)(pal|(t ));
\r
700 case 4: t=pack&0xf0000000; if (t) pd[3]=(unsigned char)(pal|(t>>28));
\r
701 case 3: t=pack&0x0f000000; if (t) pd[2]=(unsigned char)(pal|(t>>24));
\r
702 case 2: t=pack&0x00f00000; if (t) pd[1]=(unsigned char)(pal|(t>>20));
\r
703 case 1: t=pack&0x000f0000; if (t) pd[0]=(unsigned char)(pal|(t>>16));
\r
711 case 7: t=pack&0x00f00000; if (t) pd[6]=(unsigned char)(pal|(t>>20));
\r
712 case 6: t=pack&0x0f000000; if (t) pd[5]=(unsigned char)(pal|(t>>24));
\r
713 case 5: t=pack&0xf0000000; if (t) pd[4]=(unsigned char)(pal|(t>>28));
\r
714 case 4: t=pack&0x0000000f; if (t) pd[3]=(unsigned char)(pal|(t ));
\r
715 case 3: t=pack&0x000000f0; if (t) pd[2]=(unsigned char)(pal|(t>> 4));
\r
716 case 2: t=pack&0x00000f00; if (t) pd[1]=(unsigned char)(pal|(t>> 8));
\r
717 case 1: t=pack&0x0000f000; if (t) pd[0]=(unsigned char)(pal|(t>>12));
\r
724 // --------------------------------------------
\r
726 // Index + 0 : hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size
\r
727 // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
\r
729 static void DrawSprite(int *sprite, int **hc, int sh, int as)
\r
731 int width=0,height=0;
\r
734 int tile=0,delta=0;
\r
736 int (*fTileFunc)(int sx,int addr,int pal);
\r
738 // parse the sprite data
\r
743 height=(sy>>24)&7; // Width and height in tiles
\r
744 sy=(sy<<16)>>16; // Y
\r
746 row=Scanline-sy; // Row of the sprite we are on
\r
748 if (code&0x1000) row=(height<<3)-1-row; // Flip Y
\r
750 tile=code&0x7ff; // Tile number
\r
751 tile+=row>>3; // Tile number increases going down
\r
752 delta=height; // Delta to increase tile by going right
\r
753 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
\r
755 tile<<=4; tile+=(row&7)<<1; // Tile address
\r
757 if (code&0x8000) { // high priority - cache it
\r
758 *(*hc)++ = (tile<<16)|((code&0x0800)<<5)|((sx<<6)&0x0000ffc0)|((code>>9)&0x30)|((sprite[0]>>16)&0xf);
\r
759 // we need all for accurate sprites, cached will be used to recover ones overwritten by high layer
\r
763 delta<<=4; // Delta of address
\r
764 pal=((code>>9)&0x30)|((sh|as)<<6);
\r
766 if (sh && (code&0x6000) == 0x6000) {
\r
767 if(code&0x0800) fTileFunc=TileFlipSH;
\r
768 else fTileFunc=TileNormSH;
\r
770 if(code&0x0800) fTileFunc=TileFlip;
\r
771 else fTileFunc=TileNorm;
\r
774 for (; width; width--,sx+=8,tile+=delta)
\r
776 if(sx<=0) continue;
\r
777 if(sx>=328) break; // Offscreen
\r
779 tile&=0x7fff; // Clip tile address
\r
780 fTileFunc(sx,tile,pal);
\r
785 static void DrawSpriteInterlace(unsigned int *sprite)
\r
787 int width=0,height=0;
\r
790 int tile=0,delta=0;
\r
793 // parse the sprite data
\r
796 sy=(sy&0x3ff)-0x100; // Y
\r
797 width=(height>>2)&3; height&=3;
\r
798 width++; height++; // Width and height in tiles
\r
800 row=(Scanline<<1)-sy; // Row of the sprite we are on
\r
803 sx=((code>>16)&0x1ff)-0x78; // X
\r
805 if (code&0x1000) row^=(16<<height)-1; // Flip Y
\r
807 tile=code&0x3ff; // Tile number
\r
808 tile+=row>>4; // Tile number increases going down
\r
809 delta=height; // Delta to increase tile by going right
\r
810 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
\r
812 tile<<=5; tile+=(row&15)<<1; // Tile address
\r
814 delta<<=5; // Delta of address
\r
815 pal=((code>>9)&0x30); // Get palette pointer
\r
817 for (; width; width--,sx+=8,tile+=delta)
\r
819 if(sx<=0) continue;
\r
820 if(sx>=328) break; // Offscreen
\r
822 tile&=0x7fff; // Clip tile address
\r
823 if (code&0x0800) TileFlip(sx,tile,pal);
\r
824 else TileNorm(sx,tile,pal);
\r
829 static void DrawAllSpritesInterlace(int *hcache, int maxwidth, int pri, int sh)
\r
831 struct PicoVideo *pvid=&Pico.video;
\r
832 int i,u,table,link=0,sline=Scanline<<1;
\r
833 unsigned int *sprites[80]; // Sprite index
\r
835 table=pvid->reg[5]&0x7f;
\r
836 if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode
\r
837 table<<=8; // Get sprite table address/2
\r
839 for (i=u=0; u < 80 && i < 21; u++)
\r
841 unsigned int *sprite;
\r
842 int code, sx, sy, height;
\r
844 sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite
\r
849 if(((sx>>15)&1) != pri) goto nextsprite; // wrong priority sprite
\r
851 // check if it is on this line
\r
852 sy = (code&0x3ff)-0x100;
\r
853 height = (((code>>24)&3)+1)<<4;
\r
854 if(sline < sy || sline >= sy+height) goto nextsprite; // no
\r
856 // check if sprite is not hidden offscreen
\r
857 sx = (sx>>16)&0x1ff;
\r
858 sx -= 0x78; // Get X coordinate + 8
\r
859 if(sx <= -8*3 || sx >= maxwidth) goto nextsprite;
\r
861 // sprite is good, save it's pointer
\r
862 sprites[i++]=sprite;
\r
865 // Find next sprite
\r
866 link=(code>>16)&0x7f;
\r
867 if(!link) break; // End of sprites
\r
870 // Go through sprites backwards:
\r
871 for (i-- ;i>=0; i--)
\r
872 DrawSpriteInterlace(sprites[i]);
\r
876 #ifndef _ASM_DRAW_C
\r
877 static void DrawSpritesFromCache(int *hc, int maxwidth, int prio, int sh)
\r
879 int code, tile, sx, delta, width;
\r
881 int (*fTileFunc)(int sx,int addr,int pal);
\r
883 // *(*hc)++ = (tile<<16)|((code&0x0800)<<5)|((sx<<6)&0x0000ffc0)|((code>>9)&0x30)|((sprite[0]>>24)&0xf);
\r
885 while((code=*hc++)) {
\r
888 width=delta>>2; delta&=3;
\r
889 width++; delta++; // Width and height in tiles
\r
890 if (code&0x10000) delta=-delta; // Flip X
\r
892 tile=((unsigned int)code>>17)<<1;
\r
893 sx=(code<<16)>>22; // sx can be negative (start offscreen), so sign extend
\r
895 if(sh && pal == 0x30) { //
\r
896 if(code&0x10000) fTileFunc=TileFlipSH;
\r
897 else fTileFunc=TileNormSH;
\r
899 if(code&0x10000) fTileFunc=TileFlip;
\r
900 else fTileFunc=TileNorm;
\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
915 static void DrawSpritesFromCacheAS(int *hc, int maxwidth, int prio, int sh)
\r
917 int code, tile, sx, delta, width;
\r
919 int (*fTileFunc)(int sx,int addr,int pal);
\r
921 // *(*hc)++ = (tile<<16)|((code&0x0800)<<5)|((sx<<6)&0x0000ffc0)|((code>>9)&0x30)|((sprite[0]>>24)&0xf);
\r
923 while((code=*hc++)) {
\r
926 width=delta>>2; delta&=3;
\r
927 width++; delta++; // Width and height in tiles
\r
928 if (code&0x10000) delta=-delta; // Flip X
\r
930 tile=((unsigned int)code>>17)<<1;
\r
931 sx=(code<<16)>>22; // sx can be negative (start offscreen), so sign extend
\r
933 if(sh && pal == 0x30) { //
\r
934 if(code&0x10000) fTileFunc=TileFlipSHAS;
\r
935 else fTileFunc=TileNormSHAS;
\r
937 if(code&0x10000) fTileFunc=TileFlipAS;
\r
938 else fTileFunc=TileNormAS;
\r
941 for (; width; width--,sx+=8,tile+=delta)
\r
943 if(sx<=0) continue;
\r
944 if(sx>=328) break; // Offscreen
\r
946 tile&=0x7fff; // Clip tile address
\r
947 fTileFunc(sx,tile,pal);
\r
953 // Index + 0 : ----hhvv -lllllll -------y yyyyyyyy
\r
954 // Index + 4 : -------x xxxxxxxx pccvhnnn nnnnnnnn
\r
956 // Index + 0 : hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size
\r
957 // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
\r
959 static void PrepareSprites(int full)
\r
961 struct PicoVideo *pvid=&Pico.video;
\r
962 int u=0,link=0,sblocks=0;
\r
964 int *pd = HighPreSpr;
\r
966 table=pvid->reg[5]&0x7f;
\r
967 if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode
\r
968 table<<=8; // Get sprite table address/2
\r
973 // updates: tilecode, sx
\r
974 for (u=0; u < 80 && (pack = *pd); u++, pd+=2)
\r
976 unsigned int *sprite;
\r
977 int code, code2, sx, sy, skip=0;
\r
979 sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite
\r
981 // parse sprite info
\r
984 code2 &= ~0xfe000000;
\r
985 code2 -= 0x00780000; // Get X coordinate + 8 in upper 16 bits
\r
988 if((sx <= 8-((pack>>28)<<3) && sx >= -0x76) || sx >= 328) skip=1<<23;
\r
989 else if ((sy = (pack<<16)>>16) < 240 && sy > -32) {
\r
990 int sbl = (2<<(pack>>28))-1;
\r
991 sblocks |= sbl<<(sy>>3);
\r
994 *pd = (pack&~(1<<23))|skip;
\r
997 // Find next sprite
\r
998 link=(code>>16)&0x7f;
\r
999 if(!link) break; // End of sprites
\r
1001 SpriteBlocks |= sblocks;
\r
1005 for (; u < 80; u++)
\r
1007 unsigned int *sprite;
\r
1008 int code, code2, sx, sy, hv, height, width, skip=0, sx_min;
\r
1010 sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite
\r
1012 // parse sprite info
\r
1014 sy = (code&0x1ff)-0x80;
\r
1015 hv = (code>>24)&0xf;
\r
1016 height = (hv&3)+1;
\r
1018 if (sy > 240 || sy + (height<<3) <= 0) skip|=1<<22; // sprite offscreen (completely, y)
\r
1020 width = (hv>>2)+1;
\r
1021 code2 = sprite[1];
\r
1022 sx = (code2>>16)&0x1ff;
\r
1023 sx -= 0x78; // Get X coordinate + 8
\r
1024 sx_min = 8-(width<<3);
\r
1026 if ((sx <= sx_min && sx >= -0x76) || sx >= 328) skip|=1<<23; // offscreen x
\r
1027 else if (sx > sx_min && !skip) {
\r
1028 int sbl = (2<<height)-1;
\r
1030 if(shi < 0) shi=0; // negative sy
\r
1031 sblocks |= sbl<<shi;
\r
1034 *pd++ = (width<<28)|(height<<24)|skip|(hv<<16)|((unsigned short)sy);
\r
1035 *pd++ = (sx<<16)|((unsigned short)code2);
\r
1037 // Find next sprite
\r
1038 link=(code>>16)&0x7f;
\r
1039 if(!link) break; // End of sprites
\r
1041 SpriteBlocks = sblocks;
\r
1042 *pd = 0; // terminate
\r
1046 static void DrawAllSprites(int *hcache, int maxwidth, int prio, int sh)
\r
1049 int sx1seen = 0; // sprite with x coord 1 or 0 seen
\r
1050 int ntiles = 0; // tile counter for sprite limit emulation
\r
1051 int *sprites[40]; // Sprites to draw in fast mode
\r
1052 int max_line_sprites = 20; // 20 sprites, 40 tiles
\r
1053 int *ps, pack, rs = rendstatus, scan = Scanline;
\r
1055 if (rs & (PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES)) {
\r
1056 //dprintf("PrepareSprites(%i) [%i]", (rs>>4)&1, scan);
\r
1057 PrepareSprites(rs & PDRAW_DIRTY_SPRITES);
\r
1058 rendstatus = rs & ~(PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES);
\r
1060 if (!(SpriteBlocks & (1<<(scan>>3)))) { *hcache = 0; return; }
\r
1062 if (PicoOpt & POPT_DIS_SPRITE_LIM)
\r
1063 max_line_sprites = 80;
\r
1067 // Index + 0 : hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size
\r
1068 // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
\r
1070 for (i=u=n=0; (pack = *ps) && n < max_line_sprites; ps+=2, u++)
\r
1072 int sx, sy, row, pack2;
\r
1074 if (pack & 0x00400000) continue;
\r
1076 // get sprite info
\r
1079 sy = (pack <<16)>>16;
\r
1082 // elprintf(EL_ANOMALY, "x: %4i y: %4i p %i %ix%i", sx, sy, (pack2>>15)&1, (pack>>28)<<3, (pack>>21)&0x38);
\r
1084 if (sx == -0x77) sx1seen|=1; // for masking mode 2
\r
1086 // check if it is on this line
\r
1087 if (row < 0 || row >= ((pack>>21)&0x38)) continue; // no
\r
1089 // masking sprite?
\r
1090 if (sx == -0x78) {
\r
1091 if (n > 0) break; // masked
\r
1095 n++; // number of sprites on this line (both visible and hidden, except of x=0)
\r
1098 ntiles += pack>>28;
\r
1099 if (ntiles > max_line_sprites*2) break;
\r
1101 if (pack & 0x00800000) continue;
\r
1103 // sprite is good, save it's pointer
\r
1107 n = (rs & PDRAW_ACC_SPRITES) ? 1 : 0;
\r
1109 // Go through sprites backwards:
\r
1110 for (i--; i>=0; i--)
\r
1111 DrawSprite(sprites[i],&hcache,sh,n);
\r
1113 // terminate cache list
\r
1118 // --------------------------------------------
\r
1120 #ifndef _ASM_DRAW_C
\r
1121 static void BackFill(int reg7, int sh)
\r
1123 unsigned int back;
\r
1125 // Start with a blank scanline (background colour):
\r
1131 memset32((int *)(HighCol+8), back, 320/4);
\r
1135 // --------------------------------------------
\r
1137 unsigned short HighPal[0x100];
\r
1139 #ifndef _ASM_DRAW_C
\r
1140 static void FinalizeLineBGR444(int sh)
\r
1142 unsigned short *pd=DrawLineDest;
\r
1143 unsigned char *ps=HighCol+8;
\r
1144 unsigned short *pal=Pico.cram;
\r
1145 int len, i, t, mask=0xff;
\r
1147 if (Pico.video.reg[12]&1) {
\r
1150 if(!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32;
\r
1156 if(Pico.m.dirtyPal) {
\r
1157 blockcpy(pal, Pico.cram, 0x40*2);
\r
1158 // shadowed pixels
\r
1159 for(i = 0x3f; i >= 0; i--)
\r
1160 pal[0x40|i] = pal[0xc0|i] = (unsigned short)((pal[i]>>1)&0x0777);
\r
1161 // hilighted pixels
\r
1162 for(i = 0x3f; i >= 0; i--) {
\r
1163 t=pal[i]&0xeee;t+=0x444;if(t&0x10)t|=0xe;if(t&0x100)t|=0xe0;if(t&0x1000)t|=0xe00;t&=0xeee;
\r
1164 pal[0x80|i]=(unsigned short)t;
\r
1166 Pico.m.dirtyPal = 0;
\r
1170 if (!sh && (rendstatus & PDRAW_ACC_SPRITES))
\r
1171 mask=0x3f; // accurate sprites
\r
1173 for(i = 0; i < len; i++)
\r
1174 pd[i] = pal[ps[i] & mask];
\r
1178 static void FinalizeLineRGB555(int sh)
\r
1180 unsigned short *pd=DrawLineDest;
\r
1181 unsigned char *ps=HighCol+8;
\r
1182 unsigned short *pal=HighPal;
\r
1183 int len, i, t, dirtyPal = Pico.m.dirtyPal;
\r
1187 unsigned int *spal=(void *)Pico.cram;
\r
1188 unsigned int *dpal=(void *)HighPal;
\r
1189 for (i = 0x3f/2; i >= 0; i--)
\r
1191 dpal[i] = ((spal[i]&0x000f000f)<< 1)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)<<4);
\r
1193 dpal[i] = ((spal[i]&0x000f000f)<<12)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)>>7);
\r
1195 Pico.m.dirtyPal = 0;
\r
1201 // shadowed pixels
\r
1202 for (i = 0x3f; i >= 0; i--)
\r
1203 pal[0x40|i] = pal[0xc0|i] = (unsigned short)((pal[i]>>1)&0x738e);
\r
1204 // hilighted pixels
\r
1205 for (i = 0x3f; i >= 0; i--) {
\r
1206 t=pal[i]&0xe71c;t+=0x4208;if(t&0x20)t|=0x1c;if(t&0x800)t|=0x700;if(t&0x10000)t|=0xe000;t&=0xe71c;
\r
1207 pal[0x80|i]=(unsigned short)t;
\r
1212 if (Pico.video.reg[12]&1) {
\r
1215 if (!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32;
\r
1222 if (!sh && (rendstatus & PDRAW_ACC_SPRITES))
\r
1223 mask=0x3f; // accurate sprites, upper bits are priority stuff
\r
1225 for (i = 0; i < len; i++)
\r
1226 pd[i] = pal[ps[i] & mask];
\r
1228 extern void amips_clut(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);
\r
1229 extern void amips_clut_6bit(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);
\r
1230 if (!sh && (rendstatus & PDRAW_ACC_SPRITES))
\r
1231 amips_clut_6bit(pd, ps, pal, len);
\r
1232 else amips_clut(pd, ps, pal, len);
\r
1238 static void FinalizeLine8bit(int sh)
\r
1240 unsigned char *pd=DrawLineDest;
\r
1241 int len, rs = rendstatus;
\r
1242 static int dirty_count;
\r
1244 if (!sh && !(rs & PDRAW_ACC_SPRITES) && Pico.m.dirtyPal == 1 && Scanline < 222)
\r
1246 // a hack for mid-frame palette changes
\r
1247 if (!(rs & PDRAW_SONIC_MODE))
\r
1249 else dirty_count++;
\r
1250 rs |= PDRAW_SONIC_MODE;
\r
1252 if (dirty_count == 3) {
\r
1253 blockcpy(HighPal, Pico.cram, 0x40*2);
\r
1254 } else if (dirty_count == 11) {
\r
1255 blockcpy(HighPal+0x40, Pico.cram, 0x40*2);
\r
1259 if (Pico.video.reg[12]&1) {
\r
1262 if (!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32;
\r
1266 if (!sh && (rs & PDRAW_SONIC_MODE)) {
\r
1267 if (dirty_count >= 11) {
\r
1268 blockcpy_or(pd, HighCol+8, len, 0x80);
\r
1270 blockcpy_or(pd, HighCol+8, len, 0x40);
\r
1273 blockcpy(pd, HighCol+8, len);
\r
1277 static void (*FinalizeLine)(int sh) = FinalizeLineBGR444;
\r
1279 // hblank was enabled early during prev line processng -
\r
1280 // it should have been blanked
\r
1281 static void handle_early_blank(int scanline, int sh)
\r
1285 if (PicoScanBegin != NULL)
\r
1286 PicoScanBegin(scanline);
\r
1288 BackFill(Pico.video.reg[7], sh);
\r
1290 if (FinalizeLine != NULL)
\r
1293 if (PicoScanEnd != NULL)
\r
1294 PicoScanEnd(scanline);
\r
1297 // --------------------------------------------
\r
1299 static int DrawDisplay(int sh, int as)
\r
1301 struct PicoVideo *pvid=&Pico.video;
\r
1302 int win=0,edge=0,hvwind=0;
\r
1303 int maxw, maxcells;
\r
1305 rendstatus &= ~(PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO);
\r
1307 if(pvid->reg[12]&1) {
\r
1308 maxw = 328; maxcells = 40;
\r
1310 maxw = 264; maxcells = 32;
\r
1313 // Find out if the window is on this line:
\r
1314 win=pvid->reg[0x12];
\r
1315 edge=(win&0x1f)<<3;
\r
1317 if (win&0x80) { if (Scanline>=edge) hvwind=1; }
\r
1318 else { if (Scanline< edge) hvwind=1; }
\r
1320 if (!hvwind) { // we might have a vertical window here
\r
1321 win=pvid->reg[0x11];
\r
1324 if (!edge) hvwind=1;
\r
1325 else if(edge < (maxcells>>1)) hvwind=2;
\r
1328 else if(edge < (maxcells>>1)) hvwind=2;
\r
1333 DrawLayer(1|((sh|as)<<1), HighCacheB, 0, maxcells);
\r
1335 DrawWindow(0, maxcells>>1, 0, sh|as);
\r
1336 else if (hvwind == 2) {
\r
1337 // ahh, we have vertical window
\r
1338 DrawLayer(0|((sh|as)<<1), HighCacheA, (win&0x80) ? 0 : edge<<1, (win&0x80) ? edge<<1 : maxcells);
\r
1339 DrawWindow( (win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 0, sh|as);
\r
1341 DrawLayer(0|((sh|as)<<1), HighCacheA, 0, maxcells);
\r
1342 DrawAllSpritesLoPri(HighCacheS, maxw, 0, sh);
\r
1344 if (HighCacheB[0]) DrawTilesFromCache(HighCacheB, sh, 328);
\r
1346 DrawWindow(0, maxcells>>1, 1, sh);
\r
1347 else if (hvwind == 2) {
\r
1348 if(HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, (win&0x80) ? edge<<4 : 328);
\r
1349 DrawWindow((win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 1, sh);
\r
1351 if (HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, 328);
\r
1352 DrawAllSpritesHiPri(HighCacheS, maxw, 1, sh);
\r
1357 for (a = 0, c = HighCacheA; *c; c++, a++);
\r
1358 for (b = 0, c = HighCacheB; *c; c++, b++);
\r
1359 printf("%i:%03i: a=%i, b=%i\n", Pico.m.frame_count, Scanline, a, b);
\r
1367 PICO_INTERNAL void PicoFrameStart(void)
\r
1369 // prepare to do this frame
\r
1370 rendstatus = (PicoOpt&0x80)>>5; // accurate sprites, clear everything else
\r
1371 if ((Pico.video.reg[12]&6) == 6) {
\r
1372 rendstatus |= PDRAW_INTERLACE; // interlace mode
\r
1373 DrawAllSpritesLoPri = DrawAllSpritesInterlace;
\r
1374 DrawAllSpritesHiPri = DrawAllSpritesInterlace;
\r
1378 DrawAllSpritesLoPri = DrawAllSprites;
\r
1379 DrawAllSpritesHiPri = rendstatus ? DrawSpritesFromCacheAS : DrawSpritesFromCache;
\r
1382 if (Pico.m.dirtyPal) Pico.m.dirtyPal = 2; // reset dirty if needed
\r
1384 PrepareSprites(1);
\r
1388 PICO_INTERNAL int PicoLine(int scan)
\r
1391 if (skip_next_line>0) { skip_next_line--; return 0; } // skip_next_line rendering lines
\r
1394 sh=(Pico.video.reg[0xC]&8)>>3; // shadow/hilight?
\r
1395 if (rendstatus & PDRAW_ACC_SPRITES) as|=1; // accurate sprites
\r
1397 if (rendstatus & PDRAW_EARLY_BLANK) {
\r
1398 if (scan > 0) handle_early_blank(scan, sh);
\r
1399 rendstatus &= ~PDRAW_EARLY_BLANK;
\r
1402 if (PicoScanBegin != NULL)
\r
1403 skip_next_line = PicoScanBegin(scan);
\r
1406 BackFill(Pico.video.reg[7], sh|as);
\r
1407 if (Pico.video.reg[1]&0x40)
\r
1408 DrawDisplay(sh, as);
\r
1410 if (FinalizeLine != NULL)
\r
1413 if (PicoScanEnd != NULL)
\r
1414 PicoScanEnd(scan);
\r
1420 void PicoDrawSetColorFormat(int which)
\r
1424 case 2: FinalizeLine = FinalizeLine8bit; break;
\r
1425 case 1: FinalizeLine = FinalizeLineRGB555; break;
\r
1426 case 0: FinalizeLine = FinalizeLineBGR444; break;
\r
1427 default:FinalizeLine = NULL; break;
\r
1429 #if OVERRIDE_HIGHCOL
\r
1430 if (which) HighCol=DefHighCol;
\r