merge mapper code from FCEUX
[fceu.git] / boards / 15.c
1 /* FCE Ultra - NES/Famicom Emulator\r
2  *\r
3  * Copyright notice for this file:\r
4  *  Copyright (C) 2006 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 \r
22 #include "mapinc.h"\r
23 \r
24 static uint16 latchea;\r
25 static uint8 latched;\r
26 static uint8 *WRAM=NULL;\r
27 static uint32 WRAMSIZE;\r
28 static SFORMAT StateRegs[]=\r
29 {\r
30   {&latchea, 2, "AREG"},\r
31   {&latched, 1, "DREG"},\r
32   {0}\r
33 };\r
34 \r
35 static void Sync(void)\r
36 {\r
37   int i;\r
38   setmirror(((latched>>6)&1)^1);\r
39   switch(latchea)\r
40   {\r
41     case 0x8000:\r
42       for(i=0;i<4;i++)\r
43         setprg8(0x8000+(i<<13),(((latched&0x7F)<<1)+i)^(latched>>7));\r
44       break;\r
45     case 0x8002:\r
46       for(i=0;i<4;i++)\r
47         setprg8(0x8000+(i<<13),((latched&0x7F)<<1)+(latched>>7));\r
48       break;\r
49     case 0x8001:\r
50     case 0x8003:\r
51       for(i=0;i<4;i++)\r
52       {\r
53         unsigned int b;\r
54         b=latched&0x7F;\r
55         if(i>=2 && !(latchea&0x2))\r
56           i=0x7F;\r
57         setprg8(0x8000+(i<<13),(i&1)+((b<<1)^(latched>>7)));\r
58       }\r
59       break;\r
60   }\r
61 }\r
62 \r
63 static DECLFW(M15Write)\r
64 {\r
65   latchea=A;\r
66   latched=V;\r
67         printf("%04X = %02X\n",A,V);\r
68   Sync();\r
69 }\r
70 \r
71 static void StateRestore(int version)\r
72 {\r
73   Sync();\r
74 }\r
75 \r
76 static void M15Power(void)\r
77 {\r
78   latchea=0x8000;\r
79   latched=0;\r
80   setchr8(0);\r
81   setprg8r(0x10,0x6000,0);\r
82   SetReadHandler(0x6000,0x7FFF,CartBR);\r
83   SetWriteHandler(0x6000,0x7FFF,CartBW);\r
84   SetWriteHandler(0x8000,0xFFFF,M15Write);\r
85   SetReadHandler(0x8000,0xFFFF,CartBR);\r
86   Sync();\r
87 }\r
88 \r
89 static void M15Reset(void)\r
90 {\r
91   latchea=0x8000;\r
92   latched=0;\r
93   Sync();\r
94 }\r
95 \r
96 static void M15Close(void)\r
97 {\r
98   if(WRAM)\r
99     FCEU_gfree(WRAM);\r
100   WRAM=NULL;\r
101 }\r
102 \r
103 void Mapper15_Init(CartInfo *info)\r
104 {\r
105   info->Power=M15Power;\r
106   info->Reset=M15Reset;\r
107   info->Close=M15Close;\r
108   GameStateRestore=StateRestore;\r
109   WRAMSIZE=8192;\r
110   WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);\r
111   SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);\r
112   if(info->battery)\r
113   {\r
114     info->SaveGame[0]=WRAM;\r
115     info->SaveGameLen[0]=WRAMSIZE;\r
116   }\r
117   AddExState(WRAM, WRAMSIZE, 0, "WRAM");\r
118   AddExState(&StateRegs, ~0, 0, 0);\r
119 }\r
120 \r