1 /* FCE Ultra - NES/Famicom Emulator
3 * Copyright notice for this file:
4 * Copyright (C) 1998 BERO
5 * Copyright (C) 2003 Xodnizel
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 #include "drivers/arm/asmutils.h"
43 static void FetchSpriteData098(void);
44 static void FASTAPASS(1) RefreshLine098(int lastpixel);
45 static void RefreshSprites098(void);
46 static void CopySprites098(uint8 *target);
48 static void Fixit1(void);
49 static uint32 ppulut1[256];
50 static uint32 ppulut2[256];
51 static uint32 ppulut3[128];
53 static void makeppulut(void)
62 ppulut1[x]|=((x>>(7-y))&1)<<(y*4);
63 ppulut2[x]=ppulut1[x]<<1;
74 ppulut3[xo|(cc<<3)]=0;
75 for(pixel=0;pixel<8;pixel++)
80 ppulut3[xo|(cc<<3)]|=(( cc>>shiftr )&3)<<(2+pixel*4);
82 // printf("%08x\n",ppulut3[xo|(cc<<3)]);
92 /* Color deemphasis emulation. Joy... */
94 static int deempcnt[8];
96 extern int maxsprites;
99 extern uint8 SPRAM[0x100];
100 extern uint8 SPRBUF[0x100];
103 #define MMC5SPRVRAMADR(V) &MMC5SPRVPage[(V)>>10][(V)]
104 #define MMC5BGVRAMADR(V) &MMC5BGVPage[(V)>>10][(V)]
105 #define VRAMADR(V) &VPage[(V)>>10][(V)]
112 FCEUPPU_LineUpdate098();
114 ret|=PPUGenLatch&0x1F;
124 static DECLFR(A200x) /* Not correct for $2004 reads. */
126 FCEUPPU_LineUpdate098();
135 FCEUPPU_LineUpdate098();
145 //printf("$%02x:$%02x\n",PPUSPL,V);
151 printf("%d, %02x\n",scanline,ret);
158 uint32 tmp=RefreshAddr&0x3FFF;
160 FCEUPPU_LineUpdate098();
165 if(PPU_hook) PPU_hook(tmp);
166 PPUGenLatch=VRAMBuffer;
169 VRAMBuffer=VPage[tmp>>10][tmp];
173 VRAMBuffer=vnapage[(tmp>>10)&0x3][tmp&0x3FF];
177 if(INC32) RefreshAddr+=32;
179 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
186 // FCEU_printf("%04x:%02x, (%d) %02x, %02x\n",A,V,scanline,PPU[0],PPU_status);
188 FCEUPPU_LineUpdate098();
190 if(!(PPU[0]&0x80) && (V&0x80) && (PPU_status&0x80))
192 // FCEU_printf("Trigger NMI, %d, %d\n",timestamp,ppudead);
194 TriggerNMI(); // TODO?
203 //printf("%04x:$%02x, %d\n",A,V,scanline);
204 FCEUPPU_LineUpdate098();
218 //printf("$%04x:$%02x, %d, %d\n",A,V,timestamp,scanline);
226 //printf("Wr: %04x:$%02x\n",A,V);
236 //printf("$%02x:$%02x\n",PPUSPL,V);
247 FCEUPPU_LineUpdate098();
268 FCEUPPU_LineUpdate098();
274 TempAddr|=(V&0x3f)<<8;
281 RefreshAddr=TempAddr;
283 PPU_hook(RefreshAddr);
284 //printf("%d, %04x\n",scanline,RefreshAddr);
291 uint32 tmp=RefreshAddr&0x3FFF;
297 PALRAM[0x00]=PALRAM[0x04]=PALRAM[0x08]=PALRAM[0x0C]=V&0x3F;
298 else if(tmp&3) PALRAM[(tmp&0x1f)]=V&0x3f;
302 if(PPUCHRRAM&(1<<(tmp>>10)))
303 VPage[tmp>>10][tmp]=V;
307 if(PPUNTARAM&(1<<((tmp&0xF00)>>10)))
308 vnapage[((tmp&0xF00)>>10)][tmp&0x3FF]=V;
310 // FCEU_printf("ppu (%04x) %04x:%04x %d, %d\n",X.PC,RefreshAddr,PPUGenLatch,scanline,timestamp);
311 if(INC32) RefreshAddr+=32;
313 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
322 // X6502_DMW(0x2004,X6502_DMR(t+x));
324 B2004(0x2004,X.DB=ARead[t+x](t+x));
325 X6502_AddCycles(512);
328 #define PAL(c) ((c)+cc)
330 #define GETLASTPIXEL (PAL?((timestamp*48-linestartts)/15) : ((timestamp*48-linestartts)>>4) )
332 static uint8 *Pline=0,*Plinef;
333 static int firsttile;
334 static int linestartts;
337 static void ResetRL(uint8 *target)
339 FCEU_dwmemset(target,0xffffffff,256);
340 if(InputScanlineHook)
341 InputScanlineHook(0,0,0,0);
345 linestartts=timestamp*48+X6502_GetCycleCount();
347 FCEUPPU_LineUpdate098();
351 extern uint8 sprlinebuf[256+8];
353 void FCEUPPU_LineUpdate098(void)
362 static int rendis = 0;
364 void FCEUI_SetRenderDisable(int sprites, int bg)
366 //printf("%d, %d\n",sprites,bg);
369 if(sprites == 2) rendis ^= 1;
370 else rendis = (rendis &~1) | sprites?1:0;
374 if(bg == 2) rendis ^= 2;
375 else rendis = (rendis &~2) | bg?2:0;
379 static void CheckSpriteHit(int p);
381 static void EndRL(void)
391 static uint8 sphitdata;
393 static void CheckSpriteHit(int p)
398 if(sphitx==0x100) return;
400 for(x=sphitx;x<(sphitx+8) && x<l;x++)
402 if((sphitdata&(0x80>>(x-sphitx))) && !(Plinef[x]&64))
405 //printf("Ha: %d, %d, Hita: %d, %d, %d, %d, %d\n",p,p&~7,scanline,GETLASTPIXEL-16,&Plinef[x],Pline,Pline-Plinef);
406 //printf("%d\n",GETLASTPIXEL-16);
407 //if(Plinef[x] == 0xFF)
408 //printf("PL: %d, %02x\n",scanline, Plinef[x]);
414 static int spork=0; /* spork the world. Any sprites on this line?
415 Then this will be set to 1. Needed for zapper
416 emulation and *gasp* sprite emulation.
419 // lasttile is really "second to last tile."
420 static void FASTAPASS(1) RefreshLine098(int lastpixel)
422 static uint32 pshift[2];
423 static uint32 atlatch;
424 uint32 smorkus=RefreshAddr;
426 #define RefreshAddr smorkus
430 register uint8 *P=Pline;
431 int lasttile=lastpixel>>3;
433 static int norecurse=0; /* Yeah, recursion would be bad.
434 PPU_hook() functions can call
435 mirroring/chr bank switching functions,
436 which call FCEUPPU_LineUpdate, which call this
438 if(norecurse) return;
440 if(sphitx != 0x100 && !(PPU_status&0x40))
442 if((sphitx < (lastpixel-16)) && !(sphitx < ((lasttile - 2)*8)))
444 //printf("OK: %d\n",scanline);
450 if(lasttile>34) lasttile=34;
451 numtiles=lasttile-firsttile;
453 if(numtiles<=0) return;
459 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
461 if(!ScreenON && !SpriteON)
465 tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
468 if(firsttile+tiles > 256/8) tiles=256/8-firsttile;
470 FCEU_dwmemset(Pline,tem,tiles*8);
476 #define TOFIXNUM (272-0x4)
477 if(lastpixel>=TOFIXNUM && tofix)
483 if(InputScanlineHook && (lastpixel-16)>=0)
485 InputScanlineHook(Plinef,spork?sprlinebuf:0,linestartts,lasttile*8-16);
490 /* Priority bits, needed for sprite emulation. */
496 /* This high-level graphics MMC5 emulation code was written
497 for MMC5 carts in "CL" mode. It's probably not totally
498 correct for carts in "SL" mode.
502 if(MMC5Hack && geniestage!=1)
504 if(MMC5HackCHRMode==0 && (MMC5HackSPMode&0x80))
506 int tochange=MMC5HackSPMode&0x1F;
508 for(X1=firsttile;X1<lasttile;X1++)
510 if((tochange<=0 && MMC5HackSPMode&0x40) || (tochange>0 && !(MMC5HackSPMode&0x40)))
513 #include "pputile098.h"
518 #include "pputile098.h"
523 else if(MMC5HackCHRMode==1 && (MMC5HackSPMode&0x80))
525 int tochange=MMC5HackSPMode&0x1F;
529 #define PPUT_MMC5CHR1
530 for(X1=firsttile;X1<lasttile;X1++)
532 #include "pputile098.h"
537 else if(MMC5HackCHRMode==1)
539 #define PPUT_MMC5CHR1
540 for(X1=firsttile;X1<lasttile;X1++)
542 #include "pputile098.h"
548 for(X1=firsttile;X1<lasttile;X1++)
550 #include "pputile098.h"
559 for(X1=firsttile;X1<lasttile;X1++)
561 #include "pputile098.h"
568 for(X1=firsttile;X1<lasttile;X1++)
570 #include "pputile098.h"
577 /* Reverse changes made before. */
584 if(firsttile<=2 && 2<lasttile && !(PPU[1]&2))
587 tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
589 *(uint32 *)Plinef=*(uint32 *)(Plinef+4)=tem;
596 tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
599 tcount=lasttile-firsttile;
607 FCEU_dwmemset(Plinef+tstart*8,tem,tcount*8);
610 if(lastpixel>=TOFIXNUM && tofix)
617 //CheckSpriteHit(lasttile*8); //lasttile*8); //lastpixel);
619 CheckSpriteHit(lastpixel); /* This only works right because
620 of a hack earlier in this function.
622 if(InputScanlineHook && (lastpixel-16)>=0)
624 InputScanlineHook(Plinef,spork?sprlinebuf:0,linestartts,lasttile*8-16);
630 static INLINE void Fixit2(void)
632 if(ScreenON || SpriteON)
634 uint32 rad=RefreshAddr;
636 rad|=TempAddr&0x041f;
638 //PPU_hook(RefreshAddr);
639 //PPU_hook(RefreshAddr,-1);
643 static void Fixit1(void)
645 if(ScreenON || SpriteON)
647 uint32 rad=RefreshAddr;
649 if((rad&0x7000)==0x7000)
652 if((rad&0x3E0)==0x3A0)
659 if((rad&0x3E0)==0x3e0)
667 //PPU_hook(RefreshAddr); //,-1);
671 void MMC5_hb(int); /* Ugh ugh ugh. */
672 static void DoLine(void)
677 uint8 *target=XBuf+scanline*320+32;
679 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
684 if(rendis & 2) /* User asked to not display background data. */
687 tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
689 FCEU_dwmemset(target,tem,256);
693 CopySprites098(target);
696 if(ScreenON || SpriteON) // Yes, very el-cheapo.
699 block_and(target, 256, 0x30);
702 block_or(target, 256, 0xc0);
704 block_or(target, 256, 0x40);
706 block_andor(target, 256, 0x3f, 0x80);
708 if(ScreenON || SpriteON) // Yes, very el-cheapo.
713 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0x30303030;
719 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0xc0c0c0c0;
723 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])|0x40404040;
726 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0x80808080;
731 if(ScreenON || SpriteON)
732 FetchSpriteData098();
734 if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
744 X6502_Run(6); // Tried 65, caused problems with Slalom(maybe others)
748 // A semi-hack for Star Trek: 25th Anniversary
749 if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
755 if(GameHBIRQHook2 && (ScreenON || SpriteON))
760 ResetRL(XBuf+scanline*320+32);
777 static uint8 numsprites,SpriteBlurp;
778 static void FetchSpriteData098(void)
792 vofs=(unsigned int)(P0&0x8&(((P0&0x20)^0x20)>>2))<<9;
796 for(n=63;n>=0;n--,spr++)
798 if((unsigned int)(scanline-spr->y)>=H) continue;
799 //printf("%d, %u\n",scanline,(unsigned int)(scanline-spr->y));
810 t = (int)scanline-(spr->y);
813 vadr = ((spr->no&1)<<12) + ((spr->no&0xFE)<<4);
815 vadr = (spr->no<<4)+vofs;
830 /* Fix this geniestage hack */
831 if(MMC5Hack && geniestage!=1) C = MMC5SPRVRAMADR(vadr);
832 else C = VRAMADR(vadr);
840 *(uint32 *)&SPRBUF[ns<<2]=*(uint32 *)&dst;
852 for(n=63;n>=0;n--,spr++)
854 if((unsigned int)(scanline-spr->y)>=H) continue;
866 t = (int)scanline-(spr->y);
869 vadr = ((spr->no&1)<<12) + ((spr->no&0xFE)<<4);
871 vadr = (spr->no<<4)+vofs;
886 if(MMC5Hack) C = MMC5SPRVRAMADR(vadr);
887 else C = VRAMADR(vadr);
899 *(uint32 *)&SPRBUF[ns<<2]=*(uint32 *)&dst;
911 //printf("%d %d\n",scanline,ns);
912 if(ns>8) PPU_status|=0x20; /* Handle case when >8 sprites per
913 scanline option is enabled. */
916 for(n=0;n<(8-ns);n++)
926 static void RefreshSprites098(void)
932 if(!numsprites) return;
934 FCEU_dwmemset(sprlinebuf,0x80808080,256);
936 spr = (SPRB*)SPRBUF+numsprites;
938 for(n=numsprites;n>=0;n--,spr--)
941 //register uint32 pixdata asm ("eax");
942 //register uint8 J, atr;
944 register uint32 pixdata;
945 register uint8 J,atr;
952 pixdata=ppulut1[spr->ca[0]]|ppulut2[spr->ca[1]];
953 J=spr->ca[0]|spr->ca[1];
958 if(n==0 && SpriteBlurp && !(PPU_status&0x40))
963 sphitdata= ((J<<7)&0x80) |
974 VB = (PALRAM+0x10)+((atr&3)<<2);
980 if(J&0x80) C[7]=VB[pixdata&3]|0x40;
982 if(J&0x40) C[6]=VB[pixdata&3]|0x40;
984 if(J&0x20) C[5]=VB[pixdata&3]|0x40;
986 if(J&0x10) C[4]=VB[pixdata&3]|0x40;
988 if(J&0x08) C[3]=VB[pixdata&3]|0x40;
990 if(J&0x04) C[2]=VB[pixdata&3]|0x40;
992 if(J&0x02) C[1]=VB[pixdata&3]|0x40;
994 if(J&0x01) C[0]=VB[pixdata]|0x40;
996 if(J&0x80) C[0]=VB[pixdata&3]|0x40;
998 if(J&0x40) C[1]=VB[pixdata&3]|0x40;
1000 if(J&0x20) C[2]=VB[pixdata&3]|0x40;
1002 if(J&0x10) C[3]=VB[pixdata&3]|0x40;
1004 if(J&0x08) C[4]=VB[pixdata&3]|0x40;
1006 if(J&0x04) C[5]=VB[pixdata&3]|0x40;
1008 if(J&0x02) C[6]=VB[pixdata&3]|0x40;
1010 if(J&0x01) C[7]=VB[pixdata]|0x40;
1015 if(J&0x80) C[7]=VB[pixdata&3];
1017 if(J&0x40) C[6]=VB[pixdata&3];
1019 if(J&0x20) C[5]=VB[pixdata&3];
1021 if(J&0x10) C[4]=VB[pixdata&3];
1023 if(J&0x08) C[3]=VB[pixdata&3];
1025 if(J&0x04) C[2]=VB[pixdata&3];
1027 if(J&0x02) C[1]=VB[pixdata&3];
1029 if(J&0x01) C[0]=VB[pixdata];
1031 if(J&0x80) C[0]=VB[pixdata&3];
1033 if(J&0x40) C[1]=VB[pixdata&3];
1035 if(J&0x20) C[2]=VB[pixdata&3];
1037 if(J&0x10) C[3]=VB[pixdata&3];
1039 if(J&0x08) C[4]=VB[pixdata&3];
1041 if(J&0x04) C[5]=VB[pixdata&3];
1043 if(J&0x02) C[6]=VB[pixdata&3];
1045 if(J&0x01) C[7]=VB[pixdata];
1054 static void CopySprites098(uint8 *target)
1056 uint8 n=((PPU[1]&4)^4)<<1;
1062 if(rendis & 1) return; /* User asked to not display sprites. */
1066 uint32 t=*(uint32 *)(sprlinebuf+n);
1073 if(!(t&0x40) || (P[n]&0x40)) // Normal sprite || behind bg sprite
1079 if(!(t&0x4000) || (P[n+1]&0x40)) // Normal sprite || behind bg sprite
1080 P[n+1]=(sprlinebuf+1)[n];
1085 if(!(t&0x400000) || (P[n+2]&0x40)) // Normal sprite || behind bg sprite
1086 P[n+2]=(sprlinebuf+2)[n];
1091 if(!(t&0x40000000) || (P[n+3]&0x40)) // Normal sprite || behind bg sprite
1092 P[n+3]=(sprlinebuf+3)[n];
1095 /* TODO: Simplify */
1098 if(!(t&0x40000000)) // Normal sprite
1100 else if(P[n]&64) // behind bg sprite
1106 if(!(t&0x400000)) // Normal sprite
1107 P[n+1]=(sprlinebuf+1)[n];
1108 else if(P[n+1]&64) // behind bg sprite
1109 P[n+1]=(sprlinebuf+1)[n];
1114 if(!(t&0x4000)) // Normal sprite
1115 P[n+2]=(sprlinebuf+2)[n];
1116 else if(P[n+2]&64) // behind bg sprite
1117 P[n+2]=(sprlinebuf+2)[n];
1122 if(!(t&0x40)) // Normal sprite
1123 P[n+3]=(sprlinebuf+3)[n];
1124 else if(P[n+3]&64) // behind bg sprite
1125 P[n+3]=(sprlinebuf+3)[n];
1131 if(n) goto loopskie;
1134 void FCEUPPU_Init(void)
1139 void FCEUPPU_Reset(void)
1141 VRAMBuffer=PPU[0]=PPU[1]=PPU_status=PPU[3]=0;
1144 RefreshAddr=TempAddr=0;
1152 void FCEUPPU_Power(void)
1156 memset(NTARAM,0x00,0x800);
1157 memset(PALRAM,0x00,0x20);
1158 memset(SPRAM,0x00,0x100);
1161 for(x=0x2000;x<0x4000;x+=8)
1171 ARead[x+4]=A200x; //A2004;
1180 BWrite[0x4014]=B4014;
1184 void FCEUPPU_Loop(int skip)
1186 uint32 scanlines_per_frame = PAL ? 312 : 262;
1188 if(ppudead) /* Needed for Knight Rider, possibly others. */
1190 //memset(XBuf, 0x80, 256*240);
1191 //X6502_Run(scanlines_per_frame*(256+85));
1193 for (lines=scanlines_per_frame;lines;lines--)
1201 PPU[3]=PPUSPL=0; /* Not sure if this is correct. According
1202 to Matt Conte and my own tests, it is. Timing is probably
1203 off, though. NOTE: Not having this here
1204 breaks a Super Donkey Kong game. */
1205 /* I need to figure out the true nature and length
1209 if(FCEUGameInfo.type==GIT_NSF)
1216 // Note: this is needed for asm core
1219 X6502_Run(256+85-12);
1220 for (lines=scanlines_per_frame-242-1;lines;lines--)
1229 if(ScreenON || SpriteON)
1231 if(GameHBIRQHook && ((PPU[0]&0x38)!=0x18))
1234 for(x=0;x<42;x++) {PPU_hook(0x2000); PPU_hook(0);}
1239 if(ScreenON || SpriteON)
1241 RefreshAddr=TempAddr;
1242 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
1245 /* Clean this stuff up later. */
1252 if(FCEUGameInfo.type==GIT_NSF)
1254 // run scanlines for asm core to fuction
1255 for(scanline=0;scanline<240;scanline++)
1266 PPU_status|=0x20; // Fixes "Bee 52". Does it break anything?
1270 for(scanline=0;scanline<240;scanline++)
1272 if(ScreenON || SpriteON)
1274 if(scanline==y && SpriteON) PPU_status|=0x40;
1275 X6502_Run((scanline==239)?85:(256+85));
1280 for (lines=y;lines;lines--)
1282 if(SpriteON) PPU_status|=0x40; // Quick and very dirty hack.
1283 for (lines=240-y;lines;lines--)
1288 for (lines=240;lines;lines--)
1298 for(scanline=0;scanline<240;) //scanline is incremented in DoLine. Evil. :/
1302 if((PPUViewer) && (scanline == PPUViewScanline)) UpdatePPUView(1);
1306 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1307 for(x=1,max=0,maxref=0;x<7;x++)
1316 //FCEU_DispMessage("%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x %d",deempcnt[0],deempcnt[1],deempcnt[2],deempcnt[3],deempcnt[4],deempcnt[5],deempcnt[6],deempcnt[7],maxref);
1317 //memset(deempcnt,0,sizeof(deempcnt));
1318 SetNESDeemph(maxref,0);
1320 } /* else... to if(ppudead) */