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 FCEUGI *GameInfo = &FCEUGameInfo;
102 void (*GameInterface)(int h, void *param);
104 void FP_FASTAPASS(1) (*PPU_hook)(uint32 A);
106 void (*GameStateRestore)(int version);
107 void (*GameHBIRQHook)(void), (*GameHBIRQHook2)(void);
109 readfunc ARead[0x10000];
110 writefunc BWrite[0x10000];
111 static readfunc *AReadG;
112 static writefunc *BWriteG;
116 #ifdef DEBUG_ASM_6502
117 extern uint8 nes_internal_ram[0x800];
119 static void asmcpu_update(int32 cycles)
121 // some code from x6502.c
135 if((PSG[0x10]&0x80) && !(PSG[0x10]&0x40))
137 extern uint8 SIRQStat;
139 X6502_IRQBegin(FCEU_IQDPCM);
146 void asmcpu_unpack(void)
148 nes_registers[0] = X.A << 24;
149 nes_registers[1] = X.X;
150 nes_registers[2] = X.Y;
152 nes_registers[3] = X.PC;
154 nes_registers[4] = X.S << 24;
155 nes_registers[4]|= X.IRQlow << 8;
157 nes_registers[4] |= 1<<16; // MapIRQHook set bit
158 nes_registers[7] = (uint32)X.count << 16;
161 nes_registers[4]|= X.P & 0x5d;
162 nes_registers[5] = X.P << 24; // N
163 if (!(X.P&0x02)) nes_registers[5] |= 1; // Z
165 #ifdef DEBUG_ASM_6502
166 memcpy(nes_internal_ram, RAM, 0x800);
170 void asmcpu_pack(void)
172 X.A = nes_registers[0] >> 24;
173 X.X = nes_registers[1];
174 X.Y = nes_registers[2];
175 X.PC= nes_registers[3] - pc_base;
176 X.S = nes_registers[4] >> 24;
177 X.IRQlow = nes_registers[4] >> 8;
178 X.count = (int32) nes_registers[7] >> 16;
181 X.P = nes_registers[4] & 0x5d;
182 if ( nes_registers[5]&0x80000000) X.P |= 0x80; // N
183 if (!(nes_registers[5]&0x000000ff)) X.P |= 0x02; // Z
194 //printf("open [%04x] %02x @ %04x (%04x)\n", A, X.DB, X.PC, X.PC&0x7ff);
198 int AllocGenieRW(void)
200 if(!(AReadG=FCEU_malloc(0x8000*sizeof(readfunc))))
202 if(!(BWriteG=FCEU_malloc(0x8000*sizeof(writefunc))))
208 void FlushGenieRW(void)
214 for(x=0;x<0x8000;x++)
216 ARead[x+0x8000]=AReadG[x];
217 BWrite[x+0x8000]=BWriteG[x];
230 readfunc FASTAPASS(1) GetReadHandler(int32 a)
232 if(a>=0x8000 && RWWrap)
233 return AReadG[a-0x8000];
238 void FASTAPASS(3) SetReadHandler(int32 start, int32 end, readfunc func)
246 for(x=end;x>=start;x--)
249 AReadG[x-0x8000]=func;
255 for(x=end;x>=start;x--)
259 writefunc FASTAPASS(1) GetWriteHandler(int32 a)
261 if(RWWrap && a>=0x8000)
262 return BWriteG[a-0x8000];
267 void FASTAPASS(3) SetWriteHandler(int32 start, int32 end, writefunc func)
275 for(x=end;x>=start;x--)
278 BWriteG[x-0x8000]=func;
283 for(x=end;x>=start;x--)
290 uint32 TempAddr,RefreshAddr;
293 /* scanline is equal to the current visible scanline we're on. */
297 uint8 GameMemBlock[131072] __attribute__ ((aligned (4)));
298 uint8 NTARAM[0x800] __attribute__ ((aligned (4)));
299 uint8 PALRAM[0x20] __attribute__ ((aligned (4)));
300 #if !defined(ASM_6502) || defined(DEBUG_ASM_6502)
301 uint8 RAM[0x800] __attribute__ ((aligned (4)));
310 #define MMC5BGVRAMADR(V) &MMC5BGVPage[(V)>>10][(V)]
311 #define VRAMADR(V) &VPage[(V)>>10][(V)]
313 static int linestartts;
316 static uint8 *Plinef;
318 extern uint8 sprlinebuf[256+8];
320 extern uint8 sphitdata;
322 extern int spork; /* spork the world. Any sprites on this line?
323 Then this will be set to 1. Needed for zapper
324 emulation and *gasp* sprite emulation.
327 static void ResetRL(uint8 *target)
329 if(InputScanlineHook)
330 InputScanlineHook(0,0,0,0);
332 linestartts=timestamp*48+X6502_GetCycleCount();
336 static INLINE void Fixit1(void);
338 /* faking FCEUPPU_LineUpdate() from later versions of the emu */
339 static void FakedLineUpdate(void)
341 #define TOFIXNUM (272-0x4)
344 if (scanline >= 240) return;
346 if (tofix || sphitx != 0x100)
348 lastpixel = (timestamp*48-linestartts)>>4;
349 if (PAL) lastpixel += lastpixel>>4;
350 //printf("lastpixel: %i\n", lastpixel);
353 if (tofix && lastpixel>=TOFIXNUM)
365 for(x=sphitx;x<(sphitx+8) && x<l;x++)
367 if((sphitdata&(0x80>>(x-sphitx))) && !(Plinef[x]&64))
406 ret|=PPUGenLatch&0x1F;
410 //dprintf("r [2002] %02x",ret);
425 uint32 tmp=RefreshAddr&0x3FFF;
431 if(PPU_hook) PPU_hook(tmp);
432 PPUGenLatch=VRAMBuffer;
435 VRAMBuffer=VPage[tmp>>10][tmp];
439 VRAMBuffer=vnapage[(tmp>>10)&0x3][tmp&0x3FF];
442 if (INC32) RefreshAddr+=32;
444 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
445 dprintf("r [2007] %02x",ret);
467 //printf("$%04x:$%02x, %d\n",X.PC,V,scanline);
495 //printf("$%02x:$%02x\n",PPUSPL,V);
535 TempAddr|=(V&0x3f)<<8;
542 RefreshAddr=TempAddr;
544 PPU_hook(RefreshAddr);
552 uint32 tmp=RefreshAddr&0x3FFF;
558 PALRAM[0x00]=PALRAM[0x04]=PALRAM[0x08]=PALRAM[0x0C]=V&0x3f;
559 else if(tmp&3) PALRAM[(tmp&0x1f)]=V&0x3f;
563 if(PPUCHRRAM&(1<<(tmp>>10)))
564 VPage[tmp>>10][tmp]=V;
568 if(PPUNTARAM&(1<<((tmp&0xF00)>>10)))
569 vnapage[((tmp&0xF00)>>10)][tmp&0x3FF]=V;
571 if (INC32) RefreshAddr+=32;
573 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
582 B2004(0x2004,X.DB=ARead[t+x](t+x));
583 X6502_AddCycles(512);
586 void BGRender(uint8 *target)
589 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
595 RefreshLine(target-XOffset, vofs);
606 *(uint32 *)target=*(uint32 *)(target+4)=tem;
612 void FCEUI_FrameSkip(int x)
618 /* This is called at the beginning of each visible scanline */
619 static void LineUpdate(uint8 *target)
624 /* PRefreshLine() will not get called on skipped frames. This
625 * could cause a problem, but the solution would be rather complex,
626 * due to the current sprite 0 hit code.
631 if(scanline==y && SpriteON) PPU_status|=0x40; // hack
640 FCEU_dwmemset(target,tem,256);
642 else if(scanline < FSettings.FirstSLine || scanline > FSettings.LastSLine)
647 if(scanline==y && SpriteON) PPU_status|=0x40;
654 if(InputScanlineHook)
655 InputScanlineHook(target,spork?sprlinebuf:0,linestartts,256);
659 static void LineUpdateEnd(uint8 *target)
662 if(ScreenON || SpriteON) // Yes, very el-cheapo.
665 block_and(target, 256, 0x30);
668 block_or(target, 256, 0xc0);
670 block_or(target, 256, 0x40);
672 block_andor(target, 256, 0x3f, 0x80);
676 if(ScreenON || SpriteON) // Yes, very el-cheapo.
681 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0x30303030;
687 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0xc0c0c0c0;
691 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])|0x40404040;
694 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0x80808080;
698 ((uint32 *)target)[-2]=((uint32 *)target)[-1]=0;
699 ((uint32 *)target)[64]=((uint32 *)target)[65]=0;
702 #define PAL(c) ((c)+cc)
705 static void PRefreshLine(void)
709 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
710 void (*PPU_hook_)(uint32 A) = PPU_hook;
718 zz2=(RefreshAddr>>10)&3;
719 PPU_hook_(0x2000|(RefreshAddr&0xFFF));
721 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
725 if((RefreshAddr&0x1f)==0x1f)
732 /* This high-level graphics MMC5 emulation code was written
733 for MMC5 carts in "CL" mode. It's probably not totally
734 correct for carts in "SL" mode.
736 static void RefreshLine_MMC5Hack1(uint8 *P, uint32 vofs)
740 tochange=MMC5HackSPMode&0x1F;
742 for(X1=33;X1;X1--,P+=8)
748 if((tochange<=0 && MMC5HackSPMode&0x40) ||
749 (tochange>0 && !(MMC5HackSPMode&0x40)))
754 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
755 if(ys>=0x1E) ys-=0x1E;
756 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
758 C = MMC5HackVROMPTR+vadr;
759 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
761 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
762 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
767 zz2=(RefreshAddr>>10)&3;
768 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
769 C = MMC5BGVRAMADR(vadr);
770 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
771 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
775 if((RefreshAddr&0x1f)==0x1f)
783 static void RefreshLine_MMC5Hack2(uint8 *P, uint32 vofs)
787 tochange=MMC5HackSPMode&0x1F;
789 for(X1=33;X1;X1--,P+=8)
796 if((tochange<=0 && MMC5HackSPMode&0x40) ||
797 (tochange>0 && !(MMC5HackSPMode&0x40)))
802 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
803 if(ys>=0x1E) ys-=0x1E;
804 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
806 C = MMC5HackVROMPTR+vadr;
807 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
809 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
810 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
815 zz2=(RefreshAddr>>10)&3;
816 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
817 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
818 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
819 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
823 if((RefreshAddr&0x1f)==0x1f)
831 static void RefreshLine_MMC5Hack3(uint8 *P, uint32 vofs)
835 for(X1=33;X1;X1--,P+=8)
843 zz2=(RefreshAddr>>10)&3;
844 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
845 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
846 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
847 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
851 if((RefreshAddr&0x1f)==0x1f)
858 static void RefreshLine_MMC5Hack4(uint8 *P, uint32 vofs)
862 for(X1=33;X1;X1--,P+=8)
869 zz2=(RefreshAddr>>10)&3;
870 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
871 C = MMC5BGVRAMADR(vadr);
872 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
873 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
877 if((RefreshAddr&0x1f)==0x1f)
884 static void RefreshLine_PPU_hook(uint8 *P, uint32 vofs)
887 void (*PPU_hook_)(uint32 A) = PPU_hook;
888 uint32 rfraddr = RefreshAddr;
889 uint8 *page = vnapage[(rfraddr>>10)&3];
891 for(X1=33;X1;X1--,P+=8)
898 PPU_hook_(0x2000|(rfraddr&0xFFF));
899 cc=page[0x3c0+(zz>>2)+((rfraddr&0x380)>>4)];
900 cc=((cc >> ((zz&2) + ((rfraddr&0x40)>>4))) &3) <<2;
901 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
908 if((rfraddr&0x1f)==0x1f) {
910 page = vnapage[(rfraddr>>10)&3];
914 RefreshAddr = rfraddr;
917 static void RefreshLine_normal(uint8 *P, uint32 vofs) // vofs is 0x107 max
920 uint32 rfraddr = RefreshAddr;
921 uint8 *page = vnapage[(rfraddr>>10)&3];
924 if ((rfraddr&0xc)!=0)
925 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
927 for (X1=33;X1;X1--,P+=8)
932 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
934 if ((rfraddr&0xc)==0)
935 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
936 cc=((cc2 >> ((rfraddr&2) + ((rfraddr&0x40)>>4) + ((rfraddr&0xc)<<1))) & 3) << 2;
940 if((rfraddr&0x1f)==0x1f) {
942 page = vnapage[(rfraddr>>10)&3];
946 RefreshAddr = rfraddr;
949 static void SetRefreshLine(void)
951 if(MMC5Hack && geniestage!=1)
953 if(MMC5HackCHRMode==0 && (MMC5HackSPMode&0x80))
955 if (RefreshLine != RefreshLine_MMC5Hack1) printf("set refr RefreshLine_MMC5Hack1\n");
956 RefreshLine = RefreshLine_MMC5Hack1;
958 else if(MMC5HackCHRMode==1 && (MMC5HackSPMode&0x80))
960 if (RefreshLine != RefreshLine_MMC5Hack2) printf("set refr RefreshLine_MMC5Hack2\n");
961 RefreshLine = RefreshLine_MMC5Hack2;
963 else if(MMC5HackCHRMode==1)
965 if (RefreshLine != RefreshLine_MMC5Hack3) printf("set refr RefreshLine_MMC5Hack3\n");
966 RefreshLine = RefreshLine_MMC5Hack3;
970 if (RefreshLine != RefreshLine_MMC5Hack4) printf("set refr RefreshLine_MMC5Hack4\n");
971 RefreshLine = RefreshLine_MMC5Hack4;
973 } // End if(MMC5Hack)
976 if (RefreshLine != RefreshLine_PPU_hook) printf("set refr RefreshLine_PPU_hook\n");
977 RefreshLine = RefreshLine_PPU_hook;
981 if (RefreshLine != RefreshLine_normal) printf("set refr RefreshLine_normal\n");
982 RefreshLine = RefreshLine_normal;
989 if(ScreenON || SpriteON)
991 uint32 rad=RefreshAddr;
993 rad|=TempAddr&0x041f;
995 //PPU_hook(RefreshAddr,-1);
1002 if(ScreenON || SpriteON)
1004 uint32 rad=RefreshAddr;
1006 if((rad&0x7000)==0x7000)
1009 if((rad&0x3E0)==0x3A0)
1016 if((rad&0x3E0)==0x3e0)
1024 //PPU_hook(RefreshAddr,-1);
1029 // ============================//
1031 // ===========================//
1033 void ResetMapping(void)
1037 SetReadHandler(0x0000,0xFFFF,ANull);
1038 SetWriteHandler(0x0000,0xFFFF,BNull);
1040 SetReadHandler(0,0x7FF,ARAML);
1041 SetWriteHandler(0,0x7FF,BRAML);
1043 SetReadHandler(0x800,0x1FFF,ARAMH); /* Part of a little */
1044 SetWriteHandler(0x800,0x1FFF,BRAMH); /* hack for a small speed boost. */
1046 for(x=0x2000;x<0x4000;x+=8)
1066 BWrite[0x4014]=B4014;
1072 void CloseGame(void)
1077 if(FCEUGameInfo.type!=GIT_NSF)
1078 FCEU_FlushGameCheats(0,0);
1080 if(FSettings.NetworkPlay) KillNetplay();
1082 GameInterface(GI_CLOSE, 0);
1088 void ResetGameLoaded(void)
1090 if(GameLoaded) CloseGame();
1093 GameHBIRQHook=GameHBIRQHook2=0;
1094 GameExpSound.Fill=0;
1095 GameExpSound.RChange=0;
1096 if(GameExpSound.Kill)
1097 GameExpSound.Kill();
1098 GameExpSound.Kill=0;
1104 FCEUGameInfo.name=0;
1105 FCEUGameInfo.type=GIT_CART;
1106 FCEUGameInfo.vidsys=GIV_USER;
1107 FCEUGameInfo.input[0]=FCEUGameInfo.input[1]=-1;
1108 FCEUGameInfo.inputfc=-1;
1110 FCEUGameInfo.soundchan=0;
1111 FCEUGameInfo.soundrate=0;
1112 FCEUGameInfo.cspecial=0;
1115 char lastLoadedGameName [2048];
1116 int LoadGameLastError = 0;
1117 int UNIFLoad(const char *name, int fp);
1118 int iNESLoad(const char *name, int fp, int OverwriteVidMode);
1119 int FDSLoad(const char *name, int fp);
1120 int NSFLoad(int fp);
1122 FCEUGI *FCEUI_LoadGame(char *name)
1125 int have_movie = 0, have_ips = 0;
1129 LoadGameLastError = 0;
1132 strncpy(name2, name, sizeof(name2));
1133 name2[sizeof(name2)-1] = 0;
1135 fp=FCEU_fopen(name2,"rb");
1138 FCEU_PrintError("Error opening \"%s\"!",name);
1139 LoadGameLastError = 1;
1144 char *p = name2 + strlen(name2) - 4;
1145 if (strcasecmp(p, ".fcm") == 0) printf("movie detected\n"), have_movie = 1;
1146 if (strcasecmp(p, ".ips") == 0) printf("ips detected\n"), have_ips = 1;
1147 if (have_movie || have_ips)
1152 fp=FCEU_fopen(name2,"rb");
1153 if (!fp && p - name2 > 2)
1155 for (p--; p > name2 && *p != '.'; p--);
1157 fp=FCEU_fopen(name2,"rb");
1160 printf("no ROM for ips/movie\n");
1161 LoadGameLastError = 2;
1171 FILE *ips = fopen(name, "rb");
1173 fp=FCEU_fopen_forcemem(name2);
1174 if (!fp) { fclose(ips); return 0; }
1175 ApplyIPS(ips, fp); // closes ips
1179 if(iNESLoad(name2,fp,1))
1183 if(FDSLoad(name2,fp))
1185 if(UNIFLoad(name2,fp))
1188 FCEU_PrintError("An error occurred while loading the file.");
1190 // format handlers may set LoadGameLastError themselves.
1191 if (LoadGameLastError == 0) LoadGameLastError = 3;
1199 if(FCEUGameInfo.type!=GIT_NSF)
1200 if(FSettings.GameGenie)
1205 if(FSettings.NetworkPlay) InitNetplay();
1208 if(FCEUGameInfo.type!=GIT_NSF)
1210 FCEU_LoadGamePalette();
1211 FCEU_LoadGameCheats(0);
1214 FCEU_ResetPalette();
1218 FCEUI_LoadMovie(name, 1);
1220 strcpy(lastLoadedGameName, name2);
1222 return(&FCEUGameInfo);
1226 void FCEU_ResetVidSys(void)
1230 if(FCEUGameInfo.vidsys==GIV_NTSC)
1232 else if(FCEUGameInfo.vidsys==GIV_PAL)
1240 FSettings.FirstSLine=FSettings.UsrFirstSLine[1];
1241 FSettings.LastSLine=FSettings.UsrLastSLine[1];
1246 FSettings.FirstSLine=FSettings.UsrFirstSLine[0];
1247 FSettings.LastSLine=FSettings.UsrLastSLine[0];
1249 printf("ResetVidSys: PAL = %i\n", PAL);
1250 SetSoundVariables();
1253 int FCEUI_Initialize(void)
1255 if(!InitVirtualVideo())
1257 memset(&FSettings,0,sizeof(FSettings));
1258 FSettings.UsrFirstSLine[0]=8;
1259 FSettings.UsrFirstSLine[1]=0;
1260 FSettings.UsrLastSLine[0]=FSettings.UsrLastSLine[1]=239;
1261 FSettings.SoundVolume=100;
1263 FCEUI_Initialize098();
1264 FCEUI_SetEmuMode(0);
1269 void FCEUI_Kill(void)
1274 static void EmLoop(void);
1277 void (*ResetNES)(void) = 0;
1278 void (*FCEUI_Emulate)(void) = 0;
1280 void FCEUI_SetEmuMode(int is_new)
1282 use098code = is_new;
1285 ResetNES=ResetNES098;
1286 FCEUI_Emulate=FCEUI_Emulate098;
1290 ResetNES=ResetNES081;
1291 FCEUI_Emulate=EmLoop;
1295 void MMC5_hb(int); /* Ugh ugh ugh. */
1296 static void DoLine(void)
1298 uint8 *target=XBuf+scanline*320+32;
1302 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1306 // check: Battletoads & Double Dragon, Addams Family
1307 // sky glitches in SMB1 if done wrong
1313 if(scanline>=FSettings.FirstSLine && scanline<=FSettings.LastSLine)
1315 if(SpriteON && spork)
1316 CopySprites(target);
1318 LineUpdateEnd(target);
1322 if(ScreenON || SpriteON)
1326 if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
1332 X6502_Run(85-10-16);
1336 X6502_Run(6); // Tried 65, caused problems with Slalom(maybe others)
1343 if(GameHBIRQHook2 && (ScreenON || SpriteON))
1347 ResetRL(XBuf+scanline*320+32);
1352 static void EmLoop(void)
1357 uint32 scanlines_per_frame = PAL ? 312 : 262;
1359 FCEU_ApplyPeriodicCheats();
1362 if(ppudead) /* Needed for Knight Rider, possibly others. */
1364 //memset(XBuf, 0, 320*240);
1365 //X6502_Run(scanlines_per_frame*(256+85));
1367 for (lines=scanlines_per_frame;lines;lines--)
1376 PPU[3]=PPUSPL=0; /* Not sure if this is correct. According
1377 to Matt Conte and my own tests, it is. Timing is probably
1378 off, though. NOTE: Not having this here
1379 breaks a Super Donkey Kong game. */
1381 X6502_Run(12); /* I need to figure out the true nature and length
1384 if(FCEUGameInfo.type==GIT_NSF)
1389 // Note: this is needed for asm core
1390 // Warning: using 'scanline' var here breaks Castlevania III
1393 X6502_Run(256+85-12);
1394 for (lines=scanlines_per_frame-242-1;lines;lines--)
1397 // X6502_Run((scanlines_per_frame-242)*(256+85)-12);
1402 if(ScreenON || SpriteON)
1407 for(x=0;x<42;x++) {PPU_hook(0x2000); PPU_hook(0);} // ugh
1414 if(ScreenON || SpriteON)
1416 RefreshAddr=TempAddr;
1417 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
1426 if(FCEUGameInfo.type==GIT_NSF)
1428 // run scanlines for asm core to fuction
1429 for(scanline=0;scanline<240;scanline++)
1438 for(scanline=0;scanline<240;) // scanline is incremented in DoLine. Evil. :/
1443 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1444 for(x=1,max=0,maxref=0;x<7;x++)
1453 SetNESDeemph(maxref,0);
1466 ssize=FlushEmulateSound();
1468 timestampbase += timestamp;
1474 FCEU_PutImageDummy();
1476 FCEUD_Update(0,WaveFinalMono,ssize);
1482 FCEUD_Update(XBuf+8,WaveFinalMono,ssize);
1489 void FCEUI_CloseGame(void)
1494 static void ResetPPU(void)
1496 VRAMBuffer=PPU[0]=PPU[1]=PPU[2]=PPU[3]=0;
1499 RefreshAddr=TempAddr=0;
1505 static void PowerPPU(void)
1507 memset(NTARAM,0x00,0x800);
1508 memset(PALRAM,0x00,0x20);
1509 memset(SPRAM,0x00,0x100);
1513 void ResetNES081(void)
1515 if(!GameLoaded) return;
1516 GameInterface(GI_RESETM2, 0);
1522 void FCEU_MemoryRand(uint8 *ptr, uint32 size)
1524 #ifndef DEBUG_ASM_6502
1528 *ptr=(x&4)?0xFF:0x00;
1538 if(!GameLoaded) return;
1540 FCEU_CheatResetRAM();
1541 FCEU_CheatAddRAM(2,0,RAM);
1545 FCEU_MemoryRand(RAM,0x800);
1546 #ifdef DEBUG_ASM_6502
1547 memset(RAM,0x00,0x800);
1548 memset(nes_internal_ram,0x00,0x800);
1557 /* Have the external game hardware "powered" after the internal NES stuff.
1558 Needed for the NSF code and VS System code.
1560 GameInterface(GI_POWER, 0);
1561 if(FCEUGameInfo.type==GIT_VSUNI)
1573 /* savestate stuff */
1574 uint16 TempAddrT,RefreshAddrT;
1576 SFORMAT FCEUPPU_STATEINFO[]={
1577 { NTARAM, 0x800, "NTAR"},
1578 { PALRAM, 0x20, "PRAM"},
1579 { SPRAM, 0x100, "SPRA"},
1580 { PPU, 0x4, "PPUR"},
1581 { &XOffset, 1, "XOFF"},
1582 { &vtoggle, 1, "VTOG"},
1583 { &RefreshAddrT, 2|RLSB, "RADD"},
1584 { &TempAddrT, 2|RLSB, "TADD"},
1585 { &VRAMBuffer, 1, "VBUF"},
1586 { &PPUGenLatch, 1, "PGEN"},
1588 { &kook, 1, "KOOK"},
1589 { &ppudead, 1, "DEAD"},
1590 { &PPUSPL, 1, "PSPL"},