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