~maddie/custom-processor-simulator

16add4eee325406205b152194e0206613b729fec — Madeline Cronin 3 months ago 59de9ed
Implement tests for multiplication related behaviours.
This also caught and fixed a bug with signed multiplication where the wrong operands were used.
1 files changed, 38 insertions(+), 3 deletions(-)

M src/processor.rs
M src/processor.rs => src/processor.rs +38 -3
@@ 97,7 97,7 @@ impl <'a> Processor<'a> {
                        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);
                                    let (uncast_return, flag) = (self.reg[operands.0] as i16).overflowing_mul(self.reg[operands.1] as i16);
                                    (uncast_return as u16, flag)
                                }
                                else {


@@ 264,7 264,7 @@ impl <'a> Processor<'a> {
                            self.memory[self.reg[operands.1] as usize] = self.reg[operands.0];
                        }
                        2 => { //Disk
                            self.disk[self.reg[operands.1] as usize] = self.reg[operands.0];
                            self.disk[self.reg[operands.1] as usize + ((self.reg[29] as usize) << 16)] = self.reg[operands.0];
                        }
                        3 => { //VRAM
                            self.vram[self.reg[operands.1] as usize] = self.reg[operands.0];


@@ 288,7 288,7 @@ impl <'a> Processor<'a> {
                            self.reg[operands.0] = self.memory[self.reg[30] as usize];
                        }
                        1 => self.reg[operands.0] = self.memory[self.reg[operands.1] as usize], //Main memory
                        2 => self.reg[operands.0] = self.disk[self.reg[operands.1] as usize], //Disk
                        2 => self.reg[operands.0] = self.disk[self.reg[operands.1] as usize + ((self.reg[29] as usize) << 16)], //Disk
                        3 => self.reg[operands.0] = self.vram[self.reg[operands.1] as usize], //VRAM
                        4 => { //Serial port
                            if self.stdin_buf == Vec::new() { //Fill stdin_buf if empty


@@ 432,4 432,39 @@ mod tests {
        assert_eq!(processor.reg[3], i16::MAX as u16 - 4);
        assert_eq!(processor.reg[4], 7);
    }
    #[test]
    fn mult () {
        let memory: &mut [u16] = &mut [0x0600, 3, 0x0608, 5, 0x0102, 0x0044, 0, 0];
        let disk: &mut [u16] = &mut [0];
        let vram: &mut [u16] = &mut [0];
        let mut processor = new(memory, disk, vram);
        loop {
            if processor.run() {break};
        }
        assert_eq!(processor.reg[2], 15);
    }
    #[test]
    fn mult_overflow () {
        let memory: &mut [u16] = &mut [0x0600, 3, 0x0608, u16::MAX, 0x0142, 0x0044, 0xA618, 5, 0, 0];
        let disk: &mut [u16] = &mut [0];
        let vram: &mut [u16] = &mut [0];
        let mut processor = new(memory, disk, vram);
        loop {
            if processor.run() {break};
        }
        assert_eq!(processor.reg[2], u16::MAX - 2);
        assert_eq!(processor.reg[3], 5);
    }
    #[test]
    fn mult_signed_overflow () {
        let memory: &mut [u16] = &mut [0x0600, 2, 0x0608, i16::MAX as u16, 0x01C2, 0x0044, 0xA618, 5, 0, 0];
        let disk: &mut [u16] = &mut [0];
        let vram: &mut [u16] = &mut [0];
        let mut processor = new(memory, disk, vram);
        loop {
            if processor.run() {break};
        }
        assert_eq!(processor.reg[2], u16::MAX - 1);
        assert_eq!(processor.reg[3], 5);
    }
}