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