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