# SPDX-License-Identifier: MIT """The RIOT timer's edges against console results: the timer ROMs of Stella #108 and #606. Each ROM measures the timer and keeps what it shows in RAM, so the test reads RAM after two frames (every value is in place after the first) instead of the screen. The expected values are the consoles' (docs/validation.md "Hardware-corrected"; the screens match Stella's too, docs/validation.md "Milestone 3"). The ROMs are read in place from the fetched issue attachments and the tests skip without them (``scripts/fetch_references.py``). """ from __future__ import annotations import zipfile from itertools import pairwise from pathlib import Path import pytest from a2600_python import System ISSUES = Path(__file__).resolve().parent / "test_roms" / "stella-issues" TIMER_108 = ISSUES / "108-078-TIMER.zip" TIMER_606 = ISSUES / "606-129-TimerTest.zip" ROWS = 24 def ram_after(archive: Path, member: str, frames: int = 2) -> bytes: if not archive.exists(): pytest.skip("Stella issue ROMs not fetched: python scripts/fetch_references.py") with zipfile.ZipFile(archive) as z: system = System.from_rom(z.read(member)) system.run_frames(frames) return bytes(system.board.riot.ram) def test_test1_bas_shows_the_console_value(): """#108: test1.bas shows 128246 on a console (its batari Basic score, BCD at $93).""" ram = ram_after(TIMER_108, "TIMER/test1.bas.bin") assert ram[0x13:0x16] == bytes.fromhex("128246") def test_timint_with_delay_shows_the_console_row(): """#108: testTIMINT_withDelay reads before $00, after $80, delay +00 on a console.""" ram = ram_after(TIMER_108, "TIMER/testTIMINT_withDelay.bin") before, after, delay = ram[0x05], ram[0x06], ram[0x03] assert (before, after, delay) == (0x00, 0x80, 0x00) SET_606 = [ f"TimerTest_{interval}_{access}_{wrap}_WRAP.bin" for interval in ("TIM8T", "TIM64T", "T1024T") for access in ("READ", "WRITE") for wrap in ("1st", "2nd") ] def check_606(member: str) -> None: """#606: the timer accessed one cycle before the wrap (left column), on it (middle) and one after (right), then read every 5 cycles. ale-79's console result: the access on the wrap leaves the flag set, so the middle column counts every cycle, where one cycle later (right) the access clears it and the timer is back at its interval. (The left column depends on the access: a read just before the first wrap sees the wrap come next cycle; a write reloads the timer.)""" ram = ram_after(TIMER_606, member) middle, right = ram[ROWS : 2 * ROWS], ram[2 * ROWS : 3 * ROWS] assert all((a - b) & 0xFF == 5 for a, b in pairwise(middle)), middle.hex() assert all((a - b) & 0xFF <= 1 for a, b in pairwise(right)), right.hex() def test_606_tim8t_read_on_the_first_wrap(): check_606("TimerTest_TIM8T_READ_1st_WRAP.bin") @pytest.mark.slow @pytest.mark.parametrize("member", SET_606) def test_606_set(member): check_606(member)