1 /* FCE Ultra - NES/Famicom Emulator
3 * Copyright notice for this file:
4 * Copyright (C) 1998 BERO
5 * Copyright (C) 2002 Ben Parnell
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
54 static void (*RefreshLine)(uint8 *P, uint32 vofs) = NULL;
55 static void PRefreshLine(void);
57 static void ResetPPU(void);
58 static void PowerPPU(void);
60 uint64 timestampbase=0;
63 uint32 MMC5HackVROMMask;
64 uint8 *MMC5HackExNTARAMPtr;
65 uint8 *MMC5HackVROMPTR;
66 uint8 MMC5HackCHRMode=0;
68 uint8 MMC5HackSPScroll;
71 uint8 *MMC5SPRVPage[8];
72 uint8 *MMC5BGVPage[8];
75 uint8 VRAMBuffer,PPUGenLatch;
81 /* Color deemphasis emulation. Joy... */
83 static int deempcnt[8];
88 void (*GameInterface)(int h);
90 void FP_FASTAPASS(1) (*PPU_hook)(uint32 A);
92 void (*GameStateRestore)(int version);
93 void (*GameHBIRQHook)(void);
95 readfunc ARead[0x10000];
96 writefunc BWrite[0x10000];
97 static readfunc *AReadG;
98 static writefunc *BWriteG;
111 int AllocGenieRW(void)
113 if(!(AReadG=FCEU_malloc(0x8000*sizeof(readfunc))))
115 if(!(BWriteG=FCEU_malloc(0x8000*sizeof(writefunc))))
121 void FlushGenieRW(void)
127 for(x=0;x<0x8000;x++)
129 ARead[x+0x8000]=AReadG[x];
130 BWrite[x+0x8000]=BWriteG[x];
140 readfunc FASTAPASS(1) GetReadHandler(int32 a)
142 if(a>=0x8000 && RWWrap)
143 return AReadG[a-0x8000];
148 void FASTAPASS(3) SetReadHandler(int32 start, int32 end, readfunc func)
156 for(x=end;x>=start;x--)
159 AReadG[x-0x8000]=func;
165 for(x=end;x>=start;x--)
169 writefunc FASTAPASS(1) GetWriteHandler(int32 a)
171 if(RWWrap && a>=0x8000)
172 return BWriteG[a-0x8000];
177 void FASTAPASS(3) SetWriteHandler(int32 start, int32 end, writefunc func)
185 for(x=end;x>=start;x--)
188 BWriteG[x-0x8000]=func;
193 for(x=end;x>=start;x--)
200 uint32 TempAddr,RefreshAddr;
203 /* scanline is equal to the current visible scanline we're on. */
206 static uint32 scanlines_per_frame;
208 uint8 GameMemBlock[131072] __attribute__ ((aligned (4)));
209 uint8 NTARAM[0x800] __attribute__ ((aligned (4)));
210 uint8 PALRAM[0x20] __attribute__ ((aligned (4)));
212 uint8 RAM[0x800] __attribute__ ((aligned (4)));
221 #define MMC5BGVRAMADR(V) &MMC5BGVPage[(V)>>10][(V)]
222 #define VRAMADR(V) &VPage[(V)>>10][(V)]
251 return ret|(PPUGenLatch&0x1F);
262 uint32 tmp=RefreshAddr&0x3FFF;
264 PPUGenLatch=ret=VRAMBuffer;
265 if(PPU_hook) PPU_hook(tmp);
268 VRAMBuffer=VPage[tmp>>10][tmp];
272 VRAMBuffer=vnapage[(tmp>>10)&0x3][tmp&0x3FF];
275 if (INC32) RefreshAddr+=32;
277 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
295 //printf("$%04x:$%02x, %d\n",X.PC,V,scanline);
321 //printf("$%02x:$%02x\n",PPUSPL,V);
357 TempAddr|=(V&0x3f)<<8;
363 RefreshAddr=TempAddr;
366 PPU_hook(RefreshAddr);
373 uint32 tmp=RefreshAddr&0x3FFF;
380 PALRAM[0x00]=PALRAM[0x04]=PALRAM[0x08]=PALRAM[0x0C]=
381 PALRAM[0x10]=PALRAM[0x14]=PALRAM[0x18]=PALRAM[0x1c]=V&0x3f;
382 else if(tmp&3) PALRAM[(tmp&0x1f)]=V&0x3f;
386 if(PPUCHRRAM&(1<<(tmp>>10)))
387 VPage[tmp>>10][tmp]=V;
391 if(PPUNTARAM&(1<<((tmp&0xF00)>>10)))
392 vnapage[((tmp&0xF00)>>10)][tmp&0x3FF]=V;
394 if (INC32) RefreshAddr+=32;
396 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
404 B2004(0x2004,X.DB=ARead[t+x](t+x));
405 X6502_AddCycles(512);
408 void BGRender(uint8 *target)
411 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
417 RefreshLine(target-XOffset, vofs);
428 *(uint32 *)target=*(uint32 *)(target+4)=tem;
433 int FSkip_setting=-1; // auto
435 void FCEUI_FrameSkip(int x)
441 /* This is called at the beginning of each visible scanline */
442 static void Loop6502(void)
446 uint8 *target=XBuf+scanline*320+32;
448 if(ScreenON || SpriteON)
450 /* PRefreshLine() will not get called on skipped frames. This
451 could cause a problem, but the solution would be rather complex,
452 due to the current sprite 0 hit code.
460 if(scanline>=FSettings.FirstSLine && scanline<=FSettings.LastSLine)
470 tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
472 FCEU_dwmemset(target,tem,264);
477 if (SpriteON && scanline)
478 RefreshSprite(target);
486 ((uint32 *)target)[x]=((uint32*)target)[x]&0xF0F0F0F0;
490 ((uint32 *)target)[x]=(((uint32*)target)[x]&0x3f3f3f3f)|0x40404040;
493 ((uint32 *)target)[x]=((uint32*)target)[x]|0xC0C0C0C0;
496 ((uint32 *)target)[x]=((uint32*)target)[x]&0x3f3f3f3f;
497 FCEU_dwmemset(target- 8,0x3f3f3f3f,8);
498 FCEU_dwmemset(target+256,0x3f3f3f3f,8);
505 tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
506 FCEU_dwmemset(target,tem,256);
508 if(InputScanlineHook)
509 InputScanlineHook(target, scanline);
512 #define PAL(c) ((c)+cc)
515 static void PRefreshLine(void)
521 if (BGAdrHI) vofs = 0x1000;
523 vofs+=(RefreshAddr>>12)&7;
529 zz2=(uint8)((RefreshAddr>>10)&3);
530 PPU_hook(0x2000|(RefreshAddr&0xFFF));
531 no = vnapage[zz2][(RefreshAddr&0x3ff)];
532 PPU_hook((no<<4)+vofs);
533 if((RefreshAddr&0x1f)==0x1f)
540 /* This high-level graphics MMC5 emulation code was written
541 for MMC5 carts in "CL" mode. It's probably not totally
542 correct for carts in "SL" mode.
544 static void RefreshLine_MMC5Hack1(uint8 *P, uint32 vofs)
548 tochange=MMC5HackSPMode&0x1F;
550 for(X1=33;X1;X1--,P+=8)
556 if((tochange<=0 && MMC5HackSPMode&0x40) ||
557 (tochange>0 && !(MMC5HackSPMode&0x40)))
562 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
563 if(ys>=0x1E) ys-=0x1E;
564 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
566 C = MMC5HackVROMPTR+vadr;
567 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
569 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
570 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
575 zz2=(RefreshAddr>>10)&3;
576 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
577 C = MMC5BGVRAMADR(vadr);
578 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
579 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
583 if((RefreshAddr&0x1f)==0x1f)
591 static void RefreshLine_MMC5Hack2(uint8 *P, uint32 vofs)
595 tochange=MMC5HackSPMode&0x1F;
597 for(X1=33;X1;X1--,P+=8)
604 if((tochange<=0 && MMC5HackSPMode&0x40) ||
605 (tochange>0 && !(MMC5HackSPMode&0x40)))
610 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
611 if(ys>=0x1E) ys-=0x1E;
612 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
614 C = MMC5HackVROMPTR+vadr;
615 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
617 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
618 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
623 zz2=(RefreshAddr>>10)&3;
624 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
625 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
626 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
627 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
631 if((RefreshAddr&0x1f)==0x1f)
639 static void RefreshLine_MMC5Hack3(uint8 *P, uint32 vofs)
643 for(X1=33;X1;X1--,P+=8)
651 zz2=(RefreshAddr>>10)&3;
652 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
653 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
654 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
655 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
659 if((RefreshAddr&0x1f)==0x1f)
666 static void RefreshLine_MMC5Hack4(uint8 *P, uint32 vofs)
670 for(X1=33;X1;X1--,P+=8)
677 zz2=(RefreshAddr>>10)&3;
678 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
679 C = MMC5BGVRAMADR(vadr);
680 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
681 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
685 if((RefreshAddr&0x1f)==0x1f)
692 static void RefreshLine_PPU_hook(uint8 *P, uint32 vofs)
696 for(X1=33;X1;X1--,P+=8)
703 zz2=(RefreshAddr>>10)&3;
704 PPU_hook(0x2000|(RefreshAddr&0xFFF));
705 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
706 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
707 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
714 if((RefreshAddr&0x1f)==0x1f)
721 static void RefreshLine_normal(uint8 *P, uint32 vofs) // vofs is 0x107 max
724 uint32 rfraddr = RefreshAddr;
725 uint8 *page = vnapage[(rfraddr>>10)&3];
728 if ((rfraddr&0xc)!=0)
729 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
731 for (X1=33;X1;X1--,P+=8)
736 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
738 if ((rfraddr&0xc)==0)
739 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
740 cc=((cc2 >> ((rfraddr&2) + ((rfraddr&0x40)>>4) + ((rfraddr&0xc)<<1))) & 3) << 2;
744 if((rfraddr&0x1f)==0x1f) {
746 page = vnapage[(rfraddr>>10)&3];
750 RefreshAddr = rfraddr;
753 static void SetRefreshLine(void)
755 if(MMC5Hack && geniestage!=1)
757 if(MMC5HackCHRMode==0 && (MMC5HackSPMode&0x80))
759 if (RefreshLine != RefreshLine_MMC5Hack1) printf("set refr RefreshLine_MMC5Hack1\n");
760 RefreshLine = RefreshLine_MMC5Hack1;
762 else if(MMC5HackCHRMode==1 && (MMC5HackSPMode&0x80))
764 if (RefreshLine != RefreshLine_MMC5Hack2) printf("set refr RefreshLine_MMC5Hack2\n");
765 RefreshLine = RefreshLine_MMC5Hack2;
767 else if(MMC5HackCHRMode==1)
769 if (RefreshLine != RefreshLine_MMC5Hack3) printf("set refr RefreshLine_MMC5Hack3\n");
770 RefreshLine = RefreshLine_MMC5Hack3;
774 if (RefreshLine != RefreshLine_MMC5Hack4) printf("set refr RefreshLine_MMC5Hack4\n");
775 RefreshLine = RefreshLine_MMC5Hack4;
777 } // End if(MMC5Hack)
780 if (RefreshLine != RefreshLine_PPU_hook) printf("set refr RefreshLine_PPU_hook\n");
781 RefreshLine = RefreshLine_PPU_hook;
785 if (RefreshLine != RefreshLine_normal) printf("set refr RefreshLine_normal\n");
786 RefreshLine = RefreshLine_normal;
793 if(ScreenON || SpriteON)
795 uint32 rad=RefreshAddr;
797 rad|=TempAddr&0x041f;
799 //PPU_hook(RefreshAddr,-1);
806 if(ScreenON || SpriteON)
808 uint32 rad=RefreshAddr;
810 if((rad&0x7000)==0x7000)
813 if((rad&0x3E0)==0x3A0)
820 if((rad&0x3E0)==0x3e0)
828 //PPU_hook(RefreshAddr,-1);
834 /* This is called at the beginning of all h-blanks on visible lines. */
836 static void DoHBlank(void)
838 if(ScreenON || SpriteON)
840 if(GameHBIRQHook && (ScreenON || SpriteON))
850 X6502_Run(25); // Tried 65, caused problems with Slalom(maybe others)
855 //fprintf(stderr,"%3d: $%04x\n",scanline,RefreshAddr);
860 // ============================//
862 // ===========================//
864 void ResetMapping(void)
868 SetReadHandler(0x0000,0xFFFF,ANull);
869 SetWriteHandler(0x0000,0xFFFF,BNull);
871 SetReadHandler(0,0x7FF,ARAML);
872 SetWriteHandler(0,0x7FF,BRAML);
874 SetReadHandler(0x800,0x1FFF,ARAMH); /* Part of a little */
875 SetWriteHandler(0x800,0x1FFF,BRAMH); /* hack for a small speed boost. */
877 for(x=0x2000;x<0x4000;x+=8)
897 BWrite[0x4014]=B4014;
907 if(FCEUGameInfo.type!=GIT_NSF)
910 if(FSettings.NetworkPlay) KillNetplay();
912 GameInterface(GI_CLOSE);
918 void ResetGameLoaded(void)
920 if(GameLoaded) CloseGame();
925 GameExpSound.RChange=0;
926 if(GameExpSound.Kill)
935 FCEUGameInfo.type=GIT_CART;
936 FCEUGameInfo.vidsys=GIV_USER;
937 FCEUGameInfo.input[0]=FCEUGameInfo.input[1]=-1;
938 FCEUGameInfo.inputfc=-1;
941 FCEUGI *FCEUI_LoadGame(char *name)
948 fp=FCEU_fopen(name,"rb");
951 FCEU_PrintError("Error opening \"%s\"!",name);
956 if(iNESLoad(name,fp))
962 if(UNIFLoad(name,fp))
965 FCEU_PrintError("An error occurred while loading the file.");
974 if(FCEUGameInfo.type!=GIT_NSF)
975 if(FSettings.GameGenie)
980 if(FSettings.NetworkPlay) InitNetplay();
983 if(FCEUGameInfo.type!=GIT_NSF)
991 return(&FCEUGameInfo);
995 void FCEU_ResetVidSys(void)
999 if(FCEUGameInfo.vidsys==GIV_NTSC)
1001 else if(FCEUGameInfo.vidsys==GIV_PAL)
1009 scanlines_per_frame=312;
1010 FSettings.FirstSLine=FSettings.UsrFirstSLine[1];
1011 FSettings.LastSLine=FSettings.UsrLastSLine[1];
1016 scanlines_per_frame=262;
1017 FSettings.FirstSLine=FSettings.UsrFirstSLine[0];
1018 FSettings.LastSLine=FSettings.UsrLastSLine[0];
1020 printf("PAL = %i\n", PAL);
1021 SetSoundVariables();
1024 int FCEUI_Initialize(void)
1026 if(!InitVirtualVideo())
1028 memset(&FSettings,0,sizeof(FSettings));
1029 FSettings.UsrFirstSLine[0]=8;
1030 FSettings.UsrFirstSLine[1]=0;
1031 FSettings.UsrLastSLine[0]=FSettings.UsrLastSLine[1]=239;
1032 FSettings.SoundVolume=65535; // 100%
1036 #define harko 0xe //0x9
1037 static INLINE void Thingo(void)
1044 X6502_Run(256-harko);
1052 X6502_Run(tosprite);
1054 X6502_Run(256-tosprite-harko);
1060 X6502_Run(256-harko);
1062 X6502_Run(tosprite-(256-harko));
1064 X6502_Run(256-tosprite);
1070 X6502_Run_scanline();
1080 //printf("asdc: %i\n", asdc);
1082 ApplyPeriodicCheats();
1086 PPU[3]=PPUSPL=0; /* Not sure if this is correct. According
1087 to Matt Conte and my own tests, it is. Timing is probably
1088 off, though. NOTE: Not having this here
1089 breaks a Super Donkey Kong game. */
1091 X6502_Run(12); /* I need to figure out the true nature and length
1094 if(FCEUGameInfo.type==GIT_NSF)
1099 X6502_Run((scanlines_per_frame-242)*(256+85)-12);
1106 if(ScreenON || SpriteON)
1114 if(ScreenON || SpriteON)
1116 RefreshAddr=TempAddr;
1117 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
1119 if(FCEUGameInfo.type==GIT_NSF)
1121 X6502_Run((256+85)*240);
1129 for(scanline=0;scanline<240;scanline++)
1134 for(x=1,max=0,maxref=0;x<7;x++)
1143 //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);
1144 //memset(deempcnt,0,sizeof(deempcnt));
1145 SetNESDeemph(maxref,0);
1151 ssize=FlushEmulateSound();
1156 FCEU_PutImageDummy();
1158 FCEUD_Update(0,WaveFinalMono,ssize);
1164 FCEUD_Update(XBuf+8,WaveFinalMono,ssize);
1179 #include <sys/time.h>
1182 void FCEUI_Emulate(void)
1185 uint64 starttime,end;
1188 gettimeofday(&tv,0);
1189 starttime=((uint64)tv.tv_sec*1000000)+tv.tv_usec;
1194 // Probably won't work well on Windows port; for
1195 // debugging/speed testing.
1199 gettimeofday(&tv,0);
1200 end=((uint64)tv.tv_sec*1000000)+tv.tv_usec;
1201 w=frcount*10000000000LL/(end-starttime);
1204 printf("Average FPS: %d.%04d\n",i,frac);
1210 void FCEUI_CloseGame(void)
1215 static void ResetPPU(void)
1217 VRAMBuffer=PPU[0]=PPU[1]=PPU[2]=PPU[3]=0;
1220 RefreshAddr=TempAddr=0;
1224 static void PowerPPU(void)
1226 memset(NTARAM,0x00,0x800);
1227 memset(PALRAM,0x00,0x20);
1228 memset(SPRAM,0x00,0x100);
1234 if(!GameLoaded || (FCEUGameInfo.type==GIT_NSF)) return;
1235 GameInterface(GI_RESETM2);
1243 if(!GameLoaded) return;
1245 FCEU_CheatResetRAM();
1246 FCEU_CheatAddRAM(2,0,RAM);
1250 memset(RAM,0x00,0x800);
1252 GameInterface(GI_POWER);