// SPDX-License-Identifier: MIT //! The division unit (DIVU): signed 64 ÷ 32 and 32 ÷ 32 division, HM section 10. //! //! sh2-python's `divu.py`, rewritten there 2026-11-19 from the manuals alone (its //! docs/provenance.md). Each rule's source: //! //! - DOC (HM 10.2.2): a write to DVDNT also writes DVDNTL, extends DVDNT's sign into all //! of DVDNTH, and starts a 32 ÷ 32 division. So 32 ÷ 32 is the 64 ÷ 32 operation on a //! sign-extended dividend, and both kinds share one path here. //! - DOC (HM 10.3.1): a write to DVDNTL starts a 64 ÷ 32 division of DVDNTH:DVDNTL. //! - DOC (HM 10.3.1, 10.3.2): the quotient goes to DVDNTL (and DVDNT for 32 ÷ 32), the //! remainder to DVDNTH, 39 states after the start write; the operation is signed. //! - OBSERVED (MAME 0.285 ``sh7604.cpp``, BSD-3-Clause, read; jgenesis ``d41e2d9`` run as //! a closed box): the quotient rounds toward zero and the remainder takes the //! dividend's sign. HM does not say which signed convention the unit uses. //! - DOC (HM 10.3.3): an overflow is a quotient outside the signed 32-bit range, a //! divisor of 0, or "in division between two negative numbers, the quotient is the //! maximum value and a remainder (negative number) is generated". The last is read //! literally: dividend < 0, divisor < 0, quotient H'7FFFFFFF with a non-zero //! remainder. //! - DOC (HM 10.3.3, table 10.2): on an overflow OVF is set and the operation ends after //! 6 states, 3 of flag setting and 3 of division. DVDNTH holds the result at that //! point; DVDNTL (and DVDNT) hold it too when OVFIE = 1, and otherwise H'7FFFFFFF for a //! positive quotient or H'80000000 for a negative one. //! - HYPOTHESIS (provisional): what "the result after 6 cycles" is. Hitachi does not //! publish the unit's algorithm. ``early_state`` models it as the first three steps of //! the PM's own signed division routine (PM 6.19, example 4: a negative dividend //! decremented, DIV0S, then ROTCL low / DIV1 per step). No closed box available here //! models these values (docs/undocumented-behavior.md U12), and no hardware was run. //! - HYPOTHESIS: the quotient's sign when the divisor is 0 is the dividend's (a divisor //! of 0 counts as positive, as DIV0S's M bit would take it); jgenesis ``d41e2d9`` //! (closed box) saturates the same way for every case tried (U13). //! - HYPOTHESIS: DVDNT holds the same quotient as DVDNTL after a 64 ÷ 32 division too //! (table 10.2 gives DVDNT DVDNTL's values in both columns). //! - DOC (HM 10.4.1): an access while the unit divides is extended until it finishes. //! //! What Rust forces: Python's integers are unbounded; the dividend and quotient are //! ``i128`` here, so a quotient outside 64 bits (H'80000000_00000000 ÷ -1) is the same //! number the reference computes. use crate::arith::div1_step; use crate::bus::Bus; use crate::cpu::Sh7604; use crate::intc::Intc; use crate::module::{reg, value_of, ModuleState, Register}; pub const DIVIDE_STATES: i64 = 39; // HM 10.1.1, 10.3.1 pub const OVERFLOW_STATES: i64 = 6; // HM 10.3.3: "3 to set flags, 3 of division" const M32: u64 = 0xFFFF_FFFF; const M64: u128 = (1u128 << 64) - 1; pub const REGISTERS: [Register; 6] = [ reg(0xFFFFFF00, "DVSR", 32, None), reg(0xFFFFFF04, "DVDNT", 32, None), reg(0xFFFFFF08, "DVCR", 32, Some(0x00000000)), reg(0xFFFFFF0C, "VCRDIV", 32, None), reg(0xFFFFFF10, "DVDNTH", 32, None), reg(0xFFFFFF14, "DVDNTL", 32, None), ]; fn signed(value: u128, bits: u32) -> i128 { if (value >> (bits - 1)) != 0 { value as i128 - (1i128 << bits) } else { value as i128 } } /// Signed division toward zero; the remainder takes a's sign. fn trunc_div(a: i128, b: i128) -> (i128, i128) { let mut q = a.abs() / b.abs(); if (a < 0) != (b < 0) { q = -q; } (q, a - q * b) } /// DVDNTH and DVDNTL after an overflowing division's three division states. /// /// HYPOTHESIS (see the module docstring): the unit runs the PM 6.19 example-4 routine. /// A negative dividend is decremented (that routine's one's-complement step), DIV0S /// takes Q from the dividend's sign and M from the divisor's, then each step rotates /// the next dividend bit out of the low word (ROTCL) into a DIV1 on the high word. pub fn early_state(high: u32, low: u32, divisor: u32, steps: u32) -> (u32, u32) { let (mut high, mut low) = (high, low); if high >> 31 != 0 { let dividend = ((((high as u128) << 32) | low as u128).wrapping_sub(1)) & M64; high = (dividend >> 32) as u32; low = (dividend as u64 & M32) as u32; } let (mut q, m) = (high >> 31, divisor >> 31); let mut t = q ^ m; // DIV0S for _ in 0..steps { let carried = low >> 31; low = (low << 1) | t; // ROTCL low t = carried; let (h, nq, nt) = div1_step(high, divisor, t, q, m, false); high = h; q = nq; t = nt; } (high, low) } #[derive(Clone, Debug, Default)] pub struct Divu { pub dvsr: u32, pub dvdnt: u32, pub dvdnth: u32, pub dvdntl: u32, pub vcrdiv: u32, pub dvcr: u32, /// The clock at which the running division ends (no division: in the past). pub busy_until: i64, } impl Divu { pub fn new() -> Divu { Divu::default() } pub fn reset(&mut self) { // Only DVCR has a defined initial value (HM table 10.2); the others keep theirs. self.dvcr = 0; self.busy_until = 0; } pub fn requests(&self, intc: &Intc, out: &mut Vec<(u32, u32)>) { // OVFI: DVCR.OVF with OVFIE (HM 10.3.3), level IPRA.DIVUIP, vector VCRDIV. let level = intc.level("divu"); if level != 0 && self.dvcr & 3 == 3 { out.push((level, self.vcrdiv & 0x7F)); } } pub fn state(&self) -> ModuleState { vec![ ("DVSR", self.dvsr as i64), ("DVDNT", self.dvdnt as i64), ("DVCR", self.dvcr as i64), ("VCRDIV", self.vcrdiv as i64), ("DVDNTH", self.dvdnth as i64), ("DVDNTL", self.dvdntl as i64), ("busy_until", self.busy_until), ] } pub fn load_state(&mut self, values: &ModuleState) { self.dvsr = value_of(values, "DVSR") as u32; self.dvdnt = value_of(values, "DVDNT") as u32; self.dvcr = value_of(values, "DVCR") as u32; self.vcrdiv = value_of(values, "VCRDIV") as u32; self.dvdnth = value_of(values, "DVDNTH") as u32; self.dvdntl = value_of(values, "DVDNTL") as u32; self.busy_until = value_of(values, "busy_until"); } } impl Sh7604 { pub(crate) fn divu_read32(&mut self, address: u32) -> u32 { self.divu_wait(); let offset = address & 0x1F; let divu = &self.divu; match offset { 0x00 => divu.dvsr, 0x04 => divu.dvdnt, 0x08 => divu.dvcr, 0x0C => divu.vcrdiv, 0x10 | 0x18 => divu.dvdnth, 0x14 | 0x1C => divu.dvdntl, _ => 0, } } pub(crate) fn divu_write32(&mut self, address: u32, value: u32, mask: u32) { self.divu_wait(); let offset = address & 0x1F; if offset == 0x00 { self.divu.dvsr = (self.divu.dvsr & !mask) | value; } else if offset == 0x04 { self.divu.dvdnt = (self.divu.dvdnt & !mask) | value; self.divu_divide_32(); } else if offset == 0x08 { self.divu.dvcr = ((self.divu.dvcr & !mask) | value) & 3; self.intc_update(); } else if offset == 0x0C { self.divu.vcrdiv = ((self.divu.vcrdiv & !mask) | value) & 0xFFFF; self.intc_update(); } else if offset == 0x10 { self.divu.dvdnth = (self.divu.dvdnth & !mask) | value; } else if offset == 0x14 { self.divu.dvdntl = (self.divu.dvdntl & !mask) | value; self.divu_divide_64(); } } /// An access while dividing is extended until the division ends (HM 10.4.1). fn divu_wait(&mut self) { let remaining = self.divu.busy_until - self.clock; if remaining > 0 { self.stall(remaining); } } fn divu_overflow(&mut self) { self.divu.dvcr |= 1; self.divu.busy_until = self.clock + OVERFLOW_STATES; self.intc_update(); } fn divu_divide_32(&mut self) { // HM 10.2.2: DVDNT's value is also written to DVDNTL and its sign fills DVDNTH. self.divu.dvdntl = self.divu.dvdnt; self.divu.dvdnth = if self.divu.dvdnt >> 31 != 0 { M32 as u32 } else { 0 }; self.divu_divide_64(); } fn divu_divide_64(&mut self) { let (high, low, divisor) = (self.divu.dvdnth, self.divu.dvdntl, signed(self.divu.dvsr as u128, 32)); let dividend = signed(((high as u128) << 32) | low as u128, 64); let mut quotient = 0; let mut remainder = 0; let overflow = if divisor != 0 { let (q, r) = trunc_div(dividend, divisor); quotient = q; remainder = r; !(-0x8000_0000..=0x7FFF_FFFF).contains("ient) || (dividend < 0 && divisor < 0 && quotient == 0x7FFF_FFFF && remainder != 0) } else { true }; if !overflow { self.divu.busy_until = self.clock + DIVIDE_STATES; self.divu.dvdnth = (remainder & M32 as i128) as u32; self.divu.dvdntl = (quotient & M32 as i128) as u32; self.divu.dvdnt = self.divu.dvdntl; return; } let (dvdnth, partial_low) = early_state(high, low, self.divu.dvsr, 3); self.divu.dvdnth = dvdnth; if self.divu.dvcr & 2 != 0 { // OVFIE: both halves hold the early result (table 10.2) self.divu.dvdntl = partial_low; } else { // the quotient's sign picks the saturated value (HM 10.3.3) self.divu.dvdntl = if (dividend < 0) != (divisor < 0) { 0x8000_0000 } else { 0x7FFF_FFFF }; } self.divu.dvdnt = self.divu.dvdntl; self.divu_overflow(); } }