ff9aceb7aa977c3827ce11dafcf0530f0243fa45
[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   free(fn);
457   if(!fp)
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   }
473   if(GENIEROM[0]==0x4E)  /* iNES ROM image */
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);
488
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. */
498 void 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
506 void FCEU_KillGenie(void)
507 {
508  if(GENIEROM)
509  {
510   free(GENIEROM);
511   GENIEROM=0;
512  }
513 }
514
515 static DECLFR(GenieRead)
516 {
517  return GENIEROM[A&4095];
518 }
519
520 static 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)
541          FixGenieMap();
542         else
543         {
544          modcon=V^0xFF;
545          if(V==0x71)
546     modcon=0;
547         }
548         break;
549  }
550 }
551
552 static readfunc GenieBackup[3];
553
554 static DECLFR(GenieFix1)
555 {
556  uint8 r=GenieBackup[0](A);
557
558  if((modcon>>1)&1)    // No check
559   return genieval[0];
560  else if(r==geniech[0])
561   return genieval[0];
562
563  return r;
564 }
565
566 static DECLFR(GenieFix2)
567 {
568  uint8 r=GenieBackup[1](A);
569
570  if((modcon>>2)&1)        // No check
571   return genieval[1];
572  else if(r==geniech[1])
573   return genieval[1];
574
575  return r;
576 }
577
578 static DECLFR(GenieFix3)
579 {
580  uint8 r=GenieBackup[2](A);
581
582  if((modcon>>3)&1)        // No check
583   return genieval[2];
584  else if(r==geniech[2])
585   return genieval[2];
586
587  return r;
588 }
589
590
591 void 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();
602  //printf("Rightyo\n");
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
612 void 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
641 void 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
669 int disableBatteryLoading=0;
670
671 void 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