#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Write seeded lockstep scripts for rung 2, run by scripts/lockstep_ref.py (pokey-python) and examples/pokey-trace.rs (this crate) alike. python3 scripts/lockstep_gen.py OUT_DIR [--count 64] [--seed 1] [--cycles 20000] Each script is a random stream of bus accesses, ticks and pin changes over about ``--cycles`` ticks, with seeded power-ons, snapshots restored mid-run, ``record=True``, both variants and the optional PotRC helper mixed in. Script lines (one per operation): new VARIANT RECORD construct (C012294 or LINEAR; record 0/1) and wire the loggers power SEED | power - power_on(seed) / power_on() w REG VALUE write r REG read (traced) t N N ticks, each traced P MASK pot_level(n) = bit n of MASK PR T0 ... T7 wire PotRC with these targets (- for None) K DEFAULT CODE:RESP ... key_response(code): RESP for the listed codes, else DEFAULT S V | B V the SID pin, BCLK as an input save | load snapshot() into the one slot / restore() it state | take traced: every state field / take_outputs() """ from __future__ import annotations import argparse import random from pathlib import Path #: weights of the register a write goes to: the audio registers most, every one some WRITE_REGS = [0, 1, 2, 3, 4, 5, 6, 7] * 3 + [8] * 4 + [9] * 2 + [10, 11, 11, 13, 14, 14, 15, 15, 15] def audc(rnd: random.Random) -> int: return rnd.choice((0x00, 0x20, 0x40, 0x60, 0x80, 0xA0, 0xC0, 0xE0, 0x10, 0x30)) | rnd.randrange(16) def value_for(rnd: random.Random, reg: int) -> int: if reg < 8: if reg & 1: return audc(rnd) return rnd.choice((rnd.randrange(4), rnd.randrange(16), rnd.randrange(256))) if reg == 15: # mostly running (bits 0-1), with every other bit random; init mode sometimes v = rnd.randrange(256) if rnd.random() < 0.7: v |= 0x03 if rnd.random() < 0.5: v &= ~0x70 # clock mode 000 often: two-tone and async are bits 3 and 4 return v if reg == 14: return rnd.choice((0x00, 0xFF, 0x07, 0xC0, 0x08, rnd.randrange(256))) return rnd.randrange(256) def key_map(rnd: random.Random) -> str: default = rnd.choice((0, 0, 0, 2)) entries = [] for _ in range(rnd.choice((0, 1, 1, 2, 3))): code = rnd.choice((0x00, 0x10, 0x30, rnd.randrange(64))) entries.append(f"{code}:{rnd.randrange(4)}") return " ".join([f"K {default}", *entries]) def script(rnd: random.Random, cycles: int) -> list[str]: lines = [f"new {rnd.choice(('C012294', 'C012294', 'LINEAR'))} {rnd.choice((0, 0, 1))}"] if rnd.random() < 0.3: lines.append(f"power {rnd.choice((rnd.randrange(1 << 16), rnd.randrange(1 << 64)))}") state_every = rnd.choice((1, 7, 50, 300)) if rnd.random() < 0.8: # a usual start: running, some audio lines += ["w 15 3", "t 1"] for ch in range(4): lines += [f"w {2 * ch} {rnd.randrange(256)}", "t 1", f"w {2 * ch + 1} {audc(rnd)}", "t 1"] lines += [f"w 8 {rnd.randrange(256)}", "t 1", "w 9 0", "t 1"] used = 0 saved = False while used < cycles: kind = rnd.random() if kind < 0.40: reg = rnd.choice(WRITE_REGS) mirror = 16 * rnd.randrange(16) if rnd.random() < 0.1 else 0 # A3-A0 only (B1.1) lines += [f"w {reg + mirror} {value_for(rnd, reg)}"] elif kind < 0.62: lines.append(f"r {rnd.randrange(32)}") elif kind < 0.66: lines.append(f"P {rnd.choice((0, 0xFF, rnd.randrange(256)))}") elif kind < 0.675: targets = [rnd.choice(("-", rnd.randrange(30000), rnd.randrange(400))) for _ in range(8)] lines.append("PR " + " ".join(str(t) for t in targets)) elif kind < 0.70: lines.append(key_map(rnd)) elif kind < 0.715: lines.append(f"S {rnd.randrange(2)}") elif kind < 0.725: lines.append(f"B {rnd.randrange(2)}") elif kind < 0.735: lines.append("save") saved = True elif kind < 0.74 and saved: lines.append("load") elif kind < 0.745: lines.append(f"power {rnd.randrange(1 << 64)}" if rnd.random() < 0.7 else "power -") elif kind < 0.76: lines.append("take") # each operation is followed by its cycle's tick (B0.1), then a gap g = rnd.random() if g < 0.7: gap = rnd.randrange(1, 4) elif g < 0.95: gap = rnd.randrange(4, 40) elif g < 0.99: gap = rnd.randrange(40, 400) else: gap = rnd.randrange(400, 3000) lines.append(f"t {gap}") used += gap if rnd.random() < 1.0 / max(1, state_every // 20 + 1): lines.append("state") lines += ["state", "take"] return lines def scripted() -> dict[str, list[str]]: """Directed scripts that random streams reach rarely.""" out = {} # the slow pot scan to its end with the RC helper, twice (B5.x, B5.8) out["pots-rc"] = ["new C012294 0", "PR 11400 - 300 0 22000 5 - 999", "w 15 3", "t 1", "w 11 0", "t 26500", "state", "w 11 0", "t 26500", "state"] # fmt: skip # keyboard with debounce on and off, Shift/Control/Break, overrun (B6.x) out["keys"] = ["new C012294 0", "w 14 192", "t 1", "w 15 3", "t 1", "K 0 21:1 16:2 0:2", "t 20000", "state", "K 0 48:2 34:1", "t 20000", "w 10 0", "t 1", "state", "w 15 2", "t 1", "K 0 12:1 13:1 44:1 45:1", "t 20000", "state"] # fmt: skip # two-tone, async hold, clock modes, force break (B7.x) lines = ["new C012294 1", "w 8 64", "t 1", "w 0 0", "t 1", "w 2 255", "t 1"] for skctl in (0x0B, 0x8B, 0x13, 0x23, 0x33, 0x43, 0x53, 0x63, 0x73, 0x03): lines += [f"w 15 {skctl}", "t 1", "w 9 0", "t 3000", "state", "take"] out["serial"] = lines # seeded power-ons, several seeds, including the seed 0 and multi-word seeds lines = ["new C012294 0"] for seed in (0, 1, 2, 12345, (1 << 32) - 1, 1 << 32, (1 << 64) - 1): lines += [f"power {seed}", "state", "t 500", "state"] out["seeds"] = lines return out def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("out_dir", type=Path) parser.add_argument("--count", type=int, default=64) parser.add_argument("--seed", type=int, default=1) parser.add_argument("--cycles", type=int, default=20000) args = parser.parse_args() args.out_dir.mkdir(parents=True, exist_ok=True) for i in range(args.count): rnd = random.Random(args.seed * 1_000_003 + i) (args.out_dir / f"random-{i:03d}.txt").write_text("\n".join(script(rnd, args.cycles)) + "\n") for name, lines in scripted().items(): (args.out_dir / f"scripted-{name}.txt").write_text("\n".join(lines) + "\n") print(f"{args.count} random and {len(scripted())} scripted scripts in {args.out_dir}") return 0 if __name__ == "__main__": raise SystemExit(main())