-
Notifications
You must be signed in to change notification settings - Fork 20
/
mapper_3e.go
311 lines (260 loc) · 7.81 KB
/
mapper_3e.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
// This file is part of Gopher2600.
//
// Gopher2600 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Gopher2600 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
package cartridge
import (
"fmt"
"math/rand"
"strings"
"github.com/jetsetilly/gopher2600/curated"
"github.com/jetsetilly/gopher2600/hardware/memory/bus"
"github.com/jetsetilly/gopher2600/hardware/memory/cartridge/mapper"
"github.com/jetsetilly/gopher2600/hardware/memory/memorymap"
)
type m3e struct {
mappingID string
description string
bankSize int
banks [][]uint8
// rewindable state
state *m3eState
}
// cartridges:
// - Sokoboo
func new3e(data []byte) (mapper.CartMapper, error) {
cart := &m3e{
mappingID: "3E",
description: "m3e",
bankSize: 2048,
state: newM3eState(),
}
if len(data)%cart.bankSize != 0 {
return nil, curated.Errorf("3E: %v", "wrong number bytes in the cartridge data")
}
numBanks := len(data) / cart.bankSize
cart.banks = make([][]uint8, numBanks)
// partition binary
for k := 0; k < numBanks; k++ {
cart.banks[k] = make([]uint8, cart.bankSize)
offset := k * cart.bankSize
copy(cart.banks[k], data[offset:offset+cart.bankSize])
}
return cart, nil
}
func (cart *m3e) String() string {
s := strings.Builder{}
s.WriteString(fmt.Sprintf("%s segments: ", cart.mappingID))
for i := range cart.state.segment {
s.WriteString(fmt.Sprintf("%d", cart.state.segment[i]))
if cart.state.segmentIsRAM[i] {
s.WriteString("R ")
} else {
s.WriteString(" ")
}
}
return s.String()
}
// ID implements the mapper.CartMapper interface.
func (cart *m3e) ID() string {
return cart.mappingID
}
// Snapshot implements the mapper.CartMapper interface.
func (cart *m3e) Snapshot() mapper.CartMapper {
n := *cart
n.state = cart.state.Snapshot()
return &n
}
// Plumb implements the mapper.CartMapper interface.
func (cart *m3e) Plumb() {
}
// Reset implements the mapper.CartMapper interface.
func (cart *m3e) Reset(randSrc *rand.Rand) {
for b := range cart.state.ram {
for i := range cart.state.ram[b] {
if randSrc != nil {
cart.state.ram[b][i] = uint8(randSrc.Intn(0xff))
} else {
cart.state.ram[b][i] = 0
}
}
}
// the last segment always points to the last bank
cart.state.segment[0] = cart.NumBanks() - 2
cart.state.segment[1] = cart.NumBanks() - 1
}
// Read implements the mapper.CartMapper interface.
func (cart *m3e) Read(addr uint16, _ bool) (uint8, error) {
var segment int
if addr >= 0x0000 && addr <= 0x07ff {
segment = 0
} else if addr >= 0x0800 && addr <= 0x0fff {
segment = 1
}
var data uint8
if cart.state.segmentIsRAM[segment] {
data = cart.state.ram[cart.state.segment[segment]][addr&0x03ff]
} else {
bank := cart.state.segment[segment]
if bank < len(cart.banks) {
data = cart.banks[bank][addr&0x07ff]
}
}
return data, nil
}
// Write implements the mapper.CartMapper interface.
func (cart *m3e) Write(addr uint16, data uint8, passive bool, poke bool) error {
if passive {
return nil
}
var segment int
if addr >= 0x0000 && addr <= 0x07ff {
segment = 0
} else if addr >= 0x0c00 && addr <= 0x0fff {
segment = 3
}
if cart.state.segmentIsRAM[segment] {
cart.state.ram[cart.state.segment[segment]][addr&0x03ff] = data
return nil
} else if poke {
cart.banks[cart.state.segment[segment]][addr&0x07ff] = data
return nil
}
return curated.Errorf("3E: %v", curated.Errorf(bus.AddressError, addr))
}
// NumBanks implements the mapper.CartMapper interface.
func (cart *m3e) NumBanks() int {
return len(cart.banks)
}
// GetBank implements the mapper.CartMapper interface.
func (cart *m3e) GetBank(addr uint16) mapper.BankInfo {
if addr >= 0x0000 && addr <= 0x07ff {
return mapper.BankInfo{Number: cart.state.segment[0], IsRAM: false, Segment: 0}
}
return mapper.BankInfo{Number: cart.state.segment[1], IsRAM: false, Segment: 1}
}
// Patch implements the mapper.CartMapper interface.
func (cart *m3e) Patch(offset int, data uint8) error {
if offset >= cart.bankSize*len(cart.banks) {
return curated.Errorf("3E: %v", fmt.Errorf("patch offset too high (%v)", offset))
}
bank := offset / cart.bankSize
offset %= cart.bankSize
cart.banks[bank][offset] = data
return nil
}
// Listen implements the mapper.CartMapper interface.
func (cart *m3e) Listen(addr uint16, data uint8) {
// mapper 3e is a derivative of tigervision and so uses the same Listen()
// mechanism. see the tigervision commentary for details
// bankswitch on hotspot access
if addr == 0x3f {
segment := data >> 6
bank := data & 0x3f
cart.state.segment[segment] = int(bank)
cart.state.segmentIsRAM[segment] = false
} else if addr == 0x3e {
segment := data >> 6
bank := data & 0x3f
cart.state.segment[segment] = int(bank)
cart.state.segmentIsRAM[segment] = true
}
}
// Step implements the mapper.CartMapper interface.
func (cart *m3e) Step(_ float32) {
}
// GetRAM implements the mapper.CartRAMBus interface.
func (cart *m3e) GetRAM() []mapper.CartRAM {
r := make([]mapper.CartRAM, len(cart.state.ram))
for i := range cart.state.ram {
mapped := false
origin := uint16(0x0000)
for s := range cart.state.segment {
mapped = cart.state.segment[s] == i && cart.state.segmentIsRAM[s]
if mapped {
switch s {
case 0:
origin = uint16(0x1000)
case 1:
origin = uint16(0x1800)
}
break // for loop
}
}
r[i] = mapper.CartRAM{
Label: fmt.Sprintf("%d", i),
Origin: origin,
Data: make([]uint8, len(cart.state.ram[i])),
Mapped: mapped,
}
copy(r[i].Data, cart.state.ram[i])
}
return r
}
// PutRAM implements the mapper.CartRAMBus interface.
func (cart *m3e) PutRAM(bank int, idx int, data uint8) {
cart.state.ram[bank][idx] = data
}
// IterateBank implements the mapper.CartMapper interface.
func (cart *m3e) CopyBanks() []mapper.BankContent {
c := make([]mapper.BankContent, len(cart.banks))
for b := 0; b < len(cart.banks)-1; b++ {
c[b] = mapper.BankContent{Number: b,
Data: cart.banks[b],
Origins: []uint16{memorymap.OriginCart},
}
}
// always points to the last segment
b := len(cart.banks) - 1
c[b] = mapper.BankContent{Number: b,
Data: cart.banks[b],
Origins: []uint16{memorymap.OriginCart + uint16(cart.bankSize)},
}
return c
}
// rewindable state for the 3e cartridge.
type m3eState struct {
// 32 is the maximum number of banks possible under the 3e scheme
ram [32][]uint8
// 3e cartridges divide memory into two 2k segments
// o the last segment always points to the last bank
// o the first segment can point to any of the other three
//
// the bank pointed to by the first segment is changed through the listen()
// function (part of the implementation of the mapper.CartMapper interface).
segment [2]int
// in the 3e format only the first segment can contain RAM but for
// simplicity we keep track of both segments
segmentIsRAM [2]bool
}
func newM3eState() *m3eState {
s := &m3eState{}
// a ram bank is half the size of the available bank size. this is because
// of how ram is accessed - half the addresses are for reading and the
// other half are for writing.
const ramSize = 1024
// allocate ram
for k := 0; k < len(s.ram); k++ {
s.ram[k] = make([]uint8, ramSize)
}
return s
}
// Snapshot implements the mapper.CartMapper interface.
func (s *m3eState) Snapshot() *m3eState {
n := *s
for k := 0; k < len(s.ram); k++ {
n.ram[k] = make([]uint8, len(s.ram[k]))
copy(n.ram[k], s.ram[k])
}
return &n
}