#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Compare this board's CPU bus cycles with a logic-analyser capture, access by access. The capture (tier 1) shows every DRAM cycle: its address (row and column) and the tick its CAS falls in. The board runs the same test ROM to the same trigger (the first call of a routine named on the command line) and records every CPU cycle it makes: tick, address, read or write. RAM cycles appear in both; register cycles (Mikey, Suzy, the boot ROM) appear only in the board's list and as gaps in the capture's. Both sequences are aligned on the first RAM cycle at ``--align`` and walked together; each line shows the ticks between consecutive RAM cycles on each side, so a per-access cost difference shows where it happens. CAS timing (measured with this script and ``la_trace.py``): in a 5-tick read that opens a row, CAS falls one tick after the cycle starts; in a 4-tick page-mode read it falls as the cycle starts; in a write, two ticks after. The comparison therefore puts each board RAM cycle at ``start + 1``, ``start`` or ``start + 2``. python -I scripts/la_compare.py CAPTURE.dsl ROM_NAME ROUTINE --align ADDR [--trigger N] [--count N] [--row lynx1|lynx2] """ from __future__ import annotations import argparse import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) from la_trace import Bus from lynx_python import testrom from lynx_python.board import Lynx from lynx_python.machine import MACHINES def board_cycles(name: str, routine: str, count: int, row: str) -> list[tuple]: """(tick, address, kind) per CPU cycle from the first entry to ``routine``.""" records: list[tuple] = [] recording = [False] original_read, original_write = Lynx._read, Lynx._write def read(self, address): start = self.tick value = original_read(self, address) if recording[0]: records.append((start, self.tick, address, "R", self.cpu.sync)) return value def write(self, address, value): start = self.tick original_write(self, address, value) if recording[0]: records.append((start, self.tick, address, "W", False)) Lynx._read, Lynx._write = read, write try: board = testrom.boot(name, MACHINES[row]) target = testrom.symbols(name)[routine] cpu = board.cpu limit = board.tick + 30 * 16_000_000 while cpu.pc != target and board.tick < limit: board.step() recording[0] = True while len(records) < count and board.tick < limit: board.step() finally: Lynx._read, Lynx._write = original_read, original_write return records def is_ram(address: int) -> bool: return address < 0xFC00 def main(argv: list[str]) -> int: parser = argparse.ArgumentParser() parser.add_argument("capture", type=Path) parser.add_argument("rom") parser.add_argument("routine") parser.add_argument("--align", type=lambda v: int(v, 16), required=True) parser.add_argument("--trigger", type=int, default=0) parser.add_argument("--count", type=int, default=600) parser.add_argument("--row", default="lynx2") parser.add_argument("--diff-only", action="store_true") parser.add_argument("--registers", action="store_true") args = parser.parse_args(argv) bus = Bus(args.capture) start = bus.txd[args.trigger] la = [ (tick, (row << 8) | col, new) for tick, new, row, col, _low in bus.cycles(start, start + 6 * 16 * 100_000) ] ours = board_cycles(args.rom, args.routine, args.count, args.row) i = next(k for k, c in enumerate(la) if c[1] == args.align) j = next(k for k, c in enumerate(ours) if c[2] == args.align and is_ram(c[2])) la_base, our_base = la[i][0], ours[j][0] pending: list[tuple] = [] total_la = total_ours = 0 prev_la = prev_ours = prev_end = None la_only = False registers: list[tuple] | None = [] if args.registers else None differences = 0 while i < len(la) and j < len(ours): start_tick, end_tick, address, kind, _ = ours[j] if not is_ram(address): pending.append((kind, address, end_tick - start_tick)) j += 1 continue la_tick, la_address, la_new = la[i] if la_address != address: # A capture cycle the board does not make (refresh, DMA): show it alone. if not args.diff_only: print(f" LA only {la_address:04X} at +{la_tick - la_base} new={la_new}") la_only = True i += 1 continue our_cas = start_tick + (2 if kind == "W" else 1 if end_tick - start_tick >= 5 else 0) # The capture's cycle starts before its CAS fall by the offsets above. la_start = la_tick - (2 if kind == "W" else 1 if la_new else 0) if registers is not None and prev_end is not None and len(pending) == 1 and not la_only: registers.append((pending[0], la_start - prev_end, la_start)) prev_end = la_start + 5 if (kind == "W" or la_new) else la_start + 4 la_only = False dl = None if prev_la is None else la_tick - prev_la do = None if prev_ours is None else our_cas - prev_ours flag = "" if dl == do else " <-- differs" if flag: differences += 1 if dl is not None and do is not None: total_la += dl total_ours += do if not args.diff_only or flag: print( f"{kind}{address:04X} LA +{la_tick - la_base:<6} d{dl!s:<4} " f"ours +{our_cas - our_base:<6} d{do!s:<4} " + " ".join(f"{k}{a:04X}:{t}" for k, a, t in pending) + flag ) pending = [] prev_la, prev_ours = la_tick, our_cas i += 1 j += 1 print(f"{differences} differing gaps; their sum LA {total_la}, ours {total_ours}") if registers is not None: # Each register access alone between two RAM cycles: its length in the capture, # and the capture tick it ends on modulo 16 (the 1 us prescaler's period). print("register access: length in ticks, end tick mod 16 (absolute capture ticks)") for (kind, address, ours_ticks), length, end in registers: print(f" {kind}{address:04X} ours {ours_ticks:2} LA {length:2} end%16 {end % 16:2}") return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))