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