# SPDX-License-Identifier: MIT """The tooling: DebugSession, JSONL traces, the command debugger, the CLI, the replay's mechanics on a synthetic MAME log.""" from __future__ import annotations import io import pytest from conftest import image_path from words import core, jp, ld, op from upd7725_python.__main__ import main, parse_command from upd7725_python.console import CommandDebugger, CommandError from upd7725_python.debug import DebugSession, StopReason from upd7725_python.harness import Harness from upd7725_python.replay import Replay, parse_log, port_of from upd7725_python.trace import first_divergence, read_trace, write_trace DST_MEM, DST_KLM, SRC_MEM = 15, 12, 15 def test_ram_accesses_and_watchpoints(): dsp = core( ld(0x1234, DST_MEM), op(alu=5, p=0, src=SRC_MEM, dst=DST_KLM), jp(0x100, na=2), dp=3, ) session = DebugSession(dsp) record = session.step() assert record.ram_accesses == (("w", 3, 0x1234),) record = session.step() assert record.ram_accesses == (("r", 3, 0x1234), ("r", 3, 0x1234), ("r", 0x43, 0)) session.add_watchpoint(3, "w") session.add_breakpoint(1) dsp.pc = 0 result = session.run(max_steps=10) assert result.reason is StopReason.WATCHPOINT and result.hits == (("w", 3, 0x1234),) result = session.run(max_steps=10) assert result.reason is StopReason.BREAKPOINT and result.state.pc == 1 def test_host_accesses_are_recorded(): dsp = core(ld(0xABCD, 6)) session = DebugSession(dsp) session.add_host_watchpoint("R", "DR") session.step() assert dsp.read_sr() == 0x80 assert dsp.read_dr() == 0xCD assert [(a.direction, a.port, a.value) for a in session.host_log] == [ ("R", "SR", 0x80), ("R", "DR", 0xCD), ] assert len(session.take_host_hits()) == 1 session.close() assert "read_dr" not in vars(dsp) def test_trace_round_trip(): dsp = core(ld(0x0001, 1), op(alu=9), op(alu=9)) session = DebugSession(dsp) records = [session.step() for _ in range(3)] stream = io.StringIO() assert write_trace([*records, *session.host_log], stream) == 3 stream.seek(0) decoded = list(read_trace(stream)) assert decoded[2]["after"]["a"] == 3 and decoded[1]["text"] == "INC ACCA" other = [dict(record) for record in decoded] other[2] = {**other[2], "after": {**other[2]["after"], "a": 4}} assert first_divergence(decoded, decoded) is None assert first_divergence(decoded, other)[0] == 2 def test_console(): dsp = core(ld(0x0080, 6), jp(0x0BE, na=1)) debugger = CommandDebugger(DebugSession(dsp)) assert any("JRQM 001h" in line for line in debugger.execute("disassemble 0 2").lines) debugger.execute("step 3") assert debugger.execute("registers").lines[0].startswith("PC=0001") assert debugger.execute("host sr").lines == ("host sr: 80",) debugger.execute("host write 5") # 16-bit mode (SR = 0): low byte, then high byte debugger.execute("host write 0") debugger.execute("step") assert dsp.pc == 2 debugger.execute("set a 0x1234") assert dsp.a == 0x1234 assert debugger.execute("quit").quit with pytest.raises(CommandError): debugger.execute("bogus") def test_cli(capsys): path = image_path("dsp1b") if path is None: pytest.skip("BYOR image absent (set MAME_ROMPATH)") assert parse_command("1F/0,12=1024") == (0x1F, [0, 0x12], 1024) assert main([str(path), "--command", "2F/0=1"]) == 0 assert "2F: 0101" in capsys.readouterr().out def test_replay_mechanics(): """A synthetic MAME log: host write at stamp 3 (seen by instruction 4), D lines, L.""" program = [ld(0x0400, 7), ld(0x0080, 6), jp(0x0BE, na=2), op(src=8, dst=1), jp(0x100, na=3)] log = [ "D 2 0 0 0 0 0 0 0 0 0 80 2", # the JRQM $ wait "L 1", "W 3F8000 55 3", # after instruction 3; first seen by instruction 4 "D 2 0 0 0 0 0 0 0 0 0 55 4", "D 3 0 0 0 0 0 0 0 0 0 55 5", # the wait fell through "D 4 55 0 0 0 0 0 0 0 0 55 6", "D 3 55 0 0 0 0 0 0 0 0 55 7", "R 3FC000 84 7", # MOV @A,DR set RQM: SR = 8400h ] replay = Replay(program, [], Harness(program, []).dsp.model, "a14") result = replay.run(parse_log(log)) assert result.finding is None, result.finding assert (result.traced, result.reads_checked) == (6, 1) bad = [*log[:-1], "R 3FC000 00 7"] replay = Replay(program, [], Harness(program, []).dsp.model, "a14") assert "host read of SR" in replay.run(parse_log(bad)).finding def test_port_decode(): assert port_of(0x3FC000, "a14") == "SR" and port_of(0x3F8001, "a14") == "DR" assert port_of(0x007000, "a12") == "SR" and port_of(0x006FFF, "a12") == "DR" assert port_of(0x600001, "seta") == "SR" and port_of(0x680123, "seta") == "RAM"