# SPDX-License-Identifier: MIT """Rung 1: MULT, UMULT, FMULT, LMULT (BK2 8.2, 9.33, 9.52, 9.66, 9.67, 9.90; C9, U25).""" from __future__ import annotations from conftest import execute, make_gsu, run_to_stop, start from gsu_asm import FMULT, LMULT, STOP, mult, mult_i, program, to, umult, umult_i from gsu_python import GSU, MC1 from gsu_python._sfr import CY, S, Z def run(*parts, **regs): return execute(program(*parts, STOP), **regs) def test_mult_signed_bytes(): g = run(mult(1), r0=0x12FF, r1=0x3402) # -1 x 2 assert g.r[0] == 0xFFFE assert g.sfr & (S | Z) == S assert run(mult_i(15), r0=0x00F0).r[0] == (-16 * 15) & 0xFFFF def test_umult_unsigned_bytes(): assert run(umult(1), r0=0x00FF, r1=0x00FF).r[0] == 0xFE01 assert run(umult_i(15), r0=0x00FF).r[0] == 0x0EF1 def test_fmult_high_word_and_carry(): """FMULT: Dreg = (Sreg x R6) >> 16 signed; CY = product bit 15.""" g = run(FMULT, r0=0x4000, r6=0x4000) # 1000_0000h assert g.r[0] == 0x1000 assert not g.sfr & CY g = run(FMULT, r0=0x0001, r6=0x8000) # -32768 -> FFFF_8000h assert g.r[0] == 0xFFFF assert g.sfr & (S | CY) == S | CY def test_lmult_low_word_to_r4(): g = run(LMULT, r0=0x1234, r6=0x5678) product = 0x1234 * 0x5678 assert (g.r[0], g.r[4]) == (product >> 16, product & 0xFFFF) def test_lmult_into_r4_keeps_the_high_word(): """BK2 8.2 CAUTION: with Dreg = R4, LMULT leaves the high word in R4 (all sources).""" g = run(to(4), LMULT, r0=0x1234, r6=0x5678) assert g.r[4] == (0x1234 * 0x5678) >> 16 def test_fmult_into_r4_c9(): """C9: BK2 9.33 says FMULT into R4 is lost (R4 unchanged); the emulators store it.""" g = run(to(4), FMULT, r0=0x4000, r6=0x4000, r4=0xAAAA) assert g.r[4] == 0xAAAA class Emulators(GSU): FMULT_R4_UNCHANGED = False code = program(to(4), FMULT, STOP) g = make_gsu(code, cls=Emulators) g.r[0], g.r[6], g.r[4] = 0x4000, 0x4000, 0xAAAA start(g) run_to_stop(g) assert g.r[4] == 0x1000 def test_mc1_ignores_ms0(): """U25: the MC1 always multiplies at standard speed, whatever CFGR bit 5 says.""" code = program(FMULT, STOP) times = {} for name, model in (("gsu2", None), ("mc1", MC1)): g = make_gsu(code, model=model) if model else make_gsu(code) start(g, cfgr=0xA0) # MS0 = 1, IRQ masked g.step() # the pipeline NOP times[name] = g.step() assert times["mc1"] - times["gsu2"] == 4 # 7 vs 3 extra cycles at CLSR = 1