#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Measure each hardware-register access of a known routine in a logic-analyser capture. Tier 1 evidence, our own decoder. The routine's code is taken from the test ROM as this board loads it (run to ``_main``), disassembled with w65c02-python's table. For every absolute-addressed instruction whose operand is a hardware register ($FC00-$FDFF, $FFF9), the capture's RAM cycles give the end of the instruction's last operand fetch and the start of the next RAM cycle; the register cycle is the time between (one register cycle: loads and stores only). Printed per register: the lengths seen and the capture tick (mod 16) the access ends on. python -I scripts/la_registers.py CAPTURE.dsl ROM SYMBOL [--bytes N] CAS offsets as in ``la_compare.py``: a read that opens a row has CAS one tick into its cycle, a page-mode read at the start, a write two ticks in; every cycle is 5 ticks except a page-mode read (4). """ from __future__ import annotations import argparse import collections 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 w65c02_python.disasm import decode_table from lynx_python import testrom from lynx_python.machine import LYNX2 LOADS = {"LDA", "LDX", "LDY", "BIT", "CMP", "CPX", "CPY", "AND", "ORA", "EOR", "ADC", "SBC"} STORES = {"STA", "STX", "STY", "STZ"} def routine(rom: str, symbol: str, length: int) -> list[tuple[int, str, int]]: """(address, mnemonic, register) for each register load/store in the routine.""" board = testrom.boot(rom, LYNX2) labels = testrom.symbols(rom) main_at = labels["_main"] limit = board.tick + 30 * 16_000_000 while board.cpu.pc != main_at and board.tick < limit: board.step() table = decode_table(LYNX2.cpu) start = labels[symbol] out = [] pc = start while pc < start + length: opcode = board.ram[pc] name, mode, operands = table[opcode][0], table[opcode][1], table[opcode][2] if mode == "absolute" and operands == 2: target = board.ram[pc + 1] | (board.ram[pc + 2] << 8) if (0xFC00 <= target < 0xFE00 or target == 0xFFF9) and ( name.upper() in LOADS or name.upper() in STORES ): out.append((pc, name.upper(), target)) if name.upper() in ("RTI", "RTS"): break pc += 1 + operands return out def main(argv: list[str]) -> int: parser = argparse.ArgumentParser() parser.add_argument("capture", type=Path, nargs="+") parser.add_argument("--rom", required=True) parser.add_argument("--symbol", required=True) parser.add_argument("--bytes", type=int, default=256) args = parser.parse_args(argv) accesses = routine(args.rom, args.symbol, args.bytes) wanted = {pc + 2: (pc, name, reg) for pc, name, reg in accesses} for pc, name, reg in accesses: print(f" ${pc:04X} {name} ${reg:04X}") for path in args.capture: lengths, pairs = measure(path, wanted) print(f"{path.name}: register {{length: count}}; (end tick mod 16, length): count") for reg in sorted(lengths): print(f" ${reg:04X} {dict(sorted(lengths[reg].items()))}") print(f" {sorted(pairs[reg].items())[:24]}") return 0 def measure(path: Path, wanted: dict) -> tuple[dict, dict]: """Per register: lengths seen, and (end tick mod 16, length) pairs, in one capture (the absolute phase differs from capture to capture).""" lengths: dict[int, collections.Counter] = collections.defaultdict(collections.Counter) pairs: dict[int, collections.Counter] = collections.defaultdict(collections.Counter) bus = Bus(path) cycles = list(bus.cycles(0, bus.clk[-1])) addresses = [(row << 8) | col for _t, _n, row, col, _l in cycles] for k in range(len(cycles) - 1): hit = wanted.get(addresses[k]) if hit is None or addresses[k - 1] != addresses[k] - 1: continue pc, _name, reg = hit tick = cycles[k][0] next_tick, next_new, *_ = cycles[k + 1] if addresses[k + 1] != pc + 3: continue # an interrupt, refresh or DMA cycle in between: skip # The last operand read ends 4 ticks after its CAS (page mode: CAS at the # start of 4 ticks; new row: CAS 1 tick into 5); the next read starts 1 tick # before its CAS when it opens a row (it always does after a register cycle). end_prev = tick + 4 start_next = next_tick - (1 if next_new else 0) length = start_next - end_prev lengths[reg][length] += 1 pairs[reg][((start_next % 16), length)] += 1 return lengths, pairs if __name__ == "__main__": sys.exit(main(sys.argv[1:]))