fixed some portability issues
[picodrive.git] / Pico / Draw.c
1 // This is part of Pico Library\r
2 \r
3 // (c) Copyright 2004 Dave, All rights reserved.\r
4 // (c) Copyright 2006 notaz, All rights reserved.\r
5 // Free for non-commercial use.\r
6 \r
7 // For commercial use, separate licencing terms must be obtained.\r
8 \r
9 \r
10 #include "PicoInt.h"\r
11 \r
12 int (*PicoScan)(unsigned int num, void *data)=NULL;\r
13 \r
14 #if OVERRIDE_HIGHCOL\r
15 static unsigned char DefHighCol[8+320+8];\r
16 unsigned char *HighCol=DefHighCol;\r
17 #else\r
18 unsigned char  HighCol[8+320+8];\r
19 #endif\r
20 unsigned short DefOutBuff[320*2];\r
21 void *DrawLineDest=DefOutBuff; // pointer to dest buffer where to draw this line to\r
22 \r
23 static int  HighCacheA[41+1];   // caches for high layers\r
24 static int  HighCacheB[41+1];\r
25 static int  HighCacheS[80+1];   // and sprites\r
26 static int  HighPreSpr[80*2+1]; // slightly preprocessed sprites\r
27 char HighSprZ[320+8+8]; // Z-buffer for accurate sprites and shadow/hilight mode\r
28                         // (if bit 7 == 0, sh caused by tile; if bit 6 == 0 pixel must be shadowed, else hilighted, if bit5 == 1)\r
29 // lsb->msb: moved sprites, not all window tiles use same priority, accurate sprites (copied from PicoOpt), interlace\r
30 //           dirty sprites, sonic mode, have layer with all hi prio tiles (mk3), layer sh/hi already processed\r
31 int rendstatus;\r
32 int Scanline=0; // Scanline\r
33 \r
34 static int SpriteBlocks;\r
35 //unsigned short ppt[] = { 0x0f11, 0x0ff1, 0x01f1, 0x011f, 0x01ff, 0x0f1f, 0x0f0e, 0x0e7c };\r
36 \r
37 struct TileStrip\r
38 {\r
39   int nametab; // Position in VRAM of name table (for this tile line)\r
40   int line;    // Line number in pixels 0x000-0x3ff within the virtual tilemap\r
41   int hscroll; // Horizontal scroll value in pixels for the line\r
42   int xmask;   // X-Mask (0x1f - 0x7f) for horizontal wraparound in the tilemap\r
43   int *hc;     // cache for high tile codes and their positions\r
44   int cells;   // cells (tiles) to draw (32 col mode doesn't need to update whole 320)\r
45 };\r
46 \r
47 // stuff available in asm:\r
48 #ifdef _ASM_DRAW_C\r
49 void DrawWindow(int tstart, int tend, int prio, int sh);\r
50 void BackFill(int reg7, int sh);\r
51 void DrawSprite(int *sprite, int **hc, int sh);\r
52 void DrawTilesFromCache(int *hc, int sh, int rlim);\r
53 void DrawSpritesFromCache(int *hc, int sh);\r
54 void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells);\r
55 void FinalizeLineBGR444(int sh);\r
56 void FinalizeLineRGB555(int sh);\r
57 void blockcpy_or(void *dst, void *src, size_t n, int pat);\r
58 #else\r
59 // utility\r
60 void blockcpy_or(void *dst, void *src, size_t n, int pat)\r
61 {\r
62   unsigned char *pd = dst, *ps = src;\r
63   for (; n; n--)\r
64     *pd++ = (unsigned char) (*ps++ | pat);\r
65 }\r
66 #endif\r
67 \r
68 \r
69 #ifdef _ASM_DRAW_C_AMIPS\r
70 int TileNorm(int sx,int addr,int pal);\r
71 int TileFlip(int sx,int addr,int pal);\r
72 #else\r
73 static int TileNorm(int sx,int addr,int pal)\r
74 {\r
75   unsigned char *pd = HighCol+sx;\r
76   unsigned int pack=0; unsigned int t=0;\r
77 \r
78   pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels\r
79   if (pack)\r
80   {\r
81     t=pack&0x0000f000; if (t) pd[0]=(unsigned char)(pal|(t>>12));\r
82     t=pack&0x00000f00; if (t) pd[1]=(unsigned char)(pal|(t>> 8));\r
83     t=pack&0x000000f0; if (t) pd[2]=(unsigned char)(pal|(t>> 4));\r
84     t=pack&0x0000000f; if (t) pd[3]=(unsigned char)(pal|(t    ));\r
85     t=pack&0xf0000000; if (t) pd[4]=(unsigned char)(pal|(t>>28));\r
86     t=pack&0x0f000000; if (t) pd[5]=(unsigned char)(pal|(t>>24));\r
87     t=pack&0x00f00000; if (t) pd[6]=(unsigned char)(pal|(t>>20));\r
88     t=pack&0x000f0000; if (t) pd[7]=(unsigned char)(pal|(t>>16));\r
89     return 0;\r
90   }\r
91 \r
92   return 1; // Tile blank\r
93 }\r
94 \r
95 static int TileFlip(int sx,int addr,int pal)\r
96 {\r
97   unsigned char *pd = HighCol+sx;\r
98   unsigned int pack=0; unsigned int t=0;\r
99 \r
100   pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels\r
101   if (pack)\r
102   {\r
103     t=pack&0x000f0000; if (t) pd[0]=(unsigned char)(pal|(t>>16));\r
104     t=pack&0x00f00000; if (t) pd[1]=(unsigned char)(pal|(t>>20));\r
105     t=pack&0x0f000000; if (t) pd[2]=(unsigned char)(pal|(t>>24));\r
106     t=pack&0xf0000000; if (t) pd[3]=(unsigned char)(pal|(t>>28));\r
107     t=pack&0x0000000f; if (t) pd[4]=(unsigned char)(pal|(t    ));\r
108     t=pack&0x000000f0; if (t) pd[5]=(unsigned char)(pal|(t>> 4));\r
109     t=pack&0x00000f00; if (t) pd[6]=(unsigned char)(pal|(t>> 8));\r
110     t=pack&0x0000f000; if (t) pd[7]=(unsigned char)(pal|(t>>12));\r
111     return 0;\r
112   }\r
113   return 1; // Tile blank\r
114 }\r
115 #endif\r
116 \r
117 // tile renderers for hacky operator sprite support\r
118 #define sh_pix(x) \\r
119   if(!t); \\r
120   else if(t==0xe) pd[x]=(unsigned char)((pd[x]&0x3f)|0x80); /* hilight */ \\r
121   else if(t==0xf) pd[x]=(unsigned char)((pd[x]&0x3f)|0xc0); /* shadow  */ \\r
122   else pd[x]=(unsigned char)(pal|t)\r
123 \r
124 #ifndef _ASM_DRAW_C\r
125 static int TileNormSH(int sx,int addr,int pal)\r
126 {\r
127   unsigned int pack=0; unsigned int t=0;\r
128   unsigned char *pd = HighCol+sx;\r
129 \r
130   pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels\r
131   if (pack)\r
132   {\r
133     t=(pack&0x0000f000)>>12; sh_pix(0);\r
134     t=(pack&0x00000f00)>> 8; sh_pix(1);\r
135     t=(pack&0x000000f0)>> 4; sh_pix(2);\r
136     t=(pack&0x0000000f)    ; sh_pix(3);\r
137     t=(pack&0xf0000000)>>28; sh_pix(4);\r
138     t=(pack&0x0f000000)>>24; sh_pix(5);\r
139     t=(pack&0x00f00000)>>20; sh_pix(6);\r
140     t=(pack&0x000f0000)>>16; sh_pix(7);\r
141     return 0;\r
142   }\r
143 \r
144   return 1; // Tile blank\r
145 }\r
146 \r
147 static int TileFlipSH(int sx,int addr,int pal)\r
148 {\r
149   unsigned int pack=0; unsigned int t=0;\r
150   unsigned char *pd = HighCol+sx;\r
151 \r
152   pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels\r
153   if (pack)\r
154   {\r
155     t=(pack&0x000f0000)>>16; sh_pix(0);\r
156     t=(pack&0x00f00000)>>20; sh_pix(1);\r
157     t=(pack&0x0f000000)>>24; sh_pix(2);\r
158     t=(pack&0xf0000000)>>28; sh_pix(3);\r
159     t=(pack&0x0000000f)    ; sh_pix(4);\r
160     t=(pack&0x000000f0)>> 4; sh_pix(5);\r
161     t=(pack&0x00000f00)>> 8; sh_pix(6);\r
162     t=(pack&0x0000f000)>>12; sh_pix(7);\r
163     return 0;\r
164   }\r
165   return 1; // Tile blank\r
166 }\r
167 #endif\r
168 \r
169 static int TileNormZ(int sx,int addr,int pal,int zval)\r
170 {\r
171   unsigned int pack=0; unsigned int t=0;\r
172   unsigned char *pd = HighCol+sx;\r
173   char *zb = HighSprZ+sx;\r
174   int collision = 0, zb_s;\r
175 \r
176   pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels\r
177   if (pack)\r
178   {\r
179     t=pack&0x0000f000; if(t) { zb_s=zb[0]; if(zb_s) collision=1; if(zval>zb_s) { pd[0]=(unsigned char)(pal|(t>>12)); zb[0]=(char)zval; } }\r
180     t=pack&0x00000f00; if(t) { zb_s=zb[1]; if(zb_s) collision=1; if(zval>zb_s) { pd[1]=(unsigned char)(pal|(t>> 8)); zb[1]=(char)zval; } }\r
181     t=pack&0x000000f0; if(t) { zb_s=zb[2]; if(zb_s) collision=1; if(zval>zb_s) { pd[2]=(unsigned char)(pal|(t>> 4)); zb[2]=(char)zval; } }\r
182     t=pack&0x0000000f; if(t) { zb_s=zb[3]; if(zb_s) collision=1; if(zval>zb_s) { pd[3]=(unsigned char)(pal|(t    )); zb[3]=(char)zval; } }\r
183     t=pack&0xf0000000; if(t) { zb_s=zb[4]; if(zb_s) collision=1; if(zval>zb_s) { pd[4]=(unsigned char)(pal|(t>>28)); zb[4]=(char)zval; } }\r
184     t=pack&0x0f000000; if(t) { zb_s=zb[5]; if(zb_s) collision=1; if(zval>zb_s) { pd[5]=(unsigned char)(pal|(t>>24)); zb[5]=(char)zval; } }\r
185     t=pack&0x00f00000; if(t) { zb_s=zb[6]; if(zb_s) collision=1; if(zval>zb_s) { pd[6]=(unsigned char)(pal|(t>>20)); zb[6]=(char)zval; } }\r
186     t=pack&0x000f0000; if(t) { zb_s=zb[7]; if(zb_s) collision=1; if(zval>zb_s) { pd[7]=(unsigned char)(pal|(t>>16)); zb[7]=(char)zval; } }\r
187     if(collision) Pico.video.status|=0x20;\r
188     return 0;\r
189   }\r
190 \r
191   return 1; // Tile blank\r
192 }\r
193 \r
194 static int TileFlipZ(int sx,int addr,int pal,int zval)\r
195 {\r
196   unsigned int pack=0; unsigned int t=0;\r
197   unsigned char *pd = HighCol+sx;\r
198   char *zb = HighSprZ+sx;\r
199   int collision = 0, zb_s;\r
200 \r
201   pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels\r
202   if (pack)\r
203   {\r
204     t=pack&0x000f0000; if(t) { zb_s=zb[0]&0x1f; if(zb_s) collision=1; if(zval>zb_s) { pd[0]=(unsigned char)(pal|(t>>16)); zb[0]=(char)zval; } }\r
205     t=pack&0x00f00000; if(t) { zb_s=zb[1]&0x1f; if(zb_s) collision=1; if(zval>zb_s) { pd[1]=(unsigned char)(pal|(t>>20)); zb[1]=(char)zval; } }\r
206     t=pack&0x0f000000; if(t) { zb_s=zb[2]&0x1f; if(zb_s) collision=1; if(zval>zb_s) { pd[2]=(unsigned char)(pal|(t>>24)); zb[2]=(char)zval; } }\r
207     t=pack&0xf0000000; if(t) { zb_s=zb[3]&0x1f; if(zb_s) collision=1; if(zval>zb_s) { pd[3]=(unsigned char)(pal|(t>>28)); zb[3]=(char)zval; } }\r
208     t=pack&0x0000000f; if(t) { zb_s=zb[4]&0x1f; if(zb_s) collision=1; if(zval>zb_s) { pd[4]=(unsigned char)(pal|(t    )); zb[4]=(char)zval; } }\r
209     t=pack&0x000000f0; if(t) { zb_s=zb[5]&0x1f; if(zb_s) collision=1; if(zval>zb_s) { pd[5]=(unsigned char)(pal|(t>> 4)); zb[5]=(char)zval; } }\r
210     t=pack&0x00000f00; if(t) { zb_s=zb[6]&0x1f; if(zb_s) collision=1; if(zval>zb_s) { pd[6]=(unsigned char)(pal|(t>> 8)); zb[6]=(char)zval; } }\r
211     t=pack&0x0000f000; if(t) { zb_s=zb[7]&0x1f; if(zb_s) collision=1; if(zval>zb_s) { pd[7]=(unsigned char)(pal|(t>>12)); zb[7]=(char)zval; } }\r
212     if(collision) Pico.video.status|=0x20;\r
213     return 0;\r
214   }\r
215   return 1; // Tile blank\r
216 }\r
217 \r
218 \r
219 #define sh_pixZ(x) \\r
220   if(t) { \\r
221     if(zb[x]) collision=1; \\r
222     if(zval>zb[x]) { \\r
223       if     (t==0xe) { pd[x]=(unsigned char)((pd[x]&0x3f)|0x80); /* hilight */ } \\r
224       else if(t==0xf) { pd[x]=(unsigned char)((pd[x]&0x3f)|0xc0); /* shadow  */ } \\r
225       else            { zb[x]=(char)zval; pd[x]=(unsigned char)(pal|t); } \\r
226     } \\r
227   }\r
228 \r
229 static int TileNormZSH(int sx,int addr,int pal,int zval)\r
230 {\r
231   unsigned int pack=0; unsigned int t=0;\r
232   unsigned char *pd = HighCol+sx;\r
233   char *zb = HighSprZ+sx;\r
234   int collision = 0;\r
235 \r
236   pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels\r
237   if (pack)\r
238   {\r
239     t=(pack&0x0000f000)>>12; sh_pixZ(0);\r
240     t=(pack&0x00000f00)>> 8; sh_pixZ(1);\r
241     t=(pack&0x000000f0)>> 4; sh_pixZ(2);\r
242     t=(pack&0x0000000f)    ; sh_pixZ(3);\r
243     t=(pack&0xf0000000)>>28; sh_pixZ(4);\r
244     t=(pack&0x0f000000)>>24; sh_pixZ(5);\r
245     t=(pack&0x00f00000)>>20; sh_pixZ(6);\r
246     t=(pack&0x000f0000)>>16; sh_pixZ(7);\r
247     if(collision) Pico.video.status|=0x20;\r
248     return 0;\r
249   }\r
250 \r
251   return 1; // Tile blank\r
252 }\r
253 \r
254 static int TileFlipZSH(int sx,int addr,int pal,int zval)\r
255 {\r
256   unsigned int pack=0; unsigned int t=0;\r
257   unsigned char *pd = HighCol+sx;\r
258   char *zb = HighSprZ+sx;\r
259   int collision = 0;\r
260 \r
261   pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels\r
262   if (pack)\r
263   {\r
264     t=(pack&0x000f0000)>>16; sh_pixZ(0);\r
265     t=(pack&0x00f00000)>>20; sh_pixZ(1);\r
266     t=(pack&0x0f000000)>>24; sh_pixZ(2);\r
267     t=(pack&0xf0000000)>>28; sh_pixZ(3);\r
268     t=(pack&0x0000000f)    ; sh_pixZ(4);\r
269     t=(pack&0x000000f0)>> 4; sh_pixZ(5);\r
270     t=(pack&0x00000f00)>> 8; sh_pixZ(6);\r
271     t=(pack&0x0000f000)>>12; sh_pixZ(7);\r
272     if(collision) Pico.video.status|=0x20;\r
273     return 0;\r
274   }\r
275   return 1; // Tile blank\r
276 }\r
277 \r
278 // --------------------------------------------\r
279 \r
280 #ifndef _ASM_DRAW_C\r
281 static void DrawStrip(struct TileStrip *ts, int plane_sh, int cellskip)\r
282 {\r
283   int tilex,dx,ty,code=0,addr=0,cells;\r
284   int oldcode=-1,blank=-1; // The tile we know is blank\r
285   int pal=0,sh;\r
286 \r
287   // Draw tiles across screen:\r
288   sh=(plane_sh<<5)&0x40;\r
289   tilex=((-ts->hscroll)>>3)+cellskip;\r
290   ty=(ts->line&7)<<1; // Y-Offset into tile\r
291   dx=((ts->hscroll-1)&7)+1;\r
292   cells = ts->cells - cellskip;\r
293   if(dx != 8) cells++; // have hscroll, need to draw 1 cell more\r
294   dx+=cellskip<<3;\r
295 \r
296   for (; cells > 0; dx+=8,tilex++,cells--)\r
297   {\r
298     int zero=0;\r
299 \r
300     code=Pico.vram[ts->nametab+(tilex&ts->xmask)];\r
301     if (code==blank) continue;\r
302     if (code>>15) { // high priority tile\r
303       int cval = code | (dx<<16) | (ty<<25);\r
304       if(code&0x1000) cval^=7<<26;\r
305       *ts->hc++ = cval; // cache it\r
306       continue;\r
307     }\r
308 \r
309     if (code!=oldcode) {\r
310       oldcode = code;\r
311       // Get tile address/2:\r
312       addr=(code&0x7ff)<<4;\r
313       addr+=ty;\r
314       if (code&0x1000) addr^=0xe; // Y-flip\r
315 \r
316       pal=((code>>9)&0x30)|sh;\r
317     }\r
318 \r
319     if (code&0x0800) zero=TileFlip(dx,addr,pal);\r
320     else             zero=TileNorm(dx,addr,pal);\r
321 \r
322     if (zero) blank=code; // We know this tile is blank now\r
323   }\r
324 \r
325   // terminate the cache list\r
326   *ts->hc = 0;\r
327   // if oldcode wasn't changed, it means all layer is hi priority\r
328   if (oldcode == -1) rendstatus|=0x40;\r
329 }\r
330 \r
331 // this is messy\r
332 void DrawStripVSRam(struct TileStrip *ts, int plane_sh, int cellskip)\r
333 {\r
334   int tilex,dx,code=0,addr=0,cell=0;\r
335   int oldcode=-1,blank=-1; // The tile we know is blank\r
336   int pal=0,scan=Scanline;\r
337 \r
338   // Draw tiles across screen:\r
339   tilex=(-ts->hscroll)>>3;\r
340   dx=((ts->hscroll-1)&7)+1;\r
341   if(dx != 8) cell--; // have hscroll, start with negative cell\r
342   cell+=cellskip;\r
343   tilex+=cellskip;\r
344   dx+=cellskip<<3;\r
345 \r
346   for (; cell < ts->cells; dx+=8,tilex++,cell++)\r
347   {\r
348     int zero=0,nametabadd,ty;\r
349 \r
350     //if((cell&1)==0)\r
351     {\r
352       int line,vscroll;\r
353       vscroll=Pico.vsram[(plane_sh&1)+(cell&~1)];\r
354 \r
355       // Find the line in the name table\r
356       line=(vscroll+scan)&ts->line&0xffff; // ts->line is really ymask ..\r
357       nametabadd=(line>>3)<<(ts->line>>24);    // .. and shift[width]\r
358       ty=(line&7)<<1; // Y-Offset into tile\r
359     }\r
360 \r
361     code=Pico.vram[ts->nametab+nametabadd+(tilex&ts->xmask)];\r
362     if (code==blank) continue;\r
363     if (code>>15) { // high priority tile\r
364       int cval = code | (dx<<16) | (ty<<25);\r
365       if(code&0x1000) cval^=7<<26;\r
366       *ts->hc++ = cval; // cache it\r
367       continue;\r
368     }\r
369 \r
370     if (code!=oldcode) {\r
371       oldcode = code;\r
372       // Get tile address/2:\r
373       addr=(code&0x7ff)<<4;\r
374       if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip\r
375 \r
376       pal=((code>>9)&0x30)|((plane_sh<<5)&0x40);\r
377     }\r
378 \r
379     if (code&0x0800) zero=TileFlip(dx,addr,pal);\r
380     else             zero=TileNorm(dx,addr,pal);\r
381 \r
382     if (zero) blank=code; // We know this tile is blank now\r
383   }\r
384 \r
385   // terminate the cache list\r
386   *ts->hc = 0;\r
387   if (oldcode == -1) rendstatus|=0x40;\r
388 }\r
389 #endif\r
390 \r
391 #ifndef _ASM_DRAW_C\r
392 static\r
393 #endif\r
394 void DrawStripInterlace(struct TileStrip *ts)\r
395 {\r
396   int tilex=0,dx=0,ty=0,code=0,addr=0,cells;\r
397   int oldcode=-1,blank=-1; // The tile we know is blank\r
398   int pal=0;\r
399 \r
400   // Draw tiles across screen:\r
401   tilex=(-ts->hscroll)>>3;\r
402   ty=(ts->line&15)<<1; // Y-Offset into tile\r
403   dx=((ts->hscroll-1)&7)+1;\r
404   cells = ts->cells;\r
405   if(dx != 8) cells++; // have hscroll, need to draw 1 cell more\r
406 \r
407   for (; cells; dx+=8,tilex++,cells--)\r
408   {\r
409     int zero=0;\r
410 \r
411     code=Pico.vram[ts->nametab+(tilex&ts->xmask)];\r
412     if (code==blank) continue;\r
413     if (code>>15) { // high priority tile\r
414       int cval = (code&0xfc00) | (dx<<16) | (ty<<25);\r
415       cval|=(code&0x3ff)<<1;\r
416       if(code&0x1000) cval^=0xf<<26;\r
417       *ts->hc++ = cval; // cache it\r
418       continue;\r
419     }\r
420 \r
421     if (code!=oldcode) {\r
422       oldcode = code;\r
423       // Get tile address/2:\r
424       addr=(code&0x7ff)<<5;\r
425       if (code&0x1000) addr+=30-ty; else addr+=ty; // Y-flip\r
426 \r
427 //      pal=Pico.cram+((code>>9)&0x30);\r
428       pal=((code>>9)&0x30);\r
429     }\r
430 \r
431     if (code&0x0800) zero=TileFlip(dx,addr,pal);\r
432     else             zero=TileNorm(dx,addr,pal);\r
433 \r
434     if (zero) blank=code; // We know this tile is blank now\r
435   }\r
436 \r
437   // terminate the cache list\r
438   *ts->hc = 0;\r
439 }\r
440 \r
441 // --------------------------------------------\r
442 \r
443 #ifndef _ASM_DRAW_C\r
444 static void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells)\r
445 {\r
446   struct PicoVideo *pvid=&Pico.video;\r
447   const char shift[4]={5,6,5,7}; // 32,64 or 128 sized tilemaps (2 is invalid)\r
448   struct TileStrip ts;\r
449   int width, height, ymask;\r
450   int vscroll, htab;\r
451 \r
452   ts.hc=hcache;\r
453   ts.cells=maxcells;\r
454 \r
455   // Work out the TileStrip to draw\r
456 \r
457   // Work out the name table size: 32 64 or 128 tiles (0-3)\r
458   width=pvid->reg[16];\r
459   height=(width>>4)&3; width&=3;\r
460 \r
461   ts.xmask=(1<<shift[width])-1; // X Mask in tiles (0x1f-0x7f)\r
462   ymask=(height<<8)|0xff;       // Y Mask in pixels\r
463   if(width == 1)   ymask&=0x1ff;\r
464   else if(width>1) ymask =0x0ff;\r
465 \r
466   // Find name table:\r
467   if (plane_sh&1) ts.nametab=(pvid->reg[4]&0x07)<<12; // B\r
468   else            ts.nametab=(pvid->reg[2]&0x38)<< 9; // A\r
469 \r
470   htab=pvid->reg[13]<<9; // Horizontal scroll table address\r
471   if ( pvid->reg[11]&2)     htab+=Scanline<<1; // Offset by line\r
472   if ((pvid->reg[11]&1)==0) htab&=~0xf; // Offset by tile\r
473   htab+=plane_sh&1; // A or B\r
474 \r
475   // Get horizontal scroll value, will be masked later\r
476   ts.hscroll=Pico.vram[htab&0x7fff];\r
477 \r
478   if((pvid->reg[12]&6) == 6) {\r
479     // interlace mode 2\r
480     vscroll=Pico.vsram[plane_sh&1]; // Get vertical scroll value\r
481 \r
482     // Find the line in the name table\r
483     ts.line=(vscroll+(Scanline<<1))&((ymask<<1)|1);\r
484     ts.nametab+=(ts.line>>4)<<shift[width];\r
485 \r
486     DrawStripInterlace(&ts);\r
487   } else if( pvid->reg[11]&4) {\r
488     // shit, we have 2-cell column based vscroll\r
489     // luckily this doesn't happen too often\r
490     ts.line=ymask|(shift[width]<<24); // save some stuff instead of line\r
491     DrawStripVSRam(&ts, plane_sh, cellskip);\r
492   } else {\r
493     vscroll=Pico.vsram[plane_sh&1]; // Get vertical scroll value\r
494 \r
495     // Find the line in the name table\r
496     ts.line=(vscroll+Scanline)&ymask;\r
497     ts.nametab+=(ts.line>>3)<<shift[width];\r
498 \r
499     DrawStrip(&ts, plane_sh, cellskip);\r
500   }\r
501 }\r
502 \r
503 \r
504 // --------------------------------------------\r
505 \r
506 // tstart & tend are tile pair numbers\r
507 static void DrawWindow(int tstart, int tend, int prio, int sh) // int *hcache\r
508 {\r
509   struct PicoVideo *pvid=&Pico.video;\r
510   int tilex=0,ty=0,nametab,code=0;\r
511   int blank=-1; // The tile we know is blank\r
512 \r
513   // Find name table line:\r
514   if (pvid->reg[12]&1)\r
515   {\r
516     nametab=(pvid->reg[3]&0x3c)<<9; // 40-cell mode\r
517     nametab+=(Scanline>>3)<<6;\r
518   }\r
519   else\r
520   {\r
521     nametab=(pvid->reg[3]&0x3e)<<9; // 32-cell mode\r
522     nametab+=(Scanline>>3)<<5;\r
523   }\r
524 \r
525   tilex=tstart<<1;\r
526   tend<<=1;\r
527 \r
528   ty=(Scanline&7)<<1; // Y-Offset into tile\r
529 \r
530   if(!(rendstatus&2)) {\r
531     // check the first tile code\r
532     code=Pico.vram[nametab+tilex];\r
533     // if the whole window uses same priority (what is often the case), we may be able to skip this field\r
534     if((code>>15) != prio) return;\r
535   }\r
536 \r
537   // Draw tiles across screen:\r
538   if (!sh)\r
539   {\r
540     for (; tilex < tend; tilex++)\r
541     {\r
542       int addr=0,zero=0;\r
543       int pal;\r
544 \r
545       code=Pico.vram[nametab+tilex];\r
546       if(code==blank) continue;\r
547       if((code>>15) != prio) {\r
548         rendstatus|=2;\r
549         continue;\r
550       }\r
551 \r
552       pal=((code>>9)&0x30);\r
553 \r
554       // Get tile address/2:\r
555       addr=(code&0x7ff)<<4;\r
556       if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip\r
557 \r
558       if (code&0x0800) zero=TileFlip(8+(tilex<<3),addr,pal);\r
559       else             zero=TileNorm(8+(tilex<<3),addr,pal);\r
560 \r
561       if (zero) blank=code; // We know this tile is blank now\r
562     }\r
563   }\r
564   else\r
565   {\r
566     for (; tilex < tend; tilex++)\r
567     {\r
568       int addr=0,zero=0;\r
569       int pal, tmp, *zb;\r
570 \r
571       code=Pico.vram[nametab+tilex];\r
572       if(code==blank) continue;\r
573       if((code>>15) != prio) {\r
574         rendstatus|=2;\r
575         continue;\r
576       }\r
577 \r
578       pal=((code>>9)&0x30);\r
579 \r
580       zb = (int *)(HighCol+8+(tilex<<3));\r
581       if(prio) {\r
582         tmp = *zb;\r
583         if(!(tmp&0x00000080)) tmp&=~0x000000c0; if(!(tmp&0x00008000)) tmp&=~0x0000c000;\r
584         if(!(tmp&0x00800000)) tmp&=~0x00c00000; if(!(tmp&0x80000000)) tmp&=~0xc0000000;\r
585         *zb++=tmp; tmp = *zb;\r
586         if(!(tmp&0x00000080)) tmp&=~0x000000c0; if(!(tmp&0x00008000)) tmp&=~0x0000c000;\r
587         if(!(tmp&0x00800000)) tmp&=~0x00c00000; if(!(tmp&0x80000000)) tmp&=~0xc0000000;\r
588         *zb++=tmp;\r
589       } else {\r
590         pal |= 0x40;\r
591       }\r
592 \r
593       // Get tile address/2:\r
594       addr=(code&0x7ff)<<4;\r
595       if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip\r
596 \r
597       if (code&0x0800) zero=TileFlip(8+(tilex<<3),addr,pal);\r
598       else             zero=TileNorm(8+(tilex<<3),addr,pal);\r
599 \r
600       if (zero) blank=code; // We know this tile is blank now\r
601     }\r
602   }\r
603 }\r
604 \r
605 // --------------------------------------------\r
606 \r
607 static void DrawTilesFromCacheShPrep(void)\r
608 {\r
609   if (!(rendstatus&0x80))\r
610   {\r
611     // as some layer has covered whole line with hi priority tiles,\r
612     // we can process whole line and then act as if sh/hi mode was off.\r
613     int c = 320/4, *zb = (int *)(HighCol+8);\r
614     rendstatus|=0x80;\r
615     while (c--)\r
616     {\r
617       int tmp = *zb;\r
618       if (!(tmp & 0x80808080)) *zb=tmp&0x3f3f3f3f;\r
619       else {\r
620         if(!(tmp&0x00000080)) tmp&=~0x000000c0; if(!(tmp&0x00008000)) tmp&=~0x0000c000;\r
621         if(!(tmp&0x00800000)) tmp&=~0x00c00000; if(!(tmp&0x80000000)) tmp&=~0xc0000000;\r
622         *zb=tmp;\r
623       }\r
624       zb++;\r
625     }\r
626   }\r
627 }\r
628 \r
629 static void DrawTilesFromCache(int *hc, int sh, int rlim)\r
630 {\r
631   int code, addr, dx;\r
632   int pal;\r
633 \r
634   // *ts->hc++ = code | (dx<<16) | (ty<<25); // cache it\r
635 \r
636   if (sh && (rendstatus&0xc0))\r
637   {\r
638     DrawTilesFromCacheShPrep();\r
639     sh = 0;\r
640   }\r
641 \r
642   if (!sh)\r
643   {\r
644     short blank=-1; // The tile we know is blank\r
645     while ((code=*hc++)) {\r
646       int zero;\r
647       if((short)code == blank) continue;\r
648       // Get tile address/2:\r
649       addr=(code&0x7ff)<<4;\r
650       addr+=(unsigned int)code>>25; // y offset into tile\r
651       dx=(code>>16)&0x1ff;\r
652 \r
653       pal=((code>>9)&0x30);\r
654       if (rlim-dx < 0) goto last_cut_tile;\r
655 \r
656       if (code&0x0800) zero=TileFlip(dx,addr,pal);\r
657       else             zero=TileNorm(dx,addr,pal);\r
658 \r
659       if (zero) blank=(short)code;\r
660     }\r
661   }\r
662   else\r
663   {\r
664     while ((code=*hc++)) {\r
665       unsigned char *zb;\r
666       // Get tile address/2:\r
667       addr=(code&0x7ff)<<4;\r
668       addr+=(unsigned int)code>>25; // y offset into tile\r
669       dx=(code>>16)&0x1ff;\r
670       zb = HighCol+dx;\r
671       if(!(*zb&0x80)) *zb&=0x3f; zb++; if(!(*zb&0x80)) *zb&=0x3f; zb++;\r
672       if(!(*zb&0x80)) *zb&=0x3f; zb++; if(!(*zb&0x80)) *zb&=0x3f; zb++;\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 \r
676       pal=((code>>9)&0x30);\r
677       if (rlim-dx < 0) goto last_cut_tile;\r
678 \r
679       if (code&0x0800) TileFlip(dx,addr,pal);\r
680       else             TileNorm(dx,addr,pal);\r
681     }\r
682   }\r
683   return;\r
684 \r
685 last_cut_tile:\r
686   {\r
687     unsigned int t, pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels\r
688     unsigned char *pd = HighCol+dx;\r
689     if (!pack) return;\r
690     if (code&0x0800)\r
691     {\r
692       switch (rlim-dx+8)\r
693       {\r
694         case 7: t=pack&0x00000f00; if (t) pd[6]=(unsigned char)(pal|(t>> 8)); // "break" is left out intentionally\r
695         case 6: t=pack&0x000000f0; if (t) pd[5]=(unsigned char)(pal|(t>> 4));\r
696         case 5: t=pack&0x0000000f; if (t) pd[4]=(unsigned char)(pal|(t    ));\r
697         case 4: t=pack&0xf0000000; if (t) pd[3]=(unsigned char)(pal|(t>>28));\r
698         case 3: t=pack&0x0f000000; if (t) pd[2]=(unsigned char)(pal|(t>>24));\r
699         case 2: t=pack&0x00f00000; if (t) pd[1]=(unsigned char)(pal|(t>>20));\r
700         case 1: t=pack&0x000f0000; if (t) pd[0]=(unsigned char)(pal|(t>>16));\r
701         default: break;\r
702       }\r
703     }\r
704     else\r
705     {\r
706       switch (rlim-dx+8)\r
707       {\r
708         case 7: t=pack&0x00f00000; if (t) pd[6]=(unsigned char)(pal|(t>>20));\r
709         case 6: t=pack&0x0f000000; if (t) pd[5]=(unsigned char)(pal|(t>>24));\r
710         case 5: t=pack&0xf0000000; if (t) pd[4]=(unsigned char)(pal|(t>>28));\r
711         case 4: t=pack&0x0000000f; if (t) pd[3]=(unsigned char)(pal|(t    ));\r
712         case 3: t=pack&0x000000f0; if (t) pd[2]=(unsigned char)(pal|(t>> 4));\r
713         case 2: t=pack&0x00000f00; if (t) pd[1]=(unsigned char)(pal|(t>> 8));\r
714         case 1: t=pack&0x0000f000; if (t) pd[0]=(unsigned char)(pal|(t>>12));\r
715         default: break;\r
716       }\r
717     }\r
718   }\r
719 }\r
720 \r
721 // --------------------------------------------\r
722 \r
723 // Index + 0  :    hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size\r
724 // Index + 4  :    xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8\r
725 \r
726 static void DrawSprite(int *sprite, int **hc, int sh)\r
727 {\r
728   int width=0,height=0;\r
729   int row=0,code=0;\r
730   int pal;\r
731   int tile=0,delta=0;\r
732   int sx, sy;\r
733   int (*fTileFunc)(int sx,int addr,int pal);\r
734 \r
735   // parse the sprite data\r
736   sy=sprite[0];\r
737   code=sprite[1];\r
738   sx=code>>16; // X\r
739   width=sy>>28;\r
740   height=(sy>>24)&7; // Width and height in tiles\r
741   sy=(sy<<16)>>16; // Y\r
742 \r
743   row=Scanline-sy; // Row of the sprite we are on\r
744 \r
745   if (code&0x1000) row=(height<<3)-1-row; // Flip Y\r
746 \r
747   tile=code&0x7ff; // Tile number\r
748   tile+=row>>3; // Tile number increases going down\r
749   delta=height; // Delta to increase tile by going right\r
750   if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X\r
751 \r
752   tile<<=4; tile+=(row&7)<<1; // Tile address\r
753 \r
754   if(code&0x8000) { // high priority - cache it\r
755     *(*hc)++ = (tile<<16)|((code&0x0800)<<5)|((sx<<6)&0x0000ffc0)|((code>>9)&0x30)|((sprite[0]>>16)&0xf);\r
756   } else {\r
757     delta<<=4; // Delta of address\r
758     pal=((code>>9)&0x30)|(sh<<6);\r
759 \r
760     if(sh && (code&0x6000) == 0x6000) {\r
761       if(code&0x0800) fTileFunc=TileFlipSH;\r
762       else            fTileFunc=TileNormSH;\r
763     } else {\r
764       if(code&0x0800) fTileFunc=TileFlip;\r
765       else            fTileFunc=TileNorm;\r
766     }\r
767 \r
768     for (; width; width--,sx+=8,tile+=delta)\r
769     {\r
770       if(sx<=0)   continue;\r
771       if(sx>=328) break; // Offscreen\r
772 \r
773       tile&=0x7fff; // Clip tile address\r
774       fTileFunc(sx,tile,pal);\r
775     }\r
776   }\r
777 }\r
778 #endif\r
779 \r
780 \r
781 // Index + 0  :    hhhhvvvv s---hhvv yyyyyyyy yyyyyyyy // s: skip flag, h: horiz. size\r
782 // Index + 4  :    xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8\r
783 \r
784 static void DrawSpriteZ(int pack, int pack2, int shpri, int sprio)\r
785 {\r
786   int width=0,height=0;\r
787   int row=0;\r
788   int pal;\r
789   int tile=0,delta=0;\r
790   int sx, sy;\r
791   int (*fTileFunc)(int sx,int addr,int pal,int zval);\r
792 \r
793   // parse the sprite data\r
794   sx=pack2>>16; // X\r
795   sy=(pack <<16)>>16; // Y\r
796   width=pack>>28;\r
797   height=(pack>>24)&7; // Width and height in tiles\r
798 \r
799   row=Scanline-sy; // Row of the sprite we are on\r
800 \r
801   if (pack2&0x1000) row=(height<<3)-1-row; // Flip Y\r
802 \r
803   tile=pack2&0x7ff; // Tile number\r
804   tile+=row>>3; // Tile number increases going down\r
805   delta=height; // Delta to increase tile by going right\r
806   if (pack2&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X\r
807 \r
808   tile<<=4; tile+=(row&7)<<1; // Tile address\r
809   delta<<=4; // Delta of address\r
810   pal=((pack2>>9)&0x30);\r
811   if((shpri&1)&&!(shpri&2)) pal|=0x40;\r
812 \r
813   shpri&=1;\r
814   if((pack2&0x6000) != 0x6000) shpri = 0;\r
815   shpri |= (pack2&0x0800)>>10;\r
816   switch(shpri) {\r
817     default:\r
818     case 0: fTileFunc=TileNormZ;   break;\r
819     case 1: fTileFunc=TileNormZSH; break;\r
820     case 2: fTileFunc=TileFlipZ;   break;\r
821     case 3: fTileFunc=TileFlipZSH; break;\r
822   }\r
823 \r
824   for (; width; width--,sx+=8,tile+=delta)\r
825   {\r
826     if(sx<=0)   continue;\r
827     if(sx>=328) break; // Offscreen\r
828 \r
829     tile&=0x7fff; // Clip tile address\r
830     fTileFunc(sx,tile,pal,sprio);\r
831   }\r
832 }\r
833 \r
834 static void DrawSpriteInterlace(unsigned int *sprite)\r
835 {\r
836   int width=0,height=0;\r
837   int row=0,code=0;\r
838   int pal;\r
839   int tile=0,delta=0;\r
840   int sx, sy;\r
841 \r
842   // parse the sprite data\r
843   sy=sprite[0];\r
844   height=sy>>24;\r
845   sy=(sy&0x3ff)-0x100; // Y\r
846   width=(height>>2)&3; height&=3;\r
847   width++; height++; // Width and height in tiles\r
848 \r
849   row=(Scanline<<1)-sy; // Row of the sprite we are on\r
850 \r
851   code=sprite[1];\r
852   sx=((code>>16)&0x1ff)-0x78; // X\r
853 \r
854   if (code&0x1000) row^=(16<<height)-1; // Flip Y\r
855 \r
856   tile=code&0x3ff; // Tile number\r
857   tile+=row>>4; // Tile number increases going down\r
858   delta=height; // Delta to increase tile by going right\r
859   if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X\r
860 \r
861   tile<<=5; tile+=(row&15)<<1; // Tile address\r
862 \r
863   delta<<=5; // Delta of address\r
864   pal=((code>>9)&0x30); // Get palette pointer\r
865 \r
866   for (; width; width--,sx+=8,tile+=delta)\r
867   {\r
868     if(sx<=0)   continue;\r
869     if(sx>=328) break; // Offscreen\r
870 \r
871     tile&=0x7fff; // Clip tile address\r
872     if (code&0x0800) TileFlip(sx,tile,pal);\r
873     else             TileNorm(sx,tile,pal);\r
874   }\r
875 }\r
876 \r
877 \r
878 static void DrawAllSpritesInterlace(int pri, int maxwidth)\r
879 {\r
880   struct PicoVideo *pvid=&Pico.video;\r
881   int i,u,table,link=0,sline=Scanline<<1;\r
882   unsigned int *sprites[80]; // Sprite index\r
883 \r
884   table=pvid->reg[5]&0x7f;\r
885   if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode\r
886   table<<=8; // Get sprite table address/2\r
887 \r
888   for (i=u=0; u < 80 && i < 21; u++)\r
889   {\r
890     unsigned int *sprite;\r
891     int code, sx, sy, height;\r
892 \r
893     sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
894 \r
895     // get sprite info\r
896     code = sprite[0];\r
897     sx = sprite[1];\r
898     if(((sx>>15)&1) != pri) goto nextsprite; // wrong priority sprite\r
899 \r
900     // check if it is on this line\r
901     sy = (code&0x3ff)-0x100;\r
902     height = (((code>>24)&3)+1)<<4;\r
903     if(sline < sy || sline >= sy+height) goto nextsprite; // no\r
904 \r
905     // check if sprite is not hidden offscreen\r
906     sx = (sx>>16)&0x1ff;\r
907     sx -= 0x78; // Get X coordinate + 8\r
908     if(sx <= -8*3 || sx >= maxwidth) goto nextsprite;\r
909 \r
910     // sprite is good, save it's pointer\r
911     sprites[i++]=sprite;\r
912 \r
913     nextsprite:\r
914     // Find next sprite\r
915     link=(code>>16)&0x7f;\r
916     if(!link) break; // End of sprites\r
917   }\r
918 \r
919   // Go through sprites backwards:\r
920   for (i-- ;i>=0; i--)\r
921     DrawSpriteInterlace(sprites[i]);\r
922 }\r
923 \r
924 \r
925 #ifndef _ASM_DRAW_C\r
926 static void DrawSpritesFromCache(int *hc, int sh)\r
927 {\r
928   int code, tile, sx, delta, width;\r
929   int pal;\r
930   int (*fTileFunc)(int sx,int addr,int pal);\r
931 \r
932   // *(*hc)++ = (tile<<16)|((code&0x0800)<<5)|((sx<<6)&0x0000ffc0)|((code>>9)&0x30)|((sprite[0]>>24)&0xf);\r
933 \r
934   while((code=*hc++)) {\r
935     pal=(code&0x30);\r
936     delta=code&0xf;\r
937     width=delta>>2; delta&=3;\r
938     width++; delta++; // Width and height in tiles\r
939     if (code&0x10000) delta=-delta; // Flip X\r
940     delta<<=4;\r
941     tile=((unsigned int)code>>17)<<1;\r
942     sx=(code<<16)>>22; // sx can be negative (start offscreen), so sign extend\r
943 \r
944     if(sh && pal == 0x30) { //\r
945       if(code&0x10000) fTileFunc=TileFlipSH;\r
946       else             fTileFunc=TileNormSH;\r
947     } else {\r
948       if(code&0x10000) fTileFunc=TileFlip;\r
949       else             fTileFunc=TileNorm;\r
950     }\r
951 \r
952     for (; width; width--,sx+=8,tile+=delta)\r
953     {\r
954       if(sx<=0)   continue;\r
955       if(sx>=328) break; // Offscreen\r
956 \r
957       tile&=0x7fff; // Clip tile address\r
958       fTileFunc(sx,tile,pal);\r
959     }\r
960   }\r
961 }\r
962 #endif\r
963 \r
964 \r
965 // Index + 0  :    ----hhvv -lllllll -------y yyyyyyyy\r
966 // Index + 4  :    -------x xxxxxxxx pccvhnnn nnnnnnnn\r
967 // v\r
968 // Index + 0  :    hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size\r
969 // Index + 4  :    xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8\r
970 \r
971 static void PrepareSprites(int full)\r
972 {\r
973   struct PicoVideo *pvid=&Pico.video;\r
974   int u=0,link=0,sblocks=0;\r
975   int table=0;\r
976   int *pd = HighPreSpr;\r
977 \r
978   table=pvid->reg[5]&0x7f;\r
979   if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode\r
980   table<<=8; // Get sprite table address/2\r
981 \r
982   if (!full)\r
983   {\r
984     int pack;\r
985     // updates: tilecode, sx\r
986     for (u=0; u < 80 && (pack = *pd); u++, pd+=2)\r
987     {\r
988       unsigned int *sprite;\r
989       int code, code2, sx, sy, skip=0;\r
990 \r
991       sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
992 \r
993       // parse sprite info\r
994       code  = sprite[0];\r
995       code2 = sprite[1];\r
996       code2 &= ~0xfe000000;\r
997       code2 -=  0x00780000; // Get X coordinate + 8 in upper 16 bits\r
998       sx = code2>>16;\r
999 \r
1000       if((sx <= 8-((pack>>28)<<3) && sx >= -0x76) || sx >= 328) skip=1<<23;\r
1001       else if ((sy = (pack<<16)>>16) < 240 && sy > -32) {\r
1002         int sbl = (2<<(pack>>28))-1;\r
1003         sblocks |= sbl<<(sy>>3);\r
1004       }\r
1005 \r
1006       *pd = (pack&~(1<<23))|skip;\r
1007       *(pd+1) = code2;\r
1008 \r
1009       // Find next sprite\r
1010       link=(code>>16)&0x7f;\r
1011       if(!link) break; // End of sprites\r
1012     }\r
1013     SpriteBlocks |= sblocks;\r
1014   }\r
1015   else\r
1016   {\r
1017     for (; u < 80; u++)\r
1018     {\r
1019       unsigned int *sprite;\r
1020       int code, code2, sx, sy, hv, height, width, skip=0, sx_min;\r
1021 \r
1022       sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
1023 \r
1024       // parse sprite info\r
1025       code = sprite[0];\r
1026       sy = (code&0x1ff)-0x80;\r
1027       hv = (code>>24)&0xf;\r
1028       height = (hv&3)+1;\r
1029 \r
1030       if(sy > 240 || sy + (height<<3) <= 0) skip|=1<<22;\r
1031 \r
1032       width  = (hv>>2)+1;\r
1033       code2 = sprite[1];\r
1034       sx = (code2>>16)&0x1ff;\r
1035       sx -= 0x78; // Get X coordinate + 8\r
1036       sx_min = 8-(width<<3);\r
1037 \r
1038       if((sx <= sx_min && sx >= -0x76) || sx >= 328) skip|=1<<23;\r
1039       else if (sx > sx_min && !skip) {\r
1040         int sbl = (2<<height)-1;\r
1041         int shi = sy>>3;\r
1042         if(shi < 0) shi=0; // negative sy\r
1043         sblocks |= sbl<<shi;\r
1044       }\r
1045 \r
1046       *pd++ = (width<<28)|(height<<24)|skip|(hv<<16)|((unsigned short)sy);\r
1047       *pd++ = (sx<<16)|((unsigned short)code2);\r
1048 \r
1049       // Find next sprite\r
1050       link=(code>>16)&0x7f;\r
1051       if(!link) break; // End of sprites\r
1052     }\r
1053     SpriteBlocks = sblocks;\r
1054     *pd = 0; // terminate\r
1055   }\r
1056 }\r
1057 \r
1058 static void DrawAllSprites(int *hcache, int maxwidth, int prio, int sh)\r
1059 {\r
1060   int i,u,n;\r
1061   int sx1seen=0; // sprite with x coord 1 or 0 seen\r
1062   int ntiles = 0; // tile counter for sprite limit emulation\r
1063   int *sprites[40]; // Sprites to draw in fast mode\r
1064   int *ps, pack, rs = rendstatus, scan=Scanline;\r
1065 \r
1066   if(rs&8) {\r
1067     DrawAllSpritesInterlace(prio, maxwidth);\r
1068     return;\r
1069   }\r
1070   if(rs&0x11) {\r
1071     //dprintf("PrepareSprites(%i) [%i]", (rs>>4)&1, scan);\r
1072     PrepareSprites(rs&0x10);\r
1073     rendstatus=rs&~0x11;\r
1074   }\r
1075   if (!(SpriteBlocks & (1<<(scan>>3)))) return;\r
1076 \r
1077   if(((rs&4)||sh)&&prio==0)\r
1078     memset(HighSprZ, 0, 328);\r
1079   if(!(rs&4)&&prio) {\r
1080     if(hcache[0]) DrawSpritesFromCache(hcache, sh);\r
1081     return;\r
1082   }\r
1083 \r
1084   ps = HighPreSpr;\r
1085 \r
1086   // Index + 0  :    hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size\r
1087   // Index + 4  :    xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8\r
1088 \r
1089   for(i=u=n=0; (pack = *ps) && n < 20; ps+=2, u++)\r
1090   {\r
1091     int sx, sy, row, pack2;\r
1092 \r
1093     if(pack & 0x00400000) continue;\r
1094 \r
1095     // get sprite info\r
1096     pack2 = *(ps+1);\r
1097     sx =  pack2>>16;\r
1098     sy = (pack <<16)>>16;\r
1099     row = scan-sy;\r
1100 \r
1101     //dprintf("x: %i y: %i %ix%i", sx, sy, (pack>>28)<<3, (pack>>21)&0x38);\r
1102 \r
1103     if(sx == -0x77) sx1seen|=1; // for masking mode 2\r
1104 \r
1105     // check if it is on this line\r
1106     if(row < 0 || row >= ((pack>>21)&0x38)) continue; // no\r
1107     n++; // number of sprites on this line (both visible and hidden, max is 20) [broken]\r
1108 \r
1109     // sprite limit\r
1110     ntiles += pack>>28;\r
1111     if(ntiles > 40) break;\r
1112 \r
1113     if(pack & 0x00800000) continue;\r
1114 \r
1115     // masking sprite?\r
1116     if(sx == -0x78) {\r
1117       if(!(sx1seen&1) || sx1seen==3) {\r
1118         break; // this sprite is not drawn and remaining sprites are masked\r
1119       }\r
1120       if((sx1seen>>8) == 0) sx1seen=(i+1)<<8;\r
1121       continue;\r
1122     }\r
1123     else if(sx == -0x77) {\r
1124       // masking mode2 (Outrun, Galaxy Force II, Shadow of the beast)\r
1125       if(sx1seen>>8) { i=(sx1seen>>8)-1; break; } // seen both 0 and 1\r
1126       sx1seen |= 2;\r
1127       continue;\r
1128     }\r
1129 \r
1130     // accurate sprites\r
1131     //dprintf("P:%i",((sx>>15)&1));\r
1132     if(rs&4) {\r
1133       // might need to skip this sprite\r
1134       if((pack2&0x8000) ^ (prio<<15)) continue;\r
1135       DrawSpriteZ(pack,pack2,sh|(prio<<1),(char)(0x1f-n));\r
1136       continue;\r
1137     }\r
1138 \r
1139     // sprite is good, save it's pointer\r
1140     sprites[i++]=ps;\r
1141   }\r
1142 \r
1143   // Go through sprites backwards:\r
1144   if(!(rs&4)) {\r
1145     for (i--; i>=0; i--)\r
1146       DrawSprite(sprites[i],&hcache,sh);\r
1147 \r
1148     // terminate cache list\r
1149     *hcache = 0;\r
1150   }\r
1151 }\r
1152 \r
1153 \r
1154 // --------------------------------------------\r
1155 \r
1156 #ifndef _ASM_DRAW_C\r
1157 static void BackFill(int reg7, int sh)\r
1158 {\r
1159   unsigned int back;\r
1160 \r
1161   // Start with a blank scanline (background colour):\r
1162   back=reg7&0x3f;\r
1163   back|=sh<<6;\r
1164   back|=back<<8;\r
1165   back|=back<<16;\r
1166 \r
1167   memset32((int *)(HighCol+8), back, 320/4);\r
1168 }\r
1169 #endif\r
1170 \r
1171 // --------------------------------------------\r
1172 \r
1173 unsigned short HighPal[0x100];\r
1174 \r
1175 #ifndef _ASM_DRAW_C\r
1176 static void FinalizeLineBGR444(int sh)\r
1177 {\r
1178   unsigned short *pd=DrawLineDest;\r
1179   unsigned char  *ps=HighCol+8;\r
1180   unsigned short *pal=Pico.cram;\r
1181   int len, i, t;\r
1182 \r
1183   if (Pico.video.reg[12]&1) {\r
1184     len = 320;\r
1185   } else {\r
1186     if(!(PicoOpt&0x100)) pd+=32;\r
1187     len = 256;\r
1188   }\r
1189 \r
1190   if(sh) {\r
1191     pal=HighPal;\r
1192     if(Pico.m.dirtyPal) {\r
1193       blockcpy(pal, Pico.cram, 0x40*2);\r
1194       // shadowed pixels\r
1195       for(i = 0x3f; i >= 0; i--)\r
1196         pal[0x40|i] = pal[0xc0|i] = (unsigned short)((pal[i]>>1)&0x0777);\r
1197       // hilighted pixels\r
1198       for(i = 0x3f; i >= 0; i--) {\r
1199         t=pal[i]&0xeee;t+=0x444;if(t&0x10)t|=0xe;if(t&0x100)t|=0xe0;if(t&0x1000)t|=0xe00;t&=0xeee;\r
1200         pal[0x80|i]=(unsigned short)t;\r
1201       }\r
1202       Pico.m.dirtyPal = 0;\r
1203     }\r
1204   }\r
1205 \r
1206   for(i = 0; i < len; i++)\r
1207     pd[i] = pal[ps[i]];\r
1208 }\r
1209 \r
1210 \r
1211 static void FinalizeLineRGB555(int sh)\r
1212 {\r
1213   unsigned short *pd=DrawLineDest;\r
1214   unsigned char  *ps=HighCol+8;\r
1215   unsigned short *pal=HighPal;\r
1216   int len, i, t, dirtyPal = Pico.m.dirtyPal;\r
1217 \r
1218   if (dirtyPal)\r
1219   {\r
1220     unsigned int *spal=(void *)Pico.cram;\r
1221     unsigned int *dpal=(void *)HighPal;\r
1222     for (i = 0x3f/2; i >= 0; i--)\r
1223 #ifdef USE_BGR555\r
1224       dpal[i] = ((spal[i]&0x000f000f)<< 1)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)<<4);\r
1225 #else\r
1226       dpal[i] = ((spal[i]&0x000f000f)<<12)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)>>7);\r
1227 #endif\r
1228     Pico.m.dirtyPal = 0;\r
1229   }\r
1230 \r
1231   if (sh)\r
1232   {\r
1233     if (dirtyPal) {\r
1234       // shadowed pixels\r
1235       for (i = 0x3f; i >= 0; i--)\r
1236         pal[0x40|i] = pal[0xc0|i] = (unsigned short)((pal[i]>>1)&0x738e);\r
1237       // hilighted pixels\r
1238       for (i = 0x3f; i >= 0; i--) {\r
1239         t=pal[i]&0xe71c;t+=0x4208;if(t&0x20)t|=0x1c;if(t&0x800)t|=0x700;if(t&0x10000)t|=0xe000;t&=0xe71c;\r
1240         pal[0x80|i]=(unsigned short)t;\r
1241       }\r
1242     }\r
1243   }\r
1244 \r
1245   if (Pico.video.reg[12]&1) {\r
1246     len = 320;\r
1247   } else {\r
1248     if (!(PicoOpt&0x100)) pd+=32;\r
1249     len = 256;\r
1250   }\r
1251 \r
1252 #ifndef PSP\r
1253   for (i = 0; i < len; i++)\r
1254     pd[i] = pal[ps[i]];\r
1255 #else\r
1256   {\r
1257     extern void amips_clut(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);\r
1258     amips_clut(pd, ps, pal, len);\r
1259   }\r
1260 #endif\r
1261 }\r
1262 #endif\r
1263 \r
1264 static void FinalizeLine8bit(int sh)\r
1265 {\r
1266   unsigned char *pd=DrawLineDest;\r
1267   int len, rs = rendstatus;\r
1268   static int dirty_count;\r
1269 \r
1270   if (!sh && Pico.m.dirtyPal == 1 && Scanline < 222) {\r
1271     // a hack for mid-frame palette changes\r
1272     if (!(rs & 0x20))\r
1273          dirty_count = 1;\r
1274     else dirty_count++;\r
1275     rs |= 0x20;\r
1276     rendstatus = rs;\r
1277     if (dirty_count == 3) {\r
1278       blockcpy(HighPal, Pico.cram, 0x40*2);\r
1279     } else if (dirty_count == 11) {\r
1280       blockcpy(HighPal+0x40, Pico.cram, 0x40*2);\r
1281     }\r
1282   }\r
1283 \r
1284   if (Pico.video.reg[12]&1) {\r
1285     len = 320;\r
1286   } else {\r
1287     if(!(PicoOpt&0x100)) pd+=32;\r
1288     len = 256;\r
1289   }\r
1290 \r
1291   if (!sh && rs & 0x20) {\r
1292     if (dirty_count >= 11) {\r
1293       blockcpy_or(pd, HighCol+8, len, 0x80);\r
1294     } else {\r
1295       blockcpy_or(pd, HighCol+8, len, 0x40);\r
1296     }\r
1297   } else {\r
1298     blockcpy(pd, HighCol+8, len);\r
1299   }\r
1300 }\r
1301 \r
1302 static void (*FinalizeLine)(int sh) = FinalizeLineBGR444;\r
1303 \r
1304 // --------------------------------------------\r
1305 \r
1306 static int DrawDisplay(int sh)\r
1307 {\r
1308   struct PicoVideo *pvid=&Pico.video;\r
1309   int win=0,edge=0,hvwind=0;\r
1310   int maxw, maxcells;\r
1311 \r
1312   rendstatus&=~0xc0;\r
1313 \r
1314   if(pvid->reg[12]&1) {\r
1315     maxw = 328; maxcells = 40;\r
1316   } else {\r
1317     maxw = 264; maxcells = 32;\r
1318   }\r
1319 \r
1320   // Find out if the window is on this line:\r
1321   win=pvid->reg[0x12];\r
1322   edge=(win&0x1f)<<3;\r
1323 \r
1324   if (win&0x80) { if (Scanline>=edge) hvwind=1; }\r
1325   else          { if (Scanline< edge) hvwind=1; }\r
1326 \r
1327   if (!hvwind) { // we might have a vertical window here\r
1328     win=pvid->reg[0x11];\r
1329     edge=win&0x1f;\r
1330     if (win&0x80) {\r
1331       if (!edge) hvwind=1;\r
1332       else if(edge < (maxcells>>1)) hvwind=2;\r
1333     } else {\r
1334       if (!edge);\r
1335       else if(edge < (maxcells>>1)) hvwind=2;\r
1336       else hvwind=1;\r
1337     }\r
1338   }\r
1339 \r
1340   DrawLayer(1|(sh<<1), HighCacheB, 0, maxcells);\r
1341   if (hvwind == 1)\r
1342     DrawWindow(0, maxcells>>1, 0, sh);\r
1343   else if (hvwind == 2) {\r
1344     // ahh, we have vertical window\r
1345     DrawLayer(0|(sh<<1), HighCacheA, (win&0x80) ?    0 : edge<<1, (win&0x80) ?     edge<<1 : maxcells);\r
1346     DrawWindow(                      (win&0x80) ? edge :       0, (win&0x80) ? maxcells>>1 : edge, 0, sh);\r
1347   } else\r
1348     DrawLayer(0|(sh<<1), HighCacheA, 0, maxcells);\r
1349   DrawAllSprites(HighCacheS, maxw, 0, sh);\r
1350 \r
1351   if (HighCacheB[0]) DrawTilesFromCache(HighCacheB, sh, 328);\r
1352   if (hvwind == 1)\r
1353     DrawWindow(0, maxcells>>1, 1, sh);\r
1354   else if (hvwind == 2) {\r
1355     if(HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, (win&0x80) ? edge<<4 : 328);\r
1356     DrawWindow((win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 1, sh);\r
1357   } else\r
1358     if (HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, 328);\r
1359   DrawAllSprites(HighCacheS, maxw, 1, sh);\r
1360 \r
1361 #if 0\r
1362   {\r
1363     int *c, a, b;\r
1364     for (a = 0, c = HighCacheA; *c; c++, a++);\r
1365     for (b = 0, c = HighCacheB; *c; c++, b++);\r
1366     printf("%i:%03i: a=%i, b=%i\n", Pico.m.frame_count, Scanline, a, b);\r
1367   }\r
1368 #endif\r
1369 \r
1370   return 0;\r
1371 }\r
1372 \r
1373 \r
1374 static int Skip=0;\r
1375 \r
1376 PICO_INTERNAL void PicoFrameStart(void)\r
1377 {\r
1378   // prepare to do this frame\r
1379   rendstatus = (PicoOpt&0x80)>>5;    // accurate sprites\r
1380   if(rendstatus)\r
1381        Pico.video.status &= ~0x0020;\r
1382   else Pico.video.status |=  0x0020; // sprite collision\r
1383   if((Pico.video.reg[12]&6) == 6) rendstatus |= 8; // interlace mode\r
1384   if(Pico.m.dirtyPal) Pico.m.dirtyPal = 2; // reset dirty if needed\r
1385 \r
1386   PrepareSprites(1);\r
1387   Skip=0;\r
1388 }\r
1389 \r
1390 PICO_INTERNAL int PicoLine(int scan)\r
1391 {\r
1392   int sh;\r
1393   if (Skip>0) { Skip--; return 0; } // Skip rendering lines\r
1394 \r
1395   Scanline=scan;\r
1396   sh=(Pico.video.reg[0xC]&8)>>3; // shadow/hilight?\r
1397 \r
1398   // Draw screen:\r
1399   BackFill(Pico.video.reg[7], sh);\r
1400   if (Pico.video.reg[1]&0x40)\r
1401     DrawDisplay(sh);\r
1402 \r
1403   if (FinalizeLine != NULL)\r
1404     FinalizeLine(sh);\r
1405 \r
1406   Skip=PicoScan(Scanline,DrawLineDest);\r
1407 \r
1408   return 0;\r
1409 }\r
1410 \r
1411 \r
1412 void PicoDrawSetColorFormat(int which)\r
1413 {\r
1414   switch (which)\r
1415   {\r
1416     case 2: FinalizeLine = FinalizeLine8bit;   break;\r
1417     case 1: FinalizeLine = FinalizeLineRGB555; break;\r
1418     case 0: FinalizeLine = FinalizeLineBGR444; break;\r
1419     default:FinalizeLine = NULL; break;\r
1420   }\r
1421 #if OVERRIDE_HIGHCOL\r
1422   if (which) HighCol=DefHighCol;\r
1423 #endif\r
1424 }\r
1425 \r