forked from Rativel/BurritOS
♻️ Simplified imports and moved Register struct to own file
This commit is contained in:
51
src/simulator/register.rs
Normal file
51
src/simulator/register.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use std::ops::{Add, Sub};
|
||||
|
||||
pub trait RegisterNum: Add<Output=Self> + Sub<Output=Self> + PartialEq + Copy {}
|
||||
|
||||
impl RegisterNum for i64 {}
|
||||
|
||||
impl RegisterNum for f32 {}
|
||||
|
||||
/// Machine register array
|
||||
#[derive(PartialEq)]
|
||||
pub struct Register<U: RegisterNum> {
|
||||
/// 32 available registers of type U
|
||||
register: [U; 32]
|
||||
}
|
||||
|
||||
impl<U: RegisterNum> Register<U> {
|
||||
|
||||
/// Returns the current value held in register *position*
|
||||
pub fn get_reg(&self, position: u8) -> U {
|
||||
self.register[position as usize]
|
||||
}
|
||||
|
||||
/// Set value of register *position* to *value*
|
||||
pub fn set_reg(&mut self, position: usize, value: U) {
|
||||
self.register[position] = value;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
impl Register<i64> {
|
||||
|
||||
/// i64 register constructor
|
||||
pub fn init() -> Register<i64> {
|
||||
Register {
|
||||
register: [0i64; 32]
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Register<f32> {
|
||||
|
||||
/// f32 register constructor
|
||||
pub fn init() -> Register<f32> {
|
||||
Register {
|
||||
register: [0f32; 32]
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user