summaryrefslogtreecommitdiff
path: root/src/devices/math.rs
blob: c8d2194f5163c4432b0a868637c52ddead62815e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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 (low, high) = 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);
    }
}