#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """List the DRAM bus cycles in a Lynx II logic-analyser capture, tick by tick. Our own decoder (tier 1 evidence; no DSView or sigrok code). For every CAS fall: the 16 MHz tick it falls in (CLK rising edges since the window start), the ticks since the previous CAS fall, whether RAS fell since the previous CAS (a new row), and the multiplexed address byte A0-A7 sampled at the RAS fall (row) and a little after the CAS fall (column). Gaps with no CAS are printed as such. python -I scripts/la_trace.py CAPTURE.dsl --trigger N [--before T] [--ticks T] ``--trigger N`` starts the window at the N-th TXD fall (the test ROMs pulse TXD before each measurement); ``--at SAMPLE`` starts it at a sample index instead. """ from __future__ import annotations import argparse import bisect import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) from dsl import lows, open_capture ADDRESS_LINES = [f"A{i}" for i in range(8)] class Bus: """The capture's clock, RAS, CAS and address lines as sorted edge lists.""" def __init__(self, path: Path) -> None: capture = open_capture(path) self.capture = capture self.clk = _rising(capture, "CLK") self.ras = lows(capture, "RAS") self.ras_falls = [s for s, _e in self.ras] self.cas = lows(capture, "CAS") self.cas_falls = [s for s, _e in self.cas] self.lines = [] for name in ADDRESS_LINES: first, changes = capture.edges(name) self.lines.append((first, changes)) self.txd = [s for s, _e in lows(capture, "TXD")] if "TXD" in capture.probes else [] def level(self, line: int, sample: int) -> int: first, changes = self.lines[line] return (first + bisect.bisect_right(changes, sample)) & 1 def address(self, sample: int) -> int: return sum(self.level(i, sample) << i for i in range(8)) def tick(self, sample: int) -> int: return bisect.bisect_left(self.clk, sample) def cycles(self, start: int, end: int): """(tick, new_row, row, column, cas_low_ticks) for each CAS fall in [start, end).""" i = bisect.bisect_left(self.cas_falls, start) while i < len(self.cas) and self.cas[i][0] < end: fall, rise = self.cas[i] r = bisect.bisect_right(self.ras_falls, fall) - 1 ras_fall = self.ras_falls[r] if r >= 0 else -1 prev = self.cas_falls[i - 1] if i else -1 new_row = ras_fall > prev row = self.address(ras_fall) if r >= 0 else -1 # stable from -2 to +2 samples column = self.address(fall + 3) yield self.tick(fall), new_row, row, column, self.tick(rise) - self.tick(fall) i += 1 def _rising(capture, name: str) -> list[int]: first, changes = capture.edges(name) level, out = first, [] for at in changes: level ^= 1 if level: out.append(at) return out def main(argv: list[str]) -> int: parser = argparse.ArgumentParser() parser.add_argument("capture", type=Path) parser.add_argument("--trigger", type=int) parser.add_argument("--at", type=int) parser.add_argument("--address", type=lambda v: int(v, 16), help="first CAS at this address") parser.add_argument("--before", type=int, default=0, help="ticks before the start") parser.add_argument("--ticks", type=int, default=400) args = parser.parse_args(argv) bus = Bus(args.capture) if args.address is not None: hits = [ bus.cas_falls[k] for k in range(len(bus.cas_falls)) if bus.address(bus.cas_falls[k] + 3) == args.address & 0xFF ] start = None for fall in hits: r = bisect.bisect_right(bus.ras_falls, fall) - 1 if r >= 0 and bus.address(bus.ras_falls[r]) == args.address >> 8: start = fall break if start is None: print("address not found") return 1 elif args.trigger is not None: print(f"{len(bus.txd)} TXD falls; window at fall {args.trigger}") start = bus.txd[args.trigger] else: start = args.at or 0 t0 = bus.tick(start) - args.before first = bus.clk[max(0, t0)] last = bus.clk[min(len(bus.clk) - 1, t0 + args.before + args.ticks)] previous = None for tick, new_row, row, column, low in bus.cycles(first, last): gap = "" if previous is None else f"+{tick - previous:<3}" mark = f"RAS row {row:02X}" if new_row else " " print(f"{tick - t0 - args.before:7} {gap:5} {mark} col {column:02X} cas {low}") previous = tick return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))