merge mapper code from FCEUX
[fceu.git] / boards / 103.c
1 /* FCE Ultra - NES/Famicom Emulator\r
2  *\r
3  * Copyright notice for this file:\r
4  *  Copyright (C) 2007 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
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA\r
19  */\r
20 \r
21 #include "mapinc.h"\r
22 \r
23 static uint8 reg0, reg1, reg2;\r
24 static uint8 *WRAM=NULL;\r
25 static uint32 WRAMSIZE;\r
26 \r
27 static SFORMAT StateRegs[]=\r
28 {\r
29   {&reg0, 1, "REG0"},\r
30   {&reg1, 1, "REG1"},\r
31   {&reg2, 1, "REG2"},\r
32   {0}\r
33 };\r
34 \r
35 static void Sync(void)\r
36 {\r
37   setchr8(0);\r
38   setprg8(0x8000,0xc);\r
39   setprg8(0xe000,0xf);\r
40   if(reg2&0x10)\r
41   {\r
42     setprg8(0x6000,reg0);\r
43     setprg8(0xa000,0xd);\r
44     setprg8(0xc000,0xe);\r
45   }\r
46   else\r
47   {\r
48     setprg8r(0x10,0x6000,0);\r
49     setprg4(0xa000,(0xd<<1));\r
50     setprg2(0xb000,(0xd<<2)+2);\r
51     setprg2r(0x10,0xb800,4);\r
52     setprg2r(0x10,0xc000,5);\r
53     setprg2r(0x10,0xc800,6);\r
54     setprg2r(0x10,0xd000,7);\r
55     setprg2(0xd800,(0xe<<2)+3);\r
56   }\r
57   setmirror(reg1^1);\r
58 }\r
59 \r
60 static DECLFW(M103RamWrite0)\r
61 {\r
62   WRAM[A&0x1FFF]=V;\r
63 }\r
64 \r
65 static DECLFW(M103RamWrite1)\r
66 {\r
67   WRAM[0x2000+((A-0xB800)&0x1FFF)]=V;\r
68 }\r
69 \r
70 static DECLFW(M103Write0)\r
71 {\r
72   reg0=V&0xf;\r
73   Sync();\r
74 }\r
75 \r
76 static DECLFW(M103Write1)\r
77 {\r
78   reg1=(V>>3)&1;\r
79   Sync();\r
80 }\r
81 \r
82 static DECLFW(M103Write2)\r
83 {\r
84   reg2=V;\r
85   Sync();\r
86 }\r
87 \r
88 static void M103Power(void)\r
89 {\r
90   reg0=reg1=0; reg2=0;\r
91   Sync();\r
92   SetReadHandler(0x6000,0x7FFF,CartBR);\r
93   SetWriteHandler(0x6000,0x7FFF,M103RamWrite0);\r
94   SetReadHandler(0x8000,0xFFFF,CartBR);\r
95   SetWriteHandler(0xB800,0xD7FF,M103RamWrite1);\r
96   SetWriteHandler(0x8000,0x8FFF,M103Write0);\r
97   SetWriteHandler(0xE000,0xEFFF,M103Write1);\r
98   SetWriteHandler(0xF000,0xFFFF,M103Write2);\r
99 }\r
100 \r
101 static void M103Close(void)\r
102 {\r
103   if(WRAM)\r
104     FCEU_gfree(WRAM);\r
105   WRAM=NULL;\r
106 }\r
107 \r
108 static void StateRestore(int version)\r
109 {\r
110   Sync();\r
111 }\r
112 \r
113 void Mapper103_Init(CartInfo *info)\r
114 {\r
115   info->Power=M103Power;\r
116   info->Close=M103Close;\r
117   GameStateRestore=StateRestore;\r
118 \r
119   WRAMSIZE=16384;\r
120   WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);\r
121   SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);\r
122   AddExState(WRAM, WRAMSIZE, 0, "WRAM");\r
123 \r
124   AddExState(&StateRegs, ~0, 0, 0);\r
125 }\r