merge mapper code from FCEUX
[fceu.git] / boards / bandai.c
diff --git a/boards/bandai.c b/boards/bandai.c
new file mode 100644 (file)
index 0000000..8468aad
--- /dev/null
@@ -0,0 +1,176 @@
+/* FCE Ultra - NES/Famicom Emulator\r
+ *\r
+ * Copyright notice for this file:\r
+ *  Copyright (C) 2007 CaH4e3\r
+ *  Copyright (C) 2011 FCEUX team\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program; if not, write to the Free Software\r
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA\r
+ *\r
+ * Bandai mappers\r
+ *\r
+ */\r
+\r
+//Famicom Jump 2 should get transformed to m153\r
+//All other games are not supporting EEPROM saving right now. \r
+//We may need to distinguish between 16 and 159 in order to know the EEPROM configuration.\r
+//Until then, we just return 0x00 from the EEPROM read\r
+\r
+#include "mapinc.h"\r
+\r
+static uint8 reg[16], is153;\r
+static uint8 IRQa;\r
+static int16 IRQCount, IRQLatch;\r
+\r
+static uint8 *WRAM=NULL;\r
+static uint32 WRAMSIZE;\r
+\r
+static SFORMAT StateRegs[]=\r
+{\r
+  {reg, 16, "REGS"},\r
+  {&IRQa, 1, "IRQA"},\r
+  {&IRQCount, 2, "IRQC"},\r
+  {&IRQLatch, 2, "IRQL"}, // need for Famicom Jump II - Saikyou no 7 Nin (J) [!]\r
+  {0}\r
+};\r
+\r
+static void BandaiIRQHook(int a)\r
+{\r
+  if(IRQa)\r
+  {\r
+    IRQCount -= a;\r
+    if(IRQCount<0)\r
+    {\r
+      X6502_IRQBegin(FCEU_IQEXT);\r
+      IRQa = 0;\r
+      IRQCount = -1;\r
+    }\r
+  }\r
+}\r
+\r
+static void BandaiSync(void)\r
+{\r
+  if(is153)\r
+  {\r
+    int base=(reg[0]&1)<<4;\r
+    setchr8(0);\r
+    setprg16(0x8000,(reg[8]&0x0F)|base);\r
+    setprg16(0xC000,0x0F|base);\r
+  }\r
+  else\r
+  {\r
+    int i;\r
+    for(i=0; i<8; i++) setchr1(i<<10,reg[i]);\r
+    setprg16(0x8000,reg[8]);\r
+    setprg16(0xC000,~0);\r
+  }\r
+  switch(reg[9]&3)\r
+  {\r
+    case 0: setmirror(MI_V); break;\r
+    case 1: setmirror(MI_H); break;\r
+    case 2: setmirror(MI_0); break;\r
+    case 3: setmirror(MI_1); break;\r
+  }\r
+}\r
+\r
+static DECLFW(BandaiWrite)\r
+{\r
+  A&=0x0F;\r
+  if(A<0x0A)\r
+  {\r
+    reg[A&0x0F]=V;\r
+    BandaiSync();\r
+  }\r
+  else\r
+    switch(A)\r
+    {\r
+      case 0x0A: X6502_IRQEnd(FCEU_IQEXT); IRQa=V&1; IRQCount=IRQLatch; break;\r
+      case 0x0B: IRQLatch&=0xFF00; IRQLatch|=V;  break;\r
+      case 0x0C: IRQLatch&=0xFF; IRQLatch|=V<<8; break;\r
+      case 0x0D: break;// Serial EEPROM control port\r
+    }\r
+}\r
+\r
+DECLFR(BandaiRead)
+{
+       return 0;
+}\r
+\r
+static void BandaiPower(void)\r
+{\r
+  BandaiSync();\r
+  SetReadHandler(0x8000,0xFFFF,CartBR);\r
+  SetWriteHandler(0x6000,0xFFFF,BandaiWrite);\r
+  SetReadHandler(0x6000,0x7FFF,BandaiRead);\r
+}\r
+\r
+static void M153Power(void)\r
+{\r
+  BandaiSync();\r
+  setprg8r(0x10,0x6000,0);\r
+  SetReadHandler(0x6000,0x7FFF,CartBR);\r
+  SetWriteHandler(0x6000,0x7FFF,CartBW);\r
+  SetReadHandler(0x8000,0xFFFF,CartBR);\r
+  SetWriteHandler(0x8000,0xFFFF,BandaiWrite);\r
+}\r
+\r
+\r
+static void M153Close(void)\r
+{\r
+  if(WRAM)\r
+    FCEU_gfree(WRAM);\r
+  WRAM=NULL;\r
+}\r
+\r
+static void StateRestore(int version)\r
+{\r
+  BandaiSync();\r
+}\r
+\r
+void Mapper16_Init(CartInfo *info)\r
+{\r
+  is153=0;\r
+  info->Power=BandaiPower;\r
+  MapIRQHook=BandaiIRQHook;\r
+  GameStateRestore=StateRestore;\r
+  AddExState(&StateRegs, ~0, 0, 0);\r
+}\r
+\r
+void Mapper159_Init(CartInfo *info)\r
+{\r
+  Mapper16_Init(info);\r
+}\r
+\r
+\r
+void Mapper153_Init(CartInfo *info)\r
+{\r
+  is153=1;\r
+  info->Power=M153Power;\r
+  info->Close=M153Close;\r
+  MapIRQHook=BandaiIRQHook;\r
+\r
+  WRAMSIZE=8192;\r
+  WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);\r
+  SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);\r
+  AddExState(WRAM, WRAMSIZE, 0, "WRAM");\r
+\r
+  if(info->battery)\r
+  {\r
+    info->SaveGame[0]=WRAM;\r
+    info->SaveGameLen[0]=WRAMSIZE;\r
+  }\r
+\r
+  GameStateRestore=StateRestore;\r
+  AddExState(&StateRegs, ~0, 0, 0);\r
+}\r