1 /*
2  * tile renderer
3  * (C) notaz, 2006-2008
4  *
5  * This work is licensed under the terms of MAME license.
6  * See COPYING file in the top-level directory.
7  */
8 
9 #include "pico_int.h"
10 
11 #define START_ROW  0 // which row of tiles to start rendering at?
12 #define END_ROW   28 // ..end
13 
14 #define VSRAM      0 // 2-cell vscroll (broken for line based hscroll)
15 #define INTERLACE  0 // interlace mode 2
16 
17 #define TILE_ROWS END_ROW-START_ROW
18 
19 // note: this is not implemented in ARM asm
20 #if defined(DRAW2_OVERRIDE_LINE_WIDTH)
21 #define LINE_WIDTH DRAW2_OVERRIDE_LINE_WIDTH
22 #else
23 #define LINE_WIDTH 328
24 #endif
25 
26 static unsigned char PicoDraw2FB_[(8+320) * (8+240+8) + 8];
27 
28 static int HighCache2A[2*41*(TILE_ROWS+1)+1+1]; // caches for high layers
29 static int HighCache2B[2*41*(TILE_ROWS+1)+1+1];
30 
31 unsigned short *PicoCramHigh=PicoMem.cram; // pointer to CRAM buff (0x40 shorts), converted to native device color (works only with 16bit for now)
32 void (*PicoPrepareCram)()=0;            // prepares PicoCramHigh for renderer to use
33 
34 
35 // stuff available in asm:
36 #ifdef _ASM_DRAW_C
37 void BackFillFull(void *dst, int reg7);
38 void DrawLayerFull(int plane, int *hcache, int planestart, int planeend,
39                    struct PicoEState *est);
40 void DrawTilesFromCacheF(int *hc, struct PicoEState *est);
41 void DrawWindowFull(int start, int end, int prio, struct PicoEState *est);
42 void DrawSpriteFull(unsigned int *sprite, struct PicoEState *est);
43 #else
44 
45 
TileXnormYnorm(unsigned char * pd,int addr,unsigned char pal,struct PicoVideo * pvid)46 static int TileXnormYnorm(unsigned char *pd,int addr,unsigned char pal, struct PicoVideo *pvid)
47 {
48 	unsigned int pack=0; unsigned int t=0, blank = 1;
49 	int i, inc=2;
50 
51 #if INTERLACE
52 	if ((pvid->reg[12]&6) == 6) inc = 4;
53 #endif
54 	for(i=8; i; i--, addr+=inc, pd += LINE_WIDTH) {
55 		pack=*(unsigned int *)(PicoMem.vram+addr); // Get 8 pixels
56 		if(!pack) continue;
57 
58 		t=pack&0x0000f000; if (t) pd[0]=(unsigned char)((t>>12)|pal);
59 		t=pack&0x00000f00; if (t) pd[1]=(unsigned char)((t>> 8)|pal);
60 		t=pack&0x000000f0; if (t) pd[2]=(unsigned char)((t>> 4)|pal);
61 		t=pack&0x0000000f; if (t) pd[3]=(unsigned char)((t    )|pal);
62 		t=pack&0xf0000000; if (t) pd[4]=(unsigned char)((t>>28)|pal);
63 		t=pack&0x0f000000; if (t) pd[5]=(unsigned char)((t>>24)|pal);
64 		t=pack&0x00f00000; if (t) pd[6]=(unsigned char)((t>>20)|pal);
65 		t=pack&0x000f0000; if (t) pd[7]=(unsigned char)((t>>16)|pal);
66 		blank = 0;
67 	}
68 
69 	return blank; // Tile blank?
70 }
71 
TileXflipYnorm(unsigned char * pd,int addr,unsigned char pal,struct PicoVideo * pvid)72 static int TileXflipYnorm(unsigned char *pd,int addr,unsigned char pal, struct PicoVideo *pvid)
73 {
74 	unsigned int pack=0; unsigned int t=0, blank = 1;
75 	int i, inc=2;
76 
77 #if INTERLACE
78 	if ((pvid->reg[12]&6) == 6) inc = 4;
79 #endif
80 	for(i=8; i; i--, addr+=inc, pd += LINE_WIDTH) {
81 		pack=*(unsigned int *)(PicoMem.vram+addr); // Get 8 pixels
82 		if(!pack) continue;
83 
84 		t=pack&0x000f0000; if (t) pd[0]=(unsigned char)((t>>16)|pal);
85 		t=pack&0x00f00000; if (t) pd[1]=(unsigned char)((t>>20)|pal);
86 		t=pack&0x0f000000; if (t) pd[2]=(unsigned char)((t>>24)|pal);
87 		t=pack&0xf0000000; if (t) pd[3]=(unsigned char)((t>>28)|pal);
88 		t=pack&0x0000000f; if (t) pd[4]=(unsigned char)((t    )|pal);
89 		t=pack&0x000000f0; if (t) pd[5]=(unsigned char)((t>> 4)|pal);
90 		t=pack&0x00000f00; if (t) pd[6]=(unsigned char)((t>> 8)|pal);
91 		t=pack&0x0000f000; if (t) pd[7]=(unsigned char)((t>>12)|pal);
92 		blank = 0;
93 	}
94 	return blank; // Tile blank?
95 }
96 
TileXnormYflip(unsigned char * pd,int addr,unsigned char pal,struct PicoVideo * pvid)97 static int TileXnormYflip(unsigned char *pd,int addr,unsigned char pal, struct PicoVideo *pvid)
98 {
99 	unsigned int pack=0; unsigned int t=0, blank = 1;
100 	int i, inc=2;
101 
102 #if INTERLACE
103 	if ((pvid->reg[12]&6) == 6) inc = 4, addr += 16;
104 #endif
105 	addr+=14;
106 	for(i=8; i; i--, addr-=inc, pd += LINE_WIDTH) {
107 		pack=*(unsigned int *)(PicoMem.vram+addr); // Get 8 pixels
108 		if(!pack) continue;
109 
110 		t=pack&0x0000f000; if (t) pd[0]=(unsigned char)((t>>12)|pal);
111 		t=pack&0x00000f00; if (t) pd[1]=(unsigned char)((t>> 8)|pal);
112 		t=pack&0x000000f0; if (t) pd[2]=(unsigned char)((t>> 4)|pal);
113 		t=pack&0x0000000f; if (t) pd[3]=(unsigned char)((t    )|pal);
114 		t=pack&0xf0000000; if (t) pd[4]=(unsigned char)((t>>28)|pal);
115 		t=pack&0x0f000000; if (t) pd[5]=(unsigned char)((t>>24)|pal);
116 		t=pack&0x00f00000; if (t) pd[6]=(unsigned char)((t>>20)|pal);
117 		t=pack&0x000f0000; if (t) pd[7]=(unsigned char)((t>>16)|pal);
118 		blank = 0;
119 	}
120 
121 	return blank; // Tile blank?
122 }
123 
TileXflipYflip(unsigned char * pd,int addr,unsigned char pal,struct PicoVideo * pvid)124 static int TileXflipYflip(unsigned char *pd,int addr,unsigned char pal, struct PicoVideo *pvid)
125 {
126 	unsigned int pack=0; unsigned int t=0, blank = 1;
127 	int i, inc=2;
128 
129 #if INTERLACE
130 	if ((pvid->reg[12]&6) == 6) inc = 4, addr += 16;
131 #endif
132 	addr+=14;
133 	for(i=8; i; i--, addr-=inc, pd += LINE_WIDTH) {
134 		pack=*(unsigned int *)(PicoMem.vram+addr); // Get 8 pixels
135 		if(!pack) continue;
136 
137 		t=pack&0x000f0000; if (t) pd[0]=(unsigned char)((t>>16)|pal);
138 		t=pack&0x00f00000; if (t) pd[1]=(unsigned char)((t>>20)|pal);
139 		t=pack&0x0f000000; if (t) pd[2]=(unsigned char)((t>>24)|pal);
140 		t=pack&0xf0000000; if (t) pd[3]=(unsigned char)((t>>28)|pal);
141 		t=pack&0x0000000f; if (t) pd[4]=(unsigned char)((t    )|pal);
142 		t=pack&0x000000f0; if (t) pd[5]=(unsigned char)((t>> 4)|pal);
143 		t=pack&0x00000f00; if (t) pd[6]=(unsigned char)((t>> 8)|pal);
144 		t=pack&0x0000f000; if (t) pd[7]=(unsigned char)((t>>12)|pal);
145 		blank = 0;
146 	}
147 	return blank; // Tile blank?
148 }
149 
150 
151 // start: (tile_start<<16)|row_start, end: [same]
DrawWindowFull(int start,int end,int prio,struct PicoEState * est)152 static void DrawWindowFull(int start, int end, int prio, struct PicoEState *est)
153 {
154 	struct PicoVideo *pvid=&Pico.video;
155 	int nametab, nametab_step, trow, tilex, blank=-1, code;
156 	unsigned char *scrpos = est->Draw2FB;
157 	int tile_start, tile_end; // in cells
158 
159 	// parse ranges
160 	tile_start = start>>16;
161 	tile_end = end>>16;
162 	start = start<<16>>16;
163 	end = end<<16>>16;
164 
165 	// Find name table line:
166 	if (pvid->reg[12]&1)
167 	{
168 		nametab=(pvid->reg[3]&0x3c)<<9; // 40-cell mode
169 		nametab_step = 1<<6;
170 	}
171 	else
172 	{
173 		nametab=(pvid->reg[3]&0x3e)<<9; // 32-cell mode
174 		nametab_step = 1<<5;
175 		if (!(PicoIn.opt&POPT_DIS_32C_BORDER))
176 			scrpos += 32;
177 	}
178 	nametab += nametab_step*start;
179 
180 	// check priority
181 	code=PicoMem.vram[nametab+tile_start];
182 	if ((code>>15) != prio) return; // hack: just assume that whole window uses same priority
183 
184 	scrpos+=8*LINE_WIDTH+8;
185 	scrpos+=8*LINE_WIDTH*(start-START_ROW);
186 
187 	// do a window until we reach planestart row
188 	for(trow = start; trow < end; trow++, nametab+=nametab_step) { // current tile row
189 		for (tilex=tile_start; tilex<tile_end; tilex++)
190 		{
191 			int code,addr,zero=0;
192 //			unsigned short *pal=NULL;
193 			unsigned char pal;
194 
195 			code=PicoMem.vram[nametab+tilex];
196 			if (code==blank) continue;
197 
198 			// Get tile address/2:
199 			addr=(code&0x7ff)<<4;
200 
201 //			pal=PicoCramHigh+((code>>9)&0x30);
202 			pal=(unsigned char)((code>>9)&0x30);
203 
204 			switch((code>>11)&3) {
205 				case 0: zero=TileXnormYnorm(scrpos+(tilex<<3),addr,pal,pvid); break;
206 				case 1: zero=TileXflipYnorm(scrpos+(tilex<<3),addr,pal,pvid); break;
207 				case 2: zero=TileXnormYflip(scrpos+(tilex<<3),addr,pal,pvid); break;
208 				case 3: zero=TileXflipYflip(scrpos+(tilex<<3),addr,pal,pvid); break;
209 			}
210 			if(zero) blank=code; // We know this tile is blank now
211 		}
212 
213 		scrpos += LINE_WIDTH*8;
214 	}
215 }
216 
217 
DrawLayerFull(int plane,int * hcache,int planestart,int planeend,struct PicoEState * est)218 static void DrawLayerFull(int plane, int *hcache, int planestart, int planeend,
219 			  struct PicoEState *est)
220 {
221 	struct PicoVideo *pvid=&Pico.video;
222 	static char shift[4]={5,6,5,7}; // 32,64 or 128 sized tilemaps
223 	int width, height, ymask, htab;
224 	int nametab, hscroll=0, vscroll, cells;
225 	unsigned char *scrpos;
226 	int blank=-1, xmask, nametab_row, trow;
227 
228 	// parse ranges
229 	cells = (planeend>>16)-(planestart>>16);
230 	planestart = planestart<<16>>16;
231 	planeend = planeend<<16>>16;
232 
233 	// Work out the Tiles to draw
234 
235 	htab=pvid->reg[13]<<9; // Horizontal scroll table address
236 //	if ( pvid->reg[11]&2)     htab+=Scanline<<1; // Offset by line
237 //	if ((pvid->reg[11]&1)==0) htab&=~0xf; // Offset by tile
238 	htab+=plane; // A or B
239 
240 	if(!(pvid->reg[11]&3)) { // full screen scroll
241 		// Get horizontal scroll value
242 		hscroll=PicoMem.vram[htab&0x7fff];
243 		htab = 0; // this marks that we don't have to update scroll value
244 	}
245 
246 	// Work out the name table size: 32 64 or 128 tiles (0-3)
247 	width=pvid->reg[16];
248 	height=(width>>4)&3; width&=3;
249 
250 	xmask=(1<<shift[width ])-1; // X Mask in tiles
251 	ymask=(height<<5)|0x1f;     // Y Mask in tiles
252 	if(width == 1)   ymask&=0x3f;
253 	else if(width>1) ymask =0x1f;
254 
255 	// Find name table:
256 	if (plane==0) nametab=(pvid->reg[2]&0x38)<< 9; // A
257 	else          nametab=(pvid->reg[4]&0x07)<<12; // B
258 
259 	scrpos = est->Draw2FB;
260 	if (!(pvid->reg[12]&1) && !(PicoIn.opt&POPT_DIS_32C_BORDER))
261 		scrpos += 32;
262 	scrpos+=8*LINE_WIDTH*(planestart-START_ROW);
263 
264 	if((pvid->reg[11]&4)||(PicoMem.vsram[plane]&7))
265 		planeend++; // we (may) have vertically clipped tiles due to vscroll, so we need 1 more row
266 	for(trow = planestart; trow < planeend; trow++) { // current tile row
267 		int cellc=cells,tilex,dx,vsidx=0;
268 
269 		// Get vertical scroll value:
270 		vscroll=PicoMem.vsram[plane];//&0x1ff;
271 #if VSRAM
272 		if (!(pvid->reg[12]&1) && (pvid->reg[11]&4)) // H32 + 2-cell mode
273 			vscroll=PicoMem.vsram[plane+0x20];//&0x1ff;
274 #endif
275 #if INTERLACE
276 		if ((pvid->reg[12]&6) == 6) vscroll >>= 1;
277 #endif
278 		nametab_row = nametab + (((trow+(vscroll>>3))&ymask)<<shift[width]); // pointer to nametable entries for this row
279 
280 		// Find the tile row in the name table
281 		//ts.line=(vscroll+Scanline)&ymask;
282 		//ts.nametab+=(ts.line>>3)<<shift[width];
283 
284 		// update hscroll if needed
285 		if(htab) {
286 			int htaddr=htab+(trow<<4);
287 			if(trow) htaddr-=(vscroll&7)<<1;
288 			hscroll=PicoMem.vram[htaddr&0x7fff];
289 		}
290 
291 		// Draw tiles across screen:
292 		tilex=(-hscroll)>>3;
293 		dx=((hscroll-1)&7)+1;
294 		if(dx != 8) cellc++, vsidx--; // have hscroll, do more cells
295 
296 		for (; cellc; dx+=8,tilex++,cellc--)
297 		{
298 			int code=0,addr=0,zero=0,scroff;
299 //			unsigned short *pal=NULL;
300 			unsigned char pal;
301 
302 #if VSRAM
303 			if ((pvid->reg[11]&4) && !(vsidx&1)) { // 2-cell mode
304 				vscroll=PicoMem.vsram[vsidx+plane];//&0x1ff;
305 #if INTERLACE
306 				if ((pvid->reg[12]&6) == 6) vscroll >>= 1;
307 #endif
308 				nametab_row = nametab + (((trow+(vscroll>>3))&ymask)<<shift[width]); // pointer to nametable entries for this row
309 			}
310 #endif
311 			vsidx++;
312 
313 			code=PicoMem.vram[nametab_row+(tilex&xmask)];
314 			if (code==blank) continue;
315 
316 			if (code>>15) { // high priority tile
317 				*hcache++ = code|(dx<<16)|(trow<<27); // cache it
318 				*hcache++ = 8-(vscroll&7); // push y-offset to tilecache
319 				continue;
320 			}
321 
322 			// Get tile address/2:
323 #if INTERLACE
324 		if ((pvid->reg[12]&6) == 6)
325 			addr=(code&0x3ff)<<5;
326 		else
327 #endif
328 			addr=(code&0x7ff)<<4;
329 
330 //			pal=PicoCramHigh+((code>>9)&0x30);
331 			pal=(unsigned char)((code>>9)&0x30);
332 
333 			scroff=(8-(vscroll&7))*LINE_WIDTH;
334 			switch((code>>11)&3) {
335 				case 0: zero=TileXnormYnorm(scrpos+scroff+dx,addr,pal,pvid); break;
336 				case 1: zero=TileXflipYnorm(scrpos+scroff+dx,addr,pal,pvid); break;
337 				case 2: zero=TileXnormYflip(scrpos+scroff+dx,addr,pal,pvid); break;
338 				case 3: zero=TileXflipYflip(scrpos+scroff+dx,addr,pal,pvid); break;
339 			}
340 			if(zero) blank=code; // We know this tile is blank now
341 		}
342 
343 		scrpos += LINE_WIDTH*8;
344 	}
345 
346 	*hcache = 0; // terminate cache
347 }
348 
349 
DrawTilesFromCacheF(int * hc,struct PicoEState * est)350 static void DrawTilesFromCacheF(int *hc, struct PicoEState *est)
351 {
352 	int code, addr, zero = 0, vscroll;
353 	unsigned int prevy=0xFFFFFFFF;
354 //	unsigned short *pal;
355 	unsigned char pal;
356 	short blank=-1; // The tile we know is blank
357 	unsigned char *scrpos = est->Draw2FB, *pd = 0;
358 
359 	if (!(Pico.video.reg[12]&1) && !(PicoIn.opt&POPT_DIS_32C_BORDER))
360 		scrpos += 32;
361 
362 	while((code=*hc++)) {
363 		vscroll=(*hc++)*LINE_WIDTH - START_ROW*LINE_WIDTH*8;
364 		if((short)code == blank) continue;
365 
366 		// y pos
367 		if(((unsigned)code>>27) != prevy) {
368 			prevy = (unsigned)code>>27;
369 			pd = scrpos + prevy*LINE_WIDTH*8;
370 		}
371 
372 		// Get tile address/2:
373 #if INTERLACE
374 		if ((Pico.video.reg[12]&6) == 6)
375 		addr=(code&0x3ff)<<5;
376 		else
377 #endif
378 		addr=(code&0x7ff)<<4;
379 //		pal=PicoCramHigh+((code>>9)&0x30);
380 		pal=(unsigned char)((code>>9)&0x30);
381 
382 		switch((code>>11)&3) {
383 			case 0: zero=TileXnormYnorm(pd+vscroll+((code>>16)&0x1ff),addr,pal,&Pico.video); break;
384 			case 1: zero=TileXflipYnorm(pd+vscroll+((code>>16)&0x1ff),addr,pal,&Pico.video); break;
385 			case 2: zero=TileXnormYflip(pd+vscroll+((code>>16)&0x1ff),addr,pal,&Pico.video); break;
386 			case 3: zero=TileXflipYflip(pd+vscroll+((code>>16)&0x1ff),addr,pal,&Pico.video); break;
387 		}
388 
389 		if(zero) blank=(short)code;
390 	}
391 }
392 
393 
394 // sx and sy are coords of virtual screen with 8pix borders on top and on left
DrawSpriteFull(unsigned int * sprite,struct PicoEState * est)395 static void DrawSpriteFull(unsigned int *sprite, struct PicoEState *est)
396 {
397 	int width=0,height=0;
398 //	unsigned short *pal=NULL;
399 	unsigned char pal;
400 	int tile,code,tdeltax,tdeltay;
401 	unsigned char *scrpos;
402 	int sx, sy;
403 
404 	sy=sprite[0];
405 	height=sy>>24;
406 #if INTERLACE
407 	if ((Pico.video.reg[12]&6) == 6)
408 	sy = ((sy>>1)&0x1ff)-0x78;
409 	else
410 #endif
411 	sy=(sy&0x1ff)-0x78; // Y
412 	width=(height>>2)&3; height&=3;
413 	width++; height++; // Width and height in tiles
414 
415 	code=sprite[1];
416 	sx=((code>>16)&0x1ff)-0x78; // X
417 
418 	tile=code&0x7ff; // Tile number
419 	tdeltax=height; // Delta to increase tile by going right
420 	tdeltay=1;      // Delta to increase tile by going down
421 	if (code&0x1000) { tile+=tdeltax-1; tdeltay=-tdeltay; } // Flip Y
422 	if (code&0x0800) { tile+=tdeltax*(width-1); tdeltax=-tdeltax; } // Flip X
423 
424 	//delta<<=4; // Delta of address
425 //	pal=PicoCramHigh+((code>>9)&0x30); // Get palette pointer
426 	pal=(unsigned char)((code>>9)&0x30);
427 
428 	// goto first vertically visible tile
429 	while(sy <= START_ROW*8) { sy+=8; tile+=tdeltay; height--; }
430 
431 	scrpos = est->Draw2FB;
432 	if (!(Pico.video.reg[12]&1) && !(PicoIn.opt&POPT_DIS_32C_BORDER))
433 		scrpos += 32;
434 	scrpos+=(sy-START_ROW*8)*LINE_WIDTH;
435 
436 	for (; height > 0; height--, sy+=8, tile+=tdeltay)
437 	{
438 		int w = width, x=sx, t=tile;
439 		int s=4;
440 
441 		if(sy >= END_ROW*8+8) return; // offscreen
442 
443 		for (; w; w--,x+=8,t+=tdeltax)
444 		{
445 			if(x<=0)   continue;
446 			if(x>=328) break; // Offscreen
447 
448 			t&=0x7fff; // Clip tile address
449 #if INTERLACE
450 	if ((Pico.video.reg[12]&6) == 6) s=5;
451 #endif
452 			switch((code>>11)&3) {
453 				case 0: TileXnormYnorm(scrpos+x,t<<s,pal,&Pico.video); break;
454 				case 1: TileXflipYnorm(scrpos+x,t<<s,pal,&Pico.video); break;
455 				case 2: TileXnormYflip(scrpos+x,t<<s,pal,&Pico.video); break;
456 				case 3: TileXflipYflip(scrpos+x,t<<s,pal,&Pico.video); break;
457 			}
458 		}
459 
460 		scrpos+=8*LINE_WIDTH;
461 	}
462 }
463 #endif
464 
465 
DrawAllSpritesFull(int prio,int maxwidth)466 static void DrawAllSpritesFull(int prio, int maxwidth)
467 {
468 	struct PicoVideo *pvid=&Pico.video;
469 	int table=0,maskrange=0;
470 	int i,u,link=0;
471 	unsigned int *sprites[80]; // Sprites
472 	int y_min=START_ROW*8, y_max=END_ROW*8; // for a simple sprite masking
473 	int max_sprites = Pico.video.reg[12]&1 ? 80 : 64;
474 
475 	table=pvid->reg[5]&0x7f;
476 	if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode
477 	table<<=8; // Get sprite table address/2
478 
479 	for (i = u = 0; u < max_sprites && link < max_sprites; u++)
480 	{
481 		unsigned int *sprite=NULL;
482 		int code, code2, sx, sy, height;
483 
484 		sprite=(unsigned int *)(PicoMem.vram+((table+(link<<2))&0x7ffc)); // Find sprite
485 
486 		// get sprite info
487 		code = sprite[0];
488 
489 		// check if it is not hidden vertically
490 #if INTERLACE
491 		if ((pvid->reg[12]&6) == 6)
492 		sy = ((code>>1)&0x1ff)-0x80;
493 		else
494 #endif
495 		sy = (code&0x1ff)-0x80;
496 		height = (((code>>24)&3)+1)<<3;
497 		if(sy+height <= y_min || sy > y_max) goto nextsprite;
498 
499 		// masking sprite?
500 		code2=sprite[1];
501 		sx = (code2>>16)&0x1ff;
502 		if(!sx) {
503 			int to = sy+height; // sy ~ from
504 			if(maskrange) {
505 				// try to merge with previous range
506 				if((maskrange>>16)+1 >= sy && (maskrange>>16) <= to && (maskrange&0xffff) < sy) sy = (maskrange&0xffff);
507 				else if((maskrange&0xffff)-1 <= to && (maskrange&0xffff) >= sy && (maskrange>>16) > to) to = (maskrange>>16);
508 			}
509 			// support only very simple masking (top and bottom of screen)
510 			if(sy <= y_min && to+1 > y_min) y_min = to+1;
511 			else if(to >= y_max && sy-1 < y_max) y_max = sy-1;
512 			else maskrange=sy|(to<<16);
513 
514 			goto nextsprite;
515 		}
516 
517 		// priority
518 		if(((code2>>15)&1) != prio) goto nextsprite; // wrong priority
519 
520 		// check if sprite is not hidden horizontally
521 		sx -= 0x78; // Get X coordinate + 8
522 		if(sx <= -8*3 || sx >= maxwidth) goto nextsprite;
523 
524 		// sprite is good, save it's index
525 		sprites[i++]=sprite;
526 
527 		nextsprite:
528 		// Find next sprite
529 		link=(code>>16)&0x7f;
530 		if(!link) break; // End of sprites
531 	}
532 
533 	// Go through sprites backwards:
534 	for (i--; i >= 0; i--)
535 	{
536 		DrawSpriteFull(sprites[i], &Pico.est);
537 	}
538 }
539 
540 #ifndef _ASM_DRAW_C
BackFillFull(void * dst,int reg7)541 static void BackFillFull(void *dst, int reg7)
542 {
543 	unsigned int back;
544 
545 	// Start with a background color:
546 	back=reg7&0x3f;
547 	back|=back<<8;
548 	back|=back<<16;
549 
550 	memset32(dst, back, LINE_WIDTH*(8+(END_ROW-START_ROW)*8)/4);
551 }
552 #endif
553 
DrawDisplayFull(void)554 static void DrawDisplayFull(void)
555 {
556 	struct PicoEState *est = &Pico.est;
557 	struct PicoVideo *pvid=&Pico.video;
558 	int win, edge=0, hvwin=0; // LSb->MSb: hwin&plane, vwin&plane, full
559 	int planestart=START_ROW, planeend=END_ROW; // plane A start/end when window shares display with plane A (in tile rows or columns)
560 	int winstart=START_ROW, winend=END_ROW;     // same for window
561 	int maxw, maxcolc; // max width and col cells
562 
563 	if(pvid->reg[12]&1) {
564 		maxw = 328; maxcolc = 40;
565 	} else {
566 		maxw = 264; maxcolc = 32;
567 	}
568 
569 	// 32C border for centering? (for asm)
570 	est->rendstatus &= ~PDRAW_BORDER_32;
571 	if ((est->rendstatus&PDRAW_32_COLS) && !(PicoIn.opt&POPT_DIS_32C_BORDER))
572 		est->rendstatus |= PDRAW_BORDER_32;
573 
574 	// horizontal window?
575 	if ((win=pvid->reg[0x12]))
576 	{
577 		hvwin=1; // hwindow shares display with plane A
578 		edge=win&0x1f;
579 		if(win == 0x80) {
580 			// fullscreen window
581 			hvwin=4;
582 		} else if(win < 0x80) {
583 			// window on the top
584 			     if(edge <= START_ROW) hvwin=0; // window not visible in our drawing region
585 			else if(edge >= END_ROW)   hvwin=4;
586 			else planestart = winend = edge;
587 		} else if(win > 0x80) {
588 			// window at the bottom
589 			if(edge >= END_ROW) hvwin=0;
590 			else planeend = winstart = edge;
591 		}
592 	}
593 
594 	// check for vertical window, but only if win is not fullscreen
595 	if (hvwin != 4)
596 	{
597 		win=pvid->reg[0x11];
598 		edge=win&0x1f;
599 		if (win&0x80) {
600 			if(!edge) hvwin=4;
601 			else if(edge < (maxcolc>>1)) {
602 				// window is on the right
603 				hvwin|=2;
604 				planeend|=edge<<17;
605 				winstart|=edge<<17;
606 				winend|=maxcolc<<16;
607 			}
608 		} else {
609 			if(edge >= (maxcolc>>1)) hvwin=4;
610 			else if(edge) {
611 				// window is on the left
612 				hvwin|=2;
613 				winend|=edge<<17;
614 				planestart|=edge<<17;
615 				planeend|=maxcolc<<16;
616 			}
617 		}
618 	}
619 
620 	if (hvwin==1) { winend|=maxcolc<<16; planeend|=maxcolc<<16; }
621 
622 	HighCache2A[0] = HighCache2B[0] = 0;
623 	if (!(pvid->debug_p & PVD_KILL_B))
624 		DrawLayerFull(1, HighCache2B, START_ROW, (maxcolc<<16)|END_ROW, est);
625 	if (!(pvid->debug_p & PVD_KILL_A)) switch (hvwin)
626 	{
627 		case 4:
628 		// fullscreen window
629 		DrawWindowFull(START_ROW, (maxcolc<<16)|END_ROW, 0, est);
630 		break;
631 
632 		case 3:
633 		// we have plane A and both v and h windows
634 		DrawLayerFull(0, HighCache2A, planestart, planeend, est);
635 		DrawWindowFull( winstart&~0xff0000, (winend&~0xff0000)|(maxcolc<<16), 0, est); // h
636 		DrawWindowFull((winstart&~0xff)|START_ROW, (winend&~0xff)|END_ROW, 0, est);    // v
637 		break;
638 
639 		case 2:
640 		case 1:
641 		// both window and plane A visible, window is vertical XOR horizontal
642 		DrawLayerFull(0, HighCache2A, planestart, planeend, est);
643 		DrawWindowFull(winstart, winend, 0, est);
644 		break;
645 
646 		default:
647 		// fullscreen plane A
648 		DrawLayerFull(0, HighCache2A, START_ROW, (maxcolc<<16)|END_ROW, est);
649 		break;
650 	}
651 	if (!(pvid->debug_p & PVD_KILL_S_LO))
652 		DrawAllSpritesFull(0, maxw);
653 
654 	if (HighCache2B[0]) DrawTilesFromCacheF(HighCache2B, est);
655 	if (HighCache2A[0]) DrawTilesFromCacheF(HighCache2A, est);
656 	if (!(pvid->debug_p & PVD_KILL_A)) switch (hvwin)
657 	{
658 		case 4:
659 		// fullscreen window
660 		DrawWindowFull(START_ROW, (maxcolc<<16)|END_ROW, 1, est);
661 		break;
662 
663 		case 3:
664 		// we have plane A and both v and h windows
665 		DrawWindowFull( winstart&~0xff0000, (winend&~0xff0000)|(maxcolc<<16), 1, est); // h
666 		DrawWindowFull((winstart&~0xff)|START_ROW, (winend&~0xff)|END_ROW, 1, est);    // v
667 		break;
668 
669 		case 2:
670 		case 1:
671 		// both window and plane A visible, window is vertical XOR horizontal
672 		DrawWindowFull(winstart, winend, 1, est);
673 		break;
674 	}
675 	if (!(pvid->debug_p & PVD_KILL_S_HI))
676 		DrawAllSpritesFull(1, maxw);
677 }
678 
679 
PicoFrameFull()680 PICO_INTERNAL void PicoFrameFull()
681 {
682 	pprof_start(draw);
683 
684 	// prepare cram?
685 	if (PicoPrepareCram) PicoPrepareCram();
686 
687 	// Draw screen:
688 	BackFillFull(Pico.est.Draw2FB, Pico.video.reg[7]);
689 	if (Pico.video.reg[1] & 0x40)
690 		DrawDisplayFull();
691 
692 	pprof_end(draw);
693 }
694 
PicoDraw2SetOutBuf(void * dest)695 void PicoDraw2SetOutBuf(void *dest)
696 {
697 	if (dest)
698 		Pico.est.Draw2FB = dest;
699 	else
700 		Pico.est.Draw2FB = PicoDraw2FB_;
701 }
702 
PicoDraw2Init(void)703 void PicoDraw2Init(void)
704 {
705 	PicoDraw2SetOutBuf(NULL);
706 }
707