--- /dev/null
+\r
+#include "PicoInt.h"\r
+#ifndef __GNUC__\r
+#pragma warning (disable:4706) // Disable assignment with conditional\r
+#endif\r
+\r
+int (*PicoScan)(unsigned int num,unsigned short *data)=NULL;\r
+\r
+// Line colour indices - in the format 00ppcccc pp=palette, cccc=colour\r
+static unsigned short HighCol[32+320+8]; // Gap for 32 column, and messy border on right\r
+static int Scanline=0; // Scanline\r
+\r
+int PicoMask=0xfff; // Mask of which layers to draw\r
+\r
+static int TileNorm(unsigned short *pd,int addr,unsigned int *pal)\r
+{\r
+ unsigned int pack=0; unsigned int t=0;\r
+\r
+ pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels\r
+ if (pack)\r
+ {\r
+ t=pack&0x0000f000; if (t) { t=pal[t>>12]; pd[0]=(unsigned short)t; }\r
+ t=pack&0x00000f00; if (t) { t=pal[t>> 8]; pd[1]=(unsigned short)t; }\r
+ t=pack&0x000000f0; if (t) { t=pal[t>> 4]; pd[2]=(unsigned short)t; }\r
+ t=pack&0x0000000f; if (t) { t=pal[t ]; pd[3]=(unsigned short)t; }\r
+ t=pack&0xf0000000; if (t) { t=pal[t>>28]; pd[4]=(unsigned short)t; }\r
+ t=pack&0x0f000000; if (t) { t=pal[t>>24]; pd[5]=(unsigned short)t; }\r
+ t=pack&0x00f00000; if (t) { t=pal[t>>20]; pd[6]=(unsigned short)t; }\r
+ t=pack&0x000f0000; if (t) { t=pal[t>>16]; pd[7]=(unsigned short)t; }\r
+ return 0;\r
+ }\r
+\r
+ return 1; // Tile blank\r
+}\r
+\r
+static int TileFlip(unsigned short *pd,int addr,unsigned int *pal)\r
+{\r
+ unsigned int pack=0; unsigned int t=0;\r
+\r
+ pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels\r
+ if (pack)\r
+ {\r
+ t=pack&0x000f0000; if (t) { t=pal[t>>16]; pd[0]=(unsigned short)t; }\r
+ t=pack&0x00f00000; if (t) { t=pal[t>>20]; pd[1]=(unsigned short)t; }\r
+ t=pack&0x0f000000; if (t) { t=pal[t>>24]; pd[2]=(unsigned short)t; }\r
+ t=pack&0xf0000000; if (t) { t=pal[t>>28]; pd[3]=(unsigned short)t; }\r
+ t=pack&0x0000000f; if (t) { t=pal[t ]; pd[4]=(unsigned short)t; }\r
+ t=pack&0x000000f0; if (t) { t=pal[t>> 4]; pd[5]=(unsigned short)t; }\r
+ t=pack&0x00000f00; if (t) { t=pal[t>> 8]; pd[6]=(unsigned short)t; }\r
+ t=pack&0x0000f000; if (t) { t=pal[t>>12]; pd[7]=(unsigned short)t; }\r
+ return 0;\r
+ }\r
+ return 1; // Tile blank\r
+}\r
+\r
+struct TileStrip\r
+{\r
+ int nametab; // Position in VRAM of name table (for this tile line)\r
+ int line; // Line number in pixels 0x000-0x3ff within the virtual tilemap \r
+ int hscroll; // Horizontal scroll value in pixels for the line\r
+ int xmask; // X-Mask (0x1f - 0x7f) for horizontal wraparound in the tilemap\r
+ int high; // High or low tiles\r
+};\r
+\r
+static int WrongPri=0; // 1 if there were tiles which are the wrong priority\r
+\r
+static int DrawStrip(struct TileStrip ts)\r
+{\r
+ int tilex=0,dx=0,ty=0;\r
+ int blank=-1; // The tile we know is blank\r
+\r
+ WrongPri=0;\r
+\r
+ // Draw tiles across screen:\r
+ tilex=(-ts.hscroll)>>3;\r
+ ty=(ts.line&7)<<1; // Y-Offset into tile\r
+ for (dx=((ts.hscroll-1)&7)+1; dx<328; dx+=8,tilex++)\r
+ {\r
+ int code=0,addr=0,zero=0;\r
+ unsigned int *pal=NULL;\r
+\r
+ code=Pico.vram[ts.nametab+(tilex&ts.xmask)];\r
+ if (code==blank) continue;\r
+ if ((code>>15)!=ts.high) { WrongPri=1; continue; }\r
+\r
+ // Get tile address/2:\r
+ addr=(code&0x7ff)<<4;\r
+ if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip\r
+\r
+ pal=Pico.highpal+((code>>9)&0x30);\r
+\r
+ if (code&0x0800) zero=TileFlip(HighCol+24+dx,addr,pal);\r
+ else zero=TileNorm(HighCol+24+dx,addr,pal);\r
+\r
+ if (zero) blank=code; // We know this tile is blank now\r
+ }\r
+\r
+ return 0;\r
+}\r
+\r
+static int DrawLayer(int plane,int high)\r
+{\r
+ struct PicoVideo *pvid=&Pico.video;\r
+ static char shift[4]={5,6,6,7}; // 32,64 or 128 sized tilemaps\r
+ struct TileStrip ts;\r
+\r
+ // Work out the TileStrip to draw\r
+\r
+ // Get vertical scroll value:\r
+ int vscroll=Pico.vsram[plane];\r
+\r
+ int htab=pvid->reg[13]<<9; // Horizontal scroll table address\r
+ if ( pvid->reg[11]&2) htab+=Scanline<<1; // Offset by line\r
+ if ((pvid->reg[11]&1)==0) htab&=~0xf; // Offset by tile\r
+ htab+=plane; // A or B\r
+\r
+ // Get horizontal scroll value\r
+ ts.hscroll=Pico.vram[htab&0x7fff];\r
+\r
+ // Work out the name table size: 32 64 or 128 tiles (0-3)\r
+ int width=pvid->reg[16];\r
+ int height=(width>>4)&3; width&=3;\r
+\r
+ ts.xmask=(1<<shift[width ])-1; // X Mask in tiles\r
+ int ymask=(8<<shift[height])-1; // Y Mask in pixels\r
+\r
+ // Find name table:\r
+ if (plane==0) ts.nametab=(pvid->reg[2]&0x38)<< 9; // A\r
+ else ts.nametab=(pvid->reg[4]&0x07)<<12; // B\r
+\r
+ // Find the line in the name table\r
+ ts.line=(vscroll+Scanline)&ymask;\r
+ ts.nametab+=(ts.line>>3)<<shift[width];\r
+\r
+ ts.high=high;\r
+\r
+ DrawStrip(ts);\r
+ return 0;\r
+}\r
+\r
+static int DrawWindow(int high)\r
+{\r
+ struct PicoVideo *pvid=&Pico.video;\r
+ struct TileStrip ts;\r
+\r
+ ts.line=Scanline;\r
+ ts.hscroll=0;\r
+\r
+ // Find name table line:\r
+ if (Pico.video.reg[12]&1)\r
+ {\r
+ ts.nametab=(pvid->reg[3]&0x3c)<<9; // 40-cell mode\r
+ ts.nametab+=(ts.line>>3)<<6;\r
+ }\r
+ else\r
+ {\r
+ ts.nametab=(pvid->reg[3]&0x3e)<<9; // 32-cell mode\r
+ ts.nametab+=(ts.line>>3)<<5;\r
+ }\r
+\r
+ ts.xmask=0x3f;\r
+ ts.high=high;\r
+\r
+ DrawStrip(ts);\r
+ return 0;\r
+}\r
+\r
+static int DrawSprite(int sy,unsigned short *sprite,int high)\r
+{\r
+ int sx=0,width=0,height=0;\r
+ int row=0,code=0;\r
+ unsigned int *pal=NULL;\r
+ int tile=0,delta=0;\r
+ int i=0;\r
+\r
+ code=sprite[2];\r
+ if ((code>>15)!=high) { WrongPri=1; return 0; } // Wrong priority\r
+\r
+ height=sprite[1]>>8;\r
+ width=(height>>2)&3; height&=3;\r
+ width++; height++; // Width and height in tiles\r
+ if (Scanline>=sy+(height<<3)) return 0; // Not on this line after all\r
+\r
+ row=Scanline-sy; // Row of the sprite we are on\r
+ pal=Pico.highpal+((code>>9)&0x30); // Get palette pointer\r
+ if (code&0x1000) row=(height<<3)-1-row; // Flip Y\r
+\r
+ tile=code&0x7ff; // Tile number\r
+ tile+=row>>3; // Tile number increases going down\r
+ delta=height; // Delta to increase tile by going right\r
+ if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X\r
+\r
+ tile<<=4; tile+=(row&7)<<1; // Tile address\r
+ delta<<=4; // Delta of address\r
+\r
+ sx=(sprite[3]&0x1ff)-0x78; // Get X coordinate + 8\r
+\r
+ for (i=0; i<width; i++,sx+=8,tile+=delta)\r
+ {\r
+ if (sx<=0 || sx>=328) continue; // Offscreen\r
+\r
+ tile&=0x7fff; // Clip tile address\r
+ if (code&0x0800) TileFlip(HighCol+24+sx,tile,pal);\r
+ else TileNorm(HighCol+24+sx,tile,pal);\r
+ }\r
+\r
+ return 0;\r
+}\r
+\r
+static int DrawAllSprites(int high)\r
+{\r
+ struct PicoVideo *pvid=&Pico.video;\r
+ int table=0;\r
+ int i=0,link=0;\r
+ unsigned char spin[80]; // Sprite index\r
+\r
+ WrongPri=0;\r
+\r
+ table=pvid->reg[5]&0x7f;\r
+ if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode\r
+ table<<=8; // Get sprite table address/2\r
+\r
+ for (;;)\r
+ {\r
+ unsigned short *sprite=NULL;\r
+ \r
+ spin[i]=(unsigned char)link;\r
+ sprite=Pico.vram+((table+(link<<2))&0x7ffc); // Find sprite\r
+\r
+ // Find next sprite\r
+ link=sprite[1]&0x7f;\r
+ if (link==0 || i>=79) break; // End of sprites\r
+ i++;\r
+ }\r
+\r
+ // Go through sprites backwards:\r
+ for ( ;i>=0; i--)\r
+ {\r
+ unsigned short *sprite=NULL;\r
+ int sy=0;\r
+ \r
+ sprite=Pico.vram+((table+(spin[i]<<2))&0x7ffc); // Find sprite\r
+\r
+ sy=(sprite[0]&0x1ff)-0x80; // Get Y coordinate\r
+\r
+ if (Scanline>=sy && Scanline<sy+32) DrawSprite(sy,sprite,high); // Possibly on this line\r
+ }\r
+\r
+ return 0;\r
+}\r
+\r
+static void BackFill()\r
+{\r
+ unsigned int back=0;\r
+ unsigned int *pd=NULL,*end=NULL;\r
+\r
+ // Start with a blank scanline (background colour):\r
+ back=Pico.video.reg[7]&0x3f;\r
+ back=Pico.highpal[back];\r
+ back|=back<<16;\r
+\r
+ pd= (unsigned int *)(HighCol+32);\r
+ end=(unsigned int *)(HighCol+32+320);\r
+\r
+ do { pd[0]=pd[1]=pd[2]=pd[3]=back; pd+=4; } while (pd<end);\r
+}\r
+\r
+static int DrawDisplay()\r
+{\r
+ int win=0,edge=0,full=0;\r
+ int bhigh=1,ahigh=1,shigh=1;\r
+\r
+ // Find out if the window is on this line:\r
+ win=Pico.video.reg[0x12];\r
+ edge=(win&0x1f)<<3;\r
+\r
+ if (win&0x80) { if (Scanline>=edge) full=1; }\r
+ else { if (Scanline< edge) full=1; }\r
+\r
+ if (PicoMask&0x04) { DrawLayer(1,0); bhigh=WrongPri; }\r
+ if (PicoMask&0x08) { if (full) DrawWindow(0); else DrawLayer(0,0); ahigh=WrongPri; }\r
+ if (PicoMask&0x10) { DrawAllSprites(0); shigh=WrongPri; }\r
+ \r
+ if (bhigh) if (PicoMask&0x20) DrawLayer(1,1);\r
+ if (ahigh) if (PicoMask&0x40) { if (full) DrawWindow(1); else DrawLayer(0,1); }\r
+ if (shigh) if (PicoMask&0x80) DrawAllSprites(1);\r
+ return 0;\r
+}\r
+\r
+static int UpdatePalette()\r
+{\r
+ int c=0;\r
+\r
+ // Update palette:\r
+ for (c=0;c<64;c++) Pico.highpal[c]=(unsigned short)PicoCram(Pico.cram[c]);\r
+ Pico.m.dirtyPal=0;\r
+\r
+ return 0;\r
+}\r
+\r
+static int Overlay()\r
+{\r
+ int col=0,x=0;\r
+\r
+ if (PmovAction==0) return 0;\r
+ if (Scanline>=4) return 0;\r
+\r
+ if (PmovAction&1) col =0x00f;\r
+ if (PmovAction&2) col|=0x0f0;\r
+ col=PicoCram(col);\r
+\r
+ for (x=0;x<4;x++) HighCol[32+x]=(unsigned short)col;\r
+\r
+ return 0;\r
+}\r
+\r
+static int Skip=0;\r
+\r
+\r
+int PicoLine(int scan)\r
+{\r
+ if (Skip>0) { Skip--; return 0; } // Skip rendering lines\r
+\r
+ Scanline=scan;\r
+\r
+ if (Pico.m.dirtyPal) UpdatePalette();\r
+\r
+ // Draw screen:\r
+ if (PicoMask&0x02) BackFill();\r
+ if (Pico.video.reg[1]&0x40) DrawDisplay();\r
+\r
+ Overlay();\r
+\r
+ if (Pico.video.reg[12]&1)\r
+ {\r
+ Skip=PicoScan(Scanline,HighCol+32); // 40-column mode\r
+ }\r
+ else\r
+ {\r
+ // Crop, centre and return 32-column mode\r
+ memset(HighCol, 0,64); // Left border\r
+ memset(HighCol+288,0,64); // Right border\r
+ Skip=PicoScan(Scanline,HighCol);\r
+ }\r
+\r
+ return 0;\r
+}\r
+\r