-
Notifications
You must be signed in to change notification settings - Fork 19
/
mapper_commavid.go
215 lines (183 loc) · 5.81 KB
/
mapper_commavid.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
// 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"
"github.com/jetsetilly/gopher2600/environment"
"github.com/jetsetilly/gopher2600/hardware/memory/cartridge/mapper"
"github.com/jetsetilly/gopher2600/hardware/memory/memorymap"
"github.com/jetsetilly/gopher2600/logger"
)
// from http://www.taswegian.com/WoodgrainWizard/tiki-index.php?page=CV
//
// $F000-$F3FF read from RAM
// $F400-$F7FF write to RAM
// $F800-$FFFF ROM
//
// this seems to be the same principle as other simple RAM cartridges, like the
// Atari Superchip or CBS cartridges
//
// from bankswitch_sizes.txt:
//
// cartridges:
// - MagiCard
// - Video Lfe
//
// and the reason why I implemented this format for Gopher2600:
//
// https://forums.atariage.com/topic/342021-new-512-bytes-demo-released-gar-nix/
type commavid struct {
env *environment.Environment
mappingID string
bankSize int
bankData []uint8
// rewindable state
state *commavidState
}
func newCommaVid(env *environment.Environment, data []byte) (mapper.CartMapper, error) {
cart := &commavid{
env: env,
bankSize: 4096,
mappingID: "CV",
state: newCommaVidState(),
}
cart.bankData = make([]uint8, cart.bankSize)
if len(data) < 2048 {
// place undersized binaries at the end of memory
copy(cart.bankData[4096-len(data):], data)
logger.Logf("CV", "placing undersized commavid data at end of cartridge memory")
} else if len(data) == 2048 {
copy(cart.bankData[2048:], data[:2048])
logger.Logf("CV", "placing 2k commavid data at end of cartridge memory")
} else {
return nil, fmt.Errorf("CV: unhandled size for commavid cartridges (%d)", len(data))
}
return cart, nil
}
// MappedBanks implements the mapper.CartMapper interface.
func (cart *commavid) MappedBanks() string {
return fmt.Sprintf("Bank: 0")
}
// ID implements the mapper.CartMapper interface.
func (cart *commavid) ID() string {
return cart.mappingID
}
// Snapshot implements the mapper.CartMapper interface.
func (cart *commavid) Snapshot() mapper.CartMapper {
n := *cart
n.state = cart.state.Snapshot()
return &n
}
// Plumb implements the mapper.CartMapper interface.
func (cart *commavid) Plumb(env *environment.Environment) {
cart.env = env
}
// Reset implements the mapper.CartMapper interface.
func (cart *commavid) Reset() {
// always starting with random state. this is because Video Life, one of
// the few original CommaVid cartridges, expects it for the opening effect
for i := range cart.state.ram {
cart.state.ram[i] = uint8(cart.env.Random.NoRewind(0xff))
}
}
// Access implements the mapper.CartMapper interface.
func (cart *commavid) Access(addr uint16, _ bool) (uint8, uint8, error) {
if addr >= 0x0000 && addr <= 0x03ff {
return cart.state.ram[addr], mapper.CartDrivenPins, nil
}
if addr >= 0x0400 && addr <= 0x07ff {
return 0, 0, nil
}
return cart.bankData[addr], mapper.CartDrivenPins, nil
}
// AccessVolatile implements the mapper.CartMapper interface.
func (cart *commavid) AccessVolatile(addr uint16, data uint8, poke bool) error {
if addr >= 0x0400 && addr <= 0x07ff {
cart.state.ram[addr&0x03ff] = data
return nil
}
if poke {
cart.bankData[addr] = data
return nil
}
return nil
}
// NumBanks implements the mapper.CartMapper interface.
func (cart *commavid) NumBanks() int {
return 1
}
// GetBank implements the mapper.CartMapper interface.
func (cart *commavid) GetBank(addr uint16) mapper.BankInfo {
// commavid cartridges are like atari cartridges in that the entire address
// space points to the selected bank
return mapper.BankInfo{Number: 0, IsRAM: addr <= 0x07ff}
}
// Patch implements the mapper.CartMapper interface.
func (cart *commavid) Patch(offset int, data uint8) error {
if offset >= cart.bankSize {
return fmt.Errorf("CV: patch offset too high (%d)", offset)
}
cart.bankData[offset] = data
return nil
}
// AccessPassive implements the mapper.CartMapper interface.
func (cart *commavid) AccessPassive(addr uint16, data uint8) {
}
// Step implements the mapper.CartMapper interface.
func (cart *commavid) Step(_ float32) {
}
// GetRAM implements the mapper.CartRAMBus interface.
func (cart *commavid) GetRAM() []mapper.CartRAM {
r := make([]mapper.CartRAM, 1)
r[0] = mapper.CartRAM{
Label: "CommaVid",
Origin: 0x0,
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 *commavid) PutRAM(_ int, idx int, data uint8) {
cart.state.ram[idx] = data
}
// IterateBank implements the mapper.CartMapper interface.
func (cart *commavid) CopyBanks() []mapper.BankContent {
c := make([]mapper.BankContent, 1)
c[0] = mapper.BankContent{Number: 0,
Data: cart.bankData,
Origins: []uint16{memorymap.OriginCart},
}
return c
}
// rewindable state for the CBS cartridge.
type commavidState struct {
// commavid cartridges are distinguished for having 1k of onboard ram
ram []uint8
}
func newCommaVidState() *commavidState {
const commavidRAMsize = 1024
return &commavidState{
ram: make([]uint8, commavidRAMsize),
}
}
// Snapshot implements the mapper.CartMapper interface.
func (s *commavidState) Snapshot() *commavidState {
n := *s
n.ram = make([]uint8, len(s.ram))
copy(n.ram, s.ram)
return &n
}