# SPDX-License-Identifier: MIT """The tooling: DebugSession, the JSONL trace, the command debugger and ``python -m``.""" from __future__ import annotations import io import json from rig import Rig from hg51b_python.__main__ import main from hg51b_python.console import CommandDebugger, format_state from hg51b_python.debug import DebugSession, StopReason from hg51b_python.trace import TraceWriter, first_divergence, read_trace def program(rig: Rig) -> None: # 00: LD A,#$12; ST RAM,A; WRRAM 0,DPR+#$05; RDRAM 1,DPR+#$05; INC MAR; # 05: JMP $07 (over the HALT at 06); 07: HALT rig.place([0x6412, 0xE00C, 0xEC05, 0x6D05, 0x4000, 0x0807, 0xFC00, 0xFC00]) def test_step_records_and_ram_accesses() -> None: rig = Rig() program(rig) session = DebugSession(rig) rig.start() first = session.step() assert first is not None and first.instruction.text == "LD A,#$12" assert first.clocks == 2560 + 1 # the page load came first session.step() write = session.step() assert write.ram_accesses == (("w", 5, 0x12),) read = session.step() assert read.ram_accesses == (("r", 5, 0x12),) and read.after.ram == 0x1212 records = [session.step() for _ in range(3)] assert records[-1].instruction.text == "HALT" assert session.step() is None # idle kinds = [(e.kind, e.phase) for e in session.bus_log] assert kinds[:2] == [("cache", "start"), ("cache", "end")] session.close() assert "read" not in rig.cx.__dict__ def test_breakpoints_and_watchpoints() -> None: rig = Rig() program(rig) session = DebugSession(rig) session.add_breakpoint(0x0005) rig.start() result = session.run(max_steps=100) assert result.reason is StopReason.BREAKPOINT and rig.cx.pc == 5 rig2 = Rig() program(rig2) session2 = DebugSession(rig2) session2.add_watchpoint(5, "w") rig2.start() result = session2.run(max_steps=100) assert result.reason is StopReason.WATCHPOINT and result.hits == (("w", 5, 0x12),) result = session2.run(max_steps=100) assert result.reason is StopReason.IDLE def test_host_and_bus_watchpoints() -> None: rig = Rig() session = DebugSession(rig) session.add_host_watchpoint(0x7F5E, "R") rig.cx.read(0x7F5E) rig.cx.write(0x7F5E, 0) hits = session.take_host_hits() assert [(h.direction, h.address) for h in hits] == [("R", 0x7F5E)] rig.poke(0x0A8000, b"\x77") rig.cx.r[0] = 0x0A8000 rig.place([0x6260, 0x612E, 0x1C00, 0xFC00]) session.add_bus_watchpoint(0x0A8000) rig.start() result = session.run(max_steps=10) assert result.reason is StopReason.BUS_WATCHPOINT and result.hits[0].detail == 0x77 def test_trace_round_trip_and_divergence() -> None: def record() -> str: rig = Rig() program(rig) session = DebugSession(rig, history_limit=0) stream = io.StringIO() writer = TraceWriter(session, stream) rig.start() while writer.step() is not None: pass writer.flush() return stream.getvalue() one, two = record(), record() records = list(read_trace(io.StringIO(one))) assert {r["type"] for r in records} >= {"step", "host", "cache"} steps = [r for r in records if r["type"] == "step"] assert [s["text"] for s in steps][-1] == "HALT" assert first_divergence(read_trace(io.StringIO(one)), read_trace(io.StringIO(two))) is None changed = one.replace('"a":18', '"a":19', 1) assert first_divergence(read_trace(io.StringIO(one)), read_trace(io.StringIO(changed))) json.loads(one.splitlines()[0]) def test_command_debugger() -> None: rig = Rig() program(rig) debugger = CommandDebugger(DebugSession(rig)) assert debugger.execute("call 0000:00").lines == ("started 0000:00",) lines = debugger.execute("step 2").lines assert lines[0].split()[2:4] == ["LD", "A,#$12"] and "ST RAM,A" in lines[1] assert debugger.execute("d 0000:02 1").lines[0].endswith("WRRAM 0,DPR+#$05") debugger.execute("set r3 0x123") assert rig.cx.r[3] == 0x123 assert debugger.execute("host read 0x7F89").lines == ("host read 7F89: 23",) debugger.execute("break 0000:05") assert "breakpoint" in debugger.execute("continue").lines[0] assert debugger.execute("ram 5 1").lines == ("005: 12",) assert debugger.execute("quit").quit out = io.StringIO() debugger.interact(io.StringIO("bogus\nregisters\nq\n"), out) assert "error:" in out.getvalue() and "PB:PC" in out.getvalue() assert len(format_state(rig.cx.capture_state(memory=False))) == 7 def test_python_m(cartridge_file, capsys, tmp_path) -> None: path = cartridge_file("retail") # the No-Intro zip, read in place assert main([str(path), "--test-function", "0E:89"]) == 0 assert "R0=054336 R1=FFFFFF" in capsys.readouterr().out trace = tmp_path / "t.jsonl" assert main([str(path), "--test-function", "0E:3C", "--trace", str(trace)]) == 0 assert "R0=00000F" in capsys.readouterr().out assert sum(1 for line in trace.open() if '"step"' in line) == 4