From 46752df2c90292499dfee261ddba86582e6ec165 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Thu, 26 Jan 2023 00:02:22 +0100 Subject: [PATCH] Add a first implementation to fix register zero issue --- src/simulator/machine.rs | 59 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/src/simulator/machine.rs b/src/simulator/machine.rs index 83be4a4..31e4993 100644 --- a/src/simulator/machine.rs +++ b/src/simulator/machine.rs @@ -1,8 +1,61 @@ +use std::ops::{Add, Sub}; + use super::{decode::{Instruction, decode}}; use super::global::*; /// doit disparaitre const MEM_SIZE : usize = 4096; +trait RegisterNum: Add + Sub + PartialEq + Copy {} + +impl RegisterNum for i64 {} + +impl RegisterNum for f32 {} + +struct Register { + register: [U; 32] +} + +impl Register { + + pub fn get_reg(&self, position: usize) -> U { + self.register[position] + } + +} + +impl Register { + + pub fn init() -> Register { + Register { + register: [0i64; 32] + } + } + + pub fn write_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 write_reg(&mut self, position: usize, value: f32) { + self.register[position] = value; + } + +} + pub struct Machine { pub pc : u64, pub int_reg : [i64 ; 32], @@ -26,13 +79,17 @@ impl Machine { value >>= 1; } + // let int_reg = Register::::init(); + // let fp_reg = Register::::init(); + Machine { pc : 0, instructions : [0 ; 100], int_reg : [0 ; 32], main_memory : [0 ; MEM_SIZE], shiftmask - } + } + } /// Read from main memory of the machine