-
Notifications
You must be signed in to change notification settings - Fork 20
/
mapper_ef.go
292 lines (246 loc) · 8.16 KB
/
mapper_ef.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
// 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"
"strings"
"github.com/jetsetilly/gopher2600/curated"
"github.com/jetsetilly/gopher2600/hardware/instance"
"github.com/jetsetilly/gopher2600/hardware/memory/cartridge/mapper"
"github.com/jetsetilly/gopher2600/hardware/memory/cpubus"
"github.com/jetsetilly/gopher2600/hardware/memory/memorymap"
)
type ef struct {
instance *instance.Instance
mappingID string
// ef cartridges have 3 banks of 4096 bytes
bankSize int
banks [][]uint8
// rewindable state
state *efState
// superchip signature ID has been detected. we use this to decide whether
// the cartridge RAM should be allocated
//
// the superchip is not added automatically
needsSuperchip bool
}
// returns new ef type, whether a superchip is required, and any errors
func newEF(instance *instance.Instance, data []byte) (mapper.CartMapper, error) {
cart := &ef{
instance: instance,
mappingID: "EF",
bankSize: 4096,
state: newEFstate(),
}
if len(data) != cart.bankSize*cart.NumBanks() {
return nil, curated.Errorf("EF: %v", "wrong number of bytes in the cartridge data")
}
cart.banks = make([][]uint8, cart.NumBanks())
for k := 0; k < cart.NumBanks(); k++ {
cart.banks[k] = make([]uint8, cart.bankSize)
offset := k * cart.bankSize
copy(cart.banks[k], data[offset:offset+cart.bankSize])
}
// EF cartridge is a superchip cartridge if the string EFSC appears in the
// data somewhere
cart.needsSuperchip = strings.Index(string(data), "EFSC") != -1
return cart, nil
}
// MappedBanks implements the mapper.CartMapper interface.
func (cart *ef) MappedBanks() string {
return fmt.Sprintf("Bank: %d", cart.state.bank)
}
// ID implements the mapper.CartMapper interface.
func (cart *ef) ID() string {
return cart.mappingID
}
// Snapshot implements the mapper.CartMapper interface.
func (cart *ef) Snapshot() mapper.CartMapper {
n := *cart
n.state = cart.state.Snapshot()
return &n
}
// Plumb implements the mapper.CartMapper interface.
func (cart *ef) Plumb() {
}
// Reset implements the mapper.CartMapper interface.
func (cart *ef) Reset() {
cart.state.bank = 0 //len(cart.banks) - 1
}
// Read implements the mapper.CartMapper interface.
func (cart *ef) Read(addr uint16, passive bool) (uint8, error) {
cart.bankswitch(addr, passive)
if cart.state.ram != nil {
if addr >= 0x80 && addr <= 0xff {
return cart.state.ram[addr-superchipRAMsize], nil
}
}
return cart.banks[cart.state.bank][addr], nil
}
// Write implements the mapper.CartMapper interface.
func (cart *ef) Write(addr uint16, data uint8, passive bool, poke bool) error {
if cart.bankswitch(addr, passive) {
return nil
}
if cart.state.ram != nil {
if addr <= 0x7f {
cart.state.ram[addr] = data
return nil
}
}
if poke {
cart.banks[cart.state.bank][addr] = data
return nil
}
return curated.Errorf("EF: %v", curated.Errorf(cpubus.AddressError, addr))
}
// bankswitch on hotspot access.
func (cart *ef) bankswitch(addr uint16, passive bool) bool {
if addr >= 0x0fe0 && addr <= 0x0fef {
if passive {
return true
}
cart.state.bank = int(addr & 0x000f)
return true
}
return false
}
// NumBanks implements the mapper.CartMapper interface.
func (cart *ef) NumBanks() int {
return 16
}
// GetBank implements the mapper.CartMapper interface.
func (cart *ef) GetBank(addr uint16) mapper.BankInfo {
// ef cartridges are like atari cartridges in that the entire address
// space points to the selected bank
return mapper.BankInfo{Number: cart.state.bank, IsRAM: addr <= 0x00ff}
}
// Patch implements the mapper.CartMapper interface.
func (cart *ef) Patch(offset int, data uint8) error {
if offset >= cart.bankSize*len(cart.banks) {
return curated.Errorf("EF: %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 *ef) Listen(addr uint16, data uint8) {
// Sometimes, cartridge addresses can be accessed inadvertently. in most
// instances, there are no consequences but in the case of the Superchip,
// the write addresses can be accessed and the RAM data changed. we handle
// that here.
//
// https://atariage.com/forums/topic/329888-indexed-read-page-crossing-and-sc-ram/
if cart.state.ram != nil {
if addr&memorymap.OriginCart == memorymap.OriginCart {
addr &= memorymap.MaskCart
addr ^= memorymap.OriginCart
// EF Superchip is 128 bytes
if addr&0xff80 == 0x0000 {
cart.state.ram[addr&0x7f] = data
}
}
}
}
// Step implements the mapper.CartMapper interface.
func (cart *ef) Step(_ float32) {
}
// IterateBank implements the mapper.CartMapper interface.
func (cart *ef) CopyBanks() []mapper.BankContent {
c := make([]mapper.BankContent, len(cart.banks))
for b := 0; b < len(cart.banks); b++ {
c[b] = mapper.BankContent{Number: b,
Data: cart.banks[b],
Origins: []uint16{memorymap.OriginCart},
}
}
return c
}
// ReadHotspots implements the mapper.CartHotspotsBus interface.
func (cart *ef) ReadHotspots() map[uint16]mapper.CartHotspotInfo {
return map[uint16]mapper.CartHotspotInfo{
0x1fe0: {Symbol: "BANK0", Action: mapper.HotspotBankSwitch},
0x1fe1: {Symbol: "BANK1", Action: mapper.HotspotBankSwitch},
0x1fe2: {Symbol: "BANK2", Action: mapper.HotspotBankSwitch},
0x1fe3: {Symbol: "BANK3", Action: mapper.HotspotBankSwitch},
0x1fe4: {Symbol: "BANK4", Action: mapper.HotspotBankSwitch},
0x1fe5: {Symbol: "BANK5", Action: mapper.HotspotBankSwitch},
0x1fe6: {Symbol: "BANK6", Action: mapper.HotspotBankSwitch},
0x1fe7: {Symbol: "BANK7", Action: mapper.HotspotBankSwitch},
0x1fe8: {Symbol: "BANK8", Action: mapper.HotspotBankSwitch},
0x1fe9: {Symbol: "BANK9", Action: mapper.HotspotBankSwitch},
0x1fea: {Symbol: "BANK10", Action: mapper.HotspotBankSwitch},
0x1feb: {Symbol: "BANK11", Action: mapper.HotspotBankSwitch},
0x1fec: {Symbol: "BANK12", Action: mapper.HotspotBankSwitch},
0x1fed: {Symbol: "BANK13", Action: mapper.HotspotBankSwitch},
0x1fee: {Symbol: "BANK14", Action: mapper.HotspotBankSwitch},
0x1fef: {Symbol: "BANK15", Action: mapper.HotspotBankSwitch},
}
}
// WriteHotspots implements the mapper.CartHotspotsBus interface.
func (cart *ef) WriteHotspots() map[uint16]mapper.CartHotspotInfo {
return cart.ReadHotspots()
}
// AddSuperchip implements the mapper.OptionalSuperchip interface.
func (cart *ef) AddSuperchip() {
if cart.needsSuperchip && cart.state.ram == nil {
cart.mappingID = fmt.Sprintf("%s (SC)", cart.mappingID)
cart.state.ram = make([]uint8, superchipRAMsize)
}
}
// GetRAM implements the mapper.CartRAMBus interface.
func (cart *ef) GetRAM() []mapper.CartRAM {
if cart.state.ram == nil {
return nil
}
r := make([]mapper.CartRAM, 1)
r[0] = mapper.CartRAM{
Label: "Superchip",
Origin: 0x1080,
Data: make([]uint8, len(cart.state.ram)),
Mapped: true,
}
copy(r[0].Data, cart.state.ram)
return r
}
// PutRAM implements the mapper.CartRAMBus interface.
func (cart *ef) PutRAM(_ int, idx int, data uint8) {
if cart.state.ram == nil {
return
}
cart.state.ram[idx] = data
}
// rewindable state for the CBS cartridge.
type efState struct {
// identifies the currently selected bank
bank int
// some atari ROMs support aditional RAM. this is sometimes referred to as
// the superchip. ram is only added when it is detected
ram []uint8
}
func newEFstate() *efState {
return &efState{}
}
// Snapshot implements the mapper.CartMapper interface.
func (s *efState) Snapshot() *efState {
n := *s
if s.ram != nil {
n.ram = make([]uint8, len(s.ram))
copy(n.ram, s.ram)
}
return &n
}