-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.go
More file actions
79 lines (65 loc) · 1.68 KB
/
Copy pathdebug.go
File metadata and controls
79 lines (65 loc) · 1.68 KB
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
package main
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/fatih/color"
)
func (chip *chip8) HandleDebugInput(input string) {
var cmd string
var count int
s := strings.Split(input, "x")
if len(s) == 1 {
cmd = s[0]
count = 1
} else {
cmd = s[0]
count, _ = strconv.Atoi(s[1])
}
switch cmd {
case "c", "continue":
for i := 0; i < count; i++ {
chip.EmulateNext()
}
case "p", "print":
for i := 0; i < count; i++ {
chip.printChipState()
}
default:
add, err := decodeTextOpcode(input)
if err != nil {
log.Println("Error: invalid debug input command")
return
}
for chip.pc != address(add) {
fmt.Printf("Skipping op at: %x\n", chip.pc)
chip.EmulateNext()
}
}
}
func decodeTextOpcode(input string) (uint16, error) {
hex, err := strconv.ParseInt(input, 0, 32) // 32 bit to fit w/i 4 hex
if err != nil {
return 0, err
}
return uint16(hex), nil
}
func (chip *chip8) printChipState() {
fmt16Bit := "[bin] 0b%.16b [hex] 0x%.4x [dec] %d\n"
fmt8Bit := "[bin] 0b%.8b [hex] 0x%.2x [dec] %d\n"
// next instruction to execute
nextOp := chip.DecodeInstruction(chip.pc)
color.Magenta(fmt.Sprintf(fmt16Bit, nextOp, nextOp, nextOp))
fmt.Println()
color.Green(fmt.Sprintf("PC: "+fmt16Bit, chip.pc, chip.pc, chip.pc))
color.Green(fmt.Sprintf("SP: "+fmt16Bit, chip.sp, chip.sp, chip.sp))
color.Green(fmt.Sprintf("DT: "+fmt8Bit, chip.dt, chip.dt, chip.dt))
color.Green(fmt.Sprintf("ST: "+fmt8Bit, chip.st, chip.st, chip.st))
color.Green(fmt.Sprintf(" I: "+fmt16Bit, chip.I, chip.I, chip.I))
fmt.Println()
for i := 0; i < len(chip.reg); i++ {
color.Red(fmt.Sprintf("V%x: "+fmt8Bit, i, chip.reg[i], chip.reg[i], chip.reg[i]))
}
fmt.Println()
}