# SPDX-License-Identifier: MIT """Rung 1, the S-CPU side (U11-U14, U16, C9, C13, C18): the map, read-back, starts, status, suspend, vectors, IRQ. Byuu's glue (ares ``hitachidsp/memory.cpp``, ISC) except where a C entry says.""" from __future__ import annotations from rig import Rig from hg51b_python import HG51B def test_data_ram_window_and_mirror() -> None: cx = HG51B(lambda address: 0) cx.write(0x6000, 0x11) cx.write(0x7BFF, 0x22) cx.write(0x8A6005, 0x33) # any bank: the board decides which reach the chip assert cx.data_ram[0] == 0x11 and cx.data_ram[0xBFF] == 0x22 and cx.data_ram[5] == 0x33 assert cx.read(0x7000) == 0x11 and cx.read(0x6BFF) == 0x22 def test_io_block_mirrors_and_empty_space() -> None: cx = HG51B(lambda address: 0) cx.write(0x7F50, 0x44) assert cx.read(0x6F50) == 0x44 # $6C00-$6FFF is the I/O block too (A9-A0 decode) assert cx.read(0x7C00) == 0 and cx.read(0x7F3F) == 0 and cx.read(0x7FB0) == 0 assert cx.read(0x7FFF) == 0 def test_interface_registers_read_back() -> None: """The INTERFACE REGISTER test writes and reads these back (MAME host log).""" cx = HG51B(lambda address: 0) values = { 0x7F40: 0x12, 0x7F41: 0x34, 0x7F42: 0x56, 0x7F43: 0x78, 0x7F44: 0x9A, 0x7F45: 0xBC, 0x7F46: 0xDE, 0x7F49: 0x00, 0x7F4A: 0x80, 0x7F4B: 0x02, 0x7F4C: 0x03, 0x7F4D: 0x0E, 0x7F4E: 0x7F, 0x7F50: 0x44, 0x7F51: 0x01, 0x7F52: 0x00, 0x7F6A: 0xEF, 0x7F6B: 0x1F, 0x7F6E: 0xF3, 0x7F6F: 0x1F, } # fmt: skip for address, value in values.items(): cx.write(address, value) for address, value in values.items(): assert cx.read(address) == value, hex(address) cx.write(0x7F4E, 0xFF) assert cx.read(0x7F4E) == 0x7F # PB is 15 bits def test_register_file_bytes() -> None: cx = HG51B(lambda address: 0) for i in range(48): cx.write(0x7F80 + i, i + 1) assert cx.r[0] == 0x030201 and cx.r[15] == 0x302F2E assert cx.read(0x7FC0) == 1 and cx.read(0x7FEF) == 0x30 # the mirror cx.write(0x7FC3, 0xAA) assert cx.r[1] == 0x0605AA def test_start_only_when_halted() -> None: rig = Rig() rig.place([0x0800], page=0) rig.place([0x6477, 0xFC00], page=1) rig.start(pb=0) rig.cx.run(4000) rig.start(pb=1) # ignored: running rig.cx.run(4000) assert rig.cx.pb == 0 and rig.cx.a == 0 assert rig.cx.read(0x7F4D) == 1 # but the registers are written def test_suspend_and_resume() -> None: rig = Rig() rig.place([0x0800]) rig.start() while rig.cx.instructions < 10: rig.cx.step() executed, clock = rig.cx.instructions, rig.cx.clock rig.cx.write(0x7F56, 0) # 32 clocks assert rig.cx.read(0x7F5E) & 0x01 while rig.cx.suspend_enabled: rig.cx.step() assert rig.cx.instructions == executed and rig.cx.clock - clock == 32 rig.cx.write(0x7F55, 0) # until $7F5D rig.cx.run(10_000) assert rig.cx.instructions == executed and rig.cx.suspend_enabled rig.cx.write(0x7F5D, 0) rig.cx.run(30) assert rig.cx.instructions == executed + 10 # JMP $00: 3 clocks each def test_vectors_and_rom_ownership() -> None: rig = Rig() for offset in range(32): rig.cx.write(0x7F60 + offset, 0xA0 + offset) assert rig.cx.vector(0x0A) == 0xAA and rig.cx.vector(0x1F) == 0xBF assert not rig.cx.owns_rom_bus rig.place([0xFC00]) rig.start() rig.cx.run(10) assert rig.cx.owns_rom_bus # loading the cache (U16) def test_irq_disable_flag_c18() -> None: class Mesen2Irq(HG51B): IRQ_DISABLE_SETS_FLAG = True for core, flag in ((HG51B, 0), (Mesen2Irq, 0x02)): rig = Rig(core) rig.cx.write(0x7F51, 0) rig.execute([]) assert rig.cx.irq rig.cx.write(0x7F51, 1) assert not rig.cx.irq and rig.cx.read(0x7F5E) & 0x02 == flag def test_reads_have_no_side_effects() -> None: rig = Rig() rig.cx.write(0x7F51, 0) rig.execute([]) before = rig.cx.capture_state() for address in range(0x6000, 0x8000): rig.cx.read(address) assert rig.cx.capture_state() == before