amalgamation
[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, all window tiles don't use same priority, accurate sprites (copied from PicoOpt), interlace\r
26 //           dirty sprites, sonic mode\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);\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 }\r
320 \r
321 // this is messy\r
322 void DrawStripVSRam(struct TileStrip *ts, int plane)\r
323 {\r
324   int tilex=0,dx=0,ty=0,code=0,addr=0,cell=0,nametabadd=0;\r
325   int oldcode=-1,blank=-1; // The tile we know is blank\r
326   int pal=0,scan=Scanline;\r
327 \r
328   // Draw tiles across screen:\r
329   tilex=(-ts->hscroll)>>3;\r
330   dx=((ts->hscroll-1)&7)+1;\r
331   if(dx != 8) {\r
332     int vscroll, line;\r
333     cell--; // have hscroll, start with negative cell\r
334     // also calculate intial VS stuff\r
335     vscroll=Pico.vsram[plane];\r
336 \r
337     // Find the line in the name table\r
338     line=(vscroll+scan)&ts->line&0xffff; // ts->line is really ymask ..\r
339     nametabadd=(line>>3)<<(ts->line>>24);    // .. and shift[width]\r
340     ty=(line&7)<<1; // Y-Offset into tile\r
341   }\r
342 \r
343   for (; cell < ts->cells; dx+=8,tilex++,cell++)\r
344   {\r
345     int zero=0;\r
346 \r
347     if((cell&1)==0) {\r
348       int line,vscroll;\r
349       vscroll=Pico.vsram[plane+(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=Pico.cram+((code>>9)&0x30);\r
373       pal=((code>>9)&0x30);\r
374     }\r
375 \r
376     if (code&0x0800) zero=TileFlip(dx,addr,pal);\r
377     else             zero=TileNorm(dx,addr,pal);\r
378 \r
379     if (zero) blank=code; // We know this tile is blank now\r
380   }\r
381 \r
382   // terminate the cache list\r
383   *ts->hc = 0;\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, int *hcache, int maxcells, int sh)\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==0) ts.nametab=(pvid->reg[2]&0x38)<< 9; // A\r
464   else          ts.nametab=(pvid->reg[4]&0x07)<<12; // B\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; // 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]; // 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);\r
488   } else {\r
489     vscroll=Pico.vsram[plane]; // 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, sh);\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)\r
580 {\r
581   int code, addr, zero, dx;\r
582   int pal;\r
583   short blank=-1; // The tile we know is blank\r
584 \r
585   // *ts->hc++ = code | (dx<<16) | (ty<<25); // cache it\r
586 \r
587   while((code=*hc++)) {\r
588     if(!sh && (short)code == blank) continue;\r
589 \r
590     // Get tile address/2:\r
591     addr=(code&0x7ff)<<4;\r
592     addr+=(unsigned int)code>>25; // y offset into tile\r
593     dx=(code>>16)&0x1ff;\r
594     if(sh) {\r
595       unsigned char *zb = HighCol+dx;\r
596       if(!(*zb&0x80)) *zb&=0x3f; zb++; if(!(*zb&0x80)) *zb&=0x3f; zb++;\r
597       if(!(*zb&0x80)) *zb&=0x3f; zb++; if(!(*zb&0x80)) *zb&=0x3f; zb++;\r
598       if(!(*zb&0x80)) *zb&=0x3f; zb++; if(!(*zb&0x80)) *zb&=0x3f; zb++;\r
599       if(!(*zb&0x80)) *zb&=0x3f; zb++; if(!(*zb&0x80)) *zb&=0x3f; zb++;\r
600     }\r
601 \r
602     pal=((code>>9)&0x30);\r
603 \r
604     if (code&0x0800) zero=TileFlip(dx,addr,pal);\r
605     else             zero=TileNorm(dx,addr,pal);\r
606 \r
607     if(zero) blank=(short)code;\r
608   }\r
609 }\r
610 \r
611 // --------------------------------------------\r
612 \r
613 // Index + 0  :    hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size\r
614 // Index + 4  :    xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8\r
615 \r
616 static void DrawSprite(int *sprite, int **hc, int sh)\r
617 {\r
618   int width=0,height=0;\r
619   int row=0,code=0;\r
620   int pal;\r
621   int tile=0,delta=0;\r
622   int sx, sy;\r
623   int (*fTileFunc)(int sx,int addr,int pal);\r
624 \r
625   // parse the sprite data\r
626   sy=sprite[0];\r
627   code=sprite[1];\r
628   sx=code>>16; // X\r
629   width=sy>>28;\r
630   height=(sy>>24)&7; // Width and height in tiles\r
631   sy=(sy<<16)>>16; // Y\r
632 \r
633   row=Scanline-sy; // Row of the sprite we are on\r
634 \r
635   if (code&0x1000) row=(height<<3)-1-row; // Flip Y\r
636 \r
637   tile=code&0x7ff; // Tile number\r
638   tile+=row>>3; // Tile number increases going down\r
639   delta=height; // Delta to increase tile by going right\r
640   if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X\r
641 \r
642   tile<<=4; tile+=(row&7)<<1; // Tile address\r
643 \r
644   if(code&0x8000) { // high priority - cache it\r
645     *(*hc)++ = (tile<<16)|((code&0x0800)<<5)|((sx<<6)&0x0000ffc0)|((code>>9)&0x30)|((sprite[0]>>16)&0xf);\r
646   } else {\r
647     delta<<=4; // Delta of address\r
648     pal=((code>>9)&0x30)|(sh<<6);\r
649 \r
650     if(sh && (code&0x6000) == 0x6000) {\r
651       if(code&0x0800) fTileFunc=TileFlipSH;\r
652       else            fTileFunc=TileNormSH;\r
653     } else {\r
654       if(code&0x0800) fTileFunc=TileFlip;\r
655       else            fTileFunc=TileNorm;\r
656     }\r
657 \r
658     for (; width; width--,sx+=8,tile+=delta)\r
659     {\r
660       if(sx<=0)   continue;\r
661       if(sx>=328) break; // Offscreen\r
662 \r
663       tile&=0x7fff; // Clip tile address\r
664       fTileFunc(sx,tile,pal);\r
665     }\r
666   }\r
667 }\r
668 #endif\r
669 \r
670 \r
671 // Index + 0  :    hhhhvvvv s---hhvv yyyyyyyy yyyyyyyy // s: skip flag, h: horiz. size\r
672 // Index + 4  :    xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8\r
673 \r
674 static void DrawSpriteZ(int pack, int pack2, int shpri, int sprio)\r
675 {\r
676   int width=0,height=0;\r
677   int row=0;\r
678   int pal;\r
679   int tile=0,delta=0;\r
680   int sx, sy;\r
681   int (*fTileFunc)(int sx,int addr,int pal,int zval);\r
682 \r
683   // parse the sprite data\r
684   sx=pack2>>16; // X\r
685   sy=(pack <<16)>>16; // Y\r
686   width=pack>>28;\r
687   height=(pack>>24)&7; // Width and height in tiles\r
688 \r
689   row=Scanline-sy; // Row of the sprite we are on\r
690 \r
691   if (pack2&0x1000) row=(height<<3)-1-row; // Flip Y\r
692 \r
693   tile=pack2&0x7ff; // Tile number\r
694   tile+=row>>3; // Tile number increases going down\r
695   delta=height; // Delta to increase tile by going right\r
696   if (pack2&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X\r
697 \r
698   tile<<=4; tile+=(row&7)<<1; // Tile address\r
699   delta<<=4; // Delta of address\r
700   pal=((pack2>>9)&0x30);\r
701   if((shpri&1)&&!(shpri&2)) pal|=0x40;\r
702 \r
703   shpri&=1;\r
704   if((pack2&0x6000) != 0x6000) shpri = 0;\r
705   shpri |= (pack2&0x0800)>>10;\r
706   switch(shpri) {\r
707     default:\r
708     case 0: fTileFunc=TileNormZ;   break;\r
709     case 1: fTileFunc=TileNormZSH; break;\r
710     case 2: fTileFunc=TileFlipZ;   break;\r
711     case 3: fTileFunc=TileFlipZSH; break;\r
712   }\r
713 \r
714   for (; width; width--,sx+=8,tile+=delta)\r
715   {\r
716     if(sx<=0)   continue;\r
717     if(sx>=328) break; // Offscreen\r
718 \r
719     tile&=0x7fff; // Clip tile address\r
720     fTileFunc(sx,tile,pal,sprio);\r
721   }\r
722 }\r
723 \r
724 static void DrawSpriteInterlace(unsigned int *sprite)\r
725 {\r
726   int width=0,height=0;\r
727   int row=0,code=0;\r
728   int pal;\r
729   int tile=0,delta=0;\r
730   int sx, sy;\r
731 \r
732   // parse the sprite data\r
733   sy=sprite[0];\r
734   height=sy>>24;\r
735   sy=(sy&0x3ff)-0x100; // Y\r
736   width=(height>>2)&3; height&=3;\r
737   width++; height++; // Width and height in tiles\r
738 \r
739   row=(Scanline<<1)-sy; // Row of the sprite we are on\r
740 \r
741   code=sprite[1];\r
742   sx=((code>>16)&0x1ff)-0x78; // X\r
743 \r
744   if (code&0x1000) row^=(16<<height)-1; // Flip Y\r
745 \r
746   tile=code&0x3ff; // Tile number\r
747   tile+=row>>4; // Tile number increases going down\r
748   delta=height; // Delta to increase tile by going right\r
749   if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X\r
750 \r
751   tile<<=5; tile+=(row&15)<<1; // Tile address\r
752 \r
753   delta<<=5; // Delta of address\r
754   pal=((code>>9)&0x30); // Get palette pointer\r
755 \r
756   for (; width; width--,sx+=8,tile+=delta)\r
757   {\r
758     if(sx<=0)   continue;\r
759     if(sx>=328) break; // Offscreen\r
760 \r
761     tile&=0x7fff; // Clip tile address\r
762     if (code&0x0800) TileFlip(sx,tile,pal);\r
763     else             TileNorm(sx,tile,pal);\r
764   }\r
765 }\r
766 \r
767 \r
768 static void DrawAllSpritesInterlace(int pri, int maxwidth)\r
769 {\r
770   struct PicoVideo *pvid=&Pico.video;\r
771   int i,u,table,link=0,sline=Scanline<<1;\r
772   unsigned int *sprites[80]; // Sprite index\r
773 \r
774   table=pvid->reg[5]&0x7f;\r
775   if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode\r
776   table<<=8; // Get sprite table address/2\r
777 \r
778   for (i=u=0; u < 80 && i < 21; u++)\r
779   {\r
780     unsigned int *sprite;\r
781     int code, sx, sy, height;\r
782 \r
783     sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
784 \r
785     // get sprite info\r
786     code = sprite[0];\r
787     sx = sprite[1];\r
788     if(((sx>>15)&1) != pri) goto nextsprite; // wrong priority sprite\r
789 \r
790     // check if it is on this line\r
791     sy = (code&0x3ff)-0x100;\r
792     height = (((code>>24)&3)+1)<<4;\r
793     if(sline < sy || sline >= sy+height) goto nextsprite; // no\r
794 \r
795     // check if sprite is not hidden offscreen\r
796     sx = (sx>>16)&0x1ff;\r
797     sx -= 0x78; // Get X coordinate + 8\r
798     if(sx <= -8*3 || sx >= maxwidth) goto nextsprite;\r
799 \r
800     // sprite is good, save it's pointer\r
801     sprites[i++]=sprite;\r
802 \r
803     nextsprite:\r
804     // Find next sprite\r
805     link=(code>>16)&0x7f;\r
806     if(!link) break; // End of sprites\r
807   }\r
808 \r
809   // Go through sprites backwards:\r
810   for (i-- ;i>=0; i--)\r
811     DrawSpriteInterlace(sprites[i]);\r
812 }\r
813 \r
814 \r
815 #ifndef _ASM_DRAW_C\r
816 static void DrawSpritesFromCache(int *hc, int sh)\r
817 {\r
818   int code, tile, sx, delta, width;\r
819   int pal;\r
820   int (*fTileFunc)(int sx,int addr,int pal);\r
821 \r
822   // *(*hc)++ = (tile<<16)|((code&0x0800)<<5)|((sx<<6)&0x0000ffc0)|((code>>9)&0x30)|((sprite[0]>>24)&0xf);\r
823 \r
824   while((code=*hc++)) {\r
825     pal=(code&0x30);\r
826     delta=code&0xf;\r
827     width=delta>>2; delta&=3;\r
828     width++; delta++; // Width and height in tiles\r
829     if (code&0x10000) delta=-delta; // Flip X\r
830     delta<<=4;\r
831     tile=((unsigned int)code>>17)<<1;\r
832     sx=(code<<16)>>22; // sx can be negative (start offscreen), so sign extend\r
833 \r
834     if(sh && pal == 0x30) { //\r
835       if(code&0x10000) fTileFunc=TileFlipSH;\r
836       else             fTileFunc=TileNormSH;\r
837     } else {\r
838       if(code&0x10000) fTileFunc=TileFlip;\r
839       else             fTileFunc=TileNorm;\r
840     }\r
841 \r
842     for (; width; width--,sx+=8,tile+=delta)\r
843     {\r
844       if(sx<=0)   continue;\r
845       if(sx>=328) break; // Offscreen\r
846 \r
847       tile&=0x7fff; // Clip tile address\r
848       fTileFunc(sx,tile,pal);\r
849     }\r
850   }\r
851 }\r
852 #endif\r
853 \r
854 \r
855 // Index + 0  :    ----hhvv -lllllll -------y yyyyyyyy\r
856 // Index + 4  :    -------x xxxxxxxx pccvhnnn nnnnnnnn\r
857 // v\r
858 // Index + 0  :    hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size\r
859 // Index + 4  :    xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8\r
860 \r
861 static void PrepareSprites(int full)\r
862 {\r
863   struct PicoVideo *pvid=&Pico.video;\r
864   int u=0,link=0,sblocks=0;\r
865   int table=0;\r
866   int *pd = HighPreSpr;\r
867 \r
868   table=pvid->reg[5]&0x7f;\r
869   if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode\r
870   table<<=8; // Get sprite table address/2\r
871 \r
872   if (!full)\r
873   {\r
874     int pack;\r
875     // updates: tilecode, sx\r
876     for (u=0; u < 80 && (pack = *pd); u++, pd+=2)\r
877     {\r
878       unsigned int *sprite;\r
879       int code, code2, sx, sy, skip=0;\r
880 \r
881       sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
882 \r
883       // parse sprite info\r
884       code  = sprite[0];\r
885       code2 = sprite[1];\r
886       code2 &= ~0xfe000000;\r
887       code2 -=  0x00780000; // Get X coordinate + 8 in upper 16 bits\r
888       sx = code2>>16;\r
889 \r
890       if((sx <= 8-((pack>>28)<<3) && sx >= -0x76) || sx >= 328) skip=1<<23;\r
891       else if ((sy = (pack<<16)>>16) < 240 && sy > -32) {\r
892         int sbl = (2<<(pack>>28))-1;\r
893         sblocks |= sbl<<(sy>>3);\r
894       }\r
895 \r
896       *pd = (pack&~(1<<23))|skip;\r
897       *(pd+1) = code2;\r
898 \r
899       // Find next sprite\r
900       link=(code>>16)&0x7f;\r
901       if(!link) break; // End of sprites\r
902     }\r
903     SpriteBlocks |= sblocks;\r
904   }\r
905   else\r
906   {\r
907     for (; u < 80; u++)\r
908     {\r
909       unsigned int *sprite;\r
910       int code, code2, sx, sy, hv, height, width, skip=0, sx_min;\r
911 \r
912       sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r
913 \r
914       // parse sprite info\r
915       code = sprite[0];\r
916       sy = (code&0x1ff)-0x80;\r
917       hv = (code>>24)&0xf;\r
918       height = (hv&3)+1;\r
919 \r
920       if(sy > 240 || sy + (height<<3) <= 0) skip|=1<<22;\r
921 \r
922       width  = (hv>>2)+1;\r
923       code2 = sprite[1];\r
924       sx = (code2>>16)&0x1ff;\r
925       sx -= 0x78; // Get X coordinate + 8\r
926       sx_min = 8-(width<<3);\r
927 \r
928       if((sx <= sx_min && sx >= -0x76) || sx >= 328) skip|=1<<23;\r
929       else if (sx > sx_min && !skip) {\r
930         int sbl = (2<<height)-1;\r
931         int shi = sy>>3;\r
932         if(shi < 0) shi=0; // negative sy\r
933         sblocks |= sbl<<shi;\r
934       }\r
935 \r
936       *pd++ = (width<<28)|(height<<24)|skip|(hv<<16)|((unsigned short)sy);\r
937       *pd++ = (sx<<16)|((unsigned short)code2);\r
938 \r
939       // Find next sprite\r
940       link=(code>>16)&0x7f;\r
941       if(!link) break; // End of sprites\r
942     }\r
943     SpriteBlocks = sblocks;\r
944     *pd = 0; // terminate\r
945   }\r
946 }\r
947 \r
948 static void DrawAllSprites(int *hcache, int maxwidth, int prio, int sh)\r
949 {\r
950   int i,u,n;\r
951   int sx1seen=0; // sprite with x coord 1 or 0 seen\r
952   int ntiles = 0; // tile counter for sprite limit emulation\r
953   int *sprites[40]; // Sprites to draw in fast mode\r
954   int *ps, pack, rs = rendstatus, scan=Scanline;\r
955 \r
956   if(rs&8) {\r
957     DrawAllSpritesInterlace(prio, maxwidth);\r
958     return;\r
959   }\r
960   if(rs&0x11) {\r
961     //dprintf("PrepareSprites(%i) [%i]", (rs>>4)&1, scan);\r
962     PrepareSprites(rs&0x10);\r
963     rendstatus=rs&~0x11;\r
964   }\r
965   if (!(SpriteBlocks & (1<<(scan>>3)))) return;\r
966 \r
967   if(((rs&4)||sh)&&prio==0)\r
968     memset(HighSprZ, 0, 328);\r
969   if(!(rs&4)&&prio) {\r
970     if(hcache[0]) DrawSpritesFromCache(hcache, sh);\r
971     return;\r
972   }\r
973 \r
974   ps = HighPreSpr;\r
975 \r
976   // Index + 0  :    hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size\r
977   // Index + 4  :    xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8\r
978 \r
979   for(i=u=n=0; (pack = *ps) && n < 20; ps+=2, u++)\r
980   {\r
981     int sx, sy, row, pack2;\r
982 \r
983     if(pack & 0x00400000) continue;\r
984 \r
985     // get sprite info\r
986     pack2 = *(ps+1);\r
987     sx =  pack2>>16;\r
988     sy = (pack <<16)>>16;\r
989     row = scan-sy;\r
990 \r
991     //dprintf("x: %i y: %i %ix%i", sx, sy, (pack>>28)<<3, (pack>>21)&0x38);\r
992 \r
993     if(sx == -0x77) sx1seen|=1; // for masking mode 2\r
994 \r
995     // check if it is on this line\r
996     if(row < 0 || row >= ((pack>>21)&0x38)) continue; // no\r
997     n++; // number of sprites on this line (both visible and hidden, max is 20) [broken]\r
998 \r
999     // sprite limit\r
1000     ntiles += pack>>28;\r
1001     if(ntiles > 40) break;\r
1002 \r
1003     if(pack & 0x00800000) continue;\r
1004 \r
1005     // masking sprite?\r
1006     if(sx == -0x78) {\r
1007       if(!(sx1seen&1) || sx1seen==3) {\r
1008         break; // this sprite is not drawn and remaining sprites are masked\r
1009       }\r
1010       if((sx1seen>>8) == 0) sx1seen=(i+1)<<8;\r
1011       continue;\r
1012     }\r
1013     else if(sx == -0x77) {\r
1014       // masking mode2 (Outrun, Galaxy Force II, Shadow of the beast)\r
1015       if(sx1seen>>8) { i=(sx1seen>>8)-1; break; } // seen both 0 and 1\r
1016       sx1seen |= 2;\r
1017       continue;\r
1018     }\r
1019 \r
1020     // accurate sprites\r
1021     //dprintf("P:%i",((sx>>15)&1));\r
1022     if(rs&4) {\r
1023       // might need to skip this sprite\r
1024       if((pack2&0x8000) ^ (prio<<15)) continue;\r
1025       DrawSpriteZ(pack,pack2,sh|(prio<<1),(char)(0x1f-n));\r
1026       continue;\r
1027     }\r
1028 \r
1029     // sprite is good, save it's pointer\r
1030     sprites[i++]=ps;\r
1031   }\r
1032 \r
1033   // Go through sprites backwards:\r
1034   if(!(rs&4)) {\r
1035     for (i--; i>=0; i--)\r
1036       DrawSprite(sprites[i],&hcache,sh);\r
1037 \r
1038     // terminate cache list\r
1039     *hcache = 0;\r
1040   }\r
1041 }\r
1042 \r
1043 \r
1044 // --------------------------------------------\r
1045 \r
1046 #ifndef _ASM_DRAW_C\r
1047 static void BackFill(int reg7, int sh)\r
1048 {\r
1049   unsigned int back=0;\r
1050   unsigned int *pd=NULL,*end=NULL;\r
1051 \r
1052   // Start with a blank scanline (background colour):\r
1053   back=reg7&0x3f;\r
1054   back|=sh<<6;\r
1055   back|=back<<8;\r
1056   back|=back<<16;\r
1057 \r
1058   pd= (unsigned int *)(HighCol+8);\r
1059   end=(unsigned int *)(HighCol+8+320);\r
1060 \r
1061   do { pd[0]=pd[1]=pd[2]=pd[3]=back; pd+=4; } while (pd<end);\r
1062 }\r
1063 #endif\r
1064 \r
1065 // --------------------------------------------\r
1066 \r
1067 unsigned short HighPal[0x100];\r
1068 \r
1069 #ifndef _ASM_DRAW_C\r
1070 static void FinalizeLineBGR444(int sh)\r
1071 {\r
1072   unsigned short *pd=DrawLineDest;\r
1073   unsigned char  *ps=HighCol+8;\r
1074   unsigned short *pal=Pico.cram;\r
1075   int len, i, t;\r
1076 \r
1077   if (Pico.video.reg[12]&1) {\r
1078     len = 320;\r
1079   } else {\r
1080     if(!(PicoOpt&0x100)) pd+=32;\r
1081     len = 256;\r
1082   }\r
1083 \r
1084   if(sh) {\r
1085     pal=HighPal;\r
1086     if(Pico.m.dirtyPal) {\r
1087       blockcpy(pal, Pico.cram, 0x40*2);\r
1088       // shadowed pixels\r
1089       for(i = 0x3f; i >= 0; i--)\r
1090         pal[0x40|i] = pal[0xc0|i] = (unsigned short)((pal[i]>>1)&0x0777);\r
1091       // hilighted pixels\r
1092       for(i = 0x3f; i >= 0; i--) {\r
1093         t=pal[i]&0xeee;t+=0x444;if(t&0x10)t|=0xe;if(t&0x100)t|=0xe0;if(t&0x1000)t|=0xe00;t&=0xeee;\r
1094         pal[0x80|i]=(unsigned short)t;\r
1095       }\r
1096       Pico.m.dirtyPal = 0;\r
1097     }\r
1098   }\r
1099 \r
1100   for(i = 0; i < len; i++)\r
1101     pd[i] = pal[ps[i]];\r
1102 }\r
1103 \r
1104 \r
1105 static void FinalizeLineRGB555(int sh)\r
1106 {\r
1107   unsigned short *pd=DrawLineDest;\r
1108   unsigned char  *ps=HighCol+8;\r
1109   unsigned short *pal=HighPal;\r
1110   int len, i, t, dirtyPal = Pico.m.dirtyPal;\r
1111 \r
1112   if(dirtyPal) {\r
1113     unsigned short *ppal=Pico.cram;\r
1114     for(i = 0x3f; i >= 0; i--)\r
1115       pal[i] = (unsigned short) (((ppal[i]&0x00f)<<12)|((ppal[i]&0x0f0)<<3)|((ppal[i]&0xf00)>>7));\r
1116     Pico.m.dirtyPal = 0;\r
1117   }\r
1118 \r
1119   if (Pico.video.reg[12]&1) {\r
1120     len = 320;\r
1121   } else {\r
1122     if(!(PicoOpt&0x100)) pd+=32;\r
1123     len = 256;\r
1124   }\r
1125 \r
1126   if(sh) {\r
1127     if(dirtyPal) {\r
1128       // shadowed pixels\r
1129       for(i = 0x3f; i >= 0; i--)\r
1130         pal[0x40|i] = pal[0xc0|i] = (unsigned short)((pal[i]>>1)&0x738e);\r
1131       // hilighted pixels\r
1132       for(i = 0x3f; i >= 0; i--) {\r
1133         t=pal[i]&0xe71c;t+=0x4208;if(t&0x20)t|=0x1c;if(t&0x800)t|=0x700;if(t&0x10000)t|=0xe000;t&=0xe71c;\r
1134         pal[0x80|i]=(unsigned short)t;\r
1135       }\r
1136     }\r
1137   }\r
1138 \r
1139   for(i = 0; i < len; i++)\r
1140     pd[i] = pal[ps[i]];\r
1141 }\r
1142 #endif\r
1143 \r
1144 static void FinalizeLine8bit(int sh)\r
1145 {\r
1146   unsigned char *pd=DrawLineDest;\r
1147   int len, rs = rendstatus;\r
1148   static int dirty_count;\r
1149 \r
1150   if (!sh && Pico.m.dirtyPal == 1 && Scanline < 222) {\r
1151     // a hack for mid-frame palette changes\r
1152     if (!(rs & 0x20))\r
1153          dirty_count = 1;\r
1154     else dirty_count++;\r
1155     rs |= 0x20;\r
1156     rendstatus = rs;\r
1157     if (dirty_count == 3) {\r
1158       blockcpy(HighPal, Pico.cram, 0x40*2);\r
1159     } else if (dirty_count == 11) {\r
1160       blockcpy(HighPal+0x40, Pico.cram, 0x40*2);\r
1161     }\r
1162   }\r
1163 \r
1164   if (Pico.video.reg[12]&1) {\r
1165     len = 320;\r
1166   } else {\r
1167     if(!(PicoOpt&0x100)) pd+=32;\r
1168     len = 256;\r
1169   }\r
1170 \r
1171   if (!sh && rs & 0x20) {\r
1172     if (dirty_count >= 11) {\r
1173       blockcpy_or(pd, HighCol+8, len, 0x80);\r
1174     } else {\r
1175       blockcpy_or(pd, HighCol+8, len, 0x40);\r
1176     }\r
1177   } else {\r
1178     blockcpy(pd, HighCol+8, len);\r
1179   }\r
1180 }\r
1181 \r
1182 void (*FinalizeLine)(int sh) = FinalizeLineBGR444;\r
1183 \r
1184 // --------------------------------------------\r
1185 \r
1186 static int DrawDisplay(int sh)\r
1187 {\r
1188   struct PicoVideo *pvid=&Pico.video;\r
1189   int win=0,edge=0,hvwind=0;\r
1190   int maxw, maxcells;\r
1191 \r
1192   if(pvid->reg[12]&1) {\r
1193     maxw = 328; maxcells = 40;\r
1194   } else {\r
1195     maxw = 264; maxcells = 32;\r
1196   }\r
1197 \r
1198   // Find out if the window is on this line:\r
1199   win=pvid->reg[0x12];\r
1200   edge=(win&0x1f)<<3;\r
1201 \r
1202   if (win&0x80) { if (Scanline>=edge) hvwind=1; }\r
1203   else          { if (Scanline< edge) hvwind=1; }\r
1204 \r
1205   if(!hvwind) { // we might have a vertical window here\r
1206     win=pvid->reg[0x11];\r
1207     edge=win&0x1f;\r
1208     if(win&0x80) {\r
1209       if(!edge) hvwind=1;\r
1210       else if(edge < (maxcells>>1)) hvwind=2;\r
1211     } else {\r
1212       if(!edge);\r
1213       else if(edge < (maxcells>>1)) hvwind=2;\r
1214       else hvwind=1;\r
1215     }\r
1216   }\r
1217 \r
1218   DrawLayer(1, HighCacheB, maxcells, sh);\r
1219   if(hvwind == 1)\r
1220     DrawWindow(0, maxcells>>1, 0, sh); // HighCacheAW\r
1221   else if(hvwind == 2) {\r
1222     // ahh, we have vertical window\r
1223     DrawLayer(0, HighCacheA, (win&0x80) ? edge<<1 : maxcells, sh);\r
1224     DrawWindow((win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 0, sh); // HighCacheW\r
1225   } else\r
1226     DrawLayer(0, HighCacheA, maxcells, sh);\r
1227   DrawAllSprites(HighCacheS, maxw, 0, sh);\r
1228 \r
1229   if(HighCacheB[0])  DrawTilesFromCache(HighCacheB, sh);\r
1230   if(hvwind == 1)\r
1231     DrawWindow(0, maxcells>>1, 1, sh);\r
1232   else if(hvwind == 2) {\r
1233     if(HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh);\r
1234     DrawWindow((win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 1, sh);\r
1235   } else\r
1236     if(HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh);\r
1237   DrawAllSprites(HighCacheS, maxw, 1, sh);\r
1238 \r
1239   return 0;\r
1240 }\r
1241 \r
1242 \r
1243 static int Skip=0;\r
1244 \r
1245 PICO_INTERNAL void PicoFrameStart(void)\r
1246 {\r
1247   // prepare to do this frame\r
1248   rendstatus = (PicoOpt&0x80)>>5;    // accurate sprites\r
1249   if(rendstatus)\r
1250        Pico.video.status &= ~0x0020;\r
1251   else Pico.video.status |=  0x0020; // sprite collision\r
1252   if((Pico.video.reg[12]&6) == 6) rendstatus |= 8; // interlace mode\r
1253   if(Pico.m.dirtyPal) Pico.m.dirtyPal = 2; // reset dirty if needed\r
1254 \r
1255   PrepareSprites(1);\r
1256   Skip=0;\r
1257 }\r
1258 \r
1259 PICO_INTERNAL int PicoLine(int scan)\r
1260 {\r
1261   int sh;\r
1262   if (Skip>0) { Skip--; return 0; } // Skip rendering lines\r
1263 \r
1264   Scanline=scan;\r
1265   sh=(Pico.video.reg[0xC]&8)>>3; // shadow/hilight?\r
1266 \r
1267   // Draw screen:\r
1268   BackFill(Pico.video.reg[7], sh);\r
1269   if (Pico.video.reg[1]&0x40)\r
1270     DrawDisplay(sh);\r
1271 \r
1272   FinalizeLine(sh);\r
1273   //if (SpriteBlocks & (1<<(scan>>3))) for (sh=0; sh < 30; sh++) DrawLineDest[sh] = 0xf;\r
1274 \r
1275   Skip=PicoScan(Scanline,DrawLineDest);\r
1276 \r
1277   return 0;\r
1278 }\r
1279 \r
1280 \r
1281 void PicoDrawSetColorFormat(int which)\r
1282 {\r
1283     if (which == 2)\r
1284          FinalizeLine = FinalizeLine8bit;\r
1285     else if (which == 1)\r
1286          FinalizeLine = FinalizeLineRGB555;\r
1287     else FinalizeLine = FinalizeLineBGR444;\r
1288 }\r