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