merge mapper code from FCEUX
[fceu.git] / boards / 15.c
diff --git a/boards/15.c b/boards/15.c
new file mode 100644 (file)
index 0000000..6405afa
--- /dev/null
@@ -0,0 +1,120 @@
+/* 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
+\r
+#include "mapinc.h"\r
+\r
+static uint16 latchea;\r
+static uint8 latched;\r
+static uint8 *WRAM=NULL;\r
+static uint32 WRAMSIZE;\r
+static SFORMAT StateRegs[]=\r
+{\r
+  {&latchea, 2, "AREG"},\r
+  {&latched, 1, "DREG"},\r
+  {0}\r
+};\r
+\r
+static void Sync(void)\r
+{\r
+  int i;\r
+  setmirror(((latched>>6)&1)^1);\r
+  switch(latchea)\r
+  {\r
+    case 0x8000:\r
+      for(i=0;i<4;i++)\r
+        setprg8(0x8000+(i<<13),(((latched&0x7F)<<1)+i)^(latched>>7));\r
+      break;\r
+    case 0x8002:\r
+      for(i=0;i<4;i++)\r
+        setprg8(0x8000+(i<<13),((latched&0x7F)<<1)+(latched>>7));\r
+      break;\r
+    case 0x8001:\r
+    case 0x8003:\r
+      for(i=0;i<4;i++)\r
+      {\r
+        unsigned int b;\r
+        b=latched&0x7F;\r
+        if(i>=2 && !(latchea&0x2))\r
+          i=0x7F;\r
+        setprg8(0x8000+(i<<13),(i&1)+((b<<1)^(latched>>7)));\r
+      }\r
+      break;\r
+  }\r
+}\r
+\r
+static DECLFW(M15Write)\r
+{\r
+  latchea=A;\r
+  latched=V;\r
+       printf("%04X = %02X\n",A,V);\r
+  Sync();\r
+}\r
+\r
+static void StateRestore(int version)\r
+{\r
+  Sync();\r
+}\r
+\r
+static void M15Power(void)\r
+{\r
+  latchea=0x8000;\r
+  latched=0;\r
+  setchr8(0);\r
+  setprg8r(0x10,0x6000,0);\r
+  SetReadHandler(0x6000,0x7FFF,CartBR);\r
+  SetWriteHandler(0x6000,0x7FFF,CartBW);\r
+  SetWriteHandler(0x8000,0xFFFF,M15Write);\r
+  SetReadHandler(0x8000,0xFFFF,CartBR);\r
+  Sync();\r
+}\r
+\r
+static void M15Reset(void)\r
+{\r
+  latchea=0x8000;\r
+  latched=0;\r
+  Sync();\r
+}\r
+\r
+static void M15Close(void)\r
+{\r
+  if(WRAM)\r
+    FCEU_gfree(WRAM);\r
+  WRAM=NULL;\r
+}\r
+\r
+void Mapper15_Init(CartInfo *info)\r
+{\r
+  info->Power=M15Power;\r
+  info->Reset=M15Reset;\r
+  info->Close=M15Close;\r
+  GameStateRestore=StateRestore;\r
+  WRAMSIZE=8192;\r
+  WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);\r
+  SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);\r
+  if(info->battery)\r
+  {\r
+    info->SaveGame[0]=WRAM;\r
+    info->SaveGameLen[0]=WRAMSIZE;\r
+  }\r
+  AddExState(WRAM, WRAMSIZE, 0, "WRAM");\r
+  AddExState(&StateRegs, ~0, 0, 0);\r
+}\r
+\r