-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtraceProDOS.go
386 lines (358 loc) · 10 KB
/
traceProDOS.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
package izapple2
import "fmt"
/*
See:
https://prodos8.com/docs/techref/calls-to-the-mli/
*/
type traceProDOS struct {
a *Apple2
callPending bool // We assume MLI is not reentrant
functionCode uint8
paramsAdddress uint16
returnAddress uint16
deviceDrivers []uint16
}
const (
mliAddress uint16 = 0xbf00
biAddress uint16 = 0xbe03
deviceCountAddress uint16 = 0xbf31 // DEVCNT
deviceListAddress uint16 = 0xbf32
deviceDriverVectors uint16 = 0xbf10 // DEVADR01
deviceDateTimeVector uint16 = 0xbf07 // DATETIME+1
)
func newTraceProDOS() *traceProDOS {
var t traceProDOS
t.deviceDrivers = make([]uint16, 0)
return &t
}
func (t *traceProDOS) connect(a *Apple2) {
t.a = a
}
func (t *traceProDOS) inspect() {
if t.a.dmaActive {
return
}
pc, _ := t.a.cpu.GetPCAndSP()
if pc == mliAddress {
/*
MLI has been called (provided we are running proDOS and the proper page)
Calls to MLI must be:
JSR $BF00
DFB function_code
DW addr_op_parms
*/
if t.callPending {
if t.functionCode == 0x65 {
// QUIT when successful does not return
fmt.Printf("Ok \n")
} else {
fmt.Print("<there was a call pending>\n")
}
}
t.dumpMLICall()
t.refreshDeviceDrives()
t.callPending = true
} else if t.callPending && pc == t.returnAddress {
t.dumpMLIReturn()
t.callPending = false
} else if pc == biAddress {
t.dumpBIExec()
} else if /*t.callPending &&*/ t.isDriverAddress(pc) {
t.dumpDriverCall()
}
}
func (t *traceProDOS) dumpMLICall() {
_, sp := t.a.cpu.GetPCAndSP()
caller := uint16(t.a.mmu.Peek(0x100+uint16(sp+1))) +
uint16(t.a.mmu.Peek(0x100+uint16(sp+2)))<<8 - 2
t.functionCode = t.a.mmu.Peek(caller + 3)
t.paramsAdddress = uint16(t.a.mmu.Peek(caller+4)) + uint16(t.a.mmu.Peek(caller+5))<<8
t.returnAddress = caller + 6
fmt.Printf("MLI call $%02x from $%04x", t.functionCode, caller)
switch t.functionCode {
case 0x40:
fmt.Printf(" ALLOC_INTERRUPT()")
case 0x41:
fmt.Printf(" DEALLOC_INTERRUPT()")
case 0x65:
fmt.Printf(" QUIT()")
case 0x80:
fmt.Printf(" READ_BLOCK(unit=%s, block=$%04x)", parseUnit(t.paramByte(1)), t.paramWord(4))
case 0x81:
fmt.Printf(" WRITE_BLOCK(unit=%s, block=$%04x)", parseUnit(t.paramByte(1)), t.paramWord(4))
case 0x82:
fmt.Printf(" GET_TIME()")
case 0xc0:
fmt.Printf(" CREATE(\"%s\")", t.paramString(1))
case 0xc1:
fmt.Printf(" DESTROY(\"%s\")", t.paramString(1))
case 0xc2:
fmt.Printf(" RENAME(old=\"%s\", new=\"%s\")", t.paramString(1), t.paramString(3))
case 0xc3:
fmt.Printf(" GET_FILE_INFO(\"%s\")", t.paramString(1))
case 0xc4:
fmt.Printf(" SET_FILE_INFO(\"%s\")", t.paramString(1))
case 0xc5:
fmt.Printf(" ONLINE(unit=%s)", parseUnit(t.paramByte(1)))
case 0xc6:
fmt.Printf(" SET_PREFIX(\"%s\")", t.paramString(1))
case 0xc7:
fmt.Printf(" GET_PREFIX()")
case 0xc8:
fmt.Printf(" OPEN(\"%s\")", t.paramString(1))
case 0xc9:
fmt.Printf(" NEWLINE(ref=%v, mask=$%02x, char=$%02x)", t.paramByte(1), t.paramByte(2), t.paramByte(3))
case 0xca:
fmt.Printf(" READ(ref=%v, len=%v)", t.paramByte(1), t.paramWord(4))
case 0xcb:
fmt.Printf(" WRITE(ref=%v, len=%v)", t.paramByte(1), t.paramWord(4))
case 0xcc:
fmt.Printf(" CLOSE(ref=%v)", t.paramByte(1))
case 0xcd:
fmt.Printf(" FLUSH(ref=%v)", t.paramByte(1))
case 0xce:
fmt.Printf(" SET_MARK(ref=%v, pos=%v)", t.paramByte(1), t.paramLen(2))
case 0xcf:
fmt.Printf(" GET_MARK(ref=%v)", t.paramByte(1))
case 0xd1:
fmt.Printf(" GET_EOF(ref=%v)", t.paramByte(1))
case 0xd2:
fmt.Printf(" SET_BUF(ref=%v)", t.paramByte(1))
case 0xd3:
fmt.Printf(" GET_BUF(ref=%v)", t.paramByte(1))
}
fmt.Printf(" => ")
}
func (t *traceProDOS) dumpMLIReturn() {
error, acc := t.a.cpu.GetCarryAndAcc()
if error {
fmt.Printf("error $%02x: %v\n", acc, getMliErrorText(acc))
} else {
switch t.functionCode {
case 0x82: // Get Time
// Globals will be updated
date := uint16(t.a.mmu.Peek(0xbf90)) + uint16(t.a.mmu.Peek(0xbf91))<<8
minute := t.a.mmu.Peek(0xbf92)
hour := t.a.mmu.Peek(0xbf93)
fmt.Printf("%04v-%02v-%02v %02v:%02v\n",
date>>9+1900, (date>>5)&0x0f, date&0x1f, // Review Y2K
hour, minute)
case 0xc5: // Online
dataAddress := t.paramWord(2)
for {
b := t.a.mmu.Peek(dataAddress)
dataAddress++
if b == 0 {
fmt.Printf("\n")
break
}
unit := parseUnit(b)
size := b & 0xf
if size != 0 {
// No error
name := ""
for i := uint8(0); i < size; i++ {
name += string(t.a.mmu.Peek(dataAddress+uint16(i)) & 0x7f)
}
fmt.Printf("%s: \"%s\" ", unit, name)
} else {
err := t.a.mmu.Peek(dataAddress)
fmt.Printf("%s: error $%02x ", unit, err)
}
if t.paramByte(1) != 0 {
fmt.Printf("\n")
break // Only one entry requested
}
dataAddress += 15
}
case 0xc7: // Get prefix
fmt.Printf("\"%v\"\n", t.paramString(1))
case 0xc8: // Open file
fmt.Printf("ref: %v\n", t.paramByte(5))
case 0xca: // Read
fmt.Printf("%v bytes read \n", t.paramWord(6))
case 0xcb: // Write
fmt.Printf("%v bytes written \n", t.paramWord(6))
case 0xcf: // File position
fmt.Printf("%v\n", t.paramLen(2))
case 0xd1: // File size
fmt.Printf("%v bytes\n", t.paramLen(2))
default:
fmt.Printf("Ok\n")
}
}
}
func (t *traceProDOS) dumpBIExec() {
s := ""
for i := uint16(1); i < 256; i++ {
ch := t.a.mmu.Peek(0x200 + i)
if ch == 0 || ch == 0x8d {
break
}
s += string(ch)
}
fmt.Printf("Prodos BI exec: \"%s\".\n", s)
}
var proDosCommandNames = []string{"STATUS", "READ", "WRITE", "FORMAT"}
func (t *traceProDOS) dumpDriverCall() {
pc, _ := t.a.cpu.GetPCAndSP()
command := t.a.mmu.Peek(0x42)
unit := t.a.mmu.Peek(0x43)
address := uint16(t.a.mmu.Peek(0x44)) + uint16(t.a.mmu.Peek(0x45))<<8
block := uint16(t.a.mmu.Peek(0x46)) + uint16(t.a.mmu.Peek(0x47))<<8
commandName := "OTHER"
if int(command) < len(proDosCommandNames) {
commandName = proDosCommandNames[command]
}
fmt.Printf("\n Prodos driver $%04x command %02x-%s on unit $%x, block %v to/from $%04x ==> ", pc, command, commandName, unit, block, address)
}
//lint:ignore U1000 unused but stays as reference
func (t *traceProDOS) dumpDevices() {
// Active disk devices
// See https://prodos8.com/docs/techref/writing-a-prodos-system-program/#page94
// See https://prodos8.com/docs/technote/21/
mem := t.a.mmu.getPhysicalMainRAM(false)
count := mem.peek(deviceCountAddress) + 1
fmt.Printf("Prodos disk devices: \n")
for i := uint8(0); i < count; i++ {
value := mem.peek(deviceListAddress + uint16(i))
id := "unknown"
switch value & 0xf {
case 0x0:
id = "DiskII"
case 0x4:
id = "ProFile" // Used also be the Memory Expansion Ram disk
case 0xf:
id = "RAM"
}
fmt.Printf((" S%vD%v %s($%02x)\n"), (value>>4)&7, (value>>7)+1, id, value)
}
// Device drivers
fmt.Printf("ProDOS device drivers:\n")
for slot := uint16(0); slot <= 7; slot++ {
for drive := uint16(0); drive < 2; drive++ {
address := deviceDriverVectors + (slot+drive*8)*2
value := uint16(mem.peek(address)) + 0x100*uint16(mem.peek(address+1))
fmt.Printf(" S%vD%v: $%04x\n", slot, drive+1, value)
}
}
// Datetime
value := uint16(mem.peek(deviceDateTimeVector)) + 0x100*uint16(mem.peek(deviceDateTimeVector+1))
fmt.Printf(" Datetime: $%04x\n", value)
}
func (t *traceProDOS) refreshDeviceDrives() {
mem := t.a.mmu.getPhysicalMainRAM(false)
drivers := make([]uint16, 0, 15)
for i := uint16(0); i < 16; i++ {
address := deviceDriverVectors + i*2
value := uint16(mem.peek(address)) + 0x100*uint16(mem.peek(address+1))
drivers = append(drivers, value)
}
// Datetime
value := uint16(mem.peek(deviceDateTimeVector)) + 0x100*uint16(mem.peek(deviceDateTimeVector+1))
drivers = append(drivers, value)
t.deviceDrivers = drivers
}
func (t *traceProDOS) isDriverAddress(pc uint16) bool {
for _, vector := range t.deviceDrivers {
if vector == pc {
return true
}
}
return false
}
func (t *traceProDOS) paramByte(pos uint16) uint8 {
return t.a.mmu.Peek(t.paramsAdddress + pos)
}
func (t *traceProDOS) paramWord(pos uint16) uint16 {
// Two bytes
return uint16(t.a.mmu.Peek(t.paramsAdddress+pos)) + uint16(t.a.mmu.Peek(t.paramsAdddress+pos+1))<<8
}
func (t *traceProDOS) paramLen(pos uint16) uint32 {
// Three bytes
return uint32(t.paramWord(pos)) + uint32(t.paramByte(pos+2))<<16
}
func (t *traceProDOS) paramString(pos uint16) string {
address := t.paramWord(pos)
size := t.a.mmu.Peek(address)
s := ""
for i := uint8(0); i < size; i++ {
s += string(t.a.mmu.Peek(address+1+uint16(i)) & 0x7f)
}
return s
}
func parseUnit(unit uint8) string {
if unit == 0 {
return "All"
}
drive := unit >> 7
slot := (unit >> 4) & 7
return fmt.Sprintf("S%v,D%v", slot, drive+1)
}
func getMliErrorText(code uint8) string {
// From https://prodos8.com/docs/techref/quick-reference-card/
switch code {
case 0x00:
return "No error"
case 0x01:
return "Bad system call number"
case 0x04:
return "Bad system call parameter count"
case 0x25:
return "Interrupt table full"
case 0x27:
return "I/O error"
case 0x28:
return "No device connected"
case 0x2B:
return "Disk write protected"
case 0x2E:
return "Disk switched"
case 0x40:
return "Invalid pathname"
case 0x42:
return "Maximum number of files open"
case 0x43:
return "Invalid reference number"
case 0x44:
return "Directory not found"
case 0x45:
return "Volume not found"
case 0x46:
return "File not found"
case 0x47:
return "Duplicate filename"
case 0x48:
return "Volume full"
case 0x49:
return "Volume directory full"
case 0x4A:
return "Incompatible file format, also a ProDOS directory"
case 0x4B:
return "Unsupported storage_type"
case 0x4C:
return "End of file encountered"
case 0x4D:
return "Position out of range"
case 0x4E:
return "File access error, also file locked"
case 0x50:
return "File is open"
case 0x51:
return "Directory structure damaged"
case 0x52:
return "Not a ProDOS volume"
case 0x53:
return "Invalid system call parameter"
case 0x55:
return "Volume Control Block table full"
case 0x56:
return "Bad buffer address"
case 0x57:
return "Duplicate volume"
case 0x5A:
return "File structure damaged"
default:
return "Unknown error"
}
}