From 77067f6e244a9cf4a6ef59df2e3d735b4f172c35 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Sun, 24 Dec 2023 22:19:11 +1300 Subject: First commit --- src/devices/math.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/devices/math.rs (limited to 'src/devices/math.rs') diff --git a/src/devices/math.rs b/src/devices/math.rs new file mode 100644 index 0000000..6260529 --- /dev/null +++ b/src/devices/math.rs @@ -0,0 +1,35 @@ +pub struct MathDevice { + pub operand_1: u16, + pub operand_2: u16, + pub product_high: u16, + pub product_low: u16, + pub quotient: u16, + pub remainder: u16, +} + +impl MathDevice { + pub fn new() -> Self { + Self { + operand_1: 0x0000, + operand_2: 0x0000, + product_high: 0x0000, + product_low: 0x0000, + quotient: 0x0000, + remainder: 0x0000, + } + } + + pub fn multiply(&mut self) { + let (high, low) = self.operand_1.widening_mul(self.operand_2); + self.product_high = high; + self.product_low = low; + } + + pub fn divide(&mut self) { + self.quotient = self.operand_1.checked_div(self.operand_2).unwrap_or(0); + } + + pub fn modulo(&mut self) { + self.remainder = self.operand_1.checked_rem(self.operand_2).unwrap_or(0); + } +} -- cgit v1.2.3-70-g09d2