~maddie/custom-processor-simulator

1ac1ad24ff0febbb6f6a999d64e7bdf50ff62fa8 — Madeline Cronin 3 months ago 86418b4
Implement the rest of the arithmetic ops.
This implements subtraction, multiplication, and division, as well as the associated
checks for overflows and div by 0
1 files changed, 41 insertions(+), 1 deletions(-)

M src/processor.rs
M src/processor.rs => src/processor.rs +41 -1
@@ 72,7 72,47 @@ impl <'a> Processor<'a> {
                        else {self.reg[operands.2] = self.reg[operands.0].wrapping_add(self.reg[operands.1])};
                    }
                    0x0001 => { //Subtraction
                        
                        if toggles.1 {
                            (self.reg[operands.2], self.flags[toggles.2 as usize]) = {
                                if toggles.0 {
                                    let (uncast_return, flag) = (self.reg[operands.0] as i16).overflowing_add(-(self.reg[operands.1] as i16));
                                    (uncast_return as u16, flag)
                                }
                                else {
                                    (self.reg[operands.0].wrapping_sub(self.reg[operands.1]), self.reg[operands.0] < self.reg[operands.1])
                                }
                            }
                        }
                        else {
                            self.reg[operands.2] = self.reg[operands.0].wrapping_sub(self.reg[operands.1]);
                        }
                    }
                    0x0002 => { //Multiplication
                        if toggles.1 {
                            (self.reg[operands.2], self.flags[toggles.2 as usize]) = {
                                if toggles.0 {
                                    let (uncast_return, flag) = (self.reg[operands.1] as i16).overflowing_mul(self.reg[operands.1] as i16);
                                    (uncast_return as u16, flag)
                                }
                                else {
                                    self.reg[operands.0].overflowing_mul(self.reg[operands.1])
                                }
                            }
                        }
                        else {
                            self.reg[operands.2] = self.reg[operands.0].wrapping_mul(self.reg[operands.1]);
                        }
                    }
                    0x0003 => {
                        if self.reg[operands.2] == 0 && toggles.1 {self.flags[toggles.2 as usize] = true}
                        else if self.reg[operands.2] != 0 {
                            if toggles.0 {
                                self.reg[operands.2] = ((self.reg[operands.0] as i16)/(self.reg[operands.1] as i16)) as u16;
                            }
                            else {
                                self.reg[operands.2] = self.reg[operands.0]/self.reg[operands.1];
                            }
                        }
                    }
                    _ => return true
                }