2023-02-13 11:08:24 +01:00
|
|
|
use std::{ops::{Add, Sub}, io::Write};
|
2023-01-26 00:02:22 +01:00
|
|
|
|
2023-03-10 19:17:41 +01:00
|
|
|
use crate::simulator::print;
|
|
|
|
|
2023-03-08 13:21:08 +01:00
|
|
|
use super::{decode::{Instruction, decode}, interrupt::Interrupt};
|
2023-01-20 18:21:32 +01:00
|
|
|
use super::global::*;
|
2023-02-13 11:08:24 +01:00
|
|
|
use std::fs::File;
|
2023-02-08 14:34:09 +01:00
|
|
|
|
2023-03-01 15:45:49 +01:00
|
|
|
pub const STACK_REG: usize = 2;
|
|
|
|
|
2023-02-28 14:43:40 +01:00
|
|
|
pub const NUM_INT_REGS: usize = 32;
|
|
|
|
pub const NUM_FP_REGS: usize = 32;
|
|
|
|
|
2022-11-23 16:04:21 +01:00
|
|
|
/// doit disparaitre
|
2023-03-01 16:12:46 +01:00
|
|
|
const MEM_SIZE : usize = 0x500000;
|
2022-11-15 21:21:24 +01:00
|
|
|
|
2023-02-01 17:10:11 +01:00
|
|
|
pub trait RegisterNum: Add<Output=Self> + Sub<Output=Self> + PartialEq + Copy {}
|
2023-01-26 00:02:22 +01:00
|
|
|
|
|
|
|
impl RegisterNum for i64 {}
|
|
|
|
|
|
|
|
impl RegisterNum for f32 {}
|
|
|
|
|
2023-02-08 14:34:09 +01:00
|
|
|
|
2023-03-08 15:48:33 +01:00
|
|
|
#[derive(PartialEq)]
|
2023-02-01 17:10:11 +01:00
|
|
|
pub struct Register<U: RegisterNum> {
|
2023-01-26 00:02:22 +01:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 00:08:49 +01:00
|
|
|
pub fn set_reg(&mut self, position: usize, value: i64) {
|
2023-01-26 00:02:22 +01:00
|
|
|
if position != 0 {
|
|
|
|
self.register[position] = value;
|
|
|
|
} else {
|
|
|
|
// Panic ou rien ? (dans le doute pour le moment panic)
|
2023-03-05 23:49:28 +01:00
|
|
|
// unreachable!("You can't write to zero register")
|
2023-01-26 00:02:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Register<f32> {
|
|
|
|
|
|
|
|
pub fn init() -> Register<f32> {
|
|
|
|
Register {
|
|
|
|
register: [0f32; 32]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 00:08:49 +01:00
|
|
|
pub fn set_reg(&mut self, position: usize, value: f32) {
|
2023-01-26 00:02:22 +01:00
|
|
|
self.register[position] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-08 15:48:33 +01:00
|
|
|
#[derive(PartialEq)]
|
2022-11-09 15:59:05 +01:00
|
|
|
pub struct Machine {
|
2022-11-23 16:29:02 +01:00
|
|
|
pub pc : u64,
|
2023-02-08 14:46:56 +01:00
|
|
|
pub sp: usize,
|
2023-02-01 16:39:40 +01:00
|
|
|
pub int_reg : Register<i64>,
|
2023-02-01 17:26:34 +01:00
|
|
|
pub fp_reg : Register<f32>,
|
2023-03-11 23:49:20 +01:00
|
|
|
pub main_memory : Vec<u8>,
|
2023-03-08 13:21:08 +01:00
|
|
|
pub shiftmask : [u64 ; 64],
|
2023-03-09 12:08:33 +01:00
|
|
|
pub registers_trace : String, // for tests
|
|
|
|
pub interrupt: Interrupt
|
2022-11-15 21:21:24 +01:00
|
|
|
// futur taille à calculer int memSize = g_cfg->NumPhysPages * g_cfg->PageSize;
|
|
|
|
//creer une struct cfg(configuration) qui s'initialise avec valeur dans un fichier cfg
|
2022-11-09 15:59:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Machine {
|
2022-11-09 16:47:26 +01:00
|
|
|
|
2023-03-11 23:49:20 +01:00
|
|
|
pub fn init_machine() -> Machine {
|
2022-11-23 16:29:02 +01:00
|
|
|
let mut shiftmask : [u64 ; 64] = [0 ; 64];
|
|
|
|
let mut value : u64 = 0xffffffff;
|
2022-11-16 17:59:09 +01:00
|
|
|
|
2022-11-23 16:29:02 +01:00
|
|
|
value = (value << 32) + value;
|
2023-01-11 15:04:54 +01:00
|
|
|
for item in &mut shiftmask {
|
|
|
|
*item = value;
|
2023-01-11 15:34:12 +01:00
|
|
|
value >>= 1;
|
2022-11-16 17:59:09 +01:00
|
|
|
}
|
2022-11-09 15:59:05 +01:00
|
|
|
|
2023-03-08 17:58:38 +01:00
|
|
|
let mut ret = Machine {
|
2022-11-09 16:47:26 +01:00
|
|
|
pc : 0,
|
2023-02-08 14:46:56 +01:00
|
|
|
sp: 0,
|
2023-02-01 16:39:40 +01:00
|
|
|
int_reg : Register::<i64>::init(),
|
2023-02-01 17:29:31 +01:00
|
|
|
fp_reg : Register::<f32>::init(),
|
2023-03-11 23:49:20 +01:00
|
|
|
main_memory : vec![0_u8; MEM_SIZE],
|
2023-03-08 17:58:38 +01:00
|
|
|
shiftmask,
|
2023-03-09 12:08:33 +01:00
|
|
|
interrupt: Interrupt::new(),
|
2023-03-08 17:58:38 +01:00
|
|
|
registers_trace : String::from("")
|
|
|
|
};
|
|
|
|
|
|
|
|
ret.int_reg.set_reg(10, -1);
|
2023-03-09 12:08:33 +01:00
|
|
|
ret
|
2022-11-09 15:59:05 +01:00
|
|
|
}
|
2022-11-09 16:47:26 +01:00
|
|
|
|
2022-11-23 18:04:35 +01:00
|
|
|
/// Read from main memory of the machine
|
|
|
|
///
|
2023-01-18 15:03:58 +01:00
|
|
|
/// `panic!` when size is not 1, 2, 4 or 8
|
|
|
|
///
|
2022-11-23 18:04:35 +01:00
|
|
|
/// ### Parameters
|
|
|
|
///
|
|
|
|
/// - **machine** which contains the main memory
|
|
|
|
/// - **size** the number of bytes to read (1, 2, 4, 8)
|
|
|
|
/// - **address** in the memory to read
|
|
|
|
pub fn read_memory(machine : &mut Machine, size : i32, address : usize) -> u64 {
|
2023-01-18 15:03:58 +01:00
|
|
|
if ![1, 2, 4, 8].contains(&size) {
|
2023-02-04 18:16:52 +01:00
|
|
|
panic!("ERROR read_memory : wrong size parameter {size}, must be (1, 2, 4 or 8)");
|
2022-11-23 18:04:35 +01:00
|
|
|
}
|
|
|
|
|
2023-01-18 15:03:58 +01:00
|
|
|
let mut ret: u64 = 0;
|
|
|
|
for i in 0..size {
|
2023-01-11 15:04:54 +01:00
|
|
|
ret <<= 8;
|
2023-01-18 15:03:58 +01:00
|
|
|
ret += machine.main_memory[address + i as usize] as u64;
|
2022-11-23 18:04:35 +01:00
|
|
|
}
|
2023-01-11 15:04:54 +01:00
|
|
|
ret
|
2022-11-23 18:04:35 +01:00
|
|
|
}
|
2022-11-21 13:21:48 +01:00
|
|
|
|
2023-01-16 19:12:20 +01:00
|
|
|
/// Write to the main memory of the machine
|
|
|
|
///
|
2023-01-18 15:03:58 +01:00
|
|
|
/// `panic!` when size is not 1, 2, 4 or 8
|
|
|
|
///
|
|
|
|
/// ### Parameters
|
|
|
|
///
|
|
|
|
/// - **machine** contains the memory
|
|
|
|
/// - **size** the number of bytes to write (1, 2, 4 or 8)
|
|
|
|
/// - **address** the address to write to
|
|
|
|
/// - **value** data to be written
|
|
|
|
pub fn write_memory(machine: &mut Machine, size: i32, address: usize, value: u64) {
|
|
|
|
if ![1, 2, 4, 8].contains(&size) {
|
2023-02-04 18:16:52 +01:00
|
|
|
panic!("ERROR write_memory: WRONG `size` PARAMETER ({size}), must be 1, 2, 4 or 8")
|
2023-01-16 19:12:20 +01:00
|
|
|
}
|
2023-01-18 15:03:58 +01:00
|
|
|
for i in 0..size as usize {
|
2023-01-18 17:42:56 +01:00
|
|
|
let inv_i = size as usize - i - 1;
|
2023-02-04 18:16:52 +01:00
|
|
|
machine.main_memory[address + i] = ((value & 0xff << (8 * inv_i)) >> (inv_i * 8)) as u8;
|
2023-01-18 15:03:58 +01:00
|
|
|
}
|
2023-01-16 19:12:20 +01:00
|
|
|
}
|
|
|
|
|
2023-02-13 11:08:24 +01:00
|
|
|
/// Write the contains of the main memory of the machine
|
|
|
|
/// in a file called burritos_memory.txt
|
|
|
|
///
|
|
|
|
/// ### Parameters
|
|
|
|
///
|
|
|
|
/// - **machine** contains the memory
|
2023-03-10 10:59:14 +01:00
|
|
|
pub fn _extract_memory(machine: &mut Machine){
|
2023-03-10 10:32:20 +01:00
|
|
|
let file_path = "burritos_memory.txt";
|
|
|
|
let write_to_file = |path| -> std::io::Result<File> {
|
|
|
|
let mut file = File::create(path)?;
|
|
|
|
file.write_all(&machine.main_memory)?;
|
|
|
|
Ok(file)
|
|
|
|
};
|
|
|
|
match write_to_file(file_path) {
|
|
|
|
Err(e) => eprintln!("Failed to write memory to file: {}", e),
|
|
|
|
Ok(_) => println!("Memory extracted to {}", file_path)
|
|
|
|
};
|
2023-02-13 11:08:24 +01:00
|
|
|
}
|
|
|
|
|
2023-03-05 23:49:28 +01:00
|
|
|
pub fn print_machine_status(machine: &mut Machine) {
|
|
|
|
println!("######### Machine status #########");
|
2023-03-06 13:50:45 +01:00
|
|
|
for i in (0..32).step_by(3) {
|
2023-03-08 17:58:38 +01:00
|
|
|
print!(">{0: <4} : {1:<16x} ", print::REG_X[i], machine.int_reg.get_reg(i));
|
|
|
|
print!(">{0: <4} : {1:<16x} ", print::REG_X[i+1], machine.int_reg.get_reg(i+1));
|
2023-03-06 13:50:45 +01:00
|
|
|
if i+2 < 32 {
|
2023-03-08 17:58:38 +01:00
|
|
|
print!(">{0: <4} : {1:<16x} ", print::REG_X[i+2], machine.int_reg.get_reg(i+2));
|
2023-03-06 13:50:45 +01:00
|
|
|
}
|
|
|
|
println!();
|
2023-03-05 23:49:28 +01:00
|
|
|
}
|
2023-03-06 13:50:45 +01:00
|
|
|
println!("________________SP________________");
|
|
|
|
let sp_index = machine.int_reg.get_reg(2);
|
|
|
|
for i in 0..5 {
|
|
|
|
println!("SP+{:<2} : {:16x}", i*8, Self::read_memory(machine, 8, (sp_index + i*8) as usize));
|
|
|
|
}
|
2023-03-05 23:49:28 +01:00
|
|
|
println!("##################################");
|
|
|
|
}
|
|
|
|
|
2023-03-08 17:58:38 +01:00
|
|
|
pub fn string_registers(machine: &mut Machine) -> String {
|
|
|
|
let mut s = String::from("");
|
|
|
|
for i in 0..32 {
|
|
|
|
s.push_str(format!("{} ", machine.int_reg.get_reg(i)).as_str());
|
|
|
|
}
|
|
|
|
s
|
|
|
|
}
|
|
|
|
|
2022-11-23 16:04:21 +01:00
|
|
|
/// Execute the instructions table of a machine putted in param
|
|
|
|
///
|
|
|
|
/// ### Parameters
|
|
|
|
///
|
|
|
|
/// - **machine** which contains a table of instructions
|
2023-03-08 17:58:38 +01:00
|
|
|
pub fn run(machine : &mut Machine){
|
|
|
|
while Machine::one_instruction(machine) == 0 {}
|
|
|
|
println!("trace : \n{}", machine.registers_trace);
|
2022-11-21 13:17:42 +01:00
|
|
|
}
|
|
|
|
|
2022-11-23 16:04:21 +01:00
|
|
|
/// execute the current instruction
|
|
|
|
///
|
|
|
|
/// ### Parameters
|
|
|
|
///
|
|
|
|
/// - **machine** which contains a table of instructions and a pc to the actual instruction
|
2023-03-07 17:32:59 +01:00
|
|
|
pub fn one_instruction(machine :&mut Machine) -> i32 {
|
2022-11-15 21:21:24 +01:00
|
|
|
|
2023-01-11 15:04:54 +01:00
|
|
|
let unsigned_reg1 : u64;
|
|
|
|
let unsigned_reg2 : u64;
|
|
|
|
let long_result : i128;
|
2022-11-16 17:48:55 +01:00
|
|
|
|
|
|
|
/*__int128 longResult;
|
2023-01-11 15:04:54 +01:00
|
|
|
int32_t local_data_a, local_data_b;
|
2022-11-16 17:48:55 +01:00
|
|
|
int64_t localLongResult;
|
2023-01-11 15:04:54 +01:00
|
|
|
uint32_t local_data_aUnsigned, local_data_bUnsigned;
|
2022-11-16 17:48:55 +01:00
|
|
|
int32_t localResult;
|
|
|
|
float localFloat;
|
|
|
|
uint64_t value;*/
|
2022-11-15 21:21:24 +01:00
|
|
|
|
2023-02-08 14:46:56 +01:00
|
|
|
if machine.main_memory.len() <= machine.pc as usize {
|
2023-03-01 16:12:46 +01:00
|
|
|
panic!("ERROR : number max of instructions rushed");
|
2022-11-09 16:47:26 +01:00
|
|
|
}
|
2023-03-05 23:49:28 +01:00
|
|
|
let mut val: [u8; 4] = [0; 4];
|
2023-03-10 10:59:14 +01:00
|
|
|
for (i, mut _item) in val.iter_mut().enumerate() {
|
|
|
|
_item = &mut machine.main_memory[machine.pc as usize + i];
|
2023-02-08 14:46:56 +01:00
|
|
|
}
|
2022-11-09 17:35:16 +01:00
|
|
|
|
2023-03-05 23:49:28 +01:00
|
|
|
let val = u32::from_be_bytes(val) as u64;
|
2023-03-07 17:32:59 +01:00
|
|
|
let inst : Instruction = decode(val);
|
2023-03-05 23:49:28 +01:00
|
|
|
Self::print_machine_status(machine);
|
|
|
|
println!("executing instruction : {:016x} at pc {:x}", val, machine.pc);
|
|
|
|
println!("{}", print::print(decode(val), machine.pc as i32));
|
2023-03-08 17:58:38 +01:00
|
|
|
let trace = Self::string_registers(machine);
|
|
|
|
machine.registers_trace.push_str(format!("{}\n", trace).as_str());
|
2023-03-07 17:32:59 +01:00
|
|
|
|
2023-03-08 17:58:38 +01:00
|
|
|
machine.pc += 4;
|
2022-12-07 17:09:53 +01:00
|
|
|
|
2022-11-15 21:21:24 +01:00
|
|
|
match inst.opcode {
|
2022-11-09 16:47:26 +01:00
|
|
|
RISCV_LUI => {
|
2023-02-01 16:39:40 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, inst.imm31_12 as i64);
|
2022-11-09 16:47:26 +01:00
|
|
|
},
|
2022-11-23 16:29:02 +01:00
|
|
|
RISCV_AUIPC => {
|
2023-02-15 18:09:18 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize,machine.pc as i64 - 4 + inst.imm31_12 as i64);
|
2022-11-23 16:29:02 +01:00
|
|
|
},
|
|
|
|
RISCV_JAL => {
|
2023-02-01 16:39:40 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, machine.pc as i64);
|
2023-03-06 13:50:45 +01:00
|
|
|
machine.pc = (machine.pc as i64 + inst.imm21_1_signed as i64 - 4) as u64;
|
2022-11-23 16:29:02 +01:00
|
|
|
},
|
|
|
|
RISCV_JALR => {
|
|
|
|
let tmp = machine.pc;
|
2023-03-06 13:50:45 +01:00
|
|
|
machine.pc = (machine.int_reg.get_reg(inst.rs1 as usize) + inst.imm12_I_signed as i64) as u64 & 0xfffffffe;
|
2023-02-01 16:39:40 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, tmp as i64);
|
2022-11-23 18:04:35 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
//******************************************************************************************
|
|
|
|
// Treatment for: BRANCH INSTRUCTIONS
|
|
|
|
RISCV_BR => {
|
|
|
|
match inst.funct3 {
|
|
|
|
RISCV_BR_BEQ => {
|
2023-02-01 16:39:40 +01:00
|
|
|
if machine.int_reg.get_reg(inst.rs1 as usize) == machine.int_reg.get_reg(inst.rs2 as usize) {
|
2023-03-06 13:50:45 +01:00
|
|
|
machine.pc = (machine.pc as i64 + inst.imm13_signed as i64 - 4) as u64;
|
2022-11-23 18:04:35 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
RISCV_BR_BNE => {
|
2023-02-01 16:39:40 +01:00
|
|
|
if machine.int_reg.get_reg(inst.rs1 as usize) != machine.int_reg.get_reg(inst.rs2 as usize) {
|
2023-03-06 13:50:45 +01:00
|
|
|
machine.pc = (machine.pc as i64 + inst.imm13_signed as i64 - 4) as u64;
|
2022-11-23 18:04:35 +01:00
|
|
|
}
|
|
|
|
},
|
2023-01-11 15:04:54 +01:00
|
|
|
RISCV_BR_BLT => {
|
2023-02-01 16:39:40 +01:00
|
|
|
if machine.int_reg.get_reg(inst.rs1 as usize) < machine.int_reg.get_reg(inst.rs2 as usize) {
|
2023-03-06 13:50:45 +01:00
|
|
|
machine.pc = (machine.pc as i64 + inst.imm13_signed as i64 - 4) as u64;
|
2022-11-23 18:04:35 +01:00
|
|
|
}
|
|
|
|
},
|
2023-01-11 15:04:54 +01:00
|
|
|
RISCV_BR_BGE => {
|
2023-02-01 16:39:40 +01:00
|
|
|
if machine.int_reg.get_reg(inst.rs1 as usize) >= machine.int_reg.get_reg(inst.rs2 as usize) {
|
2023-03-06 13:50:45 +01:00
|
|
|
machine.pc = (machine.pc as i64 + inst.imm13_signed as i64 - 4) as u64;
|
2022-11-23 18:04:35 +01:00
|
|
|
}
|
|
|
|
},
|
2023-01-11 15:04:54 +01:00
|
|
|
RISCV_BR_BLTU => {
|
2023-02-01 16:39:40 +01:00
|
|
|
if machine.int_reg.get_reg(inst.rs1 as usize) < machine.int_reg.get_reg(inst.rs2 as usize) {
|
2023-03-06 13:50:45 +01:00
|
|
|
machine.pc = (machine.pc as i64 + inst.imm13_signed as i64 - 4) as u64;
|
2022-11-23 18:04:35 +01:00
|
|
|
}
|
|
|
|
},
|
2023-01-11 15:04:54 +01:00
|
|
|
RISCV_BR_BGEU => {
|
2023-02-01 16:39:40 +01:00
|
|
|
if machine.int_reg.get_reg(inst.rs1 as usize) >= machine.int_reg.get_reg(inst.rs2 as usize) {
|
2023-03-06 13:50:45 +01:00
|
|
|
machine.pc = (machine.pc as i64 + inst.imm13_signed as i64 - 4) as u64;
|
2022-11-23 18:04:35 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {
|
2023-01-16 19:12:20 +01:00
|
|
|
panic!("In BR switch case, this should never happen... Instr was {}", inst.value);
|
2022-11-23 18:04:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
//******************************************************************************************
|
|
|
|
// Treatment for: LOAD INSTRUCTIONS
|
|
|
|
RISCV_LD => {
|
|
|
|
match inst.funct3 {
|
2023-02-01 16:39:40 +01:00
|
|
|
RISCV_LD_LB | RISCV_LD_LBU => {
|
2023-02-01 17:10:11 +01:00
|
|
|
let tmp = Self::read_memory(machine, 1, (machine.int_reg.get_reg(inst.rs1 as usize) + inst.imm12_I_signed as i64) as usize) as i64;
|
|
|
|
machine.int_reg.set_reg(inst.rd as usize, tmp);
|
2022-11-23 18:04:35 +01:00
|
|
|
},
|
2023-02-01 16:39:40 +01:00
|
|
|
RISCV_LD_LH | RISCV_LD_LHU => {
|
2023-02-01 17:10:11 +01:00
|
|
|
let tmp = Self::read_memory(machine, 2, (machine.int_reg.get_reg(inst.rs1 as usize) + inst.imm12_I_signed as i64) as usize) as i64;
|
|
|
|
machine.int_reg.set_reg(inst.rd as usize, tmp);
|
2022-11-23 18:04:35 +01:00
|
|
|
},
|
2023-02-01 16:39:40 +01:00
|
|
|
RISCV_LD_LW | RISCV_LD_LWU => {
|
2023-02-01 17:10:11 +01:00
|
|
|
let tmp = Self::read_memory(machine, 4, (machine.int_reg.get_reg(inst.rs1 as usize) + inst.imm12_I_signed as i64) as usize) as i64;
|
|
|
|
machine.int_reg.set_reg(inst.rd as usize, tmp);
|
2022-11-23 18:04:35 +01:00
|
|
|
},
|
|
|
|
RISCV_LD_LD => {
|
2023-03-06 13:50:45 +01:00
|
|
|
let tmp = Self::read_memory(machine, 8, (machine.int_reg.get_reg(inst.rs1 as usize) + inst.imm12_I_signed as i64) as usize) as i64;
|
2023-02-01 17:10:11 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, tmp);
|
2022-11-23 18:04:35 +01:00
|
|
|
},
|
|
|
|
_ => {
|
2023-01-16 19:12:20 +01:00
|
|
|
panic!("In LD switch case, this should never happen... Instr was {}", inst.value);
|
2022-11-23 18:04:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2023-01-18 22:15:06 +01:00
|
|
|
// store instructions
|
2023-01-16 19:12:20 +01:00
|
|
|
RISCV_ST => {
|
|
|
|
match inst.funct3 {
|
|
|
|
RISCV_ST_STB => {
|
2023-02-01 16:39:40 +01:00
|
|
|
Self::write_memory(machine, 1, (machine.int_reg.get_reg(inst.rs1 as usize) + inst.imm12_S_signed as i64) as usize, machine.int_reg.get_reg(inst.rs2 as usize) as u64);
|
2023-01-16 19:12:20 +01:00
|
|
|
},
|
|
|
|
RISCV_ST_STH => {
|
2023-02-01 16:39:40 +01:00
|
|
|
Self::write_memory(machine, 2, (machine.int_reg.get_reg(inst.rs1 as usize) + inst.imm12_S_signed as i64) as usize, machine.int_reg.get_reg(inst.rs2 as usize) as u64);
|
2023-01-16 19:12:20 +01:00
|
|
|
},
|
|
|
|
RISCV_ST_STW => {
|
2023-02-01 16:39:40 +01:00
|
|
|
Self::write_memory(machine, 4, (machine.int_reg.get_reg(inst.rs1 as usize) + inst.imm12_S_signed as i64) as usize, machine.int_reg.get_reg(inst.rs2 as usize) as u64);
|
2023-01-16 19:12:20 +01:00
|
|
|
},
|
|
|
|
RISCV_ST_STD => {
|
2023-02-01 16:39:40 +01:00
|
|
|
Self::write_memory(machine, 8, (machine.int_reg.get_reg(inst.rs1 as usize) + inst.imm12_S_signed as i64) as usize, machine.int_reg.get_reg(inst.rs2 as usize) as u64);
|
2023-01-16 19:12:20 +01:00
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
panic!("In ST switch case, this should never happen... Instr was {}", inst.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-09 17:35:16 +01:00
|
|
|
//******************************************************************************************
|
|
|
|
// Treatment for: OPI INSTRUCTIONS
|
|
|
|
RISCV_OPI => {
|
2022-11-15 21:21:24 +01:00
|
|
|
match inst.funct3 {
|
2022-11-09 17:35:16 +01:00
|
|
|
RISCV_OPI_ADDI => {
|
2023-02-01 16:39:40 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, machine.int_reg.get_reg(inst.rs1 as usize) + inst.imm12_I_signed as i64);
|
2022-11-09 17:35:16 +01:00
|
|
|
},
|
|
|
|
RISCV_OPI_SLTI => {
|
2023-03-10 10:59:14 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, (machine.int_reg.get_reg(inst.rs1 as usize) < inst.imm12_I_signed as i64) as i64);
|
2022-11-09 17:35:16 +01:00
|
|
|
},
|
|
|
|
RISCV_OPI_XORI => {
|
2023-02-01 16:39:40 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, machine.int_reg.get_reg(inst.rs1 as usize) ^ inst.imm12_I_signed as i64);
|
2022-11-09 17:35:16 +01:00
|
|
|
},
|
|
|
|
RISCV_OPI_ORI => {
|
2023-02-01 16:39:40 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, machine.int_reg.get_reg(inst.rs1 as usize) | inst.imm12_I_signed as i64);
|
2022-11-09 17:35:16 +01:00
|
|
|
},
|
|
|
|
RISCV_OPI_ANDI => {
|
2023-02-01 16:39:40 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, machine.int_reg.get_reg(inst.rs1 as usize) & inst.imm12_I_signed as i64);
|
2022-11-09 17:35:16 +01:00
|
|
|
},
|
|
|
|
RISCV_OPI_SLLI => {
|
2023-02-01 16:39:40 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, machine.int_reg.get_reg(inst.rs1 as usize) << inst.shamt);
|
2022-11-16 17:59:09 +01:00
|
|
|
},
|
|
|
|
RISCV_OPI_SRI => {
|
|
|
|
if inst.funct7_smaller == RISCV_OPI_SRI_SRLI {
|
2023-02-01 17:04:10 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, (machine.int_reg.get_reg(inst.rs1 as usize) >> inst.shamt) & machine.shiftmask[inst.shamt as usize] as i64);
|
2022-11-16 17:59:09 +01:00
|
|
|
} else { // SRAI
|
2023-02-01 17:04:10 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, machine.int_reg.get_reg(inst.rs1 as usize) >> inst.shamt);
|
2022-11-16 17:59:09 +01:00
|
|
|
}
|
2022-11-09 17:35:16 +01:00
|
|
|
}
|
2023-01-18 15:26:44 +01:00
|
|
|
_ => { panic!("In OPI switch case, this should never happen... Instr was %x\n {}", inst.value); }
|
2022-11-09 17:35:16 +01:00
|
|
|
}
|
|
|
|
},
|
2022-11-15 21:21:24 +01:00
|
|
|
|
|
|
|
RISCV_OP => {
|
2023-01-16 19:12:20 +01:00
|
|
|
if inst.funct7 == 1 {
|
2022-11-16 17:48:55 +01:00
|
|
|
match inst.funct3 {
|
|
|
|
RISCV_OP_M_MUL => {
|
2023-02-01 17:04:10 +01:00
|
|
|
long_result = (machine.int_reg.get_reg(inst.rs1 as usize) * machine.int_reg.get_reg(inst.rs2 as usize)) as i128;
|
|
|
|
machine.int_reg.set_reg(inst.rd as usize, (long_result & 0xffffffffffffffff) as i64);
|
2022-11-16 17:48:55 +01:00
|
|
|
},
|
|
|
|
RISCV_OP_M_MULH => {
|
2023-02-01 17:04:10 +01:00
|
|
|
long_result = (machine.int_reg.get_reg(inst.rs1 as usize) * machine.int_reg.get_reg(inst.rs2 as usize)) as i128;
|
|
|
|
machine.int_reg.set_reg(inst.rd as usize, ((long_result >> 64) & 0xffffffffffffffff) as i64);
|
2022-11-16 17:48:55 +01:00
|
|
|
},
|
|
|
|
RISCV_OP_M_MULHSU => {
|
2023-02-01 17:04:10 +01:00
|
|
|
unsigned_reg2 = machine.int_reg.get_reg(inst.rs2 as usize) as u64;
|
|
|
|
long_result = (machine.int_reg.get_reg(inst.rs1 as usize) as u64 * unsigned_reg2) as i128;
|
|
|
|
machine.int_reg.set_reg(inst.rd as usize, ((long_result >> 64) & 0xffffffffffffffff) as i64);
|
2022-11-16 17:48:55 +01:00
|
|
|
},
|
|
|
|
// VOIR CE QUE FAIT EXACTEMENT CE TRUC , PK on converve
|
|
|
|
/*
|
|
|
|
* VOIR SI LES CAST machine.int_reg[....] = i128*u64 as u32 FAUSSE RESULTAT (suit pas la logique du code c++)
|
|
|
|
* WHAT DA HECK
|
|
|
|
*/
|
|
|
|
RISCV_OP_M_MULHU => {
|
2023-02-01 17:04:10 +01:00
|
|
|
unsigned_reg1 = machine.int_reg.get_reg(inst.rs1 as usize) as u64;
|
|
|
|
unsigned_reg2 = machine.int_reg.get_reg(inst.rs2 as usize) as u64;
|
2022-11-16 17:48:55 +01:00
|
|
|
long_result = (unsigned_reg1 * unsigned_reg2) as i128;
|
2023-02-01 17:04:10 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, ((long_result >> 64) & 0xffffffffffffffff) as i64);
|
2022-11-16 17:48:55 +01:00
|
|
|
},
|
|
|
|
RISCV_OP_M_DIV => {
|
2023-02-01 17:04:10 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, machine.int_reg.get_reg(inst.rs1 as usize) / machine.int_reg.get_reg(inst.rs2 as usize));
|
2022-11-15 21:21:24 +01:00
|
|
|
}
|
2022-11-16 17:48:55 +01:00
|
|
|
_ => {
|
2023-01-16 19:12:20 +01:00
|
|
|
panic!("RISCV_OP : funct7 = 1 (Multiplication) :: Error\n");
|
2022-11-16 17:48:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match inst.funct3 {
|
|
|
|
RISCV_OP_ADD => {
|
2023-01-11 15:04:54 +01:00
|
|
|
if inst.funct7 == RISCV_OP_ADD_ADD {
|
2023-02-01 17:02:19 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, machine.int_reg.get_reg(inst.rs1 as usize) + machine.int_reg.get_reg(inst.rs2 as usize));
|
2022-12-07 17:09:53 +01:00
|
|
|
} else {
|
2023-02-01 17:02:19 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, machine.int_reg.get_reg(inst.rs1 as usize) - machine.int_reg.get_reg(inst.rs2 as usize));
|
2022-12-07 17:09:53 +01:00
|
|
|
}
|
2022-11-16 17:48:55 +01:00
|
|
|
},
|
|
|
|
RISCV_OP_SLL => {
|
2023-02-01 17:02:19 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, machine.int_reg.get_reg(inst.rs1 as usize) << (machine.int_reg.get_reg(inst.rs2 as usize) & 0x3f));
|
2022-11-16 17:48:55 +01:00
|
|
|
},
|
|
|
|
RISCV_OP_SLT => {
|
2023-02-01 17:02:19 +01:00
|
|
|
if machine.int_reg.get_reg(inst.rs1 as usize) < machine.int_reg.get_reg(inst.rs2 as usize) {
|
|
|
|
machine.int_reg.set_reg(inst.rd as usize, 1);
|
2022-11-16 17:48:55 +01:00
|
|
|
} else {
|
2023-02-01 17:02:19 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, 0);
|
2022-11-16 17:48:55 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
RISCV_OP_SLTU => {
|
2023-02-01 17:02:19 +01:00
|
|
|
unsigned_reg1 = machine.int_reg.get_reg(inst.rs1 as usize) as u64;
|
|
|
|
unsigned_reg2 = machine.int_reg.get_reg(inst.rs2 as usize) as u64;
|
2022-11-16 17:48:55 +01:00
|
|
|
if unsigned_reg1 < unsigned_reg2 {
|
2023-02-01 17:02:19 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, 1);
|
2022-11-16 17:48:55 +01:00
|
|
|
} else {
|
2023-02-01 17:02:19 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, 0);
|
2022-11-16 17:48:55 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
RISCV_OP_XOR => {
|
2023-02-01 17:02:19 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, machine.int_reg.get_reg(inst.rs1 as usize) ^ machine.int_reg.get_reg(inst.rs2 as usize));
|
2022-11-16 17:48:55 +01:00
|
|
|
},
|
|
|
|
RISCV_OP_SR => {
|
|
|
|
// RISCV_OP_SR_SRL inaccessible
|
2023-02-01 17:02:19 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, machine.int_reg.get_reg(inst.rs1 as usize) >> machine.int_reg.get_reg(inst.rs2 as usize));
|
2022-11-16 17:48:55 +01:00
|
|
|
},
|
|
|
|
RISCV_OP_OR => {
|
2023-02-01 17:02:19 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, machine.int_reg.get_reg(inst.rs1 as usize) | machine.int_reg.get_reg(inst.rs2 as usize));
|
2022-11-16 17:48:55 +01:00
|
|
|
},
|
|
|
|
RISCV_OP_AND => {
|
2023-02-01 17:02:19 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, machine.int_reg.get_reg(inst.rs1 as usize) & machine.int_reg.get_reg(inst.rs2 as usize));
|
2022-11-16 17:48:55 +01:00
|
|
|
},
|
|
|
|
_ => {
|
2023-01-16 19:12:20 +01:00
|
|
|
panic!("RISCV_OP undefined case\n");
|
2022-11-15 21:21:24 +01:00
|
|
|
}
|
2022-11-16 17:48:55 +01:00
|
|
|
}//LA
|
2022-11-15 21:21:24 +01:00
|
|
|
}
|
2022-11-16 17:59:09 +01:00
|
|
|
},
|
|
|
|
//******************************************************************************************
|
2023-02-04 18:16:52 +01:00
|
|
|
// Treatment for OPIW INSTRUCTIONS
|
|
|
|
RISCV_OPIW => {
|
|
|
|
let local_data = machine.int_reg.get_reg(inst.rs1 as usize);
|
|
|
|
match inst.funct3 {
|
|
|
|
RISCV_OPIW_ADDIW => {
|
|
|
|
let result = local_data + inst.imm12_I_signed as i64;
|
|
|
|
machine.int_reg.set_reg(inst.rd as usize, result);
|
|
|
|
},
|
|
|
|
RISCV_OPIW_SLLIW => {
|
|
|
|
let result = local_data << inst.shamt;
|
|
|
|
machine.int_reg.set_reg(inst.rd as usize, result);
|
|
|
|
},
|
|
|
|
RISCV_OPIW_SRW => {
|
2023-03-10 10:59:14 +01:00
|
|
|
let result = if inst.funct7 == RISCV_OPIW_SRW_SRLIW {
|
|
|
|
(local_data >> inst.shamt) & machine.shiftmask[32 + inst.shamt as usize] as i64
|
2023-02-04 18:16:52 +01:00
|
|
|
} else { // SRAIW
|
2023-03-10 10:59:14 +01:00
|
|
|
local_data >> inst.shamt
|
|
|
|
};
|
2023-02-04 18:16:52 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, result);
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
panic!("In OPI switch case, this should never happen... Instr was {}\n", inst.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
//******************************************************************************************
|
2022-11-16 17:59:09 +01:00
|
|
|
// Treatment for: OPW INSTRUCTIONS
|
|
|
|
RISCV_OPW => {
|
2023-02-04 18:16:52 +01:00
|
|
|
if inst.funct7 == 1 { // rv64m
|
2023-02-01 17:02:19 +01:00
|
|
|
let local_data_a = machine.int_reg.get_reg(inst.rs1 as usize) & 0xffffffff;
|
|
|
|
let local_data_b = machine.int_reg.get_reg(inst.rs2 as usize) & 0xffffffff;
|
|
|
|
let local_data_a_unsigned = machine.int_reg.get_reg(inst.rs1 as usize) & 0xffffffff;
|
|
|
|
let local_data_b_unsigned = machine.int_reg.get_reg(inst.rs2 as usize) & 0xffffffff;
|
2022-11-16 17:59:09 +01:00
|
|
|
|
|
|
|
// Match case for multiplication operations (in standard extension RV32M)
|
|
|
|
match inst.funct3 {
|
|
|
|
RISCV_OPW_M_MULW => {
|
2023-02-01 17:02:19 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, local_data_a * local_data_b);
|
2022-11-16 17:59:09 +01:00
|
|
|
},
|
|
|
|
RISCV_OPW_M_DIVW => {
|
2023-02-01 17:02:19 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, local_data_a / local_data_b);
|
2022-11-16 17:59:09 +01:00
|
|
|
},
|
|
|
|
RISCV_OPW_M_DIVUW => {
|
2023-02-01 17:02:19 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, local_data_a_unsigned / local_data_b_unsigned);
|
2022-11-16 17:59:09 +01:00
|
|
|
},
|
|
|
|
RISCV_OPW_M_REMW => {
|
2023-02-01 17:02:19 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, local_data_a % local_data_b);
|
2022-11-16 17:59:09 +01:00
|
|
|
},
|
|
|
|
RISCV_OPW_M_REMUW => {
|
2023-02-01 17:02:19 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, local_data_a_unsigned % local_data_b_unsigned);
|
2022-11-16 17:59:09 +01:00
|
|
|
},
|
|
|
|
_ => {
|
2023-01-16 19:12:20 +01:00
|
|
|
panic!("this instruction ({}) doesn't exists", inst.value);
|
2022-11-16 17:59:09 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-04 18:16:52 +01:00
|
|
|
} else { // others rv64 OPW operations
|
2023-02-01 17:02:19 +01:00
|
|
|
let local_dataa = machine.int_reg.get_reg(inst.rs1 as usize) & 0xffffffff;
|
|
|
|
let local_datab = machine.int_reg.get_reg(inst.rs2 as usize) & 0xffffffff;
|
2022-11-16 17:59:09 +01:00
|
|
|
|
|
|
|
// Match case for base OP operation
|
|
|
|
match inst.funct3 {
|
|
|
|
RISCV_OPW_ADDSUBW => {
|
|
|
|
if inst.funct7 == RISCV_OPW_ADDSUBW_ADDW {
|
2023-02-01 17:02:19 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, local_dataa + local_datab);
|
2022-11-16 17:59:09 +01:00
|
|
|
} else { // SUBW
|
2023-02-01 17:02:19 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, local_dataa - local_datab);
|
2022-11-16 17:59:09 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
RISCV_OPW_SLLW => {
|
2023-02-01 17:02:19 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, local_dataa << (local_datab & 0x1f));
|
2022-11-16 17:59:09 +01:00
|
|
|
},
|
|
|
|
RISCV_OPW_SRW => {
|
|
|
|
if inst.funct7 == RISCV_OPW_SRW_SRLW {
|
2023-02-01 17:02:19 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, local_dataa >> (local_datab & 0x1f) & machine.shiftmask[32 + local_datab as usize] as i64);
|
2022-11-16 17:59:09 +01:00
|
|
|
} else { // SRAW
|
2023-02-01 17:02:19 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, local_dataa >> (local_datab & 0x1f));
|
2022-11-16 17:59:09 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {
|
2023-01-16 19:12:20 +01:00
|
|
|
panic!("this instruction ({}) doesn't exists", inst.value);
|
2022-11-16 17:59:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-01 16:41:49 +01:00
|
|
|
},
|
|
|
|
//******************************************************************************************
|
|
|
|
// Treatment for: Simple floating point extension
|
|
|
|
RISCV_FP => {
|
|
|
|
match inst.funct7 {
|
|
|
|
RISCV_FP_ADD => {
|
2023-02-01 17:26:34 +01:00
|
|
|
machine.fp_reg.set_reg(inst.rd as usize, machine.fp_reg.get_reg(inst.rs1 as usize) + machine.fp_reg.get_reg(inst.rs2 as usize));
|
2023-02-01 16:41:49 +01:00
|
|
|
},
|
|
|
|
RISCV_FP_SUB => {
|
2023-02-01 17:26:34 +01:00
|
|
|
machine.fp_reg.set_reg(inst.rd as usize, machine.fp_reg.get_reg(inst.rs1 as usize) - machine.fp_reg.get_reg(inst.rs2 as usize));
|
2023-02-01 16:41:49 +01:00
|
|
|
},
|
|
|
|
RISCV_FP_MUL => {
|
2023-02-01 17:26:34 +01:00
|
|
|
machine.fp_reg.set_reg(inst.rd as usize, machine.fp_reg.get_reg(inst.rs1 as usize) * machine.fp_reg.get_reg(inst.rs2 as usize));
|
2023-02-01 16:41:49 +01:00
|
|
|
},
|
|
|
|
RISCV_FP_DIV => {
|
2023-02-01 17:26:34 +01:00
|
|
|
machine.fp_reg.set_reg(inst.rd as usize, machine.fp_reg.get_reg(inst.rs1 as usize) / machine.fp_reg.get_reg(inst.rs2 as usize));
|
2023-02-01 16:41:49 +01:00
|
|
|
},
|
|
|
|
RISCV_FP_SQRT => {
|
2023-02-01 17:26:34 +01:00
|
|
|
machine.fp_reg.set_reg(inst.rd as usize, machine.fp_reg.get_reg(inst.rs1 as usize).sqrt());
|
2023-02-01 16:41:49 +01:00
|
|
|
},
|
|
|
|
RISCV_FP_FSGN => {
|
2023-02-01 17:32:17 +01:00
|
|
|
let local_float = machine.fp_reg.get_reg(inst.rs1 as usize);
|
2023-02-01 16:41:49 +01:00
|
|
|
match inst.funct3 {
|
|
|
|
RISCV_FP_FSGN_J => {
|
2023-02-01 17:32:17 +01:00
|
|
|
if machine.fp_reg.get_reg(inst.rs2 as usize) < 0f32 {
|
|
|
|
machine.fp_reg.set_reg(inst.rd as usize, -local_float);
|
2023-02-01 16:41:49 +01:00
|
|
|
} else {
|
2023-02-01 17:32:17 +01:00
|
|
|
machine.fp_reg.set_reg(inst.rd as usize, local_float);
|
2023-02-01 16:41:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RISCV_FP_FSGN_JN => {
|
2023-02-01 17:32:17 +01:00
|
|
|
if machine.fp_reg.get_reg(inst.rs2 as usize) < 0f32 {
|
|
|
|
machine.fp_reg.set_reg(inst.rd as usize, local_float);
|
2023-02-01 16:41:49 +01:00
|
|
|
} else {
|
2023-02-01 17:32:17 +01:00
|
|
|
machine.fp_reg.set_reg(inst.rd as usize, -local_float);
|
2023-02-01 16:41:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RISCV_FP_FSGN_JX => {
|
2023-02-01 17:29:31 +01:00
|
|
|
if (machine.fp_reg.get_reg(inst.rs2 as usize) < 0.0 && machine.fp_reg.get_reg(inst.rs1 as usize) >= 0.0) || (machine.fp_reg.get_reg(inst.rs2 as usize) >= 0.0 && machine.fp_reg.get_reg(inst.rs1 as usize) < 0.0) {
|
|
|
|
machine.fp_reg.set_reg(inst.rd as usize, -local_float);
|
2023-02-01 16:41:49 +01:00
|
|
|
} else {
|
2023-02-01 17:29:31 +01:00
|
|
|
machine.fp_reg.set_reg(inst.rd as usize, local_float);
|
2023-02-01 16:41:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
panic!("this instruction ({}) doesn't exists", inst.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
RISCV_FP_MINMAX => {
|
2023-02-01 17:29:31 +01:00
|
|
|
let r1 = machine.fp_reg.get_reg(inst.rs1 as usize);
|
|
|
|
let r2 = machine.fp_reg.get_reg(inst.rs2 as usize);
|
2023-02-01 16:41:49 +01:00
|
|
|
match inst.funct3 {
|
|
|
|
RISCV_FP_MINMAX_MIN => {
|
2023-02-01 17:29:31 +01:00
|
|
|
machine.fp_reg.set_reg(inst.rd as usize, if r1 < r2 {r1} else {r2});
|
2023-02-01 16:41:49 +01:00
|
|
|
},
|
|
|
|
RISCV_FP_MINMAX_MAX => {
|
2023-02-01 17:29:31 +01:00
|
|
|
machine.fp_reg.set_reg(inst.rd as usize, if r1 > r2 {r1} else {r2});
|
2023-02-01 16:41:49 +01:00
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
panic!("this instruction ({}) doesn't exists", inst.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
RISCV_FP_FCVTW => {
|
|
|
|
if inst.rs2 == RISCV_FP_FCVTW_W {
|
2023-02-01 17:29:31 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, machine.fp_reg.get_reg(inst.rs1 as usize) as i64);
|
2023-02-01 16:41:49 +01:00
|
|
|
} else {
|
2023-02-01 17:29:31 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, (machine.fp_reg.get_reg(inst.rs1 as usize) as u64) as i64);
|
2023-02-01 16:41:49 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
RISCV_FP_FCVTS => {
|
|
|
|
if inst.rs2 == RISCV_FP_FCVTS_W {
|
2023-02-01 17:29:31 +01:00
|
|
|
machine.fp_reg.set_reg(inst.rd as usize, machine.int_reg.get_reg(inst.rs1 as usize) as f32);
|
2023-02-01 16:41:49 +01:00
|
|
|
} else {
|
2023-02-01 17:29:31 +01:00
|
|
|
machine.fp_reg.set_reg(inst.rd as usize, (machine.int_reg.get_reg(inst.rs1 as usize) as u32) as f32);
|
2023-02-01 16:41:49 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
RISCV_FP_FMVW => {
|
2023-02-01 17:29:31 +01:00
|
|
|
machine.fp_reg.set_reg(inst.rd as usize, machine.int_reg.get_reg(inst.rs1 as usize) as f32);
|
2023-02-01 16:41:49 +01:00
|
|
|
},
|
|
|
|
RISCV_FP_FMVXFCLASS => {
|
|
|
|
if inst.funct3 == RISCV_FP_FMVXFCLASS_FMVX {
|
2023-02-01 17:29:31 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, machine.fp_reg.get_reg(inst.rs1 as usize) as i64);
|
2023-02-01 16:41:49 +01:00
|
|
|
} else {
|
|
|
|
panic!("Fclass instruction is not handled in riscv simulator");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
RISCV_FP_FCMP => {
|
|
|
|
match inst.funct3 {
|
|
|
|
RISCV_FP_FCMP_FEQ => {
|
2023-03-10 10:59:14 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, (machine.fp_reg.get_reg(inst.rs1 as usize) == machine.fp_reg.get_reg(inst.rs2 as usize)) as i64);
|
2023-02-01 16:41:49 +01:00
|
|
|
},
|
|
|
|
RISCV_FP_FCMP_FLT => {
|
2023-03-10 10:59:14 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, (machine.fp_reg.get_reg(inst.rs1 as usize) < machine.fp_reg.get_reg(inst.rs2 as usize)) as i64);
|
2023-02-01 16:41:49 +01:00
|
|
|
},
|
|
|
|
RISCV_FP_FCMP_FLE => {
|
2023-03-10 10:59:14 +01:00
|
|
|
machine.int_reg.set_reg(inst.rd as usize, (machine.fp_reg.get_reg(inst.rs1 as usize) <= machine.fp_reg.get_reg(inst.rs2 as usize)) as i64);
|
2023-02-01 16:41:49 +01:00
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
panic!("this instruction ({}) doesn't exists", inst.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
panic!("this instruction ({}) doesn't exists", inst.value);
|
|
|
|
}
|
|
|
|
}
|
2022-11-16 15:48:46 +01:00
|
|
|
}
|
2023-03-07 17:32:59 +01:00
|
|
|
RISCV_SYSTEM => {
|
|
|
|
// temporary return value to stop the loop of run
|
|
|
|
// before we can use system call
|
|
|
|
return 1;
|
|
|
|
}
|
2023-03-05 23:49:28 +01:00
|
|
|
_ => { panic!("{:x} opcode non géré pc : {:x}", inst.opcode, machine.pc)},
|
2022-11-09 16:47:26 +01:00
|
|
|
}
|
|
|
|
|
2023-03-10 10:59:14 +01:00
|
|
|
0
|
2022-11-09 16:47:26 +01:00
|
|
|
|
|
|
|
}
|
2023-03-01 15:11:35 +01:00
|
|
|
|
2023-03-01 16:12:46 +01:00
|
|
|
/// print memory FOR DEBUG
|
|
|
|
///
|
|
|
|
/// "@"adresse [16 bytes]
|
2023-03-10 10:59:14 +01:00
|
|
|
pub fn _print_memory(machine : &mut Machine, from: usize, to: usize) {
|
2023-03-01 16:12:46 +01:00
|
|
|
for i in from..to {
|
2023-03-01 15:11:35 +01:00
|
|
|
if i%16 == 0 {
|
|
|
|
print!("\n@{:04x} ", i);
|
|
|
|
}
|
|
|
|
print!("{:02x}", machine.main_memory[i]);
|
|
|
|
}
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
|
2023-03-09 12:08:33 +01:00
|
|
|
pub fn read_int_register(&self, index: usize) -> i64 {
|
|
|
|
self.int_reg.get_reg(index)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_fp_register(&self, index: usize) -> f32 {
|
|
|
|
self.fp_reg.get_reg(index)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_int_register(&mut self, index: usize, value: i64) {
|
|
|
|
self.int_reg.set_reg(index, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_fp_register(&mut self, index: usize, value: f32) {
|
|
|
|
self.fp_reg.set_reg(index, value);
|
|
|
|
}
|
2022-11-09 15:59:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2023-03-08 17:58:38 +01:00
|
|
|
mod test {
|
|
|
|
use std::fs;
|
|
|
|
|
|
|
|
use crate::simulator::{machine::Machine, mem_cmp};
|
2023-01-11 15:36:27 +01:00
|
|
|
|
2023-03-11 23:49:20 +01:00
|
|
|
#[test]
|
|
|
|
fn test_init_machine() {
|
|
|
|
let _ = Machine::init_machine();
|
|
|
|
}
|
|
|
|
|
2023-01-18 17:01:48 +01:00
|
|
|
#[test]
|
|
|
|
fn test_read_memory() {
|
2023-03-11 23:49:20 +01:00
|
|
|
let mut m = Machine::init_machine();
|
2023-01-18 17:01:48 +01:00
|
|
|
m.main_memory[4] = 43;
|
|
|
|
m.main_memory[5] = 150;
|
|
|
|
assert_eq!((43 << 8) + 150, Machine::read_memory(&mut m, 2, 4));
|
|
|
|
}
|
|
|
|
|
2023-01-18 17:42:56 +01:00
|
|
|
#[test]
|
2023-01-18 17:01:48 +01:00
|
|
|
fn test_write_memory() {
|
2023-03-11 23:49:20 +01:00
|
|
|
let mut m = Machine::init_machine();
|
2023-01-18 17:01:48 +01:00
|
|
|
Machine::write_memory(&mut m, 2, 6, (43 << 8) + 150);
|
|
|
|
assert_eq!(43, m.main_memory[6]);
|
|
|
|
assert_eq!(150, m.main_memory[7]);
|
2023-02-15 18:09:18 +01:00
|
|
|
Machine::write_memory(&mut m, 4, 8, (52 << 24) + (20 << 16) + (43 << 8) + 150);
|
|
|
|
assert_eq!(52, m.main_memory[8]);
|
|
|
|
assert_eq!(20, m.main_memory[9]);
|
|
|
|
assert_eq!(43, m.main_memory[10]);
|
|
|
|
assert_eq!(150, m.main_memory[11]);
|
2023-01-18 17:01:48 +01:00
|
|
|
}
|
2023-03-08 17:58:38 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_comp() {
|
2023-03-11 23:49:20 +01:00
|
|
|
let mut m = Machine::init_machine();
|
2023-03-08 17:58:38 +01:00
|
|
|
let path_before = "memoryComp.txt".to_string();
|
|
|
|
let path_after = "memoryCompEnd.txt".to_string();
|
2023-03-11 23:49:20 +01:00
|
|
|
let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap();
|
|
|
|
let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap();
|
2023-03-08 17:58:38 +01:00
|
|
|
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
|
|
|
Machine::run(&mut m);
|
|
|
|
|
|
|
|
let path_trace = "memoryCompTrace.txt".to_string();
|
|
|
|
let expected_trace = fs::read_to_string(path_trace).unwrap();
|
|
|
|
|
|
|
|
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
|
|
|
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_div() {
|
2023-03-11 23:49:20 +01:00
|
|
|
let mut m = Machine::init_machine();
|
|
|
|
let path_before = "memoryDiv.txt";
|
|
|
|
let path_after = "memoryDivEnd.txt";
|
|
|
|
let memory_before = mem_cmp::MemChecker::from(path_before).unwrap();
|
|
|
|
let memory_after = mem_cmp::MemChecker::from(path_after).unwrap();
|
2023-03-08 17:58:38 +01:00
|
|
|
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
|
|
|
Machine::run(&mut m);
|
|
|
|
|
|
|
|
let path_trace = "memoryDivTrace.txt".to_string();
|
|
|
|
let expected_trace = fs::read_to_string(path_trace).unwrap();
|
|
|
|
|
|
|
|
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
|
|
|
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_if() {
|
2023-03-11 23:49:20 +01:00
|
|
|
let mut m = Machine::init_machine();
|
2023-03-08 17:58:38 +01:00
|
|
|
let path_before = "memoryIf.txt".to_string();
|
|
|
|
let path_after = "memoryIfEnd.txt".to_string();
|
2023-03-11 23:49:20 +01:00
|
|
|
let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap();
|
|
|
|
let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap();
|
2023-03-08 17:58:38 +01:00
|
|
|
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
|
|
|
Machine::run(&mut m);
|
|
|
|
|
|
|
|
let path_trace = "memoryIfTrace.txt".to_string();
|
|
|
|
let expected_trace = fs::read_to_string(path_trace).unwrap();
|
|
|
|
|
|
|
|
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
|
|
|
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_jump() {
|
2023-03-11 23:49:20 +01:00
|
|
|
let mut m = Machine::init_machine();
|
2023-03-08 17:58:38 +01:00
|
|
|
let path_before = "memoryJump.txt".to_string();
|
|
|
|
let path_after = "memoryJumpEnd.txt".to_string();
|
2023-03-11 23:49:20 +01:00
|
|
|
let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap();
|
|
|
|
let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap();
|
2023-03-08 17:58:38 +01:00
|
|
|
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
|
|
|
Machine::run(&mut m);
|
|
|
|
|
|
|
|
let path_trace = "memoryJumpTrace.txt".to_string();
|
|
|
|
let expected_trace = fs::read_to_string(path_trace).unwrap();
|
|
|
|
|
|
|
|
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
|
|
|
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mul() {
|
2023-03-11 23:49:20 +01:00
|
|
|
let mut m = Machine::init_machine();
|
2023-03-08 17:58:38 +01:00
|
|
|
let path_before = "memoryMul.txt".to_string();
|
|
|
|
let path_after = "memoryMulEnd.txt".to_string();
|
2023-03-11 23:49:20 +01:00
|
|
|
let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap();
|
|
|
|
let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap();
|
2023-03-08 17:58:38 +01:00
|
|
|
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
|
|
|
Machine::run(&mut m);
|
|
|
|
|
|
|
|
let path_trace = "memoryMulTrace.txt".to_string();
|
|
|
|
let expected_trace = fs::read_to_string(path_trace).unwrap();
|
|
|
|
|
|
|
|
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
|
|
|
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ret() {
|
2023-03-11 23:49:20 +01:00
|
|
|
let mut m = Machine::init_machine();
|
2023-03-08 17:58:38 +01:00
|
|
|
let path_before = "memoryRet.txt".to_string();
|
|
|
|
let path_after = "memoryRetEnd.txt".to_string();
|
2023-03-11 23:49:20 +01:00
|
|
|
let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap();
|
|
|
|
let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap();
|
2023-03-08 17:58:38 +01:00
|
|
|
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
|
|
|
Machine::run(&mut m);
|
|
|
|
|
|
|
|
let path_trace = "memoryRetTrace.txt".to_string();
|
|
|
|
let expected_trace = fs::read_to_string(path_trace).unwrap();
|
|
|
|
|
|
|
|
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
|
|
|
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_sub() {
|
2023-03-11 23:49:20 +01:00
|
|
|
let mut m = Machine::init_machine();
|
2023-03-08 17:58:38 +01:00
|
|
|
let path_before = "memorySub.txt".to_string();
|
|
|
|
let path_after = "memorySubEnd.txt".to_string();
|
2023-03-11 23:49:20 +01:00
|
|
|
let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap();
|
|
|
|
let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap();
|
2023-03-08 17:58:38 +01:00
|
|
|
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
|
|
|
Machine::run(&mut m);
|
|
|
|
|
|
|
|
let path_trace = "memorySubTrace.txt".to_string();
|
|
|
|
let expected_trace = fs::read_to_string(path_trace).unwrap();
|
|
|
|
|
|
|
|
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
|
|
|
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_switch() {
|
2023-03-11 23:49:20 +01:00
|
|
|
let mut m = Machine::init_machine();
|
2023-03-08 17:58:38 +01:00
|
|
|
let path_before = "memorySwitch.txt".to_string();
|
|
|
|
let path_after = "memorySwitchEnd.txt".to_string();
|
2023-03-11 23:49:20 +01:00
|
|
|
let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap();
|
|
|
|
let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap();
|
2023-03-08 17:58:38 +01:00
|
|
|
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
|
|
|
Machine::run(&mut m);
|
|
|
|
|
|
|
|
let path_trace = "memorySwitchTrace.txt".to_string();
|
|
|
|
let expected_trace = fs::read_to_string(path_trace).unwrap();
|
|
|
|
|
|
|
|
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
|
|
|
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
|
|
|
}
|
2023-01-11 15:36:27 +01:00
|
|
|
}
|