# SPDX-License-Identifier: MIT """HMOVE's window edges, as the die shows them (hmove_cycles trace; timing.md "HMOVE").""" from __future__ import annotations from a2600_python.board import Board from a2600_python.cartridge import load from a2600_python.machine import VCS_NTSC HMOVE = 0x2A RHB_END, LRHB_END = 136, 152 # the step HBLANK ends on, without and with the comb def hblank_ends_after_hmove_on(cycle: int) -> tuple[int, int]: """(this line's, the next line's) step where HBLANK ends, for an HMOVE written on ``cycle`` of a line (cycle k writes on step 6k + 4).""" board = Board(VCS_NTSC, load(bytes(0x1000), scheme="4K").cartridge) target = 6 * cycle + 4 while (board.tia.step_in_line + 6) % 456 != target: board.read_byte(0x1000) tia = board.tia line = tia.line + (1 if target < tia.step_in_line else 0) board.write_byte(HMOVE, 0) ends = {} was_blank = True while tia.line <= line + 1: tia.step() if was_blank and not tia.hblank: ends.setdefault(tia.line, tia.step_in_line) was_blank = tia.hblank return ends.get(line, -1), ends.get(line + 1, -1) def test_hmove_on_cycle_19_still_gets_the_comb(): assert hblank_ends_after_hmove_on(19)[0] == LRHB_END def test_hmove_on_cycle_20_is_too_late_for_the_comb(): assert hblank_ends_after_hmove_on(20)[0] == RHB_END def test_hmove_on_cycle_73_is_cleared_by_the_wrap(): """Towers' and Mott's trick: motion without the comb.""" assert hblank_ends_after_hmove_on(73)[1] == RHB_END def test_hmove_on_cycle_74_wins_the_race_with_the_wrap(): """Seven consoles (C27); Sim2600's netlist clears it instead.""" assert hblank_ends_after_hmove_on(74)[1] == LRHB_END def test_hmove_on_cycle_75_extends_the_next_line(): assert hblank_ends_after_hmove_on(75)[1] == LRHB_END def playfield_line(hmove: bool) -> bytes: """The visible line after a playfield of PF1 = $0F, PF2 = 0 is set, with or without an HMOVE at the line's start (TIA.pixels, 160 clocks).""" 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 ((0x08, 0x1E), (0x0E, 0x0F), (0x0F, 0x00)): # COLUPF, PF1, PF2 board.write_byte(register, value) while (tia.step_in_line + 6) % 456 != 4: # the next cycle is a line's cycle 0 board.read_byte(0x1000) if hmove: board.write_byte(HMOVE, 0) # the comb on this line count = len(lines) while len(lines) < count + 2: board.read_byte(0x1000) return lines[count] def test_the_comb_hides_the_playfield_but_does_not_move_it(): """C29: the playfield counter starts at RHB even when HBLANK lasts to LRHB (die trace hmove_playfield; Pitfall!'s trees).""" plain, combed = playfield_line(False), playfield_line(True) lit = [x for x, v in enumerate(plain) if v == 0x1F] assert lit == list(range(32, 48)) + list(range(112, 128)) # PF1 D3-D0, both halves assert combed[:8] == bytes(8) # blanked assert combed[8:] == plain[8:]