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