# SPDX-License-Identifier: MIT """The contested switches after the Wave 2 rewrite (2026-11-19): each default against byuu's model (ares, ISC), each other position against what a closed-box Mesen2 run showed (OBSERVED, ``scripts/cx4_probe.py``; the probe rows are quoted in docs/undocumented- behavior.md). C2, C3 and C9 have no switch: their Mesen2 observations are recorded, not modelled.""" from __future__ import annotations import pytest from rig import Rig from hg51b_python import HG51B HALT = 0xFC00 def run(words: list[int], *, core=HG51B, a: int = 0, r: dict[int, int] | None = None): rig = Rig(core) rig.cx.a = a for index, value in (r or {}).items(): rig.cx.r[index] = value return rig.execute(words) # ----- C1: shift counts 24-31 -------------------------------------------------------------- class ShiftObserved(HG51B): SHIFT_LIMIT = "mesen2-observed" @pytest.mark.parametrize("op", [0xC400, 0xCC00, 0xDC00]) # SHR, ASR, SHL by immediate def test_shift_by_24_ares_clears_observed_keeps(op: int) -> None: # ares: ``if(s > 24) s = 0``, so 24 shifts a 24-bit value out (ASR of a positive one too) assert run([op | 24], a=0x7FFFFF).a == 0 cx = run([op | 24], a=0x800000, core=ShiftObserved) # OBSERVED: A unchanged, N set assert cx.a == 0x800000 and cx.n and not cx.z @pytest.mark.parametrize("count", range(25, 32)) def test_shift_by_25_to_31_leaves_a_in_both(count: int) -> None: for core in (HG51B, ShiftObserved): assert run([0xC400 | count], a=0x123456, core=core).a == 0x123456 def test_shift_limit_rejects_unknown_rules() -> None: class Bad(HG51B): SHIFT_LIMIT = "mod32" # removed in Wave 2 (MiSTer, no closed-box route) with pytest.raises(ValueError): run([0xC400 | 24], core=Bad) # ----- C2: A< None: cx = run([0x9600], a=0x800000) # SUB A<<8,#0: 80000000h cut to 0 assert cx.a == 0 and cx.z and cx.c cx = run([0x8500], a=0x800000) # ADD A<<1,#0: no carry out of bit 23 of the cut value assert cx.a == 0 and cx.z and not cx.c # ----- C4: the stack -------------------------------------------------------------------------- class RingObserved(HG51B): STACK_MODEL = "ring-observed" def stack_program() -> list[int]: """scripts/cx4_probe.py's stack probe: ten nested calls from 40h, a check at 53h.""" words = [HALT] * 0x40 + [0x2842, HALT] for k in range(8): words += [0x2800 | (0x44 + 2 * k), 0x3C00] words += [0x2858, 0x54BB, 0x0C00, 0x64BB, 0x3C00, HALT, 0x64AA, 0x3C00] return words def test_stack_ares_shift_register_pulls_zero_when_empty() -> None: rig = Rig() rig.place(stack_program()) rig.start(pc=0x40) rig.finish() # nine returns: the eight kept entries, then an empty one (PB 0, PC 0) halts at 00h assert rig.cx.pc == 0x01 and rig.cx.a == 0xBB and rig.cx.instructions == 24 def test_stack_observed_ring_comes_back_to_the_newest() -> None: rig = Rig(RingObserved) rig.place(stack_program()) rig.start(pc=0x40) rig.finish() # OBSERVED (Mesen2, 26 rows): after eight returns the ninth goes back to 53h, which # now sees A = BBh and jumps to the HALT at 00h assert rig.cx.pc == 0x01 and rig.cx.instructions == 26 # ----- C17: the far bit -------------------------------------------------------------------- class EitherObserved(HG51B): FAR_SELECT = "either-observed" def test_jump_with_bit_8_far_only_when_observed() -> None: page0 = [0x6701, 0x0910] + [HALT] * 14 + [0x6422, HALT] page1 = [HALT] * 0x10 + [0x6411, HALT] for core, expected in ((HG51B, (0, 0x22)), (EitherObserved, (1, 0x11))): rig = Rig(core) rig.place(page0) rig.place(page1, page=1) rig.start() rig.finish() assert (rig.cx.pb, rig.cx.a) == expected # ----- C7, C8: bus delay, internal access cost ------------------------------------------------ @pytest.mark.parametrize(("extra", "wait_clocks"), [(0, 4), (1, 5)]) def test_wait_after_a_rom_read(extra: int, wait_clocks: int) -> None: class Core(HG51B): BUS_DELAY_EXTRA = extra rig = Rig(Core) rig.place([0x6258, 0x612E, 0x1C00, 0x0000, HALT]) # LD MAR,#008000h; LD MDR,BUS; WAIT rig.start() seen = [] while rig.cx.running: seen.append((rig.cx.pc, rig.cx.clock)) rig.cx.step() clock_at = {pc: clock for pc, clock in seen} assert clock_at[3] - clock_at[2] == wait_clocks # Mesen2 (OBSERVED): 5 with ROM wait 4 @pytest.mark.parametrize(("cost", "per_byte"), [(0, 5), (1, 6)]) def test_internal_byte_cost(cost: int, per_byte: int) -> None: class Core(HG51B): INTERNAL_ACCESS_COST = cost rig = Rig(Core) for i, value in enumerate((0x00, 0x80, 0x10, 0x00, 0x01, 0x00, 0x60, 0x00)): rig.cx.write(0x7F40 + i, value) # ROM 108000h to data RAM 006000h, 256 bytes assert rig.finish(budget=100_000) == 256 * per_byte # ----- C10, C11, C16 ----------------------------------------------------------------------------- def test_clear_ares_zeroes_observed_is_a_nop() -> None: setup = [0x6477, 0xE01C, 0x6705, 0x6444] # DPR = 77h, P = 5, A = 44h cx = run([*setup, 0xF800]) assert (cx.a, cx.p, cx.dpr) == (0, 0, 0) class Nop(HG51B): CLEAR_IS_NOP = True cx = run([*setup, 0xF9FF], core=Nop) assert (cx.a, cx.p, cx.dpr) == (0x44, 5, 0x77) @pytest.mark.parametrize( ("code", "ares", "observed"), [(0x00, 0x30, 0x5A), (0x06, 0x36, 0), (0x51, 0x31, 0x7FFF), (0x56, 0x36, 0), (0x66, 0x36, 0x36), (0x76, 0x36, 0x36), (0x86, 0x36, 0), (0xE6, 0x36, 0x36)], ) # fmt: skip def test_ld_p_reg_source(code: int, ares: int, observed: int) -> None: class Operand(HG51B): LD_P_SOURCE = "operand-observed" registers = {n: 0x30 + n for n in range(16)} assert run([0x6300 | code], a=0x5A, r=registers).p == ares assert run([0x6300 | code], a=0x5A, r=registers, core=Operand).p == observed # ----- C13: the status mirror ------------------------------------------------------------------ def test_status_mirror() -> None: class All(HG51B): STATUS_MIRROR = "all-observed" for core, at_58 in ((HG51B, 0), (All, None)): rig = Rig(core) rig.place([0x0000, 0x0800]) # a loop: running rig.start() rig.cx.run(10_000) status = rig.cx.read(0x7F5E) assert status & 0x40 assert rig.cx.read(0x7F58) == (status if at_58 is None else at_58) assert rig.cx.read(0x7F5F) == status # ----- C3, C9 (no switch): the ares rules -------------------------------------------------------- @pytest.mark.parametrize( ("source", "target", "locks"), [(0x108000, 0x006000, False), (0x108000, 0x108400, True), (0x006000, 0x006100, False), (0x006000, 0x108400, False), (0x700000, 0x700100, True)], ) # fmt: skip def test_dma_lock_ares(source: int, target: int, locks: bool) -> None: rig = Rig() data = [*source.to_bytes(3, "little"), 16, 0, *target.to_bytes(3, "little")] for i, value in enumerate(data): rig.cx.write(0x7F40 + i, value) rig.cx.run(100_000) assert rig.cx.lock is locks def test_preload_reloads_a_cached_page() -> None: rig = Rig() rig.place([HALT]) rig.start() rig.finish() reads = len(rig.reads) rig.cx.write(0x7F48, 0) # ares memory.cpp l.201-208: a forced preload rig.finish() assert len(rig.reads) - reads == 512