# SPDX-License-Identifier: MIT """Rung 1, the ALU: flags (U9), shifted operands (U7, C2), shifts (C1), MUL (U8, C6), SXB/SXW (U19), logic ops. Expected values follow start-here.md "Flags" (fullsnes + byuu's model in ares, ISC; research and emulator tier); the contested ones name their C entry and test both settings.""" from __future__ import annotations import pytest from rig import Rig from hg51b_python import HG51B LD_A = 0x6400 # LD A,#imm SWAP = 0xF000 def run(words: list[int], *, a: int = 0, r: dict[int, int] | None = None, core=HG51B): rig = Rig(core) rig.cx.a = a for index, value in (r or {}).items(): rig.cx.r[index] = value return rig.execute(words) def flags(cx) -> str: return "".join(name if getattr(cx, name.lower()) else "-" for name in "NZCV") # ----- ADD (U9): N, Z, C = carry out of bit 23, V = signed overflow ------------------------ @pytest.mark.parametrize( ("a", "imm", "result", "expected"), [ (0x000001, 0x01, 0x000002, "----"), (0xFFFFFF, 0x01, 0x000000, "-ZC-"), (0x7FFFFF, 0x01, 0x800000, "N--V"), (0xFFFFF0, 0x0F, 0xFFFFFF, "N---"), ], ) def test_add_immediate_flags(a: int, imm: int, result: int, expected: str) -> None: cx = run([0x8400 | imm], a=a) assert cx.a == result and flags(cx) == expected def test_add_register_and_shift_field() -> None: # ADD A<<8,R1: (A << 8) + R1 (U7) cx = run([0x8261], a=0x000123, r={1: 0x000045}) assert cx.a == 0x012345 cx = run([0x8361], a=0x000012, r={1: 0x000034}) # A<<16 assert cx.a == 0x120034 cx = run([0x8161], a=0x000010, r={1: 1}) # A<<1 assert cx.a == 0x000021 # ----- SUB/SUBR/CMP/CMPR (U9): C = 1 means no borrow --------------------------------------- def test_sub_carry_is_not_borrow() -> None: cx = run([0x9405], a=0x000010) # SUB A,#5 assert cx.a == 0x00000B and flags(cx) == "--C-" cx = run([0x9405], a=0x000003) # V by the emulators' formula (C10): see below assert cx.a == 0xFFFFFE and flags(cx) == "N--V" cx = run([0x9405], a=0x000005) assert cx.a == 0 and flags(cx) == "-ZC-" def test_subr_reverses_operands() -> None: cx = run([0x8C10], a=0x000003) # SUBR #$10,A: 10h - A assert cx.a == 0x00000D and cx.c cx = run([0x8861], a=0x000020, r={1: 0x10}) # SUBR R1,A assert cx.a == 0xFFFFF0 and not cx.c and cx.n def test_cmp_and_cmpr_set_flags_only() -> None: cx = run([0x5405], a=0x000005) # CMP A,#5 assert cx.a == 5 and flags(cx) == "-ZC-" cx = run([0x4C05], a=0x000006) # CMPR #5,A: 5 - 6 assert cx.a == 6 and flags(cx) == "N--V" def test_sub_overflow_uses_the_emulators_formula() -> None: """C10: byuu's model (ares ``algorithmSUB``) computes V for subtraction as ~(x ^ y) & (x ^ z) & 800000h.""" cx = run([0x9401], a=0x800000) # 800000h - 1 = 7FFFFFh: x, y differ in sign assert cx.a == 0x7FFFFF and not cx.v # a true signed overflow, but the formula says no cx = run([0x9061], a=0x000000, r={1: 0x000001}) # SUB A,R1: 0 - 1: same signs, result negative assert cx.a == 0xFFFFFF and cx.v # ----- C2: is A< None: rig = Rig() rig.cx.a = 0xF0F0F0 rig.cx.c = rig.cx.v = True cx = rig.execute([0xB4FF]) # AND A,#$FF assert cx.a == 0x0000F0 and cx.c and cx.v and not cx.n and not cx.z cx = run([0xBC0F], a=0x800000) # OR A,#$0F assert cx.a == 0x80000F and cx.n cx = run([0xAC0F], a=0x00000F) # XOR A,#$0F assert cx.a == 0 and cx.z cx = run([0xA400], a=0x0000FF) # XNOR A,#0: ~A ^ 0 (C10, ares) assert cx.a == 0xFFFF00 and cx.n cx = run([0xB051], a=0x123456) # AND A,#$FFFFFF (constant register 51h) assert cx.a == 0x123456 # ----- shifts (C1) -------------------------------------------------------------------------- @pytest.mark.parametrize( ("word", "a", "result"), [ (0xC404, 0x123456, 0x012345), # SHR A,#4 (0xCC04, 0x823456, 0xF82345), # ASR A,#4 (0xD404, 0x123456, 0x612345), # ROR A,#4 (0xDC04, 0x123456, 0x234560), # SHL A,#4 (0xD400, 0x123456, 0x123456), # ROR A,#0 (0xDC00, 0x123456, 0x123456), # SHL A,#0 ], ) def test_shift_immediate(word: int, a: int, result: int) -> None: cx = run([word], a=a) assert cx.a == result def test_shift_by_register_uses_low_five_bits() -> None: cx = run([0xC061], a=0x000100, r={1: 0x000024}) # SHR A,R1 with R1 = 24h -> count 4 assert cx.a == 0x000010 # ----- sign extension (U19) --------------------------------------------------------------- def test_sxb_sxw() -> None: cx = run([0x5900], a=0x123480) assert cx.a == 0xFFFF80 and cx.n cx = run([0x5A00], a=0x127FFF) assert cx.a == 0x007FFF and not cx.n cx = run([0x5A00], a=0x128000) assert cx.a == 0xFF8000 cx = run([0x5900], a=0x123400) assert cx.a == 0 and cx.z # ----- MUL (U8, C6) ----------------------------------------------------------------------- def test_mul_is_signed_24_by_24() -> None: cx = run([0x9861], a=0xFFFFFF, r={1: 0x000002}) # -1 x 2 assert cx.mul == 0xFFFFFFFFFFFE cx = run([0x9861], a=0x7FFFFF, r={1: 0x7FFFFF}) assert cx.mul == 0x7FFFFF * 0x7FFFFF cx = run([0x9861], a=0x800000, r={1: 0x800000}) # -2^23 squared assert cx.mul == 1 << 46 def test_mul_immediate_is_unsigned() -> None: cx = run([0x9CFF], a=0x000002) # 2 x FFh, not 2 x -1 assert cx.mul == 0x1FE cx = run([0x9C02], a=0xFFFFFF) # -1 x 2 assert cx.mul == 0xFFFFFFFFFFFE def test_mh_and_ml_operands() -> None: # MUL, NOP, LD A,MH -> R0; LD A,ML words = [0x9861, 0x0000, 0x6001, 0xE060, 0x6002] cx = run(words, a=0x123456, r={1: 0x654321}) product = 0x123456 * 0x654321 assert cx.r[0] == product >> 24 and cx.a == product & 0xFFFFFF def test_mul_latency_switch() -> None: """C6: with latency 0 the next instruction reads the product; with 1, the one after.""" class Late(HG51B): MUL_LATENCY = 1 words = [0x9861, 0x6002, 0xE062, 0x6002] # MUL R1; LD A,ML; ST R2,A; LD A,ML cx = run(words, a=3, r={1: 5}) assert cx.r[2] == 15 and cx.a == 15 cx = run(words, a=3, r={1: 5}, core=Late) assert cx.r[2] == 0 and cx.a == 15