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
639 FCEU_dwmemset(target,tem,256);
641 else if(scanline < FSettings.FirstSLine || scanline > FSettings.LastSLine)
646 if(scanline==y && SpriteON) PPU_status|=0x40;
653 if(InputScanlineHook)
654 InputScanlineHook(target,spork?sprlinebuf:0,linestartts,256);
658 static void LineUpdateEnd(uint8 *target)
661 if(ScreenON || SpriteON) // Yes, very el-cheapo.
664 block_and(target, 256, 0x30);
667 block_or(target, 256, 0xc0);
669 block_or(target, 256, 0x40);
671 block_andor(target, 256, 0x3f, 0x80);
675 if(ScreenON || SpriteON) // Yes, very el-cheapo.
680 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0x30303030;
686 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0xc0c0c0c0;
690 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])|0x40404040;
693 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0x80808080;
697 ((uint32 *)target)[-2]=((uint32 *)target)[-1]=0;
698 ((uint32 *)target)[64]=((uint32 *)target)[65]=0;
701 #define PAL(c) ((c)+cc)
704 static void PRefreshLine(void)
708 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
709 void (*PPU_hook_)(uint32 A) = PPU_hook;
717 zz2=(RefreshAddr>>10)&3;
718 PPU_hook_(0x2000|(RefreshAddr&0xFFF));
720 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
724 if((RefreshAddr&0x1f)==0x1f)
731 /* This high-level graphics MMC5 emulation code was written
732 for MMC5 carts in "CL" mode. It's probably not totally
733 correct for carts in "SL" mode.
735 static void RefreshLine_MMC5Hack1(uint8 *P, uint32 vofs)
739 tochange=MMC5HackSPMode&0x1F;
741 for(X1=33;X1;X1--,P+=8)
747 if((tochange<=0 && MMC5HackSPMode&0x40) ||
748 (tochange>0 && !(MMC5HackSPMode&0x40)))
753 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
754 if(ys>=0x1E) ys-=0x1E;
755 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
757 C = MMC5HackVROMPTR+vadr;
758 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
760 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
761 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
766 zz2=(RefreshAddr>>10)&3;
767 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
768 C = MMC5BGVRAMADR(vadr);
769 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
770 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
774 if((RefreshAddr&0x1f)==0x1f)
782 static void RefreshLine_MMC5Hack2(uint8 *P, uint32 vofs)
786 tochange=MMC5HackSPMode&0x1F;
788 for(X1=33;X1;X1--,P+=8)
795 if((tochange<=0 && MMC5HackSPMode&0x40) ||
796 (tochange>0 && !(MMC5HackSPMode&0x40)))
801 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
802 if(ys>=0x1E) ys-=0x1E;
803 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
805 C = MMC5HackVROMPTR+vadr;
806 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
808 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
809 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
814 zz2=(RefreshAddr>>10)&3;
815 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
816 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
817 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
818 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
822 if((RefreshAddr&0x1f)==0x1f)
830 static void RefreshLine_MMC5Hack3(uint8 *P, uint32 vofs)
834 for(X1=33;X1;X1--,P+=8)
842 zz2=(RefreshAddr>>10)&3;
843 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
844 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
845 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
846 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
850 if((RefreshAddr&0x1f)==0x1f)
857 static void RefreshLine_MMC5Hack4(uint8 *P, uint32 vofs)
861 for(X1=33;X1;X1--,P+=8)
868 zz2=(RefreshAddr>>10)&3;
869 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
870 C = MMC5BGVRAMADR(vadr);
871 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
872 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
876 if((RefreshAddr&0x1f)==0x1f)
883 static void RefreshLine_PPU_hook(uint8 *P, uint32 vofs)
886 void (*PPU_hook_)(uint32 A) = PPU_hook;
887 uint32 rfraddr = RefreshAddr;
888 uint8 *page = vnapage[(rfraddr>>10)&3];
890 for(X1=33;X1;X1--,P+=8)
897 PPU_hook_(0x2000|(rfraddr&0xFFF));
898 cc=page[0x3c0+(zz>>2)+((rfraddr&0x380)>>4)];
899 cc=((cc >> ((zz&2) + ((rfraddr&0x40)>>4))) &3) <<2;
900 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
907 if((rfraddr&0x1f)==0x1f) {
909 page = vnapage[(rfraddr>>10)&3];
913 RefreshAddr = rfraddr;
916 static void RefreshLine_normal(uint8 *P, uint32 vofs) // vofs is 0x107 max
919 uint32 rfraddr = RefreshAddr;
920 uint8 *page = vnapage[(rfraddr>>10)&3];
923 if ((rfraddr&0xc)!=0)
924 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
926 for (X1=33;X1;X1--,P+=8)
931 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
933 if ((rfraddr&0xc)==0)
934 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
935 cc=((cc2 >> ((rfraddr&2) + ((rfraddr&0x40)>>4) + ((rfraddr&0xc)<<1))) & 3) << 2;
939 if((rfraddr&0x1f)==0x1f) {
941 page = vnapage[(rfraddr>>10)&3];
945 RefreshAddr = rfraddr;
948 static void SetRefreshLine(void)
950 if(MMC5Hack && geniestage!=1)
952 if(MMC5HackCHRMode==0 && (MMC5HackSPMode&0x80))
954 if (RefreshLine != RefreshLine_MMC5Hack1) printf("set refr RefreshLine_MMC5Hack1\n");
955 RefreshLine = RefreshLine_MMC5Hack1;
957 else if(MMC5HackCHRMode==1 && (MMC5HackSPMode&0x80))
959 if (RefreshLine != RefreshLine_MMC5Hack2) printf("set refr RefreshLine_MMC5Hack2\n");
960 RefreshLine = RefreshLine_MMC5Hack2;
962 else if(MMC5HackCHRMode==1)
964 if (RefreshLine != RefreshLine_MMC5Hack3) printf("set refr RefreshLine_MMC5Hack3\n");
965 RefreshLine = RefreshLine_MMC5Hack3;
969 if (RefreshLine != RefreshLine_MMC5Hack4) printf("set refr RefreshLine_MMC5Hack4\n");
970 RefreshLine = RefreshLine_MMC5Hack4;
972 } // End if(MMC5Hack)
975 if (RefreshLine != RefreshLine_PPU_hook) printf("set refr RefreshLine_PPU_hook\n");
976 RefreshLine = RefreshLine_PPU_hook;
980 if (RefreshLine != RefreshLine_normal) printf("set refr RefreshLine_normal\n");
981 RefreshLine = RefreshLine_normal;
988 if(ScreenON || SpriteON)
990 uint32 rad=RefreshAddr;
992 rad|=TempAddr&0x041f;
994 //PPU_hook(RefreshAddr,-1);
1001 if(ScreenON || SpriteON)
1003 uint32 rad=RefreshAddr;
1005 if((rad&0x7000)==0x7000)
1008 if((rad&0x3E0)==0x3A0)
1015 if((rad&0x3E0)==0x3e0)
1023 //PPU_hook(RefreshAddr,-1);
1028 // ============================//
1030 // ===========================//
1032 void ResetMapping(void)
1036 SetReadHandler(0x0000,0xFFFF,ANull);
1037 SetWriteHandler(0x0000,0xFFFF,BNull);
1039 SetReadHandler(0,0x7FF,ARAML);
1040 SetWriteHandler(0,0x7FF,BRAML);
1042 SetReadHandler(0x800,0x1FFF,ARAMH); /* Part of a little */
1043 SetWriteHandler(0x800,0x1FFF,BRAMH); /* hack for a small speed boost. */
1045 for(x=0x2000;x<0x4000;x+=8)
1065 BWrite[0x4014]=B4014;
1071 void CloseGame(void)
1076 if(FCEUGameInfo.type!=GIT_NSF)
1077 FCEU_FlushGameCheats(0,0);
1079 if(FSettings.NetworkPlay) KillNetplay();
1081 GameInterface(GI_CLOSE, 0);
1087 void ResetGameLoaded(void)
1089 if(GameLoaded) CloseGame();
1092 GameHBIRQHook=GameHBIRQHook2=0;
1093 GameExpSound.Fill=0;
1094 GameExpSound.RChange=0;
1095 if(GameExpSound.Kill)
1096 GameExpSound.Kill();
1097 GameExpSound.Kill=0;
1103 FCEUGameInfo.name=0;
1104 FCEUGameInfo.type=GIT_CART;
1105 FCEUGameInfo.vidsys=GIV_USER;
1106 FCEUGameInfo.input[0]=FCEUGameInfo.input[1]=-1;
1107 FCEUGameInfo.inputfc=-1;
1109 FCEUGameInfo.soundchan=0;
1110 FCEUGameInfo.soundrate=0;
1111 FCEUGameInfo.cspecial=0;
1114 char lastLoadedGameName [2048];
1115 int LoadGameLastError = 0;
1116 int UNIFLoad(const char *name, int fp);
1117 int iNESLoad(const char *name, int fp);
1118 int FDSLoad(const char *name, int fp);
1119 int NSFLoad(int fp);
1121 FCEUGI *FCEUI_LoadGame(char *name)
1124 int have_movie = 0, have_ips = 0;
1128 LoadGameLastError = 0;
1131 strncpy(name2, name, sizeof(name2));
1132 name2[sizeof(name2)-1] = 0;
1134 fp=FCEU_fopen(name2,"rb");
1137 FCEU_PrintError("Error opening \"%s\"!",name);
1138 LoadGameLastError = 1;
1143 char *p = name2 + strlen(name2) - 4;
1144 if (strcasecmp(p, ".fcm") == 0) printf("movie detected\n"), have_movie = 1;
1145 if (strcasecmp(p, ".ips") == 0) printf("ips detected\n"), have_ips = 1;
1146 if (have_movie || have_ips)
1151 fp=FCEU_fopen(name2,"rb");
1152 if (!fp && p - name2 > 2)
1154 for (p--; p > name2 && *p != '.'; p--);
1156 fp=FCEU_fopen(name2,"rb");
1159 printf("no ROM for ips/movie\n");
1160 LoadGameLastError = 2;
1170 FILE *ips = fopen(name, "rb");
1172 fp=FCEU_fopen_forcemem(name2);
1173 if (!fp) { fclose(ips); return 0; }
1174 ApplyIPS(ips, fp); // closes ips
1178 if(iNESLoad(name2,fp))
1182 if(FDSLoad(name2,fp))
1184 if(UNIFLoad(name2,fp))
1187 FCEU_PrintError("An error occurred while loading the file.");
1189 // format handlers may set LoadGameLastError themselves.
1190 if (LoadGameLastError == 0) LoadGameLastError = 3;
1198 if(FCEUGameInfo.type!=GIT_NSF)
1199 if(FSettings.GameGenie)
1204 if(FSettings.NetworkPlay) InitNetplay();
1207 if(FCEUGameInfo.type!=GIT_NSF)
1209 FCEU_LoadGamePalette();
1210 FCEU_LoadGameCheats(0);
1213 FCEU_ResetPalette();
1217 FCEUI_LoadMovie(name, 1);
1219 strcpy(lastLoadedGameName, name2);
1221 return(&FCEUGameInfo);
1225 void FCEU_ResetVidSys(void)
1229 if(FCEUGameInfo.vidsys==GIV_NTSC)
1231 else if(FCEUGameInfo.vidsys==GIV_PAL)
1239 FSettings.FirstSLine=FSettings.UsrFirstSLine[1];
1240 FSettings.LastSLine=FSettings.UsrLastSLine[1];
1245 FSettings.FirstSLine=FSettings.UsrFirstSLine[0];
1246 FSettings.LastSLine=FSettings.UsrLastSLine[0];
1248 printf("ResetVidSys: PAL = %i\n", PAL);
1249 SetSoundVariables();
1252 int FCEUI_Initialize(void)
1254 if(!InitVirtualVideo())
1256 memset(&FSettings,0,sizeof(FSettings));
1257 FSettings.UsrFirstSLine[0]=8;
1258 FSettings.UsrFirstSLine[1]=0;
1259 FSettings.UsrLastSLine[0]=FSettings.UsrLastSLine[1]=239;
1260 FSettings.SoundVolume=100;
1262 FCEUI_Initialize098();
1263 FCEUI_SetEmuMode(0);
1268 void FCEUI_Kill(void)
1273 static void EmLoop(void);
1276 void (*ResetNES)(void) = 0;
1277 void (*FCEUI_Emulate)(void) = 0;
1279 void FCEUI_SetEmuMode(int is_new)
1281 use098code = is_new;
1284 ResetNES=ResetNES098;
1285 FCEUI_Emulate=FCEUI_Emulate098;
1289 ResetNES=ResetNES081;
1290 FCEUI_Emulate=EmLoop;
1294 void MMC5_hb(int); /* Ugh ugh ugh. */
1295 static void DoLine(void)
1297 uint8 *target=XBuf+scanline*320+32;
1301 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1305 // check: Battletoads & Double Dragon, Addams Family
1306 // sky glitches in SMB1 if done wrong
1312 if(scanline>=FSettings.FirstSLine && scanline<=FSettings.LastSLine)
1314 if(SpriteON && spork)
1315 CopySprites(target);
1317 LineUpdateEnd(target);
1321 if(ScreenON || SpriteON)
1325 if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
1331 X6502_Run(85-10-16);
1335 X6502_Run(6); // Tried 65, caused problems with Slalom(maybe others)
1342 if(GameHBIRQHook2 && (ScreenON || SpriteON))
1346 ResetRL(XBuf+scanline*320+32);
1351 static void EmLoop(void)
1356 uint32 scanlines_per_frame = PAL ? 312 : 262;
1358 FCEU_ApplyPeriodicCheats();
1361 if(ppudead) /* Needed for Knight Rider, possibly others. */
1363 //memset(XBuf, 0, 320*240);
1364 //X6502_Run(scanlines_per_frame*(256+85));
1366 for (lines=scanlines_per_frame;lines;lines--)
1375 PPU[3]=PPUSPL=0; /* Not sure if this is correct. According
1376 to Matt Conte and my own tests, it is. Timing is probably
1377 off, though. NOTE: Not having this here
1378 breaks a Super Donkey Kong game. */
1380 X6502_Run(12); /* I need to figure out the true nature and length
1383 if(FCEUGameInfo.type==GIT_NSF)
1388 // Note: this is needed for asm core
1389 // Warning: using 'scanline' var here breaks Castlevania III
1392 X6502_Run(256+85-12);
1393 for (lines=scanlines_per_frame-242-1;lines;lines--)
1396 // X6502_Run((scanlines_per_frame-242)*(256+85)-12);
1401 if(ScreenON || SpriteON)
1406 for(x=0;x<42;x++) {PPU_hook(0x2000); PPU_hook(0);} // ugh
1413 if(ScreenON || SpriteON)
1415 RefreshAddr=TempAddr;
1416 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
1425 if(FCEUGameInfo.type==GIT_NSF)
1427 // run scanlines for asm core to fuction
1428 for(scanline=0;scanline<240;scanline++)
1437 for(scanline=0;scanline<240;) // scanline is incremented in DoLine. Evil. :/
1442 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1443 for(x=1,max=0,maxref=0;x<7;x++)
1452 SetNESDeemph(maxref,0);
1465 ssize=FlushEmulateSound();
1467 timestampbase += timestamp;
1473 FCEU_PutImageDummy();
1475 FCEUD_Update(0,WaveFinalMono,ssize);
1481 FCEUD_Update(XBuf+8,WaveFinalMono,ssize);
1488 void FCEUI_CloseGame(void)
1493 static void ResetPPU(void)
1495 VRAMBuffer=PPU[0]=PPU[1]=PPU[2]=PPU[3]=0;
1498 RefreshAddr=TempAddr=0;
1504 static void PowerPPU(void)
1506 memset(NTARAM,0x00,0x800);
1507 memset(PALRAM,0x00,0x20);
1508 memset(SPRAM,0x00,0x100);
1512 void ResetNES081(void)
1514 if(!GameLoaded) return;
1515 GameInterface(GI_RESETM2, 0);
1521 #ifndef DEBUG_ASM_6502
1522 static void FCEU_MemoryRand(uint8 *ptr, uint32 size)
1527 *ptr=(x&4)?0xFF:0x00;
1537 if(!GameLoaded) return;
1539 FCEU_CheatResetRAM();
1540 FCEU_CheatAddRAM(2,0,RAM);
1544 #ifndef DEBUG_ASM_6502
1545 FCEU_MemoryRand(RAM,0x800);
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"},