merge mapper code from FCEUX
[fceu.git] / boards / 176.c
1 /* FCE Ultra - NES/Famicom Emulator\r
2  *\r
3  * Copyright notice for this file:\r
4  *  Copyright (C) 2007 CaH4e3\r
5  *  Copyright (C) 2012 FCEUX team\r
6  *\r
7  * This program is free software; you can redistribute it and/or modify\r
8  * it under the terms of the GNU General Public License as published by\r
9  * the Free Software Foundation; either version 2 of the License, or\r
10  * (at your option) any later version.\r
11  *\r
12  * This program is distributed in the hope that it will be useful,\r
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15  * GNU General Public License for more details.\r
16  *\r
17  * You should have received a copy of the GNU General Public License\r
18  * along with this program; if not, write to the Free Software\r
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA\r
20  */\r
21 \r
22 #include "mapinc.h"\r
23 \r
24 extern uint32 ROM_size;\r
25 \r
26 static uint8 prg[4], chr, sbw, we_sram;\r
27 static uint8 *WRAM=NULL;\r
28 static uint32 WRAMSIZE;\r
29 \r
30 static SFORMAT StateRegs[]=\r
31 {\r
32   {prg, 4, "PRG"},\r
33   {&chr, 1, "CHR"},\r
34         {&sbw, 1, "SBW"},\r
35   {0}\r
36 };\r
37 \r
38 static void Sync(void)\r
39 {\r
40   setprg8r(0x10,0x6000,0);\r
41         setprg8(0x8000,prg[0]);\r
42         setprg8(0xA000,prg[1]);\r
43         setprg8(0xC000,prg[2]);\r
44         setprg8(0xE000,prg[3]);\r
45 \r
46   setchr8(chr);\r
47 }\r
48 \r
49 static DECLFW(M176Write_5001)\r
50\r
51         printf("%04X = $%02X\n",A,V);\r
52         if(sbw)\r
53         {\r
54                 prg[0] = V*4;\r
55                 prg[1] = V*4+1;\r
56                 prg[2] = V*4+2;\r
57                 prg[3] = V*4+3;\r
58         }\r
59   Sync();\r
60 }\r
61 \r
62 static DECLFW(M176Write_5010)\r
63\r
64         printf("%04X = $%02X\n",A,V);\r
65         if(V == 0x24) sbw = 1;\r
66   Sync();\r
67 }\r
68 \r
69 static DECLFW(M176Write_5011)\r
70\r
71         printf("%04X = $%02X\n",A,V);\r
72         V >>= 1;\r
73         if(sbw)\r
74         {\r
75                 prg[0] = V*4;\r
76                 prg[1] = V*4+1;\r
77                 prg[2] = V*4+2;\r
78                 prg[3] = V*4+3;\r
79         }\r
80   Sync();\r
81 }\r
82 \r
83 static DECLFW(M176Write_5FF1)\r
84\r
85         printf("%04X = $%02X\n",A,V);\r
86   V >>= 1;\r
87         prg[0] = V*4;\r
88         prg[1] = V*4+1;\r
89         prg[2] = V*4+2;\r
90         prg[3] = V*4+3;\r
91   Sync();\r
92 }\r
93 \r
94 static DECLFW(M176Write_5FF2)\r
95\r
96         printf("%04X = $%02X\n",A,V);\r
97   chr = V;\r
98   Sync();\r
99 }\r
100 \r
101 static DECLFW(M176Write_A001)\r
102 {\r
103         we_sram = V & 0x03;\r
104 }\r
105 \r
106 static DECLFW(M176Write_WriteSRAM)\r
107 {\r
108 //      if(we_sram)\r
109                 CartBW(A,V);\r
110 }\r
111 \r
112 static void M176Power(void)\r
113 {\r
114   SetReadHandler(0x6000,0x7fff,CartBR);\r
115   SetWriteHandler(0x6000,0x7fff,M176Write_WriteSRAM);\r
116   SetReadHandler(0x8000,0xFFFF,CartBR);\r
117         SetWriteHandler(0xA001,0xA001,M176Write_A001);\r
118         SetWriteHandler(0x5001,0x5001,M176Write_5001);\r
119         SetWriteHandler(0x5010,0x5010,M176Write_5010);\r
120         SetWriteHandler(0x5011,0x5011,M176Write_5011);\r
121   SetWriteHandler(0x5ff1,0x5ff1,M176Write_5FF1);\r
122   SetWriteHandler(0x5ff2,0x5ff2,M176Write_5FF2);\r
123         \r
124         we_sram = 0;\r
125         sbw = 0;\r
126         prg[0] = 0;\r
127         prg[1] = 1;\r
128         prg[2] = (ROM_size-2)&63;\r
129         prg[3] = (ROM_size-1)&63;\r
130   Sync();\r
131 }\r
132 \r
133 \r
134 static void M176Close(void)\r
135 {\r
136   if(WRAM)\r
137     FCEU_gfree(WRAM);\r
138   WRAM=NULL;\r
139 }\r
140 \r
141 static void StateRestore(int version)\r
142 {\r
143   Sync();\r
144 }\r
145 \r
146 void Mapper176_Init(CartInfo *info)\r
147 {\r
148   info->Power=M176Power;\r
149   info->Close=M176Close;\r
150 \r
151   GameStateRestore=StateRestore;\r
152 \r
153   WRAMSIZE=8192;\r
154   WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);\r
155   SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);\r
156   AddExState(WRAM, WRAMSIZE, 0, "WRAM");\r
157   AddExState(&StateRegs, ~0, 0, 0);\r
158 }\r