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
59 #include "drivers/gp2x/asmutils.h"
65 static void (*RefreshLine)(uint8 *P, uint32 vofs) = NULL;
66 static void PRefreshLine(void);
68 static void ResetPPU(void);
69 static void PowerPPU(void);
71 uint64 timestampbase=0;
77 uint32 MMC5HackVROMMask;
78 uint8 *MMC5HackExNTARAMPtr;
79 uint8 *MMC5HackVROMPTR;
80 uint8 MMC5HackCHRMode=0;
82 uint8 MMC5HackSPScroll;
85 uint8 *MMC5SPRVPage[8];
86 uint8 *MMC5BGVPage[8];
89 uint8 VRAMBuffer,PPUGenLatch;
95 /* Color deemphasis emulation. Joy... */
97 static int deempcnt[8];
100 void (*GameInterface)(int h, void *param);
102 void FP_FASTAPASS(1) (*PPU_hook)(uint32 A);
104 void (*GameStateRestore)(int version);
105 void (*GameHBIRQHook)(void), (*GameHBIRQHook2)(void);
107 readfunc ARead[0x10000];
108 writefunc BWrite[0x10000];
109 static readfunc *AReadG;
110 static writefunc *BWriteG;
114 static void asmcpu_update(int32 cycles)
116 // some code from x6502.c
130 if((PSG[0x10]&0x80) && !(PSG[0x10]&0x40))
132 extern uint8 SIRQStat;
134 X6502_IRQBegin(FCEU_IQDPCM);
140 void asmcpu_unpack(void)
142 nes_registers[0] = X.A << 24;
143 nes_registers[1] = X.X;
144 nes_registers[2] = X.Y;
146 nes_registers[3] = X.PC;
148 nes_registers[4] = X.S << 24;
149 nes_registers[4]|= X.IRQlow << 8;
151 nes_registers[4] |= 1<<16; // MapIRQHook set bit
152 nes_registers[7] = (uint32)X.count << 16;
155 nes_registers[4]|= X.P & 0x5d;
156 nes_registers[5] = X.P << 24; // N
157 if (!(X.P&0x02)) nes_registers[5] |= 1; // Z
160 void asmcpu_pack(void)
162 X.A = nes_registers[0] >> 24;
163 X.X = nes_registers[1];
164 X.Y = nes_registers[2];
165 X.PC= nes_registers[3] - pc_base;
166 X.S = nes_registers[4] >> 24;
167 X.IRQlow = nes_registers[4] >> 8;
168 X.count = (int32) nes_registers[7] >> 16;
171 X.P = nes_registers[4] & 0x5d;
172 if ( nes_registers[5]&0x80000000) X.P |= 0x80; // N
173 if (!(nes_registers[5]&0x000000ff)) X.P |= 0x02; // Z
187 int AllocGenieRW(void)
189 if(!(AReadG=FCEU_malloc(0x8000*sizeof(readfunc))))
191 if(!(BWriteG=FCEU_malloc(0x8000*sizeof(writefunc))))
197 void FlushGenieRW(void)
203 for(x=0;x<0x8000;x++)
205 ARead[x+0x8000]=AReadG[x];
206 BWrite[x+0x8000]=BWriteG[x];
219 readfunc FASTAPASS(1) GetReadHandler(int32 a)
221 if(a>=0x8000 && RWWrap)
222 return AReadG[a-0x8000];
227 void FASTAPASS(3) SetReadHandler(int32 start, int32 end, readfunc func)
235 for(x=end;x>=start;x--)
238 AReadG[x-0x8000]=func;
244 for(x=end;x>=start;x--)
248 writefunc FASTAPASS(1) GetWriteHandler(int32 a)
250 if(RWWrap && a>=0x8000)
251 return BWriteG[a-0x8000];
256 void FASTAPASS(3) SetWriteHandler(int32 start, int32 end, writefunc func)
264 for(x=end;x>=start;x--)
267 BWriteG[x-0x8000]=func;
272 for(x=end;x>=start;x--)
279 uint32 TempAddr,RefreshAddr;
282 /* scanline is equal to the current visible scanline we're on. */
286 uint8 GameMemBlock[131072] __attribute__ ((aligned (4)));
287 uint8 NTARAM[0x800] __attribute__ ((aligned (4)));
288 uint8 PALRAM[0x20] __attribute__ ((aligned (4)));
289 #if !defined(ASM_6502) || defined(DEBUG_ASM_6502)
290 uint8 RAM[0x800] __attribute__ ((aligned (4)));
299 #define MMC5BGVRAMADR(V) &MMC5BGVPage[(V)>>10][(V)]
300 #define VRAMADR(V) &VPage[(V)>>10][(V)]
302 static int linestartts;
305 static uint8 *Plinef;
307 extern uint8 sprlinebuf[256+8];
309 extern uint8 sphitdata;
311 extern int spork; /* spork the world. Any sprites on this line?
312 Then this will be set to 1. Needed for zapper
313 emulation and *gasp* sprite emulation.
316 static void ResetRL(uint8 *target)
318 if(InputScanlineHook)
319 InputScanlineHook(0,0,0,0);
321 linestartts=timestamp*48+X6502_GetCycleCount();
325 static INLINE void Fixit1(void);
327 /* faking FCEUPPU_LineUpdate() from later versions of the emu */
328 static void FakedLineUpdate(void)
330 #define TOFIXNUM (272-0x4)
333 if (scanline >= 240) return;
335 if (tofix || sphitx != 0x100)
337 lastpixel = (timestamp*48-linestartts)>>4;
338 if (PAL) lastpixel += lastpixel>>4;
339 //printf("lastpixel: %i\n", lastpixel);
342 if (tofix && lastpixel>=TOFIXNUM)
354 for(x=sphitx;x<(sphitx+8) && x<l;x++)
356 if((sphitdata&(0x80>>(x-sphitx))) && !(Plinef[x]&64))
395 ret|=PPUGenLatch&0x1F;
399 //dprintf("r [2002] %02x",ret);
414 uint32 tmp=RefreshAddr&0x3FFF;
420 if(PPU_hook) PPU_hook(tmp);
421 PPUGenLatch=VRAMBuffer;
424 VRAMBuffer=VPage[tmp>>10][tmp];
428 VRAMBuffer=vnapage[(tmp>>10)&0x3][tmp&0x3FF];
431 if (INC32) RefreshAddr+=32;
433 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
434 dprintf("r [2007] %02x",ret);
456 //printf("$%04x:$%02x, %d\n",X.PC,V,scanline);
484 //printf("$%02x:$%02x\n",PPUSPL,V);
524 TempAddr|=(V&0x3f)<<8;
531 RefreshAddr=TempAddr;
533 PPU_hook(RefreshAddr);
541 uint32 tmp=RefreshAddr&0x3FFF;
547 PALRAM[0x00]=PALRAM[0x04]=PALRAM[0x08]=PALRAM[0x0C]=V&0x3f;
548 else if(tmp&3) PALRAM[(tmp&0x1f)]=V&0x3f;
552 if(PPUCHRRAM&(1<<(tmp>>10)))
553 VPage[tmp>>10][tmp]=V;
557 if(PPUNTARAM&(1<<((tmp&0xF00)>>10)))
558 vnapage[((tmp&0xF00)>>10)][tmp&0x3FF]=V;
560 if (INC32) RefreshAddr+=32;
562 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
571 B2004(0x2004,X.DB=ARead[t+x](t+x));
572 X6502_AddCycles(512);
575 void BGRender(uint8 *target)
578 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
584 RefreshLine(target-XOffset, vofs);
595 *(uint32 *)target=*(uint32 *)(target+4)=tem;
601 void FCEUI_FrameSkip(int x)
607 /* This is called at the beginning of each visible scanline */
608 static void LineUpdate(uint8 *target)
613 /* PRefreshLine() will not get called on skipped frames. This
614 * could cause a problem, but the solution would be rather complex,
615 * due to the current sprite 0 hit code.
620 if(scanline==y && SpriteON) PPU_status|=0x40; // hack
624 if(scanline < FSettings.FirstSLine || scanline > FSettings.LastSLine)
629 if(scanline==y && SpriteON) PPU_status|=0x40;
642 FCEU_dwmemset(target,tem,256);
646 if(InputScanlineHook)
647 InputScanlineHook(target,spork?sprlinebuf:0,linestartts,256);
651 static void LineUpdateEnd(uint8 *target)
654 if(ScreenON || SpriteON) // Yes, very el-cheapo.
657 block_and(target, 256, 0x30);
660 block_or(target, 256, 0xc0);
662 block_or(target, 256, 0x40);
664 block_andor(target, 256, 0x3f, 0x80);
668 if(ScreenON || SpriteON) // Yes, very el-cheapo.
673 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0x30303030;
679 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0xc0c0c0c0;
683 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])|0x40404040;
686 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0x80808080;
690 ((uint32 *)target)[-2]=((uint32 *)target)[-1]=0;
691 ((uint32 *)target)[64]=((uint32 *)target)[65]=0;
694 #define PAL(c) ((c)+cc)
697 static void PRefreshLine(void)
701 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
702 void (*PPU_hook_)(uint32 A) = PPU_hook;
710 zz2=(RefreshAddr>>10)&3;
711 PPU_hook_(0x2000|(RefreshAddr&0xFFF));
713 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
717 if((RefreshAddr&0x1f)==0x1f)
724 /* This high-level graphics MMC5 emulation code was written
725 for MMC5 carts in "CL" mode. It's probably not totally
726 correct for carts in "SL" mode.
728 static void RefreshLine_MMC5Hack1(uint8 *P, uint32 vofs)
732 tochange=MMC5HackSPMode&0x1F;
734 for(X1=33;X1;X1--,P+=8)
740 if((tochange<=0 && MMC5HackSPMode&0x40) ||
741 (tochange>0 && !(MMC5HackSPMode&0x40)))
746 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
747 if(ys>=0x1E) ys-=0x1E;
748 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
750 C = MMC5HackVROMPTR+vadr;
751 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
753 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
754 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
759 zz2=(RefreshAddr>>10)&3;
760 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
761 C = MMC5BGVRAMADR(vadr);
762 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
763 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
767 if((RefreshAddr&0x1f)==0x1f)
775 static void RefreshLine_MMC5Hack2(uint8 *P, uint32 vofs)
779 tochange=MMC5HackSPMode&0x1F;
781 for(X1=33;X1;X1--,P+=8)
788 if((tochange<=0 && MMC5HackSPMode&0x40) ||
789 (tochange>0 && !(MMC5HackSPMode&0x40)))
794 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
795 if(ys>=0x1E) ys-=0x1E;
796 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
798 C = MMC5HackVROMPTR+vadr;
799 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
801 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
802 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
807 zz2=(RefreshAddr>>10)&3;
808 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
809 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
810 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
811 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
815 if((RefreshAddr&0x1f)==0x1f)
823 static void RefreshLine_MMC5Hack3(uint8 *P, uint32 vofs)
827 for(X1=33;X1;X1--,P+=8)
835 zz2=(RefreshAddr>>10)&3;
836 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
837 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
838 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
839 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
843 if((RefreshAddr&0x1f)==0x1f)
850 static void RefreshLine_MMC5Hack4(uint8 *P, uint32 vofs)
854 for(X1=33;X1;X1--,P+=8)
861 zz2=(RefreshAddr>>10)&3;
862 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
863 C = MMC5BGVRAMADR(vadr);
864 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
865 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
869 if((RefreshAddr&0x1f)==0x1f)
876 static void RefreshLine_PPU_hook(uint8 *P, uint32 vofs)
879 void (*PPU_hook_)(uint32 A) = PPU_hook;
880 uint32 rfraddr = RefreshAddr;
881 uint8 *page = vnapage[(rfraddr>>10)&3];
883 for(X1=33;X1;X1--,P+=8)
890 PPU_hook_(0x2000|(rfraddr&0xFFF));
891 cc=page[0x3c0+(zz>>2)+((rfraddr&0x380)>>4)];
892 cc=((cc >> ((zz&2) + ((rfraddr&0x40)>>4))) &3) <<2;
893 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
900 if((rfraddr&0x1f)==0x1f) {
902 page = vnapage[(rfraddr>>10)&3];
906 RefreshAddr = rfraddr;
909 static void RefreshLine_normal(uint8 *P, uint32 vofs) // vofs is 0x107 max
912 uint32 rfraddr = RefreshAddr;
913 uint8 *page = vnapage[(rfraddr>>10)&3];
916 if ((rfraddr&0xc)!=0)
917 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
919 for (X1=33;X1;X1--,P+=8)
924 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
926 if ((rfraddr&0xc)==0)
927 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
928 cc=((cc2 >> ((rfraddr&2) + ((rfraddr&0x40)>>4) + ((rfraddr&0xc)<<1))) & 3) << 2;
932 if((rfraddr&0x1f)==0x1f) {
934 page = vnapage[(rfraddr>>10)&3];
938 RefreshAddr = rfraddr;
941 static void SetRefreshLine(void)
943 if(MMC5Hack && geniestage!=1)
945 if(MMC5HackCHRMode==0 && (MMC5HackSPMode&0x80))
947 if (RefreshLine != RefreshLine_MMC5Hack1) printf("set refr RefreshLine_MMC5Hack1\n");
948 RefreshLine = RefreshLine_MMC5Hack1;
950 else if(MMC5HackCHRMode==1 && (MMC5HackSPMode&0x80))
952 if (RefreshLine != RefreshLine_MMC5Hack2) printf("set refr RefreshLine_MMC5Hack2\n");
953 RefreshLine = RefreshLine_MMC5Hack2;
955 else if(MMC5HackCHRMode==1)
957 if (RefreshLine != RefreshLine_MMC5Hack3) printf("set refr RefreshLine_MMC5Hack3\n");
958 RefreshLine = RefreshLine_MMC5Hack3;
962 if (RefreshLine != RefreshLine_MMC5Hack4) printf("set refr RefreshLine_MMC5Hack4\n");
963 RefreshLine = RefreshLine_MMC5Hack4;
965 } // End if(MMC5Hack)
968 if (RefreshLine != RefreshLine_PPU_hook) printf("set refr RefreshLine_PPU_hook\n");
969 RefreshLine = RefreshLine_PPU_hook;
973 if (RefreshLine != RefreshLine_normal) printf("set refr RefreshLine_normal\n");
974 RefreshLine = RefreshLine_normal;
981 if(ScreenON || SpriteON)
983 uint32 rad=RefreshAddr;
985 rad|=TempAddr&0x041f;
987 //PPU_hook(RefreshAddr,-1);
994 if(ScreenON || SpriteON)
996 uint32 rad=RefreshAddr;
998 if((rad&0x7000)==0x7000)
1001 if((rad&0x3E0)==0x3A0)
1008 if((rad&0x3E0)==0x3e0)
1016 //PPU_hook(RefreshAddr,-1);
1021 // ============================//
1023 // ===========================//
1025 void ResetMapping(void)
1029 SetReadHandler(0x0000,0xFFFF,ANull);
1030 SetWriteHandler(0x0000,0xFFFF,BNull);
1032 SetReadHandler(0,0x7FF,ARAML);
1033 SetWriteHandler(0,0x7FF,BRAML);
1035 SetReadHandler(0x800,0x1FFF,ARAMH); /* Part of a little */
1036 SetWriteHandler(0x800,0x1FFF,BRAMH); /* hack for a small speed boost. */
1038 for(x=0x2000;x<0x4000;x+=8)
1058 BWrite[0x4014]=B4014;
1064 void CloseGame(void)
1069 if(FCEUGameInfo.type!=GIT_NSF)
1070 FCEU_FlushGameCheats(0,0);
1072 if(FSettings.NetworkPlay) KillNetplay();
1074 GameInterface(GI_CLOSE, 0);
1080 void ResetGameLoaded(void)
1082 if(GameLoaded) CloseGame();
1085 GameHBIRQHook=GameHBIRQHook2=0;
1086 GameExpSound.Fill=0;
1087 GameExpSound.RChange=0;
1088 if(GameExpSound.Kill)
1089 GameExpSound.Kill();
1090 GameExpSound.Kill=0;
1096 FCEUGameInfo.name=0;
1097 FCEUGameInfo.type=GIT_CART;
1098 FCEUGameInfo.vidsys=GIV_USER;
1099 FCEUGameInfo.input[0]=FCEUGameInfo.input[1]=-1;
1100 FCEUGameInfo.inputfc=-1;
1102 FCEUGameInfo.soundchan=0;
1103 FCEUGameInfo.soundrate=0;
1104 FCEUGameInfo.cspecial=0;
1107 char lastLoadedGameName [2048];
1108 int LoadGameLastError = 0;
1109 int UNIFLoad(const char *name, int fp);
1110 int iNESLoad(const char *name, int fp);
1111 int FDSLoad(const char *name, int fp);
1112 int NSFLoad(int fp);
1114 FCEUGI *FCEUI_LoadGame(char *name)
1117 int have_movie = 0, have_ips = 0;
1121 LoadGameLastError = 0;
1124 strncpy(name2, name, sizeof(name2));
1125 name2[sizeof(name2)-1] = 0;
1127 fp=FCEU_fopen(name2,"rb");
1130 FCEU_PrintError("Error opening \"%s\"!",name);
1131 LoadGameLastError = 1;
1136 char *p = name2 + strlen(name2) - 4;
1137 if (strcasecmp(p, ".fcm") == 0) printf("movie detected\n"), have_movie = 1;
1138 if (strcasecmp(p, ".ips") == 0) printf("ips detected\n"), have_ips = 1;
1139 if (have_movie || have_ips)
1144 fp=FCEU_fopen(name2,"rb");
1145 if (!fp && p - name2 > 2)
1147 for (p--; p > name2 && *p != '.'; p--);
1149 fp=FCEU_fopen(name2,"rb");
1152 printf("no ROM for ips/movie\n");
1153 LoadGameLastError = 2;
1163 FILE *ips = fopen(name, "rb");
1165 fp=FCEU_fopen_forcemem(name2);
1166 if (!fp) { fclose(ips); return 0; }
1167 ApplyIPS(ips, fp); // closes ips
1171 if(iNESLoad(name2,fp))
1175 if(FDSLoad(name2,fp))
1177 if(UNIFLoad(name2,fp))
1180 FCEU_PrintError("An error occurred while loading the file.");
1182 // format handlers may set LoadGameLastError themselves.
1183 if (LoadGameLastError == 0) LoadGameLastError = 3;
1191 if(FCEUGameInfo.type!=GIT_NSF)
1192 if(FSettings.GameGenie)
1197 if(FSettings.NetworkPlay) InitNetplay();
1200 if(FCEUGameInfo.type!=GIT_NSF)
1202 FCEU_LoadGamePalette();
1203 FCEU_LoadGameCheats(0);
1206 FCEU_ResetPalette();
1210 FCEUI_LoadMovie(name, 1);
1212 strcpy(lastLoadedGameName, name2);
1214 return(&FCEUGameInfo);
1218 void FCEU_ResetVidSys(void)
1222 if(FCEUGameInfo.vidsys==GIV_NTSC)
1224 else if(FCEUGameInfo.vidsys==GIV_PAL)
1232 FSettings.FirstSLine=FSettings.UsrFirstSLine[1];
1233 FSettings.LastSLine=FSettings.UsrLastSLine[1];
1238 FSettings.FirstSLine=FSettings.UsrFirstSLine[0];
1239 FSettings.LastSLine=FSettings.UsrLastSLine[0];
1241 printf("ResetVidSys: PAL = %i\n", PAL);
1242 SetSoundVariables();
1245 int FCEUI_Initialize(void)
1247 if(!InitVirtualVideo())
1249 memset(&FSettings,0,sizeof(FSettings));
1250 FSettings.UsrFirstSLine[0]=8;
1251 FSettings.UsrFirstSLine[1]=0;
1252 FSettings.UsrLastSLine[0]=FSettings.UsrLastSLine[1]=239;
1253 FSettings.SoundVolume=100;
1255 FCEUI_Initialize098();
1256 FCEUI_SetEmuMode(0);
1261 void FCEUI_Kill(void)
1266 static void EmLoop(void);
1269 void (*ResetNES)(void) = 0;
1270 void (*PowerNES)(void) = 0;
1271 void (*FCEUI_Emulate)(void) = 0;
1273 void FCEUI_SetEmuMode(int is_new)
1275 use098code = is_new;
1278 ResetNES=ResetNES098;
1279 PowerNES=PowerNES098;
1280 FCEUI_Emulate=FCEUI_Emulate098;
1284 ResetNES=ResetNES081;
1285 PowerNES=PowerNES081;
1286 FCEUI_Emulate=EmLoop;
1290 void MMC5_hb(int); /* Ugh ugh ugh. */
1291 static void DoLine(void)
1293 uint8 *target=XBuf+scanline*320+32;
1297 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1301 // check: Battletoads & Double Dragon, Addams Family
1302 // sky glitches in SMB1 if done wrong
1308 if(scanline>=FSettings.FirstSLine && scanline<=FSettings.LastSLine)
1310 if(SpriteON && spork)
1311 CopySprites(target);
1313 LineUpdateEnd(target);
1317 if(ScreenON || SpriteON)
1321 if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
1327 X6502_Run(85-10-16);
1331 X6502_Run(6); // Tried 65, caused problems with Slalom(maybe others)
1338 if(GameHBIRQHook2 && (ScreenON || SpriteON))
1342 ResetRL(XBuf+scanline*320+32);
1347 static void EmLoop(void)
1352 uint32 scanlines_per_frame = PAL ? 312 : 262;
1354 FCEU_ApplyPeriodicCheats();
1357 if(ppudead) /* Needed for Knight Rider, possibly others. */
1359 //memset(XBuf, 0, 320*240);
1360 //X6502_Run(scanlines_per_frame*(256+85));
1362 for (lines=scanlines_per_frame;lines;lines--)
1371 PPU[3]=PPUSPL=0; /* Not sure if this is correct. According
1372 to Matt Conte and my own tests, it is. Timing is probably
1373 off, though. NOTE: Not having this here
1374 breaks a Super Donkey Kong game. */
1376 X6502_Run(12); /* I need to figure out the true nature and length
1379 if(FCEUGameInfo.type==GIT_NSF)
1384 // Note: this is needed for asm core
1385 // Warning: using 'scanline' var here breaks Castlevania III
1388 X6502_Run(256+85-12);
1389 for (lines=scanlines_per_frame-242-1;lines;lines--)
1392 // X6502_Run((scanlines_per_frame-242)*(256+85)-12);
1397 if(ScreenON || SpriteON)
1402 for(x=0;x<42;x++) {PPU_hook(0x2000); PPU_hook(0);} // ugh
1409 if(ScreenON || SpriteON)
1411 RefreshAddr=TempAddr;
1412 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
1421 if(FCEUGameInfo.type==GIT_NSF)
1423 // run scanlines for asm core to fuction
1424 for(scanline=0;scanline<240;scanline++)
1433 for(scanline=0;scanline<240;) // scanline is incremented in DoLine. Evil. :/
1438 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1439 for(x=1,max=0,maxref=0;x<7;x++)
1448 SetNESDeemph(maxref,0);
1461 ssize=FlushEmulateSound();
1463 timestampbase += timestamp;
1469 FCEU_PutImageDummy();
1471 FCEUD_Update(0,WaveFinalMono,ssize);
1477 FCEUD_Update(XBuf+8,WaveFinalMono,ssize);
1484 void FCEUI_CloseGame(void)
1489 static void ResetPPU(void)
1491 VRAMBuffer=PPU[0]=PPU[1]=PPU[2]=PPU[3]=0;
1494 RefreshAddr=TempAddr=0;
1500 static void PowerPPU(void)
1502 memset(NTARAM,0x00,0x800);
1503 memset(PALRAM,0x00,0x20);
1504 memset(SPRAM,0x00,0x100);
1508 void ResetNES081(void)
1510 if(!GameLoaded) return;
1511 GameInterface(GI_RESETM2, 0);
1517 static void FCEU_MemoryRand(uint8 *ptr, uint32 size)
1522 *ptr=(x&4)?0xFF:0x00;
1529 void PowerNES081(void)
1531 if(!GameLoaded) return;
1533 FCEU_CheatResetRAM();
1534 FCEU_CheatAddRAM(2,0,RAM);
1538 #ifndef DEBUG_ASM_6502
1539 FCEU_MemoryRand(RAM,0x800);
1541 memset(RAM,0x00,0x800);
1546 GameInterface(GI_POWER, 0);
1547 if(FCEUGameInfo.type==GIT_VSUNI)
1559 /* savestate stuff */
1560 uint16 TempAddrT,RefreshAddrT;
1562 SFORMAT FCEUPPU_STATEINFO[]={
1563 { NTARAM, 0x800, "NTAR"},
1564 { PALRAM, 0x20, "PRAM"},
1565 { SPRAM, 0x100, "SPRA"},
1566 { PPU, 0x4, "PPUR"},
1567 { &XOffset, 1, "XOFF"},
1568 { &vtoggle, 1, "VTOG"},
1569 { &RefreshAddrT, 2|RLSB, "RADD"},
1570 { &TempAddrT, 2|RLSB, "TADD"},
1571 { &VRAMBuffer, 1, "VBUF"},
1572 { &PPUGenLatch, 1, "PGEN"},
1574 { &kook, 1, "KOOK"},
1575 { &ppudead, 1, "DEAD"},
1576 { &PPUSPL, 1, "PSPL"},