# SPDX-License-Identifier: MIT """The board's decoders, cycle lengths, INPTCTRL and RAM shadows ([MT84] Figure 2, [SG]).""" from __future__ import annotations import pytest from a7800_python.board import EXT, LOCK, MEN, Board from a7800_python.cartridge.connector import Linear from a7800_python.machine import NTSC def board(control: int = MEN | EXT | LOCK) -> Board: rom = bytes(range(256)) * 0x40 # 16 KiB at $C000 b = Board(NTSC, Linear(rom), bytes([0xEA]) * 0x1000) b.inptctrl = control b.locked = bool(control & LOCK) return b @pytest.mark.parametrize( ("address", "slow"), [ (0x0000, True), (0x001F, True), (0x0100, True), (0x0300, True), # TIA and mirrors (0x0020, False), (0x003F, False), (0x0120, False), # MARIA (0x0040, False), (0x01FF, False), # RAM shadows (0x0280, True), (0x02FF, True), (0x0380, True), # 6532 ports (0x0480, True), (0x05FF, True), # 6532 RAM (0x0400, False), (0x0450, False), (0x0500, False), # cartridge devices (0x1800, False), (0x2000, False), (0x4000, False), (0xFFFF, False), ], ) # fmt: skip def test_slow_rows_of_figure_2(address, slow): assert board().is_slow(address) is slow def test_every_cycle_is_slow_under_the_2600_decoder(): b = board(control=0) assert all(b.is_slow(a) for a in (0x0000, 0x0020, 0x1800, 0x4000, 0xF000)) def test_cycle_lengths_in_maria_clocks(): b = board() before = b.clocks b.read_byte(0x2000) assert b.clocks - before == 4 b.read_byte(0x0008) assert b.clocks - before == 10 b.write_byte(0x0281, 0) assert b.clocks - before == 16 assert b.periods == 3 # one Φ2 period per cycle, long or short def test_ram_shadows_and_blocks(): b = board() b.write_byte(0x0080, 0x5A) assert b.read_byte(0x2080) == 0x5A b.write_byte(0x2150, 0xA5) assert b.read_byte(0x0150) == 0xA5 b.write_byte(0x1800, 0x11) b.write_byte(0x2000, 0x99) assert b.read_byte(0x1800) == 0x11 # RAM1 and RAM0 are separate chips b.write_byte(0x2400, 0x22) assert b.read_byte(0x2C00) == 0x22 # [SG] row 7 (C15) def test_ram_is_off_while_maria_is_disabled(): b = board(control=EXT) b.data_bus = 0x77 b.write_byte(0x1900, 0x33) # A12 = 1 under the 2600 decoder: the cartridge's assert b.ram[0x100] == 0 def test_bios_over_the_upper_half_until_ext(): b = board(control=MEN) assert b.read_byte(0xF000) == 0xEA assert b.read_byte(0xC000) == 0xEA # the 4 KiB ROM repeats (U19) b.inptctrl = MEN | EXT assert b.read_byte(0xC001) == 1 def test_inptctrl_takes_tia_writes_until_locked_and_counts_for_halt(): b = Board(NTSC, Linear(bytes(0x4000)), bytes(0x1000)) assert b.inptctrl == 0 and not b.halt_enabled b.write_byte(0x0001, MEN) assert b.inptctrl == MEN and not b.halt_enabled b.write_byte(0x0005, MEN | EXT) assert b.halt_enabled b.write_byte(0x0001, MEN | EXT | LOCK) assert b.locked b.write_byte(0x0001, 0) assert b.inptctrl == MEN | EXT | LOCK def test_seeded_fault_ignore_lock_changes_the_latch(): b = Board(NTSC, Linear(bytes(0x4000)), bytes(0x1000)) b.faults.add("ignore-lock") b.write_byte(0x0001, MEN | EXT | LOCK) b.write_byte(0x0001, 0) assert b.inptctrl == 0 def test_tia_reads_drive_two_bits_and_the_rest_float(): b = board() b.read_byte(0xC0AA) # leaves $AA on the bus value = b.read_byte(0x000C) # INPT4, pulled up: D7 set assert value & 0x3F == 0xAA & 0x3F