# SPDX-License-Identifier: MIT """Rung 1, registers and memories: operand codes (U5, U6, C16), loads and stores, P (C11), data RAM fold (U3), byte lanes (U18), the data ROM (U4), SWAP, CLEAR (U17, C10), reset (U20, C15) and state capture.""" from __future__ import annotations from rig import Rig from hg51b_python import HG51B from hg51b_python._decode import CONSTANTS def test_constants_u6() -> None: for i, value in enumerate(CONSTANTS): rig = Rig() rig.execute([0x6050 + i]) # LD A,const assert rig.cx.a == value def test_mirror_and_bit7_u5() -> None: rig = Rig() rig.cx.r[3] = 0x123456 rig.execute([0x6073]) # LD A,R3 via the mirror code 73h assert rig.cx.a == 0x123456 rig.execute([0x6064, 0x60E3]) # LD A,R4; LD A,(bit 7 + 63h) = R3 assert rig.cx.a == 0x123456 def test_undefined_codes_read_zero() -> None: for code in (0x04, 0x10, 0x2D, 0x30, 0x4F): rig = Rig() rig.cx.a = 5 rig.execute([0x6000 | code]) assert rig.cx.a == 0 def test_operand_00_c16() -> None: class Bsnes00(HG51B): OPERAND_00_READS_A = False rig = Rig() rig.execute([0x6407, 0x8000]) # LD A,#7; ADD A,A(00h) assert rig.cx.a == 14 rig = Rig(Bsnes00) rig.execute([0x6407, 0x8000]) assert rig.cx.a == 7 def test_special_registers_through_ld_and_st() -> None: rig = Rig() words = [ 0x6412, 0xE013, # LD A,#$12; ST MAR,A 0x6434, 0xE01C, # DPR = 34h 0x6456, 0xE008, # ROM = 56h 0x6478, 0xE00C, # RAM = 78h 0x649A, 0xE001, # MH = 9Ah 0x64BC, 0xE002, # ML = BCh 0x64DE, 0xE003, # MDR = DEh 0x64F0, 0xE028, # P = F0h ] # fmt: skip cx = rig.execute(words) assert (cx.mar, cx.dpr, cx.rom, cx.ram, cx.mdr, cx.p) == (0x12, 0x34, 0x56, 0x78, 0xDE, 0xF0) assert cx.mul == 0x9A << 24 | 0xBC def test_pc_as_an_operand_reads_the_next_word() -> None: rig = Rig() rig.execute([0x0000, 0x0000, 0x6020]) # LD A,PC at PC 2: PC has advanced to 3 assert rig.cx.a == 3 def test_st_pc_jumps_within_the_page() -> None: rig = Rig() rig.execute([0x6404, 0xE020, 0x6411, 0xFC00, 0x6422]) # PC = 4 assert rig.cx.a == 0x22 def test_ld_p_variants() -> None: rig = Rig() rig.cx.r[2] = 0xFFFFFF rig.execute([0x6362]) # LD P,R2: 15 bits assert rig.cx.p == 0x7FFF rig.execute([0x67AB]) # LD P,#$AB assert rig.cx.p == 0xAB rig.execute([0x7C11, 0x7DFF]) # LD PL,#$11; LD PH,#$7F (bit 7 ignored) assert rig.cx.p == 0x7F11 def test_rdram_wrram_lanes_and_fold_u3_u18() -> None: rig = Rig() rig.cx.data_ram[0x100:0x103] = b"\x11\x22\x33" rig.cx.data_ram[0x800] = 0x44 rig.cx.dpr = 0x100 rig.execute([0x6C00, 0x6D01, 0x6E02]) # RDRAM 0..2, DPR+0..2 assert rig.cx.ram == 0x332211 rig.cx.a = 0xC00 # folds to 800h rig.execute([0x6800]) assert rig.cx.ram == 0x332244 rig.cx.ram = 0xABCDEF rig.cx.dpr = 0xFFE # DPR + 3 = 1001h -> 12 bits: 001h rig.execute([0xEC03, 0xEF02]) # WRRAM 0,DPR+3; lane 3 (EFh) is a NOP assert rig.cx.data_ram[0x001] == 0xEF rig.cx.a = 0xFFF # folds to BFFh rig.execute([0xEA00]) # WRRAM 2,A assert rig.cx.data_ram[0xBFF] == 0xAB def test_rdrom_u4() -> None: rig = Rig() rig.cx.a = 0x1100 # low 10 bits: 100h, the sqrt table's first word rig.execute([0x7000]) assert rig.cx.rom == 0 rig.execute([0x7740]) # RDROM #$340: tan 45 deg assert rig.cx.rom == 0x010000 rig.execute([0x7401]) # div 1 assert rig.cx.rom == 0x800000 def test_swap() -> None: rig = Rig() rig.cx.r[9] = 0x999999 rig.execute([0x6412, 0xF0F9]) # LD A,#$12; SWAP A,R9 (bits 7-4 ignored) assert rig.cx.a == 0x999999 and rig.cx.r[9] == 0x12 def test_reset_u20_c15() -> None: rig = Rig() rig.cx.data_ram[0] = 0x55 rig.execute([0x6407, 0xE060]) rig.cx.reset() cx = rig.cx assert cx.halted and cx.a == 0 and cx.r[0] == 0 and cx.wait_rom == 3 and cx.wait_ram == 3 assert cx.cache_address == [-1, -1] and not cx.irq_disabled and cx.single_rom assert cx.data_ram[0] == 0x55 # C15: kept assert HG51B(lambda address: 0).data_ram == bytearray(3072) def test_capture_and_restore() -> None: rig = Rig() rig.place([0x6401, 0x8401, 0x0801]) # LD A,#1; loop: ADD A,#1; JMP $01 rig.start() rig.cx.run(3000) saved = rig.cx.capture_state() rig.cx.run(500) after = rig.cx.capture_state() rig.cx.restore_state(saved) assert rig.cx.capture_state() == saved rig.cx.run(500) assert rig.cx.capture_state() == after light = rig.cx.capture_state(memory=False) assert light.data_ram is None and light.program_ram is None assert light.to_dict()["pc"] == after.pc