# SPDX-License-Identifier: MIT """Summarise a MAME run of scripts/mame_trace.py by the CPC's partial I/O decode. python -I scripts/mame_summary.py reference/mame/cpc6128 Reads io.log (``O|I port value seconds``) and, when present, run.log's trace lines (``T pc totalcycles``). Each access is classed by the address bits the CPC decodes (DOC: the CRTC Compendium, section 5.1, and Kevin Thacker's I/O port allocation page): A15=0,A14=1 the Gate Array (and the 6128's RAM PAL); A14=0 the CRTC with A9-A8 choosing select, write, status or read; A13=0 the upper ROM select; A12=0 the printer latch; A11=0 the 8255 with A9-A8 choosing port A, B, C or control; A10=0,A7=0 the FDC (A8=1: uPD765A, A0 choosing status or data; A8=0: the motor latch). One access may select several devices, as on the machine. The trace part prints the distribution of instruction lengths in T-states: on the CPC the Gate Array's /WAIT rounds every bus cycle to a 4-T-state boundary, so a model that follows it shows (almost) only multiples of 4. That is a fact about MAME's model (OBSERVED, closed box), not about the hardware. """ from __future__ import annotations import collections import sys from pathlib import Path GA_FUNCTIONS = ("pen select", "colour", "mode/ROM/interrupt (RMR)", "RAM config (PAL)") CRTC_PORTS = ("select", "write", "status", "read") PPI_PORTS = ("A", "B", "C", "control") def devices(port: int) -> list[str]: found = [] if port & 0xC000 == 0x4000: found.append("GA") if not port & 0x4000: found.append("CRTC " + CRTC_PORTS[port >> 8 & 3]) if not port & 0x2000: found.append("ROM select") if not port & 0x1000: found.append("printer") if not port & 0x0800: found.append("PPI " + PPI_PORTS[port >> 8 & 3]) if not port & 0x0400 and not port & 0x0080: found.append(("FDC " + ("data" if port & 1 else "status")) if port & 0x100 else "FDC motor") return found or ["(none)"] def main() -> int: run = Path(sys.argv[1]) counts: collections.Counter[str] = collections.Counter() crtc_selected = 0 crtc_writes: list[tuple[float, int, int]] = [] ga_writes: list[tuple[float, str, int]] = [] other: list[tuple[float, str, str, int]] = [] for line in (run / "io.log").read_text().splitlines(): kind, port_text, value_text, time_text = line.split() port, value, time = int(port_text, 16), int(value_text, 16), float(time_text) for device in devices(port): counts[f"{kind} {device}"] += 1 if kind == "O" and device == "CRTC select": crtc_selected = value & 0x1F elif kind == "O" and device == "CRTC write": crtc_writes.append((time, crtc_selected, value)) elif kind == "O" and device == "GA": ga_writes.append((time, GA_FUNCTIONS[value >> 6], value)) elif device.startswith(("ROM", "FDC", "PPI control")) or (kind == "O" and device.startswith("PPI")): other.append((time, kind, device, value)) print("accesses by device:") for name, count in sorted(counts.items()): print(f" {count:7d} {name}") print("first CRTC register writes (time, register, value):") for time, register, value in crtc_writes[:20]: print(f" {time * 1e6:10.0f} us R{register:<2d} = {value:3d} (&{value:02X})") print("Gate Array writes by function:", collections.Counter(f for _, f, _ in ga_writes)) print("first Gate Array writes:", " ".join(f"{v:02X}" for _, _, v in ga_writes[:40])) rmr = sorted({v for _, f, v in ga_writes if f.startswith("mode")}) print("RMR values seen:", " ".join(f"{v:02X}" for v in rmr)) print("first ROM/FDC/PPI writes:") for time, kind, device, value in other[:25]: print(f" {time * 1e6:10.0f} us {kind} {device:14s} {value:02X}") trace = run / "run.log" lengths: collections.Counter[int] = collections.Counter() previous = None if trace.exists(): for line in trace.read_text(errors="replace").splitlines(): if not line.startswith("T "): continue cycles = int(line.split()[2]) if previous is not None: lengths[cycles - previous] += 1 previous = cycles if lengths: total = sum(lengths.values()) aligned = sum(c for t, c in lengths.items() if t % 4 == 0) print(f"instruction lengths (T-states) over {total} instructions: " f"{aligned} multiples of 4 ({100 * aligned / total:.2f}%)") # fmt: skip print(" ", " ".join(f"{t}:{c}" for t, c in sorted(lengths.items()))) return 0 if __name__ == "__main__": sys.exit(main())