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
57 #include "drivers/gp2x/asmutils.h"
63 static void (*RefreshLine)(uint8 *P, uint32 vofs) = NULL;
64 static void PRefreshLine(void);
66 static void ResetPPU(void);
67 static void PowerPPU(void);
69 uint64 timestampbase=0;
75 uint32 MMC5HackVROMMask;
76 uint8 *MMC5HackExNTARAMPtr;
77 uint8 *MMC5HackVROMPTR;
78 uint8 MMC5HackCHRMode=0;
80 uint8 MMC5HackSPScroll;
83 uint8 *MMC5SPRVPage[8];
84 uint8 *MMC5BGVPage[8];
87 uint8 VRAMBuffer,PPUGenLatch;
93 /* Color deemphasis emulation. Joy... */
95 static int deempcnt[8];
98 void (*GameInterface)(int h, void *param);
100 void FP_FASTAPASS(1) (*PPU_hook)(uint32 A);
102 void (*GameStateRestore)(int version);
103 void (*GameHBIRQHook)(void), (*GameHBIRQHook2)(void);
105 readfunc ARead[0x10000];
106 writefunc BWrite[0x10000];
107 static readfunc *AReadG;
108 static writefunc *BWriteG;
112 static void asmcpu_update(int32 cycles)
114 // some code from x6502.c
128 if((PSG[0x10]&0x80) && !(PSG[0x10]&0x40))
130 extern uint8 SIRQStat;
132 X6502_IRQBegin(FCEU_IQDPCM);
138 void asmcpu_unpack(void)
140 nes_registers[0] = X.A << 24;
141 nes_registers[1] = X.X;
142 nes_registers[2] = X.Y;
144 nes_registers[3] = X.PC;
146 nes_registers[4] = X.S << 24;
147 nes_registers[4]|= X.IRQlow << 8;
149 nes_registers[4] |= 1<<16; // MapIRQHook set bit
150 nes_registers[7] = (uint32)X.count << 16;
153 nes_registers[4]|= X.P & 0x5d;
154 nes_registers[5] = X.P << 24; // N
155 if (!(X.P&0x02)) nes_registers[5] |= 1; // Z
158 void asmcpu_pack(void)
160 X.A = nes_registers[0] >> 24;
161 X.X = nes_registers[1];
162 X.Y = nes_registers[2];
163 X.PC= nes_registers[3] - pc_base;
164 X.S = nes_registers[4] >> 24;
165 X.IRQlow = nes_registers[4] >> 8;
166 X.count = (int32) nes_registers[7] >> 16;
169 X.P = nes_registers[4] & 0x5d;
170 if ( nes_registers[5]&0x80000000) X.P |= 0x80; // N
171 if (!(nes_registers[5]&0x000000ff)) X.P |= 0x02; // Z
185 int AllocGenieRW(void)
187 if(!(AReadG=FCEU_malloc(0x8000*sizeof(readfunc))))
189 if(!(BWriteG=FCEU_malloc(0x8000*sizeof(writefunc))))
195 void FlushGenieRW(void)
201 for(x=0;x<0x8000;x++)
203 ARead[x+0x8000]=AReadG[x];
204 BWrite[x+0x8000]=BWriteG[x];
217 readfunc FASTAPASS(1) GetReadHandler(int32 a)
219 if(a>=0x8000 && RWWrap)
220 return AReadG[a-0x8000];
225 void FASTAPASS(3) SetReadHandler(int32 start, int32 end, readfunc func)
233 for(x=end;x>=start;x--)
236 AReadG[x-0x8000]=func;
242 for(x=end;x>=start;x--)
246 writefunc FASTAPASS(1) GetWriteHandler(int32 a)
248 if(RWWrap && a>=0x8000)
249 return BWriteG[a-0x8000];
254 void FASTAPASS(3) SetWriteHandler(int32 start, int32 end, writefunc func)
262 for(x=end;x>=start;x--)
265 BWriteG[x-0x8000]=func;
270 for(x=end;x>=start;x--)
277 uint32 TempAddr,RefreshAddr;
280 /* scanline is equal to the current visible scanline we're on. */
284 uint8 GameMemBlock[131072] __attribute__ ((aligned (4)));
285 uint8 NTARAM[0x800] __attribute__ ((aligned (4)));
286 uint8 PALRAM[0x20] __attribute__ ((aligned (4)));
287 #if !defined(ASM_6502) || defined(DEBUG_ASM_6502)
288 uint8 RAM[0x800] __attribute__ ((aligned (4)));
297 #define MMC5BGVRAMADR(V) &MMC5BGVPage[(V)>>10][(V)]
298 #define VRAMADR(V) &VPage[(V)>>10][(V)]
300 static int linestartts;
303 static uint8 *Plinef;
305 extern uint8 sprlinebuf[256+8];
307 extern uint8 sphitdata;
309 extern int spork; /* spork the world. Any sprites on this line?
310 Then this will be set to 1. Needed for zapper
311 emulation and *gasp* sprite emulation.
314 static void ResetRL(uint8 *target)
316 if(InputScanlineHook)
317 InputScanlineHook(0,0,0,0);
319 linestartts=timestamp*48+X6502_GetCycleCount();
323 static INLINE void Fixit1(void);
325 /* faking FCEUPPU_LineUpdate() from later versions of the emu */
326 static void FakedLineUpdate(void)
328 #define TOFIXNUM (272-0x4)
331 if (scanline >= 240) return;
333 if (tofix || sphitx != 0x100)
335 lastpixel = (timestamp*48-linestartts)>>4;
336 if (PAL) lastpixel += lastpixel>>4;
337 //printf("lastpixel: %i\n", lastpixel);
340 if (tofix && lastpixel>=TOFIXNUM)
352 for(x=sphitx;x<(sphitx+8) && x<l;x++)
354 if((sphitdata&(0x80>>(x-sphitx))) && !(Plinef[x]&64))
393 ret|=PPUGenLatch&0x1F;
397 //dprintf("r [2002] %02x",ret);
412 uint32 tmp=RefreshAddr&0x3FFF;
418 if(PPU_hook) PPU_hook(tmp);
419 PPUGenLatch=VRAMBuffer;
422 VRAMBuffer=VPage[tmp>>10][tmp];
426 VRAMBuffer=vnapage[(tmp>>10)&0x3][tmp&0x3FF];
429 if (INC32) RefreshAddr+=32;
431 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
432 dprintf("r [2007] %02x",ret);
454 //printf("$%04x:$%02x, %d\n",X.PC,V,scanline);
482 //printf("$%02x:$%02x\n",PPUSPL,V);
522 TempAddr|=(V&0x3f)<<8;
529 RefreshAddr=TempAddr;
531 PPU_hook(RefreshAddr);
539 uint32 tmp=RefreshAddr&0x3FFF;
545 PALRAM[0x00]=PALRAM[0x04]=PALRAM[0x08]=PALRAM[0x0C]=V&0x3f;
546 else if(tmp&3) PALRAM[(tmp&0x1f)]=V&0x3f;
550 if(PPUCHRRAM&(1<<(tmp>>10)))
551 VPage[tmp>>10][tmp]=V;
555 if(PPUNTARAM&(1<<((tmp&0xF00)>>10)))
556 vnapage[((tmp&0xF00)>>10)][tmp&0x3FF]=V;
558 if (INC32) RefreshAddr+=32;
560 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
569 B2004(0x2004,X.DB=ARead[t+x](t+x));
570 X6502_AddCycles(512);
573 void BGRender(uint8 *target)
576 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
582 RefreshLine(target-XOffset, vofs);
593 *(uint32 *)target=*(uint32 *)(target+4)=tem;
599 void FCEUI_FrameSkip(int x)
605 /* This is called at the beginning of each visible scanline */
606 static void LineUpdate(uint8 *target)
611 /* PRefreshLine() will not get called on skipped frames. This
612 * could cause a problem, but the solution would be rather complex,
613 * due to the current sprite 0 hit code.
618 if(scanline==y && SpriteON) PPU_status|=0x40; // hack
622 if(scanline < FSettings.FirstSLine || scanline > FSettings.LastSLine)
627 if(scanline==y && SpriteON) PPU_status|=0x40;
640 FCEU_dwmemset(target,tem,256);
644 if(InputScanlineHook)
645 InputScanlineHook(target,spork?sprlinebuf:0,linestartts,256);
649 static void LineUpdateEnd(uint8 *target)
652 if(ScreenON || SpriteON) // Yes, very el-cheapo.
655 block_and(target, 256, 0x30);
658 block_or(target, 256, 0xc0);
660 block_or(target, 256, 0x40);
662 block_andor(target, 256, 0x3f, 0x80);
666 if(ScreenON || SpriteON) // Yes, very el-cheapo.
671 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0x30303030;
677 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0xc0c0c0c0;
681 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])|0x40404040;
684 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0x80808080;
688 ((uint32 *)target)[-2]=((uint32 *)target)[-1]=0;
689 ((uint32 *)target)[64]=((uint32 *)target)[65]=0;
692 #define PAL(c) ((c)+cc)
695 static void PRefreshLine(void)
699 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
700 void (*PPU_hook_)(uint32 A) = PPU_hook;
708 zz2=(RefreshAddr>>10)&3;
709 PPU_hook_(0x2000|(RefreshAddr&0xFFF));
711 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
715 if((RefreshAddr&0x1f)==0x1f)
722 /* This high-level graphics MMC5 emulation code was written
723 for MMC5 carts in "CL" mode. It's probably not totally
724 correct for carts in "SL" mode.
726 static void RefreshLine_MMC5Hack1(uint8 *P, uint32 vofs)
730 tochange=MMC5HackSPMode&0x1F;
732 for(X1=33;X1;X1--,P+=8)
738 if((tochange<=0 && MMC5HackSPMode&0x40) ||
739 (tochange>0 && !(MMC5HackSPMode&0x40)))
744 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
745 if(ys>=0x1E) ys-=0x1E;
746 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
748 C = MMC5HackVROMPTR+vadr;
749 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
751 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
752 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
757 zz2=(RefreshAddr>>10)&3;
758 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
759 C = MMC5BGVRAMADR(vadr);
760 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
761 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
765 if((RefreshAddr&0x1f)==0x1f)
773 static void RefreshLine_MMC5Hack2(uint8 *P, uint32 vofs)
777 tochange=MMC5HackSPMode&0x1F;
779 for(X1=33;X1;X1--,P+=8)
786 if((tochange<=0 && MMC5HackSPMode&0x40) ||
787 (tochange>0 && !(MMC5HackSPMode&0x40)))
792 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
793 if(ys>=0x1E) ys-=0x1E;
794 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
796 C = MMC5HackVROMPTR+vadr;
797 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
799 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
800 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
805 zz2=(RefreshAddr>>10)&3;
806 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
807 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
808 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
809 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
813 if((RefreshAddr&0x1f)==0x1f)
821 static void RefreshLine_MMC5Hack3(uint8 *P, uint32 vofs)
825 for(X1=33;X1;X1--,P+=8)
833 zz2=(RefreshAddr>>10)&3;
834 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
835 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
836 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
837 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
841 if((RefreshAddr&0x1f)==0x1f)
848 static void RefreshLine_MMC5Hack4(uint8 *P, uint32 vofs)
852 for(X1=33;X1;X1--,P+=8)
859 zz2=(RefreshAddr>>10)&3;
860 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
861 C = MMC5BGVRAMADR(vadr);
862 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
863 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
867 if((RefreshAddr&0x1f)==0x1f)
874 static void RefreshLine_PPU_hook(uint8 *P, uint32 vofs)
877 void (*PPU_hook_)(uint32 A) = PPU_hook;
878 uint32 rfraddr = RefreshAddr;
879 uint8 *page = vnapage[(rfraddr>>10)&3];
881 for(X1=33;X1;X1--,P+=8)
888 PPU_hook_(0x2000|(rfraddr&0xFFF));
889 cc=page[0x3c0+(zz>>2)+((rfraddr&0x380)>>4)];
890 cc=((cc >> ((zz&2) + ((rfraddr&0x40)>>4))) &3) <<2;
891 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
898 if((rfraddr&0x1f)==0x1f) {
900 page = vnapage[(rfraddr>>10)&3];
904 RefreshAddr = rfraddr;
907 static void RefreshLine_normal(uint8 *P, uint32 vofs) // vofs is 0x107 max
910 uint32 rfraddr = RefreshAddr;
911 uint8 *page = vnapage[(rfraddr>>10)&3];
914 if ((rfraddr&0xc)!=0)
915 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
917 for (X1=33;X1;X1--,P+=8)
922 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
924 if ((rfraddr&0xc)==0)
925 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
926 cc=((cc2 >> ((rfraddr&2) + ((rfraddr&0x40)>>4) + ((rfraddr&0xc)<<1))) & 3) << 2;
930 if((rfraddr&0x1f)==0x1f) {
932 page = vnapage[(rfraddr>>10)&3];
936 RefreshAddr = rfraddr;
939 static void SetRefreshLine(void)
941 if(MMC5Hack && geniestage!=1)
943 if(MMC5HackCHRMode==0 && (MMC5HackSPMode&0x80))
945 if (RefreshLine != RefreshLine_MMC5Hack1) printf("set refr RefreshLine_MMC5Hack1\n");
946 RefreshLine = RefreshLine_MMC5Hack1;
948 else if(MMC5HackCHRMode==1 && (MMC5HackSPMode&0x80))
950 if (RefreshLine != RefreshLine_MMC5Hack2) printf("set refr RefreshLine_MMC5Hack2\n");
951 RefreshLine = RefreshLine_MMC5Hack2;
953 else if(MMC5HackCHRMode==1)
955 if (RefreshLine != RefreshLine_MMC5Hack3) printf("set refr RefreshLine_MMC5Hack3\n");
956 RefreshLine = RefreshLine_MMC5Hack3;
960 if (RefreshLine != RefreshLine_MMC5Hack4) printf("set refr RefreshLine_MMC5Hack4\n");
961 RefreshLine = RefreshLine_MMC5Hack4;
963 } // End if(MMC5Hack)
966 if (RefreshLine != RefreshLine_PPU_hook) printf("set refr RefreshLine_PPU_hook\n");
967 RefreshLine = RefreshLine_PPU_hook;
971 if (RefreshLine != RefreshLine_normal) printf("set refr RefreshLine_normal\n");
972 RefreshLine = RefreshLine_normal;
979 if(ScreenON || SpriteON)
981 uint32 rad=RefreshAddr;
983 rad|=TempAddr&0x041f;
985 //PPU_hook(RefreshAddr,-1);
992 if(ScreenON || SpriteON)
994 uint32 rad=RefreshAddr;
996 if((rad&0x7000)==0x7000)
999 if((rad&0x3E0)==0x3A0)
1006 if((rad&0x3E0)==0x3e0)
1014 //PPU_hook(RefreshAddr,-1);
1019 // ============================//
1021 // ===========================//
1023 void ResetMapping(void)
1027 SetReadHandler(0x0000,0xFFFF,ANull);
1028 SetWriteHandler(0x0000,0xFFFF,BNull);
1030 SetReadHandler(0,0x7FF,ARAML);
1031 SetWriteHandler(0,0x7FF,BRAML);
1033 SetReadHandler(0x800,0x1FFF,ARAMH); /* Part of a little */
1034 SetWriteHandler(0x800,0x1FFF,BRAMH); /* hack for a small speed boost. */
1036 for(x=0x2000;x<0x4000;x+=8)
1056 BWrite[0x4014]=B4014;
1062 void CloseGame(void)
1067 if(FCEUGameInfo.type!=GIT_NSF)
1068 FCEU_FlushGameCheats(0,0);
1070 if(FSettings.NetworkPlay) KillNetplay();
1072 GameInterface(GI_CLOSE, 0);
1078 void ResetGameLoaded(void)
1080 if(GameLoaded) CloseGame();
1083 GameHBIRQHook=GameHBIRQHook2=0;
1084 GameExpSound.Fill=0;
1085 GameExpSound.RChange=0;
1086 if(GameExpSound.Kill)
1087 GameExpSound.Kill();
1088 GameExpSound.Kill=0;
1094 FCEUGameInfo.name=0;
1095 FCEUGameInfo.type=GIT_CART;
1096 FCEUGameInfo.vidsys=GIV_USER;
1097 FCEUGameInfo.input[0]=FCEUGameInfo.input[1]=-1;
1098 FCEUGameInfo.inputfc=-1;
1101 char lastLoadedGameName [2048];
1102 int LoadGameLastError = 0;
1103 int UNIFLoad(const char *name, int fp);
1104 int iNESLoad(const char *name, int fp);
1105 int FDSLoad(const char *name, int fp);
1106 int NSFLoad(int fp);
1108 FCEUGI *FCEUI_LoadGame(char *name)
1115 LoadGameLastError = 0;
1118 strncpy(name2, name, sizeof(name2));
1119 name2[sizeof(name2)-1] = 0;
1121 fp=FCEU_fopen(name2,"rb");
1124 FCEU_PrintError("Error opening \"%s\"!",name);
1125 LoadGameLastError = 1;
1130 char *p = name2 + strlen(name2) - 4;
1131 if (strcmp(p, ".fcm") == 0)
1134 printf("movie detected\n");
1137 fp=FCEU_fopen(name2,"rb");
1138 if (!fp && p - name2 > 2) p[-2] = 0;
1139 fp=FCEU_fopen(name2,"rb");
1141 printf("no ROM for movie\n");
1142 LoadGameLastError = 2;
1150 if(iNESLoad(name2,fp))
1154 if(FDSLoad(name2,fp))
1156 if(UNIFLoad(name2,fp))
1159 FCEU_PrintError("An error occurred while loading the file.");
1161 // format handlers may set LoadGameLastError themselves.
1162 if (LoadGameLastError == 0) LoadGameLastError = 3;
1170 if(FCEUGameInfo.type!=GIT_NSF)
1171 if(FSettings.GameGenie)
1176 if(FSettings.NetworkPlay) InitNetplay();
1179 if(FCEUGameInfo.type!=GIT_NSF)
1181 FCEU_LoadGamePalette();
1182 FCEU_LoadGameCheats(0);
1185 FCEU_ResetPalette();
1189 FCEUI_LoadMovie(name, 1);
1191 strcpy(lastLoadedGameName, name2);
1193 return(&FCEUGameInfo);
1197 void FCEU_ResetVidSys(void)
1201 if(FCEUGameInfo.vidsys==GIV_NTSC)
1203 else if(FCEUGameInfo.vidsys==GIV_PAL)
1211 FSettings.FirstSLine=FSettings.UsrFirstSLine[1];
1212 FSettings.LastSLine=FSettings.UsrLastSLine[1];
1217 FSettings.FirstSLine=FSettings.UsrFirstSLine[0];
1218 FSettings.LastSLine=FSettings.UsrLastSLine[0];
1220 printf("ResetVidSys: PAL = %i\n", PAL);
1221 SetSoundVariables();
1224 int FCEUI_Initialize(void)
1226 if(!InitVirtualVideo())
1228 memset(&FSettings,0,sizeof(FSettings));
1229 FSettings.UsrFirstSLine[0]=8;
1230 FSettings.UsrFirstSLine[1]=0;
1231 FSettings.UsrLastSLine[0]=FSettings.UsrLastSLine[1]=239;
1232 FSettings.SoundVolume=100;
1236 void MMC5_hb(int); /* Ugh ugh ugh. */
1237 static void DoLine(void)
1239 uint8 *target=XBuf+scanline*320+32;
1243 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1247 // check: Battletoads & Double Dragon, Addams Family
1248 // sky glitches in SMB1 if done wrong
1254 if(scanline>=FSettings.FirstSLine && scanline<=FSettings.LastSLine)
1256 if(SpriteON && spork)
1257 CopySprites(target);
1259 LineUpdateEnd(target);
1263 if(ScreenON || SpriteON)
1267 if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
1273 X6502_Run(85-10-16);
1277 X6502_Run(6); // Tried 65, caused problems with Slalom(maybe others)
1284 if(GameHBIRQHook2 && (ScreenON || SpriteON))
1288 ResetRL(XBuf+scanline*320+32);
1298 uint32 scanlines_per_frame = PAL ? 312 : 262;
1300 FCEU_ApplyPeriodicCheats();
1303 if(ppudead) /* Needed for Knight Rider, possibly others. */
1305 //memset(XBuf, 0, 320*240);
1306 X6502_Run(scanlines_per_frame*(256+85));
1314 PPU[3]=PPUSPL=0; /* Not sure if this is correct. According
1315 to Matt Conte and my own tests, it is. Timing is probably
1316 off, though. NOTE: Not having this here
1317 breaks a Super Donkey Kong game. */
1319 X6502_Run(12); /* I need to figure out the true nature and length
1322 if(FCEUGameInfo.type==GIT_NSF)
1327 // Note: this is needed for asm core
1328 // Warning: using 'scanline' var here breaks Castlevania III
1331 X6502_Run(256+85-12);
1332 for (lines=scanlines_per_frame-242-1;lines;lines--)
1335 // X6502_Run((scanlines_per_frame-242)*(256+85)-12);
1340 if(ScreenON || SpriteON)
1345 for(x=0;x<42;x++) {PPU_hook(0x2000); PPU_hook(0);} // ugh
1352 if(ScreenON || SpriteON)
1354 RefreshAddr=TempAddr;
1355 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
1364 if(FCEUGameInfo.type==GIT_NSF)
1366 // run scanlines for asm core to fuction
1367 for(scanline=0;scanline<240;scanline++)
1376 for(scanline=0;scanline<240;) // scanline is incremented in DoLine. Evil. :/
1381 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1382 for(x=1,max=0,maxref=0;x<7;x++)
1391 SetNESDeemph(maxref,0);
1404 ssize=FlushEmulateSound();
1406 timestampbase += timestamp;
1412 FCEU_PutImageDummy();
1414 FCEUD_Update(0,WaveFinalMono,ssize);
1420 FCEUD_Update(XBuf+8,WaveFinalMono,ssize);
1428 #include <sys/time.h>
1431 void FCEUI_Emulate(void)
1434 uint64 starttime,end;
1437 gettimeofday(&tv,0);
1438 starttime=((uint64)tv.tv_sec*1000000)+tv.tv_usec;
1443 // Probably won't work well on Windows port; for
1444 // debugging/speed testing.
1448 gettimeofday(&tv,0);
1449 end=((uint64)tv.tv_sec*1000000)+tv.tv_usec;
1450 w=frcount*10000000000LL/(end-starttime);
1453 printf("Average FPS: %d.%04d\n",i,frac);
1459 void FCEUI_CloseGame(void)
1464 static void ResetPPU(void)
1466 VRAMBuffer=PPU[0]=PPU[1]=PPU[2]=PPU[3]=0;
1469 RefreshAddr=TempAddr=0;
1475 static void PowerPPU(void)
1477 memset(NTARAM,0x00,0x800);
1478 memset(PALRAM,0x00,0x20);
1479 memset(SPRAM,0x00,0x100);
1485 if(!GameLoaded) return;
1486 GameInterface(GI_RESETM2, 0);
1492 static void FCEU_MemoryRand(uint8 *ptr, uint32 size)
1497 *ptr=(x&4)?0xFF:0x00;
1506 if(!GameLoaded) return;
1508 FCEU_CheatResetRAM();
1509 FCEU_CheatAddRAM(2,0,RAM);
1513 #ifndef DEBUG_ASM_6502
1514 FCEU_MemoryRand(RAM,0x800);
1516 memset(RAM,0x00,0x800);
1521 GameInterface(GI_POWER, 0);
1531 /* savestate stuff */
1532 uint16 TempAddrT,RefreshAddrT;
1534 SFORMAT FCEUPPU_STATEINFO[]={
1535 { NTARAM, 0x800, "NTAR"},
1536 { PALRAM, 0x20, "PRAM"},
1537 { SPRAM, 0x100, "SPRA"},
1538 { PPU, 0x4, "PPUR"},
1539 { &XOffset, 1, "XOFF"},
1540 { &vtoggle, 1, "VTOG"},
1541 { &RefreshAddrT, 2|RLSB, "RADD"},
1542 { &TempAddrT, 2|RLSB, "TADD"},
1543 { &VRAMBuffer, 1, "VBUF"},
1544 { &PPUGenLatch, 1, "PGEN"},
1546 { &kook, 1, "KOOK"},
1547 { &ppudead, 1, "DEAD"},
1548 { &PPUSPL, 1, "PSPL"},