"""Coleco's Final Test Cartridge on the board: its screens, its sound writes, and its controller test under scripted inputs. python scripts/final_test.py [--frames 3600] [--every 300] [--png-dir DIR] Prints, every ``--every`` frames, the screen's text (the cartridge's name bytes: space 0, digits 1-10, letters from $0B) and the sound writes since the last print, decoded (tone periods and attenuations per channel, noise control). The Technical Manual (VI-1): ROM and RAM, then the video test, then "All three of the sound generators are tested first, then the noise generator", then the hand-controller test, where "each function will blank out after that function has been selected". """ from __future__ import annotations import argparse import sys from pathlib import Path from colecovision_python import ColecoVision from colecovision_python.images import NO_INTRO, load_image from colecovision_python.video import write_png def screen_text(vdp) -> list[str]: base = (vdp.regs[2] & 0x0F) << 10 rows = [] for row in range(24): chars = [] for byte in vdp.vram[base + row * 32 : base + row * 32 + 32]: if byte == 0: chars.append(" ") elif byte <= 10: chars.append(str(byte - 1)) elif byte <= 0x0B + 25: chars.append(chr(ord("A") + byte - 0x0B)) else: chars.append("#") line = "".join(chars).rstrip() if line: rows.append(f"{row:2d} {line}") return rows def decode_sound(writes: list[tuple[int, int]]) -> list[str]: out, latched, period = [], 0, [0, 0, 0] for value, clock in writes: if value & 0x80: latched = (value >> 4) & 7 channel, volume = latched >> 1, latched & 1 if volume: out.append(f"{clock}: ch{channel} attenuation {value & 15}") elif channel < 3: period[channel] = (period[channel] & 0x3F0) | (value & 15) else: out.append(f"{clock}: noise control {value & 7}") elif (latched >> 1) < 3 and not latched & 1: ch = latched >> 1 period[ch] = ((value & 0x3F) << 4) | (period[ch] & 15) out.append(f"{clock}: ch{ch} period {period[ch]}") return out def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--frames", type=int, default=3600) parser.add_argument("--every", type=int, default=300) parser.add_argument("--png-dir", type=Path) parser.add_argument("--press-from", type=int, help="from this frame, press every function in turn") parser.add_argument("--all-writes", action="store_true") args = parser.parse_args() console = ColecoVision(load_image(NO_INTRO / "Final Test Cartridge (USA).zip"), io_log=[]) board = console.board functions = [("directions", d) for d in ("up", "right", "down", "left")] functions += [ ("diagonal", d) for d in (("up", "right"), ("down", "right"), ("down", "left"), ("up", "left")) ] functions += [("left_fire", True), ("right_fire", True)] functions += [("keys", k) for k in "0123456789*#"] schedule = [] # (frame, pad index, function) if args.press_from is not None: frame_no = args.press_from for pad in (0, 1): for function in functions: schedule.append((frame_no, pad, function)) frame_no += 20 for n in range(1, args.frames + 1): for pad in console.controllers: pad.directions, pad.keys, pad.left_fire, pad.right_fire = set(), set(), False, False for start, index, (name, value) in schedule: if start <= n < start + 10: pad = console.controllers[index] if name == "directions": pad.directions = {value} elif name == "diagonal": pad.directions = set(value) elif name == "keys": pad.keys = {value} else: setattr(pad, name, True) frame = console.run_frame() if n % args.every == 0: writes = [(v, s) for kind, port, v, s, _ in board.io_log if kind == "IW" and port >= 0xE0] board.io_log.clear() print(f"--- frame {n}") print("\n".join(screen_text(board.vdp))) print(f" {len(writes)} sound writes") for line in decode_sound(writes)[: None if args.all_writes else 24]: print(" " + line) if args.png_dir: args.png_dir.mkdir(parents=True, exist_ok=True) write_png(args.png_dir / f"final-{n:05d}.png", frame.pixels) return 0 if __name__ == "__main__": sys.exit(main())