~maddie/custom-processor-simulator

f6a0ff6eedba5e523cf5d81e0028a8adcdbdf87e — Madeline Cronin 3 months ago 425f054
Write tests for immediate values and addition related behaviours.
1 files changed, 73 insertions(+), 1 deletions(-)

M src/processor.rs
M src/processor.rs => src/processor.rs +73 -1
@@ 22,7 22,7 @@ impl <'a> Processor<'a> {
    ///Runs the processor through one instruction.
    ///Returns false for normal operation, and true to indicate that it should halt.
    pub fn run(&mut self) -> bool {
        let instruction: (u16,u16) = (self.disk[self.reg[31] as usize],self.disk[self.reg[31] as usize +1]);
        let instruction: (u16,u16) = (self.memory[self.reg[31] as usize],self.memory[self.reg[31] as usize +1]);
        let conditions: ((bool,bool),(bool,bool)) = (( //.0.x is activations, .1.x is conditions
                (instruction.0 & 0x8000) != 0,
                (instruction.0 & 0x4000) != 0),(


@@ 294,7 294,79 @@ impl <'a> Processor<'a> {
                self.reg[31] += 2;
                return false
            }
            0x0600 => {
                let operand = ((instruction.0 & 0x00F8) >> 3) as usize;
                if operand != 31 {
                    self.reg[operand] = instruction.1;
                }
                self.reg[31] += 2;
                return false
            }
            _ => return true,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn immediate() {
        let memory: &mut [u16] = &mut [0x0600, 45, 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[0], 45);
    }
    #[test]
    fn conditional() {
        let memory: &mut [u16] = &mut [0x0600, 17, 0xF600, 69, 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[0], 17);
    }

    #[test]
    fn add() {
        let memory: &mut [u16] = &mut [0x0600, 3, 0x0608, 4, 0x0100, 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], 7);
    }
    #[test]
    fn overflow () {
        let memory: &mut [u16] = &mut [0x0600, u16::MAX, 0x0608, 5, 0x0140, 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], 4);
        assert_eq!(processor.reg[3], 5);
    }
    #[test]
    fn signed_overflow () {
        let memory: &mut [u16] = &mut [0x0600, i16::MAX as u16, 0x0608, 5, 0x01E0, 0x0044, 0x5618, 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], i16::MIN as u16 + 4);
        assert_eq!(processor.reg[3], 5);
    }
}