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];
120 void asmcpu_unpack(void)
122 nes_registers[0] = X.A << 24;
123 nes_registers[1] = X.X;
124 nes_registers[2] = X.Y;
126 nes_registers[3] = X.PC;
128 nes_registers[4] = X.S << 24;
129 nes_registers[4]|= X.IRQlow << 8;
131 nes_registers[4] |= 1<<16; // MapIRQHook set bit
132 nes_registers[7] = (uint32)X.count << 16;
135 nes_registers[4]|= X.P & 0x5d;
136 nes_registers[5] = X.P << 24; // N
137 if (!(X.P&0x02)) nes_registers[5] |= 1; // Z
139 #ifdef DEBUG_ASM_6502
140 memcpy(nes_internal_ram, RAM, 0x800);
144 void asmcpu_pack(void)
146 X.A = nes_registers[0] >> 24;
147 X.X = nes_registers[1];
148 X.Y = nes_registers[2];
149 X.PC= nes_registers[3] - pc_base;
150 X.S = nes_registers[4] >> 24;
151 X.IRQlow = nes_registers[4] >> 8;
152 X.count = (int32) nes_registers[7] >> 16;
155 X.P = nes_registers[4] & 0x5d;
156 if ( nes_registers[5]&0x80000000) X.P |= 0x80; // N
157 if (!(nes_registers[5]&0x000000ff)) X.P |= 0x02; // Z
168 //printf("open [%04x] %02x @ %04x (%04x)\n", A, X.DB, X.PC, X.PC&0x7ff);
172 int AllocGenieRW(void)
174 if(!(AReadG=FCEU_malloc(0x8000*sizeof(readfunc))))
176 if(!(BWriteG=FCEU_malloc(0x8000*sizeof(writefunc))))
182 void FlushGenieRW(void)
188 for(x=0;x<0x8000;x++)
190 ARead[x+0x8000]=AReadG[x];
191 BWrite[x+0x8000]=BWriteG[x];
204 readfunc FASTAPASS(1) GetReadHandler(int32 a)
206 if(a>=0x8000 && RWWrap)
207 return AReadG[a-0x8000];
212 void FASTAPASS(3) SetReadHandler(int32 start, int32 end, readfunc func)
220 for(x=end;x>=start;x--)
223 AReadG[x-0x8000]=func;
229 for(x=end;x>=start;x--)
233 writefunc FASTAPASS(1) GetWriteHandler(int32 a)
235 if(RWWrap && a>=0x8000)
236 return BWriteG[a-0x8000];
241 void FASTAPASS(3) SetWriteHandler(int32 start, int32 end, writefunc func)
249 for(x=end;x>=start;x--)
252 BWriteG[x-0x8000]=func;
257 for(x=end;x>=start;x--)
264 uint32 TempAddr,RefreshAddr;
267 /* scanline is equal to the current visible scanline we're on. */
271 uint8 GameMemBlock[131072] __attribute__ ((aligned (4)));
272 uint8 NTARAM[0x800] __attribute__ ((aligned (4)));
273 uint8 PALRAM[0x20] __attribute__ ((aligned (4)));
274 #if !defined(ASM_6502) || defined(DEBUG_ASM_6502)
275 uint8 RAM[0x800] __attribute__ ((aligned (4)));
284 #define MMC5BGVRAMADR(V) &MMC5BGVPage[(V)>>10][(V)]
285 #define VRAMADR(V) &VPage[(V)>>10][(V)]
287 static int linestartts;
290 static uint8 *Plinef;
292 extern uint8 sprlinebuf[256+8];
294 extern uint8 sphitdata;
296 extern int spork; /* spork the world. Any sprites on this line?
297 Then this will be set to 1. Needed for zapper
298 emulation and *gasp* sprite emulation.
301 static void ResetRL(uint8 *target)
303 if(InputScanlineHook)
304 InputScanlineHook(0,0,0,0);
306 linestartts=timestamp*48+X6502_GetCycleCount();
310 static INLINE void Fixit1(void);
312 /* faking FCEUPPU_LineUpdate() from later versions of the emu */
313 static void FakedLineUpdate(void)
315 #define TOFIXNUM (272-0x4)
318 if (scanline >= 240) return;
320 if (tofix || sphitx != 0x100)
322 lastpixel = (timestamp*48-linestartts)>>4;
323 if (PAL) lastpixel += lastpixel>>4;
324 //printf("lastpixel: %i\n", lastpixel);
327 if (tofix && lastpixel>=TOFIXNUM)
339 for(x=sphitx;x<(sphitx+8) && x<l;x++)
341 if((sphitdata&(0x80>>(x-sphitx))) && !(Plinef[x]&64))
380 ret|=PPUGenLatch&0x1F;
384 //dprintf("r [2002] %02x",ret);
399 uint32 tmp=RefreshAddr&0x3FFF;
405 if(PPU_hook) PPU_hook(tmp);
406 PPUGenLatch=VRAMBuffer;
409 VRAMBuffer=VPage[tmp>>10][tmp];
413 VRAMBuffer=vnapage[(tmp>>10)&0x3][tmp&0x3FF];
416 if (INC32) RefreshAddr+=32;
418 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
419 dprintf("r [2007] %02x",ret);
441 //printf("$%04x:$%02x, %d\n",X.PC,V,scanline);
469 //printf("$%02x:$%02x\n",PPUSPL,V);
509 TempAddr|=(V&0x3f)<<8;
516 RefreshAddr=TempAddr;
518 PPU_hook(RefreshAddr);
526 uint32 tmp=RefreshAddr&0x3FFF;
532 PALRAM[0x00]=PALRAM[0x04]=PALRAM[0x08]=PALRAM[0x0C]=V&0x3f;
533 else if(tmp&3) PALRAM[(tmp&0x1f)]=V&0x3f;
537 if(PPUCHRRAM&(1<<(tmp>>10)))
538 VPage[tmp>>10][tmp]=V;
542 if(PPUNTARAM&(1<<((tmp&0xF00)>>10)))
543 vnapage[((tmp&0xF00)>>10)][tmp&0x3FF]=V;
545 if (INC32) RefreshAddr+=32;
547 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
556 B2004(0x2004,X.DB=ARead[t+x](t+x));
557 X6502_AddCycles(512);
560 void BGRender(uint8 *target)
563 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
569 RefreshLine(target-XOffset, vofs);
580 *(uint32 *)target=*(uint32 *)(target+4)=tem;
586 void FCEUI_FrameSkip(int x)
592 /* This is called at the beginning of each visible scanline */
593 static void LineUpdate(uint8 *target)
598 /* PRefreshLine() will not get called on skipped frames. This
599 * could cause a problem, but the solution would be rather complex,
600 * due to the current sprite 0 hit code.
605 if(scanline==y && SpriteON) PPU_status|=0x40; // hack
614 FCEU_dwmemset(target,tem,256);
616 else if(scanline < FSettings.FirstSLine || scanline > FSettings.LastSLine)
621 if(scanline==y && SpriteON) PPU_status|=0x40;
628 if(InputScanlineHook)
629 InputScanlineHook(target,spork?sprlinebuf:0,linestartts,256);
633 static void LineUpdateEnd(uint8 *target)
636 if(ScreenON || SpriteON) // Yes, very el-cheapo.
639 block_and(target, 256, 0x30);
642 block_or(target, 256, 0xc0);
644 block_or(target, 256, 0x40);
646 block_andor(target, 256, 0x3f, 0x80);
650 if(ScreenON || SpriteON) // Yes, very el-cheapo.
655 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0x30303030;
661 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0xc0c0c0c0;
665 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])|0x40404040;
668 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0x80808080;
672 ((uint32 *)target)[-2]=((uint32 *)target)[-1]=0;
673 ((uint32 *)target)[64]=((uint32 *)target)[65]=0;
676 #define PAL(c) ((c)+cc)
679 static void PRefreshLine(void)
683 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
684 void (*PPU_hook_)(uint32 A) = PPU_hook;
692 zz2=(RefreshAddr>>10)&3;
693 PPU_hook_(0x2000|(RefreshAddr&0xFFF));
695 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
699 if((RefreshAddr&0x1f)==0x1f)
706 /* This high-level graphics MMC5 emulation code was written
707 for MMC5 carts in "CL" mode. It's probably not totally
708 correct for carts in "SL" mode.
710 static void RefreshLine_MMC5Hack1(uint8 *P, uint32 vofs)
714 tochange=MMC5HackSPMode&0x1F;
716 for(X1=33;X1;X1--,P+=8)
722 if((tochange<=0 && MMC5HackSPMode&0x40) ||
723 (tochange>0 && !(MMC5HackSPMode&0x40)))
728 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
729 if(ys>=0x1E) ys-=0x1E;
730 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
732 C = MMC5HackVROMPTR+vadr;
733 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
735 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
736 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
741 zz2=(RefreshAddr>>10)&3;
742 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
743 C = MMC5BGVRAMADR(vadr);
744 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
745 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
749 if((RefreshAddr&0x1f)==0x1f)
757 static void RefreshLine_MMC5Hack2(uint8 *P, uint32 vofs)
761 tochange=MMC5HackSPMode&0x1F;
763 for(X1=33;X1;X1--,P+=8)
770 if((tochange<=0 && MMC5HackSPMode&0x40) ||
771 (tochange>0 && !(MMC5HackSPMode&0x40)))
776 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
777 if(ys>=0x1E) ys-=0x1E;
778 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
780 C = MMC5HackVROMPTR+vadr;
781 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
783 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
784 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
789 zz2=(RefreshAddr>>10)&3;
790 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
791 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
792 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
793 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
797 if((RefreshAddr&0x1f)==0x1f)
805 static void RefreshLine_MMC5Hack3(uint8 *P, uint32 vofs)
809 for(X1=33;X1;X1--,P+=8)
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)
832 static void RefreshLine_MMC5Hack4(uint8 *P, uint32 vofs)
836 for(X1=33;X1;X1--,P+=8)
843 zz2=(RefreshAddr>>10)&3;
844 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
845 C = MMC5BGVRAMADR(vadr);
846 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
847 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
851 if((RefreshAddr&0x1f)==0x1f)
858 static void RefreshLine_PPU_hook(uint8 *P, uint32 vofs)
861 void (*PPU_hook_)(uint32 A) = PPU_hook;
862 uint32 rfraddr = RefreshAddr;
863 uint8 *page = vnapage[(rfraddr>>10)&3];
865 for(X1=33;X1;X1--,P+=8)
872 PPU_hook_(0x2000|(rfraddr&0xFFF));
873 cc=page[0x3c0+(zz>>2)+((rfraddr&0x380)>>4)];
874 cc=((cc >> ((zz&2) + ((rfraddr&0x40)>>4))) &3) <<2;
875 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
882 if((rfraddr&0x1f)==0x1f) {
884 page = vnapage[(rfraddr>>10)&3];
888 RefreshAddr = rfraddr;
891 static void RefreshLine_normal(uint8 *P, uint32 vofs) // vofs is 0x107 max
894 uint32 rfraddr = RefreshAddr;
895 uint8 *page = vnapage[(rfraddr>>10)&3];
898 if ((rfraddr&0xc)!=0)
899 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
901 for (X1=33;X1;X1--,P+=8)
906 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
908 if ((rfraddr&0xc)==0)
909 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
910 cc=((cc2 >> ((rfraddr&2) + ((rfraddr&0x40)>>4) + ((rfraddr&0xc)<<1))) & 3) << 2;
914 if((rfraddr&0x1f)==0x1f) {
916 page = vnapage[(rfraddr>>10)&3];
920 RefreshAddr = rfraddr;
923 static void SetRefreshLine(void)
925 if(MMC5Hack && geniestage!=1)
927 if(MMC5HackCHRMode==0 && (MMC5HackSPMode&0x80))
929 if (RefreshLine != RefreshLine_MMC5Hack1) printf("set refr RefreshLine_MMC5Hack1\n");
930 RefreshLine = RefreshLine_MMC5Hack1;
932 else if(MMC5HackCHRMode==1 && (MMC5HackSPMode&0x80))
934 if (RefreshLine != RefreshLine_MMC5Hack2) printf("set refr RefreshLine_MMC5Hack2\n");
935 RefreshLine = RefreshLine_MMC5Hack2;
937 else if(MMC5HackCHRMode==1)
939 if (RefreshLine != RefreshLine_MMC5Hack3) printf("set refr RefreshLine_MMC5Hack3\n");
940 RefreshLine = RefreshLine_MMC5Hack3;
944 if (RefreshLine != RefreshLine_MMC5Hack4) printf("set refr RefreshLine_MMC5Hack4\n");
945 RefreshLine = RefreshLine_MMC5Hack4;
947 } // End if(MMC5Hack)
950 if (RefreshLine != RefreshLine_PPU_hook) printf("set refr RefreshLine_PPU_hook\n");
951 RefreshLine = RefreshLine_PPU_hook;
955 if (RefreshLine != RefreshLine_normal) printf("set refr RefreshLine_normal\n");
956 RefreshLine = RefreshLine_normal;
963 if(ScreenON || SpriteON)
965 uint32 rad=RefreshAddr;
967 rad|=TempAddr&0x041f;
969 //PPU_hook(RefreshAddr,-1);
976 if(ScreenON || SpriteON)
978 uint32 rad=RefreshAddr;
980 if((rad&0x7000)==0x7000)
983 if((rad&0x3E0)==0x3A0)
990 if((rad&0x3E0)==0x3e0)
998 //PPU_hook(RefreshAddr,-1);
1003 // ============================//
1005 // ===========================//
1007 void ResetMapping(void)
1011 SetReadHandler(0x0000,0xFFFF,ANull);
1012 SetWriteHandler(0x0000,0xFFFF,BNull);
1014 SetReadHandler(0,0x7FF,ARAML);
1015 SetWriteHandler(0,0x7FF,BRAML);
1017 SetReadHandler(0x800,0x1FFF,ARAMH); /* Part of a little */
1018 SetWriteHandler(0x800,0x1FFF,BRAMH); /* hack for a small speed boost. */
1020 for(x=0x2000;x<0x4000;x+=8)
1040 BWrite[0x4014]=B4014;
1046 void CloseGame(void)
1051 if(FCEUGameInfo.type!=GIT_NSF)
1052 FCEU_FlushGameCheats(0,0);
1054 if(FSettings.NetworkPlay) KillNetplay();
1056 GameInterface(GI_CLOSE, 0);
1062 void ResetGameLoaded(void)
1064 if(GameLoaded) CloseGame();
1067 GameHBIRQHook=GameHBIRQHook2=0;
1068 GameExpSound.Fill=0;
1069 GameExpSound.RChange=0;
1070 if(GameExpSound.Kill)
1071 GameExpSound.Kill();
1072 GameExpSound.Kill=0;
1078 FCEUGameInfo.name=0;
1079 FCEUGameInfo.type=GIT_CART;
1080 FCEUGameInfo.vidsys=GIV_USER;
1081 FCEUGameInfo.input[0]=FCEUGameInfo.input[1]=-1;
1082 FCEUGameInfo.inputfc=-1;
1084 FCEUGameInfo.soundchan=0;
1085 FCEUGameInfo.soundrate=0;
1086 FCEUGameInfo.cspecial=0;
1089 char lastLoadedGameName [2048];
1090 int LoadGameLastError = 0;
1091 int UNIFLoad(const char *name, int fp);
1092 int iNESLoad(const char *name, int fp, int OverwriteVidMode);
1093 int FDSLoad(const char *name, int fp);
1094 int NSFLoad(int fp);
1096 FCEUGI *FCEUI_LoadGame(char *name)
1099 int have_movie = 0, have_ips = 0;
1103 LoadGameLastError = 0;
1106 strncpy(name2, name, sizeof(name2));
1107 name2[sizeof(name2)-1] = 0;
1109 fp=FCEU_fopen(name2,"rb");
1112 FCEU_PrintError("Error opening \"%s\"!",name);
1113 LoadGameLastError = 1;
1118 char *p = name2 + strlen(name2) - 4;
1119 if (strcasecmp(p, ".fcm") == 0) printf("movie detected\n"), have_movie = 1;
1120 if (strcasecmp(p, ".ips") == 0) printf("ips detected\n"), have_ips = 1;
1121 if (have_movie || have_ips)
1126 fp=FCEU_fopen(name2,"rb");
1127 if (!fp && p - name2 > 2)
1129 for (p--; p > name2 && *p != '.'; p--);
1131 fp=FCEU_fopen(name2,"rb");
1134 printf("no ROM for ips/movie\n");
1135 LoadGameLastError = 2;
1145 FILE *ips = fopen(name, "rb");
1147 fp=FCEU_fopen_forcemem(name2);
1148 if (!fp) { fclose(ips); return 0; }
1149 ApplyIPS(ips, fp); // closes ips
1153 if(iNESLoad(name2,fp,1))
1157 if(FDSLoad(name2,fp))
1159 if(UNIFLoad(name2,fp))
1162 FCEU_PrintError("An error occurred while loading the file.");
1164 // format handlers may set LoadGameLastError themselves.
1165 if (LoadGameLastError == 0) LoadGameLastError = 3;
1173 if(FCEUGameInfo.type!=GIT_NSF)
1174 if(FSettings.GameGenie)
1179 if(FSettings.NetworkPlay) InitNetplay();
1182 if(FCEUGameInfo.type!=GIT_NSF)
1184 FCEU_LoadGamePalette();
1185 FCEU_LoadGameCheats(0);
1188 FCEU_ResetPalette();
1192 FCEUI_LoadMovie(name, 1);
1194 strcpy(lastLoadedGameName, name2);
1196 return(&FCEUGameInfo);
1200 void FCEU_ResetVidSys(void)
1204 if(FCEUGameInfo.vidsys==GIV_NTSC)
1206 else if(FCEUGameInfo.vidsys==GIV_PAL)
1214 FSettings.FirstSLine=FSettings.UsrFirstSLine[1];
1215 FSettings.LastSLine=FSettings.UsrLastSLine[1];
1220 FSettings.FirstSLine=FSettings.UsrFirstSLine[0];
1221 FSettings.LastSLine=FSettings.UsrLastSLine[0];
1223 printf("ResetVidSys: PAL = %i\n", PAL);
1224 SetSoundVariables();
1227 int FCEUI_Initialize(void)
1229 if(!InitVirtualVideo())
1231 memset(&FSettings,0,sizeof(FSettings));
1232 FSettings.UsrFirstSLine[0]=8;
1233 FSettings.UsrFirstSLine[1]=0;
1234 FSettings.UsrLastSLine[0]=FSettings.UsrLastSLine[1]=239;
1235 FSettings.SoundVolume=100;
1237 FCEUI_Initialize098();
1238 FCEUI_SetEmuMode(0);
1245 void FCEUI_Kill(void)
1250 static void EmLoop(void);
1253 void (*ResetNES)(void) = 0;
1254 void (*FCEUI_Emulate)(void) = 0;
1256 void FCEUI_SetEmuMode(int is_new)
1258 use098code = is_new;
1261 ResetNES=ResetNES098;
1262 FCEUI_Emulate=FCEUI_Emulate098;
1266 ResetNES=ResetNES081;
1267 FCEUI_Emulate=EmLoop;
1271 void MMC5_hb(int); /* Ugh ugh ugh. */
1272 static void DoLine(void)
1274 uint8 *target=XBuf+scanline*320+32;
1278 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1282 // check: Battletoads & Double Dragon, Addams Family
1283 // sky glitches in SMB1 if done wrong
1289 if(scanline>=FSettings.FirstSLine && scanline<=FSettings.LastSLine)
1291 if(SpriteON && spork)
1292 CopySprites(target);
1294 LineUpdateEnd(target);
1298 if(ScreenON || SpriteON)
1302 if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
1308 X6502_Run(85-10-16);
1312 X6502_Run(6); // Tried 65, caused problems with Slalom(maybe others)
1319 if(GameHBIRQHook2 && (ScreenON || SpriteON))
1323 ResetRL(XBuf+scanline*320+32);
1328 static void EmLoop(void)
1333 uint32 scanlines_per_frame = PAL ? 312 : 262;
1335 FCEU_ApplyPeriodicCheats();
1338 if(ppudead) /* Needed for Knight Rider, possibly others. */
1340 //memset(XBuf, 0, 320*240);
1341 //X6502_Run(scanlines_per_frame*(256+85));
1343 for (lines=scanlines_per_frame;lines;lines--)
1352 PPU[3]=PPUSPL=0; /* Not sure if this is correct. According
1353 to Matt Conte and my own tests, it is. Timing is probably
1354 off, though. NOTE: Not having this here
1355 breaks a Super Donkey Kong game. */
1357 X6502_Run(12); /* I need to figure out the true nature and length
1360 if(FCEUGameInfo.type==GIT_NSF)
1365 // Note: this is needed for asm core
1366 // Warning: using 'scanline' var here breaks Castlevania III
1369 X6502_Run(256+85-12);
1370 for (lines=scanlines_per_frame-242-1;lines;lines--)
1373 // X6502_Run((scanlines_per_frame-242)*(256+85)-12);
1378 if(ScreenON || SpriteON)
1383 for(x=0;x<42;x++) {PPU_hook(0x2000); PPU_hook(0);} // ugh
1390 if(ScreenON || SpriteON)
1392 RefreshAddr=TempAddr;
1393 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
1402 if(FCEUGameInfo.type==GIT_NSF)
1404 // run scanlines for asm core to fuction
1405 for(scanline=0;scanline<240;scanline++)
1414 for(scanline=0;scanline<240;) // scanline is incremented in DoLine. Evil. :/
1419 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1420 for(x=1,max=0,maxref=0;x<7;x++)
1429 SetNESDeemph(maxref,0);
1442 ssize=FlushEmulateSound();
1444 timestampbase += timestamp;
1450 FCEU_PutImageDummy();
1452 FCEUD_Update(0,WaveFinalMono,ssize);
1458 FCEUD_Update(XBuf+8,WaveFinalMono,ssize);
1465 void FCEUI_CloseGame(void)
1470 static void ResetPPU(void)
1472 VRAMBuffer=PPU[0]=PPU[1]=PPU[2]=PPU[3]=0;
1475 RefreshAddr=TempAddr=0;
1481 static void PowerPPU(void)
1483 memset(NTARAM,0x00,0x800);
1484 memset(PALRAM,0x00,0x20);
1485 memset(SPRAM,0x00,0x100);
1489 void ResetNES081(void)
1491 if(!GameLoaded) return;
1492 GameInterface(GI_RESETM2, 0);
1498 void FCEU_MemoryRand(uint8 *ptr, uint32 size)
1500 #ifndef DEBUG_ASM_6502
1504 *ptr=(x&4)?0xFF:0x00;
1514 if(!GameLoaded) return;
1516 FCEU_CheatResetRAM();
1517 FCEU_CheatAddRAM(2,0,RAM);
1521 FCEU_MemoryRand(RAM,0x800);
1522 #ifdef DEBUG_ASM_6502
1523 memset(RAM,0x00,0x800);
1524 memset(nes_internal_ram,0x00,0x800);
1533 /* Have the external game hardware "powered" after the internal NES stuff.
1534 Needed for the NSF code and VS System code.
1536 GameInterface(GI_POWER, 0);
1537 if(FCEUGameInfo.type==GIT_VSUNI)
1549 /* savestate stuff */
1550 uint16 TempAddrT,RefreshAddrT;
1552 SFORMAT FCEUPPU_STATEINFO[]={
1553 { NTARAM, 0x800, "NTAR"},
1554 { PALRAM, 0x20, "PRAM"},
1555 { SPRAM, 0x100, "SPRA"},
1556 { PPU, 0x4, "PPUR"},
1557 { &XOffset, 1, "XOFF"},
1558 { &vtoggle, 1, "VTOG"},
1559 { &RefreshAddrT, 2|RLSB, "RADD"},
1560 { &TempAddrT, 2|RLSB, "TADD"},
1561 { &VRAMBuffer, 1, "VBUF"},
1562 { &PPUGenLatch, 1, "PGEN"},
1564 { &kook, 1, "KOOK"},
1565 { &ppudead, 1, "DEAD"},
1566 { &PPUSPL, 1, "PSPL"},