-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMC1.cs
215 lines (192 loc) · 7.65 KB
/
MMC1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Emu6502
{
public class MMC1 : Mapper
{
int writeCount = 0;
int shiftReg = 0;
byte[] PrgRomBank0;
byte[] PrgRomBank1;
byte[][] ChrRomBanks;
private int CtrlReg;
private int ChrBank0Reg;
private int ChrBank1Reg;
private int PrgBankReg;
public MMC1(Nes nes)
: base(nes)
{
}
public override void Reset()
{
CtrlReg = 0;
ChrBank0Reg = 0;
ChrBank1Reg = 0;
PrgBankReg = 0;
int num4kBanks = nes.Rom.ChrRomBanks.Length * 2;
ChrRomBanks = new byte[num4kBanks][];
for (int i = 0; i < nes.Rom.ChrRomBanks.Length; ++i)
{
ChrRomBanks[i*2] = new byte[0x1000];
ChrRomBanks[i*2+1] = new byte[0x1000];
for(int j=0; j<0x1000; ++j)
ChrRomBanks[i*2][j] = nes.Rom.ChrRomBanks[i][j];
for (int j = 0; j < 0x1000; ++j)
ChrRomBanks[i * 2 + 1][j] = nes.Rom.ChrRomBanks[i][0x1000 + j];
}
// TODO: Do the right thing here... requires changing PPU :(
if (nes.Rom.ChrRomBanks.Length > 0)
{
/*for (int i = 0; i < 0x1000; ++i)
nes.Ppu.PatternTables[0][i] = nes.Rom.ChrRomBanks[0][i];
for (int i = 0; i < 0x1000; ++i)
nes.Ppu.PatternTables[1][i] = nes.Rom.ChrRomBanks[0][0x1000 + i];*/
nes.Ppu.PatternTables[0] = ChrRomBanks[0];
nes.Ppu.PatternTables[1] = ChrRomBanks[1];
}
PrgRomBank0 = nes.Rom.PrgRomBanks[0];
PrgRomBank1 = nes.Rom.PrgRomBanks[nes.Rom.PrgRomBanks.Length - 1];
CtrlReg = 0xC;
//SetRegister(0, 0xC);
}
public override byte PrgRomRead(int addr)
{
if (addr >= 0x8000 && addr < 0xC000)
return PrgRomBank0[addr & 0x3FFF];
else
return PrgRomBank1[addr & 0x3FFF];
}
private void UpdatePrgBanks()
{
if ((CtrlReg & 0x8) != 0)
{
// Switch 16K at address specified in prgRomLocation bit (0=$C000, 1=$8000)
if ((CtrlReg & 0x4) != 0)
{
/*Console.WriteLine("Switching PRG-ROM banks @ PC=${0:X}", nes.Cpu.PC);
Console.WriteLine(" $8000 -> Bank ${0:X2}", PrgBankReg & 0xF);
Console.WriteLine(" $C000 -> Bank ${0:X2}", nes.Rom.PrgRomBanks.Length - 1);*/
// Switch $8000
PrgRomBank0 = nes.Rom.PrgRomBanks[PrgBankReg & 0xF];
PrgRomBank1 = nes.Rom.PrgRomBanks[nes.Rom.PrgRomBanks.Length - 1];
}
else
{
/*Console.WriteLine("Switching PRG-ROM banks @ PC=${0:X}", nes.Cpu.PC);
Console.WriteLine(" $8000 -> Bank ${0:X2}", 0);
Console.WriteLine(" $C000 -> Bank ${0:X2}", PrgBankReg & 0xF);*/
// Switch $C000
PrgRomBank0 = nes.Rom.PrgRomBanks[0];
PrgRomBank1 = nes.Rom.PrgRomBanks[PrgBankReg & 0xF];
}
}
else
{
nes.RecordEvent("Mapper.MMC1.32KBPrgRomSelect");
// Switch 32 KB at $C000
// TODO
}
}
private void UpdateChrBanks()
{
//nes.RecordEvent("Mapper.MMC1.UpdateChrBanks");
if (ChrRomBanks.Length == 0)
return;
// 2x 4kB mode
if ((CtrlReg & 0x10) != 0)
{
int banknum0 = ChrBank0Reg & 0x1F;
int banknum1 = ChrBank1Reg & 0x1F;
nes.Ppu.PatternTables[0] = ChrRomBanks[banknum0 % ChrRomBanks.Length];
nes.Ppu.PatternTables[1] = ChrRomBanks[banknum1 % ChrRomBanks.Length];
}
// 8 kB mode
else
{
int banknum = ChrBank0Reg & 0x1E;
nes.Ppu.PatternTables[0] = ChrRomBanks[banknum % ChrRomBanks.Length];
nes.Ppu.PatternTables[1] = ChrRomBanks[(banknum + 1) % ChrRomBanks.Length];
}
}
private void SetRegister(int whichReg, int regValue)
{
//Console.WriteLine("Register{1} = ${0:X2}", regValue, whichReg);
regValue &= 0x1F;
// Control
if (whichReg == 0)
{
CtrlReg = regValue;
int mirrorMode = regValue & 0x3;
bool prgRomLocation = (regValue & 0x4) != 0;
bool prgRomMode = (regValue & 0x8) != 0;
bool chrRomMode = (regValue & 0x10) != 0;
MirrorType mirrorType;
if (mirrorMode == 0)
mirrorType = MirrorType.SingleScreenLower;
else if (mirrorMode == 1)
mirrorType = MirrorType.SingleScreenUpper;
else if (mirrorMode == 2)
mirrorType = MirrorType.Vertical;
else
mirrorType = MirrorType.Horizontal;
//mirrorType = MirrorType.FourScreen;
nes.Ppu.SetMirroring(mirrorType);
/*
Console.WriteLine("Control Reg:");
Console.WriteLine(" Mirror={0} ({1})", mirrorMode, mirrorType);
Console.WriteLine(" PRG-ROM Location={0} ({1})", prgRomLocation ? 1 : 0, prgRomLocation ? "Switch $8000" : "Switch $C000");
Console.WriteLine(" PRG-ROM Mode={0} ({1})", prgRomMode ? 1 : 0, prgRomMode ? "Switch 16KB at address from Location bit" : "Switch 32K at $8000, ignore low bit");
Console.WriteLine(" CHR-ROM Mode={0} ({1})", chrRomMode ? 1 : 0, chrRomMode ? "Separate 4K banks" : "Switch 8K at a time");
*/
UpdatePrgBanks();
UpdateChrBanks();
}
// CHR-ROM 0
else if (whichReg == 1)
{
ChrBank0Reg = regValue;
UpdateChrBanks();
nes.RecordEvent("Mapper.MMC1.ChrRom0Select");
}
// CHR-ROM 1
else if (whichReg == 2)
{
ChrBank1Reg = regValue;
UpdateChrBanks();
nes.RecordEvent("Mapper.MMC1.ChrRom1Select");
}
// PRG-ROM
else if (whichReg == 3)
{
PrgBankReg = regValue;
UpdatePrgBanks();
}
}
public override void PrgRomWrite(int addr, byte data)
{
//Console.WriteLine("PRG-ROM write: ${0:X4} = ${1:X2}", addr,data);
int whichReg = (addr & 0x7fff) >> 13;
if ((data & 0x80) != 0)
{
//CtrlReg |= 0x0C;
SetRegister(0, CtrlReg | 0x0C);
writeCount = 0;
shiftReg = 0;
}
else
{
if ((data & 0x1) != 0)
shiftReg |= data << writeCount;
++writeCount;
if (writeCount == 5)
{
SetRegister(whichReg, shiftReg);
writeCount = 0;
shiftReg = 0;
}
}
}
}
}