further digging and basic emulator
This commit is contained in:
76
tests/test_emulator.py
Normal file
76
tests/test_emulator.py
Normal file
@@ -0,0 +1,76 @@
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from h8536.emulator import (
|
||||
HEARTBEAT_FRAME,
|
||||
ON_CHIP_RAM_START,
|
||||
REGISTER_FIELD_START,
|
||||
SCI1_SCR,
|
||||
SCI1_SSR,
|
||||
SCI1_TDR,
|
||||
SCI_SCR_TE,
|
||||
SCI_SSR_TDRE,
|
||||
H8536Emulator,
|
||||
MemoryMap,
|
||||
SCI1,
|
||||
discover_rom_path,
|
||||
load_rom,
|
||||
)
|
||||
|
||||
|
||||
class EmulatorHarnessTest(unittest.TestCase):
|
||||
def test_memory_map_routes_rom_ram_register_and_external(self):
|
||||
memory = MemoryMap(bytes([0x12, 0x34, 0x56, 0x78]))
|
||||
|
||||
self.assertEqual(memory.read8(0x0001), 0x34)
|
||||
memory.write8(ON_CHIP_RAM_START, 0xA5)
|
||||
self.assertEqual(memory.read8(ON_CHIP_RAM_START), 0xA5)
|
||||
memory.write8(REGISTER_FIELD_START, 0x5A)
|
||||
self.assertEqual(memory.read8(REGISTER_FIELD_START), 0x5A)
|
||||
memory.write8(0xF000, 0x11)
|
||||
self.assertEqual(memory.read8(0xF000), 0x11)
|
||||
|
||||
def test_sci_transmit_capture_requires_enabled_transmitter(self):
|
||||
sci = SCI1()
|
||||
sci.write(SCI1_TDR, 0x33)
|
||||
self.assertEqual(sci.tx_bytes, [])
|
||||
self.assertFalse(sci.tx_events[-1].emitted)
|
||||
|
||||
sci.write(SCI1_SCR, sci.scr | SCI_SCR_TE)
|
||||
for byte in HEARTBEAT_FRAME:
|
||||
sci.write(SCI1_TDR, byte)
|
||||
|
||||
self.assertEqual(bytes(sci.tx_bytes), HEARTBEAT_FRAME)
|
||||
self.assertEqual(sci.tx_frames, [HEARTBEAT_FRAME])
|
||||
self.assertTrue(sci.saw_heartbeat())
|
||||
self.assertTrue(sci.read(SCI1_SSR) & SCI_SSR_TDRE)
|
||||
|
||||
def test_vector_decoding_uses_minimum_mode_reset_word(self):
|
||||
rom = bytearray(0x1004)
|
||||
rom[0:2] = b"\x10\x00"
|
||||
rom[0x1000] = 0x00
|
||||
|
||||
emulator = H8536Emulator(bytes(rom))
|
||||
|
||||
self.assertEqual(emulator.reset_vector(), 0x1000)
|
||||
self.assertEqual(emulator.cpu.pc, 0x1000)
|
||||
self.assertEqual(emulator.vectors[0x0000][1], 0x1000)
|
||||
|
||||
def test_harness_instantiates_on_repo_artifacts(self):
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
rom_path = discover_rom_path(root)
|
||||
self.assertIsNotNone(rom_path)
|
||||
rom_bytes, loaded_path = load_rom(root=root)
|
||||
|
||||
emulator = H8536Emulator(rom_bytes)
|
||||
report = emulator.run(max_steps=4)
|
||||
|
||||
self.assertEqual(loaded_path, rom_path)
|
||||
self.assertEqual(emulator.reset_vector(), 0x1000)
|
||||
self.assertGreaterEqual(len(rom_bytes), 0x1002)
|
||||
self.assertEqual(report.steps, 4)
|
||||
self.assertFalse(report.heartbeat_seen)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -1,6 +1,7 @@
|
||||
import io
|
||||
import json
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from h8536.protocol_capture import analyze_capture_text, format_text_report, main, parse_capture_text
|
||||
|
||||
@@ -15,6 +16,15 @@ class ProtocolCaptureTest(unittest.TestCase):
|
||||
self.assertEqual(chunks[0].device_direction, "tx")
|
||||
self.assertEqual(chunks[0].bytes, (0x00, 0x00, 0x15, 0x80, 0x00, 0xCF))
|
||||
|
||||
def test_parses_idle_frame_lines_without_direction_token(self):
|
||||
chunks = parse_capture_text("11:54:40.567 frame 006 00 00 00 00 80 DA\n")
|
||||
|
||||
self.assertEqual(len(chunks), 1)
|
||||
self.assertEqual(chunks[0].timestamp_ms, 42880567)
|
||||
self.assertEqual(chunks[0].analyzer_direction, "rx")
|
||||
self.assertEqual(chunks[0].device_direction, "tx")
|
||||
self.assertEqual(chunks[0].bytes, (0x00, 0x00, 0x00, 0x00, 0x80, 0xDA))
|
||||
|
||||
def test_recombines_user_split_rx_chunks_into_valid_call_frame(self):
|
||||
analysis = analyze_capture_text(
|
||||
"16:06:15.502 RX 003 bytes 00 00 15\n"
|
||||
@@ -140,6 +150,23 @@ class ProtocolCaptureTest(unittest.TestCase):
|
||||
payload = json.loads(output.getvalue())
|
||||
self.assertEqual(payload["frames"][0]["report_candidate"]["index"], 0x15)
|
||||
|
||||
def test_idle_reference_capture_when_present(self):
|
||||
path = Path("ROM/rcp-txd-idle-only.txt")
|
||||
if not path.exists():
|
||||
self.skipTest("idle reference capture is not present")
|
||||
|
||||
analysis = analyze_capture_text(path.read_text(encoding="utf-8"))
|
||||
|
||||
self.assertGreaterEqual(analysis["frame_count"], 10)
|
||||
self.assertEqual(
|
||||
analysis["gate_session_hints"]["observed_autonomous_report_names"],
|
||||
["heartbeat_alive_candidate"],
|
||||
)
|
||||
heartbeat = analysis["gate_session_hints"]["heartbeat_cadence_ms"]
|
||||
self.assertEqual(heartbeat["count"], analysis["frame_count"])
|
||||
self.assertGreater(heartbeat["average"], 600)
|
||||
self.assertLess(heartbeat["average"], 800)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
135
tests/test_report_source_trace.py
Normal file
135
tests/test_report_source_trace.py
Normal file
@@ -0,0 +1,135 @@
|
||||
import io
|
||||
import json
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from h8536.report_source_trace import analyze_report_sources, format_text_report, main, write_report_sources
|
||||
|
||||
|
||||
def ins(
|
||||
address: int,
|
||||
mnemonic: str,
|
||||
operands: str = "",
|
||||
*,
|
||||
text: str | None = None,
|
||||
targets: list[int] | None = None,
|
||||
block: int | None = None,
|
||||
) -> dict[str, object]:
|
||||
row: dict[str, object] = {
|
||||
"address": address,
|
||||
"mnemonic": mnemonic,
|
||||
"operands": operands,
|
||||
"text": text or f"{mnemonic} {operands}".strip(),
|
||||
"kind": "call" if mnemonic in {"BSR", "JSR"} else "normal",
|
||||
"targets": targets or [],
|
||||
"references": [],
|
||||
}
|
||||
if block is not None:
|
||||
row["dataflow"] = {"block": block}
|
||||
return row
|
||||
|
||||
|
||||
def payload() -> dict[str, object]:
|
||||
return {
|
||||
"call_graph": {
|
||||
"nodes": [
|
||||
{"start": 0x1000, "end": 0x10FF, "label": "loc_1000"},
|
||||
{"start": 0x2000, "end": 0x20FF, "label": "loc_2000"},
|
||||
{"start": 0x3000, "end": 0x30FF, "label": "loc_3000"},
|
||||
],
|
||||
},
|
||||
"instructions": [
|
||||
ins(0x1000, "MOV:E.B", "#H'80, R2", block=0x1000),
|
||||
ins(0x1002, "MOV:I.W", "#H'0007, R3", block=0x1000),
|
||||
ins(0x1005, "BSR", "loc_3E54", targets=[0x3E54], block=0x1000),
|
||||
ins(0x2000, "MOV:I.W", "#H'0012, R5", block=0x2000),
|
||||
ins(0x2003, "CMP:G.W", "@(-H'2000,R5), R1", block=0x2000),
|
||||
ins(0x2007, "MOV:E.B", "#H'80, R2", block=0x2000),
|
||||
ins(0x2009, "MOV:G.W", "R5, R3", block=0x2000),
|
||||
ins(0x200B, "BSR", "loc_3E54", targets=[0x3E54], block=0x2000),
|
||||
ins(0x3000, "MOV:E.B", "#H'00, R2", block=0x3000),
|
||||
ins(0x3002, "MOV:I.W", "#H'0007, R3", block=0x3000),
|
||||
ins(0x3005, "BSR", "loc_3E54", targets=[0x3E54], block=0x3000),
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
class ReportSourceTraceTest(unittest.TestCase):
|
||||
def test_finds_direct_static_report_index_0007(self):
|
||||
analysis = analyze_report_sources(payload())
|
||||
|
||||
self.assertEqual(analysis["summary"]["direct_call_count"], 3)
|
||||
self.assertEqual(analysis["summary"]["direct_static_hit_count"], 1)
|
||||
hit = analysis["calls"][0]
|
||||
self.assertTrue(hit["can_directly_enqueue_report_index"])
|
||||
self.assertEqual(hit["r2"]["bit7"], True)
|
||||
self.assertEqual(hit["r3"]["classification"], "constant")
|
||||
self.assertEqual(hit["r3"]["value"], 0x0007)
|
||||
self.assertIn("Direct static enqueue source", hit["assessment"])
|
||||
|
||||
def test_classifies_table_context_and_clear_gate(self):
|
||||
analysis = analyze_report_sources(payload())
|
||||
dynamic = analysis["calls"][1]
|
||||
gated_off = analysis["calls"][2]
|
||||
|
||||
self.assertEqual(dynamic["r3"]["classification"], "constant")
|
||||
self.assertEqual(dynamic["r3"]["value"], 0x0012)
|
||||
self.assertEqual(dynamic["table_hints"][0]["table"], "primary_value_table_candidate")
|
||||
self.assertFalse(gated_off["can_directly_enqueue_report_index"])
|
||||
self.assertEqual(gated_off["r2"]["bit7"], False)
|
||||
self.assertIn("would not enqueue", gated_off["assessment"])
|
||||
|
||||
def test_text_report_mentions_conclusion_and_caveats(self):
|
||||
text = format_text_report(analyze_report_sources(payload()))
|
||||
|
||||
self.assertIn("loc_3E54 Report Source Trace", text)
|
||||
self.assertIn("Direct static 0x0007 hits: 1", text)
|
||||
self.assertIn("Indirect dispatch", text)
|
||||
self.assertIn("R3 evidence", text)
|
||||
|
||||
def test_cli_json_output_and_out_file(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
input_path = Path(tmp) / "rom.json"
|
||||
output_path = Path(tmp) / "sources.json"
|
||||
input_path.write_text(json.dumps(payload()), encoding="utf-8")
|
||||
stdout = io.StringIO()
|
||||
|
||||
rc = main(["--json", "--out", str(output_path), str(input_path)], stdout=stdout)
|
||||
|
||||
self.assertEqual(rc, 0)
|
||||
self.assertIn("wrote", stdout.getvalue())
|
||||
written = json.loads(output_path.read_text(encoding="utf-8"))
|
||||
self.assertEqual(written["kind"], "report_source_trace")
|
||||
self.assertEqual(written["summary"]["direct_static_hit_count"], 1)
|
||||
|
||||
def test_write_text_output(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
input_path = Path(tmp) / "rom.json"
|
||||
output_path = Path(tmp) / "sources.txt"
|
||||
input_path.write_text(json.dumps(payload()), encoding="utf-8")
|
||||
|
||||
analysis = write_report_sources(input_path, output_path)
|
||||
|
||||
self.assertEqual(analysis["kind"], "report_source_trace")
|
||||
self.assertIn("Report Source Trace", output_path.read_text(encoding="utf-8"))
|
||||
|
||||
def test_real_rom_smoke_when_present(self):
|
||||
path = Path("build/rom_decompiled.json")
|
||||
if not path.exists():
|
||||
self.skipTest("build/rom_decompiled.json is not present")
|
||||
|
||||
payload_real = json.loads(path.read_text(encoding="utf-8"))
|
||||
analysis = analyze_report_sources(payload_real)
|
||||
|
||||
self.assertEqual(analysis["kind"], "report_source_trace")
|
||||
self.assertGreaterEqual(analysis["summary"]["direct_call_count"], 1)
|
||||
self.assertIn("0x0007", analysis["summary"]["conclusion"])
|
||||
for call in analysis["calls"]:
|
||||
self.assertIn("address_hex", call)
|
||||
self.assertIn("r2", call)
|
||||
self.assertIn("r3", call)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user