# SPDX-License-Identifier: MIT """Rung 2, the data ROM (U21): the generator reproduces a real chip's dump. Judge: a real chip's dump (MAME's software list names ``cx4.bin``, SHA-1 a002f4ef...; the hash is from MAME's CC0 software list) and the game's own IMMEDIATE ROM checksum, 054336h. No image is committed or needed: the SHA-1 is compared with the dump's recorded hash. With a BYOR image (``HG51B_DATA_ROM``, or one found through ``MAME_ROMPATH``), its bytes are compared too. """ from __future__ import annotations import hashlib import math import os import sys from pathlib import Path import pytest from hg51b_python import HG51B from hg51b_python.data_rom import ( BYTE_SUM, DUMP_SHA1, data_rom, from_bytes, load_data_rom, to_bytes, ) def test_generator_equals_the_dump() -> None: image = to_bytes(data_rom()) assert len(image) == 3072 assert hashlib.sha1(image).hexdigest() == DUMP_SHA1 assert sum(image) == BYTE_SUM == 0x054336 def test_landmarks() -> None: words = data_rom() assert words[0x000] == 0xFFFFFF and words[0x001] == 0x800000 and words[0x0FF] == 0x008080 assert words[0x100] == 0 and words[0x101] == 0x100000 and words[0x104] == 0x200000 assert words[0x200] == 0 and words[0x380] == 0xFFFFFF # sin 0, cos 0 saturated assert words[0x340] == 0x010000 # tan 45 deg exactly (float64 gives FFFFh) assert words[0x280] == 0 and words[0x2C0] == 0x800000 // 3 # asin(1/2) = 30 deg float_tan = math.floor(0x10000 * math.tan(math.pi * 64 / 256)) assert float_tan == 0xFFFF # why the generator is not float64 def test_round_trip_and_fresh_lists() -> None: words = data_rom() assert from_bytes(to_bytes(words)) == words words[0] = 0 assert data_rom()[0] == 0xFFFFFF def test_byor_image_is_checked(tmp_path: Path) -> None: good = tmp_path / "cx4.bin" good.write_bytes(to_bytes(data_rom())) # the generator's output, not a fetched image assert load_data_rom(good) == data_rom() bad = tmp_path / "bad.bin" bad.write_bytes(bytes(3072)) with pytest.raises(ValueError, match="SHA-1"): load_data_rom(bad) assert load_data_rom(bad, check=False) == [0] * 1024 with pytest.raises(ValueError): from_bytes(bytes(10)) def test_byor_image_matches_if_supplied() -> None: """A player's dump (``HG51B_DATA_ROM``, or found through ``MAME_ROMPATH`` by ``scripts/check_data_rom.py``) equals the computed table byte for byte.""" sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) from check_data_rom import find hit = find() if hit is None: pytest.skip("BYOR: set HG51B_DATA_ROM or MAME_ROMPATH to compare with a dump") assert hit[1] == to_bytes(data_rom()), hit[0] def test_core_accepts_words_and_rejects_bad_ones() -> None: words = data_rom() words[5] = 0x123456 cx = HG51B(lambda address: 0, data_rom=words) assert cx.data_rom[5] == 0x123456 with pytest.raises(ValueError): HG51B(lambda address: 0, data_rom=[0] * 10) with pytest.raises(ValueError): HG51B(lambda address: 0, data_rom=[0x1000000] * 1024)