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