Add a first implementation to fix register zero issue

This commit is contained in:
Quentin Legot 2023-01-26 00:02:22 +01:00
parent 40374bf26f
commit 46752df2c9

View File

@ -1,8 +1,61 @@
use std::ops::{Add, Sub};
use super::{decode::{Instruction, decode}}; use super::{decode::{Instruction, decode}};
use super::global::*; use super::global::*;
/// doit disparaitre /// doit disparaitre
const MEM_SIZE : usize = 4096; const MEM_SIZE : usize = 4096;
trait RegisterNum: Add<Output=Self> + Sub<Output=Self> + PartialEq + Copy {}
impl RegisterNum for i64 {}
impl RegisterNum for f32 {}
struct Register<U: RegisterNum> {
register: [U; 32]
}
impl<U: RegisterNum> Register<U> {
pub fn get_reg(&self, position: usize) -> U {
self.register[position]
}
}
impl Register<i64> {
pub fn init() -> Register<i64> {
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<f32> {
pub fn init() -> Register<f32> {
Register {
register: [0f32; 32]
}
}
pub fn write_reg(&mut self, position: usize, value: f32) {
self.register[position] = value;
}
}
pub struct Machine { pub struct Machine {
pub pc : u64, pub pc : u64,
pub int_reg : [i64 ; 32], pub int_reg : [i64 ; 32],
@ -26,6 +79,9 @@ impl Machine {
value >>= 1; value >>= 1;
} }
// let int_reg = Register::<i64>::init();
// let fp_reg = Register::<f32>::init();
Machine { Machine {
pc : 0, pc : 0,
instructions : [0 ; 100], instructions : [0 ; 100],
@ -33,6 +89,7 @@ impl Machine {
main_memory : [0 ; MEM_SIZE], main_memory : [0 ; MEM_SIZE],
shiftmask shiftmask
} }
} }
/// Read from main memory of the machine /// Read from main memory of the machine