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 #include "PicoInt.h"
\r
12 int (*PicoScanBegin)(unsigned int num) = NULL;
\r
13 int (*PicoScanEnd) (unsigned int num) = NULL;
\r
15 #if OVERRIDE_HIGHCOL
\r
16 static unsigned char DefHighCol[8+320+8];
\r
17 unsigned char *HighCol=DefHighCol;
\r
19 unsigned char HighCol[8+320+8];
\r
21 unsigned short DefOutBuff[320*2];
\r
22 void *DrawLineDest=DefOutBuff; // pointer to dest buffer where to draw this line to
\r
24 static int HighCacheA[41+1]; // caches for high layers
\r
25 static int HighCacheB[41+1];
\r
26 static int HighCacheS[80+1]; // and sprites
\r
27 static int HighPreSpr[80*2+1]; // slightly preprocessed sprites
\r
29 static unsigned char HighSprZ[320+8+8]; // Z-buffer for accurate sprites
\r
32 int Scanline = 0; // Scanline
\r
34 static int SpriteBlocks;
\r
35 static int skip_next_line=0;
\r
37 //unsigned short ppt[] = { 0x0f11, 0x0ff1, 0x01f1, 0x011f, 0x01ff, 0x0f1f, 0x0f0e, 0x0e7c };
\r
41 int nametab; // Position in VRAM of name table (for this tile line)
\r
42 int line; // Line number in pixels 0x000-0x3ff within the virtual tilemap
\r
43 int hscroll; // Horizontal scroll value in pixels for the line
\r
44 int xmask; // X-Mask (0x1f - 0x7f) for horizontal wraparound in the tilemap
\r
45 int *hc; // cache for high tile codes and their positions
\r
46 int cells; // cells (tiles) to draw (32 col mode doesn't need to update whole 320)
\r
49 // stuff available in asm:
\r
51 void DrawWindow(int tstart, int tend, int prio, int sh);
\r
52 void BackFill(int reg7, int sh);
\r
53 void DrawSprite(int *sprite, int **hc, int sh);
\r
54 void DrawTilesFromCache(int *hc, int sh, int rlim);
\r
55 void DrawSpritesFromCache(int *hc, int sh);
\r
56 void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells);
\r
57 void FinalizeLineBGR444(int sh);
\r
58 void FinalizeLineRGB555(int sh);
\r
59 void *blockcpy(void *dst, const void *src, size_t n);
\r
60 void blockcpy_or(void *dst, void *src, size_t n, int pat);
\r
63 void blockcpy_or(void *dst, void *src, size_t n, int pat)
\r
65 unsigned char *pd = dst, *ps = src;
\r
67 *pd++ = (unsigned char) (*ps++ | pat);
\r
69 #define blockcpy memcpy
\r
73 #ifdef _ASM_DRAW_C_AMIPS
\r
74 int TileNorm(int sx,int addr,int pal);
\r
75 int TileFlip(int sx,int addr,int pal);
\r
77 static int TileNorm(int sx,int addr,int pal)
\r
79 unsigned char *pd = HighCol+sx;
\r
80 unsigned int pack=0; unsigned int t=0;
\r
82 pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
85 t=pack&0x0000f000; if (t) pd[0]=(unsigned char)(pal|(t>>12));
\r
86 t=pack&0x00000f00; if (t) pd[1]=(unsigned char)(pal|(t>> 8));
\r
87 t=pack&0x000000f0; if (t) pd[2]=(unsigned char)(pal|(t>> 4));
\r
88 t=pack&0x0000000f; if (t) pd[3]=(unsigned char)(pal|(t ));
\r
89 t=pack&0xf0000000; if (t) pd[4]=(unsigned char)(pal|(t>>28));
\r
90 t=pack&0x0f000000; if (t) pd[5]=(unsigned char)(pal|(t>>24));
\r
91 t=pack&0x00f00000; if (t) pd[6]=(unsigned char)(pal|(t>>20));
\r
92 t=pack&0x000f0000; if (t) pd[7]=(unsigned char)(pal|(t>>16));
\r
96 return 1; // Tile blank
\r
99 static int TileFlip(int sx,int addr,int pal)
\r
101 unsigned char *pd = HighCol+sx;
\r
102 unsigned int pack=0; unsigned int t=0;
\r
104 pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
107 t=pack&0x000f0000; if (t) pd[0]=(unsigned char)(pal|(t>>16));
\r
108 t=pack&0x00f00000; if (t) pd[1]=(unsigned char)(pal|(t>>20));
\r
109 t=pack&0x0f000000; if (t) pd[2]=(unsigned char)(pal|(t>>24));
\r
110 t=pack&0xf0000000; if (t) pd[3]=(unsigned char)(pal|(t>>28));
\r
111 t=pack&0x0000000f; if (t) pd[4]=(unsigned char)(pal|(t ));
\r
112 t=pack&0x000000f0; if (t) pd[5]=(unsigned char)(pal|(t>> 4));
\r
113 t=pack&0x00000f00; if (t) pd[6]=(unsigned char)(pal|(t>> 8));
\r
114 t=pack&0x0000f000; if (t) pd[7]=(unsigned char)(pal|(t>>12));
\r
117 return 1; // Tile blank
\r
121 // tile renderers for hacky operator sprite support
\r
122 #define sh_pix(x) \
\r
124 else if(t==0xe) pd[x]=(unsigned char)((pd[x]&0x3f)|0x80); /* hilight */ \
\r
125 else if(t==0xf) pd[x]=(unsigned char)((pd[x]&0x3f)|0xc0); /* shadow */ \
\r
126 else pd[x]=(unsigned char)(pal|t)
\r
128 #ifndef _ASM_DRAW_C
\r
129 static int TileNormSH(int sx,int addr,int pal)
\r
131 unsigned int pack=0; unsigned int t=0;
\r
132 unsigned char *pd = HighCol+sx;
\r
134 pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
137 t=(pack&0x0000f000)>>12; sh_pix(0);
\r
138 t=(pack&0x00000f00)>> 8; sh_pix(1);
\r
139 t=(pack&0x000000f0)>> 4; sh_pix(2);
\r
140 t=(pack&0x0000000f) ; sh_pix(3);
\r
141 t=(pack&0xf0000000)>>28; sh_pix(4);
\r
142 t=(pack&0x0f000000)>>24; sh_pix(5);
\r
143 t=(pack&0x00f00000)>>20; sh_pix(6);
\r
144 t=(pack&0x000f0000)>>16; sh_pix(7);
\r
148 return 1; // Tile blank
\r
151 static int TileFlipSH(int sx,int addr,int pal)
\r
153 unsigned int pack=0; unsigned int t=0;
\r
154 unsigned char *pd = HighCol+sx;
\r
156 pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
159 t=(pack&0x000f0000)>>16; sh_pix(0);
\r
160 t=(pack&0x00f00000)>>20; sh_pix(1);
\r
161 t=(pack&0x0f000000)>>24; sh_pix(2);
\r
162 t=(pack&0xf0000000)>>28; sh_pix(3);
\r
163 t=(pack&0x0000000f) ; sh_pix(4);
\r
164 t=(pack&0x000000f0)>> 4; sh_pix(5);
\r
165 t=(pack&0x00000f00)>> 8; sh_pix(6);
\r
166 t=(pack&0x0000f000)>>12; sh_pix(7);
\r
169 return 1; // Tile blank
\r
173 static int TileNormZ(int sx,int addr,int pal,int zval)
\r
175 unsigned int pack=0; unsigned int t=0;
\r
176 unsigned char *pd = HighCol+sx;
\r
177 unsigned char *zb = HighSprZ+sx;
\r
180 pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
183 t=pack&0x0000f000; if(t) { if(zb[0]) collision=1; if(zval>zb[0]) { pd[0]=(unsigned char)(pal|(t>>12)); zb[0]=(char)zval; } }
\r
184 t=pack&0x00000f00; if(t) { if(zb[1]) collision=1; if(zval>zb[1]) { pd[1]=(unsigned char)(pal|(t>> 8)); zb[1]=(char)zval; } }
\r
185 t=pack&0x000000f0; if(t) { if(zb[2]) collision=1; if(zval>zb[2]) { pd[2]=(unsigned char)(pal|(t>> 4)); zb[2]=(char)zval; } }
\r
186 t=pack&0x0000000f; if(t) { if(zb[3]) collision=1; if(zval>zb[3]) { pd[3]=(unsigned char)(pal|(t )); zb[3]=(char)zval; } }
\r
187 t=pack&0xf0000000; if(t) { if(zb[4]) collision=1; if(zval>zb[4]) { pd[4]=(unsigned char)(pal|(t>>28)); zb[4]=(char)zval; } }
\r
188 t=pack&0x0f000000; if(t) { if(zb[5]) collision=1; if(zval>zb[5]) { pd[5]=(unsigned char)(pal|(t>>24)); zb[5]=(char)zval; } }
\r
189 t=pack&0x00f00000; if(t) { if(zb[6]) collision=1; if(zval>zb[6]) { pd[6]=(unsigned char)(pal|(t>>20)); zb[6]=(char)zval; } }
\r
190 t=pack&0x000f0000; if(t) { if(zb[7]) collision=1; if(zval>zb[7]) { pd[7]=(unsigned char)(pal|(t>>16)); zb[7]=(char)zval; } }
\r
191 if (collision) Pico.video.status|=0x20;
\r
195 return 1; // Tile blank
\r
198 static int TileFlipZ(int sx,int addr,int pal,int zval)
\r
200 unsigned int pack=0; unsigned int t=0;
\r
201 unsigned char *pd = HighCol+sx;
\r
202 unsigned char *zb = HighSprZ+sx;
\r
205 pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
208 t=pack&0x000f0000; if(t) { if(zb[0]) collision=1; if(zval>zb[0]) { pd[0]=(unsigned char)(pal|(t>>16)); zb[0]=(char)zval; } }
\r
209 t=pack&0x00f00000; if(t) { if(zb[1]) collision=1; if(zval>zb[1]) { pd[1]=(unsigned char)(pal|(t>>20)); zb[1]=(char)zval; } }
\r
210 t=pack&0x0f000000; if(t) { if(zb[2]) collision=1; if(zval>zb[2]) { pd[2]=(unsigned char)(pal|(t>>24)); zb[2]=(char)zval; } }
\r
211 t=pack&0xf0000000; if(t) { if(zb[3]) collision=1; if(zval>zb[3]) { pd[3]=(unsigned char)(pal|(t>>28)); zb[3]=(char)zval; } }
\r
212 t=pack&0x0000000f; if(t) { if(zb[4]) collision=1; if(zval>zb[4]) { pd[4]=(unsigned char)(pal|(t )); zb[4]=(char)zval; } }
\r
213 t=pack&0x000000f0; if(t) { if(zb[5]) collision=1; if(zval>zb[5]) { pd[5]=(unsigned char)(pal|(t>> 4)); zb[5]=(char)zval; } }
\r
214 t=pack&0x00000f00; if(t) { if(zb[6]) collision=1; if(zval>zb[6]) { pd[6]=(unsigned char)(pal|(t>> 8)); zb[6]=(char)zval; } }
\r
215 t=pack&0x0000f000; if(t) { if(zb[7]) collision=1; if(zval>zb[7]) { pd[7]=(unsigned char)(pal|(t>>12)); zb[7]=(char)zval; } }
\r
216 if (collision) Pico.video.status|=0x20;
\r
219 return 1; // Tile blank
\r
223 #define sh_pixZ(x) \
\r
225 if(zb[x]) collision=1; \
\r
227 if (t==0xe) { pd[x]=(unsigned char)((pd[x]&0x3f)|0x80); /* hilight */ } \
\r
228 else if(t==0xf) { pd[x]=(unsigned char)((pd[x]&0x3f)|0xc0); /* shadow */ } \
\r
229 else { zb[x]=(char)zval; pd[x]=(unsigned char)(pal|t); } \
\r
233 static int TileNormZSH(int sx,int addr,int pal,int zval)
\r
235 unsigned int pack=0; unsigned int t=0;
\r
236 unsigned char *pd = HighCol+sx;
\r
237 unsigned char *zb = HighSprZ+sx;
\r
240 pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
243 t=(pack&0x0000f000)>>12; sh_pixZ(0);
\r
244 t=(pack&0x00000f00)>> 8; sh_pixZ(1);
\r
245 t=(pack&0x000000f0)>> 4; sh_pixZ(2);
\r
246 t=(pack&0x0000000f) ; sh_pixZ(3);
\r
247 t=(pack&0xf0000000)>>28; sh_pixZ(4);
\r
248 t=(pack&0x0f000000)>>24; sh_pixZ(5);
\r
249 t=(pack&0x00f00000)>>20; sh_pixZ(6);
\r
250 t=(pack&0x000f0000)>>16; sh_pixZ(7);
\r
251 if(collision) Pico.video.status|=0x20;
\r
255 return 1; // Tile blank
\r
258 static int TileFlipZSH(int sx,int addr,int pal,int zval)
\r
260 unsigned int pack=0; unsigned int t=0;
\r
261 unsigned char *pd = HighCol+sx;
\r
262 unsigned char *zb = HighSprZ+sx;
\r
265 pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
268 t=(pack&0x000f0000)>>16; sh_pixZ(0);
\r
269 t=(pack&0x00f00000)>>20; sh_pixZ(1);
\r
270 t=(pack&0x0f000000)>>24; sh_pixZ(2);
\r
271 t=(pack&0xf0000000)>>28; sh_pixZ(3);
\r
272 t=(pack&0x0000000f) ; sh_pixZ(4);
\r
273 t=(pack&0x000000f0)>> 4; sh_pixZ(5);
\r
274 t=(pack&0x00000f00)>> 8; sh_pixZ(6);
\r
275 t=(pack&0x0000f000)>>12; sh_pixZ(7);
\r
276 if(collision) Pico.video.status|=0x20;
\r
279 return 1; // Tile blank
\r
282 // --------------------------------------------
\r
284 #ifndef _ASM_DRAW_C
\r
285 static void DrawStrip(struct TileStrip *ts, int plane_sh, int cellskip)
\r
287 int tilex,dx,ty,code=0,addr=0,cells;
\r
288 int oldcode=-1,blank=-1; // The tile we know is blank
\r
291 // Draw tiles across screen:
\r
292 sh=(plane_sh<<5)&0x40;
\r
293 tilex=((-ts->hscroll)>>3)+cellskip;
\r
294 ty=(ts->line&7)<<1; // Y-Offset into tile
\r
295 dx=((ts->hscroll-1)&7)+1;
\r
296 cells = ts->cells - cellskip;
\r
297 if(dx != 8) cells++; // have hscroll, need to draw 1 cell more
\r
300 for (; cells > 0; dx+=8,tilex++,cells--)
\r
304 code=Pico.vram[ts->nametab+(tilex&ts->xmask)];
\r
305 if (code==blank) continue;
\r
306 if (code>>15) { // high priority tile
\r
307 int cval = code | (dx<<16) | (ty<<25);
\r
308 if(code&0x1000) cval^=7<<26;
\r
309 *ts->hc++ = cval; // cache it
\r
313 if (code!=oldcode) {
\r
315 // Get tile address/2:
\r
316 addr=(code&0x7ff)<<4;
\r
318 if (code&0x1000) addr^=0xe; // Y-flip
\r
320 pal=((code>>9)&0x30)|sh;
\r
323 if (code&0x0800) zero=TileFlip(dx,addr,pal);
\r
324 else zero=TileNorm(dx,addr,pal);
\r
326 if (zero) blank=code; // We know this tile is blank now
\r
329 // terminate the cache list
\r
331 // if oldcode wasn't changed, it means all layer is hi priority
\r
332 if (oldcode == -1) rendstatus |= PDRAW_PLANE_HI_PRIO;
\r
336 void DrawStripVSRam(struct TileStrip *ts, int plane_sh, int cellskip)
\r
338 int tilex,dx,code=0,addr=0,cell=0;
\r
339 int oldcode=-1,blank=-1; // The tile we know is blank
\r
340 int pal=0,scan=Scanline;
\r
342 // Draw tiles across screen:
\r
343 tilex=(-ts->hscroll)>>3;
\r
344 dx=((ts->hscroll-1)&7)+1;
\r
345 if(dx != 8) cell--; // have hscroll, start with negative cell
\r
350 for (; cell < ts->cells; dx+=8,tilex++,cell++)
\r
352 int zero=0,nametabadd,ty;
\r
357 vscroll=Pico.vsram[(plane_sh&1)+(cell&~1)];
\r
359 // Find the line in the name table
\r
360 line=(vscroll+scan)&ts->line&0xffff; // ts->line is really ymask ..
\r
361 nametabadd=(line>>3)<<(ts->line>>24); // .. and shift[width]
\r
362 ty=(line&7)<<1; // Y-Offset into tile
\r
365 code=Pico.vram[ts->nametab+nametabadd+(tilex&ts->xmask)];
\r
366 if (code==blank) continue;
\r
367 if (code>>15) { // high priority tile
\r
368 int cval = code | (dx<<16) | (ty<<25);
\r
369 if(code&0x1000) cval^=7<<26;
\r
370 *ts->hc++ = cval; // cache it
\r
374 if (code!=oldcode) {
\r
376 // Get tile address/2:
\r
377 addr=(code&0x7ff)<<4;
\r
378 if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
\r
380 pal=((code>>9)&0x30)|((plane_sh<<5)&0x40);
\r
383 if (code&0x0800) zero=TileFlip(dx,addr,pal);
\r
384 else zero=TileNorm(dx,addr,pal);
\r
386 if (zero) blank=code; // We know this tile is blank now
\r
389 // terminate the cache list
\r
391 if (oldcode == -1) rendstatus |= PDRAW_PLANE_HI_PRIO;
\r
395 #ifndef _ASM_DRAW_C
\r
398 void DrawStripInterlace(struct TileStrip *ts)
\r
400 int tilex=0,dx=0,ty=0,code=0,addr=0,cells;
\r
401 int oldcode=-1,blank=-1; // The tile we know is blank
\r
404 // Draw tiles across screen:
\r
405 tilex=(-ts->hscroll)>>3;
\r
406 ty=(ts->line&15)<<1; // Y-Offset into tile
\r
407 dx=((ts->hscroll-1)&7)+1;
\r
409 if(dx != 8) cells++; // have hscroll, need to draw 1 cell more
\r
411 for (; cells; dx+=8,tilex++,cells--)
\r
415 code=Pico.vram[ts->nametab+(tilex&ts->xmask)];
\r
416 if (code==blank) continue;
\r
417 if (code>>15) { // high priority tile
\r
418 int cval = (code&0xfc00) | (dx<<16) | (ty<<25);
\r
419 cval|=(code&0x3ff)<<1;
\r
420 if(code&0x1000) cval^=0xf<<26;
\r
421 *ts->hc++ = cval; // cache it
\r
425 if (code!=oldcode) {
\r
427 // Get tile address/2:
\r
428 addr=(code&0x7ff)<<5;
\r
429 if (code&0x1000) addr+=30-ty; else addr+=ty; // Y-flip
\r
431 // pal=Pico.cram+((code>>9)&0x30);
\r
432 pal=((code>>9)&0x30);
\r
435 if (code&0x0800) zero=TileFlip(dx,addr,pal);
\r
436 else zero=TileNorm(dx,addr,pal);
\r
438 if (zero) blank=code; // We know this tile is blank now
\r
441 // terminate the cache list
\r
445 // --------------------------------------------
\r
447 #ifndef _ASM_DRAW_C
\r
448 static void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells)
\r
450 struct PicoVideo *pvid=&Pico.video;
\r
451 const char shift[4]={5,6,5,7}; // 32,64 or 128 sized tilemaps (2 is invalid)
\r
452 struct TileStrip ts;
\r
453 int width, height, ymask;
\r
459 // Work out the TileStrip to draw
\r
461 // Work out the name table size: 32 64 or 128 tiles (0-3)
\r
462 width=pvid->reg[16];
\r
463 height=(width>>4)&3; width&=3;
\r
465 ts.xmask=(1<<shift[width])-1; // X Mask in tiles (0x1f-0x7f)
\r
466 ymask=(height<<8)|0xff; // Y Mask in pixels
\r
467 if(width == 1) ymask&=0x1ff;
\r
468 else if(width>1) ymask =0x0ff;
\r
470 // Find name table:
\r
471 if (plane_sh&1) ts.nametab=(pvid->reg[4]&0x07)<<12; // B
\r
472 else ts.nametab=(pvid->reg[2]&0x38)<< 9; // A
\r
474 htab=pvid->reg[13]<<9; // Horizontal scroll table address
\r
475 if ( pvid->reg[11]&2) htab+=Scanline<<1; // Offset by line
\r
476 if ((pvid->reg[11]&1)==0) htab&=~0xf; // Offset by tile
\r
477 htab+=plane_sh&1; // A or B
\r
479 // Get horizontal scroll value, will be masked later
\r
480 ts.hscroll=Pico.vram[htab&0x7fff];
\r
482 if((pvid->reg[12]&6) == 6) {
\r
483 // interlace mode 2
\r
484 vscroll=Pico.vsram[plane_sh&1]; // Get vertical scroll value
\r
486 // Find the line in the name table
\r
487 ts.line=(vscroll+(Scanline<<1))&((ymask<<1)|1);
\r
488 ts.nametab+=(ts.line>>4)<<shift[width];
\r
490 DrawStripInterlace(&ts);
\r
491 } else if( pvid->reg[11]&4) {
\r
492 // shit, we have 2-cell column based vscroll
\r
493 // luckily this doesn't happen too often
\r
494 ts.line=ymask|(shift[width]<<24); // save some stuff instead of line
\r
495 DrawStripVSRam(&ts, plane_sh, cellskip);
\r
497 vscroll=Pico.vsram[plane_sh&1]; // Get vertical scroll value
\r
499 // Find the line in the name table
\r
500 ts.line=(vscroll+Scanline)&ymask;
\r
501 ts.nametab+=(ts.line>>3)<<shift[width];
\r
503 DrawStrip(&ts, plane_sh, cellskip);
\r
508 // --------------------------------------------
\r
510 // tstart & tend are tile pair numbers
\r
511 static void DrawWindow(int tstart, int tend, int prio, int sh) // int *hcache
\r
513 struct PicoVideo *pvid=&Pico.video;
\r
514 int tilex=0,ty=0,nametab,code=0;
\r
515 int blank=-1; // The tile we know is blank
\r
517 // Find name table line:
\r
518 if (pvid->reg[12]&1)
\r
520 nametab=(pvid->reg[3]&0x3c)<<9; // 40-cell mode
\r
521 nametab+=(Scanline>>3)<<6;
\r
525 nametab=(pvid->reg[3]&0x3e)<<9; // 32-cell mode
\r
526 nametab+=(Scanline>>3)<<5;
\r
532 ty=(Scanline&7)<<1; // Y-Offset into tile
\r
534 if (!(rendstatus & PDRAW_WND_DIFF_PRIO)) {
\r
535 // check the first tile code
\r
536 code=Pico.vram[nametab+tilex];
\r
537 // if the whole window uses same priority (what is often the case), we may be able to skip this field
\r
538 if ((code>>15) != prio) return;
\r
541 // Draw tiles across screen:
\r
544 for (; tilex < tend; tilex++)
\r
549 code=Pico.vram[nametab+tilex];
\r
550 if (code==blank) continue;
\r
551 if ((code>>15) != prio) {
\r
552 rendstatus |= PDRAW_WND_DIFF_PRIO;
\r
556 pal=((code>>9)&0x30);
\r
558 // Get tile address/2:
\r
559 addr=(code&0x7ff)<<4;
\r
560 if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
\r
562 if (code&0x0800) zero=TileFlip(8+(tilex<<3),addr,pal);
\r
563 else zero=TileNorm(8+(tilex<<3),addr,pal);
\r
565 if (zero) blank=code; // We know this tile is blank now
\r
570 for (; tilex < tend; tilex++)
\r
575 code=Pico.vram[nametab+tilex];
\r
576 if(code==blank) continue;
\r
577 if((code>>15) != prio) {
\r
578 rendstatus |= PDRAW_WND_DIFF_PRIO;
\r
582 pal=((code>>9)&0x30);
\r
584 zb = (int *)(HighCol+8+(tilex<<3));
\r
587 if(!(tmp&0x00000080)) tmp&=~0x000000c0; if(!(tmp&0x00008000)) tmp&=~0x0000c000;
\r
588 if(!(tmp&0x00800000)) tmp&=~0x00c00000; if(!(tmp&0x80000000)) tmp&=~0xc0000000;
\r
589 *zb++=tmp; tmp = *zb;
\r
590 if(!(tmp&0x00000080)) tmp&=~0x000000c0; if(!(tmp&0x00008000)) tmp&=~0x0000c000;
\r
591 if(!(tmp&0x00800000)) tmp&=~0x00c00000; if(!(tmp&0x80000000)) tmp&=~0xc0000000;
\r
597 // Get tile address/2:
\r
598 addr=(code&0x7ff)<<4;
\r
599 if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
\r
601 if (code&0x0800) zero=TileFlip(8+(tilex<<3),addr,pal);
\r
602 else zero=TileNorm(8+(tilex<<3),addr,pal);
\r
604 if (zero) blank=code; // We know this tile is blank now
\r
609 // --------------------------------------------
\r
611 static void DrawTilesFromCacheShPrep(void)
\r
613 // as some layer has covered whole line with hi priority tiles,
\r
614 // we can process whole line and then act as if sh/hi mode was off.
\r
615 int c = 320/4, *zb = (int *)(HighCol+8);
\r
616 rendstatus |= PDRAW_SHHI_DONE;
\r
620 if (!(tmp & 0x80808080)) *zb=tmp&0x3f3f3f3f;
\r
622 if(!(tmp&0x00000080)) tmp&=~0x000000c0; if(!(tmp&0x00008000)) tmp&=~0x0000c000;
\r
623 if(!(tmp&0x00800000)) tmp&=~0x00c00000; if(!(tmp&0x80000000)) tmp&=~0xc0000000;
\r
630 static void DrawTilesFromCache(int *hc, int sh, int rlim)
\r
632 int code, addr, dx;
\r
635 // *ts->hc++ = code | (dx<<16) | (ty<<25); // cache it
\r
637 if (sh && (rendstatus & (PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO)))
\r
639 if (!(rendstatus & PDRAW_SHHI_DONE))
\r
640 DrawTilesFromCacheShPrep();
\r
646 short blank=-1; // The tile we know is blank
\r
647 while ((code=*hc++)) {
\r
649 if((short)code == blank) continue;
\r
650 // Get tile address/2:
\r
651 addr=(code&0x7ff)<<4;
\r
652 addr+=(unsigned int)code>>25; // y offset into tile
\r
653 dx=(code>>16)&0x1ff;
\r
655 pal=((code>>9)&0x30);
\r
656 if (rlim-dx < 0) goto last_cut_tile;
\r
658 if (code&0x0800) zero=TileFlip(dx,addr,pal);
\r
659 else zero=TileNorm(dx,addr,pal);
\r
661 if (zero) blank=(short)code;
\r
666 while ((code=*hc++)) {
\r
668 // Get tile address/2:
\r
669 addr=(code&0x7ff)<<4;
\r
670 addr+=(unsigned int)code>>25; // y offset into tile
\r
671 dx=(code>>16)&0x1ff;
\r
673 if(!(*zb&0x80)) *zb&=0x3f; zb++; if(!(*zb&0x80)) *zb&=0x3f; zb++;
\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
678 pal=((code>>9)&0x30);
\r
679 if (rlim-dx < 0) goto last_cut_tile;
\r
681 if (code&0x0800) TileFlip(dx,addr,pal);
\r
682 else TileNorm(dx,addr,pal);
\r
689 unsigned int t, pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
690 unsigned char *pd = HighCol+dx;
\r
696 case 7: t=pack&0x00000f00; if (t) pd[6]=(unsigned char)(pal|(t>> 8)); // "break" is left out intentionally
\r
697 case 6: t=pack&0x000000f0; if (t) pd[5]=(unsigned char)(pal|(t>> 4));
\r
698 case 5: t=pack&0x0000000f; if (t) pd[4]=(unsigned char)(pal|(t ));
\r
699 case 4: t=pack&0xf0000000; if (t) pd[3]=(unsigned char)(pal|(t>>28));
\r
700 case 3: t=pack&0x0f000000; if (t) pd[2]=(unsigned char)(pal|(t>>24));
\r
701 case 2: t=pack&0x00f00000; if (t) pd[1]=(unsigned char)(pal|(t>>20));
\r
702 case 1: t=pack&0x000f0000; if (t) pd[0]=(unsigned char)(pal|(t>>16));
\r
710 case 7: t=pack&0x00f00000; if (t) pd[6]=(unsigned char)(pal|(t>>20));
\r
711 case 6: t=pack&0x0f000000; if (t) pd[5]=(unsigned char)(pal|(t>>24));
\r
712 case 5: t=pack&0xf0000000; if (t) pd[4]=(unsigned char)(pal|(t>>28));
\r
713 case 4: t=pack&0x0000000f; if (t) pd[3]=(unsigned char)(pal|(t ));
\r
714 case 3: t=pack&0x000000f0; if (t) pd[2]=(unsigned char)(pal|(t>> 4));
\r
715 case 2: t=pack&0x00000f00; if (t) pd[1]=(unsigned char)(pal|(t>> 8));
\r
716 case 1: t=pack&0x0000f000; if (t) pd[0]=(unsigned char)(pal|(t>>12));
\r
723 // --------------------------------------------
\r
725 // Index + 0 : hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size
\r
726 // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
\r
728 static void DrawSprite(int *sprite, int **hc, int sh)
\r
730 int width=0,height=0;
\r
733 int tile=0,delta=0;
\r
735 int (*fTileFunc)(int sx,int addr,int pal);
\r
737 // parse the sprite data
\r
742 height=(sy>>24)&7; // Width and height in tiles
\r
743 sy=(sy<<16)>>16; // Y
\r
745 row=Scanline-sy; // Row of the sprite we are on
\r
747 if (code&0x1000) row=(height<<3)-1-row; // Flip Y
\r
749 tile=code&0x7ff; // Tile number
\r
750 tile+=row>>3; // Tile number increases going down
\r
751 delta=height; // Delta to increase tile by going right
\r
752 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
\r
754 tile<<=4; tile+=(row&7)<<1; // Tile address
\r
756 if(code&0x8000) { // high priority - cache it
\r
757 *(*hc)++ = (tile<<16)|((code&0x0800)<<5)|((sx<<6)&0x0000ffc0)|((code>>9)&0x30)|((sprite[0]>>16)&0xf);
\r
759 delta<<=4; // Delta of address
\r
760 pal=((code>>9)&0x30)|(sh<<6);
\r
762 if(sh && (code&0x6000) == 0x6000) {
\r
763 if(code&0x0800) fTileFunc=TileFlipSH;
\r
764 else fTileFunc=TileNormSH;
\r
766 if(code&0x0800) fTileFunc=TileFlip;
\r
767 else fTileFunc=TileNorm;
\r
770 for (; width; width--,sx+=8,tile+=delta)
\r
772 if(sx<=0) continue;
\r
773 if(sx>=328) break; // Offscreen
\r
775 tile&=0x7fff; // Clip tile address
\r
776 fTileFunc(sx,tile,pal);
\r
783 // Index + 0 : hhhhvvvv s---hhvv yyyyyyyy yyyyyyyy // s: skip flag, h: horiz. size
\r
784 // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
\r
786 static void DrawSpriteZ(int pack, int pack2, int shpri, int sprio)
\r
788 int width=0,height=0;
\r
791 int tile=0,delta=0;
\r
793 int (*fTileFunc)(int sx,int addr,int pal,int zval);
\r
795 // parse the sprite data
\r
797 sy=(pack <<16)>>16; // Y
\r
799 height=(pack>>24)&7; // Width and height in tiles
\r
801 row=Scanline-sy; // Row of the sprite we are on
\r
803 if (pack2&0x1000) row=(height<<3)-1-row; // Flip Y
\r
805 tile=pack2&0x7ff; // Tile number
\r
806 tile+=row>>3; // Tile number increases going down
\r
807 delta=height; // Delta to increase tile by going right
\r
808 if (pack2&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
\r
810 tile<<=4; tile+=(row&7)<<1; // Tile address
\r
811 delta<<=4; // Delta of address
\r
812 pal=((pack2>>9)&0x30);
\r
813 if((shpri&1)&&!(shpri&2)) pal|=0x40;
\r
816 if((pack2&0x6000) != 0x6000) shpri = 0;
\r
817 shpri |= (pack2&0x0800)>>10;
\r
820 case 0: fTileFunc=TileNormZ; break;
\r
821 case 1: fTileFunc=TileNormZSH; break;
\r
822 case 2: fTileFunc=TileFlipZ; break;
\r
823 case 3: fTileFunc=TileFlipZSH; break;
\r
826 for (; width; width--,sx+=8,tile+=delta)
\r
828 if(sx<=0) continue;
\r
829 if(sx>=328) break; // Offscreen
\r
831 tile&=0x7fff; // Clip tile address
\r
832 fTileFunc(sx,tile,pal,sprio);
\r
836 static void DrawSpriteInterlace(unsigned int *sprite)
\r
838 int width=0,height=0;
\r
841 int tile=0,delta=0;
\r
844 // parse the sprite data
\r
847 sy=(sy&0x3ff)-0x100; // Y
\r
848 width=(height>>2)&3; height&=3;
\r
849 width++; height++; // Width and height in tiles
\r
851 row=(Scanline<<1)-sy; // Row of the sprite we are on
\r
854 sx=((code>>16)&0x1ff)-0x78; // X
\r
856 if (code&0x1000) row^=(16<<height)-1; // Flip Y
\r
858 tile=code&0x3ff; // Tile number
\r
859 tile+=row>>4; // Tile number increases going down
\r
860 delta=height; // Delta to increase tile by going right
\r
861 if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
\r
863 tile<<=5; tile+=(row&15)<<1; // Tile address
\r
865 delta<<=5; // Delta of address
\r
866 pal=((code>>9)&0x30); // Get palette pointer
\r
868 for (; width; width--,sx+=8,tile+=delta)
\r
870 if(sx<=0) continue;
\r
871 if(sx>=328) break; // Offscreen
\r
873 tile&=0x7fff; // Clip tile address
\r
874 if (code&0x0800) TileFlip(sx,tile,pal);
\r
875 else TileNorm(sx,tile,pal);
\r
880 static void DrawAllSpritesInterlace(int pri, int maxwidth)
\r
882 struct PicoVideo *pvid=&Pico.video;
\r
883 int i,u,table,link=0,sline=Scanline<<1;
\r
884 unsigned int *sprites[80]; // Sprite index
\r
886 table=pvid->reg[5]&0x7f;
\r
887 if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode
\r
888 table<<=8; // Get sprite table address/2
\r
890 for (i=u=0; u < 80 && i < 21; u++)
\r
892 unsigned int *sprite;
\r
893 int code, sx, sy, height;
\r
895 sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite
\r
900 if(((sx>>15)&1) != pri) goto nextsprite; // wrong priority sprite
\r
902 // check if it is on this line
\r
903 sy = (code&0x3ff)-0x100;
\r
904 height = (((code>>24)&3)+1)<<4;
\r
905 if(sline < sy || sline >= sy+height) goto nextsprite; // no
\r
907 // check if sprite is not hidden offscreen
\r
908 sx = (sx>>16)&0x1ff;
\r
909 sx -= 0x78; // Get X coordinate + 8
\r
910 if(sx <= -8*3 || sx >= maxwidth) goto nextsprite;
\r
912 // sprite is good, save it's pointer
\r
913 sprites[i++]=sprite;
\r
916 // Find next sprite
\r
917 link=(code>>16)&0x7f;
\r
918 if(!link) break; // End of sprites
\r
921 // Go through sprites backwards:
\r
922 for (i-- ;i>=0; i--)
\r
923 DrawSpriteInterlace(sprites[i]);
\r
927 #ifndef _ASM_DRAW_C
\r
928 static void DrawSpritesFromCache(int *hc, int sh)
\r
930 int code, tile, sx, delta, width;
\r
932 int (*fTileFunc)(int sx,int addr,int pal);
\r
934 // *(*hc)++ = (tile<<16)|((code&0x0800)<<5)|((sx<<6)&0x0000ffc0)|((code>>9)&0x30)|((sprite[0]>>24)&0xf);
\r
936 while((code=*hc++)) {
\r
939 width=delta>>2; delta&=3;
\r
940 width++; delta++; // Width and height in tiles
\r
941 if (code&0x10000) delta=-delta; // Flip X
\r
943 tile=((unsigned int)code>>17)<<1;
\r
944 sx=(code<<16)>>22; // sx can be negative (start offscreen), so sign extend
\r
946 if(sh && pal == 0x30) { //
\r
947 if(code&0x10000) fTileFunc=TileFlipSH;
\r
948 else fTileFunc=TileNormSH;
\r
950 if(code&0x10000) fTileFunc=TileFlip;
\r
951 else fTileFunc=TileNorm;
\r
954 for (; width; width--,sx+=8,tile+=delta)
\r
956 if(sx<=0) continue;
\r
957 if(sx>=328) break; // Offscreen
\r
959 tile&=0x7fff; // Clip tile address
\r
960 fTileFunc(sx,tile,pal);
\r
967 // Index + 0 : ----hhvv -lllllll -------y yyyyyyyy
\r
968 // Index + 4 : -------x xxxxxxxx pccvhnnn nnnnnnnn
\r
970 // Index + 0 : hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size
\r
971 // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
\r
973 static void PrepareSprites(int full)
\r
975 struct PicoVideo *pvid=&Pico.video;
\r
976 int u=0,link=0,sblocks=0;
\r
978 int *pd = HighPreSpr;
\r
980 table=pvid->reg[5]&0x7f;
\r
981 if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode
\r
982 table<<=8; // Get sprite table address/2
\r
987 // updates: tilecode, sx
\r
988 for (u=0; u < 80 && (pack = *pd); u++, pd+=2)
\r
990 unsigned int *sprite;
\r
991 int code, code2, sx, sy, skip=0;
\r
993 sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite
\r
995 // parse sprite info
\r
998 code2 &= ~0xfe000000;
\r
999 code2 -= 0x00780000; // Get X coordinate + 8 in upper 16 bits
\r
1002 if((sx <= 8-((pack>>28)<<3) && sx >= -0x76) || sx >= 328) skip=1<<23;
\r
1003 else if ((sy = (pack<<16)>>16) < 240 && sy > -32) {
\r
1004 int sbl = (2<<(pack>>28))-1;
\r
1005 sblocks |= sbl<<(sy>>3);
\r
1008 *pd = (pack&~(1<<23))|skip;
\r
1011 // Find next sprite
\r
1012 link=(code>>16)&0x7f;
\r
1013 if(!link) break; // End of sprites
\r
1015 SpriteBlocks |= sblocks;
\r
1019 for (; u < 80; u++)
\r
1021 unsigned int *sprite;
\r
1022 int code, code2, sx, sy, hv, height, width, skip=0, sx_min;
\r
1024 sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite
\r
1026 // parse sprite info
\r
1028 sy = (code&0x1ff)-0x80;
\r
1029 hv = (code>>24)&0xf;
\r
1030 height = (hv&3)+1;
\r
1032 if (sy > 240 || sy + (height<<3) <= 0) skip|=1<<22; // sprite offscreen (completely, y)
\r
1034 width = (hv>>2)+1;
\r
1035 code2 = sprite[1];
\r
1036 sx = (code2>>16)&0x1ff;
\r
1037 sx -= 0x78; // Get X coordinate + 8
\r
1038 sx_min = 8-(width<<3);
\r
1040 if ((sx <= sx_min && sx >= -0x76) || sx >= 328) skip|=1<<23; // offscreen x
\r
1041 else if (sx > sx_min && !skip) {
\r
1042 int sbl = (2<<height)-1;
\r
1044 if(shi < 0) shi=0; // negative sy
\r
1045 sblocks |= sbl<<shi;
\r
1048 *pd++ = (width<<28)|(height<<24)|skip|(hv<<16)|((unsigned short)sy);
\r
1049 *pd++ = (sx<<16)|((unsigned short)code2);
\r
1051 // Find next sprite
\r
1052 link=(code>>16)&0x7f;
\r
1053 if(!link) break; // End of sprites
\r
1055 SpriteBlocks = sblocks;
\r
1056 *pd = 0; // terminate
\r
1060 static void DrawAllSprites(int *hcache, int maxwidth, int prio, int sh)
\r
1063 int sx1seen = 0; // sprite with x coord 1 or 0 seen
\r
1064 int ntiles = 0; // tile counter for sprite limit emulation
\r
1065 int *sprites[40]; // Sprites to draw in fast mode
\r
1066 int max_line_sprites = 20; // 20 sprites, 40 tiles
\r
1067 int *ps, pack, rs = rendstatus, scan = Scanline;
\r
1070 DrawAllSpritesInterlace(prio, maxwidth);
\r
1073 if (rs & (PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES)) {
\r
1074 //dprintf("PrepareSprites(%i) [%i]", (rs>>4)&1, scan);
\r
1075 PrepareSprites(rs & PDRAW_DIRTY_SPRITES);
\r
1076 rendstatus = rs & ~(PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES);
\r
1078 if (!(SpriteBlocks & (1<<(scan>>3)))) return;
\r
1080 if (((rs&PDRAW_ACC_SPRITES)||sh) && prio==0)
\r
1081 memset(HighSprZ, 0, 328);
\r
1082 if (!(rs&PDRAW_ACC_SPRITES)&&prio) {
\r
1083 if(hcache[0]) DrawSpritesFromCache(hcache, sh);
\r
1087 if (PicoOpt & POPT_DIS_SPRITE_LIM)
\r
1088 max_line_sprites = 80;
\r
1092 // Index + 0 : hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size
\r
1093 // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
\r
1095 for (i=u=n=0; (pack = *ps) && n < max_line_sprites; ps+=2, u++)
\r
1097 int sx, sy, row, pack2;
\r
1099 if (pack & 0x00400000) continue;
\r
1101 // get sprite info
\r
1104 sy = (pack <<16)>>16;
\r
1107 // elprintf(EL_ANOMALY, "x: %4i y: %4i p %i %ix%i", sx, sy, (pack2>>15)&1, (pack>>28)<<3, (pack>>21)&0x38);
\r
1109 if (sx == -0x77) sx1seen|=1; // for masking mode 2
\r
1111 // check if it is on this line
\r
1112 if (row < 0 || row >= ((pack>>21)&0x38)) continue; // no
\r
1114 // masking sprite?
\r
1115 if (sx == -0x78) {
\r
1116 if (n > 0) break; // masked
\r
1120 n++; // number of sprites on this line (both visible and hidden, except of x=0)
\r
1123 ntiles += pack>>28;
\r
1124 if (ntiles > max_line_sprites*2) break;
\r
1126 if (pack & 0x00800000) continue;
\r
1128 // accurate sprites
\r
1129 //dprintf("P:%i",((sx>>15)&1));
\r
1130 if (rs & PDRAW_ACC_SPRITES) {
\r
1131 // might need to skip this sprite
\r
1132 if ((pack2&0x8000) ^ (prio<<15)) continue;
\r
1133 DrawSpriteZ(pack, pack2, sh|(prio<<1), n^0xff);
\r
1137 // sprite is good, save it's pointer
\r
1141 // Go through sprites backwards:
\r
1142 if (!(rs & PDRAW_ACC_SPRITES)) {
\r
1143 for (i--; i>=0; i--)
\r
1144 DrawSprite(sprites[i],&hcache,sh);
\r
1146 // terminate cache list
\r
1152 // --------------------------------------------
\r
1154 #ifndef _ASM_DRAW_C
\r
1155 static void BackFill(int reg7, int sh)
\r
1157 unsigned int back;
\r
1159 // Start with a blank scanline (background colour):
\r
1165 memset32((int *)(HighCol+8), back, 320/4);
\r
1169 // --------------------------------------------
\r
1171 unsigned short HighPal[0x100];
\r
1173 #ifndef _ASM_DRAW_C
\r
1174 static void FinalizeLineBGR444(int sh)
\r
1176 unsigned short *pd=DrawLineDest;
\r
1177 unsigned char *ps=HighCol+8;
\r
1178 unsigned short *pal=Pico.cram;
\r
1181 if (Pico.video.reg[12]&1) {
\r
1184 if(!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32;
\r
1190 if(Pico.m.dirtyPal) {
\r
1191 blockcpy(pal, Pico.cram, 0x40*2);
\r
1192 // shadowed pixels
\r
1193 for(i = 0x3f; i >= 0; i--)
\r
1194 pal[0x40|i] = pal[0xc0|i] = (unsigned short)((pal[i]>>1)&0x0777);
\r
1195 // hilighted pixels
\r
1196 for(i = 0x3f; i >= 0; i--) {
\r
1197 t=pal[i]&0xeee;t+=0x444;if(t&0x10)t|=0xe;if(t&0x100)t|=0xe0;if(t&0x1000)t|=0xe00;t&=0xeee;
\r
1198 pal[0x80|i]=(unsigned short)t;
\r
1200 Pico.m.dirtyPal = 0;
\r
1204 for(i = 0; i < len; i++)
\r
1205 pd[i] = pal[ps[i]];
\r
1209 static void FinalizeLineRGB555(int sh)
\r
1211 unsigned short *pd=DrawLineDest;
\r
1212 unsigned char *ps=HighCol+8;
\r
1213 unsigned short *pal=HighPal;
\r
1214 int len, i, t, dirtyPal = Pico.m.dirtyPal;
\r
1218 unsigned int *spal=(void *)Pico.cram;
\r
1219 unsigned int *dpal=(void *)HighPal;
\r
1220 for (i = 0x3f/2; i >= 0; i--)
\r
1222 dpal[i] = ((spal[i]&0x000f000f)<< 1)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)<<4);
\r
1224 dpal[i] = ((spal[i]&0x000f000f)<<12)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)>>7);
\r
1226 Pico.m.dirtyPal = 0;
\r
1232 // shadowed pixels
\r
1233 for (i = 0x3f; i >= 0; i--)
\r
1234 pal[0x40|i] = pal[0xc0|i] = (unsigned short)((pal[i]>>1)&0x738e);
\r
1235 // hilighted pixels
\r
1236 for (i = 0x3f; i >= 0; i--) {
\r
1237 t=pal[i]&0xe71c;t+=0x4208;if(t&0x20)t|=0x1c;if(t&0x800)t|=0x700;if(t&0x10000)t|=0xe000;t&=0xe71c;
\r
1238 pal[0x80|i]=(unsigned short)t;
\r
1243 if (Pico.video.reg[12]&1) {
\r
1246 if (!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32;
\r
1251 for (i = 0; i < len; i++)
\r
1252 pd[i] = pal[ps[i]];
\r
1255 extern void amips_clut(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);
\r
1256 amips_clut(pd, ps, pal, len);
\r
1262 static void FinalizeLine8bit(int sh)
\r
1264 unsigned char *pd=DrawLineDest;
\r
1265 int len, rs = rendstatus;
\r
1266 static int dirty_count;
\r
1268 if (!sh && Pico.m.dirtyPal == 1 && Scanline < 222)
\r
1270 // a hack for mid-frame palette changes
\r
1271 if (!(rs & PDRAW_SONIC_MODE))
\r
1273 else dirty_count++;
\r
1274 rs |= PDRAW_SONIC_MODE;
\r
1276 if (dirty_count == 3) {
\r
1277 blockcpy(HighPal, Pico.cram, 0x40*2);
\r
1278 } else if (dirty_count == 11) {
\r
1279 blockcpy(HighPal+0x40, Pico.cram, 0x40*2);
\r
1283 if (Pico.video.reg[12]&1) {
\r
1286 if (!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32;
\r
1290 if (!sh && (rs & PDRAW_SONIC_MODE)) {
\r
1291 if (dirty_count >= 11) {
\r
1292 blockcpy_or(pd, HighCol+8, len, 0x80);
\r
1294 blockcpy_or(pd, HighCol+8, len, 0x40);
\r
1297 blockcpy(pd, HighCol+8, len);
\r
1301 static void (*FinalizeLine)(int sh) = FinalizeLineBGR444;
\r
1303 // hblank was enabled early during prev line processng -
\r
1304 // it should have been blanked
\r
1305 static void handle_early_blank(int scanline, int sh)
\r
1309 if (PicoScanBegin != NULL)
\r
1310 PicoScanBegin(scanline);
\r
1312 BackFill(Pico.video.reg[7], sh);
\r
1314 if (FinalizeLine != NULL)
\r
1317 if (PicoScanEnd != NULL)
\r
1318 PicoScanEnd(scanline);
\r
1321 // --------------------------------------------
\r
1323 static int DrawDisplay(int sh)
\r
1325 struct PicoVideo *pvid=&Pico.video;
\r
1326 int win=0,edge=0,hvwind=0;
\r
1327 int maxw, maxcells;
\r
1329 rendstatus &= ~(PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO);
\r
1331 if(pvid->reg[12]&1) {
\r
1332 maxw = 328; maxcells = 40;
\r
1334 maxw = 264; maxcells = 32;
\r
1337 // Find out if the window is on this line:
\r
1338 win=pvid->reg[0x12];
\r
1339 edge=(win&0x1f)<<3;
\r
1341 if (win&0x80) { if (Scanline>=edge) hvwind=1; }
\r
1342 else { if (Scanline< edge) hvwind=1; }
\r
1344 if (!hvwind) { // we might have a vertical window here
\r
1345 win=pvid->reg[0x11];
\r
1348 if (!edge) hvwind=1;
\r
1349 else if(edge < (maxcells>>1)) hvwind=2;
\r
1352 else if(edge < (maxcells>>1)) hvwind=2;
\r
1357 DrawLayer(1|(sh<<1), HighCacheB, 0, maxcells);
\r
1359 DrawWindow(0, maxcells>>1, 0, sh);
\r
1360 else if (hvwind == 2) {
\r
1361 // ahh, we have vertical window
\r
1362 DrawLayer(0|(sh<<1), HighCacheA, (win&0x80) ? 0 : edge<<1, (win&0x80) ? edge<<1 : maxcells);
\r
1363 DrawWindow( (win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 0, sh);
\r
1365 DrawLayer(0|(sh<<1), HighCacheA, 0, maxcells);
\r
1366 DrawAllSprites(HighCacheS, maxw, 0, sh);
\r
1368 if (HighCacheB[0]) DrawTilesFromCache(HighCacheB, sh, 328);
\r
1370 DrawWindow(0, maxcells>>1, 1, sh);
\r
1371 else if (hvwind == 2) {
\r
1372 if(HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, (win&0x80) ? edge<<4 : 328);
\r
1373 DrawWindow((win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 1, sh);
\r
1375 if (HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, 328);
\r
1376 DrawAllSprites(HighCacheS, maxw, 1, sh);
\r
1381 for (a = 0, c = HighCacheA; *c; c++, a++);
\r
1382 for (b = 0, c = HighCacheB; *c; c++, b++);
\r
1383 printf("%i:%03i: a=%i, b=%i\n", Pico.m.frame_count, Scanline, a, b);
\r
1391 PICO_INTERNAL void PicoFrameStart(void)
\r
1393 // prepare to do this frame
\r
1394 rendstatus = (PicoOpt&0x80)>>5; // accurate sprites, clear everything else
\r
1396 Pico.video.status &= ~0x0020;
\r
1397 else Pico.video.status |= 0x0020; // sprite collision
\r
1398 if ((Pico.video.reg[12]&6) == 6) rendstatus |= PDRAW_INTERLACE; // interlace mode
\r
1399 if (Pico.m.dirtyPal) Pico.m.dirtyPal = 2; // reset dirty if needed
\r
1401 PrepareSprites(1);
\r
1405 PICO_INTERNAL int PicoLine(int scan)
\r
1408 if (skip_next_line>0) { skip_next_line--; return 0; } // skip_next_line rendering lines
\r
1411 sh=(Pico.video.reg[0xC]&8)>>3; // shadow/hilight?
\r
1413 if (rendstatus & PDRAW_EARLY_BLANK) {
\r
1414 if (scan > 0) handle_early_blank(scan, sh);
\r
1415 rendstatus &= ~PDRAW_EARLY_BLANK;
\r
1418 if (PicoScanBegin != NULL)
\r
1419 skip_next_line = PicoScanBegin(scan);
\r
1422 BackFill(Pico.video.reg[7], sh);
\r
1423 if (Pico.video.reg[1]&0x40)
\r
1426 if (FinalizeLine != NULL)
\r
1429 if (PicoScanEnd != NULL)
\r
1430 PicoScanEnd(scan);
\r
1436 void PicoDrawSetColorFormat(int which)
\r
1440 case 2: FinalizeLine = FinalizeLine8bit; break;
\r
1441 case 1: FinalizeLine = FinalizeLineRGB555; break;
\r
1442 case 0: FinalizeLine = FinalizeLineBGR444; break;
\r
1443 default:FinalizeLine = NULL; break;
\r
1445 #if OVERRIDE_HIGHCOL
\r
1446 if (which) HighCol=DefHighCol;
\r