merge mapper code from FCEUX
[fceu.git] / boards / 106.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[16], IRQa;\r
24 static uint32 IRQCount;\r
25 static uint8 *WRAM=NULL;\r
26 static uint32 WRAMSIZE;\r
27 \r
28 static SFORMAT StateRegs[]=\r
29 {\r
30   {&IRQa, 1, "IRQA"},\r
31   {&IRQCount, 4, "IRQCOUNT"},\r
32   {reg, 16, "REGS"},\r
33   {0}\r
34 };\r
35 \r
36 static void Sync(void)\r
37 {\r
38   setchr1(0x0000,reg[0]&0xfe);\r
39   setchr1(0x0400,reg[1]|1);\r
40   setchr1(0x0800,reg[2]&0xfe);\r
41   setchr1(0x0c00,reg[3]|1);\r
42   setchr1(0x1000,reg[4]);\r
43   setchr1(0x1400,reg[5]);\r
44   setchr1(0x1800,reg[6]);\r
45   setchr1(0x1c00,reg[7]);\r
46   setprg8r(0x10,0x6000,0);\r
47   setprg8(0x8000,(reg[0x8]&0xf)|0x10);\r
48   setprg8(0xA000,(reg[0x9]&0x1f));\r
49   setprg8(0xC000,(reg[0xa]&0x1f));\r
50   setprg8(0xE000,(reg[0xb]&0xf)|0x10);\r
51   setmirror((reg[0xc]&1)^1);\r
52 }\r
53 \r
54 static DECLFW(M106Write)\r
55 {\r
56   A&=0xF;\r
57   switch(A)\r
58   {\r
59     case 0xD: IRQa=0; IRQCount=0; X6502_IRQEnd(FCEU_IQEXT); break;\r
60     case 0xE: IRQCount=(IRQCount&0xFF00)|V; break;\r
61     case 0xF: IRQCount=(IRQCount&0x00FF)|(V<<8); IRQa=1; break;\r
62     default: reg[A]=V; Sync(); break;\r
63   }\r
64 }\r
65 \r
66 static void M106Power(void)\r
67 {\r
68   reg[8]=reg[9]=reg[0xa]=reg[0xb]=-1;\r
69   Sync();\r
70   SetReadHandler(0x6000,0x7FFF,CartBR);\r
71   SetReadHandler(0x8000,0xFFFF,CartBR);\r
72   SetWriteHandler(0x6000,0x7FFF,CartBW);\r
73   SetWriteHandler(0x8000,0xFFFF,M106Write);\r
74 }\r
75 \r
76 static void M106Reset(void)\r
77 {\r
78 }\r
79 \r
80 static void M106Close(void)\r
81 {\r
82   if(WRAM)\r
83     FCEU_gfree(WRAM);\r
84   WRAM=NULL;\r
85 }\r
86 \r
87 void M106CpuHook(int a)\r
88 {\r
89   if(IRQa)\r
90   {\r
91     IRQCount+=a;\r
92     if(IRQCount>0x10000)\r
93     {\r
94       X6502_IRQBegin(FCEU_IQEXT);\r
95       IRQa=0;\r
96     }\r
97   }\r
98 }\r
99 \r
100 static void StateRestore(int version)\r
101 {\r
102   Sync();\r
103 }\r
104 \r
105 void Mapper106_Init(CartInfo *info)\r
106 {\r
107   info->Reset=M106Reset;\r
108   info->Power=M106Power;\r
109   info->Close=M106Close;\r
110   MapIRQHook=M106CpuHook;\r
111   GameStateRestore=StateRestore;\r
112 \r
113   WRAMSIZE=8192;\r
114   WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);\r
115   SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);\r
116   AddExState(WRAM, WRAMSIZE, 0, "WRAM");\r
117 \r
118   AddExState(&StateRegs, ~0, 0, 0);\r
119 }\r