# SPDX-License-Identifier: MIT """The tooling: DebugSession, JSONL traces, the command debugger, ``python -m gsu_python``.""" from __future__ import annotations import io import json import pytest from conftest import KROM, make_gsu, require, start from gsu_asm import NOP, PLOT, STOP, branch, ibt, inc, iwt, ldw, program, stw from gsu_python.__main__ import main from gsu_python.console import CommandDebugger, CommandError from gsu_python.debug import DebugSession, StopReason, live_instruction from gsu_python.trace import encode, first_divergence, read_trace def session_for(code, **regs): g = make_gsu(code) for name, value in regs.items(): g.r[int(name[1:])] = value start(g) return DebugSession(g) def test_breakpoint_and_stop(): s = session_for(program(inc(1), inc(2), inc(3), STOP)) s.add_breakpoint(0x008002) result = s.run(max_steps=100) assert result.reason is StopReason.BREAKPOINT assert (s.gsu.r[1], s.gsu.r[2]) == (1, 1) assert s.run(max_steps=100).reason is StopReason.STOPPED def test_breakpoint_in_a_delay_slot_is_exact(): """The opcode in a delay slot is at the branch's next byte, not R15 - 1 (U1).""" s = session_for(program(branch("BRA", 0x10), inc(5), STOP) + [0] * 0x20) s.add_breakpoint(0x008002) assert s.run(max_steps=10).reason is StopReason.BREAKPOINT assert live_instruction(s.gsu).text == "INC R5" def test_ram_and_register_watchpoints(): s = session_for(program(iwt(0, 0x1234), stw(1), ldw(1), STOP), r1=0x0100) s.add_watchpoint(0x0100, "w") result = s.run(max_steps=100) assert result.reason is StopReason.WATCHPOINT assert ("w", 0x0100, 0x34) in result.hits s.add_register_watchpoint(0) s.gsu.r[0] = 0 assert s.run(max_steps=100).reason is StopReason.REGISTER_WATCHPOINT def test_plot_and_host_records(): s = session_for(program(ibt(0, 1), [0x4E], iwt(1, 5), iwt(2, 7), PLOT, STOP)) s.run(max_steps=100) assert [(p.x, p.y, p.colr) for p in s.plots] == [(5, 7, 1)] s.gsu.read_io(0x3031) assert s.host_log[-1].direction == "R" and s.host_log[-1].address == 0x3031 s.close() assert "read_io" not in s.gsu.__dict__ def test_trace_round_trip_and_divergence(): code = program(iwt(1, 0x10), inc(1), NOP, STOP) left = session_for(code) right = session_for(code) lines_a = [encode(left.step()) for _ in range(4)] right.gsu.r[1] = 0x99 lines_b = [encode(right.step()) for _ in range(4)] records = list(read_trace(io.StringIO("\n".join(lines_a)))) assert records[1]["text"] == "IWT R1,#0010h" assert records[1]["clocks"] == 15 # 3 cache-less ROM fetches at 21.4 MHz position, _, _ = first_divergence(records, read_trace(io.StringIO("\n".join(lines_b)))) assert position == 0 # R1 differs before the first step assert first_divergence(records, records) is None def test_console_commands(): s = session_for(program(inc(1), inc(1), STOP)) d = CommandDebugger(s) assert d.execute("step 2").lines[1].endswith("[GO]") assert "R1=0001" in d.execute("regs").lines[0] assert d.execute("set r3 $1234").lines[0].count("R3=1234") == 1 assert d.execute("d 00:8000 2").lines[0].endswith("INC R1") assert d.execute("host read 0x3030").lines[0].startswith("read $3030 = 20") assert d.execute("continue").lines[0].startswith("stopped") assert d.execute("quit").quit with pytest.raises(CommandError): d.execute("bogus") def test_main_krom(capsys): rom = require(KROM / "GSUTest" / "ADD" / "GSUADD.sfc") assert main([str(rom), "--krom"]) == 0 assert capsys.readouterr().out.strip().endswith("64 of 64 rows PASS") def test_main_run_and_trace(tmp_path, capsys): rom = require(KROM / "GSUTest" / "ADD" / "GSUADD.sfc") trace = tmp_path / "add.jsonl" assert main([str(rom), "--start", "00:BCB9", "--trace", str(trace)]) == 0 assert "R15=BCC3" in capsys.readouterr().out records = [json.loads(line) for line in trace.read_text().splitlines()] assert records[-1]["text"] == "STOP"