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