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