merge mapper code from FCEUX
[fceu.git] / boards / konami-qtai.c
index b78d8e3..83630a6 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
- *
- * CAI Shogakko no Sansu
- */
-
-#include "mapinc.h"
-
-static uint8 *CHRRAM=NULL;
-static uint8 SWRAM[4096];
-
-static uint8 regs[16];
-static uint8 WRAM[4096];
-static SFORMAT StateRegs[]=
-{
-  {&regs, 16, "REGS"},
-  {WRAM, 4096, "WRAM"},
-  {0}
-};
-
-static void Sync(void)
-{
-  if(regs[5]&0x40)
-  {
-    setchr4r(0,0x1000,regs[5]&0x3F);
-  }
-  else
-  {
-    setchr4r(0x10,0x0000,regs[5]);
-    setchr4r(0x10,0x1000,regs[5]^1);
-  }
-  setprg8r((regs[2]>>6)&1,0x8000,(regs[2]&0x3F));
-  setprg8r((regs[3]>>6)&1,0xA000,(regs[3]&0x3F));
-  setprg8r((regs[4]>>6)&1,0xC000,(regs[4]&0x3F));
-  setprg8r(1,0xE000,~0);
-  setmirror((regs[0xA]&3));
-}
-
-static DECLFW(M190Write)
-{
-// FCEU_printf("write %04x:%04x %d, %d\n",A,V,scanline,timestamp);
-  regs[(A&0x0F00)>>8]=V;
-  Sync();
-}
-
-static DECLFR(M190Read)
-{
-// FCEU_printf("read %04x:%04x %d, %d\n",A,regs[(A&0x0F00)>>8],scanline,timestamp);
-  return regs[(A&0x0F00)>>8];
-}
-
-static DECLFR(AWRAM)
-{
-  return(WRAM[A-0x7000]);
-}
-static DECLFW(BWRAM)
-{
-  WRAM[A-0x7000]=V;
-}
-
-static DECLFR(ASWRAM)
-{
-  return(SWRAM[A-0x6000]);
-}
-static DECLFW(BSWRAM)
-{
-  SWRAM[A-0x6000]=V;
-}
-
-static void M190Power(void)
-{
-  setvram8(CHRRAM);
-  SetReadHandler(0x8000,0xFFFF,CartBR);
-  SetWriteHandler(0x8000,0xFFFF,M190Write);
-// SetReadHandler(0xDA00,0xDA00,M190Read);
-// SetReadHandler(0xDB00,0xDB00,M190Read);
-  SetReadHandler(0xDC00,0xDC00,M190Read);
-  SetReadHandler(0xDD00,0xDD00,M190Read);
-  SetReadHandler(0x7000,0x7FFF,AWRAM);
-  SetWriteHandler(0x7000,0x7FFF,BWRAM);
-  SetReadHandler(0x6000,0x6FFF,ASWRAM);
-  SetWriteHandler(0x6000,0x6FFF,BSWRAM);
-#ifdef ASM_6502
-  Page[14]=Page[15]=WRAM-0x7000;
-  Page[12]=Page[13]=SWRAM-0x6000;
-#endif
-  Sync();
-}
-
-static void M190Close(void)
-{
-  if(CHRRAM)
-    FCEU_gfree(CHRRAM);
-  CHRRAM=NULL;
-}
-
-static void StateRestore(int version)
-{
-  Sync();
-}
-
-void Mapper190_Init(CartInfo *info)
-{
-  info->Power=M190Power;
-  info->Close=M190Close;
-  if(info->battery)
-  {
-    info->SaveGame[0]=SWRAM;
-    info->SaveGameLen[0]=4096;
-  }
-  GameStateRestore=StateRestore;
-  CHRRAM=(uint8*)FCEU_gmalloc(8192);
-  SetupCartCHRMapping(0x10,CHRRAM,8192,1);
-  AddExState(CHRRAM, 8192, 0, "CHRRAM");
-  AddExState(&StateRegs, ~0, 0, 0);
-}
+/* FCE Ultra - NES/Famicom Emulator\r
+ *\r
+ * Copyright notice for this file:\r
+ *  Copyright (C) 2005-2008 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
+ * CAI Shogakko no Sansu\r
+ */\r
+\r
+#include "mapinc.h"\r
+\r
+static uint8 QTAINTRAM[2048];\r
+static writefunc old2007wrap;\r
+\r
+static uint16 CHRSIZE = 8192;\r
+static uint16 WRAMSIZE = 8192 + 4096;\r
+static uint8 *CHRRAM=NULL;\r
+static uint8 *WRAM = NULL;\r
+\r
+static uint8 IRQa, K4IRQ;\r
+static uint32 IRQLatch, IRQCount;\r
+\r
+static uint8 regs[16];\r
+//static uint8 test[8];\r
+static SFORMAT StateRegs[]=\r
+{\r
+  {&IRQCount, 1, "IRQC"},\r
+  {&IRQLatch, 1, "IRQL"},\r
+  {&IRQa, 1, "IRQA"},\r
+  {&K4IRQ, 1, "K4IRQ"},\r
+  {regs, 16, "REGS"},\r
+  {0}\r
+};\r
+\r
+static void chrSync(void)\r
+{\r
+    setchr4r(0x10,0x0000,regs[5]&1);\r
+    setchr4r(0x10,0x1000,0);\r
+}\r
+\r
+static void Sync(void)\r
+{\r
+    chrSync();\r
+//  if(regs[0xA]&0x10)\r
+//  {\r
+/*    setchr1r(0x10,0x0000,(((regs[5]&1))<<2)+0);\r
+    setchr1r(0x10,0x0400,(((regs[5]&1))<<2)+1);\r
+    setchr1r(0x10,0x0800,(((regs[5]&1))<<2)+2);\r
+    setchr1r(0x10,0x0c00,(((regs[5]&1))<<2)+3);\r
+    setchr1r(0x10,0x1000,0);\r
+    setchr1r(0x10,0x1400,1);\r
+    setchr1r(0x10,0x1800,2);\r
+    setchr1r(0x10,0x1c00,3);*/\r
+/*    setchr1r(0x10,0x0000,(((regs[5]&1))<<2)+0);\r
+    setchr1r(0x10,0x0400,(((regs[5]&1))<<2)+1);\r
+    setchr1r(0x10,0x0800,(((regs[5]&1))<<2)+2);\r
+    setchr1r(0x10,0x0c00,(((regs[5]&1))<<2)+3);\r
+    setchr1r(0x10,0x1000,(((regs[5]&1)^1)<<2)+4);\r
+    setchr1r(0x10,0x1400,(((regs[5]&1)^1)<<2)+5);\r
+    setchr1r(0x10,0x1800,(((regs[5]&1)^1)<<2)+6);\r
+    setchr1r(0x10,0x1c00,(((regs[5]&1)^1)<<2)+7);\r
+*/\r
+//  }\r
+//  else\r
+//  {\r
+/*\r
+    setchr1r(0x10,0x0000,(((regs[5]&1)^1)<<2)+0);\r
+    setchr1r(0x10,0x0400,(((regs[5]&1)^1)<<2)+1);\r
+    setchr1r(0x10,0x0800,(((regs[5]&1)^1)<<2)+2);\r
+    setchr1r(0x10,0x0c00,(((regs[5]&1)^1)<<2)+3);\r
+    setchr1r(0x10,0x1000,(((regs[5]&1))<<2)+4);\r
+    setchr1r(0x10,0x1400,(((regs[5]&1))<<2)+5);\r
+    setchr1r(0x10,0x1800,(((regs[5]&1))<<2)+6);\r
+    setchr1r(0x10,0x1c00,(((regs[5]&1))<<2)+7);\r
+//  }\r
+//*/\r
+/*    setchr1r(1,0x0000,test[0]);\r
+    setchr1r(1,0x0400,test[1]);\r
+    setchr1r(1,0x0800,test[2]);\r
+    setchr1r(1,0x0c00,test[3]);\r
+    setchr1r(1,0x1000,test[4]);\r
+    setchr1r(1,0x1400,test[5]);\r
+    setchr1r(1,0x1800,test[6]);\r
+    setchr1r(1,0x1c00,test[7]);\r
+*/\r
+  setprg4r(0x10,0x6000,regs[0]&1);\r
+  if(regs[2]>=0x40)\r
+    setprg8r(1,0x8000,(regs[2]-0x40));\r
+  else\r
+    setprg8r(0,0x8000,(regs[2]&0x3F));\r
+  if(regs[3]>=0x40)\r
+    setprg8r(1,0xA000,(regs[3]-0x40));\r
+  else\r
+    setprg8r(0,0xA000,(regs[3]&0x3F));\r
+  if(regs[4]>=0x40)\r
+    setprg8r(1,0xC000,(regs[4]-0x40));\r
+  else\r
+    setprg8r(0,0xC000,(regs[4]&0x3F));\r
+\r
+  setprg8r(1,0xE000,~0);\r
+  setmirror(MI_V);\r
+}\r
+\r
+/*static DECLFW(TestWrite)\r
+{\r
+  test[A&7] = V;\r
+  Sync();\r
+}*/\r
+\r
+static DECLFW(M190Write)\r
+{\r
+// FCEU_printf("write %04x:%04x %d, %d\n",A,V,scanline,timestamp);\r
+  regs[(A&0x0F00)>>8]=V;\r
+  switch(A)\r
+  {  \r
+    case 0xd600:IRQLatch&=0xFF00;IRQLatch|=V;break;\r
+    case 0xd700:IRQLatch&=0x00FF;IRQLatch|=V<<8;break;\r
+    case 0xd900:IRQCount=IRQLatch;IRQa=V&2;K4IRQ=V&1;X6502_IRQEnd(FCEU_IQEXT);break;\r
+    case 0xd800:IRQa=K4IRQ;X6502_IRQEnd(FCEU_IQEXT);break;\r
+  }\r
+  Sync();\r
+}\r
+\r
+static DECLFR(M190Read)\r
+{\r
+// FCEU_printf("read %04x:%04x %d, %d\n",A,regs[(A&0x0F00)>>8],scanline,timestamp);\r
+  return regs[(A&0x0F00)>>8]+regs[0x0B];\r
+}\r
+static void VRC5IRQ(int a)\r
+{\r
+  if(IRQa)\r
+  {\r
+    IRQCount+=a;\r
+    if(IRQCount&0x10000)\r
+    {\r
+      X6502_IRQBegin(FCEU_IQEXT);\r
+//      IRQCount=IRQLatch;\r
+    }\r
+  }\r
+}\r
+\r
+static void Mapper190_PPU(uint32 A)\r
+{\r
+  if(A>=0x2000)\r
+  {\r
+     setchr4r(0x10,0x0000,QTAINTRAM[A&0x1FFF]&1);\r
+     setchr4r(0x10,0x1000,QTAINTRAM[A&0x1FFF]&1);\r
+  }\r
+//  else\r
+//     chrSync();\r
+}\r
+\r
+static DECLFW(M1902007Wrap)\r
+{\r
+  if(A>=0x2000)\r
+  {\r
+    if(regs[0xA]&1)\r
+      QTAINTRAM[A&0x1FFF]=V;\r
+    else\r
+      old2007wrap(A,V);\r
+  }\r
+}\r
+\r
+\r
+static void M190Power(void)\r
+{\r
+/*  test[0]=0;\r
+  test[1]=1;\r
+  test[2]=2;\r
+  test[3]=3;\r
+  test[4]=4;\r
+  test[5]=5;\r
+  test[6]=6;\r
+  test[7]=7;\r
+*/  \r
+  setprg4r(0x10,0x7000,2);\r
+\r
+  old2007wrap=GetWriteHandler(0x2007);\r
+  SetWriteHandler(0x2007,0x2007,M1902007Wrap);\r
+\r
+  SetReadHandler(0x6000,0xFFFF,CartBR);\r
+//  SetWriteHandler(0x5000,0x5007,TestWrite);\r
+  SetWriteHandler(0x6000,0x7FFF,CartBW);\r
+  SetWriteHandler(0x8000,0xFFFF,M190Write);\r
+  SetReadHandler(0xDC00,0xDC00,M190Read);\r
+  SetReadHandler(0xDD00,0xDD00,M190Read);\r
+  Sync();\r
+}\r
+\r
+static void M190Close(void)\r
+{\r
+  if(CHRRAM)\r
+    FCEU_gfree(CHRRAM);\r
+  CHRRAM=NULL;\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 Mapper190_Init(CartInfo *info)\r
+{\r
+  info->Power=M190Power;\r
+  info->Close=M190Close;\r
+  GameStateRestore=StateRestore;\r
+\r
+  MapIRQHook=VRC5IRQ;\r
+  //PPU_hook=Mapper190_PPU;\r
+\r
+  CHRRAM=(uint8*)FCEU_gmalloc(CHRSIZE);\r
+  SetupCartCHRMapping(0x10,CHRRAM,CHRSIZE,1);\r
+  AddExState(CHRRAM, CHRSIZE, 0, "CHRRAM");\r
+\r
+  WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);\r
+  SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);\r
+  AddExState(WRAM, WRAMSIZE, 0, "WRAM");\r
+\r
+  if(info->battery)\r
+  {\r
+    info->SaveGame[0] = WRAM;\r
+    info->SaveGameLen[0] = WRAMSIZE - 4096;\r
+  }\r
+\r
+  AddExState(&StateRegs, ~0, 0, 0);\r
+}\r