-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsmartPortCall.go
186 lines (160 loc) · 4.63 KB
/
smartPortCall.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
package izapple2
import "fmt"
/*
A smartPort device
*/
type smartPortDevice interface {
exec(call *smartPortCall) uint8
}
const (
smartPortCommandStatus = 0
smartPortCommandReadBlock = 1
smartPortCommandWriteBlock = 2
smartPortCommandFormat = 3
smartPortCommandControl = 4
smartPortCommandInit = 5
smartPortCommandOpen = 6
smartPortCommandClose = 7
smartPortCommandRead = 8
smartPortCommandWrite = 9
)
const (
smartPortStatusCodeDevice = 0
smartPortStatusCodeDeviceControlBlock = 1
smartPortStatusCodeNewline = 2
smartPortStatusCodeDeviceInfo = 3
)
const (
smartPortStatusCodeTypeBlock = uint8(1) << 7
smartPortStatusCodeTypeWrite = uint8(1) << 6
smartPortStatusCodeTypeRead = uint8(1) << 5
smartPortStatusCodeTypeOnline = uint8(1) << 4
smartPortStatusCodeTypeFormat = uint8(1) << 3
smartPortStatusCodeTypeProtected = uint8(1) << 2
smartPortStatusCodeTypeInterruping = uint8(1) << 1
smartPortStatusCodeTypeOpen = uint8(1) << 0
)
const (
smartPortNoError = uint8(0)
smartPortBadCommand = uint8(1)
smartPortErrorIO = uint8(0x27)
smartPortErrorNoDevice = uint8(0x28)
smartPortErrorWriteProtected = uint8(0x2b)
)
type smartPortCall struct {
host *CardSmartPort
command uint8
address uint16 // When the params are on the Apple memory
params []uint8 // When the params are built externally as on a ProDOS to SP translation
}
func newSmartPortCall(host *CardSmartPort, command uint8, address uint16) *smartPortCall {
var spc smartPortCall
spc.host = host
spc.command = command
spc.address = address
spc.params = nil
return &spc
}
func newSmartPortCallSynthetic(host *CardSmartPort, command uint8, params []uint8) *smartPortCall {
var spc smartPortCall
spc.host = host
spc.command = command
spc.address = 0xffff
spc.params = params
return &spc
}
func (spc *smartPortCall) unit() uint8 {
return spc.param8(1)
}
func (spc *smartPortCall) statusCode() uint8 {
if spc.command != smartPortCommandStatus {
panic("Status code paremeter requeted for a non status smartPort call")
}
return spc.param8(4)
}
func (spc *smartPortCall) param8(offset uint8) uint8 {
if spc.params == nil {
return spc.host.a.mmu.Peek(spc.address + uint16(offset))
}
if int(offset) >= len(spc.params) {
panic("Synthetised smartpot call out of range")
}
return spc.params[offset]
}
func (spc *smartPortCall) param16(offset uint8) uint16 {
return uint16(spc.param8(offset)) +
uint16(spc.param8(offset+1))<<8
}
func (spc *smartPortCall) param24(offset uint8) uint32 {
return uint32(spc.param8(offset)) +
uint32(spc.param8(offset+1))<<8 +
uint32(spc.param8(offset+2))<<16
}
func (spc *smartPortCall) paramData(offset uint8) []uint8 {
address := uint16(spc.param8(offset)) +
uint16(spc.param8(offset+1))<<8
size := spc.host.a.mmu.peekWord(address)
data := make([]uint8, size)
for i := 0; i < int(size); i++ {
data[i] = spc.host.a.mmu.Peek(address + 2 + uint16(i))
}
return data
}
func (spc *smartPortCall) String() string {
switch spc.command {
case smartPortCommandStatus:
return fmt.Sprintf("STATUS(%v, unit=%v, code=%v)",
spc.command, spc.unit(),
spc.statusCode())
case smartPortCommandReadBlock:
return fmt.Sprintf("READBLOCK(%v, unit=%v, block=%v)",
spc.command, spc.unit(),
spc.param24(4))
case smartPortCommandWriteBlock:
return fmt.Sprintf("WRITEBLOCK(%v, unit=%v, block=%v)",
spc.command, spc.unit(),
spc.param24(4))
case smartPortCommandControl:
return fmt.Sprintf("CONTROL(%v, unit=%v, code=%v)",
spc.command, spc.unit(),
spc.param8(4))
case smartPortCommandInit:
return fmt.Sprintf("INIT(%v, unit=%v)",
spc.command, spc.unit())
case smartPortCommandOpen:
return fmt.Sprintf("OPEN(%v, unit=%v)",
spc.command, spc.unit())
case smartPortCommandClose:
return fmt.Sprintf("CLOSE(%v, unit=%v)",
spc.command, spc.unit())
case smartPortCommandRead:
return fmt.Sprintf("READ(%v, unit=%v, pos=%v, len=%v)",
spc.command, spc.unit(),
spc.param24(6),
spc.param16(4))
case smartPortCommandWrite:
return fmt.Sprintf("WRITE(%v, unit=%v, pos=%v, len=%v)",
spc.command, spc.unit(),
spc.param24(6),
spc.param16(4))
default:
return fmt.Sprintf("UNKNOWN(%v, unit=%v)",
spc.command, spc.unit())
}
}
func smartPortErrorMessage(code uint8) string {
switch code {
case smartPortNoError:
return "SUCCESS"
case smartPortBadCommand:
return "BAD_COMMAND"
case smartPortErrorIO:
return "ERROR_IO"
case smartPortErrorNoDevice:
return "NO_DEVICE"
case smartPortErrorWriteProtected:
return "WRITE_PROTECT_ERROR"
default:
return string(code)
}
}