-
Notifications
You must be signed in to change notification settings - Fork 4
/
memio.go
102 lines (85 loc) · 1.87 KB
/
memio.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
package z80
import "reflect"
// DumbMemory provides Memory interface as wrapper of []uint8
type DumbMemory []uint8
// Get gets a byte at addr of memory.
func (dm DumbMemory) Get(addr uint16) uint8 {
if int(addr) >= len(dm) {
return 0
}
return dm[addr]
}
// Set sets a byte at addr of memory.
func (dm DumbMemory) Set(addr uint16, value uint8) {
if int(addr) >= len(dm) {
return
}
dm[addr] = value
}
// Put puts "data" block from addr.
func (dm DumbMemory) Put(addr uint16, data ...uint8) DumbMemory {
copy(dm[int(addr):int(addr)+len(data)], data)
return dm
}
var _ Memory = DumbMemory(nil)
// DumbIO provides IO interface as wrapper of []uint8
type DumbIO []uint8
// In gets a byte at addr of IO
func (dio DumbIO) In(addr uint8) uint8 {
if int(addr) >= len(dio) {
return 0
}
return dio[addr]
}
// Out sets a byte at addr of IO
func (dio DumbIO) Out(addr uint8, value uint8) {
if int(addr) >= len(dio) {
return
}
dio[addr] = value
}
var _ IO = DumbIO(nil)
// MapMemory implements Memory interface with a map.
type MapMemory map[uint16]uint8
// Get gets a byte at addr of memory.
func (mm MapMemory) Get(addr uint16) uint8 {
v, ok := mm[addr]
if !ok {
return 0xC7 // RST 0
}
return v
}
// Set sets a byte at addr of memory.
func (mm MapMemory) Set(addr uint16, v uint8) {
mm[addr] = v
}
// Equal checks two MapMemory same or not.
func (mm MapMemory) Equal(a0 interface{}) bool {
a, ok := a0.(MapMemory)
if !ok {
return false
}
return reflect.DeepEqual(mm, a)
}
// Clone creates a clone of this.
func (mm MapMemory) Clone() MapMemory {
cl := MapMemory{}
for k, v := range mm {
cl[k] = v
}
return cl
}
// Clear removes all.
func (mm MapMemory) Clear() {
for k := range mm {
delete(mm, k)
}
}
// Put puts "data" block from addr.
func (mm MapMemory) Put(addr uint16, data ...uint8) MapMemory {
for _, v := range data {
mm[addr] = v
addr++
}
return mm
}