# SPDX-License-Identifier: MIT """Picture rules the milestone 4 detectors found, as the die settled them.""" from __future__ import annotations from a2600_python.board import Board from a2600_python.cartridge import load from a2600_python.machine import VCS_NTSC COLUP0, COLUP1, COLUPF, COLUBK, CTRLPF, RESBL, ENABL = 0x06, 0x07, 0x08, 0x09, 0x0A, 0x14, 0x1F def ball_line(ctrlpf: int) -> bytes: """A visible line with the ball (4 clocks) in the left half, no playfield.""" board = Board(VCS_NTSC, load(bytes(0x1000), scheme="4K").cartridge) tia = board.tia lines: list[bytes] = [] tia.on_line = lambda pixels: lines.append(bytes(pixels[68:])) for register, value in ((COLUP0, 0x1E), (COLUP1, 0x7C), (COLUPF, 0x4E), (COLUBK, 0x84), (CTRLPF, ctrlpf | 0x20), (ENABL, 0x02)): # fmt: skip board.write_byte(register, value) while (tia.step_in_line + 6) % 456 != 4 + 6 * 40: # cycle 40: the left half board.read_byte(0x1000) board.write_byte(RESBL, 0) count = len(lines) while len(lines) < count + 2: board.read_byte(0x1000) return lines[-1] def test_score_mode_leaves_the_ball_colupf(): """C32: SCORE colours the playfield with the half's player colour, not the ball (die trace ball_score; Alpha Beam with Ernie).""" plain, score = ball_line(0x00), ball_line(0x02) assert {v for v in plain if v != 0x85} == {0x4F} assert score == plain GRP0, NUSIZ0, RESP0, HMOVE = 0x1B, 0x04, 0x10, 0x2A def wrapped_tail(hmove: bool) -> bytes: """The first 16 visible clocks of the line after a 4x player ran past the end of the line before, with or without an HMOVE on cycle 2.""" board = Board(VCS_NTSC, load(bytes(0x1000), scheme="4K").cartridge) tia = board.tia lines: list[bytes] = [] tia.on_line = lambda pixels: lines.append(bytes(pixels[68:])) for register, value in ((COLUP0, 0x0E), (COLUBK, 0x84), (GRP0, 0xFF), (NUSIZ0, 0x07)): board.write_byte(register, value) def at_cycle(cycle: int) -> None: while (tia.step_in_line + 6) % 456 != 4 + 6 * cycle: board.read_byte(0x1000) at_cycle(66) board.write_byte(RESP0, 0) # the copy starts near clock 135 from the next line count = len(lines) while len(lines) < count + 2: # the line the copy runs past the end of board.read_byte(0x1000) at_cycle(2) if hmove: board.write_byte(HMOVE, 0) # HMP0 is 0: the copy keeps its place count = len(lines) while len(lines) < count + 1: board.read_byte(0x1000) return lines[-1][:16] def test_hmove_gobbles_the_wrapped_tail(): """C34: under HMOVE's comb the stuffed pulses shift out a stretched player's wrapped pixels instead of leaving them for after the comb (Towers; die trace hmove_wrap; Sea Hawk).""" plain, moved = wrapped_tail(False), wrapped_tail(True) assert plain[:11] == bytes([0x0F] * 11) # the tail drawn from the line's start assert moved[:8] == bytes(8) # the comb assert moved[8:] == plain[8:] # its eight clocks ate the tail's first eight pixels NUSIZ1, RESP1, GRP1, ENAM1, RESMP1 = 0x05, 0x11, 0x1C, 0x1E, 0x29 def released_missile(nusiz: int) -> tuple[list[int], list[int]]: """Player 1's visible clocks while missile 1 is locked to it, then the missile's alone after RESMP1 lets it go on cycle 26 and GRP1 is cleared.""" board = Board(VCS_NTSC, load(bytes(0x1000), scheme="4K").cartridge) tia = board.tia lines: list[bytes] = [] tia.on_line = lambda pixels: lines.append(bytes(pixels[68:])) for register, value in ( (COLUP1, 0x0E), (COLUBK, 0x84), (GRP1, 0xFF), (ENAM1, 0x02), (NUSIZ1, nusiz), ): board.write_byte(register, value) def at_cycle(cycle: int) -> None: while (tia.step_in_line + 6) % 456 != 4 + 6 * cycle: board.read_byte(0x1000) def next_line() -> bytes: count = len(lines) while len(lines) < count + 1: board.read_byte(0x1000) return lines[-1] at_cycle(47) board.write_byte(RESP1, 0) board.write_byte(RESMP1, 0x02) next_line() locked = next_line() at_cycle(26) board.write_byte(RESMP1, 0x00) board.write_byte(GRP1, 0x00) next_line() alone = next_line() drawn = 0x0F return [i for i, v in enumerate(locked) if v == drawn], [ i for i, v in enumerate(alone) if v == drawn ] def test_a_released_missile_starts_where_the_die_puts_it(): """C38: RESMP holds the missile from the step its player's scan counter shows 1, with no delay, so a released missile starts four clocks into a 1x player and six into a 2x one (die trace resmp_release; Frostbite).""" player, missile = released_missile(0x30) # 1x player, 8-clock missile assert missile == list(range(player[0] + 4, player[0] + 12)) player, missile = released_missile(0x05) # 2x player, 1-clock missile assert missile == [player[0] + 6]