~maddie/custom-processor-simulator

ref: 67a7a4393be532f0f7043a0649389e2d030c7bf0 custom-processor-simulator/src/processor.rs -rw-r--r-- 1.2 KiB
67a7a439Madeline Cronin Initial commit 24 days ago
                                                                                
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 Processor<'a> {
    reg: [u16;32],
    flags: [bool;2], //flag A, then B
    itr_toggle: bool,
    pub memory: &'a mut [u16],
    pub disk: &'a mut [u16],
}

pub fn new<'a>(memory: &'a mut [u16], disk: &'a mut [u16]) -> Processor<'a> {
    Processor {
        reg: [0;32],
        flags: [false;2],
        itr_toggle: false,
        memory,
        disk
    }
}

impl <'a> Processor<'a> {
    ///Runs the processor through one instruction.
    pub fn run(&mut self) {
        let instruction: (u16,u16) = (self.disk[self.reg[31] as usize],self.disk[self.reg[31] as usize +1]);
        let conditions: ((bool,bool),(bool,bool)) = (( //.0.x is activations, .1.x is conditions
                (instruction.0 & 0x80) != 0,
                (instruction.0 & 0x40) != 0),(
                (instruction.0 & 0x20) != 0,
                (instruction.0 & 0x10) != 0)
        );
        if (conditions.0.0 && (conditions.1.0 != self.flags[0])) || (conditions.0.1 && (conditions.1.1 != self.flags[1])) {
            self.reg[31] += 2; //update the program counter at the end of the instruction
                               //execution.
            return; //if the conditions are not met, perform no further calculations.
        }
    }
}