f8883972bad25398f7cc9fb95bb3e697cef057d8
[fceu.git] / cart.c
1 /* FCE Ultra - NES/Famicom Emulator
2  *
3  * Copyright notice for this file:
4  *  Copyright (C) 2002 Xodnizel
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>
23 #include <stdio.h>
24
25 #include "types.h"
26 #include "fce.h"
27 #include "ppu.h"
28
29 #include "cart.h"
30 #include "memory.h"
31 #include "x6502.h"
32
33 #include "general.h"
34
35 #include "svga.h"
36 #include "file.h"
37
38 #define FCEUPPU_LineUpdate(...)
39
40 /*
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
46 uint8 *Page[32],*VPage[8];
47 uint8 **VPageR=VPage;
48 uint8 *VPageG[8];
49 uint8 *MMC5SPRVPage[8];
50 uint8 *MMC5BGVPage[8];
51
52 static uint8 PRGIsRAM[32];  /* This page is/is not PRG RAM. */
53
54 /* 16 are (sort of) reserved for UNIF/iNES and 16 to map other stuff. */
55 static int CHRram[32];
56 static int PRGram[32];
57
58 uint8 *PRGptr[32];
59 uint8 *CHRptr[32];
60
61 uint32 PRGsize[32];
62 uint32 CHRsize[32];
63
64 uint32 PRGmask2[32];
65 uint32 PRGmask4[32];
66 uint32 PRGmask8[32];
67 uint32 PRGmask16[32];
68 uint32 PRGmask32[32];
69
70 uint32 CHRmask1[32];
71 uint32 CHRmask2[32];
72 uint32 CHRmask4[32];
73 uint32 CHRmask8[32];
74
75 int geniestage=0;
76
77 int modcon;
78
79 uint8 genieval[3];
80 uint8 geniech[3];
81
82 uint32 genieaddr[3];
83
84 static INLINE void setpageptr(int s, uint32 A, uint8 *p, int ram)
85 {
86  uint32 AB=A>>11;
87  int x;
88
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   }
101 }
102
103 static uint8 nothing[8192];
104 void ResetCartMapping(void)
105 {
106  int x;
107
108  for(x=0;x<32;x++)
109  {
110   Page[x]=nothing-x*2048;
111   PRGptr[x]=CHRptr[x]=0;
112   PRGsize[x]=CHRsize[x]=0;
113  }
114  for(x=0;x<8;x++)
115  {
116   MMC5SPRVPage[x]=MMC5BGVPage[x]=VPageR[x]=nothing-0x400*x;
117  }
118
119 }
120
121 void 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;
130  PRGmask32[chip]=(size>>15)-1;
131
132  PRGram[chip]=ram?1:0;
133 }
134
135 void 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
148 DECLFR(CartBR)
149 {
150  return Page[A>>11][A];
151 }
152
153 DECLFW(CartBW)
154 {
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
160 DECLFR(CartBROB)
161 {
162  if(!Page[A>>11]) return(X.DB);
163  return Page[A>>11][A];
164 }
165
166 void 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]);
170 }
171
172 void FASTAPASS(2) setprg2(uint32 A, uint32 V)
173 {
174  setprg2r(0,A,V);
175 }
176
177 void FASTAPASS(3) setprg4r(int r, unsigned int A, unsigned int V)
178 {
179   V&=PRGmask4[r];
180   setpageptr(4,A,PRGptr[r]?(&PRGptr[r][V<<12]):0,PRGram[r]);
181 }
182
183 void FASTAPASS(2) setprg4(uint32 A, uint32 V)
184 {
185  setprg4r(0,A,V);
186 }
187
188 void FASTAPASS(3) setprg8r(int r, unsigned int A, unsigned int V)
189 {
190   if(PRGsize[r]>=8192)
191   {
192    V&=PRGmask8[r];
193    setpageptr(8,A,PRGptr[r]?(&PRGptr[r][V<<13]):0,PRGram[r]);
194   }
195   else
196   {
197    uint32 VA=V<<2;
198    int x;
199    for(x=0;x<4;x++)
200     setpageptr(2,A+(x<<11),PRGptr[r]?(&PRGptr[r][((VA+x)&PRGmask2[r])<<11]):0,PRGram[r]);
201   }
202 }
203
204 void FASTAPASS(2) setprg8(uint32 A, uint32 V)
205 {
206  setprg8r(0,A,V);
207 }
208
209 void FASTAPASS(3) setprg16r(int r, unsigned int A, unsigned int V)
210 {
211   if(PRGsize[r]>=16384)
212   {
213    V&=PRGmask16[r];
214    setpageptr(16,A,PRGptr[r]?(&PRGptr[r][V<<14]):0,PRGram[r]);
215   }
216   else
217   {
218    uint32 VA=V<<3;
219    int x;
220
221    for(x=0;x<8;x++)
222     setpageptr(2,A+(x<<11),PRGptr[r]?(&PRGptr[r][((VA+x)&PRGmask2[r])<<11]):0,PRGram[r]);
223   }
224 }
225
226 void FASTAPASS(2) setprg16(uint32 A, uint32 V)
227 {
228  setprg16r(0,A,V);
229 }
230
231 void FASTAPASS(3) setprg32r(int r,unsigned int A, unsigned int V)
232 {
233   if(PRGsize[r]>=32768)
234   {
235    V&=PRGmask32[r];
236    setpageptr(32,A,PRGptr[r]?(&PRGptr[r][V<<15]):0,PRGram[r]);
237   }
238   else
239   {
240    uint32 VA=V<<4;
241    int x;
242
243    for(x=0;x<16;x++)
244     setpageptr(2,A+(x<<11),PRGptr[r]?(&PRGptr[r][((VA+x)&PRGmask2[r])<<11]):0,PRGram[r]);
245   }
246 }
247
248 void FASTAPASS(2) setprg32(uint32 A, uint32 V)
249 {
250  setprg32r(0,A,V);
251 }
252
253 void FASTAPASS(3) setchr1r(int r, unsigned int A, unsigned int V)
254 {
255   if(!CHRptr[r]) return;
256   FCEUPPU_LineUpdate();
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
265 void FASTAPASS(3) setchr2r(int r, unsigned int A, unsigned int V)
266 {
267   if(!CHRptr[r]) return;
268   FCEUPPU_LineUpdate();
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
277 void FASTAPASS(3) setchr4r(int r, unsigned int A, unsigned int V)
278 {
279   if(!CHRptr[r]) return;
280   FCEUPPU_LineUpdate();
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
290 void FASTAPASS(2) setchr8r(int r, unsigned int V)
291 {
292   int x;
293
294   if(!CHRptr[r]) return;
295   FCEUPPU_LineUpdate();
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
305 void FASTAPASS(2) setchr1(unsigned int A, unsigned int V)
306 {
307  setchr1r(0,A,V);
308 }
309
310 void FASTAPASS(2) setchr2(unsigned int A, unsigned int V)
311 {
312  setchr2r(0,A,V);
313 }
314
315 void FASTAPASS(2) setchr4(unsigned int A, unsigned int V)
316 {
317  setchr4r(0,A,V);
318 }
319
320 void FASTAPASS(1) setchr8(unsigned int V)
321 {
322  setchr8r(0,V);
323 }
324
325 void 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
333 void 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
341 void FASTAPASS(3) setvramb1(uint8 *p, uint32 A, uint32 b)
342 {
343   FCEUPPU_LineUpdate();
344   VPageR[A>>10]=p-A+(b<<10);
345   PPUCHRRAM|=(1<<(A>>10));
346 }
347
348 void FASTAPASS(3) setvramb2(uint8 *p, uint32 A, uint32 b)
349 {
350   FCEUPPU_LineUpdate();
351   VPageR[(A>>10)]=VPageR[(A>>10)+1]=p-A+(b<<11);
352   PPUCHRRAM|=(3<<(A>>10));
353 }
354
355 void FASTAPASS(3) setvramb4(uint8 *p, uint32 A, uint32 b)
356 {
357   int x;
358
359   FCEUPPU_LineUpdate();
360   for(x=3;x>=0;x--)
361    VPageR[(A>>10)+x]=p-A+(b<<12);
362   PPUCHRRAM|=(15<<(A>>10));
363 }
364
365 void FASTAPASS(2) setvramb8(uint8 *p, uint32 b)
366 {
367   int x;
368
369   FCEUPPU_LineUpdate();
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
377 void FASTAPASS(3) setntamem(uint8 *p, int ram, uint32 b)
378 {
379  FCEUPPU_LineUpdate();
380  vnapage[b]=p;
381  PPUNTARAM&=~(1<<b);
382  if(ram)
383   PPUNTARAM|=1<<b;
384 }
385
386 static int mirrorhard=0;
387 void setmirrorw(int a, int b, int c, int d)
388 {
389  FCEUPPU_LineUpdate();
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
396 void FASTAPASS(1) setmirror(int t)
397 {
398   FCEUPPU_LineUpdate();
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
420 void SetupCartMirroring(int m, int hard, uint8 *extra)
421 {
422  if(m<4)
423  {
424   mirrorhard = 0;
425   setmirror(m);
426  }
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
438 static uint8 *GENIEROM=0;
439
440 void FixGenieMap(void);
441
442 /* Called when a game(file) is opened successfully. */
443 void OpenGenie(void)
444 {
445  FILE *fp;
446  int x;
447
448  if(!GENIEROM)
449  {
450   char *fn;
451
452   if(!(GENIEROM=(uint8 *)FCEU_malloc(4096+1024))) return;
453
454   fn=FCEU_MakeFName(FCEUMKF_GGROM,0,0);
455   fp=fopen(fn,"rb");
456   if(!fp)
457   {
458    FCEU_PrintError("Error opening Game Genie ROM image!");
459    free(GENIEROM);
460    GENIEROM=0;
461    return;
462   }
463   if(fread(GENIEROM,1,16,fp)!=16)
464   {
465    grerr:
466    FCEU_PrintError("Error reading from Game Genie ROM image!");
467    free(GENIEROM);
468    GENIEROM=0;
469    fclose(fp);
470    return;
471   }
472   if(GENIEROM[0]==0x4E)  /* iNES ROM image */
473   {
474    if(fread(GENIEROM,1,4096,fp)!=4096)
475     goto grerr;
476    if(fseek(fp,16384-4096,SEEK_CUR))
477     goto grerr;
478    if(fread(GENIEROM+4096,1,256,fp)!=256)
479     goto grerr;
480   }
481   else
482   {
483    if(fread(GENIEROM+16,1,4352-16,fp)!=(4352-16))
484     goto grerr;
485   }
486   fclose(fp);
487
488   /* Workaround for the FCE Ultra CHR page size only being 1KB */
489   for(x=0;x<4;x++)
490    memcpy(GENIEROM+4096+(x<<8),GENIEROM+4096,256);
491  }
492
493  geniestage=1;
494 }
495
496 /* Called when a game is closed. */
497 void CloseGenie(void)
498 {
499  /* No good reason to free() the Game Genie ROM image data. */
500  geniestage=0;
501  FlushGenieRW();
502  VPageR=VPage;
503 }
504
505 void FCEU_KillGenie(void)
506 {
507  if(GENIEROM)
508  {
509   free(GENIEROM);
510   GENIEROM=0;
511  }
512 }
513
514 static DECLFR(GenieRead)
515 {
516  return GENIEROM[A&4095];
517 }
518
519 static DECLFW(GenieWrite)
520 {
521  switch(A)
522  {
523   case 0x800c:
524   case 0x8008:
525   case 0x8004:genieval[((A-4)&0xF)>>2]=V;break;
526
527   case 0x800b:
528   case 0x8007:
529   case 0x8003:geniech[((A-3)&0xF)>>2]=V;break;
530
531   case 0x800a:
532   case 0x8006:
533   case 0x8002:genieaddr[((A-2)&0xF)>>2]&=0xFF00;genieaddr[((A-2)&0xF)>>2]|=V;break;
534
535   case 0x8009:
536   case 0x8005:
537   case 0x8001:genieaddr[((A-1)&0xF)>>2]&=0xFF;genieaddr[((A-1)&0xF)>>2]|=(V|0x80)<<8;break;
538
539   case 0x8000:if(!V)
540          FixGenieMap();
541         else
542         {
543          modcon=V^0xFF;
544          if(V==0x71)
545     modcon=0;
546         }
547         break;
548  }
549 }
550
551 static readfunc GenieBackup[3];
552
553 static DECLFR(GenieFix1)
554 {
555  uint8 r=GenieBackup[0](A);
556
557  if((modcon>>1)&1)    // No check
558   return genieval[0];
559  else if(r==geniech[0])
560   return genieval[0];
561
562  return r;
563 }
564
565 static DECLFR(GenieFix2)
566 {
567  uint8 r=GenieBackup[1](A);
568
569  if((modcon>>2)&1)        // No check
570   return genieval[1];
571  else if(r==geniech[1])
572   return genieval[1];
573
574  return r;
575 }
576
577 static DECLFR(GenieFix3)
578 {
579  uint8 r=GenieBackup[2](A);
580
581  if((modcon>>3)&1)        // No check
582   return genieval[2];
583  else if(r==geniech[2])
584   return genieval[2];
585
586  return r;
587 }
588
589
590 void FixGenieMap(void)
591 {
592  int x;
593
594  geniestage=2;
595
596  for(x=0;x<8;x++)
597   VPage[x]=VPageG[x];
598
599  VPageR=VPage;
600  FlushGenieRW();
601  //printf("Rightyo\n");
602  for(x=0;x<3;x++)
603   if((modcon>>(4+x))&1)
604   {
605    readfunc tmp[3]={GenieFix1,GenieFix2,GenieFix3};
606    GenieBackup[x]=GetReadHandler(genieaddr[x]);
607    SetReadHandler(genieaddr[x],genieaddr[x],tmp[x]);
608   }
609 }
610
611 void GeniePower(void)
612 {
613  uint32 x;
614
615  if(!geniestage)
616   return;
617
618  geniestage=1;
619  for(x=0;x<3;x++)
620  {
621   genieval[x]=0xFF;
622   geniech[x]=0xFF;
623   genieaddr[x]=0xFFFF;
624  }
625  modcon=0;
626
627  SetWriteHandler(0x8000,0xFFFF,GenieWrite);
628  SetReadHandler(0x8000,0xFFFF,GenieRead);
629
630  for(x=0;x<8;x++)
631   VPage[x]=GENIEROM+4096-0x400*x;
632
633  if(AllocGenieRW())
634   VPageR=VPageG;
635  else
636   geniestage=2;
637 }
638
639
640 void FCEU_SaveGameSave(CartInfo *LocalHWInfo)
641 {
642  if(LocalHWInfo->battery && LocalHWInfo->SaveGame[0])
643  {
644   FILE *sp;
645   char *soot;
646
647   soot=FCEU_MakeFName(FCEUMKF_SAV,0,"sav");
648   if((sp=FCEUD_UTF8fopen(soot,"wb"))==NULL)
649   {
650    FCEU_PrintError("WRAM file \"%s\" cannot be written to.\n",soot);
651   }
652   else
653   {
654    int x;
655
656    for(x=0;x<4;x++)
657     if(LocalHWInfo->SaveGame[x])
658     {
659      fwrite(LocalHWInfo->SaveGame[x],1,
660       LocalHWInfo->SaveGameLen[x],sp);
661     }
662   }
663   free(soot);
664  }
665 }
666
667 // hack, movie.c has to communicate with this function somehow
668 int disableBatteryLoading=0;
669
670 void FCEU_LoadGameSave(CartInfo *LocalHWInfo)
671 {
672  if(LocalHWInfo->battery && LocalHWInfo->SaveGame[0] && !disableBatteryLoading)
673  {
674   FILE *sp;
675   char *soot;
676
677   soot=FCEU_MakeFName(FCEUMKF_SAV,0,"sav");
678   sp=FCEUD_UTF8fopen(soot,"rb");
679   if(sp!=NULL)
680   {
681    int x;
682    for(x=0;x<4;x++)
683     if(LocalHWInfo->SaveGame[x])
684      fread(LocalHWInfo->SaveGame[x],1,LocalHWInfo->SaveGameLen[x],sp);
685   }
686   free(soot);
687  }
688 }
689
690