From 03cc8e17c6f8c837154ca0ea60d04ea300b9b989 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Autin?= Date: Fri, 24 Mar 2023 17:44:24 +0100 Subject: [PATCH] :recycle: Simplified imports and moved Register struct to own file --- src/simulator/machine.rs | 72 +++++++-------------------------------- src/simulator/mod.rs | 1 + src/simulator/register.rs | 51 +++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 60 deletions(-) create mode 100644 src/simulator/register.rs diff --git a/src/simulator/machine.rs b/src/simulator/machine.rs index 2cc98ad..116a942 100644 --- a/src/simulator/machine.rs +++ b/src/simulator/machine.rs @@ -1,10 +1,15 @@ -use std::{ops::{Add, Sub}, io::Write}; - -use crate::simulator::{print, error::MachineError}; - -use super::{decode::{Instruction, decode}, interrupt::Interrupt}; -use super::global::*; -use std::fs::File; +use std::{ + io::Write, + fs::File +}; +use crate::simulator::{ + print, + error::MachineError, + decode::*, + interrupt::Interrupt, + global::*, + register::* +}; /// Exceptions /// todo: is this really supposed to stand in machine.rs? @@ -39,59 +44,6 @@ pub const PAGE_SIZE : u64 = 128; /// Must be a multiple of PAGE_SIZE pub const MEM_SIZE : usize = (PAGE_SIZE*NUM_PHY_PAGE*100) as usize; -pub trait RegisterNum: Add + Sub + PartialEq + Copy {} - -impl RegisterNum for i64 {} - -impl RegisterNum for f32 {} - - -#[derive(PartialEq)] -pub struct Register { - register: [U; 32] -} - -impl Register { - - pub fn get_reg(&self, position: u8) -> U { - self.register[position as usize] - } - -} - -impl Register { - - pub fn init() -> Register { - Register { - register: [0i64; 32] - } - } - - pub fn set_reg(&mut self, position: usize, value: i64) { - if position != 0 { - self.register[position] = value; - } else { - // Panic ou rien ? (dans le doute pour le moment panic) - // unreachable!("You can't write to zero register") - } - } - -} - -impl Register { - - pub fn init() -> Register { - Register { - register: [0f32; 32] - } - } - - pub fn set_reg(&mut self, position: usize, value: f32) { - self.register[position] = value; - } - -} - #[derive(PartialEq)] pub struct Machine { pub pc : u64, diff --git a/src/simulator/mod.rs b/src/simulator/mod.rs index e13df71..956ee64 100644 --- a/src/simulator/mod.rs +++ b/src/simulator/mod.rs @@ -7,6 +7,7 @@ pub mod loader; pub mod interrupt; pub mod translationtable; pub mod mmu; +pub mod register; pub mod global { diff --git a/src/simulator/register.rs b/src/simulator/register.rs new file mode 100644 index 0000000..d352d74 --- /dev/null +++ b/src/simulator/register.rs @@ -0,0 +1,51 @@ +use std::ops::{Add, Sub}; + +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* + pub fn set_reg(&mut self, position: usize, value: U) { + self.register[position] = value; + } + + +} + +impl Register { + + /// i64 register constructor + pub fn init() -> Register { + Register { + register: [0i64; 32] + } + } + +} + +impl Register { + + /// f32 register constructor + pub fn init() -> Register { + Register { + register: [0f32; 32] + } + } + +} \ No newline at end of file