~maddie/custom-processor-simulator

67a7a4393be532f0f7043a0649389e2d030c7bf0 — Madeline Cronin 24 days ago
Initial commit
Creates processor.rs as well as the processor struct and the start of the code for executing instructions.
5 files changed, 58 insertions(+), 0 deletions(-)

A .gitignore
A Cargo.lock
A Cargo.toml
A src/main.rs
A src/processor.rs
A  => .gitignore +3 -0
@@ 1,3 @@
/target
*~
*.swp

A  => Cargo.lock +7 -0
@@ 1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "simulator"
version = "0.1.0"

A  => Cargo.toml +8 -0
@@ 1,8 @@
[package]
name = "simulator"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

A  => src/main.rs +5 -0
@@ 1,5 @@
mod processor;

fn main() {
    println!("Hello, world!");
}

A  => src/processor.rs +35 -0
@@ 1,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.
        }
    }
}