-
Notifications
You must be signed in to change notification settings - Fork 19
/
mapper_atari.go
594 lines (501 loc) · 14.3 KB
/
mapper_atari.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// 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/errors"
"github.com/jetsetilly/gopher2600/hardware/memory/bus"
"github.com/jetsetilly/gopher2600/hardware/memory/cartridge/banks"
"github.com/jetsetilly/gopher2600/hardware/memory/memorymap"
)
// from bankswitch_sizes.txt:
//
// 2K:
//
// -These carts are not bankswitched, however the data repeats twice in the
// 4K address space. You'll need to manually double-up these images to 4K
// if you want to put these in say, a 4K cart.
//
// 4K:
//
// -These images are not bankswitched.
//
// 8K:
//
// -F8: This is the 'standard' method to implement 8K carts. There are two
// addresses which select between two unique 4K sections. They are 1FF8
// and 1FF9. Any access to either one of these locations switches banks.
// Accessing 1FF8 switches in the first 4K, and accessing 1FF9 switches in
// the last 4K. Note that you can only access one 4K at a time!
//
// 16K:
//
// -F6: The 'standard' method for implementing 16K of data. It is identical
// to the F8 method above, except there are 4 4K banks. You select which
// 4K bank by accessing 1FF6, 1FF7, 1FF8, and 1FF9.
//
// 32K:
//
// -F4: The 'standard' method for implementing 32K. Only one cart is known
// to use it- Fatal Run. Like the F6 method, however there are 8 4K
// banks instead of 4. You use 1FF4 to 1FFB to select the desired bank.
//
//
// Some carts have extra RAM. The way of doing this for Atari format cartridges
// is with the addition of a "superchip".
//
// Atari's 'Super Chip' is nothing more than a 128-byte RAM chip that maps
// itsself in the first 256 bytes of cart memory. (1000-10FFh) The first 128
// bytes is the write port, while the second 128 bytes is the read port. The
// difference in addresses is because there is no dedicated address line to the
// cart to differentiate between read and write operations.
type atari struct {
mappingID string
description string
// atari formats apart from 2k and 4k are divided into banks. 2k and 4k
// ROMs conceptually have one bank
bankSize int
banks [][]uint8
// 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 (see addSuperchip()
// function)
ram []uint8
}
func (cart atari) String() string {
if len(cart.banks) == 1 {
return fmt.Sprintf("%s [%s]", cart.mappingID, cart.description)
}
return fmt.Sprintf("%s [%s] Bank: %d", cart.mappingID, cart.description, cart.bank)
}
// ID implements the cartMapper interface
func (cart atari) ID() string {
return cart.mappingID
}
// Initialise implements the cartMapper interface
func (cart *atari) Initialise() {
// which bank should be the start bank? this has gone back and forth but
// the current thinking (by me) is that it should be the last bank in the
// cartridge. most cartridges are setup so that it doesn't matter, but at
// least one cartridge will not "boot" if the start bank is anything other
// than the last bank (Hack em Hangly Pac Man)
//
// 29/05/20 - change to the second bank being the default when number of
// banks exceeds 1. bank 0 doesn't work for Stay Frosty
if len(cart.banks) == 1 {
cart.bank = 0
} else {
cart.bank = 1
}
}
// GetBank implements the cartMapper interface
func (cart atari) GetBank(addr uint16) banks.Details {
// because atari bank switching swaps out the entire memory space, every
// address points to whatever the current bank is. compare to parker bros.
// cartridges.
return banks.Details{Number: cart.bank, IsRAM: cart.ram != nil && addr >= 0x80 && addr <= 0xff}
}
// Read implements the cartMapper interface
func (cart *atari) Read(addr uint16, passive bool) (uint8, bool) {
if cart.ram != nil {
if addr >= 0x80 && addr <= 0xff {
return cart.ram[addr-128], true
}
}
return 0, false
}
// Write implements the cartMapper interface
func (cart *atari) Write(addr uint16, data uint8, passive bool, poke bool) error {
if cart.ram != nil {
if addr <= 0x7f {
cart.ram[addr] = data
return nil
}
}
if poke {
cart.banks[cart.bank][addr] = data
return nil
}
return errors.New(errors.MemoryBusError, addr)
}
func (cart *atari) addSuperchip() bool {
// check for cartridge memory:
// - this method of detection simply checks whether the first 256 of each
// bank are empty
// - I've guessed that this is a good method. if there's another one I
// don't know about it.
nullChar := cart.banks[0][0]
for k := 0; k < len(cart.banks); k++ {
for a := 0; a < 256; a++ {
if cart.banks[k][a] != nullChar {
return false
}
}
}
// allocate RAM
cart.ram = make([]uint8, 128)
// clear memory
for i := range cart.ram {
cart.ram[i] = 0x00
}
// update method string
cart.description = fmt.Sprintf("%s +RAM", cart.description)
return true
}
// Patch implements the cartMapper interface
func (cart *atari) Patch(offset int, data uint8) error {
if offset >= cart.bankSize*len(cart.banks) {
return errors.New(errors.CartridgePatchOOB, offset)
}
bank := int(offset) / cart.bankSize
offset = offset % cart.bankSize
cart.banks[bank][offset] = data
return nil
}
// Listen implements the cartMapper interface
func (cart *atari) Listen(_ uint16, _ uint8) {
}
// Step implements the cartMapper interface
func (cart *atari) Step() {
}
// GetRAM implements the bus.CartRAMBus interface
func (cart atari) GetRAM() []bus.CartRAM {
if cart.ram == nil {
return nil
}
r := make([]bus.CartRAM, 1)
r[0] = bus.CartRAM{
Label: "Superchip",
Origin: 0x1080,
Data: make([]uint8, len(cart.ram)),
Mapped: true,
}
copy(r[0].Data, cart.ram)
return r
}
// PutRAM implements the bus.CartRAMBus interface
func (cart *atari) PutRAM(_ int, idx int, data uint8) {
cart.ram[idx] = data
}
// IterateBank implemnts the disassemble interface
func (cart atari) IterateBanks(prev *banks.Content) *banks.Content {
b := prev.Number + 1
if b < len(cart.banks) {
return &banks.Content{Number: b,
Data: cart.banks[b],
Origins: []uint16{
memorymap.OriginCart,
},
}
}
return nil
}
// atari4k is the original and most straightforward format
// o Pitfall
// o River Raid
// o Barnstormer
// o etc.
type atari4k struct {
atari
}
// this is a regular cartridge of 4096 bytes
// o Pitfall
// o Adventure
// o Yars Revenge
// o etc.
func newAtari4k(data []byte) (cartMapper, error) {
cart := &atari4k{}
cart.bankSize = 4096
cart.mappingID = "4k"
cart.description = "atari 4k"
cart.banks = make([][]uint8, 1)
if len(data) != cart.bankSize*cart.NumBanks() {
return nil, errors.New(errors.CartridgeError, fmt.Sprintf("%s: wrong number of bytes in the cartridge file", cart.mappingID))
}
cart.banks[0] = make([]uint8, cart.bankSize)
copy(cart.banks[0], data)
cart.Initialise()
return cart, nil
}
// NumBanks implements the cartMapper interface
func (cart atari4k) NumBanks() int {
return 1
}
// Read implements the cartMapper interface
func (cart *atari4k) Read(addr uint16, passive bool) (uint8, error) {
if data, ok := cart.atari.Read(addr, passive); ok {
return data, nil
}
return cart.banks[0][addr], nil
}
// Write implements the cartMapper interface
func (cart *atari4k) Write(addr uint16, data uint8, passive bool, poke bool) error {
if passive {
return nil
}
return cart.atari.Write(addr, data, passive, poke)
}
// atari2k is the half-size cartridge of 2048 bytes
// o Combat
// o Dragster
// o Outlaw
// o Surround
// o early cartridges
type atari2k struct {
atari
}
func newAtari2k(data []byte) (cartMapper, error) {
cart := &atari2k{}
cart.bankSize = 2048
cart.mappingID = "2k"
cart.description = "atari 2k"
cart.banks = make([][]uint8, 1)
if len(data) != cart.bankSize*cart.NumBanks() {
return nil, errors.New(errors.CartridgeError, fmt.Sprintf("%s: wrong number of bytes in the cartridge file", cart.mappingID))
}
cart.banks[0] = make([]uint8, cart.bankSize)
copy(cart.banks[0], data)
cart.Initialise()
return cart, nil
}
// NumBanks implements the cartMapper interface
func (cart atari2k) NumBanks() int {
return 1
}
// Read implements the cartMapper interface
func (cart *atari2k) Read(addr uint16, passive bool) (uint8, error) {
if data, ok := cart.atari.Read(addr, passive); ok {
return data, nil
}
return cart.banks[0][addr&0x07ff], nil
}
// Write implements the cartMapper interface
func (cart *atari2k) Write(addr uint16, data uint8, passive bool, poke bool) error {
if passive {
return nil
}
return cart.atari.Write(addr, data, passive, poke)
}
// atari8k (F8)
// o ET
// o Krull
// o etc.
type atari8k struct {
atari
}
func newAtari8k(data []uint8) (cartMapper, error) {
cart := &atari8k{}
cart.bankSize = 4096
cart.mappingID = "F8"
cart.description = "atari 8k"
cart.banks = make([][]uint8, cart.NumBanks())
if len(data) != cart.bankSize*cart.NumBanks() {
return nil, errors.New(errors.CartridgeError, fmt.Sprintf("%s: wrong number of bytes in the cartridge file", cart.mappingID))
}
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])
}
cart.Initialise()
return cart, nil
}
// NumBanks implements the cartMapper interface
func (cart atari8k) NumBanks() int {
return 2
}
// Read implements the cartMapper interface
func (cart *atari8k) Read(addr uint16, passive bool) (uint8, error) {
if cart.hotspot(addr, passive) {
return 0, nil
}
if data, ok := cart.atari.Read(addr, passive); ok {
return data, nil
}
data := cart.banks[cart.bank][addr]
return data, nil
}
// Write implements the cartMapper interface
func (cart *atari8k) Write(addr uint16, data uint8, passive bool, poke bool) error {
if passive {
return nil
}
if cart.hotspot(addr, passive) {
return nil
}
return cart.atari.Write(addr, data, passive, poke)
}
// bankswitch on hotspot access
func (cart *atari8k) hotspot(addr uint16, passive bool) bool {
if addr >= 0x0ff8 && addr <= 0x0ff9 {
if passive {
return true
}
if addr == 0x0ff8 {
cart.bank = 0
} else if addr == 0x0ff9 {
cart.bank = 1
}
return true
}
return false
}
// atari16k (F6)
// o Crystal Castle
// o RS Boxing
// o Midnite Magic
// o etc.
type atari16k struct {
atari
}
func newAtari16k(data []byte) (cartMapper, error) {
cart := &atari16k{}
cart.bankSize = 4096
cart.mappingID = "F6"
cart.description = "atari 16k"
cart.banks = make([][]uint8, cart.NumBanks())
if len(data) != cart.bankSize*cart.NumBanks() {
return nil, errors.New(errors.CartridgeError, fmt.Sprintf("%s: wrong number of bytes in the cartridge file", cart.mappingID))
}
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])
}
cart.Initialise()
return cart, nil
}
// NumBanks implements the cartMapper interface
func (cart atari16k) NumBanks() int {
return 4
}
// Read implements the cartMapper interface
func (cart *atari16k) Read(addr uint16, passive bool) (uint8, error) {
if cart.hotspot(addr, passive) {
return 0, nil
}
if data, ok := cart.atari.Read(addr, passive); ok {
return data, nil
}
data := cart.banks[cart.bank][addr]
return data, nil
}
// Write implements the cartMapper interface
func (cart *atari16k) Write(addr uint16, data uint8, passive bool, poke bool) error {
if passive {
return nil
}
if cart.hotspot(addr, passive) {
return nil
}
return cart.atari.Write(addr, data, passive, poke)
}
// bankswitch on hotspot access
func (cart *atari16k) hotspot(addr uint16, passive bool) bool {
if addr >= 0x0ff6 && addr <= 0x0ff9 {
if passive {
return true
}
if addr == 0x0ff6 {
cart.bank = 0
} else if addr == 0x0ff7 {
cart.bank = 1
} else if addr == 0x0ff8 {
cart.bank = 2
} else if addr == 0x0ff9 {
cart.bank = 3
}
return true
}
return false
}
// atari32k (F8)
// o Fatal Run
// o Super Mario Bros.
// o Donkey Kong (homebrew)
// o etc.
type atari32k struct {
atari
}
func newAtari32k(data []byte) (cartMapper, error) {
cart := &atari32k{}
cart.bankSize = 4096
cart.mappingID = "F4"
cart.description = "atari 32k"
cart.banks = make([][]uint8, cart.NumBanks())
if len(data) != cart.bankSize*cart.NumBanks() {
return nil, errors.New(errors.CartridgeError, fmt.Sprintf("%s: wrong number of bytes in the cartridge file", cart.mappingID))
}
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])
}
cart.Initialise()
return cart, nil
}
// NumBanks implements the cartMapper interface
func (cart atari32k) NumBanks() int {
return 8
}
// Read implements the cartMapper interface
func (cart *atari32k) Read(addr uint16, passive bool) (uint8, error) {
if cart.hotspot(addr, passive) {
return 0, nil
}
if data, ok := cart.atari.Read(addr, passive); ok {
return data, nil
}
data := cart.banks[cart.bank][addr]
return data, nil
}
// Write implements the cartMapper interface
func (cart *atari32k) Write(addr uint16, data uint8, passive bool, poke bool) error {
if passive {
return nil
}
if cart.hotspot(addr, passive) {
return nil
}
return cart.atari.Write(addr, data, passive, poke)
}
// bankswitch on hotspot access
func (cart *atari32k) hotspot(addr uint16, passive bool) bool {
if addr >= 0x0ff4 && addr <= 0xffb {
if passive {
return true
}
if addr == 0x0ff4 {
cart.bank = 0
} else if addr == 0x0ff5 {
cart.bank = 1
} else if addr == 0x0ff6 {
cart.bank = 2
} else if addr == 0x0ff7 {
cart.bank = 3
} else if addr == 0x0ff8 {
cart.bank = 4
} else if addr == 0x0ff9 {
cart.bank = 5
} else if addr == 0x0ffa {
cart.bank = 6
} else if addr == 0x0ffb {
cart.bank = 7
}
return true
}
return false
}