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