### whole module """Multiply: MUL, MLA, UMULL, UMLAL, SMULL, SMLAL and Thumb's MUL. The multiplier is an 8-bit-per-cycle Booth array: it takes ``m`` internal cycles, set by how many bytes of the multiplier operand Rs are significant (timing.md, TRM 6.20), and MLA and the long forms add one or two cycles more. The PC has advanced before the operands are read, so R15 as an operand reads plus 12 (U12: NBA's ordering, which the core copies; no hardware evidence). **The carry flag.** ARM calls C "meaningless" after a multiply, but it is deterministic: it is the last carry out of the Booth array. The functions below compute it the way zayd and calc84maniac derived it from a model fuzzed against a GBA (bmchtech, 2024; U11). They are a transcription of that algorithm, altered from its original form (C to Python, renamed), and keep its licence notice: Copyright (C) 2024 zaydlang, calc84maniac This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Rung 4 judges these against zayd's full Booth-array model (``reference/build/mul_model.so``). """ from __future__ import annotations from arm7tdmi_python._core import MASK32, CoreMixin from arm7tdmi_python._modes import FLAG_C, FLAG_N, FLAG_Z S_BIT = 1 << 20 def sign_extend(value: int, bits: int) -> int: """The low ``bits`` of value, sign-extended to 32 bits (unsigned result).""" if bits >= 32: return value & MASK32 value &= (1 << bits) - 1 return (value - (1 << bits)) & MASK32 if value >> (bits - 1) else value def multiply_cycles(multiplier: int, signed: bool) -> int: """m: 1 to 4 internal cycles, by the significant bytes of Rs (TRM 6.20, timing.md). Each cycle retires 8 bits; the array stops when the remaining bits are all zero, or, for a signed multiply, all one. UMULL/UMLAL stop only on zeros (DS 4.8.3, U11). """ for m, mask in enumerate((0xFFFFFF00, 0xFFFF0000, 0xFF000000), start=1): rest = multiplier & mask if rest == 0 or (signed and rest == mask): return m return 4 def carry_full(multiplier: int) -> int: """C after a multiply that used all four cycles: the last Booth digit's injected carry, set only when the top two multiplier bits are 10 (U11).""" return 1 if multiplier >> 30 == 2 else 0 def carry_low(multiplicand: int, multiplier: int, accumulator: int = 0) -> int: """C after a multiply that stopped early: bit 31 of the carry-save adder's last carry, replaying the Booth digits four at a time (U11).""" multiplicand |= 1 # makes negation invert the upper bits; cannot reach the carry bit booth = sign_extend(multiplier, 1) carry = (multiplicand * booth) & MASK32 total = (carry + accumulator) & MASK32 shift = 29 while True: for _ in range(4): next_booth = sign_extend(multiplier, 32 - shift) factor = (next_booth - booth) & MASK32 booth = next_booth addend = (multiplicand * factor) & MASK32 accumulator ^= carry ^ addend total = (total + addend) & MASK32 carry = (total - accumulator) & MASK32 shift -= 2 if booth == multiplier: return carry >> 31 def carry_high(multiplicand: int, multiplier: int, accumulator_high: int, signed: bool) -> int: """C after a four-cycle long multiply: bit 63 of the carry, from the last three Booth digits on the upper bits of the operands (U11).""" if signed: multiplicand = sign_extend(multiplicand >> 6, 26) multiplier = sign_extend(multiplier >> 26, 6) else: multiplicand >>= 6 multiplier >>= 26 multiplicand |= 1 carry = ~accumulator_high & 0x20000000 accumulator = (accumulator_high - 0x08000000) & MASK32 booth0 = sign_extend(multiplier, 5) booth1 = sign_extend(multiplier, 3) booth2 = sign_extend(multiplier, 1) factor0 = (multiplier - booth0) & MASK32 factor1 = (booth0 - booth1) & MASK32 factor2 = (booth1 - booth2) & MASK32 addend = (multiplicand * factor2) & MASK32 accumulator = (accumulator - (addend & 0x10000000)) & MASK32 addend = (multiplicand * factor1) & MASK32 accumulator = (accumulator - (addend & 0x40000000)) & MASK32 total = (accumulator + (addend & 0x20000000)) & MASK32 accumulator = (accumulator - carry) & MASK32 addend = (multiplicand * factor0) & MASK32 total = (total + (addend & 0x40000000)) & MASK32 return ((total ^ accumulator) & MASK32) >> 31 def signed32(value: int) -> int: return value - (1 << 32) if value & 0x80000000 else value class MultiplyMixin(CoreMixin): def _multiply_short(self, op: int, accumulate: bool) -> None: """MUL/MLA: Rd = Rm x Rs (+ Rn) (DS 4.7, timing.md tables 6-5/6-6).""" rd = (op >> 16) & 0xF pc = (self.r[15] - 8) & MASK32 self.r[15] = (self.r[15] + 4) & MASK32 # the PC advances before the operands (U12) rm = self._reg(op & 0xF) rs = self._reg((op >> 8) & 0xF) m = multiply_cycles(rs, signed=True) accumulator = 0 if accumulate: self._idle(pc + 8) # MLA's first I cycle is at pc+8, the rest at pc+12 accumulator = self._reg((op >> 12) & 0xF) for _ in range(m): self._idle(pc + 12) result = (rm * rs + accumulator) & MASK32 if op & S_BIT: self._set_nz(result) c = carry_full(rs) if m == 4 else carry_low(rm, rs, accumulator) self.cpsr = (self.cpsr & ~FLAG_C) | (FLAG_C if c else 0) # V unaffected (DS 4.7.2) self._set_reg(rd, result) self.next_fetch_seq = True # merged IS (C1) if rd == 15: self._refill() def _multiply_long(self, op: int, signed: bool, accumulate: bool) -> None: """UMULL/UMLAL/SMULL/SMLAL: RdHi:RdLo = Rm x Rs (+ RdHi:RdLo) (DS 4.8, table 6-7/6-8).""" rd_lo, rd_hi = (op >> 12) & 0xF, (op >> 16) & 0xF pc = (self.r[15] - 8) & MASK32 self.r[15] = (self.r[15] + 4) & MASK32 rm = self._reg(op & 0xF) rs = self._reg((op >> 8) & 0xF) m = multiply_cycles(rs, signed) for _ in range(m + 1 + accumulate): self._idle(pc + 12) product = signed32(rm) * signed32(rs) if signed else rm * rs accumulator_lo = accumulator_hi = 0 if accumulate: accumulator_lo, accumulator_hi = self._reg(rd_lo), self._reg(rd_hi) product += (accumulator_hi << 32) | accumulator_lo result = product & 0xFFFFFFFFFFFFFFFF if op & S_BIT: if m == 4: c = carry_high(rm, rs, accumulator_hi, signed) else: c = carry_low(rm, rs, accumulator_lo) self.cpsr = ( (self.cpsr & ~(FLAG_N | FLAG_Z | FLAG_C)) | ((result >> 32) & FLAG_N) | (FLAG_Z if result == 0 else 0) | (FLAG_C if c else 0) ) self._set_reg(rd_lo, result & MASK32) self._set_reg(rd_hi, result >> 32) self.next_fetch_seq = True # merged IS (C1) if rd_lo == 15 or rd_hi == 15: self._refill() def _arm_mul(self, op: int) -> None: """MUL -- Rd = Rm x Rs; S sets N, Z and the Booth carry (DS 4.7, U11).""" self._multiply_short(op, accumulate=False) def _arm_mla(self, op: int) -> None: """MLA -- Rd = Rm x Rs + Rn (DS 4.7, U11).""" self._multiply_short(op, accumulate=True) def _arm_umull(self, op: int) -> None: """UMULL -- RdHi:RdLo = Rm x Rs, unsigned (DS 4.8, U11).""" self._multiply_long(op, signed=False, accumulate=False) def _arm_umlal(self, op: int) -> None: """UMLAL -- RdHi:RdLo += Rm x Rs, unsigned (DS 4.8, U11).""" self._multiply_long(op, signed=False, accumulate=True) def _arm_smull(self, op: int) -> None: """SMULL -- RdHi:RdLo = Rm x Rs, signed (DS 4.8, U11).""" self._multiply_long(op, signed=True, accumulate=False) def _arm_smlal(self, op: int) -> None: """SMLAL -- RdHi:RdLo += Rm x Rs, signed (DS 4.8, U11).""" self._multiply_long(op, signed=True, accumulate=True) def _thumb_mul(self, op: int) -> None: """MUL Rd,Rs -- format 4: Rd = Rs x Rd, m I cycles at pc+3L (DS 5.4, U11).""" rd = op & 7 self.r[15] = (self.r[15] + 2) & MASK32 rs, multiplier = self.r[(op >> 3) & 7], self.r[rd] m = multiply_cycles(multiplier, signed=True) for _ in range(m): self._idle(self.r[15]) result = (rs * multiplier) & MASK32 self._set_nz(result) c = carry_full(multiplier) if m == 4 else carry_low(rs, multiplier) self.cpsr = (self.cpsr & ~FLAG_C) | (FLAG_C if c else 0) self.r[rd] = result self.next_fetch_seq = True # merged IS (C1)