1 // This is part of Pico Library
\r
3 // (c) Copyright 2007, Grazvydas "notaz" Ignotas
\r
4 // Free for non-commercial use.
\r
6 // For commercial use, separate licencing terms must be obtained.
\r
9 // this is a frame-based renderer, alternative to Dave's line based which is in Draw.c
\r
12 #include "PicoInt.h"
\r
14 // port_config.h include must define these 2 defines:
\r
15 // #define START_ROW 1 // which row of tiles to start rendering at?
\r
16 // #define END_ROW 27 // ..end
\r
17 // one row means 8 pixels. If above example was used, (27-1)*8=208 lines would be rendered.
\r
19 #define TILE_ROWS END_ROW-START_ROW
\r
23 // note: this is not implemented in ARM asm
\r
24 #if defined(DRAW2_OVERRIDE_LINE_WIDTH)
\r
25 #define LINE_WIDTH DRAW2_OVERRIDE_LINE_WIDTH
\r
27 #define LINE_WIDTH 328
\r
32 static int HighCache2A[41*(TILE_ROWS+1)+1+1]; // caches for high layers
\r
33 static int HighCache2B[41*(TILE_ROWS+1)+1+1];
\r
35 unsigned short *PicoCramHigh=Pico.cram; // pointer to CRAM buff (0x40 shorts), converted to native device color (works only with 16bit for now)
\r
36 void (*PicoPrepareCram)()=0; // prepares PicoCramHigh for renderer to use
\r
39 // stuff available in asm:
\r
41 void BackFillFull(int reg7);
\r
42 void DrawLayerFull(int plane, int *hcache, int planestart, int planeend);
\r
43 void DrawTilesFromCacheF(int *hc);
\r
44 void DrawWindowFull(int start, int end, int prio);
\r
45 void DrawSpriteFull(unsigned int *sprite);
\r
49 static int TileXnormYnorm(unsigned char *pd,int addr,unsigned char pal)
\r
51 unsigned int pack=0; unsigned int t=0, blank = 1;
\r
54 for(i=8; i; i--, addr+=2, pd += LINE_WIDTH) {
\r
55 pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
58 t=pack&0x0000f000; if (t) pd[0]=(unsigned char)((t>>12)|pal);
\r
59 t=pack&0x00000f00; if (t) pd[1]=(unsigned char)((t>> 8)|pal);
\r
60 t=pack&0x000000f0; if (t) pd[2]=(unsigned char)((t>> 4)|pal);
\r
61 t=pack&0x0000000f; if (t) pd[3]=(unsigned char)((t )|pal);
\r
62 t=pack&0xf0000000; if (t) pd[4]=(unsigned char)((t>>28)|pal);
\r
63 t=pack&0x0f000000; if (t) pd[5]=(unsigned char)((t>>24)|pal);
\r
64 t=pack&0x00f00000; if (t) pd[6]=(unsigned char)((t>>20)|pal);
\r
65 t=pack&0x000f0000; if (t) pd[7]=(unsigned char)((t>>16)|pal);
\r
69 return blank; // Tile blank?
\r
72 static int TileXflipYnorm(unsigned char *pd,int addr,unsigned char pal)
\r
74 unsigned int pack=0; unsigned int t=0, blank = 1;
\r
77 for(i=8; i; i--, addr+=2, pd += LINE_WIDTH) {
\r
78 pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
81 t=pack&0x000f0000; if (t) pd[0]=(unsigned char)((t>>16)|pal);
\r
82 t=pack&0x00f00000; if (t) pd[1]=(unsigned char)((t>>20)|pal);
\r
83 t=pack&0x0f000000; if (t) pd[2]=(unsigned char)((t>>24)|pal);
\r
84 t=pack&0xf0000000; if (t) pd[3]=(unsigned char)((t>>28)|pal);
\r
85 t=pack&0x0000000f; if (t) pd[4]=(unsigned char)((t )|pal);
\r
86 t=pack&0x000000f0; if (t) pd[5]=(unsigned char)((t>> 4)|pal);
\r
87 t=pack&0x00000f00; if (t) pd[6]=(unsigned char)((t>> 8)|pal);
\r
88 t=pack&0x0000f000; if (t) pd[7]=(unsigned char)((t>>12)|pal);
\r
91 return blank; // Tile blank?
\r
94 static int TileXnormYflip(unsigned char *pd,int addr,unsigned char pal)
\r
96 unsigned int pack=0; unsigned int t=0, blank = 1;
\r
100 for(i=8; i; i--, addr-=2, pd += LINE_WIDTH) {
\r
101 pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
102 if(!pack) continue;
\r
104 t=pack&0x0000f000; if (t) pd[0]=(unsigned char)((t>>12)|pal);
\r
105 t=pack&0x00000f00; if (t) pd[1]=(unsigned char)((t>> 8)|pal);
\r
106 t=pack&0x000000f0; if (t) pd[2]=(unsigned char)((t>> 4)|pal);
\r
107 t=pack&0x0000000f; if (t) pd[3]=(unsigned char)((t )|pal);
\r
108 t=pack&0xf0000000; if (t) pd[4]=(unsigned char)((t>>28)|pal);
\r
109 t=pack&0x0f000000; if (t) pd[5]=(unsigned char)((t>>24)|pal);
\r
110 t=pack&0x00f00000; if (t) pd[6]=(unsigned char)((t>>20)|pal);
\r
111 t=pack&0x000f0000; if (t) pd[7]=(unsigned char)((t>>16)|pal);
\r
115 return blank; // Tile blank?
\r
118 static int TileXflipYflip(unsigned char *pd,int addr,unsigned char pal)
\r
120 unsigned int pack=0; unsigned int t=0, blank = 1;
\r
124 for(i=8; i; i--, addr-=2, pd += LINE_WIDTH) {
\r
125 pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
\r
126 if(!pack) continue;
\r
128 t=pack&0x000f0000; if (t) pd[0]=(unsigned char)((t>>16)|pal);
\r
129 t=pack&0x00f00000; if (t) pd[1]=(unsigned char)((t>>20)|pal);
\r
130 t=pack&0x0f000000; if (t) pd[2]=(unsigned char)((t>>24)|pal);
\r
131 t=pack&0xf0000000; if (t) pd[3]=(unsigned char)((t>>28)|pal);
\r
132 t=pack&0x0000000f; if (t) pd[4]=(unsigned char)((t )|pal);
\r
133 t=pack&0x000000f0; if (t) pd[5]=(unsigned char)((t>> 4)|pal);
\r
134 t=pack&0x00000f00; if (t) pd[6]=(unsigned char)((t>> 8)|pal);
\r
135 t=pack&0x0000f000; if (t) pd[7]=(unsigned char)((t>>12)|pal);
\r
138 return blank; // Tile blank?
\r
142 // start: (tile_start<<16)|row_start, end: [same]
\r
143 static void DrawWindowFull(int start, int end, int prio)
\r
145 struct PicoVideo *pvid=&Pico.video;
\r
146 int nametab, nametab_step, trow, tilex, blank=-1, code;
\r
147 unsigned char *scrpos = PicoDraw2FB;
\r
148 int tile_start, tile_end; // in cells
\r
151 tile_start = start>>16;
\r
152 tile_end = end>>16;
\r
153 start = start<<16>>16;
\r
156 // Find name table line:
\r
157 if (pvid->reg[12]&1)
\r
159 nametab=(pvid->reg[3]&0x3c)<<9; // 40-cell mode
\r
160 nametab_step = 1<<6;
\r
164 nametab=(pvid->reg[3]&0x3e)<<9; // 32-cell mode
\r
165 nametab_step = 1<<5;
\r
167 nametab += nametab_step*start;
\r
170 code=Pico.vram[nametab+tile_start];
\r
171 if ((code>>15) != prio) return; // hack: just assume that whole window uses same priority
\r
173 scrpos+=8*LINE_WIDTH+8;
\r
174 scrpos+=8*LINE_WIDTH*(start-START_ROW);
\r
176 // do a window until we reach planestart row
\r
177 for(trow = start; trow < end; trow++, nametab+=nametab_step) { // current tile row
\r
178 for (tilex=tile_start; tilex<tile_end; tilex++)
\r
180 int code,addr,zero=0;
\r
181 // unsigned short *pal=NULL;
\r
184 code=Pico.vram[nametab+tilex];
\r
185 if (code==blank) continue;
\r
187 // Get tile address/2:
\r
188 addr=(code&0x7ff)<<4;
\r
190 // pal=PicoCramHigh+((code>>9)&0x30);
\r
191 pal=(unsigned char)((code>>9)&0x30);
\r
193 switch((code>>11)&3) {
\r
194 case 0: zero=TileXnormYnorm(scrpos+(tilex<<3),addr,pal); break;
\r
195 case 1: zero=TileXflipYnorm(scrpos+(tilex<<3),addr,pal); break;
\r
196 case 2: zero=TileXnormYflip(scrpos+(tilex<<3),addr,pal); break;
\r
197 case 3: zero=TileXflipYflip(scrpos+(tilex<<3),addr,pal); break;
\r
199 if(zero) blank=code; // We know this tile is blank now
\r
202 scrpos += LINE_WIDTH*8;
\r
207 static void DrawLayerFull(int plane, int *hcache, int planestart, int planeend)
\r
209 struct PicoVideo *pvid=&Pico.video;
\r
210 static char shift[4]={5,6,6,7}; // 32,64 or 128 sized tilemaps
\r
211 int width, height, ymask, htab;
\r
212 int nametab, hscroll=0, vscroll, cells;
\r
213 unsigned char *scrpos;
\r
214 int blank=-1, xmask, nametab_row, trow;
\r
217 cells = (planeend>>16)-(planestart>>16);
\r
218 planestart = planestart<<16>>16;
\r
219 planeend = planeend<<16>>16;
\r
221 // Work out the Tiles to draw
\r
223 htab=pvid->reg[13]<<9; // Horizontal scroll table address
\r
224 // if ( pvid->reg[11]&2) htab+=Scanline<<1; // Offset by line
\r
225 // if ((pvid->reg[11]&1)==0) htab&=~0xf; // Offset by tile
\r
226 htab+=plane; // A or B
\r
228 if(!(pvid->reg[11]&3)) { // full screen scroll
\r
229 // Get horizontal scroll value
\r
230 hscroll=Pico.vram[htab&0x7fff];
\r
231 htab = 0; // this marks that we don't have to update scroll value
\r
234 // Work out the name table size: 32 64 or 128 tiles (0-3)
\r
235 width=pvid->reg[16];
\r
236 height=(width>>4)&3; width&=3;
\r
238 xmask=(1<<shift[width ])-1; // X Mask in tiles
\r
239 ymask=(height<<5)|0x1f; // Y Mask in tiles
\r
240 if(width == 1) ymask&=0x3f;
\r
241 else if(width>1) ymask =0x1f;
\r
243 // Find name table:
\r
244 if (plane==0) nametab=(pvid->reg[2]&0x38)<< 9; // A
\r
245 else nametab=(pvid->reg[4]&0x07)<<12; // B
\r
247 scrpos = PicoDraw2FB;
\r
248 scrpos+=8*LINE_WIDTH*(planestart-START_ROW);
\r
250 // Get vertical scroll value:
\r
251 vscroll=Pico.vsram[plane]&0x1ff;
\r
252 scrpos+=(8-(vscroll&7))*LINE_WIDTH;
\r
253 if(vscroll&7) planeend++; // we have vertically clipped tiles due to vscroll, so we need 1 more row
\r
255 *hcache++ = 8-(vscroll&7); // push y-offset to tilecache
\r
258 for(trow = planestart; trow < planeend; trow++) { // current tile row
\r
259 int cellc=cells,tilex,dx;
\r
261 // Find the tile row in the name table
\r
262 //ts.line=(vscroll+Scanline)&ymask;
\r
263 //ts.nametab+=(ts.line>>3)<<shift[width];
\r
264 nametab_row = nametab + (((trow+(vscroll>>3))&ymask)<<shift[width]); // pointer to nametable entries for this row
\r
266 // update hscroll if needed
\r
268 int htaddr=htab+(trow<<4);
\r
269 if(trow) htaddr-=(vscroll&7)<<1;
\r
270 hscroll=Pico.vram[htaddr&0x7fff];
\r
273 // Draw tiles across screen:
\r
274 tilex=(-hscroll)>>3;
\r
275 dx=((hscroll-1)&7)+1;
\r
276 if(dx != 8) cellc++; // have hscroll, do more cells
\r
278 for (; cellc; dx+=8,tilex++,cellc--)
\r
280 int code=0,addr=0,zero=0;
\r
281 // unsigned short *pal=NULL;
\r
284 code=Pico.vram[nametab_row+(tilex&xmask)];
\r
285 if (code==blank) continue;
\r
288 if (code>>15) { // high priority tile
\r
289 *hcache++ = code|(dx<<16)|(trow<<27); // cache it
\r
291 if ((code>>15) != currpri) {
\r
296 // Get tile address/2:
\r
297 addr=(code&0x7ff)<<4;
\r
299 // pal=PicoCramHigh+((code>>9)&0x30);
\r
300 pal=(unsigned char)((code>>9)&0x30);
\r
302 switch((code>>11)&3) {
\r
303 case 0: zero=TileXnormYnorm(scrpos+dx,addr,pal); break;
\r
304 case 1: zero=TileXflipYnorm(scrpos+dx,addr,pal); break;
\r
305 case 2: zero=TileXnormYflip(scrpos+dx,addr,pal); break;
\r
306 case 3: zero=TileXflipYflip(scrpos+dx,addr,pal); break;
\r
308 if(zero) blank=code; // We know this tile is blank now
\r
311 scrpos += LINE_WIDTH*8;
\r
314 *hcache = 0; // terminate cache
\r
318 static void DrawTilesFromCacheF(int *hc)
\r
320 int code, addr, zero = 0;
\r
321 unsigned int prevy=0xFFFFFFFF;
\r
322 // unsigned short *pal;
\r
324 short blank=-1; // The tile we know is blank
\r
325 unsigned char *scrpos = PicoDraw2FB, *pd = 0;
\r
327 // *hcache++ = code|(dx<<16)|(trow<<27); // cache it
\r
328 scrpos+=(*hc++)*LINE_WIDTH - START_ROW*LINE_WIDTH*8;
\r
330 while((code=*hc++)) {
\r
331 if((short)code == blank) continue;
\r
334 if(((unsigned)code>>27) != prevy) {
\r
335 prevy = (unsigned)code>>27;
\r
336 pd = scrpos + prevy*LINE_WIDTH*8;
\r
339 // Get tile address/2:
\r
340 addr=(code&0x7ff)<<4;
\r
341 // pal=PicoCramHigh+((code>>9)&0x30);
\r
342 pal=(unsigned char)((code>>9)&0x30);
\r
344 switch((code>>11)&3) {
\r
345 case 0: zero=TileXnormYnorm(pd+((code>>16)&0x1ff),addr,pal); break;
\r
346 case 1: zero=TileXflipYnorm(pd+((code>>16)&0x1ff),addr,pal); break;
\r
347 case 2: zero=TileXnormYflip(pd+((code>>16)&0x1ff),addr,pal); break;
\r
348 case 3: zero=TileXflipYflip(pd+((code>>16)&0x1ff),addr,pal); break;
\r
351 if(zero) blank=(short)code;
\r
356 // sx and sy are coords of virtual screen with 8pix borders on top and on left
\r
357 static void DrawSpriteFull(unsigned int *sprite)
\r
359 int width=0,height=0;
\r
360 // unsigned short *pal=NULL;
\r
362 int tile,code,tdeltax,tdeltay;
\r
363 unsigned char *scrpos;
\r
368 sy=(sy&0x1ff)-0x78; // Y
\r
369 width=(height>>2)&3; height&=3;
\r
370 width++; height++; // Width and height in tiles
\r
373 sx=((code>>16)&0x1ff)-0x78; // X
\r
375 tile=code&0x7ff; // Tile number
\r
376 tdeltax=height; // Delta to increase tile by going right
\r
377 tdeltay=1; // Delta to increase tile by going down
\r
378 if (code&0x0800) { tdeltax=-tdeltax; tile+=height*(width-1); } // Flip X
\r
379 if (code&0x1000) { tdeltay=-tdeltay; tile+=height-1; } // Flip Y
\r
381 //delta<<=4; // Delta of address
\r
382 // pal=PicoCramHigh+((code>>9)&0x30); // Get palette pointer
\r
383 pal=(unsigned char)((code>>9)&0x30);
\r
385 // goto first vertically visible tile
\r
386 while(sy <= START_ROW*8) { sy+=8; tile+=tdeltay; height--; }
\r
388 scrpos = PicoDraw2FB;
\r
389 scrpos+=(sy-START_ROW*8)*LINE_WIDTH;
\r
391 for (; height > 0; height--, sy+=8, tile+=tdeltay)
\r
393 int w = width, x=sx, t=tile;
\r
395 if(sy >= END_ROW*8+8) return; // offscreen
\r
397 for (; w; w--,x+=8,t+=tdeltax)
\r
400 if(x>=328) break; // Offscreen
\r
402 t&=0x7fff; // Clip tile address
\r
403 switch((code>>11)&3) {
\r
404 case 0: TileXnormYnorm(scrpos+x,t<<4,pal); break;
\r
405 case 1: TileXflipYnorm(scrpos+x,t<<4,pal); break;
\r
406 case 2: TileXnormYflip(scrpos+x,t<<4,pal); break;
\r
407 case 3: TileXflipYflip(scrpos+x,t<<4,pal); break;
\r
411 scrpos+=8*LINE_WIDTH;
\r
417 static void DrawAllSpritesFull(int prio, int maxwidth)
\r
419 struct PicoVideo *pvid=&Pico.video;
\r
420 int table=0,maskrange=0;
\r
422 unsigned int *sprites[80]; // Sprites
\r
423 int y_min=START_ROW*8, y_max=END_ROW*8; // for a simple sprite masking
\r
425 table=pvid->reg[5]&0x7f;
\r
426 if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode
\r
427 table<<=8; // Get sprite table address/2
\r
429 for (i=u=0; u < 80; u++)
\r
431 unsigned int *sprite=NULL;
\r
432 int code, code2, sx, sy, height;
\r
434 sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite
\r
439 // check if it is not hidden vertically
\r
440 sy = (code&0x1ff)-0x80;
\r
441 height = (((code>>24)&3)+1)<<3;
\r
442 if(sy+height <= y_min || sy > y_max) goto nextsprite;
\r
446 sx = (code2>>16)&0x1ff;
\r
448 int to = sy+height; // sy ~ from
\r
450 // try to merge with previous range
\r
451 if((maskrange>>16)+1 >= sy && (maskrange>>16) <= to && (maskrange&0xffff) < sy) sy = (maskrange&0xffff);
\r
452 else if((maskrange&0xffff)-1 <= to && (maskrange&0xffff) >= sy && (maskrange>>16) > to) to = (maskrange>>16);
\r
454 // support only very simple masking (top and bottom of screen)
\r
455 if(sy <= y_min && to+1 > y_min) y_min = to+1;
\r
456 else if(to >= y_max && sy-1 < y_max) y_max = sy-1;
\r
457 else maskrange=sy|(to<<16);
\r
463 if(((code2>>15)&1) != prio) goto nextsprite; // wrong priority
\r
465 // check if sprite is not hidden horizontally
\r
466 sx -= 0x78; // Get X coordinate + 8
\r
467 if(sx <= -8*3 || sx >= maxwidth) goto nextsprite;
\r
469 // sprite is good, save it's index
\r
470 sprites[i++]=sprite;
\r
473 // Find next sprite
\r
474 link=(code>>16)&0x7f;
\r
475 if(!link) break; // End of sprites
\r
478 // Go through sprites backwards:
\r
479 for (i-- ;i>=0; i--)
\r
481 DrawSpriteFull(sprites[i]);
\r
485 #ifndef _ASM_DRAW_C
\r
486 static void BackFillFull(int reg7)
\r
490 // Start with a background color:
\r
491 // back=PicoCramHigh[reg7&0x3f];
\r
496 memset32((int *)PicoDraw2FB, back, LINE_WIDTH*(8+(END_ROW-START_ROW)*8)/4);
\r
500 static void DrawDisplayFull()
\r
502 struct PicoVideo *pvid=&Pico.video;
\r
503 int win, edge=0, hvwin=0; // LSb->MSb: hwin&plane, vwin&plane, full
\r
504 int planestart=START_ROW, planeend=END_ROW; // plane A start/end when window shares display with plane A (in tile rows or columns)
\r
505 int winstart=START_ROW, winend=END_ROW; // same for window
\r
506 int maxw, maxcolc; // max width and col cells
\r
508 if(pvid->reg[12]&1) {
\r
509 maxw = 328; maxcolc = 40;
\r
511 maxw = 264; maxcolc = 32;
\r
514 // horizontal window?
\r
515 if((win=pvid->reg[0x12])) {
\r
516 hvwin=1; // hwindow shares display with plane A
\r
519 // fullscreen window
\r
521 } else if(win < 0x80) {
\r
522 // window on the top
\r
523 if(edge <= START_ROW) hvwin=0; // window not visible in our drawing region
\r
524 else if(edge >= END_ROW) hvwin=4;
\r
525 else planestart = winend = edge;
\r
526 } else if(win > 0x80) {
\r
527 // window at the bottom
\r
528 if(edge >= END_ROW) hvwin=0;
\r
529 else planeend = winstart = edge;
\r
533 // check for vertical window, but only if win is not fullscreen
\r
535 win=pvid->reg[0x11];
\r
539 else if(edge < (maxcolc>>1)) {
\r
540 // window is on the right
\r
542 planeend|=edge<<17;
\r
543 winstart|=edge<<17;
\r
544 winend|=maxcolc<<16;
\r
547 if(edge >= (maxcolc>>1)) hvwin=4;
\r
549 // window is on the left
\r
552 planestart|=edge<<17;
\r
553 planeend|=maxcolc<<16;
\r
558 if(hvwin==1) { winend|=maxcolc<<16; planeend|=maxcolc<<16; }
\r
561 DrawLayerFull(1, HighCache2B, START_ROW, (maxcolc<<16)|END_ROW);
\r
564 // fullscreen window
\r
565 DrawWindowFull(START_ROW, (maxcolc<<16)|END_ROW, 0);
\r
566 HighCache2A[1] = 0;
\r
570 // we have plane A and both v and h windows
\r
571 DrawLayerFull(0, HighCache2A, planestart, planeend);
\r
572 DrawWindowFull( winstart&~0xff0000, (winend&~0xff0000)|(maxcolc<<16), 0); // h
\r
573 DrawWindowFull((winstart&~0xff)|START_ROW, (winend&~0xff)|END_ROW, 0); // v
\r
578 // both window and plane A visible, window is vertical XOR horizontal
\r
579 DrawLayerFull(0, HighCache2A, planestart, planeend);
\r
580 DrawWindowFull(winstart, winend, 0);
\r
584 // fullscreen plane A
\r
585 DrawLayerFull(0, HighCache2A, START_ROW, (maxcolc<<16)|END_ROW);
\r
588 DrawAllSpritesFull(0, maxw);
\r
591 if(HighCache2B[1]) DrawTilesFromCacheF(HighCache2B);
\r
592 if(HighCache2A[1]) DrawTilesFromCacheF(HighCache2A);
\r
595 // fullscreen window
\r
596 DrawWindowFull(START_ROW, (maxcolc<<16)|END_ROW, 1);
\r
600 // we have plane A and both v and h windows
\r
601 DrawWindowFull( winstart&~0xff0000, (winend&~0xff0000)|(maxcolc<<16), 1); // h
\r
602 DrawWindowFull((winstart&~0xff)|START_ROW, (winend&~0xff)|END_ROW, 1); // v
\r
607 // both window and plane A visible, window is vertical XOR horizontal
\r
608 DrawWindowFull(winstart, winend, 1);
\r
615 DrawAllSpritesFull(1, maxw);
\r
619 PICO_INTERNAL void PicoFrameFull()
\r
622 if(PicoPrepareCram) PicoPrepareCram();
\r
625 BackFillFull(Pico.video.reg[7]);
\r
626 if (Pico.video.reg[1]&0x40) DrawDisplayFull();
\r