-
Notifications
You must be signed in to change notification settings - Fork 20
/
cartridge_cbs.go
182 lines (146 loc) · 4.21 KB
/
cartridge_cbs.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
// 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/>.
//
// *** NOTE: all historical versions of this file, as found in any
// git repository, are also covered by the licence, even when this
// notice is not present ***
package cartridge
import (
"fmt"
"github.com/jetsetilly/gopher2600/errors"
)
type cbs struct {
formatID string
description string
// cbs cartridges have 3 banks of 4096 bytes
banks [][]uint8
// identifies the currently selected bank
bank int
// superchip contains the cartridge RAM. not sure if it was actually called
// the superchip in CBS cartridges but
superchip []uint8
// ram details
ramInfo []RAMinfo
}
func newCBS(data []byte) (cartMapper, error) {
const bankSize = 4096
cart := &cbs{}
cart.description = "CBS"
cart.formatID = "FA"
cart.banks = make([][]uint8, cart.numBanks())
if len(data) != bankSize*cart.numBanks() {
return nil, errors.New(errors.CartridgeError, fmt.Sprintf("%s: wrong number of bytes in the cartridge file", cart.formatID))
}
for k := 0; k < cart.numBanks(); k++ {
cart.banks[k] = make([]uint8, bankSize)
offset := k * bankSize
copy(cart.banks[k], data[offset:offset+bankSize])
}
// 256 bytes of cartidge ram in all instances
cart.superchip = make([]uint8, 256)
// prepare ram details
cart.ramInfo = make([]RAMinfo, 5)
cart.ramInfo[0] = RAMinfo{
Label: "CBS RAM+",
Active: true,
ReadOrigin: 0x1080,
ReadMemtop: 0x10ff,
WriteOrigin: 0x1000,
WriteMemtop: 0x107f,
}
cart.initialise()
return cart, nil
}
func (cart cbs) String() string {
return fmt.Sprintf("%s [%s] Bank: %d", cart.description, cart.formatID, cart.bank)
}
func (cart cbs) format() string {
return cart.formatID
}
func (cart *cbs) initialise() {
cart.bank = len(cart.banks) - 1
for i := range cart.superchip {
cart.superchip[i] = 0x00
}
}
func (cart *cbs) read(addr uint16) (uint8, error) {
if addr >= 0x0100 && addr <= 0x01ff {
return cart.superchip[addr-0x100], nil
}
data := cart.banks[cart.bank][addr]
if addr == 0x0ff8 {
cart.bank = 0
} else if addr == 0x0ff9 {
cart.bank = 1
} else if addr == 0x0ffa {
cart.bank = 2
}
return data, nil
}
func (cart *cbs) write(addr uint16, data uint8) error {
if addr <= 0x00ff {
cart.superchip[addr] = data
return nil
}
if addr == 0x0ff8 {
cart.bank = 0
} else if addr == 0x0ff9 {
cart.bank = 1
} else if addr == 0x0ffa {
cart.bank = 2
} else {
return errors.New(errors.BusError, addr)
}
return nil
}
func (cart cbs) getBank(addr uint16) int {
// cbs cartridges are like atari cartridges in that the entire address
// space points to the selected bank
return cart.bank
}
func (cart *cbs) setBank(addr uint16, bank int) error {
if bank < 0 || bank > len(cart.banks) {
return errors.New(errors.CartridgeError, fmt.Sprintf("%s: invalid bank [%d]", cart.formatID, bank))
}
cart.bank = bank
return nil
}
func (cart *cbs) saveState() interface{} {
superchip := make([]uint8, len(cart.superchip))
copy(superchip, cart.superchip)
return []interface{}{cart.bank, superchip}
}
func (cart *cbs) restoreState(state interface{}) error {
cart.bank = state.([]interface{})[0].(int)
copy(cart.superchip, state.([]interface{})[1].([]uint8))
return nil
}
func (cart *cbs) numBanks() int {
return 3
}
func (cart *cbs) listen(addr uint16, data uint8) {
}
func (cart *cbs) poke(addr uint16, data uint8) error {
return errors.New(errors.UnpokeableAddress, addr)
}
func (cart *cbs) patch(addr uint16, data uint8) error {
return errors.New(errors.UnpatchableCartType, cart.formatID)
}
func (cart cbs) getRAMinfo() []RAMinfo {
if cart.superchip == nil {
return nil
}
return cart.ramInfo
}