merge mapper code from FCEUX
[fceu.git] / boards / sc-127.c
1 /* FCE Ultra - NES/Famicom Emulator\r
2  *\r
3  * Copyright notice for this file:\r
4  *  Copyright (C) 2009 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  * Wario Land II (Kirby hack)\r
21  */\r
22 \r
23 #include "mapinc.h"\r
24 \r
25 static uint8 reg[8], chr[8];\r
26 static uint8 *WRAM=NULL;\r
27 static uint32 WRAMSIZE;\r
28 static uint16 IRQCount, IRQa;\r
29 \r
30 static SFORMAT StateRegs[]=\r
31 {\r
32   {reg, 8, "REGS"},\r
33   {chr, 8, "CHRS"},\r
34   {&IRQCount, 16, "IRQc"},\r
35   {&IRQa, 16, "IRQa"},\r
36   {0}\r
37 };\r
38 \r
39 static void Sync(void)\r
40 {\r
41   int i;\r
42   setprg8(0x8000,reg[0]);\r
43   setprg8(0xA000,reg[1]);\r
44   setprg8(0xC000,reg[2]);\r
45   for(i=0; i<8; i++)\r
46     setchr1(i << 10,chr[i]);\r
47   setmirror(reg[3]^1);\r
48 }\r
49 \r
50 static DECLFW(UNLSC127Write)\r
51 {\r
52   switch(A)\r
53   {\r
54     case 0x8000: reg[0] = V; break; \r
55     case 0x8001: reg[1] = V; break;\r
56     case 0x8002: reg[2] = V; break;\r
57     case 0x9000: chr[0] = V; break;\r
58     case 0x9001: chr[1] = V; break;\r
59     case 0x9002: chr[2] = V; break;\r
60     case 0x9003: chr[3] = V; break;\r
61     case 0x9004: chr[4] = V; break;\r
62     case 0x9005: chr[5] = V; break;\r
63     case 0x9006: chr[6] = V; break;\r
64     case 0x9007: chr[7] = V; break;\r
65     case 0xC002: IRQa=0; X6502_IRQEnd(FCEU_IQEXT); break;\r
66     case 0xC005: IRQCount=V; break;\r
67     case 0xC003: IRQa=1; break;\r
68     case 0xD001: reg[3] = V; break;\r
69   }\r
70   Sync();\r
71 }\r
72 \r
73 static void UNLSC127Power(void)\r
74 {\r
75   Sync();\r
76   setprg8r(0x10,0x6000,0);\r
77   setprg8(0xE000,~0);\r
78   SetReadHandler(0x6000,0x7fff,CartBR);\r
79   SetWriteHandler(0x6000,0x7fff,CartBW);\r
80   SetReadHandler(0x8000,0xFFFF,CartBR);\r
81   SetWriteHandler(0x8000,0xFFFF,UNLSC127Write);\r
82 }\r
83 \r
84 static void UNLSC127IRQ(void)\r
85 {\r
86  if(IRQa)\r
87  {\r
88    IRQCount--;\r
89    if(IRQCount==0)\r
90    {\r
91     X6502_IRQBegin(FCEU_IQEXT);\r
92     IRQa=0;\r
93    }\r
94  }\r
95 }\r
96 \r
97 static void UNLSC127Reset(void)\r
98 {\r
99 }\r
100 \r
101 static void UNLSC127Close(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 UNLSC127_Init(CartInfo *info)\r
114 {\r
115   info->Reset=UNLSC127Reset;\r
116   info->Power=UNLSC127Power;\r
117   info->Close=UNLSC127Close;\r
118   GameHBIRQHook=UNLSC127IRQ;\r
119   GameStateRestore=StateRestore;\r
120   WRAMSIZE=8192;\r
121   WRAM=(uint8*)FCEU_gmalloc(WRAMSIZE);\r
122   SetupCartPRGMapping(0x10,WRAM,WRAMSIZE,1);\r
123   AddExState(WRAM, WRAMSIZE, 0, "WRAM");\r
124   AddExState(&StateRegs, ~0, 0, 0);\r
125 }\r