# SPDX-License-Identifier: MIT """Rung 1, the external bus port (U10, C7) and the chip's bus regions.""" from __future__ import annotations from rig import Rig from hg51b_python import HG51B, region from hg51b_python.cpu import REGION_DATA_RAM, REGION_IO, REGION_NONE, REGION_ROM, REGION_SRAM def test_regions() -> None: assert region(0x008000) == REGION_ROM and region(0xBFFFFF) == REGION_ROM assert region(0xC00000) == REGION_ROM and region(0x400000) == REGION_NONE assert region(0x700000) == REGION_SRAM and region(0x777FFF) == REGION_SRAM assert region(0x006000) == REGION_DATA_RAM and region(0x807BFF) == REGION_DATA_RAM assert region(0x006C00) == REGION_IO and region(0x007F5E) == REGION_IO assert region(0x406000) == REGION_NONE def idiom(rig: Rig, address: int) -> HG51B: """fullsnes' idiom: LD MAR (via R0), LD MDR,BUS; INC MAR; WAIT; then MDR -> R1.""" rig.cx.r[0] = address return rig.execute([0x6260, 0x612E, 0x4000, 0x1C00, 0xE161]) def test_bus_read_lands_in_mdr_after_wait() -> None: rig = Rig() rig.poke(0x0A8000, b"\x5a") cx = idiom(rig, 0x0A8000) assert cx.r[1] == 0x5A and cx.mar == 0x0A8001 and not cx.bus_enabled def test_bus_read_is_zero_until_complete() -> None: rig = Rig() rig.poke(0x0A8000, b"\x5a") rig.cx.r[0] = 0x0A8000 # LD MAR,R0; LD MDR,BUS; ST R1,MDR (too early: reads the 0 the operand returned) cx = rig.execute([0x6260, 0x612E, 0xE161]) assert cx.r[1] == 0 assert cx.mdr == 0x5A # it completed while the chip halted def test_bus_ram_wait_operand_2f() -> None: rig = Rig() rig.cx.write(0x7F50, 0x42) # RAM wait 2, ROM wait 4 rig.cx.r[0] = 0x700010 rig.place([0x6260, 0x612F, 0xFC00]) rig.start() while rig.cx.instructions < 2: rig.cx.step() assert rig.cx.bus_pending == 2 def test_bus_write_reaches_sram_only() -> None: rig = Rig() rig.cx.r[0] = 0x700010 # LD MAR,R0; LD MDR,#$77; ST BUS,A: storing to 2Eh starts a write of MDR (the stored # value is dropped: ares ``registers.cpp`` ``writeRegister``); WAIT rig.execute([0x6260, 0x6577, 0xE02E, 0x1C00]) assert rig.sram_writes == [(0x700010, 0x77)] def test_bus_reads_data_ram_inside_the_chip() -> None: rig = Rig() rig.cx.write(0x6123, 0x99) rig.cx.r[0] = 0x006123 cx = rig.execute([0x6260, 0x612E, 0x1C00, 0xE161]) assert cx.r[1] == 0x99