cff531af |
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 |
e5fa9817 |
9 | /*\r |
10 | * The renderer has 4 modes now:\r |
11 | * - normal\r |
12 | * - shadow/hilight (s/h)\r |
40644bfe |
13 | * - "sonic mode" for midline palette changes (8bit mode only)\r |
14 | * - accurate sprites (AS) [+ s/h]\r |
e5fa9817 |
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 |
40644bfe |
19 | *\r |
bfa12428 |
20 | * since renderer always draws line in 8bit mode, there are 2 spare bits:\r |
21 | * b \ mode: s/h as sonic\r |
a39d8ba5 |
22 | * 00 normal - pal index\r |
bfa12428 |
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 |
40644bfe |
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 |
e5fa9817 |
30 | */\r |
cc68a136 |
31 | \r |
efcba75f |
32 | #include "pico_int.h"\r |
cc68a136 |
33 | \r |
602133e1 |
34 | int (*PicoScanBegin)(unsigned int num) = NULL;\r |
35 | int (*PicoScanEnd) (unsigned int num) = NULL;\r |
cc68a136 |
36 | \r |
ea8c405f |
37 | static unsigned char DefHighCol[8+320+8];\r |
5a681086 |
38 | unsigned char *HighCol = DefHighCol;\r |
39 | static unsigned char *HighColBase = DefHighCol;\r |
40 | static int HighColIncrement;\r |
41 | \r |
87b0845f |
42 | static unsigned int DefOutBuff[320*2/2];\r |
5a681086 |
43 | void *DrawLineDest = DefOutBuff; // pointer to dest buffer where to draw this line to\r |
44 | void *DrawLineDestBase = DefOutBuff;\r |
45 | int DrawLineDestIncrement;\r |
ea8c405f |
46 | \r |
cc68a136 |
47 | static int HighCacheA[41+1]; // caches for high layers\r |
48 | static int HighCacheB[41+1];\r |
283fec1b |
49 | int HighPreSpr[80*2+1]; // slightly preprocessed sprites\r |
381eea9b |
50 | \r |
fbc65db7 |
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 |
55 | unsigned char HighLnSpr[240][3 + MAX_LINE_SPRITES]; // sprite_count, ^flags, tile_count, [spritep]...\r |
d9fc2fe1 |
56 | \r |
19954be1 |
57 | int rendstatus, rendstatus_old;\r |
ae87bffa |
58 | int rendlines;\r |
19954be1 |
59 | int DrawScanline;\r |
fbc65db7 |
60 | int PicoDrawMask = -1;\r |
cc68a136 |
61 | \r |
602133e1 |
62 | static int skip_next_line=0;\r |
63 | \r |
cc68a136 |
64 | //unsigned short ppt[] = { 0x0f11, 0x0ff1, 0x01f1, 0x011f, 0x01ff, 0x0f1f, 0x0f0e, 0x0e7c };\r |
65 | \r |
66 | struct TileStrip\r |
67 | {\r |
68 | int nametab; // Position in VRAM of name table (for this tile line)\r |
6d7acf9e |
69 | int line; // Line number in pixels 0x000-0x3ff within the virtual tilemap\r |
cc68a136 |
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 |
78 | void DrawWindow(int tstart, int tend, int prio, int sh);\r |
0fc0e241 |
79 | void DrawAllSprites(unsigned char *sprited, int prio, int sh);\r |
7a7c6476 |
80 | void DrawTilesFromCache(int *hc, int sh, int rlim);\r |
0fc0e241 |
81 | void DrawSpritesSHi(unsigned char *sprited);\r |
83c093a4 |
82 | void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells);\r |
974fdb5b |
83 | void FinalizeLineBGR444(int sh, int line);\r |
7b802576 |
84 | void *blockcpy(void *dst, const void *src, size_t n);\r |
cc68a136 |
85 | void blockcpy_or(void *dst, void *src, size_t n, int pat);\r |
86 | #else\r |
87 | // utility\r |
88 | void 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 |
7b802576 |
94 | #define blockcpy memcpy\r |
cc68a136 |
95 | #endif\r |
96 | \r |
97 | \r |
07abbab1 |
98 | #define TileNormMaker(funcname,pix_func) \\r |
99 | static 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 |
cc68a136 |
119 | }\r |
120 | \r |
cc68a136 |
121 | \r |
07abbab1 |
122 | #define TileFlipMaker(funcname,pix_func) \\r |
123 | static 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 |
cc68a136 |
143 | }\r |
cc68a136 |
144 | \r |
cc68a136 |
145 | \r |
07abbab1 |
146 | #ifdef _ASM_DRAW_C_AMIPS\r |
147 | int TileNorm(int sx,int addr,int pal);\r |
148 | int TileFlip(int sx,int addr,int pal);\r |
149 | #else\r |
cc68a136 |
150 | \r |
07abbab1 |
151 | #define pix_just_write(x) \\r |
152 | if (t) pd[x]=pal|t\r |
cc68a136 |
153 | \r |
07abbab1 |
154 | TileNormMaker(TileNorm,pix_just_write)\r |
155 | TileFlipMaker(TileFlip,pix_just_write)\r |
cc68a136 |
156 | \r |
cc68a136 |
157 | #endif\r |
158 | \r |
e352c3af |
159 | #ifndef _ASM_DRAW_C\r |
160 | \r |
07abbab1 |
161 | // draw a sprite pixel, process operator colors\r |
162 | #define pix_sh(x) \\r |
163 | if (!t); \\r |
40644bfe |
164 | else if (t>=0xe) pd[x]=(pd[x]&0x3f)|(t<<6); /* c0 shadow, 80 hilight */ \\r |
07abbab1 |
165 | else pd[x]=pal|t\r |
e5fa9817 |
166 | \r |
07abbab1 |
167 | TileNormMaker(TileNormSH, pix_sh)\r |
168 | TileFlipMaker(TileFlipSH, pix_sh)\r |
cc68a136 |
169 | \r |
40644bfe |
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 |
cc68a136 |
175 | \r |
40644bfe |
176 | TileNormMaker(TileNormSH_markop, pix_sh_markop)\r |
177 | TileFlipMaker(TileFlipSH_markop, pix_sh_markop)\r |
6c254710 |
178 | \r |
40644bfe |
179 | // process operator pixels only, apply only on low pri tiles and other op pixels\r |
07abbab1 |
180 | #define pix_sh_onlyop(x) \\r |
40644bfe |
181 | if (t>=0xe && (pd[x]&0xc0)) \\r |
182 | pd[x]=(pd[x]&0x3f)|(t<<6); /* c0 shadow, 80 hilight */ \\r |
cc68a136 |
183 | \r |
07abbab1 |
184 | TileNormMaker(TileNormSH_onlyop_lp, pix_sh_onlyop)\r |
185 | TileFlipMaker(TileFlipSH_onlyop_lp, pix_sh_onlyop)\r |
cc68a136 |
186 | \r |
e352c3af |
187 | #endif\r |
188 | \r |
07abbab1 |
189 | // draw a sprite pixel (AS)\r |
190 | #define pix_as(x) \\r |
191 | if (t && !(pd[x]&0x80)) pd[x]=pal|t\r |
cc68a136 |
192 | \r |
07abbab1 |
193 | TileNormMaker(TileNormAS, pix_as)\r |
194 | TileFlipMaker(TileFlipAS, pix_as)\r |
cc68a136 |
195 | \r |
07abbab1 |
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 |
cc68a136 |
199 | \r |
07abbab1 |
200 | TileNormMaker(TileNormAS_noop, pix_sh_as_noop)\r |
201 | TileFlipMaker(TileFlipAS_noop, pix_sh_as_noop)\r |
cc68a136 |
202 | \r |
07abbab1 |
203 | // mark pixel as sprite pixel (AS)\r |
204 | #define pix_sh_as_onlymark(x) \\r |
205 | if (t) pd[x]|=0x80\r |
cc68a136 |
206 | \r |
07abbab1 |
207 | TileNormMaker(TileNormAS_onlymark, pix_sh_as_onlymark)\r |
208 | TileFlipMaker(TileFlipAS_onlymark, pix_sh_as_onlymark)\r |
cc68a136 |
209 | \r |
e5fa9817 |
210 | \r |
cc68a136 |
211 | // --------------------------------------------\r |
212 | \r |
213 | #ifndef _ASM_DRAW_C\r |
83c093a4 |
214 | static void DrawStrip(struct TileStrip *ts, int plane_sh, int cellskip)\r |
cc68a136 |
215 | {\r |
83c093a4 |
216 | int tilex,dx,ty,code=0,addr=0,cells;\r |
cc68a136 |
217 | int oldcode=-1,blank=-1; // The tile we know is blank\r |
83c093a4 |
218 | int pal=0,sh;\r |
cc68a136 |
219 | \r |
220 | // Draw tiles across screen:\r |
83c093a4 |
221 | sh=(plane_sh<<5)&0x40;\r |
222 | tilex=((-ts->hscroll)>>3)+cellskip;\r |
cc68a136 |
223 | ty=(ts->line&7)<<1; // Y-Offset into tile\r |
224 | dx=((ts->hscroll-1)&7)+1;\r |
83c093a4 |
225 | cells = ts->cells - cellskip;\r |
cc68a136 |
226 | if(dx != 8) cells++; // have hscroll, need to draw 1 cell more\r |
83c093a4 |
227 | dx+=cellskip<<3;\r |
cc68a136 |
228 | \r |
83c093a4 |
229 | for (; cells > 0; dx+=8,tilex++,cells--)\r |
cc68a136 |
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 |
83c093a4 |
249 | pal=((code>>9)&0x30)|sh;\r |
cc68a136 |
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 |
740da8c6 |
260 | // if oldcode wasn't changed, it means all layer is hi priority\r |
602133e1 |
261 | if (oldcode == -1) rendstatus |= PDRAW_PLANE_HI_PRIO;\r |
cc68a136 |
262 | }\r |
cc68a136 |
263 | \r |
264 | // this is messy\r |
83c093a4 |
265 | void DrawStripVSRam(struct TileStrip *ts, int plane_sh, int cellskip)\r |
cc68a136 |
266 | {\r |
83c093a4 |
267 | int tilex,dx,code=0,addr=0,cell=0;\r |
cc68a136 |
268 | int oldcode=-1,blank=-1; // The tile we know is blank\r |
b6d7ac70 |
269 | int pal=0,scan=DrawScanline;\r |
cc68a136 |
270 | \r |
271 | // Draw tiles across screen:\r |
272 | tilex=(-ts->hscroll)>>3;\r |
273 | dx=((ts->hscroll-1)&7)+1;\r |
83c093a4 |
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 |
cc68a136 |
278 | \r |
279 | for (; cell < ts->cells; dx+=8,tilex++,cell++)\r |
280 | {\r |
83c093a4 |
281 | int zero=0,nametabadd,ty;\r |
cc68a136 |
282 | \r |
83c093a4 |
283 | //if((cell&1)==0)\r |
284 | {\r |
cc68a136 |
285 | int line,vscroll;\r |
83c093a4 |
286 | vscroll=Pico.vsram[(plane_sh&1)+(cell&~1)];\r |
cc68a136 |
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 |
83c093a4 |
309 | pal=((code>>9)&0x30)|((plane_sh<<5)&0x40);\r |
cc68a136 |
310 | }\r |
311 | \r |
312 | if (code&0x0800) zero=TileFlip(dx,addr,pal);\r |
313 | else zero=TileNorm(dx,addr,pal);\r |
314 | \r |
315 | if (zero) blank=code; // We know this tile is blank now\r |
316 | }\r |
317 | \r |
318 | // terminate the cache list\r |
319 | *ts->hc = 0;\r |
602133e1 |
320 | if (oldcode == -1) rendstatus |= PDRAW_PLANE_HI_PRIO;\r |
cc68a136 |
321 | }\r |
6d7acf9e |
322 | #endif\r |
cc68a136 |
323 | \r |
324 | #ifndef _ASM_DRAW_C\r |
325 | static\r |
326 | #endif\r |
327 | void 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 |
83c093a4 |
377 | static void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells)\r |
cc68a136 |
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 |
83c093a4 |
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 |
cc68a136 |
402 | \r |
403 | htab=pvid->reg[13]<<9; // Horizontal scroll table address\r |
b6d7ac70 |
404 | if ( pvid->reg[11]&2) htab+=DrawScanline<<1; // Offset by line\r |
cc68a136 |
405 | if ((pvid->reg[11]&1)==0) htab&=~0xf; // Offset by tile\r |
83c093a4 |
406 | htab+=plane_sh&1; // A or B\r |
cc68a136 |
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 |
83c093a4 |
413 | vscroll=Pico.vsram[plane_sh&1]; // Get vertical scroll value\r |
cc68a136 |
414 | \r |
415 | // Find the line in the name table\r |
b6d7ac70 |
416 | ts.line=(vscroll+(DrawScanline<<1))&((ymask<<1)|1);\r |
cc68a136 |
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 |
83c093a4 |
424 | DrawStripVSRam(&ts, plane_sh, cellskip);\r |
cc68a136 |
425 | } else {\r |
83c093a4 |
426 | vscroll=Pico.vsram[plane_sh&1]; // Get vertical scroll value\r |
cc68a136 |
427 | \r |
428 | // Find the line in the name table\r |
b6d7ac70 |
429 | ts.line=(vscroll+DrawScanline)&ymask;\r |
cc68a136 |
430 | ts.nametab+=(ts.line>>3)<<shift[width];\r |
431 | \r |
83c093a4 |
432 | DrawStrip(&ts, plane_sh, cellskip);\r |
cc68a136 |
433 | }\r |
434 | }\r |
435 | \r |
436 | \r |
437 | // --------------------------------------------\r |
438 | \r |
439 | // tstart & tend are tile pair numbers\r |
440 | static void DrawWindow(int tstart, int tend, int prio, int sh) // int *hcache\r |
441 | {\r |
442 | struct PicoVideo *pvid=&Pico.video;\r |
07abbab1 |
443 | int tilex,ty,nametab,code=0;\r |
cc68a136 |
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 |
b6d7ac70 |
450 | nametab+=(DrawScanline>>3)<<6;\r |
cc68a136 |
451 | }\r |
452 | else\r |
453 | {\r |
454 | nametab=(pvid->reg[3]&0x3e)<<9; // 32-cell mode\r |
b6d7ac70 |
455 | nametab+=(DrawScanline>>3)<<5;\r |
cc68a136 |
456 | }\r |
457 | \r |
458 | tilex=tstart<<1;\r |
cc68a136 |
459 | \r |
602133e1 |
460 | if (!(rendstatus & PDRAW_WND_DIFF_PRIO)) {\r |
cc68a136 |
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 |
602133e1 |
464 | if ((code>>15) != prio) return;\r |
cc68a136 |
465 | }\r |
466 | \r |
07abbab1 |
467 | tend<<=1;\r |
b6d7ac70 |
468 | ty=(DrawScanline&7)<<1; // Y-Offset into tile\r |
07abbab1 |
469 | \r |
cc68a136 |
470 | // Draw tiles across screen:\r |
81fda4e8 |
471 | if (!sh)\r |
cc68a136 |
472 | {\r |
81fda4e8 |
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 |
602133e1 |
479 | if (code==blank) continue;\r |
480 | if ((code>>15) != prio) {\r |
481 | rendstatus |= PDRAW_WND_DIFF_PRIO;\r |
81fda4e8 |
482 | continue;\r |
483 | }\r |
cc68a136 |
484 | \r |
81fda4e8 |
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 |
cc68a136 |
495 | }\r |
81fda4e8 |
496 | }\r |
497 | else\r |
498 | {\r |
499 | for (; tilex < tend; tilex++)\r |
500 | {\r |
501 | int addr=0,zero=0;\r |
07abbab1 |
502 | int pal;\r |
81fda4e8 |
503 | \r |
504 | code=Pico.vram[nametab+tilex];\r |
505 | if(code==blank) continue;\r |
506 | if((code>>15) != prio) {\r |
602133e1 |
507 | rendstatus |= PDRAW_WND_DIFF_PRIO;\r |
81fda4e8 |
508 | continue;\r |
509 | }\r |
cc68a136 |
510 | \r |
81fda4e8 |
511 | pal=((code>>9)&0x30);\r |
cc68a136 |
512 | \r |
07abbab1 |
513 | if (prio) {\r |
514 | int *zb = (int *)(HighCol+8+(tilex<<3));\r |
bfa12428 |
515 | *zb++ &= 0xbfbfbfbf;\r |
516 | *zb &= 0xbfbfbfbf;\r |
cc68a136 |
517 | } else {\r |
518 | pal |= 0x40;\r |
519 | }\r |
cc68a136 |
520 | \r |
81fda4e8 |
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 |
cc68a136 |
524 | \r |
81fda4e8 |
525 | if (code&0x0800) zero=TileFlip(8+(tilex<<3),addr,pal);\r |
526 | else zero=TileNorm(8+(tilex<<3),addr,pal);\r |
cc68a136 |
527 | \r |
81fda4e8 |
528 | if (zero) blank=code; // We know this tile is blank now\r |
529 | }\r |
cc68a136 |
530 | }\r |
cc68a136 |
531 | }\r |
532 | \r |
533 | // --------------------------------------------\r |
534 | \r |
81fda4e8 |
535 | static void DrawTilesFromCacheShPrep(void)\r |
536 | {\r |
602133e1 |
537 | // as some layer has covered whole line with hi priority tiles,\r |
40644bfe |
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 |
602133e1 |
540 | int c = 320/4, *zb = (int *)(HighCol+8);\r |
541 | rendstatus |= PDRAW_SHHI_DONE;\r |
542 | while (c--)\r |
81fda4e8 |
543 | {\r |
40644bfe |
544 | *zb++ &= 0xbfbfbfbf;\r |
81fda4e8 |
545 | }\r |
546 | }\r |
547 | \r |
7a7c6476 |
548 | static void DrawTilesFromCache(int *hc, int sh, int rlim)\r |
cc68a136 |
549 | {\r |
740da8c6 |
550 | int code, addr, dx;\r |
cc68a136 |
551 | int pal;\r |
cc68a136 |
552 | \r |
553 | // *ts->hc++ = code | (dx<<16) | (ty<<25); // cache it\r |
554 | \r |
602133e1 |
555 | if (sh && (rendstatus & (PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO)))\r |
740da8c6 |
556 | {\r |
602133e1 |
557 | if (!(rendstatus & PDRAW_SHHI_DONE))\r |
558 | DrawTilesFromCacheShPrep();\r |
740da8c6 |
559 | sh = 0;\r |
560 | }\r |
cc68a136 |
561 | \r |
81fda4e8 |
562 | if (!sh)\r |
740da8c6 |
563 | {\r |
81fda4e8 |
564 | short blank=-1; // The tile we know is blank\r |
7a7c6476 |
565 | while ((code=*hc++)) {\r |
81fda4e8 |
566 | int zero;\r |
567 | if((short)code == blank) continue;\r |
740da8c6 |
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 |
740da8c6 |
572 | \r |
573 | pal=((code>>9)&0x30);\r |
7a7c6476 |
574 | if (rlim-dx < 0) goto last_cut_tile;\r |
740da8c6 |
575 | \r |
81fda4e8 |
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 |
cc68a136 |
580 | }\r |
740da8c6 |
581 | }\r |
582 | else\r |
583 | {\r |
7a7c6476 |
584 | while ((code=*hc++)) {\r |
81fda4e8 |
585 | unsigned char *zb;\r |
740da8c6 |
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 |
81fda4e8 |
590 | zb = HighCol+dx;\r |
40644bfe |
591 | *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf;\r |
592 | *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf;\r |
cc68a136 |
593 | \r |
740da8c6 |
594 | pal=((code>>9)&0x30);\r |
7a7c6476 |
595 | if (rlim-dx < 0) goto last_cut_tile;\r |
cc68a136 |
596 | \r |
81fda4e8 |
597 | if (code&0x0800) TileFlip(dx,addr,pal);\r |
598 | else TileNorm(dx,addr,pal);\r |
7a7c6476 |
599 | }\r |
600 | }\r |
601 | return;\r |
602 | \r |
603 | last_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 |
947fb5f9 |
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 |
7a7c6476 |
634 | }\r |
740da8c6 |
635 | }\r |
cc68a136 |
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 |
97a7f774 |
644 | static void DrawSprite(int *sprite, int sh)\r |
cc68a136 |
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 |
b6d7ac70 |
661 | row=DrawScanline-sy; // Row of the sprite we are on\r |
cc68a136 |
662 | \r |
663 | if (code&0x1000) row=(height<<3)-1-row; // Flip Y\r |
664 | \r |
07abbab1 |
665 | tile=code + (row>>3); // Tile number increases going down\r |
cc68a136 |
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 |
07abbab1 |
669 | tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address\r |
ee4f03ae |
670 | delta<<=4; // Delta of address\r |
07abbab1 |
671 | \r |
672 | pal=(code>>9)&0x30;\r |
97a7f774 |
673 | pal|=sh<<6;\r |
e5fa9817 |
674 | \r |
675 | if (sh && (code&0x6000) == 0x6000) {\r |
40644bfe |
676 | if(code&0x0800) fTileFunc=TileFlipSH_markop;\r |
677 | else fTileFunc=TileNormSH_markop;\r |
e5fa9817 |
678 | } else {\r |
679 | if(code&0x0800) fTileFunc=TileFlip;\r |
680 | else fTileFunc=TileNorm;\r |
cc68a136 |
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 |
e5fa9817 |
689 | fTileFunc(sx,tile,pal);\r |
cc68a136 |
690 | }\r |
691 | }\r |
e5fa9817 |
692 | #endif\r |
cc68a136 |
693 | \r |
694 | static 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 |
b6d7ac70 |
709 | row=(DrawScanline<<1)-sy; // Row of the sprite we are on\r |
cc68a136 |
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 |
ee4f03ae |
738 | static void DrawAllSpritesInterlace(int pri, int sh)\r |
cc68a136 |
739 | {\r |
740 | struct PicoVideo *pvid=&Pico.video;\r |
b6d7ac70 |
741 | int i,u,table,link=0,sline=DrawScanline<<1;\r |
cc68a136 |
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 |
947fb5f9 |
768 | if(sx <= -8*3 || sx >= 328) goto nextsprite;\r |
cc68a136 |
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 |
40644bfe |
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 |
fbc65db7 |
793 | static void DrawSpritesSHi(unsigned char *sprited)\r |
cc68a136 |
794 | {\r |
cc68a136 |
795 | int (*fTileFunc)(int sx,int addr,int pal);\r |
ee4f03ae |
796 | unsigned char *p;\r |
797 | int cnt;\r |
798 | \r |
fbc65db7 |
799 | cnt = sprited[0] & 0x7f;\r |
ee4f03ae |
800 | if (cnt == 0) return;\r |
cc68a136 |
801 | \r |
fbc65db7 |
802 | p = &sprited[3];\r |
cc68a136 |
803 | \r |
ee4f03ae |
804 | // Go through sprites backwards:\r |
805 | for (cnt--; cnt >= 0; cnt--)\r |
07abbab1 |
806 | {\r |
ee4f03ae |
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 |
cc68a136 |
814 | \r |
fbc65db7 |
815 | if (pal == 0x30)\r |
07abbab1 |
816 | {\r |
ee4f03ae |
817 | if (code & 0x8000) // hi priority\r |
07abbab1 |
818 | {\r |
ee4f03ae |
819 | if (code&0x800) fTileFunc=TileFlipSH;\r |
820 | else fTileFunc=TileNormSH;\r |
07abbab1 |
821 | } else {\r |
ee4f03ae |
822 | if (code&0x800) fTileFunc=TileFlipSH_onlyop_lp;\r |
823 | else fTileFunc=TileNormSH_onlyop_lp;\r |
07abbab1 |
824 | }\r |
cc68a136 |
825 | } else {\r |
ee4f03ae |
826 | if (!(code & 0x8000)) continue; // non-operator low sprite, already drawn\r |
827 | if (code&0x800) fTileFunc=TileFlip;\r |
828 | else fTileFunc=TileNorm;\r |
cc68a136 |
829 | }\r |
830 | \r |
ee4f03ae |
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 |
cc68a136 |
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 |
f8af9634 |
859 | #endif // !_ASM_DRAW_C\r |
cc68a136 |
860 | \r |
fbc65db7 |
861 | static void DrawSpritesHiAS(unsigned char *sprited, int sh)\r |
e5fa9817 |
862 | {\r |
e5fa9817 |
863 | int (*fTileFunc)(int sx,int addr,int pal);\r |
ee4f03ae |
864 | unsigned char *p;\r |
865 | int entry, cnt, sh_cnt = 0;\r |
866 | \r |
fbc65db7 |
867 | cnt = sprited[0] & 0x7f;\r |
ee4f03ae |
868 | if (cnt == 0) return;\r |
e5fa9817 |
869 | \r |
f8af9634 |
870 | rendstatus |= PDRAW_SPR_LO_ON_HI;\r |
871 | \r |
fbc65db7 |
872 | p = &sprited[3];\r |
e5fa9817 |
873 | \r |
ee4f03ae |
874 | // Go through sprites:\r |
875 | for (entry = 0; entry < cnt; entry++)\r |
07abbab1 |
876 | {\r |
ee4f03ae |
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 |
07abbab1 |
886 | {\r |
887 | if (sh && pal == 0x30)\r |
888 | {\r |
ee4f03ae |
889 | if (code&0x800) fTileFunc=TileFlipAS_noop;\r |
890 | else fTileFunc=TileNormAS_noop;\r |
07abbab1 |
891 | } else {\r |
ee4f03ae |
892 | if (code&0x800) fTileFunc=TileFlipAS;\r |
893 | else fTileFunc=TileNormAS;\r |
07abbab1 |
894 | }\r |
e5fa9817 |
895 | } else {\r |
ee4f03ae |
896 | if (code&0x800) fTileFunc=TileFlipAS_onlymark;\r |
897 | else fTileFunc=TileNormAS_onlymark;\r |
e5fa9817 |
898 | }\r |
411cba90 |
899 | if (sh && pal == 0x30)\r |
ee4f03ae |
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 |
e5fa9817 |
919 | \r |
07abbab1 |
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 |
fbc65db7 |
931 | if (!sh || !(sprited[1]&SPRL_MAY_HAVE_OP)) return;\r |
07abbab1 |
932 | \r |
933 | /* nasty 1: remove 'sprite' flags */\r |
934 | {\r |
bfa12428 |
935 | int c = 320/4/4, *zb = (int *)(HighCol+8);\r |
07abbab1 |
936 | while (c--)\r |
937 | {\r |
bfa12428 |
938 | *zb++ &= 0x7f7f7f7f; *zb++ &= 0x7f7f7f7f;\r |
939 | *zb++ &= 0x7f7f7f7f; *zb++ &= 0x7f7f7f7f;\r |
07abbab1 |
940 | }\r |
941 | }\r |
942 | \r |
ee4f03ae |
943 | /* nasty 2: sh operator pass */\r |
fbc65db7 |
944 | sprited[0] = sh_cnt;\r |
945 | DrawSpritesSHi(sprited);\r |
e5fa9817 |
946 | }\r |
947 | \r |
cc68a136 |
948 | \r |
949 | // Index + 0 : ----hhvv -lllllll -------y yyyyyyyy\r |
950 | // Index + 4 : -------x xxxxxxxx pccvhnnn nnnnnnnn\r |
951 | // v\r |
d9fc2fe1 |
952 | // Index + 0 : hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size\r |
cc68a136 |
953 | // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8\r |
954 | \r |
283fec1b |
955 | void PrepareSprites(int full)\r |
cc68a136 |
956 | {\r |
957 | struct PicoVideo *pvid=&Pico.video;\r |
fbc65db7 |
958 | int u,link=0,sh;\r |
cc68a136 |
959 | int table=0;\r |
960 | int *pd = HighPreSpr;\r |
947fb5f9 |
961 | int max_lines = 224, max_sprites = 80, max_width = 328;\r |
d9fc2fe1 |
962 | int max_line_sprites = 20; // 20 sprites, 40 tiles\r |
963 | \r |
964 | if (!(Pico.video.reg[12]&1))\r |
947fb5f9 |
965 | max_sprites = 64, max_line_sprites = 16, max_width = 264;\r |
d9fc2fe1 |
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 |
fbc65db7 |
970 | sh = Pico.video.reg[0xC]&8; // shadow/hilight?\r |
cc68a136 |
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 |
4c436138 |
980 | for (u=0; u < max_sprites && (pack = *pd); u++, pd+=2)\r |
cc68a136 |
981 | {\r |
982 | unsigned int *sprite;\r |
d9fc2fe1 |
983 | int code2, sx, sy, height;\r |
cc68a136 |
984 | \r |
985 | sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite\r |
986 | \r |
987 | // parse sprite info\r |
cc68a136 |
988 | code2 = sprite[1];\r |
d9fc2fe1 |
989 | sx = (code2>>16)&0x1ff;\r |
990 | sx -= 0x78; // Get X coordinate + 8\r |
991 | sy = (pack << 16) >> 16;\r |
947fb5f9 |
992 | height = (pack >> 24) & 0xf;\r |
cc68a136 |
993 | \r |
d9fc2fe1 |
994 | if (sy < max_lines && sy + (height<<3) > DrawScanline && // sprite onscreen (y)?\r |
947fb5f9 |
995 | (sx > -24 || sx < max_width)) // onscreen x\r |
d9fc2fe1 |
996 | {\r |
997 | int y = (sy >= DrawScanline) ? sy : DrawScanline;\r |
ee4f03ae |
998 | int entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80);\r |
d9fc2fe1 |
999 | for (; y < sy + (height<<3) && y < max_lines; y++)\r |
1000 | {\r |
947fb5f9 |
1001 | int i, cnt;\r |
d9fc2fe1 |
1002 | cnt = HighLnSpr[y][0] & 0x7f;\r |
1003 | if (cnt >= max_line_sprites) continue; // sprite limit?\r |
d9fc2fe1 |
1004 | \r |
1005 | for (i = 0; i < cnt; i++)\r |
fbc65db7 |
1006 | if (((HighLnSpr[y][3+i] ^ entry) & 0x7f) == 0) goto found;\r |
d9fc2fe1 |
1007 | \r |
1008 | // this sprite was previously missing\r |
fbc65db7 |
1009 | HighLnSpr[y][3+cnt] = entry;\r |
d9fc2fe1 |
1010 | HighLnSpr[y][0] = cnt + 1;\r |
1011 | found:;\r |
e352c3af |
1012 | if (entry & 0x80)\r |
fbc65db7 |
1013 | HighLnSpr[y][1] |= SPRL_HAVE_HI;\r |
1014 | else HighLnSpr[y][1] |= SPRL_HAVE_LO;\r |
d9fc2fe1 |
1015 | }\r |
cc68a136 |
1016 | }\r |
1017 | \r |
d9fc2fe1 |
1018 | code2 &= ~0xfe000000;\r |
1019 | code2 -= 0x00780000; // Get X coordinate + 8 in upper 16 bits\r |
1020 | pd[1] = code2;\r |
cc68a136 |
1021 | \r |
1022 | // Find next sprite\r |
d9fc2fe1 |
1023 | link=(sprite[0]>>16)&0x7f;\r |
1024 | if (!link) break; // End of sprites\r |
cc68a136 |
1025 | }\r |
cc68a136 |
1026 | }\r |
1027 | else\r |
1028 | {\r |
d9fc2fe1 |
1029 | for (u = 0; u < max_lines; u++)\r |
1030 | *((int *)&HighLnSpr[u][0]) = 0;\r |
1031 | \r |
4c436138 |
1032 | for (u = 0; u < max_sprites; u++)\r |
cc68a136 |
1033 | {\r |
1034 | unsigned int *sprite;\r |
d9fc2fe1 |
1035 | int code, code2, sx, sy, hv, height, width;\r |
cc68a136 |
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 |
cc68a136 |
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 |
6d7acf9e |
1049 | \r |
d9fc2fe1 |
1050 | if (sy < max_lines && sy + (height<<3) > DrawScanline) // sprite onscreen (y)?\r |
1051 | {\r |
fbc65db7 |
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 |
fbc65db7 |
1059 | entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80);\r |
1060 | y = (sy >= DrawScanline) ? sy : DrawScanline;\r |
d9fc2fe1 |
1061 | for (; y < sy + (height<<3) && y < max_lines; y++)\r |
1062 | {\r |
0fc0e241 |
1063 | unsigned char *p = &HighLnSpr[y][0];\r |
1064 | int cnt = p[0];\r |
d9fc2fe1 |
1065 | if (cnt >= max_line_sprites) continue; // sprite limit?\r |
1066 | \r |
0fc0e241 |
1067 | if (p[2] >= max_line_sprites*2) { // tile limit?\r |
1068 | p[0] |= 0x80;\r |
947fb5f9 |
1069 | continue;\r |
d9fc2fe1 |
1070 | }\r |
0fc0e241 |
1071 | p[2] += width;\r |
d9fc2fe1 |
1072 | \r |
1073 | if (sx == -0x78) {\r |
1074 | if (cnt > 0)\r |
0fc0e241 |
1075 | p[0] |= 0x80; // masked, no more sprites for this line\r |
947fb5f9 |
1076 | continue;\r |
d9fc2fe1 |
1077 | }\r |
1078 | // must keep the first sprite even if it's offscreen, for masking\r |
fbc65db7 |
1079 | if (cnt > 0 && !onscr_x) continue; // offscreen x\r |
d9fc2fe1 |
1080 | \r |
0fc0e241 |
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 |
d9fc2fe1 |
1087 | }\r |
cc68a136 |
1088 | }\r |
6d7acf9e |
1089 | \r |
d9fc2fe1 |
1090 | *pd++ = (width<<28)|(height<<24)|(hv<<16)|((unsigned short)sy);\r |
cc68a136 |
1091 | *pd++ = (sx<<16)|((unsigned short)code2);\r |
6d7acf9e |
1092 | \r |
cc68a136 |
1093 | // Find next sprite\r |
1094 | link=(code>>16)&0x7f;\r |
d9fc2fe1 |
1095 | if (!link) break; // End of sprites\r |
cc68a136 |
1096 | }\r |
d9fc2fe1 |
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 |
fbc65db7 |
1103 | printf("c%03i: %2i, %2i: ", u, HighLnSpr[u][0] & 0x7f, HighLnSpr[u][2]);\r |
d9fc2fe1 |
1104 | for (y = 0; y < HighLnSpr[u][0] & 0x7f; y++)\r |
fbc65db7 |
1105 | printf(" %i", HighLnSpr[u][y+3]);\r |
d9fc2fe1 |
1106 | printf("\n");\r |
1107 | }\r |
1108 | #endif\r |
cc68a136 |
1109 | }\r |
1110 | }\r |
1111 | \r |
283fec1b |
1112 | #ifndef _ASM_DRAW_C\r |
fbc65db7 |
1113 | static void DrawAllSprites(unsigned char *sprited, int prio, int sh)\r |
cc68a136 |
1114 | {\r |
fbc65db7 |
1115 | int rs = rendstatus;\r |
d9fc2fe1 |
1116 | unsigned char *p;\r |
97a7f774 |
1117 | int cnt;\r |
cc68a136 |
1118 | \r |
602133e1 |
1119 | if (rs & (PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES)) {\r |
d9fc2fe1 |
1120 | //elprintf(EL_STATUS, "PrepareSprites(%i)", (rs>>4)&1);\r |
602133e1 |
1121 | PrepareSprites(rs & PDRAW_DIRTY_SPRITES);\r |
1122 | rendstatus = rs & ~(PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES);\r |
cc68a136 |
1123 | }\r |
381eea9b |
1124 | \r |
fbc65db7 |
1125 | cnt = sprited[0] & 0x7f;\r |
ee4f03ae |
1126 | if (cnt == 0) return;\r |
cc68a136 |
1127 | \r |
fbc65db7 |
1128 | p = &sprited[3];\r |
cc68a136 |
1129 | \r |
ee4f03ae |
1130 | // Go through sprites backwards:\r |
1131 | for (cnt--; cnt >= 0; cnt--)\r |
1132 | {\r |
1133 | int offs;\r |
97a7f774 |
1134 | if ((p[cnt] >> 7) != prio) continue;\r |
ee4f03ae |
1135 | offs = (p[cnt]&0x7f) * 2;\r |
97a7f774 |
1136 | DrawSprite(HighPreSpr + offs, sh);\r |
cc68a136 |
1137 | }\r |
cc68a136 |
1138 | }\r |
1139 | \r |
1140 | \r |
1141 | // --------------------------------------------\r |
1142 | \r |
200772b7 |
1143 | void BackFill(int reg7, int sh)\r |
cc68a136 |
1144 | {\r |
b542be46 |
1145 | unsigned int back;\r |
cc68a136 |
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 |
b542be46 |
1153 | memset32((int *)(HighCol+8), back, 320/4);\r |
cc68a136 |
1154 | }\r |
1155 | #endif\r |
1156 | \r |
1157 | // --------------------------------------------\r |
1158 | \r |
1159 | unsigned short HighPal[0x100];\r |
1160 | \r |
b2305d08 |
1161 | #ifndef _ASM_DRAW_C\r |
e55f0cbb |
1162 | void PicoDoHighPal555(int sh)\r |
1163 | {\r |
19954be1 |
1164 | unsigned int *spal, *dpal;\r |
a39d8ba5 |
1165 | unsigned int t, i;\r |
e55f0cbb |
1166 | \r |
1167 | Pico.m.dirtyPal = 0;\r |
1168 | \r |
19954be1 |
1169 | spal = (void *)Pico.cram;\r |
1170 | dpal = (void *)HighPal;\r |
1171 | \r |
a39d8ba5 |
1172 | for (i = 0; i < 0x40 / 2; i++) {\r |
1173 | t = spal[i];\r |
e55f0cbb |
1174 | #ifdef USE_BGR555\r |
19954be1 |
1175 | t = ((t & 0x000e000e)<< 1) | ((t & 0x00e000e0)<<3) | ((t & 0x0e000e00)<<4);\r |
e55f0cbb |
1176 | #else\r |
19954be1 |
1177 | t = ((t & 0x000e000e)<<12) | ((t & 0x00e000e0)<<3) | ((t & 0x0e000e00)>>7);\r |
e55f0cbb |
1178 | #endif\r |
a39d8ba5 |
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 |
19954be1 |
1182 | dpal[i] = t;\r |
e55f0cbb |
1183 | }\r |
1184 | \r |
a39d8ba5 |
1185 | // norm: xxx0, sh: 0xxx, hi: 0xxx + 7\r |
e55f0cbb |
1186 | if (sh)\r |
1187 | {\r |
1188 | // shadowed pixels\r |
a39d8ba5 |
1189 | for (i = 0; i < 0x40 / 2; i++)\r |
1190 | dpal[0x40/2 | i] = dpal[0xc0/2 | i] = (dpal[i] >> 1) & 0x738e738e;\r |
e55f0cbb |
1191 | // hilighted pixels\r |
a39d8ba5 |
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 |
e55f0cbb |
1196 | }\r |
1197 | }\r |
1198 | }\r |
1199 | \r |
5a681086 |
1200 | #if 0\r |
974fdb5b |
1201 | static void FinalizeLineBGR444(int sh, int line)\r |
cc68a136 |
1202 | {\r |
1203 | unsigned short *pd=DrawLineDest;\r |
1204 | unsigned char *ps=HighCol+8;\r |
1205 | unsigned short *pal=Pico.cram;\r |
e5fa9817 |
1206 | int len, i, t, mask=0xff;\r |
cc68a136 |
1207 | \r |
1208 | if (Pico.video.reg[12]&1) {\r |
1209 | len = 320;\r |
1210 | } else {\r |
602133e1 |
1211 | if(!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32;\r |
cc68a136 |
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 |
f8af9634 |
1231 | if (!sh && (rendstatus & PDRAW_SPR_LO_ON_HI))\r |
e5fa9817 |
1232 | mask=0x3f; // accurate sprites\r |
1233 | \r |
cc68a136 |
1234 | for(i = 0; i < len; i++)\r |
e5fa9817 |
1235 | pd[i] = pal[ps[i] & mask];\r |
cc68a136 |
1236 | }\r |
5a681086 |
1237 | #endif\r |
cc68a136 |
1238 | \r |
1239 | \r |
5a681086 |
1240 | void FinalizeLine555(int sh, int line)\r |
cc68a136 |
1241 | {\r |
1242 | unsigned short *pd=DrawLineDest;\r |
1243 | unsigned char *ps=HighCol+8;\r |
1244 | unsigned short *pal=HighPal;\r |
e55f0cbb |
1245 | int len;\r |
cc68a136 |
1246 | \r |
e55f0cbb |
1247 | if (Pico.m.dirtyPal)\r |
1248 | PicoDoHighPal555(sh);\r |
cc68a136 |
1249 | \r |
70357ce5 |
1250 | if (Pico.video.reg[12]&1) {\r |
1251 | len = 320;\r |
1252 | } else {\r |
602133e1 |
1253 | if (!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32;\r |
70357ce5 |
1254 | len = 256;\r |
1255 | }\r |
1256 | \r |
e5fa9817 |
1257 | {\r |
70357ce5 |
1258 | #ifndef PSP\r |
e55f0cbb |
1259 | int i, mask=0xff;\r |
f8af9634 |
1260 | if (!sh && (rendstatus & PDRAW_SPR_LO_ON_HI))\r |
e5fa9817 |
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 |
70357ce5 |
1265 | #else\r |
70357ce5 |
1266 | extern void amips_clut(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);\r |
e5fa9817 |
1267 | extern void amips_clut_6bit(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);\r |
f8af9634 |
1268 | if (!sh && (rendstatus & PDRAW_SPR_LO_ON_HI))\r |
e5fa9817 |
1269 | amips_clut_6bit(pd, ps, pal, len);\r |
1270 | else amips_clut(pd, ps, pal, len);\r |
70357ce5 |
1271 | #endif\r |
e5fa9817 |
1272 | }\r |
cc68a136 |
1273 | }\r |
1274 | #endif\r |
1275 | \r |
974fdb5b |
1276 | static void FinalizeLine8bit(int sh, int line)\r |
cc68a136 |
1277 | {\r |
19954be1 |
1278 | unsigned char *pd = DrawLineDest;\r |
cc68a136 |
1279 | int len, rs = rendstatus;\r |
1280 | static int dirty_count;\r |
1281 | \r |
87b0845f |
1282 | if (!sh && Pico.m.dirtyPal == 1)\r |
602133e1 |
1283 | {\r |
cc68a136 |
1284 | // a hack for mid-frame palette changes\r |
602133e1 |
1285 | if (!(rs & PDRAW_SONIC_MODE))\r |
cc68a136 |
1286 | dirty_count = 1;\r |
1287 | else dirty_count++;\r |
602133e1 |
1288 | rs |= PDRAW_SONIC_MODE;\r |
cc68a136 |
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 |
19954be1 |
1300 | if (!(PicoOpt & POPT_DIS_32C_BORDER))\r |
1301 | pd += 32;\r |
cc68a136 |
1302 | len = 256;\r |
1303 | }\r |
1304 | \r |
602133e1 |
1305 | if (!sh && (rs & PDRAW_SONIC_MODE)) {\r |
cc68a136 |
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 |
974fdb5b |
1316 | static void (*FinalizeLine)(int sh, int line);\r |
cc68a136 |
1317 | \r |
b6d7ac70 |
1318 | // --------------------------------------------\r |
1319 | \r |
0fc0e241 |
1320 | static int DrawDisplay(int sh)\r |
cc68a136 |
1321 | {\r |
fbc65db7 |
1322 | unsigned char *sprited = &HighLnSpr[DrawScanline][0];\r |
cc68a136 |
1323 | struct PicoVideo *pvid=&Pico.video;\r |
1324 | int win=0,edge=0,hvwind=0;\r |
fbc65db7 |
1325 | int maxw,maxcells;\r |
cc68a136 |
1326 | \r |
602133e1 |
1327 | rendstatus &= ~(PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO);\r |
740da8c6 |
1328 | \r |
fbc65db7 |
1329 | if (pvid->reg[12]&1) {\r |
cc68a136 |
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 |
b6d7ac70 |
1339 | if (win&0x80) { if (DrawScanline>=edge) hvwind=1; }\r |
1340 | else { if (DrawScanline< edge) hvwind=1; }\r |
cc68a136 |
1341 | \r |
fbc65db7 |
1342 | if (!hvwind) // we might have a vertical window here\r |
1343 | {\r |
cc68a136 |
1344 | win=pvid->reg[0x11];\r |
1345 | edge=win&0x1f;\r |
7a7c6476 |
1346 | if (win&0x80) {\r |
1347 | if (!edge) hvwind=1;\r |
cc68a136 |
1348 | else if(edge < (maxcells>>1)) hvwind=2;\r |
1349 | } else {\r |
7a7c6476 |
1350 | if (!edge);\r |
cc68a136 |
1351 | else if(edge < (maxcells>>1)) hvwind=2;\r |
1352 | else hvwind=1;\r |
1353 | }\r |
1354 | }\r |
1355 | \r |
fbc65db7 |
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 |
7a7c6476 |
1363 | else if (hvwind == 2) {\r |
fbc65db7 |
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 |
cc68a136 |
1366 | } else\r |
fbc65db7 |
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 |
cc68a136 |
1381 | DrawWindow(0, maxcells>>1, 1, sh);\r |
7a7c6476 |
1382 | else if (hvwind == 2) {\r |
fbc65db7 |
1383 | if (HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, (win&0x80) ? edge<<4 : maxw);\r |
cc68a136 |
1384 | DrawWindow((win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 1, sh);\r |
1385 | } else\r |
947fb5f9 |
1386 | if (HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, maxw);\r |
fbc65db7 |
1387 | /* - sprites hi - */\r |
1388 | if (!(PicoDrawMask & PDRAW_SPRITES_HI_ON));\r |
1389 | else if (rendstatus & PDRAW_INTERLACE)\r |
1390 | DrawAllSpritesInterlace(1, sh);\r |
f8af9634 |
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 |
fbc65db7 |
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 |
cc68a136 |
1398 | \r |
740da8c6 |
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 |
b6d7ac70 |
1404 | printf("%i:%03i: a=%i, b=%i\n", Pico.m.frame_count, DrawScanline, a, b);\r |
740da8c6 |
1405 | }\r |
1406 | #endif\r |
1407 | \r |
cc68a136 |
1408 | return 0;\r |
1409 | }\r |
1410 | \r |
f8af9634 |
1411 | // MUST be called every frame\r |
eff55556 |
1412 | PICO_INTERNAL void PicoFrameStart(void)\r |
cc68a136 |
1413 | {\r |
5a681086 |
1414 | int offs = 8, lines = 224;\r |
ae87bffa |
1415 | \r |
cc68a136 |
1416 | // prepare to do this frame\r |
ee4f03ae |
1417 | rendstatus = 0;\r |
ae87bffa |
1418 | if ((Pico.video.reg[12] & 6) == 6)\r |
97a7f774 |
1419 | rendstatus |= PDRAW_INTERLACE; // interlace mode\r |
ae87bffa |
1420 | if (!(Pico.video.reg[12] & 1))\r |
1421 | rendstatus |= PDRAW_32_COLS;\r |
5a681086 |
1422 | if (Pico.video.reg[1] & 8) {\r |
1423 | offs = 0;\r |
ae87bffa |
1424 | lines = 240;\r |
5a681086 |
1425 | }\r |
e5fa9817 |
1426 | \r |
ae87bffa |
1427 | if (rendstatus != rendstatus_old || lines != rendlines) {\r |
1428 | rendlines = lines;\r |
b60b8745 |
1429 | // mode_change() might reset rendstatus_old by calling SetColorFormat\r |
ae87bffa |
1430 | emu_video_mode_change((lines == 240) ? 0 : 8,\r |
1431 | lines, (Pico.video.reg[12] & 1) ? 0 : 1);\r |
b60b8745 |
1432 | rendstatus_old = rendstatus;\r |
19954be1 |
1433 | }\r |
1434 | \r |
b7d64dbd |
1435 | HighCol = HighColBase + offs * HighColIncrement;\r |
1436 | DrawLineDest = (char *)DrawLineDestBase + offs * DrawLineDestIncrement;\r |
1437 | DrawScanline = 0;\r |
1438 | skip_next_line = 0;\r |
1439 | \r |
19954be1 |
1440 | if (PicoOpt & POPT_ALT_RENDERER)\r |
1441 | return;\r |
1442 | \r |
87b0845f |
1443 | if (Pico.m.dirtyPal)\r |
1444 | Pico.m.dirtyPal = 2; // reset dirty if needed\r |
cc68a136 |
1445 | PrepareSprites(1);\r |
cc68a136 |
1446 | }\r |
1447 | \r |
974fdb5b |
1448 | static void DrawBlankedLine(int line, int offs, int sh, int bgc)\r |
87b0845f |
1449 | {\r |
87b0845f |
1450 | if (PicoScanBegin != NULL)\r |
1451 | PicoScanBegin(line + offs);\r |
1452 | \r |
974fdb5b |
1453 | BackFill(bgc, sh);\r |
87b0845f |
1454 | \r |
1455 | if (FinalizeLine != NULL)\r |
974fdb5b |
1456 | FinalizeLine(sh, line);\r |
87b0845f |
1457 | \r |
1458 | if (PicoScanEnd != NULL)\r |
1459 | PicoScanEnd(line + offs);\r |
1460 | }\r |
1461 | \r |
974fdb5b |
1462 | static void PicoLine(int line, int offs, int sh, int bgc)\r |
cc68a136 |
1463 | {\r |
ae87bffa |
1464 | int skip = 0;\r |
1465 | \r |
87b0845f |
1466 | if (skip_next_line > 0) {\r |
1467 | skip_next_line--;\r |
1468 | return;\r |
1469 | }\r |
cc68a136 |
1470 | \r |
87b0845f |
1471 | DrawScanline = line;\r |
602133e1 |
1472 | if (PicoScanBegin != NULL)\r |
ae87bffa |
1473 | skip = PicoScanBegin(line + offs);\r |
1474 | \r |
1475 | if (skip) {\r |
1476 | skip_next_line = skip - 1;\r |
1477 | return;\r |
1478 | }\r |
602133e1 |
1479 | \r |
cc68a136 |
1480 | // Draw screen:\r |
974fdb5b |
1481 | BackFill(bgc, sh);\r |
cc68a136 |
1482 | if (Pico.video.reg[1]&0x40)\r |
0fc0e241 |
1483 | DrawDisplay(sh);\r |
cc68a136 |
1484 | \r |
ea8c405f |
1485 | if (FinalizeLine != NULL)\r |
974fdb5b |
1486 | FinalizeLine(sh, line);\r |
cc68a136 |
1487 | \r |
602133e1 |
1488 | if (PicoScanEnd != NULL)\r |
87b0845f |
1489 | skip_next_line = PicoScanEnd(line + offs);\r |
5a681086 |
1490 | \r |
1491 | HighCol += HighColIncrement;\r |
1492 | DrawLineDest = (char *)DrawLineDest + DrawLineDestIncrement;\r |
cc68a136 |
1493 | }\r |
1494 | \r |
b6d7ac70 |
1495 | void PicoDrawSync(int to, int blank_last_line)\r |
1496 | {\r |
87b0845f |
1497 | int line, offs = 0;\r |
974fdb5b |
1498 | int sh = (Pico.video.reg[0xC] & 8) >> 3; // shadow/hilight?\r |
1499 | int bgc = Pico.video.reg[7];\r |
87b0845f |
1500 | \r |
f6c49d38 |
1501 | pprof_start(draw);\r |
1502 | \r |
ae87bffa |
1503 | if (rendlines != 240)\r |
87b0845f |
1504 | offs = 8;\r |
1505 | \r |
1506 | for (line = DrawScanline; line < to; line++)\r |
b6d7ac70 |
1507 | {\r |
974fdb5b |
1508 | PicoLine(line, offs, sh, bgc);\r |
b6d7ac70 |
1509 | }\r |
1510 | \r |
b6d7ac70 |
1511 | // last line\r |
87b0845f |
1512 | if (line <= to)\r |
b6d7ac70 |
1513 | {\r |
1514 | if (blank_last_line)\r |
974fdb5b |
1515 | DrawBlankedLine(line, offs, sh, bgc);\r |
1516 | else PicoLine(line, offs, sh, bgc);\r |
87b0845f |
1517 | line++;\r |
b6d7ac70 |
1518 | }\r |
87b0845f |
1519 | DrawScanline = line;\r |
f6c49d38 |
1520 | \r |
1521 | pprof_end(draw);\r |
b6d7ac70 |
1522 | }\r |
cc68a136 |
1523 | \r |
5a681086 |
1524 | // also works for fast renderer\r |
1525 | void PicoDrawUpdateHighPal(void)\r |
1526 | {\r |
1527 | int sh = (Pico.video.reg[0xC] & 8) >> 3; // shadow/hilight?\r |
1528 | if (PicoOpt & POPT_ALT_RENDERER)\r |
1529 | sh = 0; // no s/h support\r |
1530 | \r |
1531 | PicoDoHighPal555(sh);\r |
1532 | if (rendstatus & PDRAW_SONIC_MODE) {\r |
1533 | // FIXME?\r |
1534 | memcpy(HighPal + 0x40, HighPal, 0x40*2);\r |
1535 | memcpy(HighPal + 0x80, HighPal, 0x40*2);\r |
1536 | }\r |
1537 | }\r |
1538 | \r |
41946d70 |
1539 | void PicoDrawSetOutFormat(pdso_t which, int use_32x_line_mode)\r |
cc68a136 |
1540 | {\r |
ea8c405f |
1541 | switch (which)\r |
1542 | {\r |
5a681086 |
1543 | case PDF_8BIT:\r |
1544 | FinalizeLine = FinalizeLine8bit;\r |
1545 | break;\r |
1546 | \r |
1547 | case PDF_RGB555:\r |
41946d70 |
1548 | if ((PicoAHW & PAHW_32X) && use_32x_line_mode)\r |
5a681086 |
1549 | FinalizeLine = FinalizeLine32xRGB555;\r |
1550 | else\r |
1551 | FinalizeLine = FinalizeLine555;\r |
1552 | break;\r |
1553 | \r |
1554 | default:\r |
1555 | FinalizeLine = NULL;\r |
1556 | break;\r |
ea8c405f |
1557 | }\r |
41946d70 |
1558 | PicoDrawSetOutFormat32x(which, use_32x_line_mode);\r |
5a681086 |
1559 | PicoDrawSetOutputMode4(which);\r |
205bc456 |
1560 | rendstatus_old = -1;\r |
5a681086 |
1561 | }\r |
1562 | \r |
e51e5983 |
1563 | // note: may be called on the middle of frame\r |
5a681086 |
1564 | void PicoDrawSetOutBuf(void *dest, int increment)\r |
1565 | {\r |
1566 | DrawLineDestBase = dest;\r |
1567 | DrawLineDestIncrement = increment;\r |
e51e5983 |
1568 | DrawLineDest = DrawLineDestBase + DrawScanline * increment;\r |
5a681086 |
1569 | }\r |
1570 | \r |
1571 | void PicoDrawSetInternalBuf(void *dest, int increment)\r |
1572 | {\r |
1573 | if (dest != NULL) {\r |
1574 | HighColBase = dest;\r |
1575 | HighColIncrement = increment;\r |
e51e5983 |
1576 | HighCol = HighColBase + DrawScanline * increment;\r |
5a681086 |
1577 | }\r |
1578 | else {\r |
1579 | HighColBase = DefHighCol;\r |
1580 | HighColIncrement = 0;\r |
1581 | }\r |
cc68a136 |
1582 | }\r |
ea8c405f |
1583 | \r |
f4750ee0 |
1584 | void PicoDrawSetCallbacks(int (*begin)(unsigned int num), int (*end)(unsigned int num))\r |
1585 | {\r |
1586 | PicoScanBegin = NULL;\r |
1587 | PicoScanEnd = NULL;\r |
1588 | PicoScan32xBegin = NULL;\r |
1589 | PicoScan32xEnd = NULL;\r |
1590 | \r |
1591 | if ((PicoAHW & PAHW_32X) && FinalizeLine != FinalizeLine32xRGB555) {\r |
1592 | PicoScan32xBegin = begin;\r |
1593 | PicoScan32xEnd = end;\r |
1594 | }\r |
1595 | else {\r |
1596 | PicoScanBegin = begin;\r |
1597 | PicoScanEnd = end;\r |
1598 | }\r |
1599 | }\r |