merge mapper code from FCEUX
[fceu.git] / boards / fk23c.c
index abf0859..c869680 100644 (file)
-/* FCE Ultra - NES/Famicom Emulator
- *
- * Copyright notice for this file:
- *  Copyright (C) 2005 CaH4e3
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-#include "mapinc.h"
-#include "mmc3.h"
-
-static uint8 unromchr;
-
-static void BMCFK23CCW(uint32 A, uint8 V)
-{
-  if(EXPREGS[0]&0x40)
-    setchr8((EXPREGS[2]&0xFC)|unromchr);
-  else
-  {
-    uint16 base=(EXPREGS[2]&0x7F)<<3;
-    setchr1(A,V|base);
-    if(EXPREGS[3]&2)
-    {
-      setchr1(0x0400,EXPREGS[6]|base);
-      setchr1(0x0C00,EXPREGS[7]|base);
-    }
-  }
-}
-
-static void BMCFK23CPW(uint32 A, uint8 V)
-{
-  if(EXPREGS[0]&4)
-    setprg32(0x8000,EXPREGS[1]>>1);
-  else
-  { 
-    if(EXPREGS[0]&2)
-      setprg8(A,(V&(0x3F>>(EXPREGS[0]&3)))|(EXPREGS[1]<<1));
-    else
-      setprg8(A,V);
-    if(EXPREGS[3]&2)
-    {
-      setprg8(0xC000,EXPREGS[4]);
-      setprg8(0xE000,EXPREGS[5]);
-    }
-  }
-}
-
-static DECLFW(BMCFK23C8003Write)
-{
-  if(EXPREGS[0]&0x40)
-  {
-    unromchr=V&3;
-    FixMMC3CHR(MMC3_cmd);
-  }
-  else
-  {
-    if((A==0x8001)&&(EXPREGS[3]&2&&MMC3_cmd&8))
-    {
-      EXPREGS[4|(MMC3_cmd&3)]=V;
-      FixMMC3PRG(MMC3_cmd);
-      FixMMC3CHR(MMC3_cmd);
-    }
-    else    
-      MMC3_CMDWrite(A,V);
-  }
-}
-
-static DECLFW(BMCFK23CWrite)
-{
-  EXPREGS[A&3]=V;
-  FixMMC3PRG(MMC3_cmd);
-  FixMMC3CHR(MMC3_cmd);
-}
-
-static void BMCFK23CReset(void)
-{
-  EXPREGS[0]=EXPREGS[1]=EXPREGS[2]=EXPREGS[3]=0;
-  EXPREGS[4]=EXPREGS[5]=EXPREGS[6]=EXPREGS[7]=0xFF;
-  MMC3RegReset();
-}
-
-static void BMCFK23CPower(void)
-{
-  EXPREGS[0]=EXPREGS[1]=EXPREGS[2]=EXPREGS[3]=0;
-  EXPREGS[4]=EXPREGS[5]=EXPREGS[6]=EXPREGS[7]=0xFF;
-  GenMMC3Power();
-  SetWriteHandler(0x5ff0,0x5fff,BMCFK23CWrite);
-  SetWriteHandler(0x8000,0x8003,BMCFK23C8003Write);
-}
-
-void BMCFK23C_Init(CartInfo *info)
-{
-  GenMMC3_Init(info, 512, 256, 8, 0);
-  cwrap=BMCFK23CCW;
-  pwrap=BMCFK23CPW;
-  info->Power=BMCFK23CPower;
-  info->Reset=BMCFK23CReset;
-  AddExState(EXPREGS, 8, 0, "EXPR");
-  AddExState(&unromchr, 1, 0, "UNCHR");
-}
+/* 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
+#include "mmc3.h"\r
+#include "../ines.h"\r
+\r
+static bool is_BMCFK23CA;\r
+static uint8 unromchr;\r
+static uint32 dipswitch;\r
+static uint8 *CHRRAM=NULL;\r
+static uint32 CHRRAMSize;\r
+\r
+static void BMCFK23CCW(uint32 A, uint8 V)\r
+{\r
+  if(EXPREGS[0]&0x40)\r
+    setchr8(EXPREGS[2]|unromchr);\r
+  else if(EXPREGS[0]&0x20) {\r
+    setchr1r(0x10, A, V);\r
+  }\r
+  else\r
+  {\r
+    uint16 base=(EXPREGS[2]&0x7F)<<3;\r
+    if(EXPREGS[3]&2)\r
+    {\r
+      int cbase=(MMC3_cmd&0x80)<<5;\r
+      setchr1(A,V|base);\r
+      setchr1(0x0000^cbase,DRegBuf[0]|base);\r
+      setchr1(0x0400^cbase,EXPREGS[6]|base);\r
+      setchr1(0x0800^cbase,DRegBuf[1]|base);\r
+      setchr1(0x0c00^cbase,EXPREGS[7]|base);\r
+    }\r
+    else\r
+      setchr1(A,V|base);\r
+  }\r
+}\r
+\r
+//some games are wired differently, and this will need to be changed.\r
+//all the WXN games require prg_bonus = 1, and cah4e3's multicarts require prg_bonus = 0\r
+//we'll populate this from a game database\r
+static int prg_bonus;\r
+static int prg_mask;\r
+\r
+//prg_bonus = 0\r
+//4-in-1 (FK23C8021)[p1][!].nes\r
+//4-in-1 (FK23C8033)[p1][!].nes\r
+//4-in-1 (FK23C8043)[p1][!].nes\r
+//4-in-1 (FK23Cxxxx, S-0210A PCB)[p1][!].nes\r
+\r
+//prg_bonus = 1\r
+//[m176]大富翁2-上海大亨.wxn.nes\r
+//[m176]宠物翡翠.fix.nes\r
+//[m176]格兰帝亚.wxn.nes\r
+//[m176]梦幻之星.wxn.nes\r
+//[m176]水浒神兽.fix.nes\r
+//[m176]西楚霸王.fix.nes\r
+//[m176]超级大富翁.wxn.nes\r
+//[m176]雄霸天下.wxn.nes\r
+\r
+//works as-is under virtuanes m176\r
+//[m176]三侠五义.wxn.nes\r
+//[m176]口袋金.fix.nes\r
+//[m176]爆笑三国.fix.nes\r
+\r
+//needs other tweaks\r
+//[m176]三国忠烈传.wxn.nes\r
+//[m176]破釜沉舟.fix.nes\r
+\r
+//PRG wrapper\r
+static void BMCFK23CPW(uint32 A, uint8 V)\r
+{\r
+  uint32 bank = (EXPREGS[1] & 0x1F);\r
+  uint32 hiblock = ((EXPREGS[0] & 8) << 4)|((EXPREGS[0] & 0x80) << 1)|(UNIFchrrama?((EXPREGS[2] & 0x40)<<3):0);\r
+  uint32 block = (EXPREGS[1] & 0x60) | hiblock;\r
+  uint32 extra = (EXPREGS[3] & 2);\r
+\r
+  if((EXPREGS[0]&7)==4)\r
+    setprg32(0x8000,EXPREGS[1]>>1);\r
+  else if ((EXPREGS[0]&7)==3)\r
+  {\r
+    setprg16(0x8000,EXPREGS[1]);\r
+    setprg16(0xC000,EXPREGS[1]);\r
+  }  \r
+  else\r
+  { \r
+    if(EXPREGS[0]&3)\r
+               {\r
+                       uint32 blocksize = (6)-(EXPREGS[0]&3);\r
+                       uint32 mask = (1<<blocksize)-1;\r
+                       V &= mask;\r
+                       //V &= 63; //? is this a good idea?\r
+                       V |= (EXPREGS[1]<<1);\r
+      setprg8(A,V);\r
+               }\r
+    else\r
+      setprg8(A,V & prg_mask);\r
+\r
+    if(EXPREGS[3]&2)\r
+    {\r
+      setprg8(0xC000,EXPREGS[4]);\r
+      setprg8(0xE000,EXPREGS[5]);\r
+    }\r
+  }\r
+       setprg8r(0x10,0x6000,A001B&3);\r
+}\r
+\r
+//PRG handler ($8000-$FFFF)\r
+static DECLFW(BMCFK23CHiWrite)\r
+{\r
+       if(EXPREGS[0]&0x40)\r
+       {\r
+               if(EXPREGS[0]&0x30)\r
+                       unromchr=0;\r
+               else\r
+               {\r
+                       unromchr=V&3;\r
+                       FixMMC3CHR(MMC3_cmd);\r
+               }\r
+       }\r
+       else\r
+       {\r
+               if((A==0x8001)&&(EXPREGS[3]&2&&MMC3_cmd&8))\r
+               {\r
+                       EXPREGS[4|(MMC3_cmd&3)]=V;\r
+                       FixMMC3PRG(MMC3_cmd);\r
+                       FixMMC3CHR(MMC3_cmd);\r
+               }\r
+    else\r
+      if(A<0xC000) {\r
+        if(UNIFchrrama) { // hacky... strange behaviour, must be bit scramble due to pcb layot restrictions\r
+                          // check if it not interfer with other dumps\r
+          if((A==0x8000)&&(V==0x46))\r
+            V=0x47;\r
+          else if((A==0x8000)&&(V==0x47))\r
+            V=0x46;\r
+        }\r
+        MMC3_CMDWrite(A,V);\r
+        FixMMC3PRG(MMC3_cmd);\r
+      }\r
+      else\r
+        MMC3_IRQWrite(A,V);\r
+  }\r
+}\r
+\r
+//EXP handler ($5000-$5FFF)\r
+static DECLFW(BMCFK23CWrite)\r
+{\r
+  if(A&(1<<(dipswitch+4)))\r
+  {\r
+               //printf("+ ");\r
+    EXPREGS[A&3]=V;\r
+\r
+               bool remap = false;\r
+\r
+               //sometimes writing to reg0 causes remappings to occur. we think the 2 signifies this. \r
+               //if not, 0x24 is a value that is known to work\r
+               //however, the low 4 bits are known to control the mapping mode, so 0x20 is more likely to be the immediate remap flag\r
+               remap |= ((EXPREGS[0]&0xF0)==0x20); \r
+\r
+               //this is an actual mapping reg. i think reg0 controls what happens when reg1 is written. anyway, we have to immediately remap these\r
+               remap |= (A&3)==1; \r
+               //this too.\r
+               remap |= (A&3)==2; \r
+\r
+               if(remap)\r
+               {\r
+                       FixMMC3PRG(MMC3_cmd);\r
+                       FixMMC3CHR(MMC3_cmd);\r
+               }\r
+  }\r
+\r
+       if(is_BMCFK23CA)\r
+       {\r
+               if(EXPREGS[3]&2)\r
+                       EXPREGS[0] &= ~7;   // hacky hacky! if someone wants extra banking, then for sure doesn't want mode 4 for it! (allow to run A version boards on normal mapper)\r
+       }\r
+\r
+       //printf("%04X = $%02X\n",A,V);\r
+       //printf("%02X %02X %02X %02X\n",EXPREGS[0],EXPREGS[1],EXPREGS[2],EXPREGS[3]);\r
+}\r
+\r
+static void BMCFK23CReset(void)\r
+{\r
+       //NOT NECESSARY ANYMORE\r
+       //this little hack makes sure that we try all the dip switch settings eventually, if we reset enough\r
+       // dipswitch++;\r
+       // dipswitch&=7;\r
+       //printf("BMCFK23C dipswitch set to %d\n",dipswitch);\r
+\r
+  EXPREGS[0]=EXPREGS[1]=EXPREGS[2]=EXPREGS[3]=0;\r
+  EXPREGS[4]=EXPREGS[5]=EXPREGS[6]=EXPREGS[7]=0xFF;\r
+  MMC3RegReset();\r
+  FixMMC3PRG(MMC3_cmd);\r
+  FixMMC3CHR(MMC3_cmd);\r
+}\r
+\r
+static void BMCFK23CPower(void)\r
+{\r
+       dipswitch = 0;\r
+  GenMMC3Power();\r
+  EXPREGS[0]=EXPREGS[1]=EXPREGS[2]=EXPREGS[3]=0;\r
+  EXPREGS[4]=EXPREGS[5]=EXPREGS[6]=EXPREGS[7]=0xFF;\r
+  GenMMC3Power();\r
+  SetWriteHandler(0x5000,0x5fff,BMCFK23CWrite);\r
+  SetWriteHandler(0x8000,0xFFFF,BMCFK23CHiWrite);\r
+  FixMMC3PRG(MMC3_cmd);\r
+  FixMMC3CHR(MMC3_cmd);\r
+}\r
+\r
+static void BMCFK23CAPower(void)\r
+{\r
+  GenMMC3Power();\r
+  dipswitch = 0;\r
+  EXPREGS[0]=EXPREGS[1]=EXPREGS[2]=EXPREGS[3]=0;\r
+  EXPREGS[4]=EXPREGS[5]=EXPREGS[6]=EXPREGS[7]=0xFF;\r
+  SetWriteHandler(0x5000,0x5fff,BMCFK23CWrite);\r
+  SetWriteHandler(0x8000,0xFFFF,BMCFK23CHiWrite);\r
+  FixMMC3PRG(MMC3_cmd);\r
+  FixMMC3CHR(MMC3_cmd);\r
+}\r
+\r
+static void BMCFK23CAClose(void)\r
+{\r
+  if(CHRRAM)\r
+    FCEU_gfree(CHRRAM);\r
+  CHRRAM=NULL;\r
+}\r
+\r
+void BMCFK23C_Init(CartInfo *info)\r
+{\r
+       is_BMCFK23CA = false;\r
+\r
+  GenMMC3_Init(info, 512, 256, 8, 0);\r
+  cwrap=BMCFK23CCW;\r
+  pwrap=BMCFK23CPW;\r
+  info->Power=BMCFK23CPower;\r
+  info->Reset=BMCFK23CReset;\r
+  AddExState(EXPREGS, 8, 0, "EXPR");\r
+  AddExState(&unromchr, 1, 0, "UNCHR");\r
+  AddExState(&dipswitch, 1, 0, "DIPSW");\r
+\r
+       prg_bonus = 1;\r
+       if(MasterRomInfoParams.find("bonus") != MasterRomInfoParams.end())\r
+               prg_bonus = atoi(MasterRomInfoParams["bonus"].c_str());\r
+\r
+       prg_mask = 0x7F>>(prg_bonus);\r
+}\r
+\r
+void BMCFK23CA_Init(CartInfo *info)\r
+{\r
+       is_BMCFK23CA = true;\r
+\r
+  GenMMC3_Init(info, 512, 256, 8, 0);\r
+  cwrap=BMCFK23CCW;\r
+  pwrap=BMCFK23CPW;\r
+  info->Power=BMCFK23CAPower;\r
+  info->Reset=BMCFK23CReset;\r
+  info->Close=BMCFK23CAClose;\r
+\r
+       CHRRAMSize=8192;\r
+  CHRRAM=(uint8*)FCEU_gmalloc(CHRRAMSize);\r
+  SetupCartCHRMapping(0x10, CHRRAM, CHRRAMSize, 1);\r
+  AddExState(CHRRAM, CHRRAMSize, 0, "CRAM");\r
+\r
+  AddExState(EXPREGS, 8, 0, "EXPR");\r
+  AddExState(&unromchr, 1, 0, "UNCHR");\r
+  AddExState(&dipswitch, 1, 0, "DIPSW");\r
+\r
+       prg_bonus = 1;\r
+       if(MasterRomInfoParams.find("bonus") != MasterRomInfoParams.end())\r
+               prg_bonus = atoi(MasterRomInfoParams["bonus"].c_str());\r
+       prg_mask = 0x7F>>(prg_bonus);\r
+}\r