merge mapper code from FCEUX
[fceu.git] / boards / bandai.c
1 /* FCE Ultra - NES/Famicom Emulator\r
2  *\r
3  * Copyright notice for this file:\r
4  *  Copyright (C) 2007 CaH4e3\r
5  *  Copyright (C) 2011 FCEUX team\r
6  *\r
7  * This program is free software; you can redistribute it and/or modify\r
8  * it under the terms of the GNU General Public License as published by\r
9  * the Free Software Foundation; either version 2 of the License, or\r
10  * (at your option) any later version.\r
11  *\r
12  * This program is distributed in the hope that it will be useful,\r
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15  * GNU General Public License for more details.\r
16  *\r
17  * You should have received a copy of the GNU General Public License\r
18  * along with this program; if not, write to the Free Software\r
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA\r
20  *\r
21  * Bandai mappers\r
22  *\r
23  */\r
24 \r
25 //Famicom Jump 2 should get transformed to m153\r
26 //All other games are not supporting EEPROM saving right now. \r
27 //We may need to distinguish between 16 and 159 in order to know the EEPROM configuration.\r
28 //Until then, we just return 0x00 from the EEPROM read\r
29 \r
30 #include "mapinc.h"\r
31 \r
32 static uint8 reg[16], is153;\r
33 static uint8 IRQa;\r
34 static int16 IRQCount, IRQLatch;\r
35 \r
36 static uint8 *WRAM=NULL;\r
37 static uint32 WRAMSIZE;\r
38 \r
39 static SFORMAT StateRegs[]=\r
40 {\r
41   {reg, 16, "REGS"},\r
42   {&IRQa, 1, "IRQA"},\r
43   {&IRQCount, 2, "IRQC"},\r
44   {&IRQLatch, 2, "IRQL"}, // need for Famicom Jump II - Saikyou no 7 Nin (J) [!]\r
45   {0}\r
46 };\r
47 \r
48 static void BandaiIRQHook(int a)\r
49 {\r
50   if(IRQa)\r
51   {\r
52     IRQCount -= a;\r
53     if(IRQCount<0)\r
54     {\r
55       X6502_IRQBegin(FCEU_IQEXT);\r
56       IRQa = 0;\r
57       IRQCount = -1;\r
58     }\r
59   }\r
60 }\r
61 \r
62 static void BandaiSync(void)\r
63 {\r
64   if(is153)\r
65   {\r
66     int base=(reg[0]&1)<<4;\r
67     setchr8(0);\r
68     setprg16(0x8000,(reg[8]&0x0F)|base);\r
69     setprg16(0xC000,0x0F|base);\r
70   }\r
71   else\r
72   {\r
73     int i;\r
74     for(i=0; i<8; i++) setchr1(i<<10,reg[i]);\r
75     setprg16(0x8000,reg[8]);\r
76     setprg16(0xC000,~0);\r
77   }\r
78   switch(reg[9]&3)\r
79   {\r
80     case 0: setmirror(MI_V); break;\r
81     case 1: setmirror(MI_H); break;\r
82     case 2: setmirror(MI_0); break;\r
83     case 3: setmirror(MI_1); break;\r
84   }\r
85 }\r
86 \r
87 static DECLFW(BandaiWrite)\r
88 {\r
89   A&=0x0F;\r
90   if(A<0x0A)\r
91   {\r
92     reg[A&0x0F]=V;\r
93     BandaiSync();\r
94   }\r
95   else\r
96     switch(A)\r
97     {\r
98       case 0x0A: X6502_IRQEnd(FCEU_IQEXT); IRQa=V&1; IRQCount=IRQLatch; break;\r
99       case 0x0B: IRQLatch&=0xFF00; IRQLatch|=V;  break;\r
100       case 0x0C: IRQLatch&=0xFF; IRQLatch|=V<<8; break;\r
101       case 0x0D: break;// Serial EEPROM control port\r
102     }\r
103 }\r
104 \r
105 DECLFR(BandaiRead)
106 {
107         return 0;
108 }\r
109 \r
110 static void BandaiPower(void)\r
111 {\r
112   BandaiSync();\r
113   SetReadHandler(0x8000,0xFFFF,CartBR);\r
114   SetWriteHandler(0x6000,0xFFFF,BandaiWrite);\r
115   SetReadHandler(0x6000,0x7FFF,BandaiRead);\r
116 }\r
117 \r
118 static void M153Power(void)\r
119 {\r
120   BandaiSync();\r
121   setprg8r(0x10,0x6000,0);\r
122   SetReadHandler(0x6000,0x7FFF,CartBR);\r
123   SetWriteHandler(0x6000,0x7FFF,CartBW);\r
124   SetReadHandler(0x8000,0xFFFF,CartBR);\r
125   SetWriteHandler(0x8000,0xFFFF,BandaiWrite);\r
126 }\r
127 \r
128 \r
129 static void M153Close(void)\r
130 {\r
131   if(WRAM)\r
132     FCEU_gfree(WRAM);\r
133   WRAM=NULL;\r
134 }\r
135 \r
136 static void StateRestore(int version)\r
137 {\r
138   BandaiSync();\r
139 }\r
140 \r
141 void Mapper16_Init(CartInfo *info)\r
142 {\r
143   is153=0;\r
144   info->Power=BandaiPower;\r
145   MapIRQHook=BandaiIRQHook;\r
146   GameStateRestore=StateRestore;\r
147   AddExState(&StateRegs, ~0, 0, 0);\r
148 }\r
149 \r
150 void Mapper159_Init(CartInfo *info)\r
151 {\r
152   Mapper16_Init(info);\r
153 }\r
154 \r
155 \r
156 void Mapper153_Init(CartInfo *info)\r
157 {\r
158   is153=1;\r
159   info->Power=M153Power;\r
160   info->Close=M153Close;\r
161   MapIRQHook=BandaiIRQHook;\r
162 \r
163   WRAMSIZE=8192;\r
164   WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);\r
165   SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);\r
166   AddExState(WRAM, WRAMSIZE, 0, "WRAM");\r
167 \r
168   if(info->battery)\r
169   {\r
170     info->SaveGame[0]=WRAM;\r
171     info->SaveGameLen[0]=WRAMSIZE;\r
172   }\r
173 \r
174   GameStateRestore=StateRestore;\r
175   AddExState(&StateRegs, ~0, 0, 0);\r
176 }\r