1 /* FCE Ultra - NES/Famicom Emulator
3 * Copyright notice for this file:
4 * Copyright (C) 2002 Ben Parnell
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.
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.
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
21 /* None of this code should use any of the iNES bank switching wrappers. */
25 void MMC5Sound(int Count);
28 static INLINE void MMC5SPRVROM_BANK1(uint32 A,uint32 V)
33 MMC5SPRVPage[(A)>>10]=&CHRptr[0][(V)<<10]-(A);
37 static INLINE void MMC5BGVROM_BANK1(uint32 A,uint32 V) {if(CHRptr[0]){V&=CHRmask1[0];MMC5BGVPage[(A)>>10]=&CHRptr[0][(V)<<10]-(A);}}
39 static INLINE void MMC5SPRVROM_BANK2(uint32 A,uint32 V) {if(CHRptr[0]){V&=CHRmask2[0];MMC5SPRVPage[(A)>>10]=MMC5SPRVPage[((A)>>10)+1]=&CHRptr[0][(V)<<11]-(A);}}
40 static INLINE void MMC5BGVROM_BANK2(uint32 A,uint32 V) {if(CHRptr[0]){V&=CHRmask2[0];MMC5BGVPage[(A)>>10]=MMC5BGVPage[((A)>>10)+1]=&CHRptr[0][(V)<<11]-(A);}}
42 static INLINE void MMC5SPRVROM_BANK4(uint32 A,uint32 V) {if(CHRptr[0]){V&=CHRmask4[0];MMC5SPRVPage[(A)>>10]=MMC5SPRVPage[((A)>>10)+1]= MMC5SPRVPage[((A)>>10)+2]=MMC5SPRVPage[((A)>>10)+3]=&CHRptr[0][(V)<<12]-(A);}}
43 static INLINE void MMC5BGVROM_BANK4(uint32 A,uint32 V) {if(CHRptr[0]){V&=CHRmask4[0];MMC5BGVPage[(A)>>10]=MMC5BGVPage[((A)>>10)+1]=MMC5BGVPage[((A)>>10)+2]=MMC5BGVPage[((A)>>10)+3]=&CHRptr[0][(V)<<12]-(A);}}
45 static INLINE void MMC5SPRVROM_BANK8(uint32 V) {if(CHRptr[0]){V&=CHRmask8[0];MMC5SPRVPage[0]=MMC5SPRVPage[1]=MMC5SPRVPage[2]=MMC5SPRVPage[3]=MMC5SPRVPage[4]=MMC5SPRVPage[5]=MMC5SPRVPage[6]=MMC5SPRVPage[7]=&CHRptr[0][(V)<<13];}}
46 static INLINE void MMC5BGVROM_BANK8(uint32 V) {if(CHRptr[0]){V&=CHRmask8[0];MMC5BGVPage[0]=MMC5BGVPage[1]=MMC5BGVPage[2]=MMC5BGVPage[3]=MMC5BGVPage[4]=MMC5BGVPage[5]=MMC5BGVPage[6]=MMC5BGVPage[7]=&CHRptr[0][(V)<<13];}}
49 uint8 MMC5fill[0x400] __attribute__ ((aligned (4)));
51 #define MMC5IRQR mapbyte3[4]
52 #define MMC5LineCounter mapbyte3[5]
53 #define mmc5psize mapbyte1[0]
54 #define mmc5vsize mapbyte1[1]
57 uint8 MMC5WRAMIndex[8];
59 uint8 MMC5ROMWrProtect[4];
62 static void MMC5CHRA(void);
63 static void MMC5CHRB(void);
65 typedef struct __cartdata {
71 // ETROM seems to have 16KB of WRAM, ELROM seems to have 8KB
72 // EWROM seems to have 32KB of WRAM
74 #define MMC5_NOCARTS 14
75 cartdata MMC5CartList[MMC5_NOCARTS]=
77 {0x9c18762b,2}, /* L'Empereur */
81 {0xaca15643,2}, /* Uncharted Waters */
82 {0xfe3488d1,2}, /* Dai Koukai Jidai */
84 {0x15fe6d0f,2}, /* BKAC */
85 {0x39f2ce4b,2}, /* Suikoden */
87 {0x8ce478db,2}, /* Nobunaga's Ambition 2 */
90 {0x1ced086f,2}, /* Ishin no Arashi */
92 {0xf540677b,4}, /* Nobunaga...Bushou Fuuun Roku */
94 {0x6f4e4312,4}, /* Aoki Ookami..Genchou */
96 {0xf011e490,4}, /* Romance of the 3 Kingdoms 2 */
97 {0x184c2124,4}, /* Sangokushi 2 */
101 // Called by iNESLoad()
102 void DetectMMC5WRAMSize(void)
108 for(x=0;x<MMC5_NOCARTS;x++)
109 if(iNESGameCRC32==MMC5CartList[x].crc32)
111 MMC5WRAMsize=MMC5CartList[x].size;
116 static void BuildWRAMSizeTable(void)
125 case 1:MMC5WRAMIndex[x]=(x>3)?255:0;break;
126 case 2:MMC5WRAMIndex[x]=(x&4)>>2;break;
127 case 4:MMC5WRAMIndex[x]=(x>3)?255:(x&3);break;
132 static void MMC5CHRA(void)
135 switch(mapbyte1[1]&3)
137 case 0:MMC5SPRVROM_BANK8(mapbyte2[7]);
138 setchr8(mapbyte2[7]);
140 case 1:MMC5SPRVROM_BANK4(0x0000,mapbyte2[3]);
141 MMC5SPRVROM_BANK4(0x1000,mapbyte2[7]);
142 setchr4(0x0000,mapbyte2[3]);
143 setchr4(0x1000,mapbyte2[7]);
145 case 2:MMC5SPRVROM_BANK2(0x0000,mapbyte2[1]);
146 MMC5SPRVROM_BANK2(0x0800,mapbyte2[3]);
147 MMC5SPRVROM_BANK2(0x1000,mapbyte2[5]);
148 MMC5SPRVROM_BANK2(0x1800,mapbyte2[7]);
149 setchr2(0x0000,mapbyte2[1]);
150 setchr2(0x0800,mapbyte2[3]);
151 setchr2(0x1000,mapbyte2[5]);
152 setchr2(0x1800,mapbyte2[7]);
157 setchr1(x<<10,mapbyte2[x]);
158 MMC5SPRVROM_BANK1(x<<10,mapbyte2[x]);
164 static void MMC5CHRB(void)
167 switch(mapbyte1[1]&3)
169 case 0:MMC5BGVROM_BANK8(mapbyte3[3]);
170 setchr8(mapbyte3[3]);
173 MMC5BGVROM_BANK4(0x0000,mapbyte3[3]);
174 MMC5BGVROM_BANK4(0x1000,mapbyte3[3]);
175 setchr4(0x0000,mapbyte3[3]);
176 setchr4(0x1000,mapbyte3[3]);
178 case 2:MMC5BGVROM_BANK2(0x0000,mapbyte3[1]);
179 MMC5BGVROM_BANK2(0x0800,mapbyte3[3]);
180 MMC5BGVROM_BANK2(0x1000,mapbyte3[1]);
181 MMC5BGVROM_BANK2(0x1800,mapbyte3[3]);
182 setchr2(0x0000,mapbyte3[1]);
183 setchr2(0x0800,mapbyte3[3]);
184 setchr2(0x1000,mapbyte3[1]);
185 setchr2(0x1800,mapbyte3[3]);
190 setchr1(x<<10,mapbyte3[x&3]);
191 MMC5BGVROM_BANK1(x<<10,mapbyte3[x&3]);
197 static void FASTAPASS(2) MMC5WRAM(uint32 A, uint32 V)
199 V=MMC5WRAMIndex[V&7];
203 MMC5MemIn[(A-0x6000)>>13]=1;
206 MMC5MemIn[(A-0x6000)>>13]=0;
209 static void MMC5PRG(void)
213 switch(mapbyte1[0]&3)
216 MMC5ROMWrProtect[0]=MMC5ROMWrProtect[1]=
217 MMC5ROMWrProtect[2]=MMC5ROMWrProtect[3]=1;
218 setprg32(0x8000,((mapbyte1[5]&0x7F)>>2));
226 MMC5ROMWrProtect[0]=MMC5ROMWrProtect[1]=1;
227 setprg16(0x8000,(mapbyte1[5]>>1));
228 MMC5MemIn[1]=MMC5MemIn[2]=1;
232 MMC5ROMWrProtect[0]=MMC5ROMWrProtect[1]=0;
233 MMC5WRAM(0x8000,mapbyte1[5]&7&0xFE);
234 MMC5WRAM(0xA000,(mapbyte1[5]&7&0xFE)+1);
236 MMC5MemIn[3]=MMC5MemIn[4]=1;
237 MMC5ROMWrProtect[2]=MMC5ROMWrProtect[3]=1;
238 setprg16(0xC000,(mapbyte1[7]&0x7F)>>1);
243 MMC5MemIn[1]=MMC5MemIn[2]=1;
244 MMC5ROMWrProtect[0]=MMC5ROMWrProtect[1]=1;
245 setprg16(0x8000,(mapbyte1[5]&0x7F)>>1);
249 MMC5ROMWrProtect[0]=MMC5ROMWrProtect[1]=0;
250 MMC5WRAM(0x8000,mapbyte1[5]&7&0xFE);
251 MMC5WRAM(0xA000,(mapbyte1[5]&7&0xFE)+1);
254 {MMC5ROMWrProtect[2]=1;MMC5MemIn[3]=1;setprg8(0xC000,mapbyte1[6]&0x7F);}
256 {MMC5ROMWrProtect[2]=0;MMC5WRAM(0xC000,mapbyte1[6]&7);}
258 MMC5ROMWrProtect[3]=1;
259 setprg8(0xE000,mapbyte1[7]&0x7F);
263 if(mapbyte1[4+x]&0x80)
265 MMC5ROMWrProtect[0]=1;
266 setprg8(0x8000+(x<<13),mapbyte1[4+x]&0x7F);
271 MMC5ROMWrProtect[0]=0;
272 MMC5WRAM(0x8000+(x<<13),mapbyte1[4+x]&7);
275 MMC5ROMWrProtect[3]=1;
276 setprg8(0xE000,mapbyte1[7]&0x7F);
281 #define mul1 mapbyte3[6]
282 #define mul2 mapbyte3[7]
284 static DECLFW(Mapper5_write)
294 switch((V>>(x<<1))&3)
296 case 0:PPUNTARAM|=1<<x;vnapage[x]=NTARAM;break;
297 case 1:PPUNTARAM|=1<<x;vnapage[x]=NTARAM+0x400;break;
298 case 2:PPUNTARAM|=1<<x;vnapage[x]=MapperExRAM+0x6000;break;
299 case 3:PPUNTARAM&=~(1<<x);vnapage[x]=MMC5fill;break;
306 case 0x5113:mapbyte4[6]=V;MMC5WRAM(0x6000,V&7);
307 X6502_Rebase();break;
308 case 0x5100:mapbyte1[0]=V;MMC5PRG();
309 X6502_Rebase();break;
310 case 0x5101:mapbyte1[1]=V;
312 {MMC5CHRB();MMC5CHRA();}
314 {MMC5CHRA();MMC5CHRB();}
321 mapbyte1[A&7]=V;MMC5PRG();
322 X6502_Rebase();break;
331 case 0x5127:mapbyte4[7]=0;
332 mapbyte2[A&7]=V;MMC5CHRA();break;
336 case 0x512b:mapbyte4[7]=1;
337 mapbyte3[A&3]=V;MMC5CHRB();break;
338 case 0x5102:mapbyte4[0]=V;break;
339 case 0x5103:mapbyte4[1]=V;break;
340 case 0x5104:mapbyte4[2]=V;MMC5HackCHRMode=V&3;break;
341 case 0x5106:if(V!=mapbyte4[4])
344 t=V|(V<<8)|(V<<16)|(V<<24);
345 FCEU_dwmemset(MMC5fill,t,0x3c0);
349 case 0x5107:if(V!=mapbyte4[5])
353 moop=V|(V<<2)|(V<<4)|(V<<6);
354 t=moop|(moop<<8)|(moop<<16)|(moop<<24);
355 FCEU_dwmemset(MMC5fill+0x3c0,t,0x40);
359 case 0x5200:MMC5HackSPMode=V;break;
360 case 0x5201:MMC5HackSPScroll=(V>>3)&0x1F;break;
361 case 0x5202:MMC5HackSPPage=V&0x3F;break;
362 case 0x5203:X6502_IRQEnd(FCEU_IQEXT);IRQCount=V;break;
363 case 0x5204:X6502_IRQEnd(FCEU_IQEXT);IRQa=V&0x80;break;
364 case 0x5205:mul1=V;break;
365 case 0x5206:mul2=V;break;
369 DECLFR(MMC5_ReadROMRAM)
371 if(MMC5MemIn[(A-0x6000)>>13])
372 return Page[A>>11][A];
377 DECLFW(MMC5_WriteROMRAM)
380 if(MMC5ROMWrProtect[(A-0x8000)>>13])
382 if(MMC5MemIn[(A-0x6000)>>13])
383 if(((mapbyte4[0]&3)|((mapbyte4[1]&3)<<2)) == 6)
387 static DECLFW(MMC5_ExRAMWr)
389 if(MMC5HackCHRMode!=3)
390 (MapperExRAM+0x6000)[A&0x3ff]=V;
393 static DECLFR(MMC5_ExRAMRd)
395 /* Not sure if this is correct, so I'll comment it out for now. */
396 //if(MMC5HackCHRMode>=2)
397 return (MapperExRAM+0x6000)[A&0x3ff];
402 static DECLFR(MMC5_read)
406 //default:printf("$%04x\n",A);break;
407 case 0x5204:X6502_IRQEnd(FCEU_IQEXT);
414 case 0x5205:return (mul1*mul2);
415 case 0x5206:return ((mul1*mul2)>>8);
429 switch((mapbyte4[3]>>(x<<1))&3)
431 case 0:PPUNTARAM|=1<<x;vnapage[x]=NTARAM;break;
432 case 1:PPUNTARAM|=1<<x;vnapage[x]=NTARAM+0x400;break;
433 case 2:PPUNTARAM|=1<<x;vnapage[x]=MapperExRAM+0x6000;break;
434 case 3:PPUNTARAM&=~(1<<x);vnapage[x]=MMC5fill;break;
437 MMC5WRAM(0x6000,mapbyte4[6]&7);
450 t=mapbyte4[4]|(mapbyte4[4]<<8)|(mapbyte4[4]<<16)|(mapbyte4[4]<<24);
451 FCEU_dwmemset(MMC5fill,t,0x3c0);
456 moop=mapbyte4[5]|(mapbyte4[5]<<2)|(mapbyte4[5]<<4)|(mapbyte4[5]<<6);
457 t=moop|(moop<<8)|(moop<<16)|(moop<<24);
458 FCEU_dwmemset(MMC5fill+0x3c0,t,0x40);
460 X6502_IRQEnd(FCEU_IQEXT);
461 MMC5HackCHRMode=mapbyte4[2]&3;
470 if(MMC5LineCounter<245)
473 if(MMC5LineCounter==IRQCount) MMC5IRQR|=0x80;
474 if((MMC5LineCounter==IRQCount && IRQa&0x80))
475 X6502_IRQBegin(FCEU_IQEXT);
477 if(MMC5LineCounter>=239)
490 if(MMC5LineCounter<240)
493 if(MMC5LineCounter==IRQCount)
497 X6502_IRQBegin(FCEU_IQEXT);
500 // printf("%d:%d\n",MMC5LineCounter,scanline);
501 if(MMC5LineCounter==240)
505 void Mapper5_StateRestore(int version)
511 FCEU_memmove(tmp,MapperExRAM,8192);
512 FCEU_memmove(MapperExRAM,MapperExRAM+8192,16384+8192);
513 FCEU_memmove(MapperExRAM+16384+8192,tmp,8192);
518 #define MMC5PSG (MapperExRAM+0x640B+8)
520 static int C5BC[3]={0,0,0};
522 static void Do5PCM(void)
528 end=(timestamp<<16)/soundtsinc;
529 if(end<=start) return;
532 if(!(MMC5PSG[0x10]&0x40) && MMC5PSG[0x11])
533 for(V=start;V<end;V++)
534 Wave[V>>4]+=MMC5PSG[0x11]<<2;
539 GameExpSound.Fill=MMC5Sound;
545 case 0x11:Do5PCM();break;
547 case 0x0:Do5SQ(0);break;
548 case 0x2:Do5SQ(0);break;
549 case 0x3:Do5SQ(0);break;
550 case 0x4:Do5SQ(1);break;
551 case 0x6:Do5SQ(1);break;
552 case 0x7:Do5SQ(1);break;
555 int t=V^MMC5PSG[0x15];
566 static int32 vcount[2];
574 end=(timestamp<<16)/soundtsinc;
575 if(end<=start) return;
580 if(MMC5PSG[0x15]&(P+1))
583 unsigned char amplitude;
586 freq=(((MMC5PSG[(P<<2)+0x2]|((MMC5PSG[(P<<2)+0x3]&7)<<8))));
588 if(freq<8) goto mmc5enda;
590 inc=(long double)((unsigned long)((FSettings.SndRate OVERSAMPLE)<<12))/((long double)PSG_base/freq);
592 switch(MMC5PSG[P<<2]&0xC0)
595 case 0x00:dcycs=inc>>3;break;
596 case 0x40:dcycs=inc>>2;break;
597 case 0x80:dcycs=inc>>1;break;
598 case 0xC0:dcycs=(inc+inc+inc)>>2;break;
601 amplitude=(MMC5PSG[P<<2]&15)<<4;
604 for(V=start;V<end;V++)
607 Wave[V>>4]+=amplitude;
609 if(vcoo>=inc) vcoo-=inc;
613 mmc5enda:; // semi-colon must be here for MSVC
616 void MMC5Sound(int Count)
626 static void MMC5SoundC(void)
628 if(FSettings.SndRate)
631 SetWriteHandler(0x5000,0x5015,0);
634 void Mapper5_ESI(void)
636 GameExpSound.RChange=MMC5SoundC;
638 if(FSettings.SndRate)
640 SetWriteHandler(0x5000,0x5015,Mapper5_SW);
641 SetWriteHandler(0x5205,0x5206,Mapper5_write);
642 SetReadHandler(0x5205,0x5206,MMC5_read);
644 if(FCEUGameInfo.type==GIT_NSF)
646 SetWriteHandler(0x5c00,0x5fef,MMC5_ExRAMWr);
647 SetReadHandler(0x5c00,0x5fef,MMC5_ExRAMRd);
651 GameHBIRQHook=MMC5_hb;
654 static void GenMMC5Reset(void)
656 mapbyte1[4]=mapbyte1[5]=mapbyte1[6]=mapbyte1[7]=~0;
657 mapbyte1[0]=mapbyte1[1]=3;
660 mapbyte4[3]=mapbyte4[4]=mapbyte4[5]=0xFF;
664 SetWriteHandler(0x4020,0x5bff,Mapper5_write);
665 SetReadHandler(0x4020,0x5bff,MMC5_read);
667 SetWriteHandler(0x5c00,0x5fff,MMC5_ExRAMWr);
668 SetReadHandler(0x5c00,0x5fff,MMC5_ExRAMRd);
670 SetWriteHandler(0x6000,0xFFFF,MMC5_WriteROMRAM);
671 SetReadHandler(0x6000,0xFFFF,MMC5_ReadROMRAM);
675 GameHBIRQHook=MMC5_hb;
676 FCEU_CheatAddRAM(8,0x6000,WRAM);
677 FCEU_CheatAddRAM(1,0x5c00,MapperExRAM+0x6000);
680 void Mapper5_init(void)
682 AddExState(&MMC5HackSPMode, 1, 0, "SPLM");
683 AddExState(&MMC5HackSPScroll, 1, 0, "SPLS");
684 AddExState(&MMC5HackSPPage, 1, 0, "SPLP");
685 SetupCartPRGMapping(0x10,WRAM,32768,1);
687 BuildWRAMSizeTable();
688 GameStateRestore=Mapper5_StateRestore;
692 static void GenMMC5_Close(void)
694 UNIFOpenWRAM(UOW_WR,0,1);
696 UNIFWriteWRAM(WRAM,8192);
698 UNIFWriteWRAM(WRAM,32768);
702 static void GenMMC5_Init(int wsize, int battery)
704 SetupCartPRGMapping(0x10,WRAM,32768,1);
705 AddExState(WRAM, 8192, 0, "WRAM");
706 AddExState(MapperExRAM, 32768, 0, "MEXR");
707 AddExState(&IRQCount, 4, 1, "IRQC");
708 AddExState(&IRQa, 1, 0, "IRQA");
709 AddExState(mapbyte1, 32, 0, "MPBY");
710 AddExState(&MMC5HackSPMode, 1, 0, "SPLM");
711 AddExState(&MMC5HackSPScroll, 1, 0, "SPLS");
712 AddExState(&MMC5HackSPPage, 1, 0, "SPLP");
714 MMC5WRAMsize=wsize/8;
715 BuildWRAMSizeTable();
716 GameStateRestore=Mapper5_StateRestore;
717 BoardPower=GenMMC5Reset;
721 UNIFOpenWRAM(UOW_RD,0,1);
723 UNIFReadWRAM(WRAM,8192);
725 UNIFReadWRAM(WRAM,32768);
727 BoardClose=GenMMC5_Close;
731 MMC5HackVROMMask=CHRmask4[0];
732 MMC5HackExNTARAMPtr=MapperExRAM+0x6000;
734 MMC5HackVROMPTR=CHRptr[0];
736 MMC5HackSPMode=MMC5HackSPScroll=MMC5HackSPPage=0;
740 // ETROM seems to have 16KB of WRAM, ELROM seems to have 8KB
741 // EWROM seems to have 32KB of WRAM
743 // ETROM and EWROM are battery-backed, ELROM isn't.
745 void ETROM_Init(void)
750 void ELROM_Init(void)
755 void EWROM_Init(void)
760 void EKROM_Init(void)