-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
115 lines (98 loc) · 2.32 KB
/
debug.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
package vm
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
const DEBUG_ADDR_START Address = 0x2_10_00
const DEBUG_ADDR_END Address = 0x2_10_10
const DEBUG_ADDR_ENABLE Address = 0x2_10_00
type DebuggerDevice struct {
app *tview.Application
running bool
}
func (m *DebuggerDevice) Get(addr Address) byte {
return 0x00 // write-only
}
func (m *DebuggerDevice) Put(addr Address, val byte) {
if addr == DEBUG_ADDR_START {
if val != 0 {
go m.enable()
return
}
m.disable()
}
}
func (m *DebuggerDevice) enable() {
m.app = tview.NewApplication()
m.setupLayout()
m.app.Run()
}
func (m *DebuggerDevice) disable() {
m.app.Stop()
}
func (m *DebuggerDevice) Range() AddressRange {
return AddressRange{DEBUG_ADDR_START, TTY_ADDR_END}
}
func (m *DebuggerDevice) setupLayout() {
m.app.SetRoot(
tview.NewFlex().
AddItem(m.ramWatcher(), 0, 1, false).
AddItem(m.romDumper(), 0, 1, false),
true,
)
}
func (m *DebuggerDevice) ramWatcher() tview.Primitive {
view := tview.NewTextView()
view.SetWordWrap(true).
SetChangedFunc(func() { m.app.Draw() }).
SetBorder(true).
SetBorderAttributes(tcell.AttrBold).
SetTitle("[cyan]RAM Watcher")
go func() {
for m.running {
fmt.Fprint(view, memDump(Memory))
time.Sleep(30 * time.Millisecond)
}
}()
return view
}
func (m *DebuggerDevice) romDumper() tview.Primitive {
view := tview.NewTextView()
view.SetWordWrap(true).
SetChangedFunc(func() { m.app.Draw() }).
SetBorder(true).
SetBorderAttributes(tcell.AttrBold).
SetTitle("[cyan]RAM Watcher")
fmt.Fprint(view, romDump(Rom))
time.Sleep(30 * time.Millisecond)
return view
}
// Get a string representing the current state of the ram.
func memDump(ram *MemDevice) string {
var sb strings.Builder
for i := 0; i < int(MEM_SIZE_MAX); i++ {
sb.WriteString("0x")
sb.WriteString(strconv.Itoa(int(ram[i])))
sb.WriteString(" ")
}
return sb.String()
}
// Get a string representing the content of the rom.
func romDump(ram *RomDevice) string {
var sb strings.Builder
for i := 0; i < int(ROM_SIZE_MAX); i++ {
sb.WriteString("0x")
sb.WriteString(strconv.Itoa(int(ram[i])))
sb.WriteString(" ")
}
return sb.String()
}
func (d *DebuggerDevice) Name() string { return "Debugger" }
var Debugger Device = &DebuggerDevice{}
func init() {
RegisterDevice(Debugger)
}