# SPDX-License-Identifier: MIT """Rungs 2 and 3: CPU test ROMs in the minimal harness (arm7tdmi_python.harness). jsmolka's ``arm.gba`` and ``thumb.gba`` (rung 2) and alyosha-tas's ``arm/arm.gba``, ``thumb/blx.gba`` and ``thumb/cpy.gba`` (rung 3; blx and cpy are hardware tests of U19), its eight ``LDM/*`` tests of the LDM^ glitch (U7, run on a Game Boy Player), ``arm/Coprocessor_14`` (U17) and ``thumb/Push_no_regs`` (U4) end in a branch to itself with R12 = 0 when every test passed, or with the first failing test's number. A failure draws its number with BIOS division (SWI 6), which the harness has no BIOS for, so it ends in :class:`EnteredBIOS` carrying R12. """ from __future__ import annotations import json from pathlib import Path import pytest from arm7tdmi_python.__main__ import main from arm7tdmi_python.harness import EnteredBIOS, Harness REFERENCE = Path(__file__).resolve().parents[1] / "reference" / "src" ROMS = { "jsmolka arm": REFERENCE / "jsmolka-gba-tests" / "arm" / "arm.gba", "jsmolka thumb": REFERENCE / "jsmolka-gba-tests" / "thumb" / "thumb.gba", "alyosha-tas arm": REFERENCE / "alyosha-gba-tests" / "arm" / "arm.gba", "alyosha-tas thumb blx": REFERENCE / "alyosha-gba-tests" / "thumb" / "blx.gba", "alyosha-tas thumb cpy": REFERENCE / "alyosha-gba-tests" / "thumb" / "cpy.gba", "alyosha-tas arm Coprocessor_14": REFERENCE / "alyosha-gba-tests/arm/Coprocessor_14.gba", "alyosha-tas thumb Push_no_regs": REFERENCE / "alyosha-gba-tests/thumb/Push_no_regs.gba", **{ f"alyosha-tas LDM {name}": REFERENCE / "alyosha-gba-tests" / "LDM" / f"{name}.gba" for name in ( "LDM_ALU", "LDM_ALU_IMM", "LDM_ALU_Store", "LDM_Bx", "LDM_LD", "LDM_MUL_UL_32", "LDM_MUL_UL_SL", "LDM_Swap", ) }, } # fmt: skip @pytest.mark.parametrize("name", sorted(ROMS)) def test_rom_passes(name): path = ROMS[name] if not path.exists(): pytest.skip("run scripts/fetch_references.py") try: r12 = Harness(path.read_bytes()).run() except EnteredBIOS as error: pytest.fail(f"test {error.r12} failed ({error})") assert r12 == 0 def test_cli_and_trace(tmp_path): path = ROMS["jsmolka thumb"] if not path.exists(): pytest.skip("run scripts/fetch_references.py") trace = tmp_path / "trace.jsonl" assert main([str(path), "--trace", str(trace)]) == 0 lines = trace.read_text().splitlines() first = json.loads(lines[0]) assert first == { "cycle": 0, "kind": "read", "address": 0x08000000, "width": 32, "access": 2, "value": first["value"], } # fmt: skip assert any(json.loads(line)["kind"] == "internal" for line in lines) def test_branch_to_self_stops_and_bios_is_an_error(): rom = (0xEAFFFFFE).to_bytes(4, "little") harness = Harness(rom) assert harness.run() == 0 and harness.cycles == 2 with pytest.raises(EnteredBIOS): Harness((0xEF000000).to_bytes(4, "little")).run() # SWI 0