continue with pandora port
[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 __arm__
60 #include        "drivers/arm/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(!ScreenON)
635         {
636           tem=Pal[0]|0x40;
637           tem|=tem << 8;
638           tem|=tem << 16;
639           FCEU_dwmemset(target,tem,256);
640         }
641         else if(scanline < FSettings.FirstSLine || scanline > FSettings.LastSLine)
642         {
643            if(PPU_hook)
644             PRefreshLine();
645            y=(int)SPRAM[0] + 1;
646            if(scanline==y && SpriteON) PPU_status|=0x40;
647         }
648         else
649         {
650          BGRender(target);
651         }
652
653         if(InputScanlineHook)
654          InputScanlineHook(target,spork?sprlinebuf:0,linestartts,256);
655 }
656
657
658 static void LineUpdateEnd(uint8 *target)
659 {
660 #ifdef __arm__
661  if(ScreenON || SpriteON)  // Yes, very el-cheapo.
662  {
663   if(PPU[1]&0x01)
664    block_and(target, 256, 0x30);
665  }
666  if((PPU[1]>>5)==0x7)
667   block_or(target, 256, 0xc0);
668  else if(PPU[1]&0xE0)
669   block_or(target, 256, 0x40);
670  else
671   block_andor(target, 256, 0x3f, 0x80);
672 #else
673  int x;
674
675  if(ScreenON || SpriteON)  // Yes, very el-cheapo.
676  {
677   if(PPU[1]&0x01)
678   {
679    for(x=63;x>=0;x--)
680    *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0x30303030;
681   }
682  }
683  if((PPU[1]>>5)==0x7)
684  {
685   for(x=63;x>=0;x--)
686    *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0xc0c0c0c0;
687  }
688  else if(PPU[1]&0xE0)
689   for(x=63;x>=0;x--)
690    *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])|0x40404040;
691  else
692   for(x=63;x>=0;x--)
693    *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0x80808080;
694 #endif
695
696  // black borders
697  ((uint32 *)target)[-2]=((uint32 *)target)[-1]=0;
698  ((uint32 *)target)[64]=((uint32 *)target)[65]=0;
699 }
700
701 #define PAL(c)  ((c)+cc)
702
703
704 static void PRefreshLine(void)
705 {
706          uint32 vofs;
707          int X1;
708          vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
709          void (*PPU_hook_)(uint32 A) = PPU_hook;
710
711
712          for(X1=33;X1;X1--)
713          {
714                 uint32 zz2;
715                 uint32 vadr;
716
717                 zz2=(RefreshAddr>>10)&3;
718                 PPU_hook_(0x2000|(RefreshAddr&0xFFF));
719
720                 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
721
722                 PPU_hook_(vadr);
723
724                 if((RefreshAddr&0x1f)==0x1f)
725                  RefreshAddr^=0x41F;
726                 else
727                  RefreshAddr++;
728          }
729 }
730
731 /* This high-level graphics MMC5 emulation code was written
732    for MMC5 carts in "CL" mode.  It's probably not totally
733    correct for carts in "SL" mode.
734    */
735 static void RefreshLine_MMC5Hack1(uint8 *P, uint32 vofs)
736 {
737           int8 tochange, X1;
738
739           tochange=MMC5HackSPMode&0x1F;
740
741           for(X1=33;X1;X1--,P+=8)
742           {
743                 uint8 *C;
744                 uint8 cc,zz,zz2;
745                 uint32 vadr;
746
747                 if((tochange<=0 && MMC5HackSPMode&0x40) ||
748                    (tochange>0 && !(MMC5HackSPMode&0x40)))
749                 {
750                  uint8 xs,ys;
751
752                  xs=33-X1;
753                  ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
754                  if(ys>=0x1E) ys-=0x1E;
755                  vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
756
757                  C = MMC5HackVROMPTR+vadr;
758                  C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
759
760                  cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
761                  cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
762                 }
763                 else
764                 {
765                  zz=RefreshAddr&0x1F;
766                  zz2=(RefreshAddr>>10)&3;
767                  vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
768                  C = MMC5BGVRAMADR(vadr);
769                  cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
770                  cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
771                 }
772                 #include "fceline.h"
773
774                 if((RefreshAddr&0x1f)==0x1f)
775                  RefreshAddr^=0x41F;
776                 else
777                  RefreshAddr++;
778                 tochange--;
779           }
780 }
781
782 static void RefreshLine_MMC5Hack2(uint8 *P, uint32 vofs)
783 {
784           int8 tochange, X1;
785
786           tochange=MMC5HackSPMode&0x1F;
787
788           for(X1=33;X1;X1--,P+=8)
789           {
790                 uint8 *C;
791                 uint8 cc;
792                 uint8 zz2;
793                 uint32 vadr;
794
795                 if((tochange<=0 && MMC5HackSPMode&0x40) ||
796                    (tochange>0 && !(MMC5HackSPMode&0x40)))
797                 {
798                  uint8 xs,ys;
799
800                  xs=33-X1;
801                  ys=((scanline>>3)+MMC5HackSPScroll)&0x1F;
802                  if(ys>=0x1E) ys-=0x1E;
803                  vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7);
804
805                  C = MMC5HackVROMPTR+vadr;
806                  C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12);
807
808                  cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)];
809                  cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2;
810                 }
811                 else
812                 {
813                  C=MMC5HackVROMPTR;
814                  zz2=(RefreshAddr>>10)&3;
815                  vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
816                  C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
817                          MMC5HackVROMMask) << 12) + (vadr & 0xfff);
818                  vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
819                  cc = vadr;
820                 }
821                 #include "fceline.h"
822                 if((RefreshAddr&0x1f)==0x1f)
823                  RefreshAddr^=0x41F;
824                 else
825                  RefreshAddr++;
826                 tochange--;
827           }
828 }
829
830 static void RefreshLine_MMC5Hack3(uint8 *P, uint32 vofs)
831 {
832           int8 X1;
833
834           for(X1=33;X1;X1--,P+=8)
835           {
836                 uint8 *C;
837                 uint8 cc;
838                 uint8 zz2;
839                 uint32 vadr;
840
841                 C=MMC5HackVROMPTR;
842                 zz2=(RefreshAddr>>10)&3;
843                 vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs;
844                 C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f &
845                         MMC5HackVROMMask) << 12) + (vadr & 0xfff);
846                 vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4;
847                 cc = vadr;
848
849                 #include "fceline.h"
850                 if((RefreshAddr&0x1f)==0x1f)
851                  RefreshAddr^=0x41F;
852                 else
853                  RefreshAddr++;
854           }
855 }
856
857 static void RefreshLine_MMC5Hack4(uint8 *P, uint32 vofs)
858 {
859           int8 X1;
860
861           for(X1=33;X1;X1--,P+=8)
862           {
863                 uint8 *C;
864                 uint8 cc,zz,zz2;
865                 uint32 vadr;
866
867                 zz=RefreshAddr&0x1F;
868                 zz2=(RefreshAddr>>10)&3;
869                 vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs;
870                 C = MMC5BGVRAMADR(vadr);
871                 cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)];
872                 cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2;
873
874                 #include "fceline.h"
875
876                 if((RefreshAddr&0x1f)==0x1f)
877                  RefreshAddr^=0x41F;
878                 else
879                  RefreshAddr++;
880           }
881 }
882
883 static void RefreshLine_PPU_hook(uint8 *P, uint32 vofs)
884 {
885          int8 X1;
886          void (*PPU_hook_)(uint32 A) = PPU_hook;
887          uint32 rfraddr = RefreshAddr;
888          uint8 *page = vnapage[(rfraddr>>10)&3];
889
890          for(X1=33;X1;X1--,P+=8)
891          {
892                 uint8 *C;
893                 uint8 cc,zz;
894                 uint32 vadr;
895
896                 zz=rfraddr&0x1F;
897                 PPU_hook_(0x2000|(rfraddr&0xFFF));
898                 cc=page[0x3c0+(zz>>2)+((rfraddr&0x380)>>4)];
899                 cc=((cc >> ((zz&2) + ((rfraddr&0x40)>>4))) &3) <<2;
900                 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
901                 C = VRAMADR(vadr);
902
903                 #include "fceline.h"
904
905                 PPU_hook_(vadr);
906
907                 if((rfraddr&0x1f)==0x1f) {
908                  rfraddr^=0x41F;
909                  page = vnapage[(rfraddr>>10)&3];
910                 } else
911                  rfraddr++;
912          }
913          RefreshAddr = rfraddr;
914 }
915
916 static void RefreshLine_normal(uint8 *P, uint32 vofs) // vofs is 0x107 max
917 {
918          int8 X1;
919          uint32 rfraddr = RefreshAddr;
920          uint8 *page = vnapage[(rfraddr>>10)&3];
921          uint32 cc2=0;
922
923          if ((rfraddr&0xc)!=0)
924           cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
925
926          for (X1=33;X1;X1--,P+=8)
927          {
928                 uint8 cc,*C;
929                 uint32 vadr;
930
931                 vadr=(page[rfraddr&0x3ff]<<4)+vofs;
932                 C = VRAMADR(vadr);
933                 if ((rfraddr&0xc)==0)
934                  cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0);
935                 cc=((cc2 >> ((rfraddr&2) + ((rfraddr&0x40)>>4) + ((rfraddr&0xc)<<1))) & 3) << 2;
936
937                 #include "fceline.h"
938
939                 if((rfraddr&0x1f)==0x1f) {
940                  rfraddr^=0x41F;
941                  page = vnapage[(rfraddr>>10)&3];
942                 } else
943                  rfraddr++;
944          }
945          RefreshAddr = rfraddr;
946 }
947
948 static void SetRefreshLine(void)
949 {
950         if(MMC5Hack && geniestage!=1)
951         {
952          if(MMC5HackCHRMode==0 && (MMC5HackSPMode&0x80))
953          {
954                  if (RefreshLine != RefreshLine_MMC5Hack1) printf("set refr RefreshLine_MMC5Hack1\n");
955                  RefreshLine = RefreshLine_MMC5Hack1;
956          }
957          else if(MMC5HackCHRMode==1 && (MMC5HackSPMode&0x80))
958          {
959                 if (RefreshLine != RefreshLine_MMC5Hack2) printf("set refr RefreshLine_MMC5Hack2\n");
960                  RefreshLine = RefreshLine_MMC5Hack2;
961          }
962          else if(MMC5HackCHRMode==1)
963          {
964                 if (RefreshLine != RefreshLine_MMC5Hack3) printf("set refr RefreshLine_MMC5Hack3\n");
965                  RefreshLine = RefreshLine_MMC5Hack3;
966          }
967          else
968          {
969                 if (RefreshLine != RefreshLine_MMC5Hack4) printf("set refr RefreshLine_MMC5Hack4\n");
970                  RefreshLine = RefreshLine_MMC5Hack4;
971          }
972         }       // End if(MMC5Hack)
973         else if(PPU_hook)
974         {
975                 if (RefreshLine != RefreshLine_PPU_hook) printf("set refr RefreshLine_PPU_hook\n");
976                 RefreshLine = RefreshLine_PPU_hook;
977         }
978         else
979         {
980                 if (RefreshLine != RefreshLine_normal) printf("set refr RefreshLine_normal\n");
981                 RefreshLine = RefreshLine_normal;
982         }
983 }
984
985 static INLINE
986 void Fixit2(void)
987 {
988    if(ScreenON || SpriteON)
989    {
990     uint32 rad=RefreshAddr;
991     rad&=0xFBE0;
992     rad|=TempAddr&0x041f;
993     RefreshAddr=rad;
994     //PPU_hook(RefreshAddr,-1);
995    }
996 }
997
998 static INLINE
999 void Fixit1(void)
1000 {
1001    if(ScreenON || SpriteON)
1002    {
1003     uint32 rad=RefreshAddr;
1004
1005     if((rad&0x7000)==0x7000)
1006     {
1007      rad^=0x7000;
1008      if((rad&0x3E0)==0x3A0)
1009      {
1010       rad^=0x3A0;
1011       rad^=0x800;
1012      }
1013      else
1014      {
1015       if((rad&0x3E0)==0x3e0)
1016        rad^=0x3e0;
1017       else rad+=0x20;
1018      }
1019     }
1020     else
1021      rad+=0x1000;
1022     RefreshAddr=rad;
1023     //PPU_hook(RefreshAddr,-1);
1024    }
1025 }
1026
1027
1028 // ============================//
1029 // end of new code
1030 // ===========================//
1031
1032 void ResetMapping(void)
1033 {
1034         int x;
1035
1036         SetReadHandler(0x0000,0xFFFF,ANull);
1037         SetWriteHandler(0x0000,0xFFFF,BNull);
1038
1039         SetReadHandler(0,0x7FF,ARAML);
1040         SetWriteHandler(0,0x7FF,BRAML);
1041
1042         SetReadHandler(0x800,0x1FFF,ARAMH);  /* Part of a little */
1043         SetWriteHandler(0x800,0x1FFF,BRAMH); /* hack for a small speed boost. */
1044
1045         for(x=0x2000;x<0x4000;x+=8)
1046         {
1047          ARead[x]=A200x;
1048          BWrite[x]=B2000;
1049          ARead[x+1]=A200x;
1050          BWrite[x+1]=B2001;
1051          ARead[x+2]=A2002;
1052          BWrite[x+2]=B2002;
1053          ARead[x+3]=A200x;
1054          BWrite[x+3]=B2003;
1055          ARead[x+4]=A200x;
1056          BWrite[x+4]=B2004;
1057          ARead[x+5]=A200x;
1058          BWrite[x+5]=B2005;
1059          ARead[x+6]=A200x;
1060          BWrite[x+6]=B2006;
1061          ARead[x+7]=A2007;
1062          BWrite[x+7]=B2007;
1063         }
1064
1065         BWrite[0x4014]=B4014;
1066         SetNESSoundMap();
1067         InitializeInput();
1068 }
1069
1070 int GameLoaded=0;
1071 void CloseGame(void)
1072 {
1073  FCEUI_StopMovie();
1074  if(GameLoaded)
1075  {
1076   if(FCEUGameInfo.type!=GIT_NSF)
1077    FCEU_FlushGameCheats(0,0);
1078   #ifdef NETWORK
1079   if(FSettings.NetworkPlay) KillNetplay();
1080   #endif
1081   GameInterface(GI_CLOSE, 0);
1082   CloseGenie();
1083   GameLoaded=0;
1084  }
1085 }
1086
1087 void ResetGameLoaded(void)
1088 {
1089         if(GameLoaded) CloseGame();
1090         GameStateRestore=0;
1091         PPU_hook=0;
1092         GameHBIRQHook=GameHBIRQHook2=0;
1093         GameExpSound.Fill=0;
1094         GameExpSound.RChange=0;
1095         if(GameExpSound.Kill)
1096          GameExpSound.Kill();
1097         GameExpSound.Kill=0;
1098         MapIRQHook=0;
1099         MMC5Hack=0;
1100         PAL&=1;
1101         pale=0;
1102
1103         FCEUGameInfo.name=0;
1104         FCEUGameInfo.type=GIT_CART;
1105         FCEUGameInfo.vidsys=GIV_USER;
1106         FCEUGameInfo.input[0]=FCEUGameInfo.input[1]=-1;
1107         FCEUGameInfo.inputfc=-1;
1108
1109         FCEUGameInfo.soundchan=0;
1110         FCEUGameInfo.soundrate=0;
1111         FCEUGameInfo.cspecial=0;
1112 }
1113
1114 char lastLoadedGameName [2048];
1115 int LoadGameLastError = 0;
1116 int UNIFLoad(const char *name, int fp);
1117 int iNESLoad(const char *name, int fp);
1118 int FDSLoad(const char *name, int fp);
1119 int NSFLoad(int fp);
1120
1121 FCEUGI *FCEUI_LoadGame(char *name)
1122 {
1123         char name2[512];
1124         int have_movie = 0, have_ips = 0;
1125         int fp;
1126
1127         //Exit=1;
1128         LoadGameLastError = 0;
1129         ResetGameLoaded();
1130
1131         strncpy(name2, name, sizeof(name2));
1132         name2[sizeof(name2)-1] = 0;
1133
1134         fp=FCEU_fopen(name2,"rb");
1135         if(!fp)
1136         {
1137          FCEU_PrintError("Error opening \"%s\"!",name);
1138          LoadGameLastError = 1;
1139          return 0;
1140         }
1141
1142         {
1143          char *p = name2 + strlen(name2) - 4;
1144          if (strcasecmp(p, ".fcm") == 0) printf("movie detected\n"), have_movie = 1;
1145          if (strcasecmp(p, ".ips") == 0) printf("ips detected\n"), have_ips = 1;
1146          if (have_movie || have_ips)
1147          {
1148           // movie detected
1149           FCEU_fclose(fp);
1150           *p = 0;
1151           fp=FCEU_fopen(name2,"rb");
1152           if (!fp && p - name2 > 2)
1153           {
1154            for (p--; p > name2 && *p != '.'; p--);
1155            *p = 0;
1156            fp=FCEU_fopen(name2,"rb");
1157           }
1158           if (!fp) {
1159            printf("no ROM for ips/movie\n");
1160            LoadGameLastError = 2;
1161            return 0;
1162           }
1163          }
1164         }
1165
1166         // do IPS patch
1167         if (have_ips)
1168         {
1169          FCEU_fclose(fp);
1170          FILE *ips = fopen(name, "rb");
1171          if (!ips) return 0;
1172          fp=FCEU_fopen_forcemem(name2);
1173          if (!fp) { fclose(ips); return 0; }
1174          ApplyIPS(ips, fp); // closes ips
1175         }
1176
1177         GetFileBase(name2);
1178         if(iNESLoad(name2,fp))
1179          goto endlseq;
1180         if(NSFLoad(fp))
1181          goto endlseq;
1182         if(FDSLoad(name2,fp))
1183          goto endlseq;
1184         if(UNIFLoad(name2,fp))
1185          goto endlseq;
1186
1187         FCEU_PrintError("An error occurred while loading the file.");
1188         FCEU_fclose(fp);
1189         // format handlers may set LoadGameLastError themselves.
1190         if (LoadGameLastError == 0) LoadGameLastError = 3;
1191         return 0;
1192
1193         endlseq:
1194         FCEU_fclose(fp);
1195         GameLoaded=1;
1196
1197         FCEU_ResetVidSys();
1198         if(FCEUGameInfo.type!=GIT_NSF)
1199          if(FSettings.GameGenie)
1200           OpenGenie();
1201
1202         PowerNES();
1203         #ifdef NETWORK
1204         if(FSettings.NetworkPlay) InitNetplay();
1205         #endif
1206         SaveStateRefresh();
1207         if(FCEUGameInfo.type!=GIT_NSF)
1208         {
1209          FCEU_LoadGamePalette();
1210          FCEU_LoadGameCheats(0);
1211         }
1212
1213         FCEU_ResetPalette();
1214         Exit=0;
1215
1216         if (have_movie)
1217                 FCEUI_LoadMovie(name, 1);
1218
1219         strcpy(lastLoadedGameName, name2);
1220
1221         return(&FCEUGameInfo);
1222 }
1223
1224
1225 void FCEU_ResetVidSys(void)
1226 {
1227  int w;
1228
1229  if(FCEUGameInfo.vidsys==GIV_NTSC)
1230   w=0;
1231  else if(FCEUGameInfo.vidsys==GIV_PAL)
1232   w=1;
1233  else
1234   w=FSettings.PAL;
1235
1236  if(w)
1237  {
1238   PAL=1;
1239   FSettings.FirstSLine=FSettings.UsrFirstSLine[1];
1240   FSettings.LastSLine=FSettings.UsrLastSLine[1];
1241  }
1242  else
1243  {
1244   PAL=0;
1245   FSettings.FirstSLine=FSettings.UsrFirstSLine[0];
1246   FSettings.LastSLine=FSettings.UsrLastSLine[0];
1247  }
1248  printf("ResetVidSys: PAL = %i\n", PAL);
1249  SetSoundVariables();
1250 }
1251
1252 int FCEUI_Initialize(void)
1253 {
1254         if(!InitVirtualVideo())
1255          return 0;
1256         memset(&FSettings,0,sizeof(FSettings));
1257         FSettings.UsrFirstSLine[0]=8;
1258         FSettings.UsrFirstSLine[1]=0;
1259         FSettings.UsrLastSLine[0]=FSettings.UsrLastSLine[1]=239;
1260         FSettings.SoundVolume=100;
1261
1262         FCEUI_Initialize098();
1263         FCEUI_SetEmuMode(0);
1264
1265         return 1;
1266 }
1267
1268 void FCEUI_Kill(void)
1269 {
1270  FCEU_KillGenie();
1271 }
1272
1273 static void EmLoop(void);
1274
1275 int use098code = 0;
1276 void (*ResetNES)(void) = 0;
1277 void (*FCEUI_Emulate)(void) = 0;
1278
1279 void FCEUI_SetEmuMode(int is_new)
1280 {
1281    use098code = is_new;
1282    if (is_new)
1283    {
1284     ResetNES=ResetNES098;
1285     FCEUI_Emulate=FCEUI_Emulate098;
1286    }
1287    else
1288    {
1289     ResetNES=ResetNES081;
1290     FCEUI_Emulate=EmLoop;
1291    }
1292 }
1293
1294 void MMC5_hb(int);     /* Ugh ugh ugh. */
1295 static void DoLine(void)
1296 {
1297    uint8 *target=XBuf+scanline*320+32;
1298
1299    LineUpdate(target);
1300
1301    if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1302
1303    X6502_Run(256);
1304
1305    // check: Battletoads & Double Dragon, Addams Family
1306    // sky glitches in SMB1 if done wrong
1307    FakedLineUpdate();
1308
1309 #ifdef FRAMESKIP
1310    if(!FSkip)
1311 #endif
1312    if(scanline>=FSettings.FirstSLine && scanline<=FSettings.LastSLine)
1313    {
1314     if(SpriteON && spork)
1315      CopySprites(target);
1316
1317     LineUpdateEnd(target);
1318    }
1319    sphitx=0x100;
1320
1321    if(ScreenON || SpriteON)
1322     FetchSpriteData();
1323
1324    // DoHBlank();
1325    if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
1326    {
1327     X6502_Run(6);
1328     Fixit2();
1329     X6502_Run(4);
1330     GameHBIRQHook();
1331     X6502_Run(85-10-16);
1332    }
1333    else
1334    {
1335     X6502_Run(6);  // Tried 65, caused problems with Slalom(maybe others)
1336     Fixit2();
1337     X6502_Run(85-6-16);
1338    }
1339
1340    if(SpriteON)
1341     RefreshSprites();
1342    if(GameHBIRQHook2 && (ScreenON || SpriteON))
1343     GameHBIRQHook2();
1344    scanline++;
1345    if (scanline<240)
1346     ResetRL(XBuf+scanline*320+32);
1347    X6502_Run(16);
1348 }
1349
1350
1351 static void EmLoop(void)
1352 {
1353  for(;;)
1354  {
1355   int x;
1356   uint32 scanlines_per_frame = PAL ? 312 : 262;
1357   UpdateInput();
1358   FCEU_ApplyPeriodicCheats();
1359
1360   // FCEUPPU_Loop:
1361   if(ppudead) /* Needed for Knight Rider, possibly others. */
1362   {
1363    //memset(XBuf, 0, 320*240);
1364    //X6502_Run(scanlines_per_frame*(256+85));
1365    int lines;
1366    for (lines=scanlines_per_frame;lines;lines--)
1367      X6502_Run(256+85);
1368    ppudead--;
1369    goto update;
1370   }
1371
1372   X6502_Run(256+85);
1373
1374   PPU[2]|=0x80;
1375   PPU[3]=PPUSPL=0;             /* Not sure if this is correct.  According
1376                                   to Matt Conte and my own tests, it is.  Timing is probably
1377                                   off, though.  NOTE:  Not having this here
1378                                   breaks a Super Donkey Kong game. */
1379
1380   X6502_Run(12);                /* I need to figure out the true nature and length
1381                                    of this delay.
1382                                 */
1383   if(FCEUGameInfo.type==GIT_NSF)
1384    DoNSFFrame();
1385   else if(VBlankON)
1386    TriggerNMI();
1387
1388   // Note: this is needed for asm core
1389   // Warning: using 'scanline' var here breaks Castlevania III
1390   {
1391    int lines;
1392    X6502_Run(256+85-12);
1393    for (lines=scanlines_per_frame-242-1;lines;lines--)
1394      X6502_Run(256+85);
1395   }
1396   // X6502_Run((scanlines_per_frame-242)*(256+85)-12);
1397   PPU_status&=0x1f;
1398   X6502_Run(256);
1399
1400   {
1401    if(ScreenON || SpriteON)
1402    {
1403     if(GameHBIRQHook)
1404      GameHBIRQHook();
1405      if(PPU_hook)
1406       for(x=0;x<42;x++) {PPU_hook(0x2000); PPU_hook(0);} // ugh
1407     if(GameHBIRQHook2)
1408      GameHBIRQHook2();
1409    }
1410
1411    X6502_Run(85-16);
1412
1413    if(ScreenON || SpriteON)
1414    {
1415     RefreshAddr=TempAddr;
1416     if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
1417    }
1418    spork=0;
1419    ResetRL(XBuf+32);
1420
1421    X6502_Run(16-kook);
1422    kook ^= 1;
1423   }
1424
1425   if(FCEUGameInfo.type==GIT_NSF)
1426   {
1427    // run scanlines for asm core to fuction
1428    for(scanline=0;scanline<240;scanline++)
1429     X6502_Run(256+85);
1430   }
1431   else
1432   {
1433    int x,max,maxref;
1434
1435    deemp=PPU[1]>>5;
1436    SetRefreshLine();
1437    for(scanline=0;scanline<240;)       // scanline is incremented in  DoLine.  Evil. :/
1438    {
1439     deempcnt[deemp]++;
1440     DoLine();
1441    }
1442    if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1443    for(x=1,max=0,maxref=0;x<7;x++)
1444    {
1445     if(deempcnt[x]>max)
1446     {
1447      max=deempcnt[x];
1448      maxref=x;
1449     }
1450     deempcnt[x]=0;
1451    }
1452    SetNESDeemph(maxref,0);
1453   }
1454
1455 update:
1456   if(Exit)
1457   {
1458    //CloseGame();
1459    break;
1460   }
1461
1462   {
1463    int ssize;
1464
1465    ssize=FlushEmulateSound();
1466
1467    timestampbase += timestamp;
1468    timestamp = 0;
1469
1470    #ifdef FRAMESKIP
1471    if(FSkip)
1472    {
1473     FCEU_PutImageDummy();
1474     FSkip--;
1475     FCEUD_Update(0,WaveFinalMono,ssize);
1476    }
1477    else
1478    #endif
1479    {
1480     FCEU_PutImage();
1481     FCEUD_Update(XBuf+8,WaveFinalMono,ssize);
1482    }
1483   }
1484
1485  } // for
1486 }
1487
1488 void FCEUI_CloseGame(void)
1489 {
1490         Exit=1;
1491 }
1492
1493 static void ResetPPU(void)
1494 {
1495         VRAMBuffer=PPU[0]=PPU[1]=PPU[2]=PPU[3]=0;
1496         PPUSPL=0;
1497         PPUGenLatch=0;
1498         RefreshAddr=TempAddr=0;
1499         vtoggle = 0;
1500         ppudead = 2;
1501         kook = 0;
1502 }
1503
1504 static void PowerPPU(void)
1505 {
1506         memset(NTARAM,0x00,0x800);
1507         memset(PALRAM,0x00,0x20);
1508         memset(SPRAM,0x00,0x100);
1509         ResetPPU();
1510 }
1511
1512 void ResetNES081(void)
1513 {
1514         if(!GameLoaded) return;
1515         GameInterface(GI_RESETM2, 0);
1516         ResetSound();
1517         ResetPPU();
1518         X6502_Reset();
1519 }
1520
1521 #ifndef DEBUG_ASM_6502
1522 static void FCEU_MemoryRand(uint8 *ptr, uint32 size)
1523 {
1524  int x=0;
1525  while(size)
1526  {
1527   *ptr=(x&4)?0xFF:0x00;
1528   x++;
1529   size--;
1530   ptr++;
1531  }
1532 }
1533 #endif
1534
1535 void PowerNES(void)
1536 {
1537         if(!GameLoaded) return;
1538
1539         FCEU_CheatResetRAM();
1540         FCEU_CheatAddRAM(2,0,RAM);
1541
1542         GeniePower();
1543
1544 #ifndef DEBUG_ASM_6502
1545         FCEU_MemoryRand(RAM,0x800);
1546 #else
1547         memset(RAM,0x00,0x800);
1548         memset(nes_internal_ram,0x00,0x800);
1549 #endif
1550         ResetMapping();
1551         PowerSound();
1552         PowerPPU();
1553
1554         if (use098code)
1555          FCEUPPU_Power();
1556
1557         /* Have the external game hardware "powered" after the internal NES stuff.
1558            Needed for the NSF code and VS System code.
1559         */
1560         GameInterface(GI_POWER, 0);
1561         if(FCEUGameInfo.type==GIT_VSUNI)
1562          FCEU_VSUniPower();
1563 #ifdef ASM_6502
1564         if (geniestage)
1565          GenieSetPages(0);
1566 #endif
1567         timestampbase=0;
1568         X6502_Power();
1569         FCEU_PowerCheats();
1570 }
1571
1572
1573 /* savestate stuff */
1574 uint16 TempAddrT,RefreshAddrT;
1575
1576 SFORMAT FCEUPPU_STATEINFO[]={
1577  { NTARAM, 0x800, "NTAR"},
1578  { PALRAM, 0x20, "PRAM"},
1579  { SPRAM, 0x100, "SPRA"},
1580  { PPU, 0x4, "PPUR"},
1581  { &XOffset, 1, "XOFF"},
1582  { &vtoggle, 1, "VTOG"},
1583  { &RefreshAddrT, 2|RLSB, "RADD"},
1584  { &TempAddrT, 2|RLSB, "TADD"},
1585  { &VRAMBuffer, 1, "VBUF"},
1586  { &PPUGenLatch, 1, "PGEN"},
1587  // from 0.98.15
1588  { &kook, 1, "KOOK"},
1589  { &ppudead, 1, "DEAD"},
1590  { &PPUSPL, 1, "PSPL"},
1591  { 0 }
1592  };
1593
1594