~maddie/custom-processor-simulator

7551a3680a32b1643251cfeb8ba696da6e052709 — Madeline Cronin 3 months ago 40dbe95
Write tests for control flow instructions and related behaviours.
1 files changed, 53 insertions(+), 4 deletions(-)

M src/processor.rs
M src/processor.rs => src/processor.rs +53 -4
@@ 220,7 220,8 @@ impl <'a> Processor<'a> {
                }
                let address = {
                    if toggles.2 { //Pop from the stack
                        self.reg[30] += 1;
                        self.reg[30] = self.reg[30].wrapping_add(1); //Operations influenced by
                                                                     //code should be unchecked.
                        self.memory[self.reg[30] as usize]
                    }
                    else { //Use a register value


@@ 231,7 232,7 @@ impl <'a> Processor<'a> {

                if toggles.1 { //Push to stack
                    self.memory[self.reg[30] as usize] = self.reg[31] + 2;
                    self.reg[30] -= 1;
                    self.reg[30] = self.reg[30].wrapping_sub(1);
                }
                if toggles.0 { //Relative jump
                    self.reg[31] = self.reg[31].wrapping_add(address);


@@ 362,7 363,7 @@ mod tests {
        assert_eq!(processor.reg[2], 7);
    }
    #[test]
    fn overflow () {
    fn add_overflow () {
        let mut memory = [0x0600, u16::MAX, 0x0608, 5, 0x0140, 0x0044, 0xA618, 5, 0, 0];
        let mut disk = [0];
        let mut vram = [0];


@@ 374,7 375,7 @@ mod tests {
        assert_eq!(processor.reg[3], 5);
    }
    #[test]
    fn signed_overflow () {
    fn add_signed_overflow () {
        let mut memory = [0x0600, i16::MAX as u16, 0x0608, 5, 0x01E0, 0x0044, 0x5618, 5, 0, 0];
        let mut disk = [0];
        let mut vram = [0];


@@ 590,4 591,52 @@ mod tests {
        }
        assert_eq!(processor.reg[2], 5);
    }
    #[test]
    fn cf_static_jump() {
        let mut memory = [0x0600, 6, 0x0400, 0x0000, 0x0600, 13, 0, 0];
        let mut disk = [0];
        let mut vram = [0];
        let mut processor = new(&mut memory, &mut disk, &mut vram);
        loop {
            if processor.run() {break};
        }
        assert_eq!(processor.reg[0], 6);
    }
    #[test]
    fn cf_dyn_jump() {
        let mut memory = [0x0600, 4, 0x0480, 0x0000, 0x0600, 13, 0, 0];
        let mut disk = [0];
        let mut vram = [0];
        let mut processor = new(&mut memory, &mut disk, &mut vram);
        loop {
            if processor.run() {break};
        }
        assert_eq!(processor.reg[0], 4);
    }
    #[test]
    fn cf_push_pop() {
        let mut memory = [0xA420, 0x0000, 0x0300, 0x0000, 0x06F0, 10, 0x0440, 0x0000, 0, 0, 0];
        let mut disk = [0];
        let mut vram = [0];
        let mut processor = new(&mut memory, &mut disk, &mut vram);
        let mut counter = 0;
        loop {
            if processor.run() {break};
            counter += 1;
            if counter > 20 {panic!()};
        }
    }
    #[test]
    fn cf_interrupt() {
        let mut memory = [0xA420, 0x0000, 0x0300, 0x0000, 0x06F0, 14, 0x0600, 2, 0x049C, 0x0000, 0x0450, 0x0000, 0, 0, 0];
        let mut disk = [0];
        let mut vram = [0];
        let mut processor = new(&mut memory, &mut disk, &mut vram);
        let mut counter = 0;
        loop {
            if processor.run() {break};
            counter += 1;
            if counter > 30 {panic!()};
        }
    }
}