♻️ Simplified imports and moved Register struct to own file
This commit is contained in:
parent
c74c99499e
commit
03cc8e17c6
@ -1,10 +1,15 @@
|
|||||||
use std::{ops::{Add, Sub}, io::Write};
|
use std::{
|
||||||
|
io::Write,
|
||||||
use crate::simulator::{print, error::MachineError};
|
fs::File
|
||||||
|
};
|
||||||
use super::{decode::{Instruction, decode}, interrupt::Interrupt};
|
use crate::simulator::{
|
||||||
use super::global::*;
|
print,
|
||||||
use std::fs::File;
|
error::MachineError,
|
||||||
|
decode::*,
|
||||||
|
interrupt::Interrupt,
|
||||||
|
global::*,
|
||||||
|
register::*
|
||||||
|
};
|
||||||
|
|
||||||
/// Exceptions
|
/// Exceptions
|
||||||
/// todo: is this really supposed to stand in machine.rs?
|
/// 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
|
/// Must be a multiple of PAGE_SIZE
|
||||||
pub const MEM_SIZE : usize = (PAGE_SIZE*NUM_PHY_PAGE*100) as usize;
|
pub const MEM_SIZE : usize = (PAGE_SIZE*NUM_PHY_PAGE*100) as usize;
|
||||||
|
|
||||||
pub trait RegisterNum: Add<Output=Self> + Sub<Output=Self> + PartialEq + Copy {}
|
|
||||||
|
|
||||||
impl RegisterNum for i64 {}
|
|
||||||
|
|
||||||
impl RegisterNum for f32 {}
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
|
||||||
pub struct Register<U: RegisterNum> {
|
|
||||||
register: [U; 32]
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<U: RegisterNum> Register<U> {
|
|
||||||
|
|
||||||
pub fn get_reg(&self, position: u8) -> U {
|
|
||||||
self.register[position as usize]
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Register<i64> {
|
|
||||||
|
|
||||||
pub fn init() -> Register<i64> {
|
|
||||||
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<f32> {
|
|
||||||
|
|
||||||
pub fn init() -> Register<f32> {
|
|
||||||
Register {
|
|
||||||
register: [0f32; 32]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_reg(&mut self, position: usize, value: f32) {
|
|
||||||
self.register[position] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
pub struct Machine {
|
pub struct Machine {
|
||||||
pub pc : u64,
|
pub pc : u64,
|
||||||
|
@ -7,6 +7,7 @@ pub mod loader;
|
|||||||
pub mod interrupt;
|
pub mod interrupt;
|
||||||
pub mod translationtable;
|
pub mod translationtable;
|
||||||
pub mod mmu;
|
pub mod mmu;
|
||||||
|
pub mod register;
|
||||||
|
|
||||||
pub mod global {
|
pub mod global {
|
||||||
|
|
||||||
|
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]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user