# SPDX-License-Identifier: MIT """Rung 1: the code cache and its S-CPU window (BK2 6.8; U14, U15, C10).""" from __future__ import annotations from conftest import make_gsu, run_to_stop, start from gsu_asm import CACHE, NOP, STOP, ibt, inc, iwt from gsu_python import GSU def upload(g: GSU, code: list[int], offset: int = 0) -> None: """The S-CPU writes code into the window ($3100+) and marks each line valid by writing its last byte (BK2 5.1.3, 6.8.3).""" padded = code + [0] * (-len(code) % 16) for i, value in enumerate(padded): g.write_io(0x3100 + offset + i, value) def test_cbr_c3a0_window_example(): """BK2 6.8.3: with CBR = C3A0h, GSU C3A0h-C3FFh is at $32A0-$32FF and C400h-C59Fh at $3100-$329F: the window is the GSU address's low 9 bits.""" code = bytearray(0x5000) base = 0xC39F - 0x8000 code[base] = CACHE[0] # R15 = C3A0h after it: CBR = C3A0h body = ibt(1, 0x11) + [NOP[0]] * (0xC400 - 0xC3A2) + inc(2) + STOP + NOP code[base + 1 : base + 1 + len(body)] = bytes(body) g = make_gsu(bytes(code)) start(g, 0xC39F) run_to_stop(g) assert g.cbr == 0xC3A0 assert g.read_io(0x32A0) == 0xA1 # IBT R1 at C3A0h assert g.read_io(0x32A1) == 0x11 assert g.read_io(0x3100) == 0xD2 # INC R2 at C400h assert all(g.cache_loaded[i] == 16 for i in range(0x1A, 0x20)) def test_cache_injection_runs_without_the_buses(): """BK2 5.1.3 / 6.8.4, krom CACHEINJECT: GO = 0 sets CBR = 0; code written at $3100 with each line's last byte written runs from 0000h with neither RON nor RAN.""" g = make_gsu([]) g.write_io(0x3030, 0) upload(g, iwt(1, 0x7FFF) + iwt(0, 0x8001) + [0x21, 0x3D, 0x50] + STOP + NOP) start(g, 0x0000, scmr=0x00, pbr=None) run_to_stop(g) assert g.r[1] == 0x0000 # 7FFF + 8001 + CY(0) assert g.r[15] == 0x000B # STOP at 0009h + 2 def test_unwritten_line_end_leaves_the_line_invalid(): """BK2 6.8.3: "If data are written only part way through the 16 bytes, the flag will not be set": a fetch there needs the bus, so with RON = 0 the GSU waits.""" g = make_gsu([]) g.write_io(0x3030, 0) for i, value in enumerate(inc(1) + STOP): g.write_io(0x3100 + i, value) start(g, 0x0000, scmr=0x00, pbr=None) assert g.waiting assert g.run(1000) == 1000 assert g.r[1] == 0 g.write_io(0x303A, 0x18) # RON back: the bytes come from ROM (00:0000 mirrors 00:8000) assert not g.waiting def test_cache_flags_cleared_only_when_cbr_changes(): """U15 (BK2 9.27): a CACHE that leaves CBR alone keeps the lines.""" g = make_gsu(CACHE + inc(1) + CACHE + inc(1) + STOP) start(g) run_to_stop(g) assert g.cbr == 0x8000 assert g.cache_loaded[0] == 6 # loaded as executed (C10): through STOP's prefetch def test_go_zero_clears_cbr_and_flags_stop_does_not(): """BK2 2-6-11: a forced stop (SFR GO = 0) sets CBR = 0 and clears the flags; STOP keeps CBR, the flags and the contents.""" g = make_gsu(CACHE + inc(1) + STOP + NOP) start(g) run_to_stop(g) assert g.cbr == 0x8000 and g.cache_loaded[0] == 4 g.write_io(0x301F, 0x80) # restart (R15 low byte latched as 00 earlier) g.write_io(0x3030, 0) # forced stop assert g.cbr == 0 assert not any(g.cache_loaded) def test_pbr_write_invalidates(): """U15: an S-CPU PBR write invalidates the cache (ares, MAME).""" g = make_gsu(CACHE + STOP + NOP) start(g) run_to_stop(g) assert g.cache_loaded[0] g.write_io(0x3034, 0) assert not any(g.cache_loaded) def test_whole_line_fill_gives_the_same_values_c10(): class WholeLine(GSU): INCREMENTAL_CACHE_FILL = False code = CACHE + iwt(1, 0x1234) + inc(1) + [0x05, 0x02] + NOP + inc(1) + inc(1) + STOP + NOP results = [] for cls in (GSU, WholeLine): g = make_gsu(code, cls=cls) start(g) run_to_stop(g) results.append((g.r[1], bytes(g.cache[:32]))) assert results[0] == results[1]