merge mapper code from FCEUX
[fceu.git] / boards / addrlatch.c
diff --git a/boards/addrlatch.c b/boards/addrlatch.c
new file mode 100644 (file)
index 0000000..4b70d6e
--- /dev/null
@@ -0,0 +1,172 @@
+/* FCE Ultra - NES/Famicom Emulator\r
+ *\r
+ * Copyright notice for this file:\r
+ *  Copyright (C) 2006 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 uint16 latche, latcheinit;\r
+static uint16 addrreg0, addrreg1;\r
+static void(*WSync)(void);\r
+static readfunc defread;\r
+\r
+static DECLFW(LatchWrite)\r
+{\r
+  FCEU_printf("%04x:%02x\n",A,V);\r
+  latche=A;\r
+  WSync();\r
+}\r
+\r
+static void LatchReset(void)\r
+{\r
+  latche=latcheinit;\r
+  WSync();\r
+}\r
+\r
+static void LatchPower(void)\r
+{\r
+  latche=latcheinit;\r
+  WSync();\r
+  SetReadHandler(0x8000,0xFFFF,defread);\r
+  SetWriteHandler(addrreg0,addrreg1,LatchWrite);\r
+}\r
+\r
+static void StateRestore(int version)\r
+{\r
+  WSync();\r
+}\r
+\r
+static void Latch_Init(CartInfo *info, void (*proc)(void), readfunc func, uint16 init, uint16 adr0, uint16 adr1)\r
+{\r
+  latcheinit=init;\r
+  addrreg0=adr0;\r
+  addrreg1=adr1;\r
+  WSync=proc;\r
+  if(func)\r
+    defread=func;\r
+  else\r
+    defread=CartBR;\r
+  info->Power=LatchPower;\r
+  info->Reset=LatchReset;\r
+  GameStateRestore=StateRestore;\r
+  AddExState(&latche, 2, 0, "LATC");\r
+}\r
+\r
+//------------------ UNLCC21 ---------------------------\r
+\r
+static void UNLCC21Sync(void)\r
+{\r
+  setprg32(0x8000,0);\r
+  setchr8(latche&1);\r
+  setmirror(MI_0+((latche&2)>>1));\r
+}\r
+\r
+void UNLCC21_Init(CartInfo *info)\r
+{ \r
+  Latch_Init(info, UNLCC21Sync, 0, 0, 0x8000, 0xFFFF);\r
+}\r
+\r
+//------------------ BMCD1038 ---------------------------\r
+\r
+static uint8 dipswitch;\r
+static void BMCD1038Sync(void)\r
+{\r
+  if(latche&0x80)\r
+  {\r
+    setprg16(0x8000,(latche&0x70)>>4);\r
+    setprg16(0xC000,(latche&0x70)>>4);\r
+  }\r
+  else\r
+    setprg32(0x8000,(latche&0x60)>>5);\r
+  setchr8(latche&7);\r
+  setmirror(((latche&8)>>3)^1);\r
+}\r
+\r
+static DECLFR(BMCD1038Read)\r
+{\r
+  if(latche&0x100)\r
+    return dipswitch;\r
+  else\r
+    return CartBR(A);\r
+}\r
+\r
+static void BMCD1038Reset(void)\r
+{\r
+  dipswitch++;\r
+  dipswitch&=3; \r
+}\r
+\r
+void BMCD1038_Init(CartInfo *info)\r
+{ \r
+  Latch_Init(info, BMCD1038Sync, BMCD1038Read, 0, 0x8000, 0xFFFF);\r
+  info->Reset=BMCD1038Reset;\r
+  AddExState(&dipswitch, 1, 0, "DIPSW");\r
+}\r
+\r
+\r
+//------------------ Map 058 ---------------------------\r
+\r
+static void BMCGK192Sync(void)\r
+{\r
+  if(latche&0x40)\r
+  {\r
+    setprg16(0x8000,latche&7);\r
+    setprg16(0xC000,latche&7);\r
+  }\r
+  else\r
+    setprg32(0x8000,(latche>>1)&3);\r
+  setchr8((latche>>3)&7);\r
+  setmirror(((latche&0x80)>>7)^1);\r
+}\r
+\r
+void BMCGK192_Init(CartInfo *info)\r
+{\r
+  Latch_Init(info, BMCGK192Sync, 0, 0, 0x8000, 0xFFFF);\r
+}\r
+\r
+//------------------ Map 200 ---------------------------\r
+\r
+static void M200Sync(void)\r
+{\r
+//  FCEU_printf("A\n");\r
+  setprg16(0x8000,latche&7);\r
+  setprg16(0xC000,latche&7);\r
+  setchr8(latche&7);\r
+  setmirror((latche&8)>>3);\r
+}\r
+\r
+void Mapper200_Init(CartInfo *info)\r
+{ \r
+  Latch_Init(info, M200Sync, 0, 0xff, 0x8000, 0xFFFF);\r
+}\r
+\r
+//------------------ 190in1 ---------------------------\r
+\r
+static void BMC190in1Sync(void)\r
+{\r
+  setprg16(0x8000,(latche>>2)&0x07);\r
+  setprg16(0xC000,(latche>>2)&0x07);\r
+  setchr8((latche>>2)&0x07);\r
+  setmirror((latche&1)^1);\r
+}\r
+\r
+void BMC190in1_Init(CartInfo *info)\r
+{ \r
+  Latch_Init(info, BMC190in1Sync, 0, 0, 0x8000, 0xFFFF);\r
+}\r
+\r