#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Suzy's bus activity while she paints sprites, from a Lynx II logic-analyser capture. Our own decoder (tier 1 evidence; built on ``la_trace.Bus``). The game captures hold RAS, CAS and the multiplexed address, not the write strobe, so a read-modify-write of a video-buffer byte shows as two CAS cycles at one address. Suzy paints a sprite line as such pairs on consecutive addresses; this script finds them: - ``--look``: the densest stretch of pairs, printed cycle by cycle (to read the pattern by eye); - default: every "painted run" (pairs on consecutive addresses, other cycles allowed between them), with its length in bytes, its ticks, the ticks per painted byte, and the cycles between one run's end and the next run's start (the per-line work: SCB and sprite-data reads, Mikey's DMA and refresh). python -I scripts/la_sprite.py CAPTURE.dsl [--look] [--limit-samples N] """ from __future__ import annotations import argparse import collections import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) from la_trace import Bus def decode(bus: Bus, end: int) -> list[tuple[int, bool, int]]: """(tick, new_row, address) for every CAS fall before sample ``end``.""" return [(t, nr, (row << 8) | col) for t, nr, row, col, _low in bus.cycles(0, end)] def pairs(cycles) -> list[int]: """Indices i where cycles i and i+1 are at one address (a read-modify-write).""" return [i for i in range(len(cycles) - 1) if cycles[i][2] == cycles[i + 1][2]] def runs(cycles, at: list[int], max_between: int = 6): """Group pairs into painted runs: each pair's address one more (or one less) than the previous pair's, at most ``max_between`` other cycles between them.""" out = [] current = [at[0]] if at else [] for k in at[1:]: last = current[-1] step = cycles[k][2] - cycles[last][2] if ( step in (1, -1) and k - last - 2 <= max_between and (len(current) < 2 or step == cycles[last][2] - cycles[current[-2]][2]) ): current.append(k) else: out.append(current) current = [k] if current: out.append(current) return out def groups_of(cycles): """RAS groups: (first tick, last tick, first address, CAS count, distinct addresses).""" out = [] for t, nr, a in cycles: if nr or not out: out.append([t, t, a, 1, {a}]) else: g = out[-1] g[1] = t g[3] += 1 g[4].add(a) return out def phases(cycles, dest_low: int, dest_high: int) -> dict[str, collections.Counter]: """Gaps (ticks from one Suzy RAS group's last CAS to the next one's first) by phase, and CAS counts per group. Display DMA (a 4-CAS group at the same row followed by a refresh, a 1-CAS group whose row equals its column) and refresh are set aside and their time is not counted in the gaps they interrupt (the gap restarts after them). A group is "dest" when its address is in [dest_low, dest_high) (the drawing buffer), else "data" (sprite data and SCBs).""" out = {k: collections.Counter() for k in ( "dest>dest", "data>data", "dest>data", "data>dest", "offset>data", "cas:dest", "cas:data", "reads per dest group", )} # fmt: skip gs = groups_of(cycles) previous = None i = 0 while i < len(gs): g = gs[i] refresh = g[3] == 1 and (g[2] >> 8) == (g[2] & 0xFF) dma = ( g[3] == 4 and i + 1 < len(gs) and gs[i + 1][3] == 1 and ((gs[i + 1][2] >> 8) == (gs[i + 1][2] & 0xFF)) ) if refresh or dma: previous = None # a steal: do not measure across it i += 1 continue kind = "dest" if dest_low <= g[2] < dest_high else "data" out[f"cas:{kind}"][g[3]] += 1 if kind == "dest": out["reads per dest group"][g[3] - len(g[4])] += 1 if previous is not None: gap = min(g[0] - previous[1], 99) pkind = "offset" if previous[2] == "data" and previous[3] == 1 else previous[2] key = f"{pkind}>{kind}" if key in out: out[key][gap] += 1 if pkind == "offset" and kind == "data": out["offset>data"][gap] += 1 previous = (g[0], g[1], kind, g[3]) i += 1 return out def main(argv: list[str]) -> int: parser = argparse.ArgumentParser() parser.add_argument("capture", type=Path) parser.add_argument("--look", action="store_true") parser.add_argument("--dest", help="drawing buffer as LOW-HIGH hex addresses: phase gaps") parser.add_argument("--limit-samples", type=int, default=0) parser.add_argument("--min-run", type=int, default=4) args = parser.parse_args(argv) bus = Bus(args.capture) end = args.limit_samples or bus.capture.samples cycles = decode(bus, end) at = pairs(cycles) print(f"{len(cycles)} CAS cycles, {len(at)} same-address pairs") if args.dest: low, high = (int(v, 16) for v in args.dest.split("-")) for name, counter in phases(cycles, low, high).items(): top = ", ".join(f"{k}:{n}" for k, n in sorted(counter.most_common(12))) print(f"{name:22} {sum(counter.values()):6} {top}") return 0 if args.look: bins = collections.Counter(cycles[i][0] // 2000 for i in at) best = max(bins, key=bins.get) start = next(i for i, c in enumerate(cycles) if c[0] // 2000 == best) previous = cycles[start][0] for t, nr, a in cycles[start : start + 160]: print(f"{t:9} +{t - previous:<3} {'RAS' if nr else ' '} {a:04X}") previous = t return 0 groups = [g for g in runs(cycles, at) if len(g) >= args.min_run] per_byte = collections.Counter() for g in groups: first, last = g[0], g[-1] ticks = cycles[last + 1][0] - cycles[first][0] between = (last - first + 2) - 2 * len(g) per_byte[(round(ticks / len(g), 1), between)] += 1 print(f"{len(groups)} painted runs of {args.min_run}+ bytes") print("ticks per painted byte (between-cycles inside the run): count") for (tpb, between), n in per_byte.most_common(25): print(f" {tpb:5} ({between:3} other cycles): {n}") return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))