-
Notifications
You must be signed in to change notification settings - Fork 3
/
zog.go
586 lines (510 loc) · 11.5 KB
/
zog.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
package zog // import "github.com/jbert/zog"
import (
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
)
type Zog struct {
Mem *Memory
reg Registers
/* In the Z80 CPU, there is
an interrupt enable flip-flop (IFF) that is set or reset by the programmer using the Enable
Interrupt (EI) and Disable Interrupt (DI) instructions. When the IFF is reset, an interrupt
cannot be accepted by the CPU.
*/
is InterruptState
interruptCh chan byte
outputHandler func(uint16, byte)
inputHandler func(uint16) byte
traces Regions
eTrace executeTrace
instIters int
numRecentTraces int
indexRecentTraces int
recentTraces []executeTrace
}
type InterruptState struct {
IFF1 bool
IFF2 bool
Mode byte
}
type locWatch struct {
new byte
old byte
}
type executeTrace struct {
ops uint64
pc uint16
inst Instruction
reg Registers
watches map[uint16]locWatch
}
func (et *executeTrace) String() string {
s := fmt.Sprintf("%d: %04X %s %s", et.ops, et.pc, et.reg.Summary(), et.inst)
for addr, lw := range et.watches {
s += fmt.Sprintf(" W:%04X [%02X->%02X]", addr, lw.old, lw.new)
}
return s
}
func New(memSize uint16) *Zog {
is := InterruptState{
Mode: 1,
}
z := &Zog{
Mem: NewMemory(memSize),
interruptCh: make(chan byte),
is: is,
}
z.Clear()
return z
}
type Region struct {
start uint16
end uint16
}
type Regions []Region
func NewRegion(start, end uint16) Region {
return Region{start: start, end: end}
}
func ParseRegions(s string) (Regions, error) {
var regions Regions
s = strings.TrimSpace(s)
if s == "" {
return regions, nil
}
startEnds := strings.Split(s, ",")
for _, startEnd := range startEnds {
bits := strings.Split(startEnd, "-")
start, err := strconv.ParseUint(bits[0], 16, 16)
if err != nil {
return regions, err
}
end, err := strconv.ParseUint(bits[1], 16, 16)
if err != nil {
return regions, err
}
region := NewRegion(uint16(start), uint16(end))
regions.add(Regions{region})
}
return regions, nil
}
func (rs *Regions) contains(addr uint16) bool {
for _, r := range *rs {
if r.contains(addr) {
return true
}
}
return false
}
func (rs *Regions) add(regions Regions) {
*rs = append(*rs, regions...)
}
func (rs Regions) String() string {
var strs []string
for _, r := range rs {
strs = append(strs, r.String())
}
return strings.Join(strs, ",")
}
func (r *Region) contains(addr uint16) bool {
return r.start <= addr && addr < r.end
}
func (r Region) String() string {
return fmt.Sprintf("%04X-%04X", r.start, r.end)
}
func (z *Zog) TraceRegions(regions Regions) error {
z.traces.add(regions)
return nil
}
func (z *Zog) WatchRegions(regions Regions) error {
z.Mem.SetWatchFunc(z.memWatchSeen)
z.Mem.watches.add(regions)
return nil
}
func (z *Zog) TraceOnHalt(numHaltTraces int) {
z.numRecentTraces = numHaltTraces
}
func (z *Zog) State() string {
return fmt.Sprintf("%s AF %04X BC %04X DE %04X HL %04X SP %04X IX %04X IY %04X",
z.FlagString(),
z.reg.Read16(AF),
z.reg.Read16(BC),
z.reg.Read16(DE),
z.reg.Read16(HL),
z.reg.Read16(SP),
z.reg.Read16(IX),
z.reg.Read16(IY))
}
func (z *Zog) RunAssembly(a *Assembly) error {
err := z.Load(a)
if err != nil {
return err
}
return z.execute(a.BaseAddr)
}
func (z *Zog) RunBytes(loadAddr uint16, buf []byte, runAddr uint16) error {
err := z.LoadBytes(loadAddr, buf)
if err != nil {
return nil
}
return z.execute(runAddr)
}
func (z *Zog) LoadROMFile(addr uint16, fname string) error {
return z.LoadFile(addr, fname, true)
}
func (z *Zog) LoadFile(addr uint16, fname string, readonly bool) error {
buf, err := ioutil.ReadFile(fname)
if err != nil {
return fmt.Errorf("Can't load file [%s]: %s", fname, err)
}
if readonly {
roRegion := NewRegion(addr, uint16(len(buf)))
fmt.Printf("Add RO region %s\n", roRegion)
z.Mem.readonly = append(z.Mem.readonly, roRegion)
}
return z.LoadBytes(addr, buf)
}
func (z *Zog) LoadRegisters(reg Registers) {
z.reg = reg
}
func (z *Zog) GetRegisters() Registers {
return z.reg
}
func (z *Zog) LoadInterruptState(is InterruptState) {
z.is = is
}
func (z *Zog) LoadBytes(addr uint16, buf []byte) error {
err := z.Mem.Copy(addr, buf)
if err != nil {
return err
}
// fmt.Printf("L: %04X [%04X]\n", addr, len(buf))
return nil
}
func (z *Zog) Load(a *Assembly) error {
buf, err := a.Encode()
if err != nil {
return err
}
return z.LoadBytes(a.BaseAddr, buf)
}
func (z *Zog) Clear() {
z.Mem.Clear()
z.reg = Registers{}
// 64KB will give zero here, correctly
z.reg.SP = uint16(z.Mem.Len())
z.is.IFF1 = false
z.is.IFF2 = false
}
func (z *Zog) RegisterOutputHandler(handler func(uint16, byte)) error {
if z.outputHandler != nil {
return errors.New("Output handler already registered")
}
z.outputHandler = handler
return nil
}
func (z *Zog) RegisterInputHandler(handler func(uint16) byte) error {
if z.inputHandler != nil {
return errors.New("Input handler already registered")
}
z.inputHandler = handler
return nil
}
// F flag register:
// S Z X H X P/V N C
type flag int
const (
F_C flag = iota
F_N
F_PV
F_3
F_H
F_5
F_Z
F_S
)
func (f flag) String() string {
switch f {
case F_C:
return "C"
case F_N:
return "N"
case F_PV:
return "PV"
case F_3:
return "X1"
case F_H:
return "H"
case F_5:
return "X2"
case F_Z:
return "Z"
case F_S:
return "S"
default:
panic(fmt.Sprintf("Unknown flag: %d", f))
}
}
func (z *Zog) SetFlag(f flag, new bool) {
mask := byte(1) << uint(f)
flags, err := F.Read8(z)
if err != nil {
panic(fmt.Sprintf("Error reading flags: %s", err))
}
if new {
flags = flags | mask
} else {
mask = ^mask
flags = flags & mask
}
err = F.Write8(z, flags)
if err != nil {
panic(fmt.Sprintf("Error writing flags: %s", err))
}
}
func (z *Zog) GetFlag(f flag) bool {
mask := byte(1) << uint(f)
flags, err := F.Read8(z)
if err != nil {
panic(fmt.Sprintf("Error reading flags: %s", err))
}
flag := flags & mask
return flag != 0
}
func (z *Zog) FlagString() string {
s := ""
for i := 7; i >= 0; i-- {
f := flag(i)
if f == F_3 || f == F_5 {
continue
}
v := 0
if z.GetFlag(f) {
v = 1
}
s += fmt.Sprintf("%s%d", f, v)
}
return s
}
var ErrHalted = errors.New("HALT called")
// Implement io.Reader
func (z *Zog) Read(buf []byte) (int, error) {
if len(buf) != 1 {
panic("Non-byte read")
}
n, err := z.Mem.Peek(z.reg.PC)
if err != nil {
return 0, fmt.Errorf("Error reading: %s", err)
}
z.reg.PC++
buf[0] = n
return 1, nil
}
func (z *Zog) jp(addr uint16) {
z.reg.PC = addr
// fmt.Printf("JP: %04X\n", z.reg.PC)
}
func (z *Zog) jr(d int8) {
z.reg.PC += uint16(d) // Wrapping works out
// fmt.Printf("JR: %04X [%d]\n", z.reg.PC, d)
}
func (z *Zog) di() error {
z.is.IFF1 = false
z.is.IFF2 = false
return nil
}
func (z *Zog) ei() error {
z.is.IFF1 = true
z.is.IFF2 = true
return nil
}
func (z *Zog) InterruptEnabled() bool {
return z.is.IFF1
}
func (z *Zog) im(mode byte) error {
if mode != 0 && mode != 1 && mode != 2 {
panic(fmt.Sprintf("Invalid interrupt mode: %d", mode))
}
z.is.Mode = mode
return nil
}
func (z *Zog) push(nn uint16) {
z.reg.SP--
z.reg.SP--
err := z.Mem.Poke16(z.reg.SP, nn)
if err != nil {
panic(fmt.Sprintf("Can't write to SP [%04X]: %s", z.reg.SP, err))
}
}
func (z *Zog) pop() uint16 {
nn, err := z.Mem.Peek16(z.reg.SP)
if err != nil {
panic(fmt.Sprintf("Can't write to SP [%04X]: %s", z.reg.SP, err))
}
z.reg.SP++
z.reg.SP++
return nn
}
func (z *Zog) out(port uint16, n byte) {
if z.outputHandler == nil {
fmt.Fprintf(os.Stderr, "Output handler not yet registered, %02X not send to %04X\n", n, port)
}
z.outputHandler(port, n)
}
func (z *Zog) in(port uint16) byte {
if z.inputHandler != nil {
return z.inputHandler(port)
}
fmt.Fprintf(os.Stderr, "Input handler not yet registered, %04X not read\n", port)
return 0xff
}
func (z *Zog) addRecentTrace(et executeTrace) {
if z.numRecentTraces <= 0 {
panic("wtf")
}
if len(z.recentTraces) < z.numRecentTraces {
z.recentTraces = append(z.recentTraces, et)
return
}
z.recentTraces[z.indexRecentTraces] = et
z.indexRecentTraces++
z.indexRecentTraces = z.indexRecentTraces % z.numRecentTraces
return
}
func (z *Zog) DoInterrupt() {
if !z.InterruptEnabled() {
return
}
z.interruptCh <- z.is.Mode
}
func (z *Zog) getInstruction(halted bool) (Instruction, error) {
// Check for interrupt
imMode := byte(0)
select {
case imMode = <-z.interruptCh:
return z.processInterrupt(imMode)
default:
}
if !halted {
return DecodeOne(z)
}
imMode = <-z.interruptCh
return z.processInterrupt(imMode)
}
func (z *Zog) processInterrupt(imMode byte) (Instruction, error) {
z.di()
switch imMode {
case 0:
return nil, fmt.Errorf("TODO: interrupt in mode 0")
case 1:
// We need to do RST 38h
return &RST{0x38}, nil
case 2:
// We get LSB from data bus (we choose 0) and MSG from I reg
addr := uint16(z.reg.I) << 8
return NewCALL(True, Imm16(addr)), nil
default:
return nil, fmt.Errorf("Unknown interrupt mode: %d", imMode)
}
}
func (z *Zog) execute(addr uint16) (errRet error) {
z.reg.PC = addr
return z.Run()
}
const ClockHz = 3500000 / 2
var TStateDuration = time.Second / time.Duration(ClockHz)
func (z *Zog) Run() (errRet error) {
ops := uint64(0)
TStates := uint64(0)
lastOps := uint64(0)
lastTStates := uint64(0)
statsEvery := uint64(1000000)
startTime := time.Now()
lastEmit := startTime
var err error
var inst Instruction
defer func() {
if r := recover(); r != nil {
switch v := r.(type) {
case error:
errRet = v
default:
errRet = fmt.Errorf("PANIC: %v", v)
}
}
if z.numRecentTraces > 0 {
for i := range z.recentTraces {
fmt.Printf("%s\n", z.recentTraces[(i+z.indexRecentTraces)%z.numRecentTraces].String())
}
}
}()
halted := false
EXECUTING:
for {
before := time.Now()
lastPC := z.reg.PC
// May be from PC, or may be interrupt
inst, err = z.getInstruction(halted)
halted = false
if err != nil {
fmt.Printf("Error decoding: %s\n", err)
break EXECUTING
}
// fmt.Printf("I: %04X %s\n", lastPC, inst)
z.instIters = 0
instErr := inst.Execute(z)
waitTStates := inst.TStates(z)
z.eTrace = executeTrace{ops: ops, pc: lastPC, reg: z.reg, inst: inst, watches: make(map[uint16]locWatch)}
if z.traces.contains(lastPC) {
println(z.eTrace.String())
}
if z.numRecentTraces > 0 {
z.addRecentTrace(z.eTrace)
}
if instErr != nil {
if instErr == ErrHalted && z.InterruptEnabled() {
halted = true
continue EXECUTING
}
// Error handling after the loop
err = instErr
break EXECUTING
}
ops++
TStates += uint64(waitTStates)
if ops%statsEvery == 0 {
now := time.Now()
dur := now.Sub(lastEmit)
secs := dur.Seconds()
opsPerSec := float64(ops-lastOps) / secs
TStatesPerSec := float64(TStates-lastTStates) / secs
fmt.Printf("%5.2f: PC [%04X] %d ops %d t : %f ops/sec %f t/sec\n", now.Sub(startTime).Seconds(), lastPC, ops, TStates, opsPerSec, TStatesPerSec)
lastEmit = now
lastOps = ops
lastTStates = TStates
}
//if waitTStates > 30 {
// println(waitTStates, " ", z.eTrace.String())
//}
waitDuration := time.Duration(waitTStates) * TStateDuration
// Busy wait - can't get time.Sleep or syscall.Nanosleep to give me good enough granularity
waitUntil := before.Add(waitDuration)
for time.Now().Before(waitUntil) {
}
}
// The only return should be on HALT. nil is bad here.
if err == ErrHalted {
return nil
}
if err == nil {
return errors.New("Execute returned nil error")
}
return fmt.Errorf("Failed to execute: %s", err)
}
func (z *Zog) memWatchSeen(addr uint16, old byte, new byte) {
z.eTrace.watches[addr] = locWatch{old: old, new: new}
}