some warnings fixed, nsf fixed, palettes, more code backported
[fceu.git] / cart.c
CommitLineData
c62d2810 1/* FCE Ultra - NES/Famicom Emulator
2 *
3 * Copyright notice for this file:
d97315ac 4 * Copyright (C) 2002 Xodnizel
c62d2810 5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <string.h>
22#include <stdlib.h>
d97315ac 23#include <stdio.h>
c62d2810 24
25#include "types.h"
c62d2810 26#include "fce.h"
d97315ac 27#include "ppu.h"
28
c62d2810 29#include "cart.h"
30#include "memory.h"
d97315ac 31#include "x6502.h"
c62d2810 32
33#include "general.h"
d97315ac 34
c62d2810 35#include "svga.h"
d97315ac 36#include "file.h"
37
38#define FCEUPPU_LineUpdate(...)
c62d2810 39
937bf65b 40/*
c62d2810 41 This file contains all code for coordinating the mapping in of the
42 address space external to the NES.
43 It's also (ab)used by the NSF code.
44*/
45
46uint8 *Page[32],*VPage[8];
47uint8 **VPageR=VPage;
48uint8 *VPageG[8];
49uint8 *MMC5SPRVPage[8];
50uint8 *MMC5BGVPage[8];
51
d97315ac 52static uint8 PRGIsRAM[32]; /* This page is/is not PRG RAM. */
c62d2810 53
d97315ac 54/* 16 are (sort of) reserved for UNIF/iNES and 16 to map other stuff. */
c62d2810 55static int CHRram[32];
56static int PRGram[32];
57
58uint8 *PRGptr[32];
59uint8 *CHRptr[32];
60
61uint32 PRGsize[32];
62uint32 CHRsize[32];
63
64uint32 PRGmask2[32];
65uint32 PRGmask4[32];
66uint32 PRGmask8[32];
67uint32 PRGmask16[32];
68uint32 PRGmask32[32];
69
70uint32 CHRmask1[32];
71uint32 CHRmask2[32];
72uint32 CHRmask4[32];
73uint32 CHRmask8[32];
74
75int geniestage=0;
76
77int modcon;
78
79uint8 genieval[3];
80uint8 geniech[3];
81
82uint32 genieaddr[3];
83
d97315ac 84static INLINE void setpageptr(int s, uint32 A, uint8 *p, int ram)
c62d2810 85{
86 uint32 AB=A>>11;
87 int x;
88
d97315ac 89 if(p)
90 for(x=(s>>1)-1;x>=0;x--)
91 {
92 PRGIsRAM[AB+x]=ram;
93 Page[AB+x]=p-A;
94 }
95 else
96 for(x=(s>>1)-1;x>=0;x--)
97 {
98 PRGIsRAM[AB+x]=0;
99 Page[AB+x]=0;
100 }
c62d2810 101}
102
d97315ac 103static uint8 nothing[8192];
c62d2810 104void ResetCartMapping(void)
105{
106 int x;
107
108 for(x=0;x<32;x++)
109 {
d97315ac 110 Page[x]=nothing-x*2048;
c62d2810 111 PRGptr[x]=CHRptr[x]=0;
112 PRGsize[x]=CHRsize[x]=0;
113 }
114 for(x=0;x<8;x++)
115 {
d97315ac 116 MMC5SPRVPage[x]=MMC5BGVPage[x]=VPageR[x]=nothing-0x400*x;
c62d2810 117 }
118
119}
120
121void SetupCartPRGMapping(int chip, uint8 *p, uint32 size, int ram)
122{
123 PRGptr[chip]=p;
124 PRGsize[chip]=size;
125
126 PRGmask2[chip]=(size>>11)-1;
127 PRGmask4[chip]=(size>>12)-1;
128 PRGmask8[chip]=(size>>13)-1;
129 PRGmask16[chip]=(size>>14)-1;
937bf65b 130 PRGmask32[chip]=(size>>15)-1;
c62d2810 131
132 PRGram[chip]=ram?1:0;
133}
134
135void SetupCartCHRMapping(int chip, uint8 *p, uint32 size, int ram)
136{
137 CHRptr[chip]=p;
138 CHRsize[chip]=size;
139
140 CHRmask1[chip]=(size>>10)-1;
141 CHRmask2[chip]=(size>>11)-1;
142 CHRmask4[chip]=(size>>12)-1;
143 CHRmask8[chip]=(size>>13)-1;
144
145 CHRram[chip]=ram;
146}
147
148DECLFR(CartBR)
149{
150 return Page[A>>11][A];
151}
152
d97315ac 153DECLFW(CartBW)
c62d2810 154{
d97315ac 155 //printf("Ok: %04x:%02x, %d\n",A,V,PRGIsRAM[A>>11]);
156 if(PRGIsRAM[A>>11] && Page[A>>11])
157 Page[A>>11][A]=V;
158}
159
160DECLFR(CartBROB)
161{
162 if(!Page[A>>11]) return(X.DB);
163 return Page[A>>11][A];
164}
c62d2810 165
d97315ac 166void FASTAPASS(3) setprg2r(int r, unsigned int A, unsigned int V)
167{
168 V&=PRGmask2[r];
169 setpageptr(2,A,PRGptr[r]?(&PRGptr[r][V<<11]):0,PRGram[r]);
c62d2810 170}
171
172void FASTAPASS(2) setprg2(uint32 A, uint32 V)
173{
174 setprg2r(0,A,V);
175}
176
d97315ac 177void FASTAPASS(3) setprg4r(int r, unsigned int A, unsigned int V)
c62d2810 178{
c62d2810 179 V&=PRGmask4[r];
d97315ac 180 setpageptr(4,A,PRGptr[r]?(&PRGptr[r][V<<12]):0,PRGram[r]);
c62d2810 181}
182
183void FASTAPASS(2) setprg4(uint32 A, uint32 V)
184{
185 setprg4r(0,A,V);
186}
187
d97315ac 188void FASTAPASS(3) setprg8r(int r, unsigned int A, unsigned int V)
c62d2810 189{
c62d2810 190 if(PRGsize[r]>=8192)
191 {
192 V&=PRGmask8[r];
d97315ac 193 setpageptr(8,A,PRGptr[r]?(&PRGptr[r][V<<13]):0,PRGram[r]);
c62d2810 194 }
195 else
196 {
197 uint32 VA=V<<2;
198 int x;
199 for(x=0;x<4;x++)
d97315ac 200 setpageptr(2,A+(x<<11),PRGptr[r]?(&PRGptr[r][((VA+x)&PRGmask2[r])<<11]):0,PRGram[r]);
c62d2810 201 }
202}
203
204void FASTAPASS(2) setprg8(uint32 A, uint32 V)
205{
206 setprg8r(0,A,V);
207}
208
d97315ac 209void FASTAPASS(3) setprg16r(int r, unsigned int A, unsigned int V)
c62d2810 210{
c62d2810 211 if(PRGsize[r]>=16384)
212 {
213 V&=PRGmask16[r];
d97315ac 214 setpageptr(16,A,PRGptr[r]?(&PRGptr[r][V<<14]):0,PRGram[r]);
c62d2810 215 }
216 else
217 {
218 uint32 VA=V<<3;
219 int x;
220
221 for(x=0;x<8;x++)
d97315ac 222 setpageptr(2,A+(x<<11),PRGptr[r]?(&PRGptr[r][((VA+x)&PRGmask2[r])<<11]):0,PRGram[r]);
c62d2810 223 }
224}
225
226void FASTAPASS(2) setprg16(uint32 A, uint32 V)
227{
228 setprg16r(0,A,V);
229}
230
d97315ac 231void FASTAPASS(3) setprg32r(int r,unsigned int A, unsigned int V)
c62d2810 232{
c62d2810 233 if(PRGsize[r]>=32768)
234 {
235 V&=PRGmask32[r];
d97315ac 236 setpageptr(32,A,PRGptr[r]?(&PRGptr[r][V<<15]):0,PRGram[r]);
c62d2810 237 }
238 else
239 {
240 uint32 VA=V<<4;
241 int x;
242
243 for(x=0;x<16;x++)
d97315ac 244 setpageptr(2,A+(x<<11),PRGptr[r]?(&PRGptr[r][((VA+x)&PRGmask2[r])<<11]):0,PRGram[r]);
c62d2810 245 }
246}
247
248void FASTAPASS(2) setprg32(uint32 A, uint32 V)
249{
250 setprg32r(0,A,V);
251}
252
d97315ac 253void FASTAPASS(3) setchr1r(int r, unsigned int A, unsigned int V)
c62d2810 254{
255 if(!CHRptr[r]) return;
d97315ac 256 FCEUPPU_LineUpdate();
c62d2810 257 V&=CHRmask1[r];
258 if(CHRram[r])
259 PPUCHRRAM|=(1<<(A>>10));
260 else
261 PPUCHRRAM&=~(1<<(A>>10));
262 VPageR[(A)>>10]=&CHRptr[r][(V)<<10]-(A);
263}
264
d97315ac 265void FASTAPASS(3) setchr2r(int r, unsigned int A, unsigned int V)
c62d2810 266{
267 if(!CHRptr[r]) return;
d97315ac 268 FCEUPPU_LineUpdate();
c62d2810 269 V&=CHRmask2[r];
270 VPageR[(A)>>10]=VPageR[((A)>>10)+1]=&CHRptr[r][(V)<<11]-(A);
271 if(CHRram[r])
272 PPUCHRRAM|=(3<<(A>>10));
273 else
274 PPUCHRRAM&=~(3<<(A>>10));
275}
276
d97315ac 277void FASTAPASS(3) setchr4r(int r, unsigned int A, unsigned int V)
c62d2810 278{
279 if(!CHRptr[r]) return;
d97315ac 280 FCEUPPU_LineUpdate();
c62d2810 281 V&=CHRmask4[r];
282 VPageR[(A)>>10]=VPageR[((A)>>10)+1]=
283 VPageR[((A)>>10)+2]=VPageR[((A)>>10)+3]=&CHRptr[r][(V)<<12]-(A);
284 if(CHRram[r])
285 PPUCHRRAM|=(15<<(A>>10));
286 else
287 PPUCHRRAM&=~(15<<(A>>10));
288}
289
d97315ac 290void FASTAPASS(2) setchr8r(int r, unsigned int V)
c62d2810 291{
292 int x;
293
294 if(!CHRptr[r]) return;
d97315ac 295 FCEUPPU_LineUpdate();
c62d2810 296 V&=CHRmask8[r];
297 for(x=7;x>=0;x--)
298 VPageR[x]=&CHRptr[r][V<<13];
299 if(CHRram[r])
300 PPUCHRRAM|=(255);
301 else
302 PPUCHRRAM&=~(255);
303}
304
305void FASTAPASS(2) setchr1(unsigned int A, unsigned int V)
306{
307 setchr1r(0,A,V);
308}
309
310void FASTAPASS(2) setchr2(unsigned int A, unsigned int V)
311{
312 setchr2r(0,A,V);
313}
314
315void FASTAPASS(2) setchr4(unsigned int A, unsigned int V)
316{
317 setchr4r(0,A,V);
318}
319
d97315ac 320void FASTAPASS(1) setchr8(unsigned int V)
c62d2810 321{
322 setchr8r(0,V);
323}
324
325void FASTAPASS(1) setvram8(uint8 *p)
326{
327 int x;
328 for(x=7;x>=0;x--)
329 VPageR[x]=p;
330 PPUCHRRAM|=255;
331}
332
333void FASTAPASS(2) setvram4(uint32 A, uint8 *p)
334{
335 int x;
336 for(x=3;x>=0;x--)
337 VPageR[(A>>10)+x]=p-A;
338 PPUCHRRAM|=(15<<(A>>10));
339}
340
341void FASTAPASS(3) setvramb1(uint8 *p, uint32 A, uint32 b)
342{
d97315ac 343 FCEUPPU_LineUpdate();
c62d2810 344 VPageR[A>>10]=p-A+(b<<10);
345 PPUCHRRAM|=(1<<(A>>10));
346}
347
348void FASTAPASS(3) setvramb2(uint8 *p, uint32 A, uint32 b)
349{
d97315ac 350 FCEUPPU_LineUpdate();
c62d2810 351 VPageR[(A>>10)]=VPageR[(A>>10)+1]=p-A+(b<<11);
352 PPUCHRRAM|=(3<<(A>>10));
353}
354
355void FASTAPASS(3) setvramb4(uint8 *p, uint32 A, uint32 b)
356{
357 int x;
358
d97315ac 359 FCEUPPU_LineUpdate();
c62d2810 360 for(x=3;x>=0;x--)
361 VPageR[(A>>10)+x]=p-A+(b<<12);
362 PPUCHRRAM|=(15<<(A>>10));
363}
364
365void FASTAPASS(2) setvramb8(uint8 *p, uint32 b)
366{
367 int x;
368
d97315ac 369 FCEUPPU_LineUpdate();
c62d2810 370 for(x=7;x>=0;x--)
371 VPageR[x]=p+(b<<13);
372 PPUCHRRAM|=255;
373}
374
375/* This function can be called without calling SetupCartMirroring(). */
376
377void FASTAPASS(3) setntamem(uint8 *p, int ram, uint32 b)
378{
d97315ac 379 FCEUPPU_LineUpdate();
c62d2810 380 vnapage[b]=p;
381 PPUNTARAM&=~(1<<b);
382 if(ram)
383 PPUNTARAM|=1<<b;
384}
385
386static int mirrorhard=0;
387void setmirrorw(int a, int b, int c, int d)
388{
d97315ac 389 FCEUPPU_LineUpdate();
c62d2810 390 vnapage[0]=NTARAM+a*0x400;
391 vnapage[1]=NTARAM+b*0x400;
392 vnapage[2]=NTARAM+c*0x400;
393 vnapage[3]=NTARAM+d*0x400;
394}
395
396void FASTAPASS(1) setmirror(int t)
397{
d97315ac 398 FCEUPPU_LineUpdate();
c62d2810 399 if(!mirrorhard)
400 {
401 switch(t)
402 {
403 case MI_H:
404 vnapage[0]=vnapage[1]=NTARAM;vnapage[2]=vnapage[3]=NTARAM+0x400;
405 break;
406 case MI_V:
407 vnapage[0]=vnapage[2]=NTARAM;vnapage[1]=vnapage[3]=NTARAM+0x400;
408 break;
409 case MI_0:
410 vnapage[0]=vnapage[1]=vnapage[2]=vnapage[3]=NTARAM;
411 break;
412 case MI_1:
413 vnapage[0]=vnapage[1]=vnapage[2]=vnapage[3]=NTARAM+0x400;
414 break;
415 }
416 PPUNTARAM=0xF;
417 }
418}
419
420void SetupCartMirroring(int m, int hard, uint8 *extra)
421{
422 if(m<4)
d97315ac 423 {
424 mirrorhard = 0;
c62d2810 425 setmirror(m);
d97315ac 426 }
c62d2810 427 else
428 {
429 vnapage[0]=NTARAM;
430 vnapage[1]=NTARAM+0x400;
431 vnapage[2]=extra;
432 vnapage[3]=extra+0x400;
433 PPUNTARAM=0xF;
434 }
435 mirrorhard=hard;
436}
437
438static uint8 *GENIEROM=0;
439
440void FixGenieMap(void);
441
442/* Called when a game(file) is opened successfully. */
443void OpenGenie(void)
444{
445 FILE *fp;
446 int x;
447
448 if(!GENIEROM)
449 {
d97315ac 450 char *fn;
451
452 if(!(GENIEROM=(uint8 *)FCEU_malloc(4096+1024))) return;
c62d2810 453
d97315ac 454 fn=FCEU_MakeFName(FCEUMKF_GGROM,0,0);
455 fp=fopen(fn,"rb");
92764e62 456 free(fn);
d97315ac 457 if(!fp)
c62d2810 458 {
459 FCEU_PrintError("Error opening Game Genie ROM image!");
460 free(GENIEROM);
461 GENIEROM=0;
462 return;
463 }
464 if(fread(GENIEROM,1,16,fp)!=16)
465 {
466 grerr:
467 FCEU_PrintError("Error reading from Game Genie ROM image!");
468 free(GENIEROM);
469 GENIEROM=0;
470 fclose(fp);
471 return;
472 }
d97315ac 473 if(GENIEROM[0]==0x4E) /* iNES ROM image */
c62d2810 474 {
475 if(fread(GENIEROM,1,4096,fp)!=4096)
476 goto grerr;
477 if(fseek(fp,16384-4096,SEEK_CUR))
478 goto grerr;
479 if(fread(GENIEROM+4096,1,256,fp)!=256)
480 goto grerr;
481 }
482 else
483 {
484 if(fread(GENIEROM+16,1,4352-16,fp)!=(4352-16))
485 goto grerr;
486 }
487 fclose(fp);
937bf65b 488
c62d2810 489 /* Workaround for the FCE Ultra CHR page size only being 1KB */
490 for(x=0;x<4;x++)
491 memcpy(GENIEROM+4096+(x<<8),GENIEROM+4096,256);
492 }
493
494 geniestage=1;
495}
496
497/* Called when a game is closed. */
498void CloseGenie(void)
499{
500 /* No good reason to free() the Game Genie ROM image data. */
501 geniestage=0;
502 FlushGenieRW();
503 VPageR=VPage;
504}
505
d97315ac 506void FCEU_KillGenie(void)
507{
508 if(GENIEROM)
509 {
510 free(GENIEROM);
511 GENIEROM=0;
512 }
513}
514
c62d2810 515static DECLFR(GenieRead)
516{
517 return GENIEROM[A&4095];
518}
519
520static DECLFW(GenieWrite)
521{
522 switch(A)
523 {
524 case 0x800c:
525 case 0x8008:
526 case 0x8004:genieval[((A-4)&0xF)>>2]=V;break;
527
528 case 0x800b:
529 case 0x8007:
530 case 0x8003:geniech[((A-3)&0xF)>>2]=V;break;
531
532 case 0x800a:
533 case 0x8006:
534 case 0x8002:genieaddr[((A-2)&0xF)>>2]&=0xFF00;genieaddr[((A-2)&0xF)>>2]|=V;break;
535
536 case 0x8009:
537 case 0x8005:
538 case 0x8001:genieaddr[((A-1)&0xF)>>2]&=0xFF;genieaddr[((A-1)&0xF)>>2]|=(V|0x80)<<8;break;
539
540 case 0x8000:if(!V)
d97315ac 541 FixGenieMap();
542 else
543 {
544 modcon=V^0xFF;
545 if(V==0x71)
546 modcon=0;
547 }
548 break;
c62d2810 549 }
550}
551
552static readfunc GenieBackup[3];
553
554static DECLFR(GenieFix1)
555{
556 uint8 r=GenieBackup[0](A);
557
d97315ac 558 if((modcon>>1)&1) // No check
c62d2810 559 return genieval[0];
560 else if(r==geniech[0])
561 return genieval[0];
562
563 return r;
564}
565
566static DECLFR(GenieFix2)
567{
568 uint8 r=GenieBackup[1](A);
569
d97315ac 570 if((modcon>>2)&1) // No check
c62d2810 571 return genieval[1];
572 else if(r==geniech[1])
573 return genieval[1];
574
575 return r;
576}
577
578static DECLFR(GenieFix3)
579{
580 uint8 r=GenieBackup[2](A);
581
d97315ac 582 if((modcon>>3)&1) // No check
c62d2810 583 return genieval[2];
584 else if(r==geniech[2])
585 return genieval[2];
586
587 return r;
588}
589
590
591void FixGenieMap(void)
592{
593 int x;
594
595 geniestage=2;
596
597 for(x=0;x<8;x++)
598 VPage[x]=VPageG[x];
599
600 VPageR=VPage;
601 FlushGenieRW();
d97315ac 602 //printf("Rightyo\n");
c62d2810 603 for(x=0;x<3;x++)
604 if((modcon>>(4+x))&1)
605 {
606 readfunc tmp[3]={GenieFix1,GenieFix2,GenieFix3};
607 GenieBackup[x]=GetReadHandler(genieaddr[x]);
608 SetReadHandler(genieaddr[x],genieaddr[x],tmp[x]);
609 }
610}
611
612void GeniePower(void)
613{
614 uint32 x;
615
616 if(!geniestage)
617 return;
618
619 geniestage=1;
620 for(x=0;x<3;x++)
621 {
622 genieval[x]=0xFF;
623 geniech[x]=0xFF;
624 genieaddr[x]=0xFFFF;
625 }
626 modcon=0;
627
628 SetWriteHandler(0x8000,0xFFFF,GenieWrite);
629 SetReadHandler(0x8000,0xFFFF,GenieRead);
630
631 for(x=0;x<8;x++)
632 VPage[x]=GENIEROM+4096-0x400*x;
633
634 if(AllocGenieRW())
635 VPageR=VPageG;
636 else
637 geniestage=2;
638}
639
640
d97315ac 641void FCEU_SaveGameSave(CartInfo *LocalHWInfo)
642{
643 if(LocalHWInfo->battery && LocalHWInfo->SaveGame[0])
644 {
645 FILE *sp;
646 char *soot;
647
648 soot=FCEU_MakeFName(FCEUMKF_SAV,0,"sav");
649 if((sp=FCEUD_UTF8fopen(soot,"wb"))==NULL)
650 {
651 FCEU_PrintError("WRAM file \"%s\" cannot be written to.\n",soot);
652 }
653 else
654 {
655 int x;
656
657 for(x=0;x<4;x++)
658 if(LocalHWInfo->SaveGame[x])
659 {
660 fwrite(LocalHWInfo->SaveGame[x],1,
661 LocalHWInfo->SaveGameLen[x],sp);
662 }
663 }
664 free(soot);
665 }
666}
667
668// hack, movie.c has to communicate with this function somehow
669int disableBatteryLoading=0;
670
671void FCEU_LoadGameSave(CartInfo *LocalHWInfo)
672{
673 if(LocalHWInfo->battery && LocalHWInfo->SaveGame[0] && !disableBatteryLoading)
674 {
675 FILE *sp;
676 char *soot;
677
678 soot=FCEU_MakeFName(FCEUMKF_SAV,0,"sav");
679 sp=FCEUD_UTF8fopen(soot,"rb");
680 if(sp!=NULL)
681 {
682 int x;
683 for(x=0;x<4;x++)
684 if(LocalHWInfo->SaveGame[x])
685 fread(LocalHWInfo->SaveGame[x],1,LocalHWInfo->SaveGameLen[x],sp);
686 }
687 free(soot);
688 }
689}
690
691