19e1b8dcdb20bd19e654823add3b333d43483a63
[fceu.git] / boards / 178.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 reg[3];\r
24 static uint8 *WRAM=NULL;\r
25 static uint32 WRAMSIZE;\r
26 \r
27 static SFORMAT StateRegs[]=\r
28 {\r
29   {reg, 3, "REGS"},\r
30   {0}\r
31 };\r
32 \r
33 static void Sync(void)\r
34 {\r
35   setmirror(reg[0]);\r
36   setprg8r(0x10,0x6000,0);\r
37   setchr8(0);\r
38   setprg32(0x8000,(reg[1]+reg[2]));\r
39 }\r
40 \r
41 static DECLFW(M178Write0)\r
42 {\r
43   reg[0]=(V&1)^1;\r
44   Sync();\r
45 }\r
46 \r
47 static DECLFW(M178Write1)\r
48 {\r
49   reg[1]=(V>>1)&0xf;\r
50   Sync();\r
51 }\r
52 \r
53 static DECLFW(M178Write2)\r
54 {\r
55   reg[2]=(V<<2);\r
56   Sync();\r
57 }\r
58 \r
59 static void M178Power(void)\r
60 {\r
61   reg[0]=1; reg[1]=0; reg[2]=0;\r
62   Sync();\r
63   SetReadHandler(0x6000,0x7FFF,CartBR);\r
64   SetWriteHandler(0x6000,0x7FFF,CartBW);\r
65   SetReadHandler(0x8000,0xFFFF,CartBR);\r
66   SetWriteHandler(0x4800,0x4800,M178Write0);\r
67   SetWriteHandler(0x4801,0x4801,M178Write1);\r
68   SetWriteHandler(0x4802,0x4802,M178Write2);\r
69 }\r
70 \r
71 static void M178Close(void)\r
72 {\r
73   if(WRAM)\r
74     FCEU_gfree(WRAM);\r
75   WRAM=NULL;\r
76 }\r
77 \r
78 \r
79 static void StateRestore(int version)\r
80 {\r
81   Sync();\r
82 }\r
83 \r
84 void Mapper178_Init(CartInfo *info)\r
85 {\r
86   info->Power=M178Power;\r
87   info->Close=M178Close;\r
88   GameStateRestore=StateRestore;\r
89 \r
90   WRAMSIZE=8192;\r
91   WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);\r
92   SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);\r
93   if(info->battery)\r
94   {\r
95     info->SaveGame[0]=WRAM;\r
96     info->SaveGameLen[0]=WRAMSIZE;\r
97   }\r
98   AddExState(WRAM, WRAMSIZE, 0, "WRAM");\r
99 \r
100   AddExState(&StateRegs, ~0, 0, 0);\r
101 }\r