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
60 #include "drivers/gp2x/asmutils.h"
66 static void (*RefreshLine)(uint8 *P, uint32 vofs) = NULL;
67 static void PRefreshLine(void);
69 static void ResetPPU(void);
70 static void PowerPPU(void);
72 uint64 timestampbase=0;
78 uint32 MMC5HackVROMMask;
79 uint8 *MMC5HackExNTARAMPtr;
80 uint8 *MMC5HackVROMPTR;
81 uint8 MMC5HackCHRMode=0;
83 uint8 MMC5HackSPScroll;
86 uint8 *MMC5SPRVPage[8];
87 uint8 *MMC5BGVPage[8];
90 uint8 VRAMBuffer,PPUGenLatch;
96 /* Color deemphasis emulation. Joy... */
98 static int deempcnt[8];
101 void (*GameInterface)(int h, void *param);
103 void FP_FASTAPASS(1) (*PPU_hook)(uint32 A);
105 void (*GameStateRestore)(int version);
106 void (*GameHBIRQHook)(void), (*GameHBIRQHook2)(void);
108 readfunc ARead[0x10000];
109 writefunc BWrite[0x10000];
110 static readfunc *AReadG;
111 static writefunc *BWriteG;
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);
141 void asmcpu_unpack(void)
143 nes_registers[0] = X.A << 24;
144 nes_registers[1] = X.X;
145 nes_registers[2] = X.Y;
147 nes_registers[3] = X.PC;
149 nes_registers[4] = X.S << 24;
150 nes_registers[4]|= X.IRQlow << 8;
152 nes_registers[4] |= 1<<16; // MapIRQHook set bit
153 nes_registers[7] = (uint32)X.count << 16;
156 nes_registers[4]|= X.P & 0x5d;
157 nes_registers[5] = X.P << 24; // N
158 if (!(X.P&0x02)) nes_registers[5] |= 1; // Z
161 void asmcpu_pack(void)
163 X.A = nes_registers[0] >> 24;
164 X.X = nes_registers[1];
165 X.Y = nes_registers[2];
166 X.PC= nes_registers[3] - pc_base;
167 X.S = nes_registers[4] >> 24;
168 X.IRQlow = nes_registers[4] >> 8;
169 X.count = (int32) nes_registers[7] >> 16;
172 X.P = nes_registers[4] & 0x5d;
173 if ( nes_registers[5]&0x80000000) X.P |= 0x80; // N
174 if (!(nes_registers[5]&0x000000ff)) X.P |= 0x02; // Z
188 int AllocGenieRW(void)
190 if(!(AReadG=FCEU_malloc(0x8000*sizeof(readfunc))))
192 if(!(BWriteG=FCEU_malloc(0x8000*sizeof(writefunc))))
198 void FlushGenieRW(void)
204 for(x=0;x<0x8000;x++)
206 ARead[x+0x8000]=AReadG[x];
207 BWrite[x+0x8000]=BWriteG[x];
220 readfunc FASTAPASS(1) GetReadHandler(int32 a)
222 if(a>=0x8000 && RWWrap)
223 return AReadG[a-0x8000];
228 void FASTAPASS(3) SetReadHandler(int32 start, int32 end, readfunc func)
236 for(x=end;x>=start;x--)
239 AReadG[x-0x8000]=func;
245 for(x=end;x>=start;x--)
249 writefunc FASTAPASS(1) GetWriteHandler(int32 a)
251 if(RWWrap && a>=0x8000)
252 return BWriteG[a-0x8000];
257 void FASTAPASS(3) SetWriteHandler(int32 start, int32 end, writefunc func)
265 for(x=end;x>=start;x--)
268 BWriteG[x-0x8000]=func;
273 for(x=end;x>=start;x--)
280 uint32 TempAddr,RefreshAddr;
283 /* scanline is equal to the current visible scanline we're on. */
287 uint8 GameMemBlock[131072] __attribute__ ((aligned (4)));
288 uint8 NTARAM[0x800] __attribute__ ((aligned (4)));
289 uint8 PALRAM[0x20] __attribute__ ((aligned (4)));
290 #if !defined(ASM_6502) || defined(DEBUG_ASM_6502)
291 uint8 RAM[0x800] __attribute__ ((aligned (4)));
300 #define MMC5BGVRAMADR(V) &MMC5BGVPage[(V)>>10][(V)]
301 #define VRAMADR(V) &VPage[(V)>>10][(V)]
303 static int linestartts;
306 static uint8 *Plinef;
308 extern uint8 sprlinebuf[256+8];
310 extern uint8 sphitdata;
312 extern int spork; /* spork the world. Any sprites on this line?
313 Then this will be set to 1. Needed for zapper
314 emulation and *gasp* sprite emulation.
317 static void ResetRL(uint8 *target)
319 if(InputScanlineHook)
320 InputScanlineHook(0,0,0,0);
322 linestartts=timestamp*48+X6502_GetCycleCount();
326 static INLINE void Fixit1(void);
328 /* faking FCEUPPU_LineUpdate() from later versions of the emu */
329 static void FakedLineUpdate(void)
331 #define TOFIXNUM (272-0x4)
334 if (scanline >= 240) return;
336 if (tofix || sphitx != 0x100)
338 lastpixel = (timestamp*48-linestartts)>>4;
339 if (PAL) lastpixel += lastpixel>>4;
340 //printf("lastpixel: %i\n", lastpixel);
343 if (tofix && lastpixel>=TOFIXNUM)
355 for(x=sphitx;x<(sphitx+8) && x<l;x++)
357 if((sphitdata&(0x80>>(x-sphitx))) && !(Plinef[x]&64))
396 ret|=PPUGenLatch&0x1F;
400 //dprintf("r [2002] %02x",ret);
415 uint32 tmp=RefreshAddr&0x3FFF;
421 if(PPU_hook) PPU_hook(tmp);
422 PPUGenLatch=VRAMBuffer;
425 VRAMBuffer=VPage[tmp>>10][tmp];
429 VRAMBuffer=vnapage[(tmp>>10)&0x3][tmp&0x3FF];
432 if (INC32) RefreshAddr+=32;
434 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
435 dprintf("r [2007] %02x",ret);
457 //printf("$%04x:$%02x, %d\n",X.PC,V,scanline);
485 //printf("$%02x:$%02x\n",PPUSPL,V);
525 TempAddr|=(V&0x3f)<<8;
532 RefreshAddr=TempAddr;
534 PPU_hook(RefreshAddr);
542 uint32 tmp=RefreshAddr&0x3FFF;
548 PALRAM[0x00]=PALRAM[0x04]=PALRAM[0x08]=PALRAM[0x0C]=V&0x3f;
549 else if(tmp&3) PALRAM[(tmp&0x1f)]=V&0x3f;
553 if(PPUCHRRAM&(1<<(tmp>>10)))
554 VPage[tmp>>10][tmp]=V;
558 if(PPUNTARAM&(1<<((tmp&0xF00)>>10)))
559 vnapage[((tmp&0xF00)>>10)][tmp&0x3FF]=V;
561 if (INC32) RefreshAddr+=32;
563 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
572 B2004(0x2004,X.DB=ARead[t+x](t+x));
573 X6502_AddCycles(512);
576 void BGRender(uint8 *target)
579 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
585 RefreshLine(target-XOffset, vofs);
596 *(uint32 *)target=*(uint32 *)(target+4)=tem;
602 void FCEUI_FrameSkip(int x)
608 /* This is called at the beginning of each visible scanline */
609 static void LineUpdate(uint8 *target)
614 /* PRefreshLine() will not get called on skipped frames. This
615 * could cause a problem, but the solution would be rather complex,
616 * due to the current sprite 0 hit code.
621 if(scanline==y && SpriteON) PPU_status|=0x40; // hack
625 if(scanline < FSettings.FirstSLine || scanline > FSettings.LastSLine)
630 if(scanline==y && SpriteON) PPU_status|=0x40;
643 FCEU_dwmemset(target,tem,256);
647 if(InputScanlineHook)
648 InputScanlineHook(target,spork?sprlinebuf:0,linestartts,256);
652 static void LineUpdateEnd(uint8 *target)
655 if(ScreenON || SpriteON) // Yes, very el-cheapo.
658 block_and(target, 256, 0x30);
661 block_or(target, 256, 0xc0);
663 block_or(target, 256, 0x40);
665 block_andor(target, 256, 0x3f, 0x80);
669 if(ScreenON || SpriteON) // Yes, very el-cheapo.
674 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0x30303030;
680 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0xc0c0c0c0;
684 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])|0x40404040;
687 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0x80808080;
691 ((uint32 *)target)[-2]=((uint32 *)target)[-1]=0;
692 ((uint32 *)target)[64]=((uint32 *)target)[65]=0;
695 #define PAL(c) ((c)+cc)
698 static void PRefreshLine(void)
702 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
703 void (*PPU_hook_)(uint32 A) = PPU_hook;
711 zz2=(RefreshAddr>>10)&3;
712 PPU_hook_(0x2000|(RefreshAddr&0xFFF));
714 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
718 if((RefreshAddr&0x1f)==0x1f)
725 /* This high-level graphics MMC5 emulation code was written
726 for MMC5 carts in "CL" mode. It's probably not totally
727 correct for carts in "SL" mode.
729 static void RefreshLine_MMC5Hack1(uint8 *P, uint32 vofs)
733 tochange=MMC5HackSPMode&0x1F;
735 for(X1=33;X1;X1--,P+=8)
741 if((tochange<=0 && MMC5HackSPMode&0x40) ||
742 (tochange>0 && !(MMC5HackSPMode&0x40)))
747 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
748 if(ys>=0x1E) ys-=0x1E;
749 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
751 C = MMC5HackVROMPTR+vadr;
752 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
754 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
755 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
760 zz2=(RefreshAddr>>10)&3;
761 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
762 C = MMC5BGVRAMADR(vadr);
763 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
764 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
768 if((RefreshAddr&0x1f)==0x1f)
776 static void RefreshLine_MMC5Hack2(uint8 *P, uint32 vofs)
780 tochange=MMC5HackSPMode&0x1F;
782 for(X1=33;X1;X1--,P+=8)
789 if((tochange<=0 && MMC5HackSPMode&0x40) ||
790 (tochange>0 && !(MMC5HackSPMode&0x40)))
795 ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
796 if(ys>=0x1E) ys-=0x1E;
797 vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
799 C = MMC5HackVROMPTR+vadr;
800 C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
802 cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
803 cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
808 zz2=(RefreshAddr>>10)&3;
809 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
810 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
811 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
812 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
816 if((RefreshAddr&0x1f)==0x1f)
824 static void RefreshLine_MMC5Hack3(uint8 *P, uint32 vofs)
828 for(X1=33;X1;X1--,P+=8)
836 zz2=(RefreshAddr>>10)&3;
837 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
838 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
839 MMC5HackVROMMask) << 12) + (vadr & 0xfff);
840 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
844 if((RefreshAddr&0x1f)==0x1f)
851 static void RefreshLine_MMC5Hack4(uint8 *P, uint32 vofs)
855 for(X1=33;X1;X1--,P+=8)
862 zz2=(RefreshAddr>>10)&3;
863 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
864 C = MMC5BGVRAMADR(vadr);
865 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
866 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
870 if((RefreshAddr&0x1f)==0x1f)
877 static void RefreshLine_PPU_hook(uint8 *P, uint32 vofs)
880 void (*PPU_hook_)(uint32 A) = PPU_hook;
881 uint32 rfraddr = RefreshAddr;
882 uint8 *page = vnapage[(rfraddr>>10)&3];
884 for(X1=33;X1;X1--,P+=8)
891 PPU_hook_(0x2000|(rfraddr&0xFFF));
892 cc=page[0x3c0+(zz>>2)+((rfraddr&0x380)>>4)];
893 cc=((cc >> ((zz&2) + ((rfraddr&0x40)>>4))) &3) <<2;
894 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
901 if((rfraddr&0x1f)==0x1f) {
903 page = vnapage[(rfraddr>>10)&3];
907 RefreshAddr = rfraddr;
910 static void RefreshLine_normal(uint8 *P, uint32 vofs) // vofs is 0x107 max
913 uint32 rfraddr = RefreshAddr;
914 uint8 *page = vnapage[(rfraddr>>10)&3];
917 if ((rfraddr&0xc)!=0)
918 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
920 for (X1=33;X1;X1--,P+=8)
925 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
927 if ((rfraddr&0xc)==0)
928 cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
929 cc=((cc2 >> ((rfraddr&2) + ((rfraddr&0x40)>>4) + ((rfraddr&0xc)<<1))) & 3) << 2;
933 if((rfraddr&0x1f)==0x1f) {
935 page = vnapage[(rfraddr>>10)&3];
939 RefreshAddr = rfraddr;
942 static void SetRefreshLine(void)
944 if(MMC5Hack && geniestage!=1)
946 if(MMC5HackCHRMode==0 && (MMC5HackSPMode&0x80))
948 if (RefreshLine != RefreshLine_MMC5Hack1) printf("set refr RefreshLine_MMC5Hack1\n");
949 RefreshLine = RefreshLine_MMC5Hack1;
951 else if(MMC5HackCHRMode==1 && (MMC5HackSPMode&0x80))
953 if (RefreshLine != RefreshLine_MMC5Hack2) printf("set refr RefreshLine_MMC5Hack2\n");
954 RefreshLine = RefreshLine_MMC5Hack2;
956 else if(MMC5HackCHRMode==1)
958 if (RefreshLine != RefreshLine_MMC5Hack3) printf("set refr RefreshLine_MMC5Hack3\n");
959 RefreshLine = RefreshLine_MMC5Hack3;
963 if (RefreshLine != RefreshLine_MMC5Hack4) printf("set refr RefreshLine_MMC5Hack4\n");
964 RefreshLine = RefreshLine_MMC5Hack4;
966 } // End if(MMC5Hack)
969 if (RefreshLine != RefreshLine_PPU_hook) printf("set refr RefreshLine_PPU_hook\n");
970 RefreshLine = RefreshLine_PPU_hook;
974 if (RefreshLine != RefreshLine_normal) printf("set refr RefreshLine_normal\n");
975 RefreshLine = RefreshLine_normal;
982 if(ScreenON || SpriteON)
984 uint32 rad=RefreshAddr;
986 rad|=TempAddr&0x041f;
988 //PPU_hook(RefreshAddr,-1);
995 if(ScreenON || SpriteON)
997 uint32 rad=RefreshAddr;
999 if((rad&0x7000)==0x7000)
1002 if((rad&0x3E0)==0x3A0)
1009 if((rad&0x3E0)==0x3e0)
1017 //PPU_hook(RefreshAddr,-1);
1022 // ============================//
1024 // ===========================//
1026 void ResetMapping(void)
1030 SetReadHandler(0x0000,0xFFFF,ANull);
1031 SetWriteHandler(0x0000,0xFFFF,BNull);
1033 SetReadHandler(0,0x7FF,ARAML);
1034 SetWriteHandler(0,0x7FF,BRAML);
1036 SetReadHandler(0x800,0x1FFF,ARAMH); /* Part of a little */
1037 SetWriteHandler(0x800,0x1FFF,BRAMH); /* hack for a small speed boost. */
1039 for(x=0x2000;x<0x4000;x+=8)
1059 BWrite[0x4014]=B4014;
1065 void CloseGame(void)
1070 if(FCEUGameInfo.type!=GIT_NSF)
1071 FCEU_FlushGameCheats(0,0);
1073 if(FSettings.NetworkPlay) KillNetplay();
1075 GameInterface(GI_CLOSE, 0);
1081 void ResetGameLoaded(void)
1083 if(GameLoaded) CloseGame();
1086 GameHBIRQHook=GameHBIRQHook2=0;
1087 GameExpSound.Fill=0;
1088 GameExpSound.RChange=0;
1089 if(GameExpSound.Kill)
1090 GameExpSound.Kill();
1091 GameExpSound.Kill=0;
1097 FCEUGameInfo.name=0;
1098 FCEUGameInfo.type=GIT_CART;
1099 FCEUGameInfo.vidsys=GIV_USER;
1100 FCEUGameInfo.input[0]=FCEUGameInfo.input[1]=-1;
1101 FCEUGameInfo.inputfc=-1;
1103 FCEUGameInfo.soundchan=0;
1104 FCEUGameInfo.soundrate=0;
1105 FCEUGameInfo.cspecial=0;
1108 char lastLoadedGameName [2048];
1109 int LoadGameLastError = 0;
1110 int UNIFLoad(const char *name, int fp);
1111 int iNESLoad(const char *name, int fp);
1112 int FDSLoad(const char *name, int fp);
1113 int NSFLoad(int fp);
1115 FCEUGI *FCEUI_LoadGame(char *name)
1118 int have_movie = 0, have_ips = 0;
1122 LoadGameLastError = 0;
1125 strncpy(name2, name, sizeof(name2));
1126 name2[sizeof(name2)-1] = 0;
1128 fp=FCEU_fopen(name2,"rb");
1131 FCEU_PrintError("Error opening \"%s\"!",name);
1132 LoadGameLastError = 1;
1137 char *p = name2 + strlen(name2) - 4;
1138 if (strcasecmp(p, ".fcm") == 0) printf("movie detected\n"), have_movie = 1;
1139 if (strcasecmp(p, ".ips") == 0) printf("ips detected\n"), have_ips = 1;
1140 if (have_movie || have_ips)
1145 fp=FCEU_fopen(name2,"rb");
1146 if (!fp && p - name2 > 2)
1148 for (p--; p > name2 && *p != '.'; p--);
1150 fp=FCEU_fopen(name2,"rb");
1153 printf("no ROM for ips/movie\n");
1154 LoadGameLastError = 2;
1164 FILE *ips = fopen(name, "rb");
1166 fp=FCEU_fopen_forcemem(name2);
1167 if (!fp) { fclose(ips); return 0; }
1168 ApplyIPS(ips, fp); // closes ips
1172 if(iNESLoad(name2,fp))
1176 if(FDSLoad(name2,fp))
1178 if(UNIFLoad(name2,fp))
1181 FCEU_PrintError("An error occurred while loading the file.");
1183 // format handlers may set LoadGameLastError themselves.
1184 if (LoadGameLastError == 0) LoadGameLastError = 3;
1192 if(FCEUGameInfo.type!=GIT_NSF)
1193 if(FSettings.GameGenie)
1198 if(FSettings.NetworkPlay) InitNetplay();
1201 if(FCEUGameInfo.type!=GIT_NSF)
1203 FCEU_LoadGamePalette();
1204 FCEU_LoadGameCheats(0);
1207 FCEU_ResetPalette();
1211 FCEUI_LoadMovie(name, 1);
1213 strcpy(lastLoadedGameName, name2);
1215 return(&FCEUGameInfo);
1219 void FCEU_ResetVidSys(void)
1223 if(FCEUGameInfo.vidsys==GIV_NTSC)
1225 else if(FCEUGameInfo.vidsys==GIV_PAL)
1233 FSettings.FirstSLine=FSettings.UsrFirstSLine[1];
1234 FSettings.LastSLine=FSettings.UsrLastSLine[1];
1239 FSettings.FirstSLine=FSettings.UsrFirstSLine[0];
1240 FSettings.LastSLine=FSettings.UsrLastSLine[0];
1242 printf("ResetVidSys: PAL = %i\n", PAL);
1243 SetSoundVariables();
1246 int FCEUI_Initialize(void)
1248 if(!InitVirtualVideo())
1250 memset(&FSettings,0,sizeof(FSettings));
1251 FSettings.UsrFirstSLine[0]=8;
1252 FSettings.UsrFirstSLine[1]=0;
1253 FSettings.UsrLastSLine[0]=FSettings.UsrLastSLine[1]=239;
1254 FSettings.SoundVolume=100;
1256 FCEUI_Initialize098();
1257 FCEUI_SetEmuMode(0);
1262 void FCEUI_Kill(void)
1267 static void EmLoop(void);
1270 void (*ResetNES)(void) = 0;
1271 void (*PowerNES)(void) = 0;
1272 void (*FCEUI_Emulate)(void) = 0;
1274 void FCEUI_SetEmuMode(int is_new)
1276 use098code = is_new;
1279 ResetNES=ResetNES098;
1280 PowerNES=PowerNES098;
1281 SetSoundVariables=SetSoundVariables098;
1282 FCEUI_Emulate=FCEUI_Emulate098;
1286 ResetNES=ResetNES081;
1287 PowerNES=PowerNES081;
1288 SetSoundVariables=SetSoundVariables081;
1289 FCEUI_Emulate=EmLoop;
1293 void MMC5_hb(int); /* Ugh ugh ugh. */
1294 static void DoLine(void)
1296 uint8 *target=XBuf+scanline*320+32;
1300 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1304 // check: Battletoads & Double Dragon, Addams Family
1305 // sky glitches in SMB1 if done wrong
1311 if(scanline>=FSettings.FirstSLine && scanline<=FSettings.LastSLine)
1313 if(SpriteON && spork)
1314 CopySprites(target);
1316 LineUpdateEnd(target);
1320 if(ScreenON || SpriteON)
1324 if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
1330 X6502_Run(85-10-16);
1334 X6502_Run(6); // Tried 65, caused problems with Slalom(maybe others)
1341 if(GameHBIRQHook2 && (ScreenON || SpriteON))
1345 ResetRL(XBuf+scanline*320+32);
1350 static void EmLoop(void)
1355 uint32 scanlines_per_frame = PAL ? 312 : 262;
1357 FCEU_ApplyPeriodicCheats();
1360 if(ppudead) /* Needed for Knight Rider, possibly others. */
1362 //memset(XBuf, 0, 320*240);
1363 //X6502_Run(scanlines_per_frame*(256+85));
1365 for (lines=scanlines_per_frame;lines;lines--)
1374 PPU[3]=PPUSPL=0; /* Not sure if this is correct. According
1375 to Matt Conte and my own tests, it is. Timing is probably
1376 off, though. NOTE: Not having this here
1377 breaks a Super Donkey Kong game. */
1379 X6502_Run(12); /* I need to figure out the true nature and length
1382 if(FCEUGameInfo.type==GIT_NSF)
1387 // Note: this is needed for asm core
1388 // Warning: using 'scanline' var here breaks Castlevania III
1391 X6502_Run(256+85-12);
1392 for (lines=scanlines_per_frame-242-1;lines;lines--)
1395 // X6502_Run((scanlines_per_frame-242)*(256+85)-12);
1400 if(ScreenON || SpriteON)
1405 for(x=0;x<42;x++) {PPU_hook(0x2000); PPU_hook(0);} // ugh
1412 if(ScreenON || SpriteON)
1414 RefreshAddr=TempAddr;
1415 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
1424 if(FCEUGameInfo.type==GIT_NSF)
1426 // run scanlines for asm core to fuction
1427 for(scanline=0;scanline<240;scanline++)
1436 for(scanline=0;scanline<240;) // scanline is incremented in DoLine. Evil. :/
1441 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1442 for(x=1,max=0,maxref=0;x<7;x++)
1451 SetNESDeemph(maxref,0);
1464 ssize=FlushEmulateSound();
1466 timestampbase += timestamp;
1472 FCEU_PutImageDummy();
1474 FCEUD_Update(0,WaveFinalMono,ssize);
1480 FCEUD_Update(XBuf+8,WaveFinalMono,ssize);
1487 void FCEUI_CloseGame(void)
1492 static void ResetPPU(void)
1494 VRAMBuffer=PPU[0]=PPU[1]=PPU[2]=PPU[3]=0;
1497 RefreshAddr=TempAddr=0;
1503 static void PowerPPU(void)
1505 memset(NTARAM,0x00,0x800);
1506 memset(PALRAM,0x00,0x20);
1507 memset(SPRAM,0x00,0x100);
1511 void ResetNES081(void)
1513 if(!GameLoaded) return;
1514 GameInterface(GI_RESETM2, 0);
1520 static void FCEU_MemoryRand(uint8 *ptr, uint32 size)
1525 *ptr=(x&4)?0xFF:0x00;
1532 void PowerNES081(void)
1534 if(!GameLoaded) return;
1536 FCEU_CheatResetRAM();
1537 FCEU_CheatAddRAM(2,0,RAM);
1541 #ifndef DEBUG_ASM_6502
1542 FCEU_MemoryRand(RAM,0x800);
1544 memset(RAM,0x00,0x800);
1549 GameInterface(GI_POWER, 0);
1550 if(FCEUGameInfo.type==GIT_VSUNI)
1562 /* savestate stuff */
1563 uint16 TempAddrT,RefreshAddrT;
1565 SFORMAT FCEUPPU_STATEINFO[]={
1566 { NTARAM, 0x800, "NTAR"},
1567 { PALRAM, 0x20, "PRAM"},
1568 { SPRAM, 0x100, "SPRA"},
1569 { PPU, 0x4, "PPUR"},
1570 { &XOffset, 1, "XOFF"},
1571 { &vtoggle, 1, "VTOG"},
1572 { &RefreshAddrT, 2|RLSB, "RADD"},
1573 { &TempAddrT, 2|RLSB, "TADD"},
1574 { &VRAMBuffer, 1, "VBUF"},
1575 { &PPUGenLatch, 1, "PGEN"},
1577 { &kook, 1, "KOOK"},
1578 { &ppudead, 1, "DEAD"},
1579 { &PPUSPL, 1, "PSPL"},