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