// SPDX-License-Identifier: MIT //! Suzy's math unit: 16 x 16 multiply with accumulate, 32 / 16 divide (`suzy/math.py`). //! //! Modelling level: functional results, with the documented completion times. //! //! DOC Spec §12 ("Hardware Multiply, Accumulate and Divide") and Appx 2. The //! registers are bytes of Suzy's register file, named by letter (Appx 2): //! //! ```text //! D=$52 C=$53 B=$54 A=$55 P=$56 N=$57 H=$60 G=$61 F=$62 E=$63 M=$6C L=$6D K=$6E J=$6F //! //! AB * CD -> EFGH, accumulated into JKLM; EFGH / NP -> ABCD, remainder (JK)LM. //! ``` //! //! Writing B, D, F, H, K or M writes 0 to A, C, E, G, J or L; writing A starts a //! multiply, writing E a divide; writing M clears the overflow bit. //! //! Differences forced by Rust: the Python unit keeps a reference to Suzy's register file; //! here the file is passed to each call (`regs`). use serde::{Deserialize, Serialize}; // Indices into Suzy's 64-byte register file: the math ports $FC52-$FC6F are the same // cells as $FC12-$FC2F (DOC Appx 2 labels them with the SCB register groups, H4, L5, ...; // TEST ROM memio 3: $FC40-$FC6F mirrors $FC00-$FC2F). pub const A: usize = 0x15; pub const B: usize = 0x14; pub const C: usize = 0x13; pub const D: usize = 0x12; pub const E: usize = 0x23; pub const F: usize = 0x22; pub const G: usize = 0x21; pub const H: usize = 0x20; pub const J: usize = 0x2F; pub const K: usize = 0x2E; pub const L: usize = 0x2D; pub const M: usize = 0x2C; pub const N: usize = 0x17; pub const P: usize = 0x16; fn negative(value: i64) -> u8 { // DOC §12 bugs: in signed multiply the hardware "thinks 8000 is a positive number" and // "thinks that 0 is a negative number". HYPOTHESIS: both follow from testing bit 15 of // (value - 1); TEST ROM math 3-4 decide. (((value - 1) >> 15) & 1) as u8 } #[derive(Clone, Default, Serialize, Deserialize)] pub struct MathUnit { pub accumulate: u8, pub busy_until: i64, pub last_carry: u8, pub mathbit: u8, pub sign_ab: u8, pub sign_cd: u8, pub signed: u8, } fn get(regs: &[u8], letters: &[usize]) -> i64 { let mut value = 0i64; for &letter in letters { value = (value << 8) | regs[letter] as i64; } value } fn put(regs: &mut [u8], mut value: i64, letters: &[usize]) { for &letter in letters.iter().rev() { regs[letter] = (value & 0xFF) as u8; value >>= 8; } } impl MathUnit { pub fn new() -> MathUnit { MathUnit { signed: 0, accumulate: 0, mathbit: 0, last_carry: 0, // SPRSYS B5: no rule found (U19); stays 0 busy_until: 0, sign_ab: 0, sign_cd: 0, } } pub fn working(&self, now: i64) -> u8 { if now < self.busy_until { 1 } else { 0 } } /// A CPU write to math port `reg` (an index; Suzy has stored the byte and, for /// an even index, zeroed the odd one above it: DOC "writing B, D, F, H, K or M /// will force a 0 to A, C, E, G, J or L"). HYPOTHESIS: the side effects below /// happen only at the math addresses $FC52-$FC6F, not through $FC12-$FC2F. pub fn written(&mut self, regs: &mut [u8], reg: usize, now: i64) { if reg == M { self.mathbit = 0; // DOC: the write to M clears the accumulator overflow bit } else if reg == C && self.signed != 0 { self.sign_cd = self.make_positive(regs, C, D); } else if reg == A { if self.signed != 0 { self.sign_ab = self.make_positive(regs, A, B); } self.multiply(regs, now); } else if reg == E { self.divide(regs, now); } } fn make_positive(&self, regs: &mut [u8], high: usize, low: usize) -> u8 { // DOC: the conversion happens when the upper byte is written; the number is // replaced by its magnitude and its sign kept (C7: a later low-byte write sets the // upper byte to 0 but does not change the sign). let value = get(regs, &[high, low]); let negative = negative(value); if negative != 0 { put(regs, (-value) & 0xFFFF, &[high, low]); } negative } fn multiply(&mut self, regs: &mut [u8], now: i64) { let mut product = get(regs, &[A, B]) * get(regs, &[C, D]); let negated = self.signed != 0 && self.sign_ab != self.sign_cd; if negated { product = (-product) & 0xFFFFFFFF; } put(regs, product, &[E, F, G, H]); // FITTED (TEST ROM math 1-4): SPRSYS B5 "last carry" reads 1 after a multiply whose // result was negated (math 3) or whose accumulate carried out (math 2), 0 after // plain products (math 1, 4). No document defines it (U19). Tried and rejected: // always 0 (fails math 2, 3), always 1 (fails math 1, 4). self.last_carry = if negated { 1 } else { 0 }; if self.accumulate != 0 { let total = get(regs, &[J, K, L, M]) + product; if total > 0xFFFFFFFF { self.mathbit = 1; // DOC: mult: mathbit = accumulator overflow self.last_carry = 1; } put(regs, total & 0xFFFFFFFF, &[J, K, L, M]); } // DOC: 44 ticks without sign or accumulate, 54 with both. HYPOTHESIS: either one // alone costs the 10 extra ticks too (TEST ROM math 8 times only the documented pair). self.busy_until = now + if self.signed != 0 || self.accumulate != 0 { 54 } else { 44 }; } fn divide(&mut self, regs: &mut [u8], now: i64) { let divisor = get(regs, &[N, P]); let dividend = get(regs, &[E, F, G, H]); let (quotient, remainder); if divisor == 0 { (quotient, remainder) = (0xFFFFFFFF, 0); // DOC: BigNum; HYPOTHESIS remainder 0 self.mathbit = 1; // DOC: div: mathbit = divide by zero attempted } else { (quotient, remainder) = (dividend / divisor, dividend % divisor); self.mathbit = 0; } // FITTED (TEST ROM math 5-7): "last carry" is 1 after a divide that leaves a // remainder (math 5) or divides by zero (math 7), 0 after an exact one (math 6). self.last_carry = if remainder != 0 || divisor == 0 { 1 } else { 0 }; put(regs, quotient, &[A, B, C, D]); put(regs, remainder, &[J, K, L, M]); // DOC: J, K set to zero as a courtesy let zeros = 16 - (64 - (divisor as u64).leading_zeros()) as i64; self.busy_until = now + 176 + 14 * zeros; // DOC: 176 + 14*N ticks } }