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