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 #ifndef DEBUG_ASM_6502
115 static void asmcpu_update(int32 cycles)
117 // some code from x6502.c
131 if((PSG[0x10]&0x80) && !(PSG[0x10]&0x40))
133 extern uint8 SIRQStat;
135 X6502_IRQBegin(FCEU_IQDPCM);
142 void asmcpu_unpack(void)
144 nes_registers[0] = X.A << 24;
145 nes_registers[1] = X.X;
146 nes_registers[2] = X.Y;
148 nes_registers[3] = X.PC;
150 nes_registers[4] = X.S << 24;
151 nes_registers[4]|= X.IRQlow << 8;
153 nes_registers[4] |= 1<<16; // MapIRQHook set bit
154 nes_registers[7] = (uint32)X.count << 16;
157 nes_registers[4]|= X.P & 0x5d;
158 nes_registers[5] = X.P << 24; // N
159 if (!(X.P&0x02)) nes_registers[5] |= 1; // Z
162 void asmcpu_pack(void)
164 X.A = nes_registers[0] >> 24;
165 X.X = nes_registers[1];
166 X.Y = nes_registers[2];
167 X.PC= nes_registers[3] - pc_base;
168 X.S = nes_registers[4] >> 24;
169 X.IRQlow = nes_registers[4] >> 8;
170 X.count = (int32) nes_registers[7] >> 16;
173 X.P = nes_registers[4] & 0x5d;
174 if ( nes_registers[5]&0x80000000) X.P |= 0x80; // N
175 if (!(nes_registers[5]&0x000000ff)) X.P |= 0x02; // Z
189 int AllocGenieRW(void)
191 if(!(AReadG=FCEU_malloc(0x8000*sizeof(readfunc))))
193 if(!(BWriteG=FCEU_malloc(0x8000*sizeof(writefunc))))
199 void FlushGenieRW(void)
205 for(x=0;x<0x8000;x++)
207 ARead[x+0x8000]=AReadG[x];
208 BWrite[x+0x8000]=BWriteG[x];
221 readfunc FASTAPASS(1) GetReadHandler(int32 a)
223 if(a>=0x8000 && RWWrap)
224 return AReadG[a-0x8000];
229 void FASTAPASS(3) SetReadHandler(int32 start, int32 end, readfunc func)
237 for(x=end;x>=start;x--)
240 AReadG[x-0x8000]=func;
246 for(x=end;x>=start;x--)
250 writefunc FASTAPASS(1) GetWriteHandler(int32 a)
252 if(RWWrap && a>=0x8000)
253 return BWriteG[a-0x8000];
258 void FASTAPASS(3) SetWriteHandler(int32 start, int32 end, writefunc func)
266 for(x=end;x>=start;x--)
269 BWriteG[x-0x8000]=func;
274 for(x=end;x>=start;x--)
281 uint32 TempAddr,RefreshAddr;
284 /* scanline is equal to the current visible scanline we're on. */
288 uint8 GameMemBlock[131072] __attribute__ ((aligned (4)));
289 uint8 NTARAM[0x800] __attribute__ ((aligned (4)));
290 uint8 PALRAM[0x20] __attribute__ ((aligned (4)));
291 #if !defined(ASM_6502) || defined(DEBUG_ASM_6502)
292 uint8 RAM[0x800] __attribute__ ((aligned (4)));
301 #define MMC5BGVRAMADR(V) &MMC5BGVPage[(V)>>10][(V)]
302 #define VRAMADR(V) &VPage[(V)>>10][(V)]
304 static int linestartts;
307 static uint8 *Plinef;
309 extern uint8 sprlinebuf[256+8];
311 extern uint8 sphitdata;
313 extern int spork; /* spork the world. Any sprites on this line?
314 Then this will be set to 1. Needed for zapper
315 emulation and *gasp* sprite emulation.
318 static void ResetRL(uint8 *target)
320 if(InputScanlineHook)
321 InputScanlineHook(0,0,0,0);
323 linestartts=timestamp*48+X6502_GetCycleCount();
327 static INLINE void Fixit1(void);
329 /* faking FCEUPPU_LineUpdate() from later versions of the emu */
330 static void FakedLineUpdate(void)
332 #define TOFIXNUM (272-0x4)
335 if (scanline >= 240) return;
337 if (tofix || sphitx != 0x100)
339 lastpixel = (timestamp*48-linestartts)>>4;
340 if (PAL) lastpixel += lastpixel>>4;
341 //printf("lastpixel: %i\n", lastpixel);
344 if (tofix && lastpixel>=TOFIXNUM)
356 for(x=sphitx;x<(sphitx+8) && x<l;x++)
358 if((sphitdata&(0x80>>(x-sphitx))) && !(Plinef[x]&64))
397 ret|=PPUGenLatch&0x1F;
401 //dprintf("r [2002] %02x",ret);
416 uint32 tmp=RefreshAddr&0x3FFF;
422 if(PPU_hook) PPU_hook(tmp);
423 PPUGenLatch=VRAMBuffer;
426 VRAMBuffer=VPage[tmp>>10][tmp];
430 VRAMBuffer=vnapage[(tmp>>10)&0x3][tmp&0x3FF];
433 if (INC32) RefreshAddr+=32;
435 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
436 dprintf("r [2007] %02x",ret);
458 //printf("$%04x:$%02x, %d\n",X.PC,V,scanline);
486 //printf("$%02x:$%02x\n",PPUSPL,V);
526 TempAddr|=(V&0x3f)<<8;
533 RefreshAddr=TempAddr;
535 PPU_hook(RefreshAddr);
543 uint32 tmp=RefreshAddr&0x3FFF;
549 PALRAM[0x00]=PALRAM[0x04]=PALRAM[0x08]=PALRAM[0x0C]=V&0x3f;
550 else if(tmp&3) PALRAM[(tmp&0x1f)]=V&0x3f;
554 if(PPUCHRRAM&(1<<(tmp>>10)))
555 VPage[tmp>>10][tmp]=V;
559 if(PPUNTARAM&(1<<((tmp&0xF00)>>10)))
560 vnapage[((tmp&0xF00)>>10)][tmp&0x3FF]=V;
562 if (INC32) RefreshAddr+=32;
564 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
573 B2004(0x2004,X.DB=ARead[t+x](t+x));
574 X6502_AddCycles(512);
577 void BGRender(uint8 *target)
580 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
586 RefreshLine(target-XOffset, vofs);
597 *(uint32 *)target=*(uint32 *)(target+4)=tem;
603 void FCEUI_FrameSkip(int x)
609 /* This is called at the beginning of each visible scanline */
610 static void LineUpdate(uint8 *target)
615 /* PRefreshLine() will not get called on skipped frames. This
616 * could cause a problem, but the solution would be rather complex,
617 * due to the current sprite 0 hit code.
622 if(scanline==y && SpriteON) PPU_status|=0x40; // hack
626 if(scanline < FSettings.FirstSLine || scanline > FSettings.LastSLine)
631 if(scanline==y && SpriteON) PPU_status|=0x40;
644 FCEU_dwmemset(target,tem,256);
648 if(InputScanlineHook)
649 InputScanlineHook(target,spork?sprlinebuf:0,linestartts,256);
653 static void LineUpdateEnd(uint8 *target)
656 if(ScreenON || SpriteON) // Yes, very el-cheapo.
659 block_and(target, 256, 0x30);
662 block_or(target, 256, 0xc0);
664 block_or(target, 256, 0x40);
666 block_andor(target, 256, 0x3f, 0x80);
670 if(ScreenON || SpriteON) // Yes, very el-cheapo.
675 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0x30303030;
681 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0xc0c0c0c0;
685 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])|0x40404040;
688 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0x80808080;
692 ((uint32 *)target)[-2]=((uint32 *)target)[-1]=0;
693 ((uint32 *)target)[64]=((uint32 *)target)[65]=0;
696 #define PAL(c) ((c)+cc)
699 static void PRefreshLine(void)
703 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
704 void (*PPU_hook_)(uint32 A) = PPU_hook;
712 zz2=(RefreshAddr>>10)&3;
713 PPU_hook_(0x2000|(RefreshAddr&0xFFF));
715 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
719 if((RefreshAddr&0x1f)==0x1f)
726 /* This high-level graphics MMC5 emulation code was written
727 for MMC5 carts in "CL" mode. It's probably not totally
728 correct for carts in "SL" mode.
730 static void RefreshLine_MMC5Hack1(uint8 *P, uint32 vofs)
734 tochange=MMC5HackSPMode&0x1F;
736 for(X1=33;X1;X1--,P+=8)
742 if((tochange<=0 && MMC5HackSPMode&0x40) ||
743 (tochange>0 && !(MMC5HackSPMode&0x40)))
748 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
749 if(ys>=0x1E) ys-=0x1E;
750 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
752 C = MMC5HackVROMPTR+vadr;
753 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
755 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
756 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
761 zz2=(RefreshAddr>>10)&3;
762 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
763 C = MMC5BGVRAMADR(vadr);
764 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
765 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
769 if((RefreshAddr&0x1f)==0x1f)
777 static void RefreshLine_MMC5Hack2(uint8 *P, uint32 vofs)
781 tochange=MMC5HackSPMode&0x1F;
783 for(X1=33;X1;X1--,P+=8)
790 if((tochange<=0 && MMC5HackSPMode&0x40) ||
791 (tochange>0 && !(MMC5HackSPMode&0x40)))
796 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
797 if(ys>=0x1E) ys-=0x1E;
798 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
800 C = MMC5HackVROMPTR+vadr;
801 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
803 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
804 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
809 zz2=(RefreshAddr>>10)&3;
810 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
811 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
812 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
813 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
817 if((RefreshAddr&0x1f)==0x1f)
825 static void RefreshLine_MMC5Hack3(uint8 *P, uint32 vofs)
829 for(X1=33;X1;X1--,P+=8)
837 zz2=(RefreshAddr>>10)&3;
838 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
839 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
840 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
841 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
845 if((RefreshAddr&0x1f)==0x1f)
852 static void RefreshLine_MMC5Hack4(uint8 *P, uint32 vofs)
856 for(X1=33;X1;X1--,P+=8)
863 zz2=(RefreshAddr>>10)&3;
864 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
865 C = MMC5BGVRAMADR(vadr);
866 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
867 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
871 if((RefreshAddr&0x1f)==0x1f)
878 static void RefreshLine_PPU_hook(uint8 *P, uint32 vofs)
881 void (*PPU_hook_)(uint32 A) = PPU_hook;
882 uint32 rfraddr = RefreshAddr;
883 uint8 *page = vnapage[(rfraddr>>10)&3];
885 for(X1=33;X1;X1--,P+=8)
892 PPU_hook_(0x2000|(rfraddr&0xFFF));
893 cc=page[0x3c0+(zz>>2)+((rfraddr&0x380)>>4)];
894 cc=((cc >> ((zz&2) + ((rfraddr&0x40)>>4))) &3) <<2;
895 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
902 if((rfraddr&0x1f)==0x1f) {
904 page = vnapage[(rfraddr>>10)&3];
908 RefreshAddr = rfraddr;
911 static void RefreshLine_normal(uint8 *P, uint32 vofs) // vofs is 0x107 max
914 uint32 rfraddr = RefreshAddr;
915 uint8 *page = vnapage[(rfraddr>>10)&3];
918 if ((rfraddr&0xc)!=0)
919 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
921 for (X1=33;X1;X1--,P+=8)
926 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
928 if ((rfraddr&0xc)==0)
929 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
930 cc=((cc2 >> ((rfraddr&2) + ((rfraddr&0x40)>>4) + ((rfraddr&0xc)<<1))) & 3) << 2;
934 if((rfraddr&0x1f)==0x1f) {
936 page = vnapage[(rfraddr>>10)&3];
940 RefreshAddr = rfraddr;
943 static void SetRefreshLine(void)
945 if(MMC5Hack && geniestage!=1)
947 if(MMC5HackCHRMode==0 && (MMC5HackSPMode&0x80))
949 if (RefreshLine != RefreshLine_MMC5Hack1) printf("set refr RefreshLine_MMC5Hack1\n");
950 RefreshLine = RefreshLine_MMC5Hack1;
952 else if(MMC5HackCHRMode==1 && (MMC5HackSPMode&0x80))
954 if (RefreshLine != RefreshLine_MMC5Hack2) printf("set refr RefreshLine_MMC5Hack2\n");
955 RefreshLine = RefreshLine_MMC5Hack2;
957 else if(MMC5HackCHRMode==1)
959 if (RefreshLine != RefreshLine_MMC5Hack3) printf("set refr RefreshLine_MMC5Hack3\n");
960 RefreshLine = RefreshLine_MMC5Hack3;
964 if (RefreshLine != RefreshLine_MMC5Hack4) printf("set refr RefreshLine_MMC5Hack4\n");
965 RefreshLine = RefreshLine_MMC5Hack4;
967 } // End if(MMC5Hack)
970 if (RefreshLine != RefreshLine_PPU_hook) printf("set refr RefreshLine_PPU_hook\n");
971 RefreshLine = RefreshLine_PPU_hook;
975 if (RefreshLine != RefreshLine_normal) printf("set refr RefreshLine_normal\n");
976 RefreshLine = RefreshLine_normal;
983 if(ScreenON || SpriteON)
985 uint32 rad=RefreshAddr;
987 rad|=TempAddr&0x041f;
989 //PPU_hook(RefreshAddr,-1);
996 if(ScreenON || SpriteON)
998 uint32 rad=RefreshAddr;
1000 if((rad&0x7000)==0x7000)
1003 if((rad&0x3E0)==0x3A0)
1010 if((rad&0x3E0)==0x3e0)
1018 //PPU_hook(RefreshAddr,-1);
1023 // ============================//
1025 // ===========================//
1027 void ResetMapping(void)
1031 SetReadHandler(0x0000,0xFFFF,ANull);
1032 SetWriteHandler(0x0000,0xFFFF,BNull);
1034 SetReadHandler(0,0x7FF,ARAML);
1035 SetWriteHandler(0,0x7FF,BRAML);
1037 SetReadHandler(0x800,0x1FFF,ARAMH); /* Part of a little */
1038 SetWriteHandler(0x800,0x1FFF,BRAMH); /* hack for a small speed boost. */
1040 for(x=0x2000;x<0x4000;x+=8)
1060 BWrite[0x4014]=B4014;
1066 void CloseGame(void)
1071 if(FCEUGameInfo.type!=GIT_NSF)
1072 FCEU_FlushGameCheats(0,0);
1074 if(FSettings.NetworkPlay) KillNetplay();
1076 GameInterface(GI_CLOSE, 0);
1082 void ResetGameLoaded(void)
1084 if(GameLoaded) CloseGame();
1087 GameHBIRQHook=GameHBIRQHook2=0;
1088 GameExpSound.Fill=0;
1089 GameExpSound.RChange=0;
1090 if(GameExpSound.Kill)
1091 GameExpSound.Kill();
1092 GameExpSound.Kill=0;
1098 FCEUGameInfo.name=0;
1099 FCEUGameInfo.type=GIT_CART;
1100 FCEUGameInfo.vidsys=GIV_USER;
1101 FCEUGameInfo.input[0]=FCEUGameInfo.input[1]=-1;
1102 FCEUGameInfo.inputfc=-1;
1104 FCEUGameInfo.soundchan=0;
1105 FCEUGameInfo.soundrate=0;
1106 FCEUGameInfo.cspecial=0;
1109 char lastLoadedGameName [2048];
1110 int LoadGameLastError = 0;
1111 int UNIFLoad(const char *name, int fp);
1112 int iNESLoad(const char *name, int fp);
1113 int FDSLoad(const char *name, int fp);
1114 int NSFLoad(int fp);
1116 FCEUGI *FCEUI_LoadGame(char *name)
1119 int have_movie = 0, have_ips = 0;
1123 LoadGameLastError = 0;
1126 strncpy(name2, name, sizeof(name2));
1127 name2[sizeof(name2)-1] = 0;
1129 fp=FCEU_fopen(name2,"rb");
1132 FCEU_PrintError("Error opening \"%s\"!",name);
1133 LoadGameLastError = 1;
1138 char *p = name2 + strlen(name2) - 4;
1139 if (strcasecmp(p, ".fcm") == 0) printf("movie detected\n"), have_movie = 1;
1140 if (strcasecmp(p, ".ips") == 0) printf("ips detected\n"), have_ips = 1;
1141 if (have_movie || have_ips)
1146 fp=FCEU_fopen(name2,"rb");
1147 if (!fp && p - name2 > 2)
1149 for (p--; p > name2 && *p != '.'; p--);
1151 fp=FCEU_fopen(name2,"rb");
1154 printf("no ROM for ips/movie\n");
1155 LoadGameLastError = 2;
1165 FILE *ips = fopen(name, "rb");
1167 fp=FCEU_fopen_forcemem(name2);
1168 if (!fp) { fclose(ips); return 0; }
1169 ApplyIPS(ips, fp); // closes ips
1173 if(iNESLoad(name2,fp))
1177 if(FDSLoad(name2,fp))
1179 if(UNIFLoad(name2,fp))
1182 FCEU_PrintError("An error occurred while loading the file.");
1184 // format handlers may set LoadGameLastError themselves.
1185 if (LoadGameLastError == 0) LoadGameLastError = 3;
1193 if(FCEUGameInfo.type!=GIT_NSF)
1194 if(FSettings.GameGenie)
1199 if(FSettings.NetworkPlay) InitNetplay();
1202 if(FCEUGameInfo.type!=GIT_NSF)
1204 FCEU_LoadGamePalette();
1205 FCEU_LoadGameCheats(0);
1208 FCEU_ResetPalette();
1212 FCEUI_LoadMovie(name, 1);
1214 strcpy(lastLoadedGameName, name2);
1216 return(&FCEUGameInfo);
1220 void FCEU_ResetVidSys(void)
1224 if(FCEUGameInfo.vidsys==GIV_NTSC)
1226 else if(FCEUGameInfo.vidsys==GIV_PAL)
1234 FSettings.FirstSLine=FSettings.UsrFirstSLine[1];
1235 FSettings.LastSLine=FSettings.UsrLastSLine[1];
1240 FSettings.FirstSLine=FSettings.UsrFirstSLine[0];
1241 FSettings.LastSLine=FSettings.UsrLastSLine[0];
1243 printf("ResetVidSys: PAL = %i\n", PAL);
1244 SetSoundVariables();
1247 int FCEUI_Initialize(void)
1249 if(!InitVirtualVideo())
1251 memset(&FSettings,0,sizeof(FSettings));
1252 FSettings.UsrFirstSLine[0]=8;
1253 FSettings.UsrFirstSLine[1]=0;
1254 FSettings.UsrLastSLine[0]=FSettings.UsrLastSLine[1]=239;
1255 FSettings.SoundVolume=100;
1257 FCEUI_Initialize098();
1258 FCEUI_SetEmuMode(0);
1263 void FCEUI_Kill(void)
1268 static void EmLoop(void);
1271 void (*ResetNES)(void) = 0;
1272 void (*PowerNES)(void) = 0;
1273 void (*FCEUI_Emulate)(void) = 0;
1275 void FCEUI_SetEmuMode(int is_new)
1277 use098code = is_new;
1280 ResetNES=ResetNES098;
1281 PowerNES=PowerNES098;
1282 FCEUI_Emulate=FCEUI_Emulate098;
1286 ResetNES=ResetNES081;
1287 PowerNES=PowerNES081;
1288 FCEUI_Emulate=EmLoop;
1292 void MMC5_hb(int); /* Ugh ugh ugh. */
1293 static void DoLine(void)
1295 uint8 *target=XBuf+scanline*320+32;
1299 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1303 // check: Battletoads & Double Dragon, Addams Family
1304 // sky glitches in SMB1 if done wrong
1310 if(scanline>=FSettings.FirstSLine && scanline<=FSettings.LastSLine)
1312 if(SpriteON && spork)
1313 CopySprites(target);
1315 LineUpdateEnd(target);
1319 if(ScreenON || SpriteON)
1323 if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
1329 X6502_Run(85-10-16);
1333 X6502_Run(6); // Tried 65, caused problems with Slalom(maybe others)
1340 if(GameHBIRQHook2 && (ScreenON || SpriteON))
1344 ResetRL(XBuf+scanline*320+32);
1349 static void EmLoop(void)
1354 uint32 scanlines_per_frame = PAL ? 312 : 262;
1356 FCEU_ApplyPeriodicCheats();
1359 if(ppudead) /* Needed for Knight Rider, possibly others. */
1361 //memset(XBuf, 0, 320*240);
1362 //X6502_Run(scanlines_per_frame*(256+85));
1364 for (lines=scanlines_per_frame;lines;lines--)
1373 PPU[3]=PPUSPL=0; /* Not sure if this is correct. According
1374 to Matt Conte and my own tests, it is. Timing is probably
1375 off, though. NOTE: Not having this here
1376 breaks a Super Donkey Kong game. */
1378 X6502_Run(12); /* I need to figure out the true nature and length
1381 if(FCEUGameInfo.type==GIT_NSF)
1386 // Note: this is needed for asm core
1387 // Warning: using 'scanline' var here breaks Castlevania III
1390 X6502_Run(256+85-12);
1391 for (lines=scanlines_per_frame-242-1;lines;lines--)
1394 // X6502_Run((scanlines_per_frame-242)*(256+85)-12);
1399 if(ScreenON || SpriteON)
1404 for(x=0;x<42;x++) {PPU_hook(0x2000); PPU_hook(0);} // ugh
1411 if(ScreenON || SpriteON)
1413 RefreshAddr=TempAddr;
1414 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
1423 if(FCEUGameInfo.type==GIT_NSF)
1425 // run scanlines for asm core to fuction
1426 for(scanline=0;scanline<240;scanline++)
1435 for(scanline=0;scanline<240;) // scanline is incremented in DoLine. Evil. :/
1440 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1441 for(x=1,max=0,maxref=0;x<7;x++)
1450 SetNESDeemph(maxref,0);
1463 ssize=FlushEmulateSound();
1465 timestampbase += timestamp;
1471 FCEU_PutImageDummy();
1473 FCEUD_Update(0,WaveFinalMono,ssize);
1479 FCEUD_Update(XBuf+8,WaveFinalMono,ssize);
1486 void FCEUI_CloseGame(void)
1491 static void ResetPPU(void)
1493 VRAMBuffer=PPU[0]=PPU[1]=PPU[2]=PPU[3]=0;
1496 RefreshAddr=TempAddr=0;
1502 static void PowerPPU(void)
1504 memset(NTARAM,0x00,0x800);
1505 memset(PALRAM,0x00,0x20);
1506 memset(SPRAM,0x00,0x100);
1510 void ResetNES081(void)
1512 if(!GameLoaded) return;
1513 GameInterface(GI_RESETM2, 0);
1519 #ifndef DEBUG_ASM_6502
1520 static void FCEU_MemoryRand(uint8 *ptr, uint32 size)
1525 *ptr=(x&4)?0xFF:0x00;
1533 void PowerNES081(void)
1535 if(!GameLoaded) return;
1537 FCEU_CheatResetRAM();
1538 FCEU_CheatAddRAM(2,0,RAM);
1542 #ifndef DEBUG_ASM_6502
1543 FCEU_MemoryRand(RAM,0x800);
1545 memset(RAM,0x00,0x800);
1550 GameInterface(GI_POWER, 0);
1551 if(FCEUGameInfo.type==GIT_VSUNI)
1563 /* savestate stuff */
1564 uint16 TempAddrT,RefreshAddrT;
1566 SFORMAT FCEUPPU_STATEINFO[]={
1567 { NTARAM, 0x800, "NTAR"},
1568 { PALRAM, 0x20, "PRAM"},
1569 { SPRAM, 0x100, "SPRA"},
1570 { PPU, 0x4, "PPUR"},
1571 { &XOffset, 1, "XOFF"},
1572 { &vtoggle, 1, "VTOG"},
1573 { &RefreshAddrT, 2|RLSB, "RADD"},
1574 { &TempAddrT, 2|RLSB, "TADD"},
1575 { &VRAMBuffer, 1, "VBUF"},
1576 { &PPUGenLatch, 1, "PGEN"},
1578 { &kook, 1, "KOOK"},
1579 { &ppudead, 1, "DEAD"},
1580 { &PPUSPL, 1, "PSPL"},