# SPDX-License-Identifier: MIT """The loader's front door on synthetic payloads (no commercial image needed).""" from __future__ import annotations import io import zipfile import pytest from a5200_python.cartridge import CartridgeError, load_cartridge, make_car from a5200_python.cartridge.mappings import BountyBob, Cart4K, Cart8K, Cart16KTwoChip, SuperCart def _payload(size: int, start: int = 0x8000) -> bytes: data = bytearray((i * 7 + 3) & 0xFF for i in range(size)) data[-2] = start & 0xFF data[-1] = start >> 8 return bytes(data) @pytest.mark.parametrize(("size", "mapping"), [(4096, "4k"), (8192, "8k"), (32768, "32k")]) def test_size_settles_the_class(size, mapping): cart = load_cartridge(_payload(size)) assert (cart.mapping, cart.mapping_from) == (mapping, "size") def test_car_header_and_raw_dump_hash_alike(): raw = _payload(16384) car = load_cartridge(make_car(raw, 6)) assert car.mapping == "16k-two" and car.mapping_from == "car-header" assert car.sha1 == load_cartridge(raw, mapping="16k-two").sha1 assert car.car_checksum_ok def test_type_159_matches_type_7(): payload = _payload(40960) alternative = payload[0x8000:] + payload[:0x8000] assert load_cartridge(make_car(alternative, 159)).sha1 == load_cartridge(payload).sha1 def test_zip_member_is_read_through_memory(): buffer = io.BytesIO() with zipfile.ZipFile(buffer, "w") as archive: archive.writestr("game.a52", _payload(8192)) assert load_cartridge(buffer.getvalue()).mapping == "8k" def test_computer_cart_type_is_refused(): with pytest.raises(CartridgeError, match="8-bit computer"): load_cartridge(make_car(_payload(8192), 1)) def test_window_layouts(): four = Cart4K(_payload(4096)) assert four.read(0x4000) is None assert four.read(0x8000) == four.read(0x9000) == four.read(0xB000) eight = Cart8K(_payload(8192)) assert eight.read(0x8123) == eight.read(0xA123) two = Cart16KTwoChip(_payload(16384)) assert two.read(0x4000) == two.read(0x6000) and two.read(0x8000) == two.read(0xA000) def test_bounty_bob_banks_switch_on_access(): payload = bytes(range(256)) * 160 cart = BountyBob(payload) cart.access(0x4FF8) assert cart.bank[0] == 2 cart.read(0x5FF7) assert cart.bank[1] == 1 def _with_rows(monkeypatch, payload: bytes, seed: str | None, reading: str | None): """A copy of the real table with a seed row and/or an observed-boot row for ``payload``.""" import copy import hashlib from a5200_python import cartridge rows = copy.deepcopy(cartridge.table()) sha1 = hashlib.sha1(payload).hexdigest() if seed is not None: rows["sha1"][sha1] = {"title": "seeded", "mapping": seed, "source": "test seed"} if reading is not None: row = { "title": "measured", "reading": reading, "kind": "observed-boot", "evidence": "test.png", "source": "measured here (test)", } if reading.startswith("16k"): row["mapping"] = reading rows.setdefault("measured", {})[sha1] = row monkeypatch.setattr(cartridge, "_TABLE", rows) def test_measured_row_fills_a_silent_seed(monkeypatch): payload = _payload(16384, 0x6000) _with_rows(monkeypatch, payload, None, "16k-one") cart = load_cartridge(payload) assert (cart.mapping, cart.mapping_from) == ("16k-one", "measured") assert cart.title == "measured" def test_measured_row_agreeing_with_the_seed_leaves_the_seed(monkeypatch): payload = _payload(16384, 0x6000) _with_rows(monkeypatch, payload, "16k-two", "16k-two") cart = load_cartridge(payload) assert (cart.mapping, cart.mapping_from) == ("16k-two", "table") assert any("agrees" in n for n in cart.notes) def test_measured_row_contradicting_the_seed_wins_and_is_reported(monkeypatch): payload = _payload(16384, 0x6000) _with_rows(monkeypatch, payload, "16k-two", "16k-one") cart = load_cartridge(payload) assert (cart.mapping, cart.mapping_from) == ("16k-one", "measured") assert any(n.startswith("CONFLICT") for n in cart.notes) assert cart.title == "seeded" # names still come from the seed def test_measured_both_or_neither_leaves_the_walk(monkeypatch): payload = _payload(16384, 0x6000) _with_rows(monkeypatch, payload, None, "both") try: cart = load_cartridge(payload) except CartridgeError as error: assert "static walk" in str(error) # the walk abstained: refused, as decision 3 says else: assert cart.mapping_from == "guess" assert any("both readings" in n for n in cart.notes) def test_pam_diag_v20_has_a_measured_one_chip_row(): from a5200_python.cartridge import table row = table()["measured"]["5cc3b4245faf01c30f77ac20b0b4940f4f8f83d8"] assert row["reading"] == row["mapping"] == "16k-one" and row["kind"] == "observed-boot" seed = table()["sha1"]["5cc3b4245faf01c30f77ac20b0b4940f4f8f83d8"] assert seed["mapping"] == "16k-two" # the conflict the loader reports def test_observed_boot_rows_for_the_39_unknown_dumps(): from a5200_python.cartridge import table rows = table()["measured"] observed = [r for r in rows.values() if r.get("walk") is not None] assert len(observed) == 39 assert all(r["kind"] == "observed-boot" and r["evidence"] for r in observed) counts = {m: sum(r["reading"] == m for r in observed) for m in ("16k-one", "16k-two")} assert counts == {"16k-one": 13, "16k-two": 26} # The walk's one contradiction: RealSports Soccer (Beta) boots only as two chips. soccer = rows["e4f020df49866079c221dedbd91f8fe74477cfcd"] assert (soccer["walk"], soccer["reading"]) == ("16k-one", "16k-two") def test_supercart_hotspots(): payload = bytearray(0x20000) for bank in range(4): payload[bank * 0x8000] = bank cart = SuperCart(bytes(payload)) assert cart.bank == 3 cart.read(0xBFD4) # bank bits 0-1 from address bits 2-3 = 1 assert cart.read(0x4000) == 1