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