//! # Register //! //! This mod contains the definition of the Register structs //! for use within the Machine module. use crate::simulator::machine::{NUM_FP_REGS, NUM_INT_REGS}; use std::ops::{Add, Sub}; /// Forcing the Register struct's generic type into having the following Traits /// /// - Add /// - Sub /// - PartialEq /// - Copy /// /// Generally speaking, only numbers have the combinaison of these traits. pub trait RegisterNum: Add + Sub + PartialEq + Copy {} impl RegisterNum for i64 {} impl RegisterNum for f32 {} /// Machine register array #[derive(PartialEq)] pub struct Register { /// 32 available registers of type U register: [U; 32] } impl Register { /// 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* /// /// Checking against trying to set a new value to the 0th register /// as its value is NOT supposed to change pub fn set_reg(&mut self, position: u8, value: U) { if position != 0 { self.register[position as usize] = value; } } } impl Register { /// i64 register constructor pub fn init() -> Register { Register { register: [0i64; NUM_INT_REGS] } } } impl Register { /// f32 register constructor pub fn init() -> Register { Register { register: [0f32; NUM_FP_REGS] } } }