# SPDX-License-Identifier: MIT """Rung 1: the pipeline, branches, jumps, LOOP, LINK, STOP (BK2 2-5-3, chapter 6 examples 1-14; U1-U3, U31, C1-C3). The examples are BK2's programs, laid out at 00:8000.""" from __future__ import annotations import pytest from conftest import make_gsu, run_to_stop, start from gsu_asm import ( ALT1, ASR, CACHE, GETB, LOOP, NOP, STOP, add, branch, dec, ibt, inc, iwt, jmp, link, ljmp, move, to, with_, ) from gsu_python import GSU from gsu_python._sfr import CY, OV, S, Z def layout(parts: dict[int, list[int]], size: int = 0x100) -> list[int]: """Place code at addresses from 8000h; gaps are STOP (00).""" code = [0] * size for address, part in parts.items(): code[address - 0x8000 : address - 0x8000 + len(part)] = part return code def run(code, *, cls=GSU, sfr=0, **regs): g = make_gsu(code, cls=cls) for name, value in regs.items(): g.r[int(name[1:])] = value g.sfr |= sfr start(g) run_to_stop(g) return g def test_example_1_delay_slot_executes(): """BNE FROG; INC R1 ... FROG: ADD R2: INC runs whether or not the branch is taken.""" code = layout({0x8000: branch("BNE", 0x0E) + inc(1), 0x8010: add(2) + STOP}) g = run(code, r0=1, r2=2) assert g.r[1] == 1 assert g.r[0] == 3 def test_example_2_split_instruction(): """BNE LOP1; BRA LOP2 ... LOP1: TO R1. The BRA opcode in the delay slot takes its operand from LOP1 (11h): it branches to LOP1 + 1 + 11h (U1).""" code = layout( { 0x8000: branch("BNE", 0x0E) + branch("BRA", 0x40), 0x8010: to(1) + NOP, 0x8022: ibt(5, 0x42) + STOP, } ) g = run(code) assert g.r[5] == 0x42 def test_example_4_iwt_r15_is_a_jump(): """IWT R0,#10h; IWT R4,#20h; IWT R15,#Address; NOP ... Address: ADD R4; INC R3.""" code = layout( {0x8000: iwt(0, 0x10) + iwt(4, 0x20) + iwt(15, 0x8040) + NOP, 0x8040: add(4) + inc(3)} ) g = run(code) assert g.r[0] == 0x30 assert g.r[3] == 1 def test_example_8_alt1_across_a_branch(): """U3: ALT1 in BNE's delay slot makes the target's ADD R1 an ADC R1.""" code = layout({0x8000: iwt(3, 3) + ALT1 + add(1) + dec(3) + branch("BNE", 0xFC) + ALT1 + STOP}) g = run(code, r1=1, sfr=CY) assert g.r[0] == 4 # ADC: (0+1+1) + (2+1+0) + (3+1+0); three ADDs would give 3 def test_example_10_with_across_a_branch(): """WITH R5 before BNE, TO R4 in its delay slot: MOVE R4,R5 (B survives the branch).""" code = layout( {0x8000: iwt(3, 2) + inc(0) + dec(3) + with_(5) + branch("BNE", 0xFB) + to(4) + STOP} ) g = run(code, r5=0x1234) assert g.r[4] == 0x1234 assert g.r[0] == 2 def test_example_12_loop(): """IWT R14,#DATA; IWT R12,#n; MOVE R13,R15; REPEAT: GETB; INC R14; LOOP; (delay slot). MOVE R13,R15 reads the address of the next instruction (U2).""" code = layout( { 0x8000: iwt(14, 0x8080) + iwt(12, 3) + move(13, 15), 0x8008: GETB + inc(14) + LOOP + inc(2) + STOP, 0x8080: [7, 8, 9], } ) g = run(code) assert g.r[13] == 0x8008 assert g.r[12] == 0 assert g.r[2] == 3 # the delay slot runs every time round, and after the last assert g.r[0] == 9 def test_examples_13_14_link_and_return(): """LINK #4; IWT R15,#SUB1; NOP; RETURN: INC R0 ... SUB1: ASR; ASR; MOVE R15,R11; NOP.""" code = layout( { 0x8000: link(4) + iwt(15, 0x8030) + NOP + inc(0) + STOP, 0x8030: ASR + ASR + move(15, 11) + NOP, } ) g = run(code, r0=8) assert g.r[11] == 0x8005 assert g.r[0] == 3 @pytest.mark.parametrize( ("name", "flags", "taken"), [ ("BRA", 0, True), ("BGE", 0, True), ("BGE", S, False), ("BGE", S | OV, True), ("BLT", S, True), ("BLT", OV, True), ("BLT", 0, False), ("BNE", 0, True), ("BNE", Z, False), ("BEQ", Z, True), ("BEQ", 0, False), ("BPL", 0, True), ("BPL", S, False), ("BMI", S, True), ("BMI", 0, False), ("BCC", 0, True), ("BCC", CY, False), ("BCS", CY, True), ("BCS", 0, False), ("BVC", 0, True), ("BVC", OV, False), ("BVS", OV, True), ("BVS", 0, False), ], ) # fmt: skip def test_branch_conditions(name, flags, taken): """BK2 9.14-9.26 with C2 (06 = BGE: (S xor OV) = 0).""" code = layout( { 0x8000: branch(name, 5) + NOP + ibt(5, 1) + STOP, 0x8006: NOP + ibt(5, 2) + STOP, } ) g = run(code, sfr=flags) assert g.r[5] == (2 if taken else 1) def test_prefixes_cleared_by_nop_and_jmp(): """U3: NOP and JMP clear the prefixes like any opcode but a prefix or a branch.""" code = layout({0x8000: ALT1 + NOP + add(1) + STOP}) assert run(code, r1=1, sfr=CY).r[0] == 1 # ADD, not ADC code = layout({0x8000: iwt(8, 0x8010) + to(3) + jmp(8) + NOP, 0x8010: add(1) + STOP}) g = run(code, r0=5, r1=1, r3=0) assert (g.r[0], g.r[3]) == (6, 0) # TO R3 was spent on the JMP def test_ljmp_operands_and_cache_c3(): """C3: LJMP Rn sets PBR = Rn and R15 = Sreg (fullsnes, emulators); CBR = target & FFF0h.""" code = layout({0x8000: iwt(8, 1) + iwt(0, 0x9004) + ljmp(8) + NOP}) rom_code = bytearray(0x20000) rom_code[: len(code)] = bytes(code) rom_code[0x9004 : 0x9004 + 3] = bytes(ibt(5, 0x77) + STOP) # 01:9004 (LoROM) g = GSU(bytes(rom_code), bytearray(0x10000)) start(g) run_to_stop(g) assert (g.pbr, g.cbr, g.r[5]) == (1, 0x9000, 0x77) class BK2Stop(GSU): STOP_R15_PLUS_TWO = False @pytest.mark.parametrize( ("case", "parts", "default", "bk2"), [ # BK2 2-5-3's four cases: R15 after STOP. ("transfer to R15", {0x8000: iwt(15, 0x8040) + STOP, 0x8040: NOP}, 0x8041, 0x8041), ("branch", {0x8000: branch("BRA", 0x3E) + STOP, 0x8040: NOP}, 0x8041, 0x8041), ("CACHE", {0x8000: CACHE + STOP}, 0x8003, 0x8002), ("other", {0x8000: NOP + STOP}, 0x8003, 0x8002), ], ) def test_r15_after_stop_c1(case, parts, default, bk2): """C1: STOP + 2 by default (emulators, MAME measured), STOP + 1 with BK2's table.""" code = layout(parts) assert run(code).r[15] == default assert run(code, cls=BK2Stop).r[15] == bk2 def test_restart_begins_with_the_pipeline_nop(): """After STOP the pipeline holds NOP (ares, MAME): a restart runs NOP first and fetches R15 behind it.""" g = make_gsu(layout({0x8000: inc(1) + STOP + inc(2) + STOP})) start(g) run_to_stop(g) assert g.pipeline == 0x01 g.write_io(0x301E, g.r[15] & 0xFF) g.write_io(0x301F, g.r[15] >> 8) run_to_stop(g) assert (g.r[1], g.r[2]) == (1, 0) # R15 = STOP + 2 skipped INC R2 (C1)