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
58 #include "drivers/gp2x/asmutils.h"
64 static void (*RefreshLine)(uint8 *P, uint32 vofs) = NULL;
65 static void PRefreshLine(void);
67 static void ResetPPU(void);
68 static void PowerPPU(void);
70 uint64 timestampbase=0;
76 uint32 MMC5HackVROMMask;
77 uint8 *MMC5HackExNTARAMPtr;
78 uint8 *MMC5HackVROMPTR;
79 uint8 MMC5HackCHRMode=0;
81 uint8 MMC5HackSPScroll;
84 uint8 *MMC5SPRVPage[8];
85 uint8 *MMC5BGVPage[8];
88 uint8 VRAMBuffer,PPUGenLatch;
94 /* Color deemphasis emulation. Joy... */
96 static int deempcnt[8];
99 void (*GameInterface)(int h, void *param);
101 void FP_FASTAPASS(1) (*PPU_hook)(uint32 A);
103 void (*GameStateRestore)(int version);
104 void (*GameHBIRQHook)(void), (*GameHBIRQHook2)(void);
106 readfunc ARead[0x10000];
107 writefunc BWrite[0x10000];
108 static readfunc *AReadG;
109 static writefunc *BWriteG;
113 static void asmcpu_update(int32 cycles)
115 // some code from x6502.c
129 if((PSG[0x10]&0x80) && !(PSG[0x10]&0x40))
131 extern uint8 SIRQStat;
133 X6502_IRQBegin(FCEU_IQDPCM);
139 void asmcpu_unpack(void)
141 nes_registers[0] = X.A << 24;
142 nes_registers[1] = X.X;
143 nes_registers[2] = X.Y;
145 nes_registers[3] = X.PC;
147 nes_registers[4] = X.S << 24;
148 nes_registers[4]|= X.IRQlow << 8;
150 nes_registers[4] |= 1<<16; // MapIRQHook set bit
151 nes_registers[7] = (uint32)X.count << 16;
154 nes_registers[4]|= X.P & 0x5d;
155 nes_registers[5] = X.P << 24; // N
156 if (!(X.P&0x02)) nes_registers[5] |= 1; // Z
159 void asmcpu_pack(void)
161 X.A = nes_registers[0] >> 24;
162 X.X = nes_registers[1];
163 X.Y = nes_registers[2];
164 X.PC= nes_registers[3] - pc_base;
165 X.S = nes_registers[4] >> 24;
166 X.IRQlow = nes_registers[4] >> 8;
167 X.count = (int32) nes_registers[7] >> 16;
170 X.P = nes_registers[4] & 0x5d;
171 if ( nes_registers[5]&0x80000000) X.P |= 0x80; // N
172 if (!(nes_registers[5]&0x000000ff)) X.P |= 0x02; // Z
186 int AllocGenieRW(void)
188 if(!(AReadG=FCEU_malloc(0x8000*sizeof(readfunc))))
190 if(!(BWriteG=FCEU_malloc(0x8000*sizeof(writefunc))))
196 void FlushGenieRW(void)
202 for(x=0;x<0x8000;x++)
204 ARead[x+0x8000]=AReadG[x];
205 BWrite[x+0x8000]=BWriteG[x];
218 readfunc FASTAPASS(1) GetReadHandler(int32 a)
220 if(a>=0x8000 && RWWrap)
221 return AReadG[a-0x8000];
226 void FASTAPASS(3) SetReadHandler(int32 start, int32 end, readfunc func)
234 for(x=end;x>=start;x--)
237 AReadG[x-0x8000]=func;
243 for(x=end;x>=start;x--)
247 writefunc FASTAPASS(1) GetWriteHandler(int32 a)
249 if(RWWrap && a>=0x8000)
250 return BWriteG[a-0x8000];
255 void FASTAPASS(3) SetWriteHandler(int32 start, int32 end, writefunc func)
263 for(x=end;x>=start;x--)
266 BWriteG[x-0x8000]=func;
271 for(x=end;x>=start;x--)
278 uint32 TempAddr,RefreshAddr;
281 /* scanline is equal to the current visible scanline we're on. */
285 uint8 GameMemBlock[131072] __attribute__ ((aligned (4)));
286 uint8 NTARAM[0x800] __attribute__ ((aligned (4)));
287 uint8 PALRAM[0x20] __attribute__ ((aligned (4)));
288 #if !defined(ASM_6502) || defined(DEBUG_ASM_6502)
289 uint8 RAM[0x800] __attribute__ ((aligned (4)));
298 #define MMC5BGVRAMADR(V) &MMC5BGVPage[(V)>>10][(V)]
299 #define VRAMADR(V) &VPage[(V)>>10][(V)]
301 static int linestartts;
304 static uint8 *Plinef;
306 extern uint8 sprlinebuf[256+8];
308 extern uint8 sphitdata;
310 extern int spork; /* spork the world. Any sprites on this line?
311 Then this will be set to 1. Needed for zapper
312 emulation and *gasp* sprite emulation.
315 static void ResetRL(uint8 *target)
317 if(InputScanlineHook)
318 InputScanlineHook(0,0,0,0);
320 linestartts=timestamp*48+X6502_GetCycleCount();
324 static INLINE void Fixit1(void);
326 /* faking FCEUPPU_LineUpdate() from later versions of the emu */
327 static void FakedLineUpdate(void)
329 #define TOFIXNUM (272-0x4)
332 if (scanline >= 240) return;
334 if (tofix || sphitx != 0x100)
336 lastpixel = (timestamp*48-linestartts)>>4;
337 if (PAL) lastpixel += lastpixel>>4;
338 //printf("lastpixel: %i\n", lastpixel);
341 if (tofix && lastpixel>=TOFIXNUM)
353 for(x=sphitx;x<(sphitx+8) && x<l;x++)
355 if((sphitdata&(0x80>>(x-sphitx))) && !(Plinef[x]&64))
394 ret|=PPUGenLatch&0x1F;
398 //dprintf("r [2002] %02x",ret);
413 uint32 tmp=RefreshAddr&0x3FFF;
419 if(PPU_hook) PPU_hook(tmp);
420 PPUGenLatch=VRAMBuffer;
423 VRAMBuffer=VPage[tmp>>10][tmp];
427 VRAMBuffer=vnapage[(tmp>>10)&0x3][tmp&0x3FF];
430 if (INC32) RefreshAddr+=32;
432 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
433 dprintf("r [2007] %02x",ret);
455 //printf("$%04x:$%02x, %d\n",X.PC,V,scanline);
483 //printf("$%02x:$%02x\n",PPUSPL,V);
523 TempAddr|=(V&0x3f)<<8;
530 RefreshAddr=TempAddr;
532 PPU_hook(RefreshAddr);
540 uint32 tmp=RefreshAddr&0x3FFF;
546 PALRAM[0x00]=PALRAM[0x04]=PALRAM[0x08]=PALRAM[0x0C]=V&0x3f;
547 else if(tmp&3) PALRAM[(tmp&0x1f)]=V&0x3f;
551 if(PPUCHRRAM&(1<<(tmp>>10)))
552 VPage[tmp>>10][tmp]=V;
556 if(PPUNTARAM&(1<<((tmp&0xF00)>>10)))
557 vnapage[((tmp&0xF00)>>10)][tmp&0x3FF]=V;
559 if (INC32) RefreshAddr+=32;
561 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
570 B2004(0x2004,X.DB=ARead[t+x](t+x));
571 X6502_AddCycles(512);
574 void BGRender(uint8 *target)
577 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
583 RefreshLine(target-XOffset, vofs);
594 *(uint32 *)target=*(uint32 *)(target+4)=tem;
600 void FCEUI_FrameSkip(int x)
606 /* This is called at the beginning of each visible scanline */
607 static void LineUpdate(uint8 *target)
612 /* PRefreshLine() will not get called on skipped frames. This
613 * could cause a problem, but the solution would be rather complex,
614 * due to the current sprite 0 hit code.
619 if(scanline==y && SpriteON) PPU_status|=0x40; // hack
623 if(scanline < FSettings.FirstSLine || scanline > FSettings.LastSLine)
628 if(scanline==y && SpriteON) PPU_status|=0x40;
641 FCEU_dwmemset(target,tem,256);
645 if(InputScanlineHook)
646 InputScanlineHook(target,spork?sprlinebuf:0,linestartts,256);
650 static void LineUpdateEnd(uint8 *target)
653 if(ScreenON || SpriteON) // Yes, very el-cheapo.
656 block_and(target, 256, 0x30);
659 block_or(target, 256, 0xc0);
661 block_or(target, 256, 0x40);
663 block_andor(target, 256, 0x3f, 0x80);
667 if(ScreenON || SpriteON) // Yes, very el-cheapo.
672 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0x30303030;
678 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0xc0c0c0c0;
682 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])|0x40404040;
685 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0x80808080;
689 ((uint32 *)target)[-2]=((uint32 *)target)[-1]=0;
690 ((uint32 *)target)[64]=((uint32 *)target)[65]=0;
693 #define PAL(c) ((c)+cc)
696 static void PRefreshLine(void)
700 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
701 void (*PPU_hook_)(uint32 A) = PPU_hook;
709 zz2=(RefreshAddr>>10)&3;
710 PPU_hook_(0x2000|(RefreshAddr&0xFFF));
712 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
716 if((RefreshAddr&0x1f)==0x1f)
723 /* This high-level graphics MMC5 emulation code was written
724 for MMC5 carts in "CL" mode. It's probably not totally
725 correct for carts in "SL" mode.
727 static void RefreshLine_MMC5Hack1(uint8 *P, uint32 vofs)
731 tochange=MMC5HackSPMode&0x1F;
733 for(X1=33;X1;X1--,P+=8)
739 if((tochange<=0 && MMC5HackSPMode&0x40) ||
740 (tochange>0 && !(MMC5HackSPMode&0x40)))
745 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
746 if(ys>=0x1E) ys-=0x1E;
747 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
749 C = MMC5HackVROMPTR+vadr;
750 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
752 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
753 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
758 zz2=(RefreshAddr>>10)&3;
759 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
760 C = MMC5BGVRAMADR(vadr);
761 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
762 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
766 if((RefreshAddr&0x1f)==0x1f)
774 static void RefreshLine_MMC5Hack2(uint8 *P, uint32 vofs)
778 tochange=MMC5HackSPMode&0x1F;
780 for(X1=33;X1;X1--,P+=8)
787 if((tochange<=0 && MMC5HackSPMode&0x40) ||
788 (tochange>0 && !(MMC5HackSPMode&0x40)))
793 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
794 if(ys>=0x1E) ys-=0x1E;
795 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
797 C = MMC5HackVROMPTR+vadr;
798 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
800 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
801 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
806 zz2=(RefreshAddr>>10)&3;
807 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
808 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
809 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
810 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
814 if((RefreshAddr&0x1f)==0x1f)
822 static void RefreshLine_MMC5Hack3(uint8 *P, uint32 vofs)
826 for(X1=33;X1;X1--,P+=8)
834 zz2=(RefreshAddr>>10)&3;
835 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
836 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
837 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
838 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
842 if((RefreshAddr&0x1f)==0x1f)
849 static void RefreshLine_MMC5Hack4(uint8 *P, uint32 vofs)
853 for(X1=33;X1;X1--,P+=8)
860 zz2=(RefreshAddr>>10)&3;
861 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
862 C = MMC5BGVRAMADR(vadr);
863 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
864 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
868 if((RefreshAddr&0x1f)==0x1f)
875 static void RefreshLine_PPU_hook(uint8 *P, uint32 vofs)
878 void (*PPU_hook_)(uint32 A) = PPU_hook;
879 uint32 rfraddr = RefreshAddr;
880 uint8 *page = vnapage[(rfraddr>>10)&3];
882 for(X1=33;X1;X1--,P+=8)
889 PPU_hook_(0x2000|(rfraddr&0xFFF));
890 cc=page[0x3c0+(zz>>2)+((rfraddr&0x380)>>4)];
891 cc=((cc >> ((zz&2) + ((rfraddr&0x40)>>4))) &3) <<2;
892 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
899 if((rfraddr&0x1f)==0x1f) {
901 page = vnapage[(rfraddr>>10)&3];
905 RefreshAddr = rfraddr;
908 static void RefreshLine_normal(uint8 *P, uint32 vofs) // vofs is 0x107 max
911 uint32 rfraddr = RefreshAddr;
912 uint8 *page = vnapage[(rfraddr>>10)&3];
915 if ((rfraddr&0xc)!=0)
916 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
918 for (X1=33;X1;X1--,P+=8)
923 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
925 if ((rfraddr&0xc)==0)
926 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
927 cc=((cc2 >> ((rfraddr&2) + ((rfraddr&0x40)>>4) + ((rfraddr&0xc)<<1))) & 3) << 2;
931 if((rfraddr&0x1f)==0x1f) {
933 page = vnapage[(rfraddr>>10)&3];
937 RefreshAddr = rfraddr;
940 static void SetRefreshLine(void)
942 if(MMC5Hack && geniestage!=1)
944 if(MMC5HackCHRMode==0 && (MMC5HackSPMode&0x80))
946 if (RefreshLine != RefreshLine_MMC5Hack1) printf("set refr RefreshLine_MMC5Hack1\n");
947 RefreshLine = RefreshLine_MMC5Hack1;
949 else if(MMC5HackCHRMode==1 && (MMC5HackSPMode&0x80))
951 if (RefreshLine != RefreshLine_MMC5Hack2) printf("set refr RefreshLine_MMC5Hack2\n");
952 RefreshLine = RefreshLine_MMC5Hack2;
954 else if(MMC5HackCHRMode==1)
956 if (RefreshLine != RefreshLine_MMC5Hack3) printf("set refr RefreshLine_MMC5Hack3\n");
957 RefreshLine = RefreshLine_MMC5Hack3;
961 if (RefreshLine != RefreshLine_MMC5Hack4) printf("set refr RefreshLine_MMC5Hack4\n");
962 RefreshLine = RefreshLine_MMC5Hack4;
964 } // End if(MMC5Hack)
967 if (RefreshLine != RefreshLine_PPU_hook) printf("set refr RefreshLine_PPU_hook\n");
968 RefreshLine = RefreshLine_PPU_hook;
972 if (RefreshLine != RefreshLine_normal) printf("set refr RefreshLine_normal\n");
973 RefreshLine = RefreshLine_normal;
980 if(ScreenON || SpriteON)
982 uint32 rad=RefreshAddr;
984 rad|=TempAddr&0x041f;
986 //PPU_hook(RefreshAddr,-1);
993 if(ScreenON || SpriteON)
995 uint32 rad=RefreshAddr;
997 if((rad&0x7000)==0x7000)
1000 if((rad&0x3E0)==0x3A0)
1007 if((rad&0x3E0)==0x3e0)
1015 //PPU_hook(RefreshAddr,-1);
1020 // ============================//
1022 // ===========================//
1024 void ResetMapping(void)
1028 SetReadHandler(0x0000,0xFFFF,ANull);
1029 SetWriteHandler(0x0000,0xFFFF,BNull);
1031 SetReadHandler(0,0x7FF,ARAML);
1032 SetWriteHandler(0,0x7FF,BRAML);
1034 SetReadHandler(0x800,0x1FFF,ARAMH); /* Part of a little */
1035 SetWriteHandler(0x800,0x1FFF,BRAMH); /* hack for a small speed boost. */
1037 for(x=0x2000;x<0x4000;x+=8)
1057 BWrite[0x4014]=B4014;
1063 void CloseGame(void)
1068 if(FCEUGameInfo.type!=GIT_NSF)
1069 FCEU_FlushGameCheats(0,0);
1071 if(FSettings.NetworkPlay) KillNetplay();
1073 GameInterface(GI_CLOSE, 0);
1079 void ResetGameLoaded(void)
1081 if(GameLoaded) CloseGame();
1084 GameHBIRQHook=GameHBIRQHook2=0;
1085 GameExpSound.Fill=0;
1086 GameExpSound.RChange=0;
1087 if(GameExpSound.Kill)
1088 GameExpSound.Kill();
1089 GameExpSound.Kill=0;
1095 FCEUGameInfo.name=0;
1096 FCEUGameInfo.type=GIT_CART;
1097 FCEUGameInfo.vidsys=GIV_USER;
1098 FCEUGameInfo.input[0]=FCEUGameInfo.input[1]=-1;
1099 FCEUGameInfo.inputfc=-1;
1102 char lastLoadedGameName [2048];
1103 int LoadGameLastError = 0;
1104 int UNIFLoad(const char *name, int fp);
1105 int iNESLoad(const char *name, int fp);
1106 int FDSLoad(const char *name, int fp);
1107 int NSFLoad(int fp);
1109 FCEUGI *FCEUI_LoadGame(char *name)
1112 int have_movie = 0, have_ips = 0;
1116 LoadGameLastError = 0;
1119 strncpy(name2, name, sizeof(name2));
1120 name2[sizeof(name2)-1] = 0;
1122 fp=FCEU_fopen(name2,"rb");
1125 FCEU_PrintError("Error opening \"%s\"!",name);
1126 LoadGameLastError = 1;
1131 char *p = name2 + strlen(name2) - 4;
1132 if (strcasecmp(p, ".fcm") == 0) printf("movie detected\n"), have_movie = 1;
1133 if (strcasecmp(p, ".ips") == 0) printf("ips detected\n"), have_ips = 1;
1134 if (have_movie || have_ips)
1139 fp=FCEU_fopen(name2,"rb");
1140 if (!fp && p - name2 > 2)
1142 for (p--; p > name2 && *p != '.'; p--);
1144 fp=FCEU_fopen(name2,"rb");
1147 printf("no ROM for ips/movie\n");
1148 LoadGameLastError = 2;
1158 FILE *ips = fopen(name, "rb");
1160 fp=FCEU_fopen_forcemem(name2);
1161 if (!fp) { fclose(ips); return 0; }
1162 ApplyIPS(ips, fp); // closes ips
1166 if(iNESLoad(name2,fp))
1170 if(FDSLoad(name2,fp))
1172 if(UNIFLoad(name2,fp))
1175 FCEU_PrintError("An error occurred while loading the file.");
1177 // format handlers may set LoadGameLastError themselves.
1178 if (LoadGameLastError == 0) LoadGameLastError = 3;
1186 if(FCEUGameInfo.type!=GIT_NSF)
1187 if(FSettings.GameGenie)
1192 if(FSettings.NetworkPlay) InitNetplay();
1195 if(FCEUGameInfo.type!=GIT_NSF)
1197 FCEU_LoadGamePalette();
1198 FCEU_LoadGameCheats(0);
1201 FCEU_ResetPalette();
1205 FCEUI_LoadMovie(name, 1);
1207 strcpy(lastLoadedGameName, name2);
1209 return(&FCEUGameInfo);
1213 void FCEU_ResetVidSys(void)
1217 if(FCEUGameInfo.vidsys==GIV_NTSC)
1219 else if(FCEUGameInfo.vidsys==GIV_PAL)
1227 FSettings.FirstSLine=FSettings.UsrFirstSLine[1];
1228 FSettings.LastSLine=FSettings.UsrLastSLine[1];
1233 FSettings.FirstSLine=FSettings.UsrFirstSLine[0];
1234 FSettings.LastSLine=FSettings.UsrLastSLine[0];
1236 printf("ResetVidSys: PAL = %i\n", PAL);
1237 SetSoundVariables();
1240 int FCEUI_Initialize(void)
1242 if(!InitVirtualVideo())
1244 memset(&FSettings,0,sizeof(FSettings));
1245 FSettings.UsrFirstSLine[0]=8;
1246 FSettings.UsrFirstSLine[1]=0;
1247 FSettings.UsrLastSLine[0]=FSettings.UsrLastSLine[1]=239;
1248 FSettings.SoundVolume=100;
1252 void MMC5_hb(int); /* Ugh ugh ugh. */
1253 static void DoLine(void)
1255 uint8 *target=XBuf+scanline*320+32;
1259 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1263 // check: Battletoads & Double Dragon, Addams Family
1264 // sky glitches in SMB1 if done wrong
1270 if(scanline>=FSettings.FirstSLine && scanline<=FSettings.LastSLine)
1272 if(SpriteON && spork)
1273 CopySprites(target);
1275 LineUpdateEnd(target);
1279 if(ScreenON || SpriteON)
1283 if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
1289 X6502_Run(85-10-16);
1293 X6502_Run(6); // Tried 65, caused problems with Slalom(maybe others)
1300 if(GameHBIRQHook2 && (ScreenON || SpriteON))
1304 ResetRL(XBuf+scanline*320+32);
1314 uint32 scanlines_per_frame = PAL ? 312 : 262;
1316 FCEU_ApplyPeriodicCheats();
1319 if(ppudead) /* Needed for Knight Rider, possibly others. */
1321 //memset(XBuf, 0, 320*240);
1322 X6502_Run(scanlines_per_frame*(256+85));
1330 PPU[3]=PPUSPL=0; /* Not sure if this is correct. According
1331 to Matt Conte and my own tests, it is. Timing is probably
1332 off, though. NOTE: Not having this here
1333 breaks a Super Donkey Kong game. */
1335 X6502_Run(12); /* I need to figure out the true nature and length
1338 if(FCEUGameInfo.type==GIT_NSF)
1343 // Note: this is needed for asm core
1344 // Warning: using 'scanline' var here breaks Castlevania III
1347 X6502_Run(256+85-12);
1348 for (lines=scanlines_per_frame-242-1;lines;lines--)
1351 // X6502_Run((scanlines_per_frame-242)*(256+85)-12);
1356 if(ScreenON || SpriteON)
1361 for(x=0;x<42;x++) {PPU_hook(0x2000); PPU_hook(0);} // ugh
1368 if(ScreenON || SpriteON)
1370 RefreshAddr=TempAddr;
1371 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
1380 if(FCEUGameInfo.type==GIT_NSF)
1382 // run scanlines for asm core to fuction
1383 for(scanline=0;scanline<240;scanline++)
1392 for(scanline=0;scanline<240;) // scanline is incremented in DoLine. Evil. :/
1397 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1398 for(x=1,max=0,maxref=0;x<7;x++)
1407 SetNESDeemph(maxref,0);
1420 ssize=FlushEmulateSound();
1422 timestampbase += timestamp;
1428 FCEU_PutImageDummy();
1430 FCEUD_Update(0,WaveFinalMono,ssize);
1436 FCEUD_Update(XBuf+8,WaveFinalMono,ssize);
1444 #include <sys/time.h>
1447 void FCEUI_Emulate(void)
1450 uint64 starttime,end;
1453 gettimeofday(&tv,0);
1454 starttime=((uint64)tv.tv_sec*1000000)+tv.tv_usec;
1459 // Probably won't work well on Windows port; for
1460 // debugging/speed testing.
1464 gettimeofday(&tv,0);
1465 end=((uint64)tv.tv_sec*1000000)+tv.tv_usec;
1466 w=frcount*10000000000LL/(end-starttime);
1469 printf("Average FPS: %d.%04d\n",i,frac);
1475 void FCEUI_CloseGame(void)
1480 static void ResetPPU(void)
1482 VRAMBuffer=PPU[0]=PPU[1]=PPU[2]=PPU[3]=0;
1485 RefreshAddr=TempAddr=0;
1491 static void PowerPPU(void)
1493 memset(NTARAM,0x00,0x800);
1494 memset(PALRAM,0x00,0x20);
1495 memset(SPRAM,0x00,0x100);
1501 if(!GameLoaded) return;
1502 GameInterface(GI_RESETM2, 0);
1508 static void FCEU_MemoryRand(uint8 *ptr, uint32 size)
1513 *ptr=(x&4)?0xFF:0x00;
1522 if(!GameLoaded) return;
1524 FCEU_CheatResetRAM();
1525 FCEU_CheatAddRAM(2,0,RAM);
1529 #ifndef DEBUG_ASM_6502
1530 FCEU_MemoryRand(RAM,0x800);
1532 memset(RAM,0x00,0x800);
1537 GameInterface(GI_POWER, 0);
1538 if(FCEUGameInfo.type==GIT_VSUNI)
1550 /* savestate stuff */
1551 uint16 TempAddrT,RefreshAddrT;
1553 SFORMAT FCEUPPU_STATEINFO[]={
1554 { NTARAM, 0x800, "NTAR"},
1555 { PALRAM, 0x20, "PRAM"},
1556 { SPRAM, 0x100, "SPRA"},
1557 { PPU, 0x4, "PPUR"},
1558 { &XOffset, 1, "XOFF"},
1559 { &vtoggle, 1, "VTOG"},
1560 { &RefreshAddrT, 2|RLSB, "RADD"},
1561 { &TempAddrT, 2|RLSB, "TADD"},
1562 { &VRAMBuffer, 1, "VBUF"},
1563 { &PPUGenLatch, 1, "PGEN"},
1565 { &kook, 1, "KOOK"},
1566 { &ppudead, 1, "DEAD"},
1567 { &PPUSPL, 1, "PSPL"},