merge mappers from FCEU-mm
[fceu.git] / boards / 82.c
1 /* FCE Ultra - NES/Famicom Emulator\r
2  *\r
3  * Copyright notice for this file:\r
4  *  Copyright (C) 2012 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19  *\r
20  * Taito X1-017 board, battery backed\r
21  *\r
22  */\r
23 \r
24 #include "mapinc.h"\r
25 \r
26 static uint8 regs[9], ctrl;\r
27 static uint8 *WRAM=NULL;\r
28 static uint32 WRAMSIZE;\r
29 \r
30 static SFORMAT StateRegs[]=\r
31 {\r
32   {regs, 9, "REGS"},\r
33   {&ctrl, 1, "CTRL"},\r
34   {0}\r
35 };\r
36 \r
37 static void Sync(void)\r
38 {\r
39   uint32 swap = ((ctrl & 2) << 11);\r
40   setchr2(0x0000^swap,regs[0]>>1);  \r
41   setchr2(0x0800^swap,regs[1]>>1);  \r
42   setchr1(0x1000^swap,regs[2]);  \r
43   setchr1(0x1400^swap,regs[3]);  \r
44   setchr1(0x1800^swap,regs[4]);  \r
45   setchr1(0x1c00^swap,regs[5]);  \r
46   setprg8r(0x10,0x6000,0);\r
47   setprg8(0x8000,regs[6]);\r
48   setprg8(0xA000,regs[7]);\r
49   setprg8(0xC000,regs[8]);\r
50   setprg8(0xE000,~0);\r
51   setmirror(ctrl & 1);\r
52 }\r
53 \r
54 static DECLFW(M82Write)\r
55 {\r
56   if(A <= 0x7ef5)\r
57     regs[A & 7] = V;\r
58   else\r
59     switch(A)\r
60     {\r
61       case 0x7ef6: ctrl = V & 3; break;\r
62       case 0x7efa: regs[6] = V >> 2; break;\r
63       case 0x7efb: regs[7] = V >> 2; break;\r
64       case 0x7efc: regs[8] = V >> 2; break;\r
65     }\r
66   Sync();\r
67 }\r
68 \r
69 static void M82Power(void)\r
70 {\r
71   Sync();\r
72   SetReadHandler(0x6000,0xffff,CartBR);\r
73   SetWriteHandler(0x6000,0x7fff,CartBW);\r
74   SetWriteHandler(0x7ef0,0x7efc,M82Write);  // external WRAM might end at $73FF\r
75 }\r
76 \r
77 static void M82Close(void)\r
78 {\r
79   if(WRAM)\r
80     FCEU_gfree(WRAM);\r
81   WRAM=NULL;\r
82 }\r
83 \r
84 static void StateRestore(int version)\r
85 {\r
86   Sync();\r
87 }\r
88 \r
89 void Mapper82_Init(CartInfo *info)\r
90 {\r
91   info->Power=M82Power;\r
92   info->Power=M82Close;\r
93 \r
94   WRAMSIZE=8192;\r
95   WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);\r
96   SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);\r
97   AddExState(WRAM, WRAMSIZE, 0, "WRAM");\r
98   if(info->battery)\r
99   {\r
100     info->SaveGame[0]=WRAM;\r
101     info->SaveGameLen[0]=WRAMSIZE;\r
102   }\r
103   GameStateRestore=StateRestore;\r
104   AddExState(&StateRegs, ~0, 0, 0);\r
105 }\r