# SPDX-License-Identifier: MIT """Suzy's math unit: 16 x 16 multiply with accumulate, 32 / 16 divide. 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): 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. """ from __future__ import annotations # 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). A, B, C, D = 0x15, 0x14, 0x13, 0x12 E, F, G, H = 0x23, 0x22, 0x21, 0x20 J, K, L, M = 0x2F, 0x2E, 0x2D, 0x2C N, P = 0x17, 0x16 def _negative(value: int) -> int: # 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. return ((value - 1) >> 15) & 1 class MathUnit: __slots__ = ( "accumulate", "busy_until", "last_carry", "mathbit", "regs", "sign_ab", "sign_cd", "signed", ) # fmt: skip def __init__(self, regs: bytearray) -> None: self.regs = regs self.signed = 0 self.accumulate = 0 self.mathbit = 0 self.last_carry = 0 # SPRSYS B5: no rule found (U19); stays 0 self.busy_until = 0 self.sign_ab = 0 self.sign_cd = 0 def working(self, now: int) -> int: return 1 if now < self.busy_until else 0 def _get(self, *letters: int) -> int: value = 0 for letter in letters: value = (value << 8) | self.regs[letter] return value def _put(self, value: int, *letters: int) -> None: for letter in reversed(letters): self.regs[letter] = value & 0xFF value >>= 8 def written(self, reg: int, now: int) -> None: """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.""" if reg == M: self.mathbit = 0 # DOC: the write to M clears the accumulator overflow bit elif reg == C and self.signed: self.sign_cd = self._make_positive(C, D) elif reg == A: if self.signed: self.sign_ab = self._make_positive(A, B) self._multiply(now) elif reg == E: self._divide(now) def _make_positive(self, high: int, low: int) -> int: # 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). value = self._get(high, low) negative = _negative(value) if negative: self._put((-value) & 0xFFFF, high, low) return negative def _multiply(self, now: int) -> None: product = self._get(A, B) * self._get(C, D) negated = self.signed and self.sign_ab != self.sign_cd if negated: product = (-product) & 0xFFFFFFFF self._put(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 = 1 if negated else 0 if self.accumulate: total = self._get(J, K, L, M) + product if total > 0xFFFFFFFF: self.mathbit = 1 # DOC: mult: mathbit = accumulator overflow self.last_carry = 1 self._put(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 + (54 if (self.signed or self.accumulate) else 44) def _divide(self, now: int) -> None: divisor = self._get(N, P) dividend = self._get(E, F, G, H) 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 = divmod(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 = 1 if (remainder or divisor == 0) else 0 self._put(quotient, A, B, C, D) self._put(remainder, J, K, L, M) # DOC: J, K set to zero as a courtesy zeros = 16 - divisor.bit_length() self.busy_until = now + 176 + 14 * zeros # DOC: 176 + 14*N ticks