forked from scottferg/Fergulator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mmc3.go
336 lines (285 loc) · 6.35 KB
/
mmc3.go
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package main
import (
"fmt"
)
const (
ChrBank2k0000 = 0
ChrBank2k0800 = 1
ChrBank1k1000 = 2
ChrBank1k1400 = 3
ChrBank1k1800 = 4
ChrBank1k1C00 = 5
PrgBank8k8000 = 6
PrgBank8kA000 = 7
PrgBankSwapModeLow = 0
PrgBankSwapModeHigh = 1
ChrA12InversionModeLow = 0
ChrA12InversionModeHigh = 1
RegisterBankSelect = iota
RegisterBankData
RegisterMirroring
RegisterPrgRamProtect
RegisterIrqLatch
RegisterIrqReload
RegisterIrqDisable
RegisterIrqEnable
)
type Mmc3 struct {
RomBanks [][]Word
VromBanks [][]Word
PrgBankCount int
ChrRomCount int
Battery bool
Data []byte
BankSelection int
PrgBankMode int
ChrA12Inversion int
AddressChanged bool
IrqEnabled bool
IrqLatchValue Word
IrqCounter Word
IrqReset bool
IrqResetVbl bool
RamProtectDest [16]int
}
func NewMmc3(r *Rom) *Mmc3 {
m := &Mmc3{
RomBanks: r.RomBanks,
VromBanks: r.VromBanks,
PrgBankCount: r.PrgBankCount,
ChrRomCount: r.ChrRomCount,
Battery: r.Battery,
Data: r.Data,
}
// This just needs to be non-zero and not a 1
// so that it can be caught when it's changed
m.PrgBankMode = 10
m.LoadRom()
return m
}
func (m *Mmc3) LoadRom() {
// The PRG banks are 8192 bytes in size, half the size of an
// iNES PRG bank. If your emulator or copier handles PRG data
// in 16384 byte chunks, you can think of the lower bit as
// selecting the first or second half of the bank
//
// http://forums.nesdev.com/viewtopic.php?p=38182#p38182
// Write hardwired PRG banks (0xC000 and 0xE000)
// Second to last bank
m.Write8kRamBank(0, 0xC000)
// Last bank
m.Write8kRamBank(((len(m.RomBanks)-1)*2)+1, 0xE000)
// Write swappable PRG banks (0x8000 and 0xA000)
WriteRamBank(m.RomBanks, 0, 0x8000, Size16k)
}
func (m *Mmc3) BatteryBacked() bool {
return m.Battery
}
func (m *Mmc3) Write(v Word, a int) {
switch m.RegisterNumber(a) {
case RegisterBankSelect:
m.BankSelect(int(v))
case RegisterBankData:
m.BankData(int(v))
case RegisterMirroring:
m.SetMirroring(int(v))
case RegisterPrgRamProtect:
m.RamProtection(int(v))
case RegisterIrqLatch:
m.IrqLatch(int(v))
case RegisterIrqReload:
m.IrqReload(int(v))
case RegisterIrqDisable:
m.IrqDisable(int(v))
case RegisterIrqEnable:
m.IrqEnable(int(v))
}
}
func (m *Mmc3) RegisterNumber(a int) int {
switch {
case a >= 0x8000 && a <= 0x9FFF:
if a%2 == 0 {
return RegisterBankSelect
} else {
return RegisterBankData
}
case a >= 0xA000 && a <= 0xBFFF:
if a%2 == 0 {
return RegisterMirroring
} else {
return RegisterPrgRamProtect
}
case a >= 0xC000 && a <= 0xDFFF:
if a%2 == 0 {
return RegisterIrqLatch
} else {
return RegisterIrqReload
}
}
if a%2 == 0 {
return RegisterIrqDisable
}
return RegisterIrqEnable
}
func (m *Mmc3) BankSelect(v int) {
// Next bank register to update
m.BankSelection = v & 0x7
address := (v >> 6) & 0x1
if m.PrgBankMode != address {
m.AddressChanged = true
}
m.PrgBankMode = address
m.ChrA12Inversion = (v >> 7) & 0x1
}
func (m *Mmc3) BankData(v int) {
loadHardBanks := func() {
if m.AddressChanged {
if m.PrgBankMode == PrgBankSwapModeLow {
m.Write8kRamBank((len(m.RomBanks)-1)*2, 0xC000)
} else {
m.Write8kRamBank((len(m.RomBanks)-1)*2, 0x8000)
}
m.AddressChanged = false
}
}
switch m.BankSelection {
case ChrBank2k0000:
if m.ChrRomCount == 0 {
break
}
if m.ChrA12Inversion == ChrA12InversionModeLow {
m.Write1kVramBank(v, 0x0000)
m.Write1kVramBank(v+1, 0x0400)
} else {
m.Write1kVramBank(v, 0x1000)
m.Write1kVramBank(v+1, 0x1400)
}
case ChrBank2k0800:
if m.ChrRomCount == 0 {
break
}
if m.ChrA12Inversion == ChrA12InversionModeLow {
m.Write1kVramBank(v, 0x0800)
m.Write1kVramBank(v+1, 0x0C00)
} else {
m.Write1kVramBank(v, 0x1800)
m.Write1kVramBank(v+1, 0x1C00)
}
case ChrBank1k1000:
if m.ChrRomCount == 0 {
break
}
if m.ChrA12Inversion == ChrA12InversionModeLow {
m.Write1kVramBank(v, 0x1000)
} else {
m.Write1kVramBank(v, 0x0000)
}
case ChrBank1k1400:
if m.ChrRomCount == 0 {
break
}
if m.ChrA12Inversion == ChrA12InversionModeLow {
m.Write1kVramBank(v, 0x1400)
} else {
m.Write1kVramBank(v, 0x0400)
}
case ChrBank1k1800:
if m.ChrRomCount == 0 {
break
}
if m.ChrA12Inversion == ChrA12InversionModeLow {
m.Write1kVramBank(v, 0x1800)
} else {
m.Write1kVramBank(v, 0x0800)
}
case ChrBank1k1C00:
if m.ChrRomCount == 0 {
break
}
if m.ChrA12Inversion == ChrA12InversionModeLow {
m.Write1kVramBank(v, 0x1C00)
} else {
m.Write1kVramBank(v, 0x0C00)
}
case PrgBank8k8000:
loadHardBanks()
if m.PrgBankMode == PrgBankSwapModeLow {
m.Write8kRamBank(v, 0x8000)
} else {
m.Write8kRamBank(v, 0xC000)
}
case PrgBank8kA000:
m.Write8kRamBank(v, 0xA000)
loadHardBanks()
}
}
func (m *Mmc3) SetMirroring(v int) {
switch v & 0x1 {
case 0x0:
ppu.Nametables.SetMirroring(MirroringVertical)
case 0x1:
ppu.Nametables.SetMirroring(MirroringHorizontal)
}
}
func (m *Mmc3) RamProtection(v int) {
// TODO: WhAT IS THIS I DON'T EVEN
fmt.Println("RamProtection register")
}
func (m *Mmc3) IrqLatch(v int) {
// $C000
m.IrqLatchValue = Word(v)
}
func (m *Mmc3) IrqReload(v int) {
// $C001
m.IrqCounter |= 0x80
if ppu.Scanline < 240 {
m.IrqReset = true
} else {
m.IrqResetVbl = true
m.IrqReset = false
}
}
func (m *Mmc3) IrqDisable(v int) {
// $E000
m.IrqEnabled = false
m.IrqCounter = m.IrqLatchValue
}
func (m *Mmc3) IrqEnable(v int) {
// $E001
m.IrqEnabled = true
}
func (m *Mmc3) Write8kRamBank(bank, dest int) {
if m.RamProtectDest[dest>>12] == bank {
return
}
m.RamProtectDest[dest>>12] = bank
b := (bank >> 1) % len(m.RomBanks)
offset := (bank % 2) * 0x2000
WriteOffsetRamBank(m.RomBanks, b, dest, Size8k, offset)
}
func (m *Mmc3) Write1kVramBank(bank, dest int) {
b := (bank >> 2) % len(m.VromBanks)
offset := (bank % 4) * 0x400
WriteOffsetVramBank(m.VromBanks, b, dest, Size1k, offset)
}
func (m *Mmc3) Hook() {
// A12 Rising Edge
if (ppu.Scanline > -1 && ppu.Scanline < 240) && (ppu.ShowBackground || ppu.ShowSprites) {
if m.IrqResetVbl {
m.IrqCounter = m.IrqLatchValue
m.IrqResetVbl = false
}
if m.IrqReset {
m.IrqCounter = m.IrqLatchValue
m.IrqReset = false
} else if m.IrqCounter > 0 {
m.IrqCounter--
}
}
if m.IrqCounter == 0 {
if m.IrqEnabled {
cpu.RequestInterrupt(InterruptIrq)
}
m.IrqReset = true
}
}