-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathioC0Page.go
185 lines (158 loc) · 4.49 KB
/
ioC0Page.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
package izapple2
import (
"fmt"
)
type ioC0Page struct {
softSwitchesR [256]softSwitchR
softSwitchesW [256]softSwitchW
softSwitchesRName [256]string
softSwitchesWName [256]string
softSwitchesData [128]uint8
keyboard KeyboardProvider
speaker SpeakerProvider
paddlesStrobeCycle uint64
joysticks JoysticksProvider
mouse MouseProvider
apple2 *Apple2
traceMask uint16 // A bit for each 16 softswitches
panicMask uint16 // A bit for each 16 softswitches
traceRegistrations bool
}
type softSwitchR func() uint8
type softSwitchW func(value uint8)
// SpeakerProvider provides a speaker implementation
type SpeakerProvider interface {
// Click receives a speaker click. The argument is the CPU cycle when it is generated
Click(cycle uint64)
}
// JoysticksProvider abstracts the joysticks
type JoysticksProvider interface {
ReadButton(i int) bool
ReadPaddle(i int) (uint8, bool)
}
// MouseProvider abstracts the mouse
type MouseProvider interface {
ReadMouse() (x uint16, y uint16, pressed bool)
}
// See https://www.kreativekorp.com/miscpages/a2info/iomemory.shtml
// See https://stason.org/TULARC/pc/apple2/programmer/004-I-d-like-to-do-some-serious-Apple-II-programming-Whe.html
const (
ssOn uint8 = 0x80
ssOff uint8 = 0x00
)
func newIoC0Page(a *Apple2) *ioC0Page {
var io ioC0Page
io.apple2 = a
return &io
}
func (p *ioC0Page) setTrace(trace bool) {
if trace {
p.traceMask = 0xffff
} else {
p.traceMask = 0x0000
}
}
func (p *ioC0Page) traceSlot(slot int) {
p.traceMask |= 1 << (8 + slot)
}
func (p *ioC0Page) panicNotImplementedSlot(slot int) {
p.panicMask |= 1 << (8 + slot)
}
func (p *ioC0Page) setTraceRegistrations(traceRegistrations bool) {
p.traceRegistrations = traceRegistrations
}
func (p *ioC0Page) setPanicNotImplemented(value bool) {
if value {
p.panicMask = 0xffff
} else {
p.panicMask = 0x0000
}
}
func (p *ioC0Page) addSoftSwitchRW(address uint8, ss softSwitchR, name string) {
p.addSoftSwitchR(address, ss, name)
p.addSoftSwitchW(address, func(uint8) {
ss()
}, name)
}
func (p *ioC0Page) addSoftSwitchR(address uint8, ss softSwitchR, name string) {
if p.traceRegistrations {
fmt.Printf("Softswitch registered in $c0%02x for reads as %s\n", address, name)
}
p.softSwitchesR[address] = ss
p.softSwitchesRName[address] = name
}
func (p *ioC0Page) addSoftSwitchW(address uint8, ss softSwitchW, name string) {
if p.traceRegistrations {
fmt.Printf("Softswitch registered in $c0%02x for writes as %s\n", address, name)
}
p.softSwitchesW[address] = ss
p.softSwitchesWName[address] = name
}
func (p *ioC0Page) isSoftSwitchActive(ioFlag uint8) bool {
return (p.softSwitchesData[ioFlag] & ssOn) == ssOn
}
func (p *ioC0Page) setKeyboardProvider(kb KeyboardProvider) {
p.keyboard = kb
}
func (p *ioC0Page) setSpeakerProvider(s SpeakerProvider) {
p.speaker = s
}
func (p *ioC0Page) setJoysticksProvider(j JoysticksProvider) {
p.joysticks = j
}
func (p *ioC0Page) setMouseProvider(m MouseProvider) {
p.mouse = m
}
func (p *ioC0Page) isTraced(address uint16) bool {
ss := address & 0xff
return address != 0xc000 && // Do not trace the spammy keyboard softswitch
(p.traceMask&(1<<(ss>>4))) != 0
}
func (p *ioC0Page) isPanicNotImplemented(address uint16) bool {
ss := address & 0xff
return ss != 0xc068 && // Ignore known IIGS softswitch
(p.panicMask&(1<<(ss>>4))) != 0
}
func (p *ioC0Page) peek(address uint16) uint8 {
pageAddress := uint8(address)
ss := p.softSwitchesR[pageAddress]
if ss == nil {
if p.isTraced(address) {
fmt.Printf("Unknown softswitch on read to $%04x\n", address)
}
if p.isPanicNotImplemented(address) {
panic(fmt.Sprintf("Unknown softswitch on read to $%04x", address))
}
return 0
}
value := ss()
if p.isTraced(address) {
name := p.softSwitchesRName[pageAddress]
fmt.Printf("Softswitch peek on $%04x %v: $%02x\n", address, name, value)
}
return value
}
func (p *ioC0Page) poke(address uint16, value uint8) {
pageAddress := uint8(address)
ss := p.softSwitchesW[pageAddress]
if ss == nil {
if p.isTraced(address) {
fmt.Printf("Unknown softswitch on write $%02x to $%04x\n", value, address)
}
if p.isPanicNotImplemented(address) {
panic(fmt.Sprintf("Unknown softswitch on write to $%04x", address))
}
return
}
if p.isTraced(address) {
name := p.softSwitchesWName[pageAddress]
fmt.Printf("Softswitch poke on $%04x %v with $%02x\n", address, name, value)
}
ss(value)
}
func ssFromBool(value bool) uint8 {
if value {
return ssOn
}
return ssOff
}