# SPDX-License-Identifier: MIT """Survey the fetched SingleStepTests/tlcs900h files (its README's binary format). python -I scripts/survey_sst.py [reference/src/sst-tlcs900h/v1/*.bin] Reads each file written in the README's format (magic `T9SSTB1\\0`, u32 version, u32 count, string family; per record: name, opcode bytes, initial and final state, initial and final RAM, the bus-transaction list) and prints, per file: the record count, the opcode lengths seen, the clock span of the transactions (the generator's states, an emulator's opinion), and the read/write counts. Written from the README only; the repository's own converter script is not fetched or run. """ from __future__ import annotations import collections import struct import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] STATE_FIELDS = ( ["xwa0", "xwa1", "xwa2", "xwa3", "xbc0", "xbc1", "xbc2", "xbc3", "xde0", "xde1", "xde2", "xde3", "xhl0", "xhl1", "xhl2", "xhl3", "xix", "xiy", "xiz", "xsp", "pc"] + [f"dmas{i}" for i in range(4)] + [f"dmad{i}" for i in range(4)] + [f"dmam{i}" for i in range(4)] + ["intnest", "cf", "nf", "vf", "hf", "zf", "sf", "ca", "na", "va", "ha", "za", "sa", "rfp", "iff", "halt", "op", "pic", "piq_size", "piq0", "piq1", "piq2", "piq3", "mar", "mdr"] ) # fmt: skip class Reader: def __init__(self, data: bytes) -> None: self.data, self.at = data, 0 def u32(self) -> int: (value,) = struct.unpack_from(" int: (value,) = struct.unpack_from(" bytes: size = self.u32() value = self.data[self.at : self.at + size] self.at += size return value def state(self) -> dict: return {name: self.u32() for name in STATE_FIELDS} def ram(self) -> list: return [(self.u32(), self.u32()) for _ in range(self.u32())] def cycles(self) -> list: return [(self.u64(), self.u32(), self.u32(), self.u32(), self.u32(), self.u32()) for _ in range(self.u32())] # fmt: skip def survey(path: Path) -> None: r = Reader(path.read_bytes()) magic = r.data[:8] r.at = 8 version, count = r.u32(), r.u32() family = r.blob().decode() lengths, spans, kinds = collections.Counter(), collections.Counter(), collections.Counter() first = None for _ in range(count): name = r.blob().decode() opcode = r.blob() initial, final = r.state(), r.state() r.ram() r.ram() cycles = r.cycles() lengths[len(opcode)] += 1 if cycles: spans[cycles[-1][0] - cycles[0][0]] += 1 for clock, action, *_ in cycles: kinds["read" if action == 1 else "write"] += 1 kinds["clock_nonzero"] += clock != 0 if first is None: first = (name, opcode.hex(), initial["pc"], final["pc"], len(cycles)) left = len(r.data) - r.at print(f"{path.name}: magic={magic!r} v{version} family={family} records={count} " f"trailing_bytes={left}") # fmt: skip print(f" opcode lengths {dict(sorted(lengths.items()))}; transactions {dict(kinds)}") print(f" clock span first-to-last transaction (top 6) {spans.most_common(6)}") print(f" first record {first}") def main() -> int: paths = [Path(p) for p in sys.argv[1:]] or sorted( (ROOT / "reference" / "src" / "sst-tlcs900h" / "v1").glob("*.bin")) for path in paths: survey(path) return 0 if __name__ == "__main__": sys.exit(main())