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