"""Small helpers shared by the tests: register and address writes as a CPU makes them.""" from __future__ import annotations from tms9918_python import TMS9918 LINE = 684 # master clocks a line SLOT = 4 # master clocks a memory cycle def set_reg(vdp: TMS9918, reg: int, value: int) -> None: vdp.write(1, value) vdp.write(1, 0x80 | reg) def set_write_address(vdp: TMS9918, addr: int) -> None: vdp.write(1, addr & 0xFF) vdp.write(1, 0x40 | (addr >> 8) & 0x3F) def set_read_address(vdp: TMS9918, addr: int) -> None: vdp.write(1, addr & 0xFF) vdp.write(1, (addr >> 8) & 0x3F) def write_block(vdp: TMS9918, addr: int, data: bytes) -> None: set_write_address(vdp, addr) for b in data: vdp.write(0, b) def run_to(vdp: TMS9918, line: int, dot: int) -> None: """Run forward to the next time (strictly later) the beam is at (line, dot).""" here = vdp.line * 684 + vdp.slot * 4 + vdp._phase there = line * 684 + dot * 2 frame = vdp.lines * 684 vdp.run((there - here - 1) % frame + 1) def instant(r1: int | None = 0x80, **kw) -> TMS9918: """A chip that serves CPU accesses at once (windows off), for protocol tests. R1 is set to ``r1`` first (16K mode by default, so VRAM indices equal addresses; after reset the chip is in 4K mode, where MacDonald's mapping scrambles them). """ from tms9918_python import TMS9928A vdp = TMS9918(kw.pop("variant", TMS9928A), kw.pop("vram_size", 16384), windows=False, **kw) if r1 is not None: set_reg(vdp, 1, r1) return vdp