merge mappers from FCEU-mm
[fceu.git] / boards / KS7017.c
diff --git a/boards/KS7017.c b/boards/KS7017.c
new file mode 100644 (file)
index 0000000..9402f99
--- /dev/null
@@ -0,0 +1,134 @@
+/* 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
+ *\r
+ * FDS Conversion\r
+ *\r
+ */\r
+\r
+#include "mapinc.h"\r
+\r
+static uint8 reg, mirr;\r
+static int32 IRQa, IRQCount, IRQLatch;\r
+static uint8 *WRAM=NULL;\r
+static uint32 WRAMSIZE;\r
+\r
+static SFORMAT StateRegs[]=\r
+{\r
+  {&mirr, 1, "MIRR"},\r
+  {&reg, 1, "REGS"},\r
+  {&IRQa, 4, "IRQA"},\r
+  {&IRQCount, 4, "IRQC"},\r
+  {&IRQLatch, 4, "IRQL"},\r
+  {0}\r
+};\r
+\r
+static void Sync(void)\r
+{\r
+  setprg16(0x8000,reg);\r
+  setprg16(0xC000,2);\r
+  setmirror(mirr);\r
+}\r
+\r
+static DECLFW(UNLKS7017Write)\r
+{\r
+//  FCEU_printf("bs %04x %02x\n",A,V);\r
+    if((A & 0xFF00) == 0x4A00)\r
+    {\r
+      reg = ((A >> 2) & 3)|((A >> 4) & 4);\r
+    }\r
+    else if ((A & 0xFF00) == 0x5100)\r
+    {\r
+      Sync();\r
+    }\r
+    else if (A == 0x4020)\r
+    {\r
+      X6502_IRQEnd(FCEU_IQEXT);\r
+      IRQCount&=0xFF00;\r
+      IRQCount|=V;\r
+    }\r
+    else if (A == 0x4021)\r
+    {\r
+      X6502_IRQEnd(FCEU_IQEXT);\r
+      IRQCount&=0xFF;\r
+      IRQCount|=V<<8;\r
+      IRQa = 1;\r
+    }\r
+    else if (A == 0x4025)\r
+    {\r
+      mirr = ((V & 8) >> 3) ^ 1;\r
+    }\r
+}\r
+\r
+static DECLFR(FDSRead4030)\r
+{\r
+  X6502_IRQEnd(FCEU_IQEXT);\r
+  return X.IRQlow&FCEU_IQEXT?1:0;\r
+}\r
+\r
+static void FP_FASTAPASS(1) UNL7017IRQ(int a)\r
+{\r
+ if(IRQa)\r
+ {\r
+  IRQCount-=a;\r
+  if(IRQCount<=0)\r
+  {\r
+    IRQa=0;\r
+    X6502_IRQBegin(FCEU_IQEXT);\r
+  }\r
+ }\r
+}\r
+\r
+static void UNLKS7017Power(void)\r
+{\r
+  Sync();\r
+  setchr8(0);\r
+  setprg8r(0x10,0x6000,0);\r
+  SetReadHandler(0x6000,0x7FFF,CartBR);\r
+  SetWriteHandler(0x6000,0x7FFF,CartBW);\r
+  SetReadHandler(0x8000,0xFFFF,CartBR);\r
+  SetReadHandler(0x4030,0x4030,FDSRead4030);\r
+  SetWriteHandler(0x4020,0x5FFF,UNLKS7017Write);\r
+}\r
+\r
+static void UNLKS7017Close(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 UNLKS7017_Init(CartInfo *info)\r
+{\r
+  info->Power=UNLKS7017Power;\r
+  info->Close=UNLKS7017Close;\r
+  MapIRQHook=UNL7017IRQ;\r
+\r
+  WRAMSIZE=8192;\r
+  WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);\r
+  SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);\r
+  AddExState(WRAM, WRAMSIZE, 0, "WRAM");\r
+\r
+  GameStateRestore=StateRestore;\r
+  AddExState(&StateRegs, ~0, 0, 0);\r
+}\r