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 FASTAPASS(1) RefreshLine(uint8 *target);
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;
211 uint8 GameMemBlock[131072];
212 uint8 NTARAM[0x800],PALRAM[0x20];
218 #define MMC5BGVRAMADR(V) &MMC5BGVPage[(V)>>10][(V)]
219 #define VRAMADR(V) &VPage[(V)>>10][(V)]
248 return ret|(PPUGenLatch&0x1F);
259 uint32 tmp=RefreshAddr&0x3FFF;
261 PPUGenLatch=ret=VRAMBuffer;
262 if(PPU_hook) PPU_hook(tmp);
265 VRAMBuffer=VPage[tmp>>10][tmp];
269 VRAMBuffer=vnapage[(tmp>>10)&0x3][tmp&0x3FF];
272 if (INC32) RefreshAddr+=32;
274 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
292 //printf("$%04x:$%02x, %d\n",X.PC,V,scanline);
318 //printf("$%02x:$%02x\n",PPUSPL,V);
354 TempAddr|=(V&0x3f)<<8;
360 RefreshAddr=TempAddr;
363 PPU_hook(RefreshAddr);
370 uint32 tmp=RefreshAddr&0x3FFF;
377 PALRAM[0x00]=PALRAM[0x04]=PALRAM[0x08]=PALRAM[0x0C]=
378 PALRAM[0x10]=PALRAM[0x14]=PALRAM[0x18]=PALRAM[0x1c]=V&0x3f;
379 else if(tmp&3) PALRAM[(tmp&0x1f)]=V&0x3f;
383 if(PPUCHRRAM&(1<<(tmp>>10)))
384 VPage[tmp>>10][tmp]=V;
388 if(PPUNTARAM&(1<<((tmp&0xF00)>>10)))
389 vnapage[((tmp&0xF00)>>10)][tmp&0x3FF]=V;
391 if (INC32) RefreshAddr+=32;
393 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
401 B2004(0x2004,X.DB=ARead[t+x](t+x));
402 X6502_AddCycles(512);
405 void BGRender(uint8 *target)
411 tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
413 *(uint32 *)target=*(uint32 *)(target+4)=tem;
418 int FSkip_setting=-1; // auto
420 void FCEUI_FrameSkip(int x)
426 /* This is called at the beginning of each visible scanline */
427 static void Loop6502(void)
431 uint8 *target=XBuf+(scanline<<8)+(scanline<<4)+8;
433 if(ScreenON || SpriteON)
435 /* PRefreshLine() will not get called on skipped frames. This
436 could cause a problem, but the solution would be rather complex,
437 due to the current sprite 0 hit code.
445 if(scanline>=FSettings.FirstSLine && scanline<=FSettings.LastSLine)
455 tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
457 FCEU_dwmemset(target,tem,264);
462 if (SpriteON && scanline)
463 RefreshSprite(target);
471 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0xF0F0F0F0;
475 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0x40404040;
478 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])|0xC0C0C0C0;
481 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0x3f3f3f3f;
489 tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
490 FCEU_dwmemset(target,tem,256);
492 if(InputScanlineHook)
493 InputScanlineHook(target, scanline);
496 #define PAL(c) ((c)+cc)
499 static void PRefreshLine(void)
505 if (BGAdrHI) vofs = 0x1000;
507 vofs+=(RefreshAddr>>12)&7;
513 zz2=(uint8)((RefreshAddr>>10)&3);
514 PPU_hook(0x2000|(RefreshAddr&0xFFF));
515 no = vnapage[zz2][(RefreshAddr&0x3ff)];
516 PPU_hook((no<<4)+vofs);
517 if((RefreshAddr&0x1f)==0x1f)
524 /* Total of 33 tiles(32 + 1 extra) */
525 static void FASTAPASS(1) RefreshLine(uint8 *target)
538 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
541 /* This high-level graphics MMC5 emulation code was written
542 for MMC5 carts in "CL" mode. It's probably not totally
543 correct for carts in "SL" mode.
545 if(MMC5Hack && geniestage!=1)
547 if(MMC5HackCHRMode==0 && (MMC5HackSPMode&0x80))
551 tochange=MMC5HackSPMode&0x1F;
553 for(X1=33;X1;X1--,P+=8)
559 if((tochange<=0 && MMC5HackSPMode&0x40) ||
560 (tochange>0 && !(MMC5HackSPMode&0x40)))
565 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
566 if(ys>=0x1E) ys-=0x1E;
567 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
569 C = MMC5HackVROMPTR+vadr;
570 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
572 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
573 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
578 zz2=(RefreshAddr>>10)&3;
579 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
580 C = MMC5BGVRAMADR(vadr);
581 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
582 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
586 if((RefreshAddr&0x1f)==0x1f)
593 else if(MMC5HackCHRMode==1 && (MMC5HackSPMode&0x80))
597 tochange=MMC5HackSPMode&0x1F;
599 for(X1=33;X1;X1--,P+=8)
606 if((tochange<=0 && MMC5HackSPMode&0x40) ||
607 (tochange>0 && !(MMC5HackSPMode&0x40)))
612 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
613 if(ys>=0x1E) ys-=0x1E;
614 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
616 C = MMC5HackVROMPTR+vadr;
617 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
619 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
620 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
625 zz2=(RefreshAddr>>10)&3;
626 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
627 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
628 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
629 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
633 if((RefreshAddr&0x1f)==0x1f)
641 else if(MMC5HackCHRMode==1)
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)
667 for(X1=33;X1;X1--,P+=8)
674 zz2=(RefreshAddr>>10)&3;
675 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
676 C = MMC5BGVRAMADR(vadr);
677 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
678 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
682 if((RefreshAddr&0x1f)==0x1f)
688 } // End if(MMC5Hack)
692 for(X1=33;X1;X1--,P+=8)
699 zz2=(RefreshAddr>>10)&3;
700 PPU_hook(0x2000|(RefreshAddr&0xFFF));
701 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
702 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
703 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
710 if((RefreshAddr&0x1f)==0x1f)
718 for(X1=33;X1;X1--,P+=8)
725 zz2=(RefreshAddr>>10)&3;
726 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
728 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
729 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
732 if((RefreshAddr&0x1f)==0x1f)
747 static INLINE void Fixit2(void)
749 if(ScreenON || SpriteON)
751 uint32 rad=RefreshAddr;
753 rad|=TempAddr&0x041f;
755 //PPU_hook(RefreshAddr,-1);
759 static INLINE void Fixit1(void)
761 if(ScreenON || SpriteON)
763 uint32 rad=RefreshAddr;
765 if((rad&0x7000)==0x7000)
768 if((rad&0x3E0)==0x3A0)
775 if((rad&0x3E0)==0x3e0)
783 //PPU_hook(RefreshAddr,-1);
787 /* This is called at the beginning of all h-blanks on visible lines. */
788 static void DoHBlank(void)
790 if(ScreenON || SpriteON)
792 if(GameHBIRQHook && (ScreenON || SpriteON))
802 X6502_Run(25); // Tried 65, caused problems with Slalom(maybe others)
807 //fprintf(stderr,"%3d: $%04x\n",scanline,RefreshAddr);
826 // ============================//
828 // ===========================//
830 void ResetMapping(void)
834 SetReadHandler(0x0000,0xFFFF,ANull);
835 SetWriteHandler(0x0000,0xFFFF,BNull);
837 SetReadHandler(0,0x7FF,ARAML);
838 SetWriteHandler(0,0x7FF,BRAML);
840 SetReadHandler(0x800,0x1FFF,ARAMH); /* Part of a little */
841 SetWriteHandler(0x800,0x1FFF,BRAMH); /* hack for a small speed boost. */
843 for(x=0x2000;x<0x4000;x+=8)
863 BWrite[0x4014]=B4014;
873 if(FCEUGameInfo.type!=GIT_NSF)
876 if(FSettings.NetworkPlay) KillNetplay();
878 GameInterface(GI_CLOSE);
884 void ResetGameLoaded(void)
886 if(GameLoaded) CloseGame();
891 GameExpSound.RChange=0;
892 if(GameExpSound.Kill)
901 FCEUGameInfo.type=GIT_CART;
902 FCEUGameInfo.vidsys=GIV_USER;
903 FCEUGameInfo.input[0]=FCEUGameInfo.input[1]=-1;
904 FCEUGameInfo.inputfc=-1;
907 FCEUGI *FCEUI_LoadGame(char *name)
914 fp=FCEU_fopen(name,"rb");
917 FCEU_PrintError("Error opening \"%s\"!",name);
922 if(iNESLoad(name,fp))
928 if(UNIFLoad(name,fp))
931 FCEU_PrintError("An error occurred while loading the file.");
940 if(FCEUGameInfo.type!=GIT_NSF)
941 if(FSettings.GameGenie)
946 if(FSettings.NetworkPlay) InitNetplay();
949 if(FCEUGameInfo.type!=GIT_NSF)
957 return(&FCEUGameInfo);
961 void FCEU_ResetVidSys(void)
965 if(FCEUGameInfo.vidsys==GIV_NTSC)
967 else if(FCEUGameInfo.vidsys==GIV_PAL)
975 scanlines_per_frame=312;
976 FSettings.FirstSLine=FSettings.UsrFirstSLine[1];
977 FSettings.LastSLine=FSettings.UsrLastSLine[1];
982 scanlines_per_frame=262;
983 FSettings.FirstSLine=FSettings.UsrFirstSLine[0];
984 FSettings.LastSLine=FSettings.UsrLastSLine[0];
986 printf("PAL = %i\n", PAL);
990 int FCEUI_Initialize(void)
992 if(!InitVirtualVideo())
994 memset(&FSettings,0,sizeof(FSettings));
995 FSettings.UsrFirstSLine[0]=8;
996 FSettings.UsrFirstSLine[1]=0;
997 FSettings.UsrLastSLine[0]=FSettings.UsrLastSLine[1]=239;
998 FSettings.SoundVolume=65535; // 100%
1002 #define harko 0xe //0x9
1003 static INLINE void Thingo(void)
1009 X6502_Run(256-harko);
1017 X6502_Run(tosprite);
1019 X6502_Run(256-tosprite-harko);
1025 X6502_Run(256-harko);
1027 X6502_Run(tosprite-(256-harko));
1029 X6502_Run(256-tosprite);
1041 ApplyPeriodicCheats();
1045 PPU[3]=PPUSPL=0; /* Not sure if this is correct. According
1046 to Matt Conte and my own tests, it is. Timing is probably
1047 off, though. NOTE: Not having this here
1048 breaks a Super Donkey Kong game. */
1050 X6502_Run(12); /* I need to figure out the true nature and length
1053 if(FCEUGameInfo.type==GIT_NSF)
1058 X6502_Run((scanlines_per_frame-242)*(256+85)-12);
1065 if(ScreenON || SpriteON)
1073 if(ScreenON || SpriteON)
1075 RefreshAddr=TempAddr;
1076 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
1078 if(FCEUGameInfo.type==GIT_NSF)
1079 X6502_Run((256+85)*240);
1085 for(scanline=0;scanline<240;scanline++)
1090 for(x=1,max=0,maxref=0;x<7;x++)
1099 //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);
1100 //memset(deempcnt,0,sizeof(deempcnt));
1101 SetNESDeemph(maxref,0);
1107 ssize=FlushEmulateSound();
1112 FCEU_PutImageDummy();
1114 FCEUD_Update(0,WaveFinalMono,ssize);
1120 FCEUD_Update(XBuf+8,WaveFinalMono,ssize);
1135 #include <sys/time.h>
1138 void FCEUI_Emulate(void)
1141 uint64 starttime,end;
1144 gettimeofday(&tv,0);
1145 starttime=((uint64)tv.tv_sec*1000000)+tv.tv_usec;
1150 // Probably won't work well on Windows port; for
1151 // debugging/speed testing.
1155 gettimeofday(&tv,0);
1156 end=((uint64)tv.tv_sec*1000000)+tv.tv_usec;
1157 w=frcount*10000000000LL/(end-starttime);
1160 printf("Average FPS: %d.%04d\n",i,frac);
1166 void FCEUI_CloseGame(void)
1171 static void ResetPPU(void)
1173 VRAMBuffer=PPU[0]=PPU[1]=PPU[2]=PPU[3]=0;
1176 RefreshAddr=TempAddr=0;
1180 static void PowerPPU(void)
1182 memset(NTARAM,0x00,0x800);
1183 memset(PALRAM,0x00,0x20);
1184 memset(SPRAM,0x00,0x100);
1190 if(!GameLoaded || (FCEUGameInfo.type==GIT_NSF)) return;
1191 GameInterface(GI_RESETM2);
1199 if(!GameLoaded) return;
1201 FCEU_CheatResetRAM();
1202 FCEU_CheatAddRAM(2,0,RAM);
1206 memset(RAM,0x00,0x800);
1208 GameInterface(GI_POWER);