# SPDX-License-Identifier: MIT """Rung 1: the S-CPU's side: the register file, GO, IRQ, the fixed vectors, RON/RAN, reset, the models (U8-U12, U16, U23, U26, C6, C7, C11; BK2 chapter 4, 5).""" from __future__ import annotations from conftest import make_gsu, run_to_stop, start from gsu_asm import NOP, STOP, inc, iwt, program from gsu_python import FIXED_VECTORS, GSU, GSU1, GSU2, GSU2_SP1, MC1 from gsu_python._sfr import GO, IRQ, R def test_write_latch_c6(): """C6: an even address only latches; the odd one writes latch | data << 8.""" g = make_gsu([]) g.write_io(0x3002, 0x34) assert g.r[1] == 0 g.write_io(0x3003, 0x12) assert g.r[1] == 0x1234 class Direct(GSU): REGISTER_WRITE_LATCH = False g = make_gsu([], cls=Direct) g.write_io(0x3002, 0x34) assert g.r[1] == 0x0034 def test_301f_starts_and_sfr_go_zero_stops(): """U8, U10: $301F starts at R15; SFR GO = 0 stops, CBR = 0. GO = 1 restarts (Dirt Trax).""" g = make_gsu(program([0x02], inc(1), [0x05, 0xFD], NOP)) # CACHE; INC R1; BRA -3 start(g) assert g.running g.run(200) assert g.cbr == 0x8000 g.write_io(0x3030, 0x00) assert not g.running and g.cbr == 0 before = g.r[1] g.write_io(0x3030, GO) g.run(50) assert g.r[1] > before def test_stop_irq_and_3031_read_c11_u11(): g = make_gsu(STOP) start(g, cfgr=0x00) # IRQ unmasked run_to_stop(g) assert g.sfr & IRQ and g.irq assert g.read_io(0x3031) & 0x80 assert not g.sfr & IRQ and not g.irq # BK2 2-5-5: the read clears it g = make_gsu(STOP) start(g, cfgr=0x80) # masked: C11, no flag (emulators) run_to_stop(g) assert not g.sfr & IRQ and not g.irq class FullsnesIrq(GSU): IRQ_FLAG_WHEN_MASKED = True g = make_gsu(STOP, cls=FullsnesIrq) start(g, cfgr=0x80) run_to_stop(g) assert g.sfr & IRQ and not g.irq def test_access_while_running_c7(): """C7: while GO = 1 only SFR, SCMR and VCR; other reads None, other writes ignored.""" g = make_gsu(program(inc(1), [0x05, 0xFD], NOP)) g.r[3] = 0x5555 start(g) assert g.read_io(0x3006) is None assert g.read_io(0x3100) is None assert g.read_io(0x3030) & GO assert g.read_io(0x303B) == 4 g.write_io(0x3006, 0x11) g.write_io(0x3007, 0x22) assert g.r[3] == 0x5555 g.write_io(0x303A, 0x08) assert g.scmr == 0x08 class Open(GSU): RESTRICT_ACCESS_WHILE_RUNNING = False g = make_gsu(program(inc(1), [0x05, 0xFD], NOP), cls=Open) g.r[3] = 0x5555 start(g) assert g.read_io(0x3006) == 0x55 def test_fixed_vectors_u16(): """BK2 table 2-5-2: while GO and RON the S-CPU reads the fixed table in ROM space.""" g = make_gsu(program(inc(1), [0x05, 0xFD], NOP)) assert g.cpu_rom_byte(0xFFEA) is None start(g) assert [g.cpu_rom_byte(0xFFE0 + i) for i in range(16)] == list(FIXED_VECTORS) assert g.cpu_rom_byte(0xFFEE) | g.cpu_rom_byte(0xFFEF) << 8 == 0x010C # IRQ assert g.cpu_rom_byte(0xFFEA) | g.cpu_rom_byte(0xFFEB) << 8 == 0x0108 # NMI assert not g.cpu_owns_ram g.write_io(0x303A, 0x00) assert g.cpu_rom_byte(0xFFEA) is None assert g.cpu_owns_ram def test_mirrors_u12(): """fullsnes "Full I/O Map with Mirrors": GSU-2 repeats $3000-$303F; MC1 has its own map.""" g = make_gsu([]) g.r[0] = 0xBEEF g.pbr = 0x12 assert g.read_io(0x3040) == 0xEF # GSU-2: $3040 = R0 assert g.read_io(0x3334) == 0x12 # $3334 = PBR assert g.read_io(0x3024) == 0x12 # $3020-$302F mirror $3030-$303F assert g.read_io(0x3032) == 0 # unused: 00h assert g.read_io(0x3500) is None m = make_gsu([], model=MC1) m.r[0] = 0xBEEF m.pbr = 0x12 assert m.read_io(0x303B) == 1 # VCR assert m.read_io(0x3020) is None # open bus assert m.read_io(0x3440) == 0xEF # R0 mirror assert m.read_io(0x3034) == m.read_io(0x3030) # status mirror, not PBR assert m.read_io(0x3480) is None m.write_io(0x3034, 5) assert m.pbr == 5 # writes still reach the registers def test_vcr_per_model_u23(): assert [make_gsu([], model=m).read_io(0x303B) for m in (MC1, GSU1, GSU2, GSU2_SP1)] == [ 1, 4, 4, 4, ] def test_r14_write_prefetch_u9(): """U9 (ares): an S-CPU R14 write starts the ROM buffer: SFR.R reads 1 for a while, then GETB has the byte.""" g = make_gsu([0xEF, 0x00]) g.write_io(0x3039, 1) g.write_io(0x301C, 0x01) g.write_io(0x301D, 0x80) assert g.read_io(0x3030) & R g.run(10) assert not g.read_io(0x3030) & R assert g.romdr == 0x00 # 00:8001 is the STOP byte def test_ron_ran_wait(): """BK2 5.3: with RON cleared the GSU waits at its next ROM fetch until RON is set again.""" g = make_gsu(program(inc(1), [0x05, 0xFD], NOP)) start(g) g.run(100) count = g.r[1] g.write_io(0x303A, 0x08) # RAN only assert g.waiting assert g.run(500) == 500 assert g.r[1] == count g.write_io(0x303A, 0x18) g.run(100) assert g.r[1] > count def test_run_returns_used_clocks(): g = make_gsu(program(iwt(1, 1), STOP)) assert g.run(100) == 100 # idle: all of it at once start(g) used = g.run(1) assert used >= 1 # whole instructions: the first one overshoots assert g.run(0) == 0 def test_reset_u26(): g = make_gsu(program(iwt(3, 0x1234), iwt(1, 9), STOP)) start(g, cfgr=0xA0, scmr=0x3F) run_to_stop(g) g.reset() assert (g.r[1], g.r[2], g.r[15], g.sfr, g.cbr, g.scmr, g.cfgr, g.clsr) == (0,) * 8 assert g.r[3] == 0x1234 # "invalid" after reset: kept assert g.pipeline == 0x01 assert not g.running def test_state_round_trip(): g = make_gsu(program(iwt(3, 0x1234), inc(4), STOP)) start(g) g.step() saved = g.capture_state() run_to_stop(g) after = g.capture_state() g.restore_state(saved) run_to_stop(g) assert g.capture_state() == after