merge mapper code from FCEUX
[fceu.git] / boards / sc-127.c
diff --git a/boards/sc-127.c b/boards/sc-127.c
new file mode 100644 (file)
index 0000000..a2c29ff
--- /dev/null
@@ -0,0 +1,125 @@
+/* FCE Ultra - NES/Famicom Emulator\r
+ *\r
+ * Copyright notice for this file:\r
+ *  Copyright (C) 2009 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
+ * Wario Land II (Kirby hack)\r
+ */\r
+\r
+#include "mapinc.h"\r
+\r
+static uint8 reg[8], chr[8];\r
+static uint8 *WRAM=NULL;\r
+static uint32 WRAMSIZE;\r
+static uint16 IRQCount, IRQa;\r
+\r
+static SFORMAT StateRegs[]=\r
+{\r
+  {reg, 8, "REGS"},\r
+  {chr, 8, "CHRS"},\r
+  {&IRQCount, 16, "IRQc"},\r
+  {&IRQa, 16, "IRQa"},\r
+  {0}\r
+};\r
+\r
+static void Sync(void)\r
+{\r
+  int i;\r
+  setprg8(0x8000,reg[0]);\r
+  setprg8(0xA000,reg[1]);\r
+  setprg8(0xC000,reg[2]);\r
+  for(i=0; i<8; i++)\r
+    setchr1(i << 10,chr[i]);\r
+  setmirror(reg[3]^1);\r
+}\r
+\r
+static DECLFW(UNLSC127Write)\r
+{\r
+  switch(A)\r
+  {\r
+    case 0x8000: reg[0] = V; break; \r
+    case 0x8001: reg[1] = V; break;\r
+    case 0x8002: reg[2] = V; break;\r
+    case 0x9000: chr[0] = V; break;\r
+    case 0x9001: chr[1] = V; break;\r
+    case 0x9002: chr[2] = V; break;\r
+    case 0x9003: chr[3] = V; break;\r
+    case 0x9004: chr[4] = V; break;\r
+    case 0x9005: chr[5] = V; break;\r
+    case 0x9006: chr[6] = V; break;\r
+    case 0x9007: chr[7] = V; break;\r
+    case 0xC002: IRQa=0; X6502_IRQEnd(FCEU_IQEXT); break;\r
+    case 0xC005: IRQCount=V; break;\r
+    case 0xC003: IRQa=1; break;\r
+    case 0xD001: reg[3] = V; break;\r
+  }\r
+  Sync();\r
+}\r
+\r
+static void UNLSC127Power(void)\r
+{\r
+  Sync();\r
+  setprg8r(0x10,0x6000,0);\r
+  setprg8(0xE000,~0);\r
+  SetReadHandler(0x6000,0x7fff,CartBR);\r
+  SetWriteHandler(0x6000,0x7fff,CartBW);\r
+  SetReadHandler(0x8000,0xFFFF,CartBR);\r
+  SetWriteHandler(0x8000,0xFFFF,UNLSC127Write);\r
+}\r
+\r
+static void UNLSC127IRQ(void)\r
+{\r
+ if(IRQa)\r
+ {\r
+   IRQCount--;\r
+   if(IRQCount==0)\r
+   {\r
+    X6502_IRQBegin(FCEU_IQEXT);\r
+    IRQa=0;\r
+   }\r
+ }\r
+}\r
+\r
+static void UNLSC127Reset(void)\r
+{\r
+}\r
+\r
+static void UNLSC127Close(void)\r
+{\r
+  if(WRAM)\r
+    FCEU_gfree(WRAM);\r
+  WRAM=NULL;\r
+}\r
+\r
+static void StateRestore(int version)\r
+{\r
+  Sync();\r
+}\r
+\r
+void UNLSC127_Init(CartInfo *info)\r
+{\r
+  info->Reset=UNLSC127Reset;\r
+  info->Power=UNLSC127Power;\r
+  info->Close=UNLSC127Close;\r
+  GameHBIRQHook=UNLSC127IRQ;\r
+  GameStateRestore=StateRestore;\r
+  WRAMSIZE=8192;\r
+  WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);\r
+  SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);\r
+  AddExState(WRAM, WRAMSIZE, 0, "WRAM");\r
+  AddExState(&StateRegs, ~0, 0, 0);\r
+}\r