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