# SPDX-License-Identifier: MIT """Rung 1, the cache (U2): page loads, lookups, locks, fall-through, preload (C9), and the per-byte progress the S-CPU can see (handoff decision 5).""" from __future__ import annotations from rig import BASE, Rig from hg51b_python import HG51B def test_load_reads_512_bytes_from_base_plus_pb_x_512() -> None: rig = Rig() rig.place([0xFC00], page=5) rig.start(pb=5) rig.finish() assert rig.cx.cache_address[0] == BASE + 5 * 512 or rig.cx.cache_address[1] == BASE + 5 * 512 assert len(rig.reads) == 512 assert rig.reads[0] == BASE + 5 * 512 and rig.reads[-1] == BASE + 5 * 512 + 511 def test_load_is_per_byte_and_visible() -> None: rig = Rig() rig.place([0xFC00]) rig.start() rig.cx.run(100) # 20 bytes at 5 clocks assert rig.cx.owns_rom_bus and rig.cx.status & 0xC0 == 0xC0 assert len(rig.reads) == 20 rig.finish() assert not rig.cx.owns_rom_bus and rig.cx.status == 0 def test_second_run_hits_the_cache() -> None: rig = Rig() rig.place([0xFC00]) rig.start() rig.finish() reads = len(rig.reads) rig.start() assert rig.finish() == 1 # HALT only assert len(rig.reads) == reads def test_two_pages_alternate() -> None: rig = Rig() rig.place([0xFC00], page=0) rig.place([0xFC00], page=1) for pb in (0, 1, 0, 1): rig.start(pb=pb) rig.finish() assert len(rig.reads) == 1024 # each page loaded once, into its own cache page assert sorted(rig.cx.cache_address) == [BASE, BASE + 512] def test_locked_page_is_not_replaced() -> None: rig = Rig() rig.place([0xFC00], page=0) rig.place([0xFC00], page=1) rig.place([0xFC00], page=2) rig.start(pb=0) rig.finish() held = rig.cx.cache_page rig.cx.write(0x7F4C, 1 << held) # lock the page holding PB 0 rig.start(pb=1) rig.finish() rig.start(pb=2) rig.finish() assert rig.cx.cache_address[held] == BASE def test_both_locked_and_a_miss_halts() -> None: rig = Rig() rig.cx.write(0x7F4C, 3) rig.cx.write(0x7F51, 0) rig.place([0x6401, 0xFC00]) rig.start() rig.finish() assert rig.cx.halted and rig.cx.instructions == 0 and rig.cx.irq def test_a_fresh_start_loads_into_the_other_cache_page() -> None: """The lookup tries the current page, then the other, and loads into that one (ares ``hg51b.cpp`` ``cache``): from reset (page 0 selected) the first load goes to cache page 1.""" rig = Rig() rig.execute([]) assert rig.cx.cache_page == 1 and rig.cx.cache_address == [-1, BASE] def test_fall_through_page_end_continues_with_pb_from_p() -> None: rig = Rig() rig.place([0x7C07], page=0, pc=0) # LD PL,#7, NOPs, and at FFh LD A,#$11 rig.place([0x6411], page=0, pc=0xFF) rig.place([0x6422, 0xFC00], page=7, pc=0) rig.cx.write(0x7F48, 0) # preload PB 0 into cache page 0 rig.finish() rig.start() rig.finish() # the wrap selects cache page 1 and PB = P = 7; the lookup misses on page 1, flips to # page 0 and, page 0 being unlocked, loads page 7 there (ares ``advance``/``cache``) assert rig.cx.a == 0x22 and rig.cx.pb == 7 and rig.cx.cache_page == 0 assert rig.cx.cache_address[0] == BASE + 7 * 512 def test_fall_through_into_a_locked_page_0_uses_page_1() -> None: rig = Rig() rig.place([0x7C07], page=0, pc=0) rig.place([0x6422, 0xFC00], page=7, pc=0) rig.cx.write(0x7F48, 0) rig.finish() rig.cx.write(0x7F4C, 1) # lock cache page 0 rig.start() rig.finish() assert rig.cx.a == 0x22 and rig.cx.cache_page == 1 def test_fall_through_on_cache_page_1_halts() -> None: rig = Rig() rig.place([0x6411], page=0, pc=0xFF) rig.place([0x6422, 0xFC00], page=7, pc=0) rig.start(pc=0xFF) # a fresh start runs on cache page 1 rig.finish(budget=10_000) assert rig.cx.halted and rig.cx.a == 0x11 and rig.cx.instructions == 1 def test_preload_ares_reloads_even_when_cached() -> None: rig = Rig() rig.place([0xFC00], page=0x0E) rig.cx.write(0x7F4D, 0x0E) rig.cx.write(0x7F4E, 0) for page in (0, 1, 0): rig.cx.write(0x7F48, page) assert rig.cx.running and rig.cx.halted rig.finish() assert len(rig.reads) == 3 * 512 assert rig.cx.cache_address == [BASE + 0x0E * 512] * 2 def test_preload_then_start_runs_from_cache() -> None: rig = Rig() rig.place([0x6409, 0xFC00], page=0x0E) rig.cx.write(0x7F4D, 0x0E) rig.cx.write(0x7F48, 0) rig.finish() rig.start(pb=0x0E) assert rig.finish() == 2 and rig.cx.a == 9