# SPDX-License-Identifier: MIT """The disassembler's syntax (BK2 mnemonics, prefixes folded: handoff-brief decision 12).""" from __future__ import annotations from gsu_python.disasm import disassemble, disassemble_bytes def texts(code: list[int], address: int = 0x8000) -> list[str]: return [i.text for i in disassemble_bytes(bytes(code), address)] def test_brief_examples(): assert texts([0x3D, 0x53]) == ["ADC R3"] assert texts([0x3E, 0x53]) == ["ADD #3"] assert texts([0x25, 0x14]) == ["MOVE R4,R5"] # WITH R5; TO R4 assert texts([0x24, 0xB5]) == ["MOVES R4,R5"] # WITH R4; FROM R5 assert texts([0x3D, 0xA1, 0x12]) == ["LMS R1,(024h)"] assert texts([0xFF, 0x03, 0xA1]) == ["IWT R15,#A103h"] def test_operand_forms(): assert texts([0xA1, 0x80]) == ["IBT R1,#80h"] assert texts([0x3D, 0xF2, 0x34, 0x12]) == ["LM R2,(1234h)"] assert texts([0x3E, 0xF2, 0x34, 0x12]) == ["SM (1234h),R2"] assert texts([0x3E, 0xA2, 0x12]) == ["SMS (024h),R2"] assert texts([0x33, 0x3D, 0x43]) == ["STW (R3)", "LDB (R3)"] assert texts([0x93, 0x9B, 0x3D, 0x9B]) == ["LINK #3", "JMP R11", "LJMP R11"] assert texts([0x3F, 0xDF, 0x3E, 0xDF, 0xDF]) == ["ROMB", "RAMB", "GETC"] def test_branch_target(): """The target is the address after the displacement plus the displacement.""" assert texts([0x08, 0xFE], 0x8000) == ["BNE 8000h"] assert texts([0x05, 0x10], 0x9000) == ["BRA 9012h"] def test_register_prefixes_stay_in_front(): assert texts([0x21, 0x50]) == ["WITH R1; ADD R0"] assert texts([0xB4, 0x12, 0x3D, 0x53]) == ["FROM R4; TO R2; ADC R3"] assert [i.length for i in disassemble_bytes(bytes([0xB4, 0x12, 0x3D, 0x53]))] == [4] def test_prefix_before_branch_is_alone(): """U3: the prefix applies after the branch, so it is printed by itself.""" assert texts([0x3D, 0x08, 0x02]) == ["ALT1", "BNE 8005h"] def test_live_state(): """Disassembling at a live PC with ALT1 already set (the trace's SFR).""" code = {0x8000: 0x50} instruction = disassemble(lambda a: code.get(a, 0), 0x8000, alt=1) assert instruction.text == "ADC R0"