SMB3, combo, fskip fixes
[fceu.git] / fce.c
1 /* FCE Ultra - NES/Famicom Emulator
2  *
3  * Copyright notice for this file:
4  *  Copyright (C) 1998 BERO
5  *  Copyright (C) 2002 Ben Parnell
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include        <string.h>
23 #include        <stdio.h>
24 #include        <stdlib.h>
25
26 #include        "types.h"
27 #include        "x6502.h"
28 #include        "fce.h"
29 #include        "fceu098.h"
30 #include        "sound.h"
31 #include        "svga.h"
32 #include        "netplay.h"
33 #include        "general.h"
34 #include        "endian.h"
35 #include        "version.h"
36 #include        "memory.h"
37
38 #include        "cart.h"
39 #include        "nsf.h"
40 #include        "fds.h"
41 #include        "ines.h"
42 #include        "unif.h"
43 #include        "vsuni.h"
44 #include        "cheat.h"
45
46 #include        "state.h"
47 #include        "video.h"
48 #include        "input.h"
49 #include        "file.h"
50 #include        "crc32.h"
51 #include        "ppu.h"
52 #include        "ppu098.h"
53
54 #include        "palette.h"
55 #include        "movie.h"
56
57 #include        "dprintf.h"
58
59 #ifdef GP2X
60 #include        "drivers/gp2x/asmutils.h"
61 #endif
62
63 #define Pal     (PALRAM)
64
65
66 static void (*RefreshLine)(uint8 *P, uint32 vofs) = NULL;
67 static void PRefreshLine(void);
68
69 static void ResetPPU(void);
70 static void PowerPPU(void);
71
72 uint64 timestampbase=0;
73
74 int ppudead=1;
75 int kook=0;
76
77 int MMC5Hack;
78 uint32 MMC5HackVROMMask;
79 uint8 *MMC5HackExNTARAMPtr;
80 uint8 *MMC5HackVROMPTR;
81 uint8 MMC5HackCHRMode=0;
82 uint8 MMC5HackSPMode;
83 uint8 MMC5HackSPScroll;
84 uint8 MMC5HackSPPage;
85
86 uint8 *MMC5SPRVPage[8];
87 uint8 *MMC5BGVPage[8];
88
89
90 uint8 VRAMBuffer,PPUGenLatch;
91
92 uint8 *vnapage[4];
93 uint8 PPUNTARAM;
94 uint8 PPUCHRRAM;
95
96 /* Color deemphasis emulation.  Joy... */
97 static uint8 deemp=0;
98 static int deempcnt[8];
99
100 FCEUGI FCEUGameInfo;
101 void (*GameInterface)(int h, void *param);
102
103 void FP_FASTAPASS(1) (*PPU_hook)(uint32 A);
104
105 void (*GameStateRestore)(int version);
106 void (*GameHBIRQHook)(void), (*GameHBIRQHook2)(void);
107
108 readfunc ARead[0x10000];
109 writefunc BWrite[0x10000];
110 static readfunc *AReadG;
111 static writefunc *BWriteG;
112 static int RWWrap=0;
113
114 #ifdef ASM_6502
115 #ifdef DEBUG_ASM_6502
116 extern uint8  nes_internal_ram[0x800];
117 #else
118 static void asmcpu_update(int32 cycles)
119 {
120  // some code from x6502.c
121  fhcnt-=cycles;
122  if(fhcnt<=0)
123  {
124   FrameSoundUpdate();
125   fhcnt+=fhinc;
126  }
127
128  if(PCMIRQCount>0)
129  {
130   PCMIRQCount-=cycles;
131   if(PCMIRQCount<=0)
132   {
133    vdis=1;
134    if((PSG[0x10]&0x80) && !(PSG[0x10]&0x40))
135    {
136     extern uint8 SIRQStat;
137     SIRQStat|=0x80;
138     X6502_IRQBegin(FCEU_IQDPCM);
139    }
140   }
141  }
142 }
143 #endif
144
145 void asmcpu_unpack(void)
146 {
147         nes_registers[0] = X.A << 24;
148         nes_registers[1] = X.X;
149         nes_registers[2] = X.Y;
150         pc_base = 0;
151         nes_registers[3] = X.PC;
152         X6502_Rebase_a();
153         nes_registers[4] = X.S << 24;
154         nes_registers[4]|= X.IRQlow << 8;
155         if (MapIRQHook)
156                 nes_registers[4] |= 1<<16; // MapIRQHook set bit
157         nes_registers[7] = (uint32)X.count << 16;
158
159         // NVUB DIZC
160         nes_registers[4]|= X.P & 0x5d;
161         nes_registers[5] = X.P << 24; // N
162         if (!(X.P&0x02)) nes_registers[5] |= 1; // Z
163
164 #ifdef DEBUG_ASM_6502
165         memcpy(nes_internal_ram, RAM, 0x800);
166 #endif
167 }
168
169 void asmcpu_pack(void)
170 {
171         X.A = nes_registers[0] >> 24;
172         X.X = nes_registers[1];
173         X.Y = nes_registers[2];
174         X.PC= nes_registers[3] - pc_base;
175         X.S = nes_registers[4] >> 24;
176         X.IRQlow = nes_registers[4] >> 8;
177         X.count = (int32) nes_registers[7] >> 16;
178
179         // NVUB DIZC
180         X.P = nes_registers[4] & 0x5d;
181         if (  nes_registers[5]&0x80000000)  X.P |= 0x80; // N
182         if (!(nes_registers[5]&0x000000ff)) X.P |= 0x02; // Z
183 }
184 #endif
185
186 DECLFW(BNull)
187 {
188
189 }
190
191 DECLFR(ANull)
192 {
193  //printf("open [%04x] %02x @ %04x (%04x)\n", A, X.DB, X.PC, X.PC&0x7ff);
194  return(X.DB);
195 }
196
197 int AllocGenieRW(void)
198 {
199  if(!(AReadG=FCEU_malloc(0x8000*sizeof(readfunc))))
200   return 0;
201  if(!(BWriteG=FCEU_malloc(0x8000*sizeof(writefunc))))
202   return 0;
203  RWWrap=1;
204  return 1;
205 }
206
207 void FlushGenieRW(void)
208 {
209  int32 x;
210
211  if(RWWrap)
212  {
213   for(x=0;x<0x8000;x++)
214   {
215    ARead[x+0x8000]=AReadG[x];
216    BWrite[x+0x8000]=BWriteG[x];
217   }
218 #ifdef ASM_6502
219   GenieSetPages(1);
220 #endif
221   free(AReadG);
222   free(BWriteG);
223   AReadG=0;
224   BWriteG=0;
225   RWWrap=0;
226  }
227 }
228
229 readfunc FASTAPASS(1) GetReadHandler(int32 a)
230 {
231   if(a>=0x8000 && RWWrap)
232    return AReadG[a-0x8000];
233   else
234    return ARead[a];
235 }
236
237 void FASTAPASS(3) SetReadHandler(int32 start, int32 end, readfunc func)
238 {
239   int32 x;
240
241   if(!func)
242    func=ANull;
243
244   if(RWWrap)
245    for(x=end;x>=start;x--)
246    {
247     if(x>=0x8000)
248      AReadG[x-0x8000]=func;
249     else
250      ARead[x]=func;
251    }
252   else
253
254    for(x=end;x>=start;x--)
255     ARead[x]=func;
256 }
257
258 writefunc FASTAPASS(1) GetWriteHandler(int32 a)
259 {
260   if(RWWrap && a>=0x8000)
261    return BWriteG[a-0x8000];
262   else
263    return BWrite[a];
264 }
265
266 void FASTAPASS(3) SetWriteHandler(int32 start, int32 end, writefunc func)
267 {
268   int32 x;
269
270   if(!func)
271    func=BNull;
272
273   if(RWWrap)
274    for(x=end;x>=start;x--)
275    {
276     if(x>=0x8000)
277      BWriteG[x-0x8000]=func;
278     else
279      BWrite[x]=func;
280    }
281   else
282    for(x=end;x>=start;x--)
283     BWrite[x]=func;
284 }
285
286 uint8 vtoggle=0;
287 uint8 XOffset=0;
288
289 uint32 TempAddr,RefreshAddr;
290
291
292 /* scanline is equal to the current visible scanline we're on. */
293
294 int scanline;
295
296 uint8 GameMemBlock[131072] __attribute__ ((aligned (4)));
297 uint8 NTARAM[0x800] __attribute__ ((aligned (4)));
298 uint8 PALRAM[0x20] __attribute__ ((aligned (4)));
299 #if !defined(ASM_6502) || defined(DEBUG_ASM_6502)
300 uint8 RAM[0x800] __attribute__ ((aligned (4)));
301 #endif
302
303 uint8 PPU[4];
304 uint8 PPUSPL;
305
306 uint8 PAL=0;
307
308
309 #define MMC5BGVRAMADR(V)      &MMC5BGVPage[(V)>>10][(V)]
310 #define VRAMADR(V)      &VPage[(V)>>10][(V)]
311
312 static int linestartts;
313 static int tofix=0;
314
315 static uint8 *Plinef;
316
317 extern uint8 sprlinebuf[256+8];
318 extern int32 sphitx;
319 extern uint8 sphitdata;
320
321 extern int spork;       /* spork the world.  Any sprites on this line?
322                            Then this will be set to 1.  Needed for zapper
323                            emulation and *gasp* sprite emulation.
324                         */
325
326 static void ResetRL(uint8 *target)
327 {
328  if(InputScanlineHook)
329   InputScanlineHook(0,0,0,0);
330  Plinef=target;
331  linestartts=timestamp*48+X6502_GetCycleCount();
332  tofix=1;
333 }
334
335 static INLINE void Fixit1(void);
336
337 /* faking FCEUPPU_LineUpdate() from later versions of the emu */
338 static void FakedLineUpdate(void)
339 {
340  #define TOFIXNUM (272-0x4)
341  int lastpixel;
342
343  if (scanline >= 240) return;
344
345  if (tofix || sphitx != 0x100)
346  {
347   lastpixel = (timestamp*48-linestartts)>>4;
348   if (PAL) lastpixel += lastpixel>>4;
349   //printf("lastpixel: %i\n", lastpixel);
350  }
351
352  if (tofix && lastpixel>=TOFIXNUM)
353  {
354   Fixit1();
355   tofix=0;
356  }
357
358  // CheckSpriteHit()
359  if(sphitx!=0x100)
360  {
361   int l=lastpixel-16;
362   int x;
363
364   for(x=sphitx;x<(sphitx+8) && x<l;x++)
365   {
366    if((sphitdata&(0x80>>(x-sphitx))) && !(Plinef[x]&64))
367    {
368     PPU_status|=0x40;
369     sphitx=0x100;
370     break;
371    }
372   }
373  }
374 }
375
376
377 static DECLFW(BRAML)
378 {
379         RAM[A]=V;
380 }
381
382 static DECLFW(BRAMH)
383 {
384         RAM[A&0x7FF]=V;
385 }
386
387 static DECLFR(ARAML)
388 {
389         return RAM[A];
390 }
391
392 static DECLFR(ARAMH)
393 {
394         return RAM[A&0x7FF];
395 }
396
397
398 static DECLFR(A2002)
399 {
400         /* merged */
401                         uint8 ret;
402
403                         FakedLineUpdate();
404                         ret = PPU_status;
405                         ret|=PPUGenLatch&0x1F;
406                         vtoggle=0;
407                         PPU_status&=0x7F;
408                          PPUGenLatch=ret;
409                         //dprintf("r [2002] %02x",ret);
410                         return ret;
411 }
412
413 static DECLFR(A200x)
414 {
415         /* merged */
416                         FakedLineUpdate();
417                         return PPUGenLatch;
418 }
419
420 static DECLFR(A2007)
421 {
422         /* merged */
423                         uint8 ret;
424                         uint32 tmp=RefreshAddr&0x3FFF;
425
426                         FakedLineUpdate();
427
428                         ret=VRAMBuffer;
429
430                         if(PPU_hook) PPU_hook(tmp);
431                         PPUGenLatch=VRAMBuffer;
432                         if(tmp<0x2000)
433                         {
434                          VRAMBuffer=VPage[tmp>>10][tmp];
435                         }
436                         else
437                         {
438                          VRAMBuffer=vnapage[(tmp>>10)&0x3][tmp&0x3FF];
439                         }
440
441                         if (INC32) RefreshAddr+=32;
442                         else RefreshAddr++;
443                         if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
444                         dprintf("r [2007] %02x",ret);
445                         return ret;
446 }
447
448 static DECLFW(B2000)
449 {
450         /* NMI2? */
451                 FakedLineUpdate();
452                 PPUGenLatch=V;
453                 PPU[0]=V;
454                 TempAddr&=0xF3FF;
455                 TempAddr|=(V&3)<<10;
456 }
457
458 static DECLFW(B2001)
459 {
460         /* merged */
461                   FakedLineUpdate();
462                   PPUGenLatch=V;
463                   PPU[1]=V;
464                   if(V&0xE0)
465                    deemp=V>>5;
466                   //printf("$%04x:$%02x, %d\n",X.PC,V,scanline);
467 }
468
469 static DECLFW(B2002)
470 {
471         /* merged */
472                  PPUGenLatch=V;
473 }
474
475 static DECLFW(B2003)
476 {
477         /* merged */
478                 PPUGenLatch=V;
479                 PPU[3]=V;
480                 PPUSPL=V&0x7;
481 }
482
483 static DECLFW(B2004)
484 {
485         /* merged */
486                 PPUGenLatch=V;
487                 if(PPUSPL>=8)
488                 {
489                  if(PPU[3]>=8)
490                   SPRAM[PPU[3]]=V;
491                 }
492                 else
493                 {
494                  //printf("$%02x:$%02x\n",PPUSPL,V);
495                  SPRAM[PPUSPL]=V;
496                 }
497                 PPU[3]++;
498                 PPUSPL++;
499
500 }
501
502 static DECLFW(B2005)
503 {
504         /* merged */
505                 uint32 tmp=TempAddr;
506                 FakedLineUpdate();
507                 PPUGenLatch=V;
508                 if (!vtoggle)
509                 {
510                  tmp&=0xFFE0;
511                  tmp|=V>>3;
512                  XOffset=V&7;
513                 }
514                 else
515                 {
516                  tmp&=0x8C1F;
517                  tmp|=((V&~0x7)<<2);
518                  tmp|=(V&7)<<12;
519                 }
520
521                 TempAddr=tmp;
522                 vtoggle^=1;
523 }
524
525 static DECLFW(B2006)
526 {
527         /* merged */
528                        FakedLineUpdate();
529
530                        PPUGenLatch=V;
531                        if(!vtoggle)
532                        {
533                         TempAddr&=0x00FF;
534                         TempAddr|=(V&0x3f)<<8;
535                        }
536                        else
537                        {
538                         TempAddr&=0xFF00;
539                         TempAddr|=V;
540
541                         RefreshAddr=TempAddr;
542                         if(PPU_hook)
543                          PPU_hook(RefreshAddr);
544                        }
545                       vtoggle^=1;
546 }
547
548 static DECLFW(B2007)
549 {
550         /* merged */
551                         uint32 tmp=RefreshAddr&0x3FFF;
552                         PPUGenLatch=V;
553                         if(tmp>=0x3F00)
554                         {
555                          // hmmm....
556                          if(!(tmp&0xf))
557                           PALRAM[0x00]=PALRAM[0x04]=PALRAM[0x08]=PALRAM[0x0C]=V&0x3f;
558                          else if(tmp&3) PALRAM[(tmp&0x1f)]=V&0x3f;
559                         }
560                         else if(tmp<0x2000)
561                         {
562                           if(PPUCHRRAM&(1<<(tmp>>10)))
563                             VPage[tmp>>10][tmp]=V;
564                         }
565                         else
566                         {
567                          if(PPUNTARAM&(1<<((tmp&0xF00)>>10)))
568                           vnapage[((tmp&0xF00)>>10)][tmp&0x3FF]=V;
569                         }
570                         if (INC32) RefreshAddr+=32;
571                         else RefreshAddr++;
572                         if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
573 }
574
575 static DECLFW(B4014)
576 {
577         uint32 t=V<<8;
578         int x;
579
580         for(x=0;x<256;x++)
581          B2004(0x2004,X.DB=ARead[t+x](t+x));
582         X6502_AddCycles(512);
583 }
584
585 void BGRender(uint8 *target)
586 {
587         uint32 tem, vofs;
588         vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
589
590         Pal[0]|=64;
591         Pal[4]|=64;
592         Pal[8]|=64;
593         Pal[0xC]|=64;
594         RefreshLine(target-XOffset, vofs);
595         Pal[0]&=63;
596         Pal[4]&=63;
597         Pal[8]&=63;
598         Pal[0xC]&=63;
599
600         if(!(PPU[1]&2))
601         {
602          tem=Pal[0]|0x40;
603          tem|=tem<<8;
604          tem|=tem<<16;
605          *(uint32 *)target=*(uint32 *)(target+4)=tem;
606         }
607 }
608
609 #ifdef FRAMESKIP
610 int FSkip=0;
611 void FCEUI_FrameSkip(int x)
612 {
613  FSkip=x;
614 }
615 #endif
616
617 /*      This is called at the beginning of each visible scanline */
618 static void LineUpdate(uint8 *target)
619 {
620         uint32 tem;
621         int y;
622
623         /* PRefreshLine() will not get called on skipped frames.  This
624          * could cause a problem, but the solution would be rather complex,
625          * due to the current sprite 0 hit code.
626          */
627         if(FSkip)
628         {
629          y=(int)SPRAM[0] + 1;
630          if(scanline==y && SpriteON) PPU_status|=0x40; // hack
631          return;
632         }
633
634         if(scanline < FSettings.FirstSLine || scanline > FSettings.LastSLine)
635         {
636            if(PPU_hook)
637             PRefreshLine();
638            y=(int)SPRAM[0] + 1;
639            if(scanline==y && SpriteON) PPU_status|=0x40;
640         }
641         else
642         {
643          if(ScreenON)
644          {
645            BGRender(target);
646          }
647          else
648          {
649            tem=Pal[0]|0x40;
650            tem|=tem << 8;
651            tem|=tem << 16;
652            FCEU_dwmemset(target,tem,256);
653          }
654         }
655
656         if(InputScanlineHook)
657          InputScanlineHook(target,spork?sprlinebuf:0,linestartts,256);
658 }
659
660
661 static void LineUpdateEnd(uint8 *target)
662 {
663 #ifdef GP2X
664  if(ScreenON || SpriteON)  // Yes, very el-cheapo.
665  {
666   if(PPU[1]&0x01)
667    block_and(target, 256, 0x30);
668  }
669  if((PPU[1]>>5)==0x7)
670   block_or(target, 256, 0xc0);
671  else if(PPU[1]&0xE0)
672   block_or(target, 256, 0x40);
673  else
674   block_andor(target, 256, 0x3f, 0x80);
675 #else
676  int x;
677
678  if(ScreenON || SpriteON)  // Yes, very el-cheapo.
679  {
680   if(PPU[1]&0x01)
681   {
682    for(x=63;x>=0;x--)
683    *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0x30303030;
684   }
685  }
686  if((PPU[1]>>5)==0x7)
687  {
688   for(x=63;x>=0;x--)
689    *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0xc0c0c0c0;
690  }
691  else if(PPU[1]&0xE0)
692   for(x=63;x>=0;x--)
693    *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])|0x40404040;
694  else
695   for(x=63;x>=0;x--)
696    *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0x80808080;
697 #endif
698
699  // black borders
700  ((uint32 *)target)[-2]=((uint32 *)target)[-1]=0;
701  ((uint32 *)target)[64]=((uint32 *)target)[65]=0;
702 }
703
704 #define PAL(c)  ((c)+cc)
705
706
707 static void PRefreshLine(void)
708 {
709          uint32 vofs;
710          int X1;
711          vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
712          void (*PPU_hook_)(uint32 A) = PPU_hook;
713
714
715          for(X1=33;X1;X1--)
716          {
717                 uint32 zz2;
718                 uint32 vadr;
719
720                 zz2=(RefreshAddr>>10)&3;
721                 PPU_hook_(0x2000|(RefreshAddr&0xFFF));
722
723                 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
724
725                 PPU_hook_(vadr);
726
727                 if((RefreshAddr&0x1f)==0x1f)
728                  RefreshAddr^=0x41F;
729                 else
730                  RefreshAddr++;
731          }
732 }
733
734 /* This high-level graphics MMC5 emulation code was written
735    for MMC5 carts in "CL" mode.  It's probably not totally
736    correct for carts in "SL" mode.
737    */
738 static void RefreshLine_MMC5Hack1(uint8 *P, uint32 vofs)
739 {
740           int8 tochange, X1;
741
742           tochange=MMC5HackSPMode&0x1F;
743
744           for(X1=33;X1;X1--,P+=8)
745           {
746                 uint8 *C;
747                 uint8 cc,zz,zz2;
748                 uint32 vadr;
749
750                 if((tochange<=0 && MMC5HackSPMode&0x40) ||
751                    (tochange>0 && !(MMC5HackSPMode&0x40)))
752                 {
753                  uint8 xs,ys;
754
755                  xs=33-X1;
756                  ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
757                  if(ys>=0x1E) ys-=0x1E;
758                  vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
759
760                  C = MMC5HackVROMPTR+vadr;
761                  C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
762
763                  cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
764                  cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
765                 }
766                 else
767                 {
768                  zz=RefreshAddr&0x1F;
769                  zz2=(RefreshAddr>>10)&3;
770                  vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
771                  C = MMC5BGVRAMADR(vadr);
772                  cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
773                  cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
774                 }
775                 #include "fceline.h"
776
777                 if((RefreshAddr&0x1f)==0x1f)
778                  RefreshAddr^=0x41F;
779                 else
780                  RefreshAddr++;
781                 tochange--;
782           }
783 }
784
785 static void RefreshLine_MMC5Hack2(uint8 *P, uint32 vofs)
786 {
787           int8 tochange, X1;
788
789           tochange=MMC5HackSPMode&0x1F;
790
791           for(X1=33;X1;X1--,P+=8)
792           {
793                 uint8 *C;
794                 uint8 cc;
795                 uint8 zz2;
796                 uint32 vadr;
797
798                 if((tochange<=0 && MMC5HackSPMode&0x40) ||
799                    (tochange>0 && !(MMC5HackSPMode&0x40)))
800                 {
801                  uint8 xs,ys;
802
803                  xs=33-X1;
804                  ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
805                  if(ys>=0x1E) ys-=0x1E;
806                  vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
807
808                  C = MMC5HackVROMPTR+vadr;
809                  C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
810
811                  cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
812                  cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
813                 }
814                 else
815                 {
816                  C=MMC5HackVROMPTR;
817                  zz2=(RefreshAddr>>10)&3;
818                  vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
819                  C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
820                          MMC5HackVROMMask) << 12) + (vadr & 0xfff);
821                  vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
822                  cc = vadr;
823                 }
824                 #include "fceline.h"
825                 if((RefreshAddr&0x1f)==0x1f)
826                  RefreshAddr^=0x41F;
827                 else
828                  RefreshAddr++;
829                 tochange--;
830           }
831 }
832
833 static void RefreshLine_MMC5Hack3(uint8 *P, uint32 vofs)
834 {
835           int8 X1;
836
837           for(X1=33;X1;X1--,P+=8)
838           {
839                 uint8 *C;
840                 uint8 cc;
841                 uint8 zz2;
842                 uint32 vadr;
843
844                 C=MMC5HackVROMPTR;
845                 zz2=(RefreshAddr>>10)&3;
846                 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
847                 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
848                         MMC5HackVROMMask) << 12) + (vadr & 0xfff);
849                 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
850                 cc = vadr;
851
852                 #include "fceline.h"
853                 if((RefreshAddr&0x1f)==0x1f)
854                  RefreshAddr^=0x41F;
855                 else
856                  RefreshAddr++;
857           }
858 }
859
860 static void RefreshLine_MMC5Hack4(uint8 *P, uint32 vofs)
861 {
862           int8 X1;
863
864           for(X1=33;X1;X1--,P+=8)
865           {
866                 uint8 *C;
867                 uint8 cc,zz,zz2;
868                 uint32 vadr;
869
870                 zz=RefreshAddr&0x1F;
871                 zz2=(RefreshAddr>>10)&3;
872                 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
873                 C = MMC5BGVRAMADR(vadr);
874                 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
875                 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
876
877                 #include "fceline.h"
878
879                 if((RefreshAddr&0x1f)==0x1f)
880                  RefreshAddr^=0x41F;
881                 else
882                  RefreshAddr++;
883           }
884 }
885
886 static void RefreshLine_PPU_hook(uint8 *P, uint32 vofs)
887 {
888          int8 X1;
889          void (*PPU_hook_)(uint32 A) = PPU_hook;
890          uint32 rfraddr = RefreshAddr;
891          uint8 *page = vnapage[(rfraddr>>10)&3];
892
893          for(X1=33;X1;X1--,P+=8)
894          {
895                 uint8 *C;
896                 uint8 cc,zz;
897                 uint32 vadr;
898
899                 zz=rfraddr&0x1F;
900                 PPU_hook_(0x2000|(rfraddr&0xFFF));
901                 cc=page[0x3c0+(zz>>2)+((rfraddr&0x380)>>4)];
902                 cc=((cc >> ((zz&2) + ((rfraddr&0x40)>>4))) &3) <<2;
903                 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
904                 C = VRAMADR(vadr);
905
906                 #include "fceline.h"
907
908                 PPU_hook_(vadr);
909
910                 if((rfraddr&0x1f)==0x1f) {
911                  rfraddr^=0x41F;
912                  page = vnapage[(rfraddr>>10)&3];
913                 } else
914                  rfraddr++;
915          }
916          RefreshAddr = rfraddr;
917 }
918
919 static void RefreshLine_normal(uint8 *P, uint32 vofs) // vofs is 0x107 max
920 {
921          int8 X1;
922          uint32 rfraddr = RefreshAddr;
923          uint8 *page = vnapage[(rfraddr>>10)&3];
924          uint32 cc2=0;
925
926          if ((rfraddr&0xc)!=0)
927           cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
928
929          for (X1=33;X1;X1--,P+=8)
930          {
931                 uint8 cc,*C;
932                 uint32 vadr;
933
934                 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
935                 C = VRAMADR(vadr);
936                 if ((rfraddr&0xc)==0)
937                  cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
938                 cc=((cc2 >> ((rfraddr&2) + ((rfraddr&0x40)>>4) + ((rfraddr&0xc)<<1))) & 3) << 2;
939
940                 #include "fceline.h"
941
942                 if((rfraddr&0x1f)==0x1f) {
943                  rfraddr^=0x41F;
944                  page = vnapage[(rfraddr>>10)&3];
945                 } else
946                  rfraddr++;
947          }
948          RefreshAddr = rfraddr;
949 }
950
951 static void SetRefreshLine(void)
952 {
953         if(MMC5Hack && geniestage!=1)
954         {
955          if(MMC5HackCHRMode==0 && (MMC5HackSPMode&0x80))
956          {
957                  if (RefreshLine != RefreshLine_MMC5Hack1) printf("set refr RefreshLine_MMC5Hack1\n");
958                  RefreshLine = RefreshLine_MMC5Hack1;
959          }
960          else if(MMC5HackCHRMode==1 && (MMC5HackSPMode&0x80))
961          {
962                 if (RefreshLine != RefreshLine_MMC5Hack2) printf("set refr RefreshLine_MMC5Hack2\n");
963                  RefreshLine = RefreshLine_MMC5Hack2;
964          }
965          else if(MMC5HackCHRMode==1)
966          {
967                 if (RefreshLine != RefreshLine_MMC5Hack3) printf("set refr RefreshLine_MMC5Hack3\n");
968                  RefreshLine = RefreshLine_MMC5Hack3;
969          }
970          else
971          {
972                 if (RefreshLine != RefreshLine_MMC5Hack4) printf("set refr RefreshLine_MMC5Hack4\n");
973                  RefreshLine = RefreshLine_MMC5Hack4;
974          }
975         }       // End if(MMC5Hack)
976         else if(PPU_hook)
977         {
978                 if (RefreshLine != RefreshLine_PPU_hook) printf("set refr RefreshLine_PPU_hook\n");
979                 RefreshLine = RefreshLine_PPU_hook;
980         }
981         else
982         {
983                 if (RefreshLine != RefreshLine_normal) printf("set refr RefreshLine_normal\n");
984                 RefreshLine = RefreshLine_normal;
985         }
986 }
987
988 static INLINE
989 void Fixit2(void)
990 {
991    if(ScreenON || SpriteON)
992    {
993     uint32 rad=RefreshAddr;
994     rad&=0xFBE0;
995     rad|=TempAddr&0x041f;
996     RefreshAddr=rad;
997     //PPU_hook(RefreshAddr,-1);
998    }
999 }
1000
1001 static INLINE
1002 void Fixit1(void)
1003 {
1004    if(ScreenON || SpriteON)
1005    {
1006     uint32 rad=RefreshAddr;
1007
1008     if((rad&0x7000)==0x7000)
1009     {
1010      rad^=0x7000;
1011      if((rad&0x3E0)==0x3A0)
1012      {
1013       rad^=0x3A0;
1014       rad^=0x800;
1015      }
1016      else
1017      {
1018       if((rad&0x3E0)==0x3e0)
1019        rad^=0x3e0;
1020       else rad+=0x20;
1021      }
1022     }
1023     else
1024      rad+=0x1000;
1025     RefreshAddr=rad;
1026     //PPU_hook(RefreshAddr,-1);
1027    }
1028 }
1029
1030
1031 // ============================//
1032 // end of new code
1033 // ===========================//
1034
1035 void ResetMapping(void)
1036 {
1037         int x;
1038
1039         SetReadHandler(0x0000,0xFFFF,ANull);
1040         SetWriteHandler(0x0000,0xFFFF,BNull);
1041
1042         SetReadHandler(0,0x7FF,ARAML);
1043         SetWriteHandler(0,0x7FF,BRAML);
1044
1045         SetReadHandler(0x800,0x1FFF,ARAMH);  /* Part of a little */
1046         SetWriteHandler(0x800,0x1FFF,BRAMH); /* hack for a small speed boost. */
1047
1048         for(x=0x2000;x<0x4000;x+=8)
1049         {
1050          ARead[x]=A200x;
1051          BWrite[x]=B2000;
1052          ARead[x+1]=A200x;
1053          BWrite[x+1]=B2001;
1054          ARead[x+2]=A2002;
1055          BWrite[x+2]=B2002;
1056          ARead[x+3]=A200x;
1057          BWrite[x+3]=B2003;
1058          ARead[x+4]=A200x;
1059          BWrite[x+4]=B2004;
1060          ARead[x+5]=A200x;
1061          BWrite[x+5]=B2005;
1062          ARead[x+6]=A200x;
1063          BWrite[x+6]=B2006;
1064          ARead[x+7]=A2007;
1065          BWrite[x+7]=B2007;
1066         }
1067
1068         BWrite[0x4014]=B4014;
1069         SetNESSoundMap();
1070         InitializeInput();
1071 }
1072
1073 int GameLoaded=0;
1074 void CloseGame(void)
1075 {
1076  FCEUI_StopMovie();
1077  if(GameLoaded)
1078  {
1079   if(FCEUGameInfo.type!=GIT_NSF)
1080    FCEU_FlushGameCheats(0,0);
1081   #ifdef NETWORK
1082   if(FSettings.NetworkPlay) KillNetplay();
1083   #endif
1084   GameInterface(GI_CLOSE, 0);
1085   CloseGenie();
1086   GameLoaded=0;
1087  }
1088 }
1089
1090 void ResetGameLoaded(void)
1091 {
1092         if(GameLoaded) CloseGame();
1093         GameStateRestore=0;
1094         PPU_hook=0;
1095         GameHBIRQHook=GameHBIRQHook2=0;
1096         GameExpSound.Fill=0;
1097         GameExpSound.RChange=0;
1098         if(GameExpSound.Kill)
1099          GameExpSound.Kill();
1100         GameExpSound.Kill=0;
1101         MapIRQHook=0;
1102         MMC5Hack=0;
1103         PAL&=1;
1104         pale=0;
1105
1106         FCEUGameInfo.name=0;
1107         FCEUGameInfo.type=GIT_CART;
1108         FCEUGameInfo.vidsys=GIV_USER;
1109         FCEUGameInfo.input[0]=FCEUGameInfo.input[1]=-1;
1110         FCEUGameInfo.inputfc=-1;
1111
1112         FCEUGameInfo.soundchan=0;
1113         FCEUGameInfo.soundrate=0;
1114         FCEUGameInfo.cspecial=0;
1115 }
1116
1117 char lastLoadedGameName [2048];
1118 int LoadGameLastError = 0;
1119 int UNIFLoad(const char *name, int fp);
1120 int iNESLoad(const char *name, int fp);
1121 int FDSLoad(const char *name, int fp);
1122 int NSFLoad(int fp);
1123
1124 FCEUGI *FCEUI_LoadGame(char *name)
1125 {
1126         char name2[512];
1127         int have_movie = 0, have_ips = 0;
1128         int fp;
1129
1130         //Exit=1;
1131         LoadGameLastError = 0;
1132         ResetGameLoaded();
1133
1134         strncpy(name2, name, sizeof(name2));
1135         name2[sizeof(name2)-1] = 0;
1136
1137         fp=FCEU_fopen(name2,"rb");
1138         if(!fp)
1139         {
1140          FCEU_PrintError("Error opening \"%s\"!",name);
1141          LoadGameLastError = 1;
1142          return 0;
1143         }
1144
1145         {
1146          char *p = name2 + strlen(name2) - 4;
1147          if (strcasecmp(p, ".fcm") == 0) printf("movie detected\n"), have_movie = 1;
1148          if (strcasecmp(p, ".ips") == 0) printf("ips detected\n"), have_ips = 1;
1149          if (have_movie || have_ips)
1150          {
1151           // movie detected
1152           FCEU_fclose(fp);
1153           *p = 0;
1154           fp=FCEU_fopen(name2,"rb");
1155           if (!fp && p - name2 > 2)
1156           {
1157            for (p--; p > name2 && *p != '.'; p--);
1158            *p = 0;
1159            fp=FCEU_fopen(name2,"rb");
1160           }
1161           if (!fp) {
1162            printf("no ROM for ips/movie\n");
1163            LoadGameLastError = 2;
1164            return 0;
1165           }
1166          }
1167         }
1168
1169         // do IPS patch
1170         if (have_ips)
1171         {
1172          FCEU_fclose(fp);
1173          FILE *ips = fopen(name, "rb");
1174          if (!ips) return 0;
1175          fp=FCEU_fopen_forcemem(name2);
1176          if (!fp) { fclose(ips); return 0; }
1177          ApplyIPS(ips, fp); // closes ips
1178         }
1179
1180         GetFileBase(name2);
1181         if(iNESLoad(name2,fp))
1182          goto endlseq;
1183         if(NSFLoad(fp))
1184          goto endlseq;
1185         if(FDSLoad(name2,fp))
1186          goto endlseq;
1187         if(UNIFLoad(name2,fp))
1188          goto endlseq;
1189
1190         FCEU_PrintError("An error occurred while loading the file.");
1191         FCEU_fclose(fp);
1192         // format handlers may set LoadGameLastError themselves.
1193         if (LoadGameLastError == 0) LoadGameLastError = 3;
1194         return 0;
1195
1196         endlseq:
1197         FCEU_fclose(fp);
1198         GameLoaded=1;
1199
1200         FCEU_ResetVidSys();
1201         if(FCEUGameInfo.type!=GIT_NSF)
1202          if(FSettings.GameGenie)
1203           OpenGenie();
1204
1205         PowerNES();
1206         #ifdef NETWORK
1207         if(FSettings.NetworkPlay) InitNetplay();
1208         #endif
1209         SaveStateRefresh();
1210         if(FCEUGameInfo.type!=GIT_NSF)
1211         {
1212          FCEU_LoadGamePalette();
1213          FCEU_LoadGameCheats(0);
1214         }
1215
1216         FCEU_ResetPalette();
1217         Exit=0;
1218
1219         if (have_movie)
1220                 FCEUI_LoadMovie(name, 1);
1221
1222         strcpy(lastLoadedGameName, name2);
1223
1224         return(&FCEUGameInfo);
1225 }
1226
1227
1228 void FCEU_ResetVidSys(void)
1229 {
1230  int w;
1231
1232  if(FCEUGameInfo.vidsys==GIV_NTSC)
1233   w=0;
1234  else if(FCEUGameInfo.vidsys==GIV_PAL)
1235   w=1;
1236  else
1237   w=FSettings.PAL;
1238
1239  if(w)
1240  {
1241   PAL=1;
1242   FSettings.FirstSLine=FSettings.UsrFirstSLine[1];
1243   FSettings.LastSLine=FSettings.UsrLastSLine[1];
1244  }
1245  else
1246  {
1247   PAL=0;
1248   FSettings.FirstSLine=FSettings.UsrFirstSLine[0];
1249   FSettings.LastSLine=FSettings.UsrLastSLine[0];
1250  }
1251  printf("ResetVidSys: PAL = %i\n", PAL);
1252  SetSoundVariables();
1253 }
1254
1255 int FCEUI_Initialize(void)
1256 {
1257         if(!InitVirtualVideo())
1258          return 0;
1259         memset(&FSettings,0,sizeof(FSettings));
1260         FSettings.UsrFirstSLine[0]=8;
1261         FSettings.UsrFirstSLine[1]=0;
1262         FSettings.UsrLastSLine[0]=FSettings.UsrLastSLine[1]=239;
1263         FSettings.SoundVolume=100;
1264
1265         FCEUI_Initialize098();
1266         FCEUI_SetEmuMode(0);
1267
1268         return 1;
1269 }
1270
1271 void FCEUI_Kill(void)
1272 {
1273  FCEU_KillGenie();
1274 }
1275
1276 static void EmLoop(void);
1277
1278 int use098code = 0;
1279 void (*ResetNES)(void) = 0;
1280 void (*FCEUI_Emulate)(void) = 0;
1281
1282 void FCEUI_SetEmuMode(int is_new)
1283 {
1284    use098code = is_new;
1285    if (is_new)
1286    {
1287     ResetNES=ResetNES098;
1288     FCEUI_Emulate=FCEUI_Emulate098;
1289    }
1290    else
1291    {
1292     ResetNES=ResetNES081;
1293     FCEUI_Emulate=EmLoop;
1294    }
1295 }
1296
1297 void MMC5_hb(int);     /* Ugh ugh ugh. */
1298 static void DoLine(void)
1299 {
1300    uint8 *target=XBuf+scanline*320+32;
1301
1302    LineUpdate(target);
1303
1304    if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1305
1306    X6502_Run(256);
1307
1308    // check: Battletoads & Double Dragon, Addams Family
1309    // sky glitches in SMB1 if done wrong
1310    FakedLineUpdate();
1311
1312 #ifdef FRAMESKIP
1313    if(!FSkip)
1314 #endif
1315    if(scanline>=FSettings.FirstSLine && scanline<=FSettings.LastSLine)
1316    {
1317     if(SpriteON && spork)
1318      CopySprites(target);
1319
1320     LineUpdateEnd(target);
1321    }
1322    sphitx=0x100;
1323
1324    if(ScreenON || SpriteON)
1325     FetchSpriteData();
1326
1327    // DoHBlank();
1328    if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
1329    {
1330     X6502_Run(6);
1331     Fixit2();
1332     X6502_Run(4);
1333     GameHBIRQHook();
1334     X6502_Run(85-10-16);
1335    }
1336    else
1337    {
1338     X6502_Run(6);  // Tried 65, caused problems with Slalom(maybe others)
1339     Fixit2();
1340     X6502_Run(85-6-16);
1341    }
1342
1343    if(SpriteON)
1344     RefreshSprites();
1345    if(GameHBIRQHook2 && (ScreenON || SpriteON))
1346     GameHBIRQHook2();
1347    scanline++;
1348    if (scanline<240)
1349     ResetRL(XBuf+scanline*320+32);
1350    X6502_Run(16);
1351 }
1352
1353
1354 static void EmLoop(void)
1355 {
1356  for(;;)
1357  {
1358   int x;
1359   uint32 scanlines_per_frame = PAL ? 312 : 262;
1360   UpdateInput();
1361   FCEU_ApplyPeriodicCheats();
1362
1363   // FCEUPPU_Loop:
1364   if(ppudead) /* Needed for Knight Rider, possibly others. */
1365   {
1366    //memset(XBuf, 0, 320*240);
1367    //X6502_Run(scanlines_per_frame*(256+85));
1368    int lines;
1369    for (lines=scanlines_per_frame;lines;lines--)
1370      X6502_Run(256+85);
1371    ppudead--;
1372    goto update;
1373   }
1374
1375   X6502_Run(256+85);
1376
1377   PPU[2]|=0x80;
1378   PPU[3]=PPUSPL=0;             /* Not sure if this is correct.  According
1379                                   to Matt Conte and my own tests, it is.  Timing is probably
1380                                   off, though.  NOTE:  Not having this here
1381                                   breaks a Super Donkey Kong game. */
1382
1383   X6502_Run(12);                /* I need to figure out the true nature and length
1384                                    of this delay.
1385                                 */
1386   if(FCEUGameInfo.type==GIT_NSF)
1387    DoNSFFrame();
1388   else if(VBlankON)
1389    TriggerNMI();
1390
1391   // Note: this is needed for asm core
1392   // Warning: using 'scanline' var here breaks Castlevania III
1393   {
1394    int lines;
1395    X6502_Run(256+85-12);
1396    for (lines=scanlines_per_frame-242-1;lines;lines--)
1397      X6502_Run(256+85);
1398   }
1399   // X6502_Run((scanlines_per_frame-242)*(256+85)-12);
1400   PPU_status&=0x1f;
1401   X6502_Run(256);
1402
1403   {
1404    if(ScreenON || SpriteON)
1405    {
1406     if(GameHBIRQHook)
1407      GameHBIRQHook();
1408      if(PPU_hook)
1409       for(x=0;x<42;x++) {PPU_hook(0x2000); PPU_hook(0);} // ugh
1410     if(GameHBIRQHook2)
1411      GameHBIRQHook2();
1412    }
1413
1414    X6502_Run(85-16);
1415
1416    if(ScreenON || SpriteON)
1417    {
1418     RefreshAddr=TempAddr;
1419     if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
1420    }
1421    spork=0;
1422    ResetRL(XBuf+32);
1423
1424    X6502_Run(16-kook);
1425    kook ^= 1;
1426   }
1427
1428   if(FCEUGameInfo.type==GIT_NSF)
1429   {
1430    // run scanlines for asm core to fuction
1431    for(scanline=0;scanline<240;scanline++)
1432     X6502_Run(256+85);
1433   }
1434   else
1435   {
1436    int x,max,maxref;
1437
1438    deemp=PPU[1]>>5;
1439    SetRefreshLine();
1440    for(scanline=0;scanline<240;)       // scanline is incremented in  DoLine.  Evil. :/
1441    {
1442     deempcnt[deemp]++;
1443     DoLine();
1444    }
1445    if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1446    for(x=1,max=0,maxref=0;x<7;x++)
1447    {
1448     if(deempcnt[x]>max)
1449     {
1450      max=deempcnt[x];
1451      maxref=x;
1452     }
1453     deempcnt[x]=0;
1454    }
1455    SetNESDeemph(maxref,0);
1456   }
1457
1458 update:
1459   if(Exit)
1460   {
1461    //CloseGame();
1462    break;
1463   }
1464
1465   {
1466    int ssize;
1467
1468    ssize=FlushEmulateSound();
1469
1470    timestampbase += timestamp;
1471    timestamp = 0;
1472
1473    #ifdef FRAMESKIP
1474    if(FSkip)
1475    {
1476     FCEU_PutImageDummy();
1477     FSkip--;
1478     FCEUD_Update(0,WaveFinalMono,ssize);
1479    }
1480    else
1481    #endif
1482    {
1483     FCEU_PutImage();
1484     FCEUD_Update(XBuf+8,WaveFinalMono,ssize);
1485    }
1486   }
1487
1488  } // for
1489 }
1490
1491 void FCEUI_CloseGame(void)
1492 {
1493         Exit=1;
1494 }
1495
1496 static void ResetPPU(void)
1497 {
1498         VRAMBuffer=PPU[0]=PPU[1]=PPU[2]=PPU[3]=0;
1499         PPUSPL=0;
1500         PPUGenLatch=0;
1501         RefreshAddr=TempAddr=0;
1502         vtoggle = 0;
1503         ppudead = 2;
1504         kook = 0;
1505 }
1506
1507 static void PowerPPU(void)
1508 {
1509         memset(NTARAM,0x00,0x800);
1510         memset(PALRAM,0x00,0x20);
1511         memset(SPRAM,0x00,0x100);
1512         ResetPPU();
1513 }
1514
1515 void ResetNES081(void)
1516 {
1517         if(!GameLoaded) return;
1518         GameInterface(GI_RESETM2, 0);
1519         ResetSound();
1520         ResetPPU();
1521         X6502_Reset();
1522 }
1523
1524 #ifndef DEBUG_ASM_6502
1525 static void FCEU_MemoryRand(uint8 *ptr, uint32 size)
1526 {
1527  int x=0;
1528  while(size)
1529  {
1530   *ptr=(x&4)?0xFF:0x00;
1531   x++;
1532   size--;
1533   ptr++;
1534  }
1535 }
1536 #endif
1537
1538 void PowerNES(void)
1539 {
1540         if(!GameLoaded) return;
1541
1542         FCEU_CheatResetRAM();
1543         FCEU_CheatAddRAM(2,0,RAM);
1544
1545         GeniePower();
1546
1547 #ifndef DEBUG_ASM_6502
1548         FCEU_MemoryRand(RAM,0x800);
1549 #else
1550         memset(RAM,0x00,0x800);
1551         memset(nes_internal_ram,0x00,0x800);
1552 #endif
1553         ResetMapping();
1554         PowerSound();
1555         PowerPPU();
1556
1557         if (use098code)
1558          FCEUPPU_Power();
1559
1560         /* Have the external game hardware "powered" after the internal NES stuff.
1561            Needed for the NSF code and VS System code.
1562         */
1563         GameInterface(GI_POWER, 0);
1564         if(FCEUGameInfo.type==GIT_VSUNI)
1565          FCEU_VSUniPower();
1566 #ifdef ASM_6502
1567         if (geniestage)
1568          GenieSetPages(0);
1569 #endif
1570         timestampbase=0;
1571         X6502_Power();
1572         FCEU_PowerCheats();
1573 }
1574
1575
1576 /* savestate stuff */
1577 uint16 TempAddrT,RefreshAddrT;
1578
1579 SFORMAT FCEUPPU_STATEINFO[]={
1580  { NTARAM, 0x800, "NTAR"},
1581  { PALRAM, 0x20, "PRAM"},
1582  { SPRAM, 0x100, "SPRA"},
1583  { PPU, 0x4, "PPUR"},
1584  { &XOffset, 1, "XOFF"},
1585  { &vtoggle, 1, "VTOG"},
1586  { &RefreshAddrT, 2|RLSB, "RADD"},
1587  { &TempAddrT, 2|RLSB, "TADD"},
1588  { &VRAMBuffer, 1, "VBUF"},
1589  { &PPUGenLatch, 1, "PGEN"},
1590  // from 0.98.15
1591  { &kook, 1, "KOOK"},
1592  { &ppudead, 1, "DEAD"},
1593  { &PPUSPL, 1, "PSPL"},
1594  { 0 }
1595  };
1596
1597