# SPDX-License-Identifier: MIT """The board: one bus callback per cycle, 13-bit decode, the floating bus, WSYNC (board.py).""" from __future__ import annotations from conftest import system_running from a2600_python import System from a2600_python.board import Board from a2600_python.cartridge import load from a2600_python.cartridge.connector import Cartridge from a2600_python.machine import VCS_NTSC # Hand-assembled. NOP = b"\xea" STA_WSYNC = b"\x85\x02" # STA $02 LDA_IMM = b"\xa9" def test_reset_lands_on_the_vector_with_the_power_on_registers(): system = system_running(NOP) assert system.cpu.pc == 0xF000 assert system.cpu.s == 0xFD assert system.cycles == 7 assert system.cpu.x == VCS_NTSC.power_on.x def test_addresses_are_masked_to_13_bits(): # LDA $F080 reads RIOT RAM $80 through its mirror (A12 = 1 would be the cart: # $F080 masks to $1080, the cartridge; $E080 masks to $0080, RAM). program = LDA_IMM + b"\x42" + b"\x85\x80" + b"\xad\x80\xe0" # LDA #$42; STA $80; LDA $E080 system = system_running(program) for _ in range(3): system.step() assert system.cpu.a == 0x42 def test_tia_reads_drive_only_d7_and_d6(): # LDA $0C (INPT4, fire released: D7 = 1): D5-D0 float, and the last byte # on the bus was the operand $0C. system = system_running(b"\xa5\x0c") system.step() assert system.cpu.a == 0x80 | 0x0C def test_fire_button_reads_low_when_pressed(): system = system_running(b"\xa5\x0c") system.board.tia.fire[0] = True system.step() assert system.cpu.a & 0x80 == 0 def board_at(step: int) -> Board: """A board whose next CPU cycle's access lands on line step ``step``.""" board = Board(VCS_NTSC, load(bytes(0x1000), scheme="4K").cartridge) while (board.tia.step_in_line + 6) % 456 != step: board.read_byte(0x1000) return board def halted_after_wsync_at(step: int) -> tuple[int, int]: """(halted cycles, step of the next read's access) for a WSYNC landing on ``step``.""" board = board_at(step) board.write_byte(0x02, 0) board.read_byte(0x1000) return board.halted_cycles, board.tia.step_in_line def test_wsync_halts_reads_until_the_release_at_step_454(): # A write at step 16: halted until the cycle ending at 454 releases RDY. halted, landed = halted_after_wsync_at(16) assert landed == 4 # the read is the first cycle of the next line (steps 455-4) assert halted == (454 - 16) // 6 def test_wsync_one_cycle_before_the_release_halts_one_cycle(): assert halted_after_wsync_at(448) == (1, 4) def test_wsync_inside_the_release_pulse_is_lost(): """tia-fpga "The bus": a strobe entirely inside the release pulse is lost.""" assert halted_after_wsync_at(454) == (0, 4) def test_wsync_overlapping_the_pulse_takes_once_it_ends(): halted, landed = halted_after_wsync_at(4) assert landed == 4 assert halted == 75 # a whole line less the write's cycle def test_every_cycle_reaches_the_cartridge(): seen = [] class Spy(Cartridge): name = "spy" def __init__(self, image): self.inner = load(image, scheme="4K").cartridge def access(self, address, value, t, write=False): seen.append((address, t)) return self.inner.access(address, value, t, write) from conftest import image_4k system = System(Spy(image_4k(b"\x85\x80" + NOP))) system.step() system.step() # STA $80: the RIOT write is seen by the cartridge too assert (0x0080, system.cycles - 1) in seen assert [t for _, t in seen] == list(range(len(seen))) def test_power_on_phase_must_be_a_multiple_of_three(): from dataclasses import replace import pytest machine = replace(VCS_NTSC, power_on=replace(VCS_NTSC.power_on, hclock=1)) with pytest.raises(ValueError): Board(machine, load(bytes(0x1000), scheme="4K").cartridge)