/
inputloop.go
492 lines (419 loc) · 14.1 KB
/
inputloop.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
// This file is part of Gopher2600.
//
// Gopher2600 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Gopher2600 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
package debugger
import (
"fmt"
"io"
"github.com/jetsetilly/gopher2600/curated"
"github.com/jetsetilly/gopher2600/debugger/script"
"github.com/jetsetilly/gopher2600/debugger/terminal"
"github.com/jetsetilly/gopher2600/disassembly"
"github.com/jetsetilly/gopher2600/emulation"
"github.com/jetsetilly/gopher2600/gui"
"github.com/jetsetilly/gopher2600/hardware/cpu/instructions"
)
// inputLoop has two modes, defined by the clockCycle argument. when clockCycle
// is true then user will be prompted every video cycle; when false the user
// is prompted every cpu instruction.
func (dbg *Debugger) inputLoop(inputter terminal.Input, clockCycle bool) error {
dbg.isClockCycleInputLoop = clockCycle
// to speed things a bit we only check for input events every
// "inputCtDelay" iterations.
const inputCtDelay = 50
inputCt := 0
for dbg.running {
// raise hasChanged flag every iteration
dbg.hasChanged = true
var err error
var checkTerm bool
// check for events every inputCtDelay iteratins
inputCt++
if inputCt%inputCtDelay == 0 {
inputCt = 0
checkTerm = inputter.TermReadCheck()
err = dbg.checkEvents()
if err != nil {
dbg.running = false
dbg.printLine(terminal.StyleError, "%s", err)
}
// check exit video loop
if dbg.inputLoopRestart {
return nil
}
}
// if debugger is no longer running after checking interrupts and
// events then break for loop
if !dbg.running {
break // for loop
}
// return immediately if this inputLoop() is a clockCycle, the current
// quantum mode has been changed to INSTRUCTION and the emulation has
// been asked to continue with (eg. with STEP)
//
// this is important in a very specific situation:
// a) the emulation has been in CLOCK quantum mode
// b) it is mid-way through a single CPU instruction
// c) the debugger has been changed to INSTRUCTION quantum mode
//
// if we don't do this then debugging output will be wrong and confusing.
if clockCycle && dbg.continueEmulation && dbg.quantum == QuantumInstruction {
return nil
}
// check trace and output in context of last CPU result
trace := dbg.traces.check()
if trace != "" {
if dbg.commandOnTrace != nil {
err := dbg.processTokensList(dbg.commandOnTrace)
if err != nil {
dbg.printLine(terminal.StyleError, "%s", err)
}
}
dbg.printLine(terminal.StyleFeedback, fmt.Sprintf(" <trace> %s", trace))
}
var stepTrapMessage string
var breakMessage string
var trapMessage string
var watchMessage string
// check for breakpoints and traps. for video cycle input loops we only
// do this if the instruction has affected flow.
if !clockCycle || (dbg.vcs.CPU.LastResult.Defn != nil &&
(dbg.vcs.CPU.LastResult.Defn.Effect == instructions.Flow ||
dbg.vcs.CPU.LastResult.Defn.Effect == instructions.Subroutine ||
dbg.vcs.CPU.LastResult.Defn.Effect == instructions.Interrupt)) {
breakMessage = dbg.breakpoints.check(breakMessage)
trapMessage = dbg.traps.check(trapMessage)
watchMessage = dbg.watches.check(watchMessage)
stepTrapMessage = dbg.stepTraps.check("")
}
// check for halt conditions
haltEmulation := stepTrapMessage != "" || breakMessage != "" ||
trapMessage != "" || watchMessage != "" ||
dbg.lastStepError || dbg.haltImmediately
// expand halt to include step-once/many flag
haltEmulation = haltEmulation || !dbg.runUntilHalt
// reset last step error
dbg.lastStepError = false
// if emulation is to be halted or if we need to check the terminal
if haltEmulation {
// always clear steptraps. if the emulation has halted for any
// reason then any existing step trap is stale.
dbg.stepTraps.clear()
// print and reset accumulated break/trap/watch messages
dbg.printLine(terminal.StyleFeedback, breakMessage)
dbg.printLine(terminal.StyleFeedback, trapMessage)
dbg.printLine(terminal.StyleFeedback, watchMessage)
breakMessage = ""
trapMessage = ""
watchMessage = ""
// input has halted. print on halt command if it is defined
if dbg.commandOnHalt != nil {
err := dbg.processTokensList(dbg.commandOnHalt)
if err != nil {
dbg.printLine(terminal.StyleError, "%s", err)
}
}
// pause TV/GUI when emulation has halted
err = dbg.tv.Pause(true)
if err != nil {
return err
}
err = dbg.scr.SetFeature(gui.ReqState, emulation.Paused)
if err != nil {
return err
}
// take note of current machine state if the emulation was in a running
// state and is halting just now
if dbg.continueEmulation && inputter.IsInteractive() && !clockCycle {
dbg.Rewind.RecordExecutionState()
}
// reset run until halt flag - it will be set again if the parsed
// command requires it (eg. the RUN command)
dbg.runUntilHalt = false
// reset haltImmediately flag - it will be set again with the next
// HALT command
dbg.haltImmediately = false
// reset continueEmulation flag - it will set again by any command
// that requires it
dbg.continueEmulation = false
// read input from terminal inputter and parse/run commands
err = dbg.readTerminal(inputter)
if err != nil {
if curated.Is(err, script.ScriptEnd) {
dbg.printLine(terminal.StyleFeedback, err.Error())
return nil
}
return err
}
// hasChanged flag may have been false for a long time after the
// readTerminal() pause. set to true immediately.
dbg.hasChanged = true
// check exit video loop
if dbg.inputLoopRestart {
return nil
}
// unpause emulation if we're continuing emulation
if dbg.runUntilHalt {
// runUntilHalt is set to true when stepping by more than a
// clock (ie. by scanline of frame) but in those cases we want
// to set gui state to StateStepping and not StateRunning.
//
// Setting to StateRunning may have different graphical
// side-effects which would look ugly when we're only in fact
// stepping.
if dbg.stepTraps.isEmpty() {
err = dbg.tv.Pause(false)
if err != nil {
return err
}
if inputter.IsInteractive() {
err = dbg.scr.SetFeature(gui.ReqState, emulation.Running)
if err != nil {
return err
}
}
} else {
err = dbg.scr.SetFeature(gui.ReqState, emulation.Stepping)
if err != nil {
return err
}
}
// update comparison point before execution continues
if !clockCycle {
dbg.Rewind.SetComparison()
}
} else if inputter.IsInteractive() {
err = dbg.scr.SetFeature(gui.ReqState, emulation.Stepping)
if err != nil {
return err
}
}
}
if checkTerm {
err := dbg.readTerminal(inputter)
if err != nil {
return err
}
// check exit video loop
if dbg.inputLoopRestart {
return nil
}
}
if dbg.continueEmulation {
// input loops with the clockCycle flag must not execute another
// call to vcs.Step()
if clockCycle {
return nil
}
err = dbg.contEmulation(inputter)
if err != nil {
return err
}
// make sure videocyle info is current
dbg.isClockCycleInputLoop = clockCycle
// check exit video loop
if dbg.inputLoopRestart {
return nil
}
// command on step is process every time emulation has continued one step
if dbg.commandOnStep != nil {
err := dbg.processTokensList(dbg.commandOnStep)
if err != nil {
dbg.printLine(terminal.StyleError, "%s", err)
}
}
}
}
return nil
}
func (dbg *Debugger) readTerminal(inputter terminal.Input) error {
// get user input
inputLen, err := inputter.TermRead(dbg.input, dbg.buildPrompt(), dbg.events)
// check exit video loop
if dbg.inputLoopRestart {
return nil
}
// errors returned by UserRead() functions are very rich. the
// following block interprets the error carefully and proceeds
// appropriately
if err != nil {
if !curated.IsAny(err) {
// if the error originated from outside of gopher2600 then
// it is probably serious or unexpected
switch err {
case io.EOF:
// treat EOF errors the same as terminal.UserAbort
err = curated.Errorf(terminal.UserAbort)
default:
// the error is probably serious. exit input loop with err
return err
}
}
// we now know the we have an project specific error
if curated.Is(err, terminal.UserInterrupt) {
// user interrupts are triggered by the user (in a terminal
// environment, usually by pressing ctrl-c)
dbg.handleInterrupt(inputter)
} else if curated.Is(err, terminal.UserAbort) {
// like UserInterrupt but with no confirmation stage
dbg.running = false
return nil
} else {
// all other errors are passed upwards to the calling function
return err
}
}
// sometimes UserRead can return zero bytes read, we need to filter
// this out before we try any
if inputLen > 0 {
// parse user input, taking note of whether the emulation should
// continue
err = dbg.parseInput(string(dbg.input[:inputLen-1]), inputter.IsInteractive(), false)
if err != nil {
dbg.printLine(terminal.StyleError, "%s", err)
}
}
return nil
}
func (dbg *Debugger) contEmulation(inputter terminal.Input) error {
quantumInstruction := func() error {
return dbg.ref.Step(dbg.lastBank)
}
quantumVideo := func() error {
var err error
if dbg.inputLoopRestart {
return err
}
// format last CPU execution result for vcs step. this is in addition
// to the FormatResult() call in the main dbg.running loop below.
dbg.lastResult, err = dbg.Disasm.FormatResult(dbg.lastBank, dbg.vcs.CPU.LastResult, disassembly.EntryLevelExecuted)
if err != nil {
return err
}
// update debugger the same way for video quantum as for cpu quantum
err = quantumInstruction()
if err != nil {
return err
}
// for video quantums we need to run any OnStep commands before
// starting a new inputLoop
if dbg.commandOnStep != nil {
err := dbg.processTokensList(dbg.commandOnStep)
if err != nil {
dbg.printLine(terminal.StyleError, "%s", err)
}
}
// start another inputLoop() with the clockCycle boolean set to true
return dbg.inputLoop(inputter, true)
}
// get bank information before we execute the next instruction. we
// use this when formatting the last result from the CPU. this has
// to happen before we call the VCS.Step() function
dbg.lastBank = dbg.vcs.Mem.Cart.GetBank(dbg.vcs.CPU.PC.Address())
// not using the err variable because we'll clobber it before we
// get to check the result of VCS.Step()
var stepErr error
switch dbg.quantum {
case QuantumInstruction:
stepErr = dbg.vcs.Step(quantumInstruction)
case QuantumVideo:
stepErr = dbg.vcs.Step(quantumVideo)
default:
stepErr = fmt.Errorf("unknown quantum mode")
}
if dbg.inputLoopRestart {
return nil
}
// update rewind state if the last CPU instruction took place during a new
// frame event
dbg.Rewind.RecordFrameState()
// check step error. note that we format and store last CPU
// execution result whether there was an error or not. in the case
// of an error the resul a fresh formatting. if there was no error
// the formatted result is returned by the ExecutedEntry() function.
if stepErr != nil {
var err error
// format last execution result even on error
dbg.lastResult, err = dbg.Disasm.FormatResult(dbg.lastBank, dbg.vcs.CPU.LastResult, disassembly.EntryLevelExecuted)
if err != nil {
return err
}
// exit input loop if error is a plain error
if !curated.IsAny(stepErr) {
return stepErr
}
// ...set lastStepError instead and allow emulation to halt
dbg.lastStepError = true
dbg.printLine(terminal.StyleError, "%s", stepErr)
// error has occurred before CPU has completed its instruction
if !dbg.lastResult.Result.Final {
dbg.printLine(terminal.StyleError, "CPU halted mid-instruction. next step may be inaccurate.")
dbg.vcs.CPU.Interrupted = true
}
} else if dbg.vcs.CPU.LastResult.Final {
var err error
// update entry and store result as last result
dbg.lastResult, err = dbg.Disasm.ExecutedEntry(dbg.lastBank, dbg.vcs.CPU.LastResult, dbg.vcs.CPU.PC.Value())
if err != nil {
return err
}
// check validity of instruction result
err = dbg.vcs.CPU.LastResult.IsValid()
if err != nil {
dbg.printLine(terminal.StyleError, "%s", dbg.vcs.CPU.LastResult.Defn)
dbg.printLine(terminal.StyleError, "%s", dbg.vcs.CPU.LastResult)
return err
}
}
return nil
}
// interrupt errors that are sent back to the debugger need some special care
// depending on the current state.
//
// - if script recording is active then recording is ended
// - for non-interactive input set running flag to false immediately
// - otherwise, prompt use for confirmation that the debugger should quit.
func (dbg *Debugger) handleInterrupt(inputter terminal.Input) {
if dbg.scriptScribe.IsActive() {
// script recording is in progress so we insert SCRIPT END into the
// input stream
dbg.input = []byte("SCRIPT END")
return
}
if !inputter.IsInteractive() {
// terminal is not interactive so we set running to false which will
// quit the debugger as soon as possible
dbg.running = false
}
// terminal is interactive so we ask for quit confirmation
confirm := make([]byte, 1)
_, err := inputter.TermRead(confirm,
terminal.Prompt{
Content: "really quit (y/n) ",
Type: terminal.PromptTypeConfirm},
dbg.events)
if err != nil {
// another UserInterrupt has occurred. we treat a second UserInterrupt
// as thought 'y' was pressed
if curated.Is(err, terminal.UserInterrupt) {
confirm[0] = 'y'
} else {
dbg.printLine(terminal.StyleError, err.Error())
}
}
// check if confirmation has been confirmed
if confirm[0] == 'y' || confirm[0] == 'Y' {
dbg.running = false
}
}