merge mapper code from FCEUX
[fceu.git] / boards / 106.c
diff --git a/boards/106.c b/boards/106.c
new file mode 100644 (file)
index 0000000..638dda9
--- /dev/null
@@ -0,0 +1,119 @@
+/* FCE Ultra - NES/Famicom Emulator\r
+ *\r
+ * Copyright notice for this file:\r
+ *  Copyright (C) 2007 CaH4e3\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
+\r
+#include "mapinc.h"\r
+\r
+static uint8 reg[16], IRQa;\r
+static uint32 IRQCount;\r
+static uint8 *WRAM=NULL;\r
+static uint32 WRAMSIZE;\r
+\r
+static SFORMAT StateRegs[]=\r
+{\r
+  {&IRQa, 1, "IRQA"},\r
+  {&IRQCount, 4, "IRQCOUNT"},\r
+  {reg, 16, "REGS"},\r
+  {0}\r
+};\r
+\r
+static void Sync(void)\r
+{\r
+  setchr1(0x0000,reg[0]&0xfe);\r
+  setchr1(0x0400,reg[1]|1);\r
+  setchr1(0x0800,reg[2]&0xfe);\r
+  setchr1(0x0c00,reg[3]|1);\r
+  setchr1(0x1000,reg[4]);\r
+  setchr1(0x1400,reg[5]);\r
+  setchr1(0x1800,reg[6]);\r
+  setchr1(0x1c00,reg[7]);\r
+  setprg8r(0x10,0x6000,0);\r
+  setprg8(0x8000,(reg[0x8]&0xf)|0x10);\r
+  setprg8(0xA000,(reg[0x9]&0x1f));\r
+  setprg8(0xC000,(reg[0xa]&0x1f));\r
+  setprg8(0xE000,(reg[0xb]&0xf)|0x10);\r
+  setmirror((reg[0xc]&1)^1);\r
+}\r
+\r
+static DECLFW(M106Write)\r
+{\r
+  A&=0xF;\r
+  switch(A)\r
+  {\r
+    case 0xD: IRQa=0; IRQCount=0; X6502_IRQEnd(FCEU_IQEXT); break;\r
+    case 0xE: IRQCount=(IRQCount&0xFF00)|V; break;\r
+    case 0xF: IRQCount=(IRQCount&0x00FF)|(V<<8); IRQa=1; break;\r
+    default: reg[A]=V; Sync(); break;\r
+  }\r
+}\r
+\r
+static void M106Power(void)\r
+{\r
+  reg[8]=reg[9]=reg[0xa]=reg[0xb]=-1;\r
+  Sync();\r
+  SetReadHandler(0x6000,0x7FFF,CartBR);\r
+  SetReadHandler(0x8000,0xFFFF,CartBR);\r
+  SetWriteHandler(0x6000,0x7FFF,CartBW);\r
+  SetWriteHandler(0x8000,0xFFFF,M106Write);\r
+}\r
+\r
+static void M106Reset(void)\r
+{\r
+}\r
+\r
+static void M106Close(void)\r
+{\r
+  if(WRAM)\r
+    FCEU_gfree(WRAM);\r
+  WRAM=NULL;\r
+}\r
+\r
+void M106CpuHook(int a)\r
+{\r
+  if(IRQa)\r
+  {\r
+    IRQCount+=a;\r
+    if(IRQCount>0x10000)\r
+    {\r
+      X6502_IRQBegin(FCEU_IQEXT);\r
+      IRQa=0;\r
+    }\r
+  }\r
+}\r
+\r
+static void StateRestore(int version)\r
+{\r
+  Sync();\r
+}\r
+\r
+void Mapper106_Init(CartInfo *info)\r
+{\r
+  info->Reset=M106Reset;\r
+  info->Power=M106Power;\r
+  info->Close=M106Close;\r
+  MapIRQHook=M106CpuHook;\r
+  GameStateRestore=StateRestore;\r
+\r
+  WRAMSIZE=8192;\r
+  WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);\r
+  SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);\r
+  AddExState(WRAM, WRAMSIZE, 0, "WRAM");\r
+\r
+  AddExState(&StateRegs, ~0, 0, 0);\r
+}\r