# SPDX-License-Identifier: MIT """The one loader: headers, the table, trimming, the refusal, the converter.""" from __future__ import annotations import pytest from conftest import LICENSED, read_zip from a7800_python.cartridge import a78, formats, table from a7800_python.cartridge.description import CartDescription, Refused def test_header_round_trip(): desc = CartDescription(payload=bytes(0x20000), scheme="supergame", at_4000="pokey", pokey=(0x4000,), title="Commando", region="ntsc") # fmt: skip header = a78.parse(a78.build(desc) + desc.payload) assert header.fields["scheme"] == "supergame" assert header.fields["pokey"] == (0x4000,) assert header.size == 0x20000 and header.title == "Commando" def test_v4_fields_win_over_v3(): desc = CartDescription(payload=bytes(0x8000), scheme="linear", pokey=(0x450,)) raw = bytearray(a78.build(desc)) raw[54] = 0x01 # v3 bit 0: POKEY at $4000; v4's audio field still says @450 assert a78.parse(bytes(raw)).fields["pokey"] == (0x450,) def test_contradictory_v3_bits_are_refused_by_name(): raw = bytearray(a78.build(CartDescription(payload=bytes(0x8000)))) raw[0] = 3 raw[53], raw[54] = 0x03, 0x00 # activision + absolute with pytest.raises(a78.HeaderError, match="more than one bank scheme"): a78.parse(bytes(raw)) def test_unknown_large_headerless_image_is_refused_with_its_hash(): payload = bytes(range(256)) * 0x400 # 256 KiB, in no table with pytest.raises(Refused) as caught: formats.load(payload, use_bios_test=False) assert caught.value.sha256 in str(caught.value) def test_small_unknown_image_is_typed_by_size_and_labelled(): desc = formats.load(bytes(range(256)) * 0x40, use_bios_test=False) assert desc.scheme == "linear" and desc.certainty == "size" def test_trimming_and_bankset_halves(): a = bytes(range(256)) * 0x40 assert formats.match_payloads(a, b"\xff" * 0x4000 + a) == "matched after trimming" halves = bytes(0x4000) + a + bytes(0x4000) + a assert formats.match_payloads(a + a, halves, bankset=True).startswith("matched after trimming") assert formats.match_payloads(a, bytes(0x8000)) is None def test_softlist_row_by_bytes(): payload = read_zip(LICENSED / "Commando (USA).zip") row = table.lookup(payload) assert row is not None and row.source == "softlist" assert row.scheme == "supergame" and row.pokey == (0x4000,) desc = formats.load(LICENSED / "Commando (USA).zip") assert desc.provenance["scheme"] == "softlist" and desc.certainty == "softlist" def test_header_and_table_conflict_is_reported_and_the_table_wins(): payload = read_zip(LICENSED / "Commando (USA).zip") wrong = CartDescription(payload=payload, scheme="supergame", title="Commando") # no POKEY desc = formats.load(a78.build(wrong) + payload) assert desc.pokey == (0x4000,) assert any("differ on pokey" in note for note in desc.notes) trusted = formats.load(a78.build(wrong) + payload, trust="header") assert trusted.pokey == () def test_a78_and_bin_resolve_to_the_same_key(): payload = read_zip(LICENSED / "Asteroids (USA).zip") headered = formats.load(a78.build(formats.load(payload)) + payload) assert headered.sha256 == formats.load(payload).sha256 def test_overrides_parse(): assert formats.parse_as("supergame,ram@4000,pokey@450") == { "scheme": "supergame", "at_4000": "ram16", "pokey": (0x450,), } # fmt: skip