merge mapper code from FCEUX
[fceu.git] / boards / 164.c
CommitLineData
d97315ac 1/* FCE Ultra - NES/Famicom Emulator
2 *
3 * Copyright notice for this file:
386f5371 4 * Copyright (C) 2002 Xodnizel 2006 CaH4e3
d97315ac 5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
386f5371 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d97315ac 19 */
20
21#include "mapinc.h"
22
386f5371 23static uint8 cmd, laststrobe, trigger;
d97315ac 24static uint8 DRegs[8];
d97315ac 25static SFORMAT StateRegs[]=
26{
27 {&cmd, 1, "CMD"},
386f5371 28 {&laststrobe, 1, "STB"},
29 {&trigger, 1, "TRG"},
d97315ac 30 {DRegs, 8, "DREG"},
31 {0}
32};
33
34static void Sync(void)
35{
36 setprg32(0x8000,(DRegs[0]<<4)|(DRegs[1]&0xF));
37}
38
39static void StateRestore(int version)
40{
41 Sync();
42}
43
386f5371 44static DECLFR(ReadLow)
45{
46 switch (A&0x7700)
47 {
48 case 0x5100: return DRegs[2]; break;
49 case 0x5500: if(trigger)
50 return DRegs[2];
51 else
52 return 0;
53 }
54 return 4;
55}
56
d97315ac 57static DECLFW(Write)
58{
59 switch (A&0x7300)
60 {
61 case 0x5100: DRegs[0]=V; Sync(); break;
62 case 0x5000: DRegs[1]=V; Sync(); break;
386f5371 63 case 0x5300: DRegs[2]=V; break;
d97315ac 64 }
65}
66
67static DECLFW(Write2)
68{
386f5371 69 if(A==0x5101)
70 {
71 if(laststrobe&&!V)
72 {
73 trigger^=1;
74 }
75 laststrobe=V;
76 }else if(A==0x5100&&V==6) //damn thoose protected games
77 setprg32(0x8000,3);
78 else
d97315ac 79 switch (A&0x7300)
80 {
81 case 0x5200: DRegs[0]=V; Sync(); break;
386f5371 82 case 0x5000: DRegs[1]=V; Sync(); if(!(DRegs[1]&0x80)&&(scanline<128)) setchr8(0); break;
83 case 0x5300: DRegs[2]=V; break;
d97315ac 84 }
85}
86
87static uint8 WRAM[8192];
88static DECLFR(AWRAM)
89{
90 return(WRAM[A-0x6000]);
91}
92
93static DECLFW(BWRAM)
94{
95 WRAM[A-0x6000]=V;
96}
97
98static void Power(void)
99{
100 memset(DRegs,0,8);
101 DRegs[1]=0xFF;
d97315ac 102 cmd=0;
103 SetReadHandler(0x8000,0xFFFF,CartBR);
386f5371 104 SetWriteHandler(0x4020,0x5FFF,Write);
d97315ac 105 SetReadHandler(0x6000,0x7FFF,AWRAM);
106 SetWriteHandler(0x6000,0x7FFF,BWRAM);
386f5371 107 setchr8(0);
d97315ac 108 Sync();
109}
110
e2d0dd92 111static void M163HB(void)
112{
386f5371 113 if(DRegs[1]&0x80)
114 {
115 if(scanline==239)
116 {
117 setchr4(0x0000,0);
118 setchr4(0x1000,0);
119 }
120 else if(scanline==127)
121 {
122 setchr4(0x0000,1);
123 setchr4(0x1000,1);
124 }
125 }
e2d0dd92 126}
127
d97315ac 128static void Power2(void)
129{
130 memset(DRegs,0,8);
131 DRegs[1]=0xFF;
386f5371 132 laststrobe=1;
d97315ac 133 cmd=0;
134 SetReadHandler(0x8000,0xFFFF,CartBR);
386f5371 135 SetWriteHandler(0x4020,0x5FFF,Write2);
d97315ac 136 SetReadHandler(0x6000,0x7FFF,AWRAM);
137 SetWriteHandler(0x6000,0x7FFF,BWRAM);
386f5371 138 SetReadHandler(0x5000,0x5FFF,ReadLow);
139 setchr8(0);
d97315ac 140 Sync();
141}
142
143void Mapper164_Init(CartInfo *info)
144{
145 info->Power=Power;
146 GameStateRestore=StateRestore;
147 AddExState(&StateRegs, ~0, 0, 0);
386f5371 148 AddExState(WRAM, 8192, 0, "WRAM");
149 info->SaveGame[0]=WRAM;\r
150 info->SaveGameLen[0]=8192;
d97315ac 151}
152
153void Mapper163_Init(CartInfo *info)
154{
155 info->Power=Power2;
e2d0dd92 156 GameHBIRQHook=M163HB;
d97315ac 157 GameStateRestore=StateRestore;
158 AddExState(&StateRegs, ~0, 0, 0);
386f5371 159 AddExState(WRAM, 8192, 0, "WRAM");
d97315ac 160}