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
56 static void (*RefreshLine)(uint8 *P, uint32 vofs) = NULL;
57 static void PRefreshLine(void);
59 static void ResetPPU(void);
60 static void PowerPPU(void);
62 uint64 timestampbase=0;
65 uint32 MMC5HackVROMMask;
66 uint8 *MMC5HackExNTARAMPtr;
67 uint8 *MMC5HackVROMPTR;
68 uint8 MMC5HackCHRMode=0;
70 uint8 MMC5HackSPScroll;
73 uint8 *MMC5SPRVPage[8];
74 uint8 *MMC5BGVPage[8];
77 uint8 VRAMBuffer,PPUGenLatch;
83 /* Color deemphasis emulation. Joy... */
85 static int deempcnt[8];
90 void (*GameInterface)(int h);
92 void FP_FASTAPASS(1) (*PPU_hook)(uint32 A);
94 void (*GameStateRestore)(int version);
95 void (*GameHBIRQHook)(void);
97 readfunc ARead[0x10000];
98 writefunc BWrite[0x10000];
99 static readfunc *AReadG;
100 static writefunc *BWriteG;
104 static void asmcpu_update(int32 cycles)
107 timestamp += ((cycles >> 4) * 43) >> 7; // aproximating /= 48
109 // some code from x6502.c
123 if((PSG[0x10]&0x80) && !(PSG[0x10]&0x40))
125 extern uint8 SIRQStat;
127 X6502_IRQBegin(FCEU_IQDPCM);
133 void asmcpu_unpack(void)
135 nes_registers[0] = X.A << 24;
136 nes_registers[1] = X.X;
137 nes_registers[2] = X.Y;
139 nes_registers[3] = X.PC;
141 nes_registers[4] = X.S << 24;
142 nes_registers[4]|= X.IRQlow << 8;
143 nes_registers[7] = (uint32)X.count;
146 nes_registers[4]|= X.P & 0x5d;
147 nes_registers[5] = X.P << 24; // N
148 if (!(X.P&0x02)) nes_registers[5] |= 1; // Z
151 void asmcpu_pack(void)
153 X.A = nes_registers[0] >> 24;
154 X.X = nes_registers[1];
155 X.Y = nes_registers[2];
156 X.PC= nes_registers[3] - pc_base;
157 X.S = nes_registers[4] >> 24;
158 X.IRQlow = nes_registers[4] >> 8;
159 X.count = (int32) nes_registers[7];
162 X.P = nes_registers[4] & 0x5d;
163 if ( nes_registers[5]&0x80000000) X.P |= 0x80; // N
164 if (!(nes_registers[5]&0x000000ff)) X.P |= 0x02; // Z
178 int AllocGenieRW(void)
180 if(!(AReadG=FCEU_malloc(0x8000*sizeof(readfunc))))
182 if(!(BWriteG=FCEU_malloc(0x8000*sizeof(writefunc))))
188 void FlushGenieRW(void)
194 for(x=0;x<0x8000;x++)
196 ARead[x+0x8000]=AReadG[x];
197 BWrite[x+0x8000]=BWriteG[x];
207 readfunc FASTAPASS(1) GetReadHandler(int32 a)
209 if(a>=0x8000 && RWWrap)
210 return AReadG[a-0x8000];
215 void FASTAPASS(3) SetReadHandler(int32 start, int32 end, readfunc func)
223 for(x=end;x>=start;x--)
226 AReadG[x-0x8000]=func;
232 for(x=end;x>=start;x--)
236 writefunc FASTAPASS(1) GetWriteHandler(int32 a)
238 if(RWWrap && a>=0x8000)
239 return BWriteG[a-0x8000];
244 void FASTAPASS(3) SetWriteHandler(int32 start, int32 end, writefunc func)
252 for(x=end;x>=start;x--)
255 BWriteG[x-0x8000]=func;
260 for(x=end;x>=start;x--)
267 uint32 TempAddr,RefreshAddr;
270 /* scanline is equal to the current visible scanline we're on. */
274 uint8 GameMemBlock[131072] __attribute__ ((aligned (4)));
275 uint8 NTARAM[0x800] __attribute__ ((aligned (4)));
276 uint8 PALRAM[0x20] __attribute__ ((aligned (4)));
277 #if !defined(ASM_6502) || defined(DEBUG_ASM_6502)
278 uint8 RAM[0x800] __attribute__ ((aligned (4)));
287 #define MMC5BGVRAMADR(V) &MMC5BGVPage[(V)>>10][(V)]
288 #define VRAMADR(V) &VPage[(V)>>10][(V)]
317 return ret|(PPUGenLatch&0x1F);
328 uint32 tmp=RefreshAddr&0x3FFF;
330 PPUGenLatch=ret=VRAMBuffer;
331 if(PPU_hook) PPU_hook(tmp);
334 VRAMBuffer=VPage[tmp>>10][tmp];
338 VRAMBuffer=vnapage[(tmp>>10)&0x3][tmp&0x3FF];
341 if (INC32) RefreshAddr+=32;
343 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
361 //printf("$%04x:$%02x, %d\n",X.PC,V,scanline);
387 //printf("$%02x:$%02x\n",PPUSPL,V);
423 TempAddr|=(V&0x3f)<<8;
429 RefreshAddr=TempAddr;
432 PPU_hook(RefreshAddr);
439 uint32 tmp=RefreshAddr&0x3FFF;
446 PALRAM[0x00]=PALRAM[0x04]=PALRAM[0x08]=PALRAM[0x0C]=
447 PALRAM[0x10]=PALRAM[0x14]=PALRAM[0x18]=PALRAM[0x1c]=V&0x3f;
448 else if(tmp&3) PALRAM[(tmp&0x1f)]=V&0x3f;
452 if(PPUCHRRAM&(1<<(tmp>>10)))
453 VPage[tmp>>10][tmp]=V;
457 if(PPUNTARAM&(1<<((tmp&0xF00)>>10)))
458 vnapage[((tmp&0xF00)>>10)][tmp&0x3FF]=V;
460 if (INC32) RefreshAddr+=32;
462 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
470 B2004(0x2004,X.DB=ARead[t+x](t+x));
471 X6502_AddCycles(512);
474 void BGRender(uint8 *target)
477 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
483 RefreshLine(target-XOffset, vofs);
494 *(uint32 *)target=*(uint32 *)(target+4)=tem;
499 int FSkip_setting=-1; // auto
501 void FCEUI_FrameSkip(int x)
507 /* This is called at the beginning of each visible scanline */
508 static void Loop6502(void)
512 uint8 *target=XBuf+scanline*320+32;
514 if(ScreenON || SpriteON)
516 /* PRefreshLine() will not get called on skipped frames. This
517 could cause a problem, but the solution would be rather complex,
518 due to the current sprite 0 hit code.
526 if(scanline>=FSettings.FirstSLine && scanline<=FSettings.LastSLine)
536 tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
538 FCEU_dwmemset(target,tem,264);
543 if (SpriteON && scanline)
544 RefreshSprite(target);
552 ((uint32 *)target)[x]=((uint32*)target)[x]&0xF0F0F0F0;
556 ((uint32 *)target)[x]=(((uint32*)target)[x]&0x3f3f3f3f)|0x40404040;
559 ((uint32 *)target)[x]=((uint32*)target)[x]|0xC0C0C0C0;
562 ((uint32 *)target)[x]=((uint32*)target)[x]&0x3f3f3f3f;
563 FCEU_dwmemset(target- 8,0x3f3f3f3f,8);
564 FCEU_dwmemset(target+256,0x3f3f3f3f,8);
571 tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
572 FCEU_dwmemset(target,tem,256);
574 if(InputScanlineHook)
575 InputScanlineHook(target, scanline);
578 #define PAL(c) ((c)+cc)
581 static void PRefreshLine(void)
587 if (BGAdrHI) vofs = 0x1000;
589 vofs+=(RefreshAddr>>12)&7;
595 zz2=(uint8)((RefreshAddr>>10)&3);
596 PPU_hook(0x2000|(RefreshAddr&0xFFF));
597 no = vnapage[zz2][(RefreshAddr&0x3ff)];
598 PPU_hook((no<<4)+vofs);
599 if((RefreshAddr&0x1f)==0x1f)
606 /* This high-level graphics MMC5 emulation code was written
607 for MMC5 carts in "CL" mode. It's probably not totally
608 correct for carts in "SL" mode.
610 static void RefreshLine_MMC5Hack1(uint8 *P, uint32 vofs)
614 tochange=MMC5HackSPMode&0x1F;
616 for(X1=33;X1;X1--,P+=8)
622 if((tochange<=0 && MMC5HackSPMode&0x40) ||
623 (tochange>0 && !(MMC5HackSPMode&0x40)))
628 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
629 if(ys>=0x1E) ys-=0x1E;
630 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
632 C = MMC5HackVROMPTR+vadr;
633 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
635 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
636 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
641 zz2=(RefreshAddr>>10)&3;
642 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
643 C = MMC5BGVRAMADR(vadr);
644 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
645 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
649 if((RefreshAddr&0x1f)==0x1f)
657 static void RefreshLine_MMC5Hack2(uint8 *P, uint32 vofs)
661 tochange=MMC5HackSPMode&0x1F;
663 for(X1=33;X1;X1--,P+=8)
670 if((tochange<=0 && MMC5HackSPMode&0x40) ||
671 (tochange>0 && !(MMC5HackSPMode&0x40)))
676 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
677 if(ys>=0x1E) ys-=0x1E;
678 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
680 C = MMC5HackVROMPTR+vadr;
681 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
683 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
684 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
689 zz2=(RefreshAddr>>10)&3;
690 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
691 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
692 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
693 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
697 if((RefreshAddr&0x1f)==0x1f)
705 static void RefreshLine_MMC5Hack3(uint8 *P, uint32 vofs)
709 for(X1=33;X1;X1--,P+=8)
717 zz2=(RefreshAddr>>10)&3;
718 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
719 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
720 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
721 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
725 if((RefreshAddr&0x1f)==0x1f)
732 static void RefreshLine_MMC5Hack4(uint8 *P, uint32 vofs)
736 for(X1=33;X1;X1--,P+=8)
743 zz2=(RefreshAddr>>10)&3;
744 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
745 C = MMC5BGVRAMADR(vadr);
746 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
747 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
751 if((RefreshAddr&0x1f)==0x1f)
758 static void RefreshLine_PPU_hook(uint8 *P, uint32 vofs)
762 for(X1=33;X1;X1--,P+=8)
769 zz2=(RefreshAddr>>10)&3;
770 PPU_hook(0x2000|(RefreshAddr&0xFFF));
771 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
772 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
773 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
780 if((RefreshAddr&0x1f)==0x1f)
787 static void RefreshLine_normal(uint8 *P, uint32 vofs) // vofs is 0x107 max
790 uint32 rfraddr = RefreshAddr;
791 uint8 *page = vnapage[(rfraddr>>10)&3];
794 if ((rfraddr&0xc)!=0)
795 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
797 for (X1=33;X1;X1--,P+=8)
802 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
804 if ((rfraddr&0xc)==0)
805 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
806 cc=((cc2 >> ((rfraddr&2) + ((rfraddr&0x40)>>4) + ((rfraddr&0xc)<<1))) & 3) << 2;
810 if((rfraddr&0x1f)==0x1f) {
812 page = vnapage[(rfraddr>>10)&3];
816 RefreshAddr = rfraddr;
819 static void SetRefreshLine(void)
821 if(MMC5Hack && geniestage!=1)
823 if(MMC5HackCHRMode==0 && (MMC5HackSPMode&0x80))
825 if (RefreshLine != RefreshLine_MMC5Hack1) printf("set refr RefreshLine_MMC5Hack1\n");
826 RefreshLine = RefreshLine_MMC5Hack1;
828 else if(MMC5HackCHRMode==1 && (MMC5HackSPMode&0x80))
830 if (RefreshLine != RefreshLine_MMC5Hack2) printf("set refr RefreshLine_MMC5Hack2\n");
831 RefreshLine = RefreshLine_MMC5Hack2;
833 else if(MMC5HackCHRMode==1)
835 if (RefreshLine != RefreshLine_MMC5Hack3) printf("set refr RefreshLine_MMC5Hack3\n");
836 RefreshLine = RefreshLine_MMC5Hack3;
840 if (RefreshLine != RefreshLine_MMC5Hack4) printf("set refr RefreshLine_MMC5Hack4\n");
841 RefreshLine = RefreshLine_MMC5Hack4;
843 } // End if(MMC5Hack)
846 if (RefreshLine != RefreshLine_PPU_hook) printf("set refr RefreshLine_PPU_hook\n");
847 RefreshLine = RefreshLine_PPU_hook;
851 if (RefreshLine != RefreshLine_normal) printf("set refr RefreshLine_normal\n");
852 RefreshLine = RefreshLine_normal;
859 if(ScreenON || SpriteON)
861 uint32 rad=RefreshAddr;
863 rad|=TempAddr&0x041f;
865 //PPU_hook(RefreshAddr,-1);
872 if(ScreenON || SpriteON)
874 uint32 rad=RefreshAddr;
876 if((rad&0x7000)==0x7000)
879 if((rad&0x3E0)==0x3A0)
886 if((rad&0x3E0)==0x3e0)
894 //PPU_hook(RefreshAddr,-1);
900 /* This is called at the beginning of all h-blanks on visible lines. */
902 static void DoHBlank(void)
904 if(ScreenON || SpriteON)
906 if(GameHBIRQHook && (ScreenON || SpriteON))
916 X6502_Run(25); // Tried 65, caused problems with Slalom(maybe others)
921 //fprintf(stderr,"%3d: $%04x\n",scanline,RefreshAddr);
926 // ============================//
928 // ===========================//
930 void ResetMapping(void)
934 SetReadHandler(0x0000,0xFFFF,ANull);
935 SetWriteHandler(0x0000,0xFFFF,BNull);
937 SetReadHandler(0,0x7FF,ARAML);
938 SetWriteHandler(0,0x7FF,BRAML);
940 SetReadHandler(0x800,0x1FFF,ARAMH); /* Part of a little */
941 SetWriteHandler(0x800,0x1FFF,BRAMH); /* hack for a small speed boost. */
943 for(x=0x2000;x<0x4000;x+=8)
963 BWrite[0x4014]=B4014;
973 if(FCEUGameInfo.type!=GIT_NSF)
976 if(FSettings.NetworkPlay) KillNetplay();
978 GameInterface(GI_CLOSE);
984 void ResetGameLoaded(void)
986 if(GameLoaded) CloseGame();
991 GameExpSound.RChange=0;
992 if(GameExpSound.Kill)
1000 FCEUGameInfo.name=0;
1001 FCEUGameInfo.type=GIT_CART;
1002 FCEUGameInfo.vidsys=GIV_USER;
1003 FCEUGameInfo.input[0]=FCEUGameInfo.input[1]=-1;
1004 FCEUGameInfo.inputfc=-1;
1007 char lastLoadedGameName [2048];
1009 FCEUGI *FCEUI_LoadGame(char *name)
1018 strncpy(name2, name, sizeof(name2));
1019 name2[sizeof(name2)-1] = 0;
1021 fp=FCEU_fopen(name2,"rb");
1024 FCEU_PrintError("Error opening \"%s\"!",name);
1029 char *p = name2 + strlen(name2) - 4;
1030 if (strcmp(p, ".fcm") == 0)
1033 printf("movie detected\n");
1036 fp=FCEU_fopen(name2,"rb");
1038 printf("no ROM for movie\n");
1045 strcpy(lastLoadedGameName, name2);
1048 if(iNESLoad(name2,fp))
1052 if(FDSLoad(name2,fp))
1054 if(UNIFLoad(name2,fp))
1057 FCEU_PrintError("An error occurred while loading the file.");
1066 if(FCEUGameInfo.type!=GIT_NSF)
1067 if(FSettings.GameGenie)
1072 if(FSettings.NetworkPlay) InitNetplay();
1075 if(FCEUGameInfo.type!=GIT_NSF)
1081 FCEU_ResetPalette();
1085 FCEUI_LoadMovie(name, 1);
1086 return(&FCEUGameInfo);
1090 void FCEU_ResetVidSys(void)
1094 if(FCEUGameInfo.vidsys==GIV_NTSC)
1096 else if(FCEUGameInfo.vidsys==GIV_PAL)
1104 FSettings.FirstSLine=FSettings.UsrFirstSLine[1];
1105 FSettings.LastSLine=FSettings.UsrLastSLine[1];
1110 FSettings.FirstSLine=FSettings.UsrFirstSLine[0];
1111 FSettings.LastSLine=FSettings.UsrLastSLine[0];
1113 printf("PAL = %i\n", PAL);
1114 SetSoundVariables();
1117 int FCEUI_Initialize(void)
1119 if(!InitVirtualVideo())
1121 memset(&FSettings,0,sizeof(FSettings));
1122 FSettings.UsrFirstSLine[0]=8;
1123 FSettings.UsrFirstSLine[1]=0;
1124 FSettings.UsrLastSLine[0]=FSettings.UsrLastSLine[1]=239;
1125 FSettings.SoundVolume=65535; // 100%
1129 #define harko 0xe //0x9
1130 static INLINE void Thingo(void)
1137 X6502_Run(256-harko);
1145 X6502_Run(tosprite);
1147 X6502_Run(256-tosprite-harko);
1153 X6502_Run(256-harko);
1155 X6502_Run(tosprite-(256-harko));
1157 X6502_Run(256-tosprite);
1163 X6502_Run_scanline();
1172 uint32 scanlines_per_frame = PAL ? 312 : 262;
1174 //printf("asdc: %i\n", asdc);
1176 ApplyPeriodicCheats();
1180 PPU[3]=PPUSPL=0; /* Not sure if this is correct. According
1181 to Matt Conte and my own tests, it is. Timing is probably
1182 off, though. NOTE: Not having this here
1183 breaks a Super Donkey Kong game. */
1185 X6502_Run(12); /* I need to figure out the true nature and length
1188 if(FCEUGameInfo.type==GIT_NSF)
1193 // Note: this is needed for asm core
1194 // Warning: using 'scanline' var here breaks Castlevania III
1197 X6502_Run(256+85-12);
1198 for (lines=scanlines_per_frame-242-1;lines;lines--)
1201 // X6502_Run((scanlines_per_frame-242)*(256+85)-12);
1208 if(ScreenON || SpriteON)
1216 if(ScreenON || SpriteON)
1218 RefreshAddr=TempAddr;
1219 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
1221 if(FCEUGameInfo.type==GIT_NSF)
1223 X6502_Run((256+85)*240);
1231 for(scanline=0;scanline<240;scanline++)
1236 for(x=1,max=0,maxref=0;x<7;x++)
1245 //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);
1246 //memset(deempcnt,0,sizeof(deempcnt));
1247 SetNESDeemph(maxref,0);
1253 ssize=FlushEmulateSound();
1258 FCEU_PutImageDummy();
1260 FCEUD_Update(0,WaveFinalMono,ssize);
1266 FCEUD_Update(XBuf+8,WaveFinalMono,ssize);
1281 #include <sys/time.h>
1284 void FCEUI_Emulate(void)
1287 uint64 starttime,end;
1290 gettimeofday(&tv,0);
1291 starttime=((uint64)tv.tv_sec*1000000)+tv.tv_usec;
1296 // Probably won't work well on Windows port; for
1297 // debugging/speed testing.
1301 gettimeofday(&tv,0);
1302 end=((uint64)tv.tv_sec*1000000)+tv.tv_usec;
1303 w=frcount*10000000000LL/(end-starttime);
1306 printf("Average FPS: %d.%04d\n",i,frac);
1312 void FCEUI_CloseGame(void)
1317 static void ResetPPU(void)
1319 VRAMBuffer=PPU[0]=PPU[1]=PPU[2]=PPU[3]=0;
1322 RefreshAddr=TempAddr=0;
1326 static void PowerPPU(void)
1328 memset(NTARAM,0x00,0x800);
1329 memset(PALRAM,0x00,0x20);
1330 memset(SPRAM,0x00,0x100);
1336 if(!GameLoaded || (FCEUGameInfo.type==GIT_NSF)) return;
1337 GameInterface(GI_RESETM2);
1345 if(!GameLoaded) return;
1347 FCEU_CheatResetRAM();
1348 FCEU_CheatAddRAM(2,0,RAM);
1352 memset(RAM,0x00,0x800);
1354 GameInterface(GI_POWER);