386f5371 |
1 | /* FCE Ultra - NES/Famicom Emulator\r |
2 | *\r |
3 | * Copyright notice for this file:\r |
4 | * Copyright (C) 2005 CaH4e3\r |
5 | *\r |
6 | * This program is free software; you can redistribute it and/or modify\r |
7 | * it under the terms of the GNU General Public License as published by\r |
8 | * the Free Software Foundation; either version 2 of the License, or\r |
9 | * (at your option) any later version.\r |
10 | *\r |
11 | * This program is distributed in the hope that it will be useful,\r |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of\r |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r |
14 | * GNU General Public License for more details.\r |
15 | *\r |
16 | * You should have received a copy of the GNU General Public License\r |
17 | * along with this program; if not, write to the Free Software\r |
43725da7 |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r |
386f5371 |
19 | *\r |
20 | * Lethal Weapon (VRC4 mapper)\r |
21 | */\r |
22 | \r |
23 | #include "mapinc.h"\r |
24 | \r |
25 | static uint8 prg0, prg1, mirr, swap;\r |
26 | static uint8 chr[8];\r |
27 | static uint8 IRQCount; \r |
28 | static uint8 IRQPre;\r |
29 | static uint8 IRQa;\r |
30 | \r |
31 | static SFORMAT StateRegs[]=\r |
32 | {\r |
33 | {&prg0, 1, "PRG0"},\r |
34 | {&prg0, 1, "PRG1"},\r |
35 | {&mirr, 1, "MIRR"},\r |
36 | {&swap, 1, "SWAP"},\r |
37 | {chr, 8, "CHR"},\r |
43725da7 |
38 | {&IRQCount, 1, "IRQC"},\r |
39 | {&IRQPre, 1, "IRQP"},\r |
386f5371 |
40 | {&IRQa, 1, "IRQA"},\r |
41 | {0}\r |
42 | };\r |
43 | \r |
44 | static void SyncPrg(void)\r |
45 | {\r |
46 | if(swap&3)\r |
47 | {\r |
48 | setprg8(0x8000,~1);\r |
49 | setprg8(0xC000,prg0);\r |
50 | } \r |
51 | else\r |
52 | {\r |
53 | setprg8(0x8000,prg0);\r |
54 | setprg8(0xC000,~1);\r |
55 | }\r |
56 | setprg8(0xA000,prg1);\r |
57 | setprg8(0xE000,~0);\r |
58 | }\r |
59 | \r |
60 | static void SyncChr(void)\r |
61 | {\r |
62 | int i;\r |
63 | for(i=0; i<8; i++)\r |
64 | setchr1(i<<10,chr[i]);\r |
65 | setmirror(mirr^1);\r |
66 | }\r |
67 | \r |
68 | static void StateRestore(int version)\r |
69 | {\r |
70 | SyncPrg();\r |
71 | SyncChr();\r |
72 | }\r |
73 | \r |
74 | static DECLFW(UNLTF1201Write)\r |
75 | {\r |
76 | A=(A&0xF003)|((A&0xC)>>2);\r |
77 | if((A>=0xB000)&&(A<=0xE003))\r |
78 | {\r |
79 | int ind=(((A>>11)-6)|(A&1))&7;\r |
80 | int sar=((A&2)<<1);\r |
81 | chr[ind]=(chr[ind]&(0xF0>>sar))|((V&0x0F)<<sar);\r |
82 | SyncChr();\r |
83 | }\r |
84 | else switch (A&0xF003)\r |
85 | {\r |
86 | case 0x8000: prg0=V; SyncPrg(); break;\r |
87 | case 0xA000: prg1=V; SyncPrg(); break;\r |
88 | case 0x9000: mirr=V&1; SyncChr(); break;\r |
89 | case 0x9001: swap=V&3; SyncPrg(); break;\r |
90 | case 0xF000: IRQCount=((IRQCount&0xF0)|(V&0xF)); break;\r |
91 | case 0xF002: IRQCount=((IRQCount&0x0F)|((V&0xF)<<4)); break;\r |
92 | case 0xF001: \r |
93 | case 0xF003: IRQa=V&2; X6502_IRQEnd(FCEU_IQEXT); if(scanline<240) IRQCount-=8; break;\r |
94 | }\r |
95 | }\r |
96 | \r |
97 | static void UNLTF1201IRQCounter(void)\r |
98 | {\r |
99 | if(IRQa)\r |
100 | {\r |
101 | IRQCount++;\r |
102 | if(IRQCount==237)\r |
103 | {\r |
104 | X6502_IRQBegin(FCEU_IQEXT);\r |
105 | }\r |
106 | }\r |
107 | }\r |
108 | \r |
109 | static void UNLTF1201Power(void)\r |
110 | {\r |
111 | IRQPre=IRQCount=IRQa=0;\r |
112 | SetReadHandler(0x8000,0xFFFF,CartBR);\r |
113 | SetWriteHandler(0x8000,0xFFFF,UNLTF1201Write);\r |
114 | SyncPrg();\r |
115 | SyncChr();\r |
116 | }\r |
117 | \r |
118 | void UNLTF1201_Init(CartInfo *info)\r |
119 | {\r |
120 | info->Power=UNLTF1201Power;\r |
121 | GameHBIRQHook=UNLTF1201IRQCounter;\r |
122 | GameStateRestore=StateRestore;\r |
123 | AddExState(&StateRegs, ~0, 0, 0);\r |
124 | }\r |