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