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