# SPDX-License-Identifier: MIT """The multiplier: MULT, UMULT, FMULT, LMULT (BK2 8.2, 9.33, 9.52, 9.66, 9.67, 9.90). MULT/UMULT multiply the low bytes (signed/unsigned) into a 16-bit Dreg. FMULT multiplies Sreg by R6, both signed 16-bit, and keeps the high word of the 32-bit product; LMULT also puts the low word in R4. CY is product bit 15 (the bit FMULT's rounding would use). BK2 8.2.1 describes the circuit: four 8 x 8 multiplies into a partial-product buffer, R4's low byte written after the first and its high byte after the third, Dreg last. So with Dreg = R4, **LMULT** leaves the high word in R4 (BK2 8.2 CAUTION, which every emulator also gives), and **FMULT** "will not be stored in R4 and the results will be lost" (BK2 9.33): R4 is unchanged (C9; fullsnes "reportedly leave R4 unchanged"). The emulators write the high word there; ``FMULT_R4_UNCHANGED = False`` gives that. Time (C14, BK2's tables = ares ``instructions.cpp`` l.228, l.304): the extra cycles depend on CFGR.MS0, which the MC1 does not implement (U25: "always using slow multiply mode"). """ from __future__ import annotations from gsu_python import timing from gsu_python._sfr import CY, S, Z _SCZ = ~(S | CY | Z) _SZ = ~(S | Z) def _signed8(value: int) -> int: value &= 0xFF return value - 0x100 if value & 0x80 else value def _signed16(value: int) -> int: return value - 0x10000 if value & 0x8000 else value class MultiplyMixin: r: list[int] sfr: int sreg: int dreg: int cfgr: int clsr: int clock: int _ms0_implemented: bool #: C9. True (default): FMULT with Dreg = R4 leaves R4 unchanged (BK2 9.33, fullsnes). #: False: R4 gets the high word (ares ``instructions.cpp`` l.296-299; MAME). FMULT_R4_UNCHANGED = True def _high_speed(self) -> bool: """CFGR bit 5 (MS0) on a chip that has it (U25).""" return bool(self.cfgr & 0x20) and self._ms0_implemented def _byte_product(self, value: int) -> None: value &= 0xFFFF self._set(self.dreg, value) self.sfr = (self.sfr & _SZ) | (S if value & 0x8000 else 0) | (0 if value else Z) if not self._high_speed(): self.clock += timing.MULT_SLOW_EXTRA[self.clsr] def _op_mult_r(self, n: int) -> None: """MULT Rn (BK2 9.66): Dreg = signed(Sreg low byte) x signed(Rn low byte).""" self._byte_product(_signed8(self.r[self.sreg]) * _signed8(self.r[n])) def _op_mult_i(self, n: int) -> None: """MULT #n (BK2 9.67): Dreg = signed(Sreg low byte) x n (0-15).""" self._byte_product(_signed8(self.r[self.sreg]) * n) def _op_umult_r(self, n: int) -> None: """UMULT Rn (BK2 9.90): unsigned bytes.""" self._byte_product((self.r[self.sreg] & 0xFF) * (self.r[n] & 0xFF)) def _op_umult_i(self, n: int) -> None: """UMULT #n (fullsnes; BK2 lists it in 2-2-6 only).""" self._byte_product((self.r[self.sreg] & 0xFF) * n) def _word_product(self, long: bool) -> None: product = (_signed16(self.r[self.sreg]) * _signed16(self.r[6])) & 0xFFFFFFFF high = product >> 16 if long: self._set(4, product & 0xFFFF) if long or self.dreg != 4 or not self.FMULT_R4_UNCHANGED: self._set(self.dreg, high) sfr = self.sfr & _SCZ if high & 0x8000: sfr |= S if product & 0x8000: sfr |= CY if not high: sfr |= Z self.sfr = sfr extra = timing.FMULT_FAST_EXTRA if self._high_speed() else timing.FMULT_SLOW_EXTRA self.clock += extra[self.clsr] def _op_fmult(self, n: int) -> None: """FMULT (BK2 9.33): Dreg = (Sreg x R6) >> 16.""" self._word_product(False) def _op_lmult(self, n: int) -> None: """LMULT (BK2 9.52): Dreg = (Sreg x R6) >> 16, R4 = the low word.""" self._word_product(True) __all__ = ["MultiplyMixin"]