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