# SPDX-License-Identifier: MIT """The board's timeline, decoder and HALT/RDY placement, on a synthetic BIOS.""" from __future__ import annotations import itertools from helpers import fake_board def test_frame_is_29868_cycles(): board = fake_board() board.run_frame() marks = [] for _ in range(3): start = board.cycles board.run_frame() marks.append(board.cycles - start) # A frame mark falls inside an instruction, so each run overshoots by at # most one instruction; the total over frames is exact. assert abs(sum(marks) - 3 * 29868) <= 7 assert board.antic.vcount == 0 def test_cycles_per_frame_exact_on_the_antic_counter(): board = fake_board() seen = [] board.on_frame = lambda b: seen.append(b.cycles) while len(seen) < 4: board.step() assert [b - a for a, b in itertools.pairwise(seen)] == [29868, 29868, 29868] def test_memory_map_and_floating_bus(): board = fake_board() board._write(0x1234, 0x5A) assert board._read(0x1234) == 0x5A assert board._read(0x0000 + 0x4000 - 1) == 0 # RAM top board.data_bus = 0x77 assert board._read(0xD000) == 0x77 # unmapped: floating bus (U5) assert board._read(0xD40B) == board.antic.read(0x0B) # VCOUNT assert board._read(0xD4F5) == 0xFF # ANTIC unassigned reads $FF assert board._read(0xF000) == board._read(0xF800) # C8 mirror default assert board._read(0xC014) == 0x0F # NTSC GTIA PAL register def test_wsync_stretches_reads_until_cycle_105(): # STA $D40A ; NOP ; NOP ; JMP * program = bytes((0x8D, 0x0A, 0xD4, 0xEA, 0xEA, 0x4C, 0x05, 0xF8)) board = fake_board(program) log = [] board.bus_observers.append(lambda k, a, v, c: log.append((k, a, board.antic.hcount))) board.step() # reset board.step() # STA WSYNC board.step() # NOP (first cycle runs, then RDY holds the second) kinds = [k for k, _, _ in log] assert "S" in kinds first_read_after = next(c for k, a, c in log if k == "R" and a == 0xF804) assert first_read_after == 105 def test_dli_less_frame_has_one_vbi_nmi(): # Enable VBI: LDA #$40 ; STA NMIEN ; JMP * (NMI vector -> $F800 restarts it) program = bytes((0xA9, 0x40, 0x8D, 0x0E, 0xD4, 0x4C, 0x05, 0xF8)) board = fake_board(program) fired = [] original = board.cpu.request_non_maskable_interrupt def spy(): fired.append((board.antic.vcount, board.antic.hcount)) original() board.cpu.request_non_maskable_interrupt = spy board.run_frame() board.run_frame() assert fired and all(pos == (248, 9) for pos in fired) # ended cycle 8 of line 248