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
60 #include "drivers/arm/asmutils.h"
66 static void (*RefreshLine)(uint8 *P, uint32 vofs) = NULL;
67 static void PRefreshLine(void);
69 static void ResetPPU(void);
70 static void PowerPPU(void);
72 uint64 timestampbase=0;
78 uint32 MMC5HackVROMMask;
79 uint8 *MMC5HackExNTARAMPtr;
80 uint8 *MMC5HackVROMPTR;
81 uint8 MMC5HackCHRMode=0;
83 uint8 MMC5HackSPScroll;
86 uint8 *MMC5SPRVPage[8];
87 uint8 *MMC5BGVPage[8];
90 uint8 VRAMBuffer,PPUGenLatch;
96 /* Color deemphasis emulation. Joy... */
98 static int deempcnt[8];
101 void (*GameInterface)(int h, void *param);
103 void FP_FASTAPASS(1) (*PPU_hook)(uint32 A);
105 void (*GameStateRestore)(int version);
106 void (*GameHBIRQHook)(void), (*GameHBIRQHook2)(void);
108 readfunc ARead[0x10000];
109 writefunc BWrite[0x10000];
110 static readfunc *AReadG;
111 static writefunc *BWriteG;
115 #ifdef DEBUG_ASM_6502
116 extern uint8 nes_internal_ram[0x800];
118 static void asmcpu_update(int32 cycles)
120 // some code from x6502.c
134 if((PSG[0x10]&0x80) && !(PSG[0x10]&0x40))
136 extern uint8 SIRQStat;
138 X6502_IRQBegin(FCEU_IQDPCM);
145 void asmcpu_unpack(void)
147 nes_registers[0] = X.A << 24;
148 nes_registers[1] = X.X;
149 nes_registers[2] = X.Y;
151 nes_registers[3] = X.PC;
153 nes_registers[4] = X.S << 24;
154 nes_registers[4]|= X.IRQlow << 8;
156 nes_registers[4] |= 1<<16; // MapIRQHook set bit
157 nes_registers[7] = (uint32)X.count << 16;
160 nes_registers[4]|= X.P & 0x5d;
161 nes_registers[5] = X.P << 24; // N
162 if (!(X.P&0x02)) nes_registers[5] |= 1; // Z
164 #ifdef DEBUG_ASM_6502
165 memcpy(nes_internal_ram, RAM, 0x800);
169 void asmcpu_pack(void)
171 X.A = nes_registers[0] >> 24;
172 X.X = nes_registers[1];
173 X.Y = nes_registers[2];
174 X.PC= nes_registers[3] - pc_base;
175 X.S = nes_registers[4] >> 24;
176 X.IRQlow = nes_registers[4] >> 8;
177 X.count = (int32) nes_registers[7] >> 16;
180 X.P = nes_registers[4] & 0x5d;
181 if ( nes_registers[5]&0x80000000) X.P |= 0x80; // N
182 if (!(nes_registers[5]&0x000000ff)) X.P |= 0x02; // Z
193 //printf("open [%04x] %02x @ %04x (%04x)\n", A, X.DB, X.PC, X.PC&0x7ff);
197 int AllocGenieRW(void)
199 if(!(AReadG=FCEU_malloc(0x8000*sizeof(readfunc))))
201 if(!(BWriteG=FCEU_malloc(0x8000*sizeof(writefunc))))
207 void FlushGenieRW(void)
213 for(x=0;x<0x8000;x++)
215 ARead[x+0x8000]=AReadG[x];
216 BWrite[x+0x8000]=BWriteG[x];
229 readfunc FASTAPASS(1) GetReadHandler(int32 a)
231 if(a>=0x8000 && RWWrap)
232 return AReadG[a-0x8000];
237 void FASTAPASS(3) SetReadHandler(int32 start, int32 end, readfunc func)
245 for(x=end;x>=start;x--)
248 AReadG[x-0x8000]=func;
254 for(x=end;x>=start;x--)
258 writefunc FASTAPASS(1) GetWriteHandler(int32 a)
260 if(RWWrap && a>=0x8000)
261 return BWriteG[a-0x8000];
266 void FASTAPASS(3) SetWriteHandler(int32 start, int32 end, writefunc func)
274 for(x=end;x>=start;x--)
277 BWriteG[x-0x8000]=func;
282 for(x=end;x>=start;x--)
289 uint32 TempAddr,RefreshAddr;
292 /* scanline is equal to the current visible scanline we're on. */
296 uint8 GameMemBlock[131072] __attribute__ ((aligned (4)));
297 uint8 NTARAM[0x800] __attribute__ ((aligned (4)));
298 uint8 PALRAM[0x20] __attribute__ ((aligned (4)));
299 #if !defined(ASM_6502) || defined(DEBUG_ASM_6502)
300 uint8 RAM[0x800] __attribute__ ((aligned (4)));
309 #define MMC5BGVRAMADR(V) &MMC5BGVPage[(V)>>10][(V)]
310 #define VRAMADR(V) &VPage[(V)>>10][(V)]
312 static int linestartts;
315 static uint8 *Plinef;
317 extern uint8 sprlinebuf[256+8];
319 extern uint8 sphitdata;
321 extern int spork; /* spork the world. Any sprites on this line?
322 Then this will be set to 1. Needed for zapper
323 emulation and *gasp* sprite emulation.
326 static void ResetRL(uint8 *target)
328 if(InputScanlineHook)
329 InputScanlineHook(0,0,0,0);
331 linestartts=timestamp*48+X6502_GetCycleCount();
335 static INLINE void Fixit1(void);
337 /* faking FCEUPPU_LineUpdate() from later versions of the emu */
338 static void FakedLineUpdate(void)
340 #define TOFIXNUM (272-0x4)
343 if (scanline >= 240) return;
345 if (tofix || sphitx != 0x100)
347 lastpixel = (timestamp*48-linestartts)>>4;
348 if (PAL) lastpixel += lastpixel>>4;
349 //printf("lastpixel: %i\n", lastpixel);
352 if (tofix && lastpixel>=TOFIXNUM)
364 for(x=sphitx;x<(sphitx+8) && x<l;x++)
366 if((sphitdata&(0x80>>(x-sphitx))) && !(Plinef[x]&64))
405 ret|=PPUGenLatch&0x1F;
409 //dprintf("r [2002] %02x",ret);
424 uint32 tmp=RefreshAddr&0x3FFF;
430 if(PPU_hook) PPU_hook(tmp);
431 PPUGenLatch=VRAMBuffer;
434 VRAMBuffer=VPage[tmp>>10][tmp];
438 VRAMBuffer=vnapage[(tmp>>10)&0x3][tmp&0x3FF];
441 if (INC32) RefreshAddr+=32;
443 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
444 dprintf("r [2007] %02x",ret);
466 //printf("$%04x:$%02x, %d\n",X.PC,V,scanline);
494 //printf("$%02x:$%02x\n",PPUSPL,V);
534 TempAddr|=(V&0x3f)<<8;
541 RefreshAddr=TempAddr;
543 PPU_hook(RefreshAddr);
551 uint32 tmp=RefreshAddr&0x3FFF;
557 PALRAM[0x00]=PALRAM[0x04]=PALRAM[0x08]=PALRAM[0x0C]=V&0x3f;
558 else if(tmp&3) PALRAM[(tmp&0x1f)]=V&0x3f;
562 if(PPUCHRRAM&(1<<(tmp>>10)))
563 VPage[tmp>>10][tmp]=V;
567 if(PPUNTARAM&(1<<((tmp&0xF00)>>10)))
568 vnapage[((tmp&0xF00)>>10)][tmp&0x3FF]=V;
570 if (INC32) RefreshAddr+=32;
572 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
581 B2004(0x2004,X.DB=ARead[t+x](t+x));
582 X6502_AddCycles(512);
585 void BGRender(uint8 *target)
588 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
594 RefreshLine(target-XOffset, vofs);
605 *(uint32 *)target=*(uint32 *)(target+4)=tem;
611 void FCEUI_FrameSkip(int x)
617 /* This is called at the beginning of each visible scanline */
618 static void LineUpdate(uint8 *target)
623 /* PRefreshLine() will not get called on skipped frames. This
624 * could cause a problem, but the solution would be rather complex,
625 * due to the current sprite 0 hit code.
630 if(scanline==y && SpriteON) PPU_status|=0x40; // hack
634 if(scanline < FSettings.FirstSLine || scanline > FSettings.LastSLine)
639 if(scanline==y && SpriteON) PPU_status|=0x40;
652 FCEU_dwmemset(target,tem,256);
656 if(InputScanlineHook)
657 InputScanlineHook(target,spork?sprlinebuf:0,linestartts,256);
661 static void LineUpdateEnd(uint8 *target)
664 if(ScreenON || SpriteON) // Yes, very el-cheapo.
667 block_and(target, 256, 0x30);
670 block_or(target, 256, 0xc0);
672 block_or(target, 256, 0x40);
674 block_andor(target, 256, 0x3f, 0x80);
678 if(ScreenON || SpriteON) // Yes, very el-cheapo.
683 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0x30303030;
689 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0xc0c0c0c0;
693 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])|0x40404040;
696 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0x80808080;
700 ((uint32 *)target)[-2]=((uint32 *)target)[-1]=0;
701 ((uint32 *)target)[64]=((uint32 *)target)[65]=0;
704 #define PAL(c) ((c)+cc)
707 static void PRefreshLine(void)
711 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
712 void (*PPU_hook_)(uint32 A) = PPU_hook;
720 zz2=(RefreshAddr>>10)&3;
721 PPU_hook_(0x2000|(RefreshAddr&0xFFF));
723 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
727 if((RefreshAddr&0x1f)==0x1f)
734 /* This high-level graphics MMC5 emulation code was written
735 for MMC5 carts in "CL" mode. It's probably not totally
736 correct for carts in "SL" mode.
738 static void RefreshLine_MMC5Hack1(uint8 *P, uint32 vofs)
742 tochange=MMC5HackSPMode&0x1F;
744 for(X1=33;X1;X1--,P+=8)
750 if((tochange<=0 && MMC5HackSPMode&0x40) ||
751 (tochange>0 && !(MMC5HackSPMode&0x40)))
756 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
757 if(ys>=0x1E) ys-=0x1E;
758 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
760 C = MMC5HackVROMPTR+vadr;
761 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
763 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
764 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
769 zz2=(RefreshAddr>>10)&3;
770 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
771 C = MMC5BGVRAMADR(vadr);
772 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
773 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
777 if((RefreshAddr&0x1f)==0x1f)
785 static void RefreshLine_MMC5Hack2(uint8 *P, uint32 vofs)
789 tochange=MMC5HackSPMode&0x1F;
791 for(X1=33;X1;X1--,P+=8)
798 if((tochange<=0 && MMC5HackSPMode&0x40) ||
799 (tochange>0 && !(MMC5HackSPMode&0x40)))
804 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
805 if(ys>=0x1E) ys-=0x1E;
806 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
808 C = MMC5HackVROMPTR+vadr;
809 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
811 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
812 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
817 zz2=(RefreshAddr>>10)&3;
818 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
819 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
820 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
821 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
825 if((RefreshAddr&0x1f)==0x1f)
833 static void RefreshLine_MMC5Hack3(uint8 *P, uint32 vofs)
837 for(X1=33;X1;X1--,P+=8)
845 zz2=(RefreshAddr>>10)&3;
846 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
847 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
848 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
849 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
853 if((RefreshAddr&0x1f)==0x1f)
860 static void RefreshLine_MMC5Hack4(uint8 *P, uint32 vofs)
864 for(X1=33;X1;X1--,P+=8)
871 zz2=(RefreshAddr>>10)&3;
872 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
873 C = MMC5BGVRAMADR(vadr);
874 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
875 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
879 if((RefreshAddr&0x1f)==0x1f)
886 static void RefreshLine_PPU_hook(uint8 *P, uint32 vofs)
889 void (*PPU_hook_)(uint32 A) = PPU_hook;
890 uint32 rfraddr = RefreshAddr;
891 uint8 *page = vnapage[(rfraddr>>10)&3];
893 for(X1=33;X1;X1--,P+=8)
900 PPU_hook_(0x2000|(rfraddr&0xFFF));
901 cc=page[0x3c0+(zz>>2)+((rfraddr&0x380)>>4)];
902 cc=((cc >> ((zz&2) + ((rfraddr&0x40)>>4))) &3) <<2;
903 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
910 if((rfraddr&0x1f)==0x1f) {
912 page = vnapage[(rfraddr>>10)&3];
916 RefreshAddr = rfraddr;
919 static void RefreshLine_normal(uint8 *P, uint32 vofs) // vofs is 0x107 max
922 uint32 rfraddr = RefreshAddr;
923 uint8 *page = vnapage[(rfraddr>>10)&3];
926 if ((rfraddr&0xc)!=0)
927 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
929 for (X1=33;X1;X1--,P+=8)
934 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
936 if ((rfraddr&0xc)==0)
937 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
938 cc=((cc2 >> ((rfraddr&2) + ((rfraddr&0x40)>>4) + ((rfraddr&0xc)<<1))) & 3) << 2;
942 if((rfraddr&0x1f)==0x1f) {
944 page = vnapage[(rfraddr>>10)&3];
948 RefreshAddr = rfraddr;
951 static void SetRefreshLine(void)
953 if(MMC5Hack && geniestage!=1)
955 if(MMC5HackCHRMode==0 && (MMC5HackSPMode&0x80))
957 if (RefreshLine != RefreshLine_MMC5Hack1) printf("set refr RefreshLine_MMC5Hack1\n");
958 RefreshLine = RefreshLine_MMC5Hack1;
960 else if(MMC5HackCHRMode==1 && (MMC5HackSPMode&0x80))
962 if (RefreshLine != RefreshLine_MMC5Hack2) printf("set refr RefreshLine_MMC5Hack2\n");
963 RefreshLine = RefreshLine_MMC5Hack2;
965 else if(MMC5HackCHRMode==1)
967 if (RefreshLine != RefreshLine_MMC5Hack3) printf("set refr RefreshLine_MMC5Hack3\n");
968 RefreshLine = RefreshLine_MMC5Hack3;
972 if (RefreshLine != RefreshLine_MMC5Hack4) printf("set refr RefreshLine_MMC5Hack4\n");
973 RefreshLine = RefreshLine_MMC5Hack4;
975 } // End if(MMC5Hack)
978 if (RefreshLine != RefreshLine_PPU_hook) printf("set refr RefreshLine_PPU_hook\n");
979 RefreshLine = RefreshLine_PPU_hook;
983 if (RefreshLine != RefreshLine_normal) printf("set refr RefreshLine_normal\n");
984 RefreshLine = RefreshLine_normal;
991 if(ScreenON || SpriteON)
993 uint32 rad=RefreshAddr;
995 rad|=TempAddr&0x041f;
997 //PPU_hook(RefreshAddr,-1);
1004 if(ScreenON || SpriteON)
1006 uint32 rad=RefreshAddr;
1008 if((rad&0x7000)==0x7000)
1011 if((rad&0x3E0)==0x3A0)
1018 if((rad&0x3E0)==0x3e0)
1026 //PPU_hook(RefreshAddr,-1);
1031 // ============================//
1033 // ===========================//
1035 void ResetMapping(void)
1039 SetReadHandler(0x0000,0xFFFF,ANull);
1040 SetWriteHandler(0x0000,0xFFFF,BNull);
1042 SetReadHandler(0,0x7FF,ARAML);
1043 SetWriteHandler(0,0x7FF,BRAML);
1045 SetReadHandler(0x800,0x1FFF,ARAMH); /* Part of a little */
1046 SetWriteHandler(0x800,0x1FFF,BRAMH); /* hack for a small speed boost. */
1048 for(x=0x2000;x<0x4000;x+=8)
1068 BWrite[0x4014]=B4014;
1074 void CloseGame(void)
1079 if(FCEUGameInfo.type!=GIT_NSF)
1080 FCEU_FlushGameCheats(0,0);
1082 if(FSettings.NetworkPlay) KillNetplay();
1084 GameInterface(GI_CLOSE, 0);
1090 void ResetGameLoaded(void)
1092 if(GameLoaded) CloseGame();
1095 GameHBIRQHook=GameHBIRQHook2=0;
1096 GameExpSound.Fill=0;
1097 GameExpSound.RChange=0;
1098 if(GameExpSound.Kill)
1099 GameExpSound.Kill();
1100 GameExpSound.Kill=0;
1106 FCEUGameInfo.name=0;
1107 FCEUGameInfo.type=GIT_CART;
1108 FCEUGameInfo.vidsys=GIV_USER;
1109 FCEUGameInfo.input[0]=FCEUGameInfo.input[1]=-1;
1110 FCEUGameInfo.inputfc=-1;
1112 FCEUGameInfo.soundchan=0;
1113 FCEUGameInfo.soundrate=0;
1114 FCEUGameInfo.cspecial=0;
1117 char lastLoadedGameName [2048];
1118 int LoadGameLastError = 0;
1119 int UNIFLoad(const char *name, int fp);
1120 int iNESLoad(const char *name, int fp);
1121 int FDSLoad(const char *name, int fp);
1122 int NSFLoad(int fp);
1124 FCEUGI *FCEUI_LoadGame(char *name)
1127 int have_movie = 0, have_ips = 0;
1131 LoadGameLastError = 0;
1134 strncpy(name2, name, sizeof(name2));
1135 name2[sizeof(name2)-1] = 0;
1137 fp=FCEU_fopen(name2,"rb");
1140 FCEU_PrintError("Error opening \"%s\"!",name);
1141 LoadGameLastError = 1;
1146 char *p = name2 + strlen(name2) - 4;
1147 if (strcasecmp(p, ".fcm") == 0) printf("movie detected\n"), have_movie = 1;
1148 if (strcasecmp(p, ".ips") == 0) printf("ips detected\n"), have_ips = 1;
1149 if (have_movie || have_ips)
1154 fp=FCEU_fopen(name2,"rb");
1155 if (!fp && p - name2 > 2)
1157 for (p--; p > name2 && *p != '.'; p--);
1159 fp=FCEU_fopen(name2,"rb");
1162 printf("no ROM for ips/movie\n");
1163 LoadGameLastError = 2;
1173 FILE *ips = fopen(name, "rb");
1175 fp=FCEU_fopen_forcemem(name2);
1176 if (!fp) { fclose(ips); return 0; }
1177 ApplyIPS(ips, fp); // closes ips
1181 if(iNESLoad(name2,fp))
1185 if(FDSLoad(name2,fp))
1187 if(UNIFLoad(name2,fp))
1190 FCEU_PrintError("An error occurred while loading the file.");
1192 // format handlers may set LoadGameLastError themselves.
1193 if (LoadGameLastError == 0) LoadGameLastError = 3;
1201 if(FCEUGameInfo.type!=GIT_NSF)
1202 if(FSettings.GameGenie)
1207 if(FSettings.NetworkPlay) InitNetplay();
1210 if(FCEUGameInfo.type!=GIT_NSF)
1212 FCEU_LoadGamePalette();
1213 FCEU_LoadGameCheats(0);
1216 FCEU_ResetPalette();
1220 FCEUI_LoadMovie(name, 1);
1222 strcpy(lastLoadedGameName, name2);
1224 return(&FCEUGameInfo);
1228 void FCEU_ResetVidSys(void)
1232 if(FCEUGameInfo.vidsys==GIV_NTSC)
1234 else if(FCEUGameInfo.vidsys==GIV_PAL)
1242 FSettings.FirstSLine=FSettings.UsrFirstSLine[1];
1243 FSettings.LastSLine=FSettings.UsrLastSLine[1];
1248 FSettings.FirstSLine=FSettings.UsrFirstSLine[0];
1249 FSettings.LastSLine=FSettings.UsrLastSLine[0];
1251 printf("ResetVidSys: PAL = %i\n", PAL);
1252 SetSoundVariables();
1255 int FCEUI_Initialize(void)
1257 if(!InitVirtualVideo())
1259 memset(&FSettings,0,sizeof(FSettings));
1260 FSettings.UsrFirstSLine[0]=8;
1261 FSettings.UsrFirstSLine[1]=0;
1262 FSettings.UsrLastSLine[0]=FSettings.UsrLastSLine[1]=239;
1263 FSettings.SoundVolume=100;
1265 FCEUI_Initialize098();
1266 FCEUI_SetEmuMode(0);
1271 void FCEUI_Kill(void)
1276 static void EmLoop(void);
1279 void (*ResetNES)(void) = 0;
1280 void (*FCEUI_Emulate)(void) = 0;
1282 void FCEUI_SetEmuMode(int is_new)
1284 use098code = is_new;
1287 ResetNES=ResetNES098;
1288 FCEUI_Emulate=FCEUI_Emulate098;
1292 ResetNES=ResetNES081;
1293 FCEUI_Emulate=EmLoop;
1297 void MMC5_hb(int); /* Ugh ugh ugh. */
1298 static void DoLine(void)
1300 uint8 *target=XBuf+scanline*320+32;
1304 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1308 // check: Battletoads & Double Dragon, Addams Family
1309 // sky glitches in SMB1 if done wrong
1315 if(scanline>=FSettings.FirstSLine && scanline<=FSettings.LastSLine)
1317 if(SpriteON && spork)
1318 CopySprites(target);
1320 LineUpdateEnd(target);
1324 if(ScreenON || SpriteON)
1328 if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
1334 X6502_Run(85-10-16);
1338 X6502_Run(6); // Tried 65, caused problems with Slalom(maybe others)
1345 if(GameHBIRQHook2 && (ScreenON || SpriteON))
1349 ResetRL(XBuf+scanline*320+32);
1354 static void EmLoop(void)
1359 uint32 scanlines_per_frame = PAL ? 312 : 262;
1361 FCEU_ApplyPeriodicCheats();
1364 if(ppudead) /* Needed for Knight Rider, possibly others. */
1366 //memset(XBuf, 0, 320*240);
1367 //X6502_Run(scanlines_per_frame*(256+85));
1369 for (lines=scanlines_per_frame;lines;lines--)
1378 PPU[3]=PPUSPL=0; /* Not sure if this is correct. According
1379 to Matt Conte and my own tests, it is. Timing is probably
1380 off, though. NOTE: Not having this here
1381 breaks a Super Donkey Kong game. */
1383 X6502_Run(12); /* I need to figure out the true nature and length
1386 if(FCEUGameInfo.type==GIT_NSF)
1391 // Note: this is needed for asm core
1392 // Warning: using 'scanline' var here breaks Castlevania III
1395 X6502_Run(256+85-12);
1396 for (lines=scanlines_per_frame-242-1;lines;lines--)
1399 // X6502_Run((scanlines_per_frame-242)*(256+85)-12);
1404 if(ScreenON || SpriteON)
1409 for(x=0;x<42;x++) {PPU_hook(0x2000); PPU_hook(0);} // ugh
1416 if(ScreenON || SpriteON)
1418 RefreshAddr=TempAddr;
1419 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
1428 if(FCEUGameInfo.type==GIT_NSF)
1430 // run scanlines for asm core to fuction
1431 for(scanline=0;scanline<240;scanline++)
1440 for(scanline=0;scanline<240;) // scanline is incremented in DoLine. Evil. :/
1445 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1446 for(x=1,max=0,maxref=0;x<7;x++)
1455 SetNESDeemph(maxref,0);
1468 ssize=FlushEmulateSound();
1470 timestampbase += timestamp;
1476 FCEU_PutImageDummy();
1478 FCEUD_Update(0,WaveFinalMono,ssize);
1484 FCEUD_Update(XBuf+8,WaveFinalMono,ssize);
1491 void FCEUI_CloseGame(void)
1496 static void ResetPPU(void)
1498 VRAMBuffer=PPU[0]=PPU[1]=PPU[2]=PPU[3]=0;
1501 RefreshAddr=TempAddr=0;
1507 static void PowerPPU(void)
1509 memset(NTARAM,0x00,0x800);
1510 memset(PALRAM,0x00,0x20);
1511 memset(SPRAM,0x00,0x100);
1515 void ResetNES081(void)
1517 if(!GameLoaded) return;
1518 GameInterface(GI_RESETM2, 0);
1524 #ifndef DEBUG_ASM_6502
1525 static void FCEU_MemoryRand(uint8 *ptr, uint32 size)
1530 *ptr=(x&4)?0xFF:0x00;
1540 if(!GameLoaded) return;
1542 FCEU_CheatResetRAM();
1543 FCEU_CheatAddRAM(2,0,RAM);
1547 #ifndef DEBUG_ASM_6502
1548 FCEU_MemoryRand(RAM,0x800);
1550 memset(RAM,0x00,0x800);
1551 memset(nes_internal_ram,0x00,0x800);
1560 /* Have the external game hardware "powered" after the internal NES stuff.
1561 Needed for the NSF code and VS System code.
1563 GameInterface(GI_POWER, 0);
1564 if(FCEUGameInfo.type==GIT_VSUNI)
1576 /* savestate stuff */
1577 uint16 TempAddrT,RefreshAddrT;
1579 SFORMAT FCEUPPU_STATEINFO[]={
1580 { NTARAM, 0x800, "NTAR"},
1581 { PALRAM, 0x20, "PRAM"},
1582 { SPRAM, 0x100, "SPRA"},
1583 { PPU, 0x4, "PPUR"},
1584 { &XOffset, 1, "XOFF"},
1585 { &vtoggle, 1, "VTOG"},
1586 { &RefreshAddrT, 2|RLSB, "RADD"},
1587 { &TempAddrT, 2|RLSB, "TADD"},
1588 { &VRAMBuffer, 1, "VBUF"},
1589 { &PPUGenLatch, 1, "PGEN"},
1591 { &kook, 1, "KOOK"},
1592 { &ppudead, 1, "DEAD"},
1593 { &PPUSPL, 1, "PSPL"},