-
Notifications
You must be signed in to change notification settings - Fork 0
/
debugger.go
268 lines (231 loc) · 6.35 KB
/
debugger.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
package debug
import (
"log"
"sync"
"github.com/kazzmir/nes/cmd/nes/gfx"
nes "github.com/kazzmir/nes/lib"
)
type DebugCommand interface {
Name() string
}
type DebugCommandSimple struct {
name string
}
func (command *DebugCommandSimple) Name() string {
return command.name
}
type DebugCommandStep struct {
name string
count int
}
func (command *DebugCommandStep) Name() string {
return command.name
}
func makeCommand(name string) DebugCommand {
return &DebugCommandSimple{name: name}
}
var DebugCommandContinue DebugCommand = makeCommand("continue")
var DebugCommandNext DebugCommand = makeCommand("next")
// break when the cpu's PC is at a specific value
// TODO: add conditional breakpoints, and break upon
// reading/writing specific memory addresses
type Breakpoint struct {
PC uint16
Id uint64
Enabled bool
}
func (breakpoint *Breakpoint) Hit(cpu *nes.CPUState) bool {
return breakpoint.Enabled && breakpoint.PC == cpu.PC
}
type Debugger interface {
Handle(*nes.CPUState) bool
AddPCBreakpoint(uint16) Breakpoint
AddCurrentPCBreakpoint() Breakpoint
RemoveBreakpoint(id uint64) bool
GetBreakpoints() []Breakpoint
Continue()
Step(count int)
Next()
IsStopped() bool
Update(*nes.CPUState, nes.InstructionTable)
Close()
}
type DebuggerMode int
const (
ModeStopped DebuggerMode = iota
ModeStepping
ModeNext
ModeContinue
)
type DefaultDebugger struct {
Commands chan DebugCommand
Mode DebuggerMode
Breakpoints []Breakpoint
BreakpointId uint64
Cpu *nes.CPUState
Window *DebugWindow
Lock sync.Mutex
LastPc uint16
StepCount int
}
type Registers struct {
A byte
X byte
Y byte
SP byte
PC uint16
Status byte
}
func (debugger *DefaultDebugger) Update(cpu *nes.CPUState, table nes.InstructionTable){
if debugger.Window != nil && debugger.Window.IsOpen {
/* Warning: fetch calls cpu.LoadMemory that could in theory impact mapper's
* if the PC is pointing into mapper memory.
*/
instruction, err := cpu.Fetch(table)
if err == nil {
pc := cpu.PC
debugger.Window.AddInstruction(pc, instruction)
debugger.Window.SetCycle(cpu.Cycle)
debugger.Window.SetRegisters(Registers{
A: cpu.A,
X: cpu.X,
Y: cpu.Y,
SP: cpu.SP,
PC: cpu.PC,
Status: cpu.Status,
})
debugger.Window.Redraw()
}
}
}
func (debugger *DefaultDebugger) IsStopped() bool {
return debugger.Mode == ModeStopped || debugger.Mode == ModeStepping
}
func (debugger *DefaultDebugger) ContinueUntilBreak(){
debugger.Mode = ModeContinue
}
func (debugger *DefaultDebugger) GetBreakpoints() []Breakpoint {
debugger.Lock.Lock()
defer debugger.Lock.Unlock()
return gfx.CopyArray(debugger.Breakpoints)
}
func (debugger *DefaultDebugger) AddPCBreakpoint(pc uint16) Breakpoint {
var breakpoint Breakpoint
debugger.WithLock(func(){
breakpoint = Breakpoint{
PC: pc,
Id: debugger.BreakpointId,
Enabled: true,
}
debugger.Breakpoints = append(debugger.Breakpoints, breakpoint)
debugger.BreakpointId += 1
})
return breakpoint
}
func (debugger *DefaultDebugger) AddCurrentPCBreakpoint() Breakpoint {
return debugger.AddPCBreakpoint(debugger.Cpu.PC)
}
func (debugger *DefaultDebugger) WithLock(fn func()){
debugger.Lock.Lock()
defer debugger.Lock.Unlock()
fn()
}
func (debugger *DefaultDebugger) RemoveBreakpoint(id uint64) bool {
found := false
debugger.WithLock(func(){
var out []Breakpoint
for _, breakpoint := range debugger.Breakpoints {
if breakpoint.Id != id {
out = append(out, breakpoint)
} else {
found = true
}
}
debugger.Breakpoints = out
})
return found
}
func (debugger *DefaultDebugger) Stop(){
debugger.Mode = ModeStopped
}
func (debugger *DefaultDebugger) Continue(){
select {
case debugger.Commands<-DebugCommandContinue:
default:
}
}
func (debugger *DefaultDebugger) Step(count int){
select {
case debugger.Commands<-&DebugCommandStep{name: "step", count: count}:
default:
}
}
func (debugger *DefaultDebugger) Next(){
select {
case debugger.Commands<-DebugCommandNext:
default:
}
}
func (debugger *DefaultDebugger) Handle(cpu *nes.CPUState) bool {
select {
case command := <-debugger.Commands:
switch command {
case DebugCommandContinue:
log.Printf("[debug] continue")
debugger.ContinueUntilBreak()
return true
case DebugCommandNext:
log.Printf("[debug] next")
debugger.Mode = ModeNext
debugger.LastPc = cpu.PC
default:
switch command.(type) {
case *DebugCommandStep:
log.Printf("[debug] step")
debugger.Mode = ModeStepping
step := command.(*DebugCommandStep)
debugger.StepCount = step.count
return true
}
}
default:
}
if debugger.Mode == ModeNext {
if cpu.PC == debugger.LastPc {
return true
} else {
debugger.Mode = ModeStopped
return false
}
}
if debugger.Mode == ModeStepping {
debugger.StepCount -= 1
return debugger.StepCount > 0
}
if debugger.Mode == ModeStopped {
return false
}
for _, breakpoint := range debugger.Breakpoints {
if breakpoint.Hit(cpu) {
debugger.Stop()
return false
}
}
return true
}
func (debugger *DefaultDebugger) Close(){
if debugger.Window != nil {
debugger.Window.SetDebugger(nil)
}
}
func MakeDebugger(cpu *nes.CPUState, window *DebugWindow) Debugger {
debugger := &DefaultDebugger{
Commands: make(chan DebugCommand, 5),
Mode: ModeContinue,
BreakpointId: 1,
Cpu: cpu,
Window: window,
}
window.SetDebugger(debugger)
return debugger
}