--- /dev/null
+/* FCE Ultra - NES/Famicom Emulator\r
+ *\r
+ * Copyright notice for this file:\r
+ * Copyright (C) 2005 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
+ * Lethal Weapon (VRC4 mapper)\r
+ */\r
+\r
+#include "mapinc.h"\r
+\r
+static uint8 prg0, prg1, mirr, swap;\r
+static uint8 chr[8];\r
+static uint8 IRQCount; \r
+static uint8 IRQPre;\r
+static uint8 IRQa;\r
+\r
+static SFORMAT StateRegs[]=\r
+{\r
+ {&prg0, 1, "PRG0"},\r
+ {&prg0, 1, "PRG1"},\r
+ {&mirr, 1, "MIRR"},\r
+ {&swap, 1, "SWAP"},\r
+ {chr, 8, "CHR"},\r
+ {&IRQCount, 1, "IRQCOUNT"},\r
+ {&IRQPre, 1, "IRQPRE"},\r
+ {&IRQa, 1, "IRQA"},\r
+ {0}\r
+};\r
+\r
+static void SyncPrg(void)\r
+{\r
+ if(swap&3)\r
+ {\r
+ setprg8(0x8000,~1);\r
+ setprg8(0xC000,prg0);\r
+ } \r
+ else\r
+ {\r
+ setprg8(0x8000,prg0);\r
+ setprg8(0xC000,~1);\r
+ }\r
+ setprg8(0xA000,prg1);\r
+ setprg8(0xE000,~0);\r
+}\r
+\r
+static void SyncChr(void)\r
+{\r
+ int i;\r
+ for(i=0; i<8; i++)\r
+ setchr1(i<<10,chr[i]);\r
+ setmirror(mirr^1);\r
+}\r
+\r
+static void StateRestore(int version)\r
+{\r
+ SyncPrg();\r
+ SyncChr();\r
+}\r
+\r
+static DECLFW(UNLTF1201Write)\r
+{\r
+ A=(A&0xF003)|((A&0xC)>>2);\r
+ if((A>=0xB000)&&(A<=0xE003))\r
+ {\r
+ int ind=(((A>>11)-6)|(A&1))&7;\r
+ int sar=((A&2)<<1);\r
+ chr[ind]=(chr[ind]&(0xF0>>sar))|((V&0x0F)<<sar);\r
+ SyncChr();\r
+ }\r
+ else switch (A&0xF003)\r
+ {\r
+ case 0x8000: prg0=V; SyncPrg(); break;\r
+ case 0xA000: prg1=V; SyncPrg(); break;\r
+ case 0x9000: mirr=V&1; SyncChr(); break;\r
+ case 0x9001: swap=V&3; SyncPrg(); break;\r
+ case 0xF000: IRQCount=((IRQCount&0xF0)|(V&0xF)); break;\r
+ case 0xF002: IRQCount=((IRQCount&0x0F)|((V&0xF)<<4)); break;\r
+ case 0xF001: \r
+ case 0xF003: IRQa=V&2; X6502_IRQEnd(FCEU_IQEXT); if(scanline<240) IRQCount-=8; break;\r
+ }\r
+}\r
+ \r
+static void UNLTF1201IRQCounter(void)\r
+{\r
+ if(IRQa)\r
+ {\r
+ IRQCount++;\r
+ if(IRQCount==237)\r
+ {\r
+ X6502_IRQBegin(FCEU_IQEXT);\r
+ }\r
+ }\r
+}\r
+ \r
+static void UNLTF1201Power(void)\r
+{\r
+ IRQPre=IRQCount=IRQa=0;\r
+ SetReadHandler(0x8000,0xFFFF,CartBR);\r
+ SetWriteHandler(0x8000,0xFFFF,UNLTF1201Write);\r
+ SyncPrg();\r
+ SyncChr();\r
+}\r
+\r
+void UNLTF1201_Init(CartInfo *info)\r
+{\r
+ info->Power=UNLTF1201Power;\r
+ GameHBIRQHook=UNLTF1201IRQCounter;\r
+ GameStateRestore=StateRestore;\r
+ AddExState(&StateRegs, ~0, 0, 0);\r
+}\r