# SPDX-License-Identifier: MIT """Schemes and identification (cartridge/).""" from __future__ import annotations import pytest from a2600_python.cartridge import UnsupportedCartridge, detect_super_chip, identify, load from a2600_python.cartridge.atari import Banked, Plain def banks(n: int) -> bytes: """n 4K banks, each filled with its own number.""" return b"".join(bytes([i]) * 0x1000 for i in range(n)) def test_2k_appears_twice(): cart = Plain(bytes(range(256)) * 8) assert cart.access(0x1000, 0, 0) == 0 assert cart.access(0x1800, 0, 0) == 0 assert cart.access(0x17FF, 0, 0) == 0xFF def test_cartridge_ignores_a12_low(): assert Plain(bytes(0x1000)).access(0x0080, 0x55, 0) is None @pytest.mark.parametrize( ("scheme", "count", "first"), [("F8", 2, 0x1FF8), ("F6", 4, 0x1FF6), ("F4", 8, 0x1FF4)] ) def test_hotspots_switch_on_any_access_and_read_the_new_bank(scheme, count, first): cart = Banked(banks(count), scheme) for bank in range(count): assert cart.access(first + bank, 0, 0) == bank # C20: the new bank's byte assert cart.access(0x1234, 0x99, 1) == bank # a write cycle switches as well cart.access(first, 0x00, 0) assert cart.bank == 0 def test_mame_power_on_banks(): assert Banked(banks(2), "F8").bank == 1 assert Banked(banks(4), "F6").bank == 0 assert Banked(banks(8), "F4").bank == 7 assert Banked(banks(2), "F8", start_bank=0).bank == 0 def test_super_chip_windows_and_write_on_read(): cart = Banked(banks(2), "F8", super_chip=True) assert cart.access(0x1005, 0x5A, 0) is None # write window: stores, drives nothing assert cart.access(0x1085, 0, 1) == 0x5A # read window cart.access(0x1005, 0x3C, 2) # a read of the write window stores the floating bus assert cart.access(0x1085, 0, 3) == 0x3C assert cart.access(0x1100, 0, 4) == cart.bank # ROM above the RAM def test_detector_by_size_and_super_chip(): plain = banks(4) assert identify(plain, mame_hash=__import__("pathlib").Path("/nonexistent"))[:3] == ( "F6", True, # every bank's first 256 bytes repeat one value: looks like SC "detector", ) varied = bytearray(banks(2)) varied[0x10] = 0x77 assert not detect_super_chip(bytes(varied)) with pytest.raises(UnsupportedCartridge): identify(bytes(10001), mame_hash=__import__("pathlib").Path("/nonexistent")) def test_explicit_scheme_wins(): loaded = load(banks(2), scheme="F8SC") assert loaded.scheme == "F8SC" assert loaded.source == "explicit"