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