-
Notifications
You must be signed in to change notification settings - Fork 9
/
memoryRange.go
93 lines (75 loc) · 1.86 KB
/
memoryRange.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
package izapple2
import (
"fmt"
)
type memoryRange struct {
base uint16
data []uint8
name string
}
type memoryRangeROM struct {
memoryRange
pageOffset uint16
pages uint8
}
func newMemoryRange(base uint16, data []uint8, name string) *memoryRange {
var m memoryRange
m.base = base
m.data = data
m.setBase(base)
m.name = name
return &m
}
func newMemoryRangeROM(base uint16, data []uint8, name string) *memoryRangeROM {
return newMemoryRangePagedROM(base, data, name, 1)
}
func newMemoryRangePagedROM(base uint16, data []uint8, name string, pages uint8) *memoryRangeROM {
var m memoryRangeROM
m.base = base
m.data = data
m.name = name
m.pages = pages
return &m
}
func (m *memoryRange) setBase(base uint16) {
m.base = base
}
func (m *memoryRange) peek(address uint16) uint8 {
return m.data[address-m.base]
}
func (m *memoryRange) poke(address uint16, value uint8) {
m.data[address-m.base] = value
}
func (m *memoryRange) subRange(a, b uint16) []uint8 {
return m.data[a-m.base : b-m.base]
}
func (m *memoryRangeROM) setPage(page uint8) {
pageSize := len(m.data) / int(m.pages)
m.pageOffset = uint16(int(page%m.pages) * pageSize)
}
func (m *memoryRangeROM) getPage() uint8 {
pageSize := len(m.data) / int(m.pages)
return uint8(m.pageOffset / uint16(pageSize))
}
func (m *memoryRangeROM) peek(address uint16) uint8 {
pos := address - m.base + m.pageOffset
if pos >= uint16(len(m.data)) {
return uint8(address) // Non existent memory
}
return m.data[pos]
}
func (m *memoryRangeROM) poke(address uint16, value uint8) {
// Ignore
}
//lint:ignore U1000 this is used to write debug code
func identifyMemory(m memoryHandler) string {
ram, ok := m.(*memoryRange)
if ok {
return fmt.Sprintf("RAM 0x%04x %s", ram.base, ram.name)
}
rom, ok := m.(*memoryRangeROM)
if ok {
return fmt.Sprintf("ROM 0x%04x %s", rom.base, rom.name)
}
return ("Unknown memory")
}