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;
148 nes_registers[7] = (uint32)X.count << 16;
151 nes_registers[4]|= X.P & 0x5d;
152 nes_registers[5] = X.P << 24; // N
153 if (!(X.P&0x02)) nes_registers[5] |= 1; // Z
156 void asmcpu_pack(void)
158 X.A = nes_registers[0] >> 24;
159 X.X = nes_registers[1];
160 X.Y = nes_registers[2];
161 X.PC= nes_registers[3] - pc_base;
162 X.S = nes_registers[4] >> 24;
163 X.IRQlow = nes_registers[4] >> 8;
164 X.count = (int32) nes_registers[7] >> 16;
167 X.P = nes_registers[4] & 0x5d;
168 if ( nes_registers[5]&0x80000000) X.P |= 0x80; // N
169 if (!(nes_registers[5]&0x000000ff)) X.P |= 0x02; // Z
183 int AllocGenieRW(void)
185 if(!(AReadG=FCEU_malloc(0x8000*sizeof(readfunc))))
187 if(!(BWriteG=FCEU_malloc(0x8000*sizeof(writefunc))))
193 void FlushGenieRW(void)
199 for(x=0;x<0x8000;x++)
201 ARead[x+0x8000]=AReadG[x];
202 BWrite[x+0x8000]=BWriteG[x];
215 readfunc FASTAPASS(1) GetReadHandler(int32 a)
217 if(a>=0x8000 && RWWrap)
218 return AReadG[a-0x8000];
223 void FASTAPASS(3) SetReadHandler(int32 start, int32 end, readfunc func)
231 for(x=end;x>=start;x--)
234 AReadG[x-0x8000]=func;
240 for(x=end;x>=start;x--)
244 writefunc FASTAPASS(1) GetWriteHandler(int32 a)
246 if(RWWrap && a>=0x8000)
247 return BWriteG[a-0x8000];
252 void FASTAPASS(3) SetWriteHandler(int32 start, int32 end, writefunc func)
260 for(x=end;x>=start;x--)
263 BWriteG[x-0x8000]=func;
268 for(x=end;x>=start;x--)
275 uint32 TempAddr,RefreshAddr;
278 /* scanline is equal to the current visible scanline we're on. */
282 uint8 GameMemBlock[131072] __attribute__ ((aligned (4)));
283 uint8 NTARAM[0x800] __attribute__ ((aligned (4)));
284 uint8 PALRAM[0x20] __attribute__ ((aligned (4)));
285 #if !defined(ASM_6502) || defined(DEBUG_ASM_6502)
286 uint8 RAM[0x800] __attribute__ ((aligned (4)));
295 #define MMC5BGVRAMADR(V) &MMC5BGVPage[(V)>>10][(V)]
296 #define VRAMADR(V) &VPage[(V)>>10][(V)]
298 static int linestartts;
301 static uint8 *Plinef;
303 extern uint8 sprlinebuf[256+8];
305 extern uint8 sphitdata;
307 extern int spork; /* spork the world. Any sprites on this line?
308 Then this will be set to 1. Needed for zapper
309 emulation and *gasp* sprite emulation.
312 static void ResetRL(uint8 *target)
314 if(InputScanlineHook)
315 InputScanlineHook(0,0,0,0);
317 linestartts=timestamp*48+X6502_GetCycleCount();
321 static INLINE void Fixit1(void);
323 /* faking FCEUPPU_LineUpdate() from later versions of the emu */
324 static void FakedLineUpdate(void)
326 #define TOFIXNUM (272-0x4)
329 if (scanline >= 240) return;
331 if (tofix || sphitx != 0x100)
333 lastpixel = (timestamp*48-linestartts)>>4;
334 if (PAL) lastpixel += lastpixel>>4;
335 //printf("lastpixel: %i\n", lastpixel);
338 if (tofix && lastpixel>=TOFIXNUM)
350 for(x=sphitx;x<(sphitx+8) && x<l;x++)
352 if((sphitdata&(0x80>>(x-sphitx))) && !(Plinef[x]&64))
391 ret|=PPUGenLatch&0x1F;
395 //dprintf("r [2002] %02x",ret);
410 uint32 tmp=RefreshAddr&0x3FFF;
416 if(PPU_hook) PPU_hook(tmp);
417 PPUGenLatch=VRAMBuffer;
420 VRAMBuffer=VPage[tmp>>10][tmp];
424 VRAMBuffer=vnapage[(tmp>>10)&0x3][tmp&0x3FF];
427 if (INC32) RefreshAddr+=32;
429 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
430 dprintf("r [2007] %02x",ret);
452 //printf("$%04x:$%02x, %d\n",X.PC,V,scanline);
480 //printf("$%02x:$%02x\n",PPUSPL,V);
520 TempAddr|=(V&0x3f)<<8;
527 RefreshAddr=TempAddr;
529 PPU_hook(RefreshAddr);
537 uint32 tmp=RefreshAddr&0x3FFF;
543 PALRAM[0x00]=PALRAM[0x04]=PALRAM[0x08]=PALRAM[0x0C]=V&0x3f;
544 else if(tmp&3) PALRAM[(tmp&0x1f)]=V&0x3f;
548 if(PPUCHRRAM&(1<<(tmp>>10)))
549 VPage[tmp>>10][tmp]=V;
553 if(PPUNTARAM&(1<<((tmp&0xF00)>>10)))
554 vnapage[((tmp&0xF00)>>10)][tmp&0x3FF]=V;
556 if (INC32) RefreshAddr+=32;
558 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
567 B2004(0x2004,X.DB=ARead[t+x](t+x));
568 X6502_AddCycles(512);
571 void BGRender(uint8 *target)
574 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
580 RefreshLine(target-XOffset, vofs);
591 *(uint32 *)target=*(uint32 *)(target+4)=tem;
597 void FCEUI_FrameSkip(int x)
603 /* This is called at the beginning of each visible scanline */
604 static void LineUpdate(uint8 *target)
609 /* PRefreshLine() will not get called on skipped frames. This
610 * could cause a problem, but the solution would be rather complex,
611 * due to the current sprite 0 hit code.
616 if(scanline==y && SpriteON) PPU_status|=0x40; // hack
620 if(scanline < FSettings.FirstSLine || scanline > FSettings.LastSLine)
625 if(scanline==y && SpriteON) PPU_status|=0x40;
638 FCEU_dwmemset(target,tem,256);
642 if(InputScanlineHook)
643 InputScanlineHook(target,spork?sprlinebuf:0,linestartts,256);
647 static void LineUpdateEnd(uint8 *target)
650 if(ScreenON || SpriteON) // Yes, very el-cheapo.
653 block_and(target, 256, 0x30);
656 block_or(target, 256, 0xc0);
658 block_or(target, 256, 0x40);
660 block_andor(target, 256, 0x3f, 0x80);
664 if(ScreenON || SpriteON) // Yes, very el-cheapo.
669 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0x30303030;
675 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0xc0c0c0c0;
679 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])|0x40404040;
682 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0x80808080;
686 ((uint32 *)target)[-2]=((uint32 *)target)[-1]=0;
687 ((uint32 *)target)[64]=((uint32 *)target)[65]=0;
690 #define PAL(c) ((c)+cc)
693 static void PRefreshLine(void)
697 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
698 void (*PPU_hook_)(uint32 A) = PPU_hook;
706 zz2=(RefreshAddr>>10)&3;
707 PPU_hook_(0x2000|(RefreshAddr&0xFFF));
709 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
713 if((RefreshAddr&0x1f)==0x1f)
720 /* This high-level graphics MMC5 emulation code was written
721 for MMC5 carts in "CL" mode. It's probably not totally
722 correct for carts in "SL" mode.
724 static void RefreshLine_MMC5Hack1(uint8 *P, uint32 vofs)
728 tochange=MMC5HackSPMode&0x1F;
730 for(X1=33;X1;X1--,P+=8)
736 if((tochange<=0 && MMC5HackSPMode&0x40) ||
737 (tochange>0 && !(MMC5HackSPMode&0x40)))
742 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
743 if(ys>=0x1E) ys-=0x1E;
744 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
746 C = MMC5HackVROMPTR+vadr;
747 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
749 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
750 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
755 zz2=(RefreshAddr>>10)&3;
756 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
757 C = MMC5BGVRAMADR(vadr);
758 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
759 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
763 if((RefreshAddr&0x1f)==0x1f)
771 static void RefreshLine_MMC5Hack2(uint8 *P, uint32 vofs)
775 tochange=MMC5HackSPMode&0x1F;
777 for(X1=33;X1;X1--,P+=8)
784 if((tochange<=0 && MMC5HackSPMode&0x40) ||
785 (tochange>0 && !(MMC5HackSPMode&0x40)))
790 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
791 if(ys>=0x1E) ys-=0x1E;
792 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
794 C = MMC5HackVROMPTR+vadr;
795 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
797 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
798 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
803 zz2=(RefreshAddr>>10)&3;
804 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
805 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
806 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
807 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
811 if((RefreshAddr&0x1f)==0x1f)
819 static void RefreshLine_MMC5Hack3(uint8 *P, uint32 vofs)
823 for(X1=33;X1;X1--,P+=8)
831 zz2=(RefreshAddr>>10)&3;
832 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
833 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
834 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
835 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
839 if((RefreshAddr&0x1f)==0x1f)
846 static void RefreshLine_MMC5Hack4(uint8 *P, uint32 vofs)
850 for(X1=33;X1;X1--,P+=8)
857 zz2=(RefreshAddr>>10)&3;
858 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
859 C = MMC5BGVRAMADR(vadr);
860 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
861 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
865 if((RefreshAddr&0x1f)==0x1f)
872 static void RefreshLine_PPU_hook(uint8 *P, uint32 vofs)
875 void (*PPU_hook_)(uint32 A) = PPU_hook;
876 uint32 rfraddr = RefreshAddr;
877 uint8 *page = vnapage[(rfraddr>>10)&3];
879 for(X1=33;X1;X1--,P+=8)
886 PPU_hook_(0x2000|(rfraddr&0xFFF));
887 cc=page[0x3c0+(zz>>2)+((rfraddr&0x380)>>4)];
888 cc=((cc >> ((zz&2) + ((rfraddr&0x40)>>4))) &3) <<2;
889 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
896 if((rfraddr&0x1f)==0x1f) {
898 page = vnapage[(rfraddr>>10)&3];
902 RefreshAddr = rfraddr;
905 static void RefreshLine_normal(uint8 *P, uint32 vofs) // vofs is 0x107 max
908 uint32 rfraddr = RefreshAddr;
909 uint8 *page = vnapage[(rfraddr>>10)&3];
912 if ((rfraddr&0xc)!=0)
913 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
915 for (X1=33;X1;X1--,P+=8)
920 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
922 if ((rfraddr&0xc)==0)
923 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
924 cc=((cc2 >> ((rfraddr&2) + ((rfraddr&0x40)>>4) + ((rfraddr&0xc)<<1))) & 3) << 2;
928 if((rfraddr&0x1f)==0x1f) {
930 page = vnapage[(rfraddr>>10)&3];
934 RefreshAddr = rfraddr;
937 static void SetRefreshLine(void)
939 if(MMC5Hack && geniestage!=1)
941 if(MMC5HackCHRMode==0 && (MMC5HackSPMode&0x80))
943 if (RefreshLine != RefreshLine_MMC5Hack1) printf("set refr RefreshLine_MMC5Hack1\n");
944 RefreshLine = RefreshLine_MMC5Hack1;
946 else if(MMC5HackCHRMode==1 && (MMC5HackSPMode&0x80))
948 if (RefreshLine != RefreshLine_MMC5Hack2) printf("set refr RefreshLine_MMC5Hack2\n");
949 RefreshLine = RefreshLine_MMC5Hack2;
951 else if(MMC5HackCHRMode==1)
953 if (RefreshLine != RefreshLine_MMC5Hack3) printf("set refr RefreshLine_MMC5Hack3\n");
954 RefreshLine = RefreshLine_MMC5Hack3;
958 if (RefreshLine != RefreshLine_MMC5Hack4) printf("set refr RefreshLine_MMC5Hack4\n");
959 RefreshLine = RefreshLine_MMC5Hack4;
961 } // End if(MMC5Hack)
964 if (RefreshLine != RefreshLine_PPU_hook) printf("set refr RefreshLine_PPU_hook\n");
965 RefreshLine = RefreshLine_PPU_hook;
969 if (RefreshLine != RefreshLine_normal) printf("set refr RefreshLine_normal\n");
970 RefreshLine = RefreshLine_normal;
977 if(ScreenON || SpriteON)
979 uint32 rad=RefreshAddr;
981 rad|=TempAddr&0x041f;
983 //PPU_hook(RefreshAddr,-1);
990 if(ScreenON || SpriteON)
992 uint32 rad=RefreshAddr;
994 if((rad&0x7000)==0x7000)
997 if((rad&0x3E0)==0x3A0)
1004 if((rad&0x3E0)==0x3e0)
1012 //PPU_hook(RefreshAddr,-1);
1017 // ============================//
1019 // ===========================//
1021 void ResetMapping(void)
1025 SetReadHandler(0x0000,0xFFFF,ANull);
1026 SetWriteHandler(0x0000,0xFFFF,BNull);
1028 SetReadHandler(0,0x7FF,ARAML);
1029 SetWriteHandler(0,0x7FF,BRAML);
1031 SetReadHandler(0x800,0x1FFF,ARAMH); /* Part of a little */
1032 SetWriteHandler(0x800,0x1FFF,BRAMH); /* hack for a small speed boost. */
1034 for(x=0x2000;x<0x4000;x+=8)
1054 BWrite[0x4014]=B4014;
1060 void CloseGame(void)
1065 if(FCEUGameInfo.type!=GIT_NSF)
1066 FCEU_FlushGameCheats(0,0);
1068 if(FSettings.NetworkPlay) KillNetplay();
1070 GameInterface(GI_CLOSE, 0);
1076 void ResetGameLoaded(void)
1078 if(GameLoaded) CloseGame();
1081 GameHBIRQHook=GameHBIRQHook2=0;
1082 GameExpSound.Fill=0;
1083 GameExpSound.RChange=0;
1084 if(GameExpSound.Kill)
1085 GameExpSound.Kill();
1086 GameExpSound.Kill=0;
1092 FCEUGameInfo.name=0;
1093 FCEUGameInfo.type=GIT_CART;
1094 FCEUGameInfo.vidsys=GIV_USER;
1095 FCEUGameInfo.input[0]=FCEUGameInfo.input[1]=-1;
1096 FCEUGameInfo.inputfc=-1;
1099 char lastLoadedGameName [2048];
1100 int LoadGameLastError = 0;
1101 int UNIFLoad(const char *name, int fp);
1102 int iNESLoad(const char *name, int fp);
1103 int FDSLoad(const char *name, int fp);
1104 int NSFLoad(int fp);
1106 FCEUGI *FCEUI_LoadGame(char *name)
1113 LoadGameLastError = 0;
1116 strncpy(name2, name, sizeof(name2));
1117 name2[sizeof(name2)-1] = 0;
1119 fp=FCEU_fopen(name2,"rb");
1122 FCEU_PrintError("Error opening \"%s\"!",name);
1123 LoadGameLastError = 1;
1128 char *p = name2 + strlen(name2) - 4;
1129 if (strcmp(p, ".fcm") == 0)
1132 printf("movie detected\n");
1135 fp=FCEU_fopen(name2,"rb");
1136 if (!fp && p - name2 > 2) p[-2] = 0;
1137 fp=FCEU_fopen(name2,"rb");
1139 printf("no ROM for movie\n");
1140 LoadGameLastError = 2;
1148 if(iNESLoad(name2,fp))
1152 if(FDSLoad(name2,fp))
1154 if(UNIFLoad(name2,fp))
1157 FCEU_PrintError("An error occurred while loading the file.");
1159 // format handlers may set LoadGameLastError themselves.
1160 if (LoadGameLastError == 0) LoadGameLastError = 3;
1168 if(FCEUGameInfo.type!=GIT_NSF)
1169 if(FSettings.GameGenie)
1174 if(FSettings.NetworkPlay) InitNetplay();
1177 if(FCEUGameInfo.type!=GIT_NSF)
1179 FCEU_LoadGamePalette();
1180 FCEU_LoadGameCheats(0);
1183 FCEU_ResetPalette();
1187 FCEUI_LoadMovie(name, 1);
1189 strcpy(lastLoadedGameName, name2);
1191 return(&FCEUGameInfo);
1195 void FCEU_ResetVidSys(void)
1199 if(FCEUGameInfo.vidsys==GIV_NTSC)
1201 else if(FCEUGameInfo.vidsys==GIV_PAL)
1209 FSettings.FirstSLine=FSettings.UsrFirstSLine[1];
1210 FSettings.LastSLine=FSettings.UsrLastSLine[1];
1215 FSettings.FirstSLine=FSettings.UsrFirstSLine[0];
1216 FSettings.LastSLine=FSettings.UsrLastSLine[0];
1218 printf("ResetVidSys: PAL = %i\n", PAL);
1219 SetSoundVariables();
1222 int FCEUI_Initialize(void)
1224 if(!InitVirtualVideo())
1226 memset(&FSettings,0,sizeof(FSettings));
1227 FSettings.UsrFirstSLine[0]=8;
1228 FSettings.UsrFirstSLine[1]=0;
1229 FSettings.UsrLastSLine[0]=FSettings.UsrLastSLine[1]=239;
1230 FSettings.SoundVolume=100;
1234 void MMC5_hb(int); /* Ugh ugh ugh. */
1235 static void DoLine(void)
1237 uint8 *target=XBuf+scanline*320+32;
1241 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1245 // check: Battletoads & Double Dragon, Addams Family
1246 // sky glitches in SMB1 if done wrong
1252 if(scanline>=FSettings.FirstSLine && scanline<=FSettings.LastSLine)
1254 if(SpriteON && spork)
1255 CopySprites(target);
1257 LineUpdateEnd(target);
1261 if(ScreenON || SpriteON)
1265 if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
1271 X6502_Run(85-10-16);
1275 X6502_Run(6); // Tried 65, caused problems with Slalom(maybe others)
1282 if(GameHBIRQHook2 && (ScreenON || SpriteON))
1286 ResetRL(XBuf+scanline*320+32);
1296 uint32 scanlines_per_frame = PAL ? 312 : 262;
1298 FCEU_ApplyPeriodicCheats();
1301 if(ppudead) /* Needed for Knight Rider, possibly others. */
1303 //memset(XBuf, 0, 320*240);
1304 X6502_Run(scanlines_per_frame*(256+85));
1312 PPU[3]=PPUSPL=0; /* Not sure if this is correct. According
1313 to Matt Conte and my own tests, it is. Timing is probably
1314 off, though. NOTE: Not having this here
1315 breaks a Super Donkey Kong game. */
1317 X6502_Run(12); /* I need to figure out the true nature and length
1320 if(FCEUGameInfo.type==GIT_NSF)
1325 // Note: this is needed for asm core
1326 // Warning: using 'scanline' var here breaks Castlevania III
1329 X6502_Run(256+85-12);
1330 for (lines=scanlines_per_frame-242-1;lines;lines--)
1333 // X6502_Run((scanlines_per_frame-242)*(256+85)-12);
1338 if(ScreenON || SpriteON)
1343 for(x=0;x<42;x++) {PPU_hook(0x2000); PPU_hook(0);} // ugh
1350 if(ScreenON || SpriteON)
1352 RefreshAddr=TempAddr;
1353 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
1362 if(FCEUGameInfo.type==GIT_NSF)
1364 // run scanlines for asm core to fuction
1365 for(scanline=0;scanline<240;scanline++)
1374 for(scanline=0;scanline<240;) // scanline is incremented in DoLine. Evil. :/
1379 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1380 for(x=1,max=0,maxref=0;x<7;x++)
1389 SetNESDeemph(maxref,0);
1402 ssize=FlushEmulateSound();
1404 timestampbase += timestamp;
1410 FCEU_PutImageDummy();
1412 FCEUD_Update(0,WaveFinalMono,ssize);
1418 FCEUD_Update(XBuf+8,WaveFinalMono,ssize);
1426 #include <sys/time.h>
1429 void FCEUI_Emulate(void)
1432 uint64 starttime,end;
1435 gettimeofday(&tv,0);
1436 starttime=((uint64)tv.tv_sec*1000000)+tv.tv_usec;
1441 // Probably won't work well on Windows port; for
1442 // debugging/speed testing.
1446 gettimeofday(&tv,0);
1447 end=((uint64)tv.tv_sec*1000000)+tv.tv_usec;
1448 w=frcount*10000000000LL/(end-starttime);
1451 printf("Average FPS: %d.%04d\n",i,frac);
1457 void FCEUI_CloseGame(void)
1462 static void ResetPPU(void)
1464 VRAMBuffer=PPU[0]=PPU[1]=PPU[2]=PPU[3]=0;
1467 RefreshAddr=TempAddr=0;
1473 static void PowerPPU(void)
1475 memset(NTARAM,0x00,0x800);
1476 memset(PALRAM,0x00,0x20);
1477 memset(SPRAM,0x00,0x100);
1483 if(!GameLoaded) return;
1484 GameInterface(GI_RESETM2, 0);
1490 static void FCEU_MemoryRand(uint8 *ptr, uint32 size)
1495 *ptr=(x&4)?0xFF:0x00;
1504 if(!GameLoaded) return;
1506 FCEU_CheatResetRAM();
1507 FCEU_CheatAddRAM(2,0,RAM);
1511 #ifndef DEBUG_ASM_6502
1512 FCEU_MemoryRand(RAM,0x800);
1514 memset(RAM,0x00,0x800);
1517 GameInterface(GI_POWER, 0);
1529 /* savestate stuff */
1530 uint16 TempAddrT,RefreshAddrT;
1532 SFORMAT FCEUPPU_STATEINFO[]={
1533 { NTARAM, 0x800, "NTAR"},
1534 { PALRAM, 0x20, "PRAM"},
1535 { SPRAM, 0x100, "SPRA"},
1536 { PPU, 0x4, "PPUR"},
1537 { &XOffset, 1, "XOFF"},
1538 { &vtoggle, 1, "VTOG"},
1539 { &RefreshAddrT, 2|RLSB, "RADD"},
1540 { &TempAddrT, 2|RLSB, "TADD"},
1541 { &VRAMBuffer, 1, "VBUF"},
1542 { &PPUGenLatch, 1, "PGEN"},
1544 { &kook, 1, "KOOK"},
1545 { &ppudead, 1, "DEAD"},
1546 { &PPUSPL, 1, "PSPL"},