-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugger.py
More file actions
46 lines (34 loc) · 1.2 KB
/
debugger.py
File metadata and controls
46 lines (34 loc) · 1.2 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
import time
class Debugger:
def __init__(self, cpu, program):
self.cpu = cpu
self.program = program
self.running = True
def show_state(self):
print("\n=== STATE ===")
print("PC:", self.cpu.pc)
print("REG:", self.cpu.reg)
if self.cpu.pc < len(self.program):
print("NEXT:", self.program[self.cpu.pc])
def run(self):
print("\n🧠 INTERACTIVE DEBUGGER")
print("commands: step | run | rewind | regs | exit\n")
while self.running:
cmd = input("(dbg) ").strip().lower()
if cmd == "step":
self.cpu.step(self.program)
self.show_state()
elif cmd == "run":
while not self.cpu.is_done(self.program):
self.cpu.step(self.program)
time.sleep(0.2)
self.show_state()
elif cmd == "rewind":
ok = self.cpu.rewind()
print("rewind" if ok else "no history")
elif cmd == "regs":
print(self.cpu.reg)
elif cmd == "exit":
self.running = False
else:
print("unknown command")