// SPDX-License-Identifier: MIT //! The multiplier's instructions: MUL.L, MULS.W, MULU.W, DMULS.L, DMULU.L, MAC.W, MAC.L. //! //! sh2-python's `_multiply.py`. PM 6.20-6.21, 6.29, 6.31, 6.38-6.40. The multiplier is a //! unit beside the ALU (PM 7.7.2); its results go to MACH:MACL. Its timing (the ``mm`` //! states, the ID stalls) is the pipeline's business, rung 3; here are the values. //! //! MAC with SR.S = 1 saturates, and the manuals contradict themselves about how (C11 in //! sh2-python's docs/undocumented-behavior.md): //! //! - ``MAC.W``: the prose of PM 6.31 and SWM says "if an overflow occurs, the LSB of the //! MACH register is set to 1"; the code on the same pages marks the two lines that do //! it "For SH-1 CPU (these 2 lines not needed for SH-2 CPU)". This core follows the //! code's SH-2 annotation: MACL saturates, MACH is unchanged. MAME and jgenesis agree; //! Ymir's vectors (origin unstated) set the bit. //! - ``MAC.L``: the prose of PM 6.29 limits the result to H'FFFF8000_00000000 .. //! H'00007FFF_FFFFFFFF using "only the lower 48 bits" of MAC; the code saturates the //! negative side to H'00008000_00000000 and adds MACH's low 16 bits unsigned. This core //! follows the prose: the accumulator is MAC's low 48 bits, sign-extended, and the sum //! is clamped to the signed 48-bit range and stored sign-extended to 64 bits. use crate::bus::Bus; use crate::cpu::Sh7604; pub const S: u32 = 0x002; fn s32(value: u32) -> i64 { value as i32 as i64 } fn s16(value: u32) -> i64 { (value & 0xFFFF) as u16 as i16 as i64 } impl Sh7604 { /// ``_set_mac``: the low 64 bits of ``value`` (two's complement) -> MACH:MACL. fn set_mac(&mut self, value: u64) { self.mach = (value >> 32) as u32; self.macl = value as u32; } /// MUL.L Rm,Rn -- the low 32 bits of Rn x Rm -> MACL; MACH unchanged (PM 6.38). pub(crate) fn op_mul_l(&mut self, op: u32) { self.macl = self.r[((op >> 8) & 15) as usize].wrapping_mul(self.r[((op >> 4) & 15) as usize]); } /// MULS.W Rm,Rn -- signed 16 x 16 of the low words -> MACL; MACH unchanged (PM 6.39). pub(crate) fn op_muls_w(&mut self, op: u32) { self.macl = (s16(self.r[((op >> 8) & 15) as usize]) * s16(self.r[((op >> 4) & 15) as usize])) as u32; } /// MULU.W Rm,Rn -- unsigned 16 x 16 of the low words -> MACL; MACH unchanged (PM 6.40). pub(crate) fn op_mulu_w(&mut self, op: u32) { self.macl = (self.r[((op >> 8) & 15) as usize] & 0xFFFF).wrapping_mul(self.r[((op >> 4) & 15) as usize] & 0xFFFF); } /// DMULS.L Rm,Rn -- signed 32 x 32 -> the 64-bit MACH:MACL (PM 6.20). pub(crate) fn op_dmuls_l(&mut self, op: u32) { let product = s32(self.r[((op >> 8) & 15) as usize]) * s32(self.r[((op >> 4) & 15) as usize]); self.set_mac(product as u64); } /// DMULU.L Rm,Rn -- unsigned 32 x 32 -> the 64-bit MACH:MACL (PM 6.21). pub(crate) fn op_dmulu_l(&mut self, op: u32) { let product = self.r[((op >> 8) & 15) as usize] as u64 * self.r[((op >> 4) & 15) as usize] as u64; self.set_mac(product); } /// MAC.W @Rm+,@Rn+ -- signed (Rn) x (Rm) + MAC -> MAC; Rn, Rm + 2 (PM 6.31). /// /// Reads @Rn first, then @Rm (PM 6.31's code). S = 0: a 64-bit accumulate. S = 1: /// MACL + product saturates to H'80000000..H'7FFFFFFF and MACH is unchanged (the /// code's SH-2 annotation; C11). pub(crate) fn op_mac_w(&mut self, op: u32) { let (n, m) = (((op >> 8) & 15) as usize, ((op >> 4) & 15) as usize); let tn = s16(self.read16(self.r[n])); self.r[n] = self.r[n].wrapping_add(2); let tm = s16(self.read16(self.r[m])); self.r[m] = self.r[m].wrapping_add(2); let product = tn * tm; if self.sr & S != 0 { let mut total = s32(self.macl) + product; if total > 0x7FFFFFFF { total = 0x7FFFFFFF; } else if total < -0x80000000 { total = -0x80000000; } self.macl = total as u32; } else { let mac = ((self.mach as u64) << 32) | self.macl as u64; self.set_mac(mac.wrapping_add(product as u64)); } } /// MAC.L @Rm+,@Rn+ -- signed (Rn) x (Rm) + MAC -> MAC; Rn, Rm + 4 (PM 6.29). /// /// Reads @Rn first, then @Rm. S = 0: a 64-bit accumulate. S = 1: MAC's low 48 bits /// plus the product, clamped to the signed 48-bit range (PM 6.29's prose; C11). pub(crate) fn op_mac_l(&mut self, op: u32) { let (n, m) = (((op >> 8) & 15) as usize, ((op >> 4) & 15) as usize); let tn = s32(self.read32(self.r[n])); self.r[n] = self.r[n].wrapping_add(4); let tm = s32(self.read32(self.r[m])); self.r[m] = self.r[m].wrapping_add(4); let product = tn * tm; let mac = ((self.mach as u64) << 32) | self.macl as u64; if self.sr & S != 0 { let mut low48 = (mac & 0xFFFFFFFFFFFF) as i64; if low48 & 0x800000000000 != 0 { low48 -= 1 << 48; } let mut total = low48 + product; if total > 0x7FFFFFFFFFFF { total = 0x7FFFFFFFFFFF; } else if total < -0x800000000000 { total = -0x800000000000; } self.set_mac(total as u64); } else { self.set_mac(mac.wrapping_add(product as u64)); } } }