2023-03-24 19:02:50 +01:00
|
|
|
//! # Machine
|
|
|
|
//!
|
|
|
|
//! This module contains a RISC-V simulator.
|
|
|
|
//! It supports the base instruction set along
|
|
|
|
//! with 32bit floating point operations.
|
|
|
|
//!
|
|
|
|
//! Basic usage:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! let mut machine = Machine::init_machine();
|
|
|
|
//! machine.run();
|
|
|
|
//! ```
|
|
|
|
|
2023-03-24 17:44:24 +01:00
|
|
|
use std::{
|
|
|
|
io::Write,
|
|
|
|
fs::File
|
|
|
|
};
|
|
|
|
use crate::simulator::{
|
|
|
|
print,
|
|
|
|
error::MachineError,
|
|
|
|
decode::*,
|
|
|
|
interrupt::Interrupt,
|
|
|
|
global::*,
|
|
|
|
register::*
|
|
|
|
};
|
2023-02-08 14:34:09 +01:00
|
|
|
|
2023-03-23 20:58:10 +01:00
|
|
|
/// Exceptions
|
|
|
|
/// todo: is this really supposed to stand in machine.rs?
|
2023-03-13 18:01:02 +01:00
|
|
|
pub enum ExceptionType {
|
2023-03-23 20:58:10 +01:00
|
|
|
/// Everything ok
|
2023-03-23 20:54:05 +01:00
|
|
|
NoException,
|
2023-03-23 20:58:10 +01:00
|
|
|
/// A program executed a system call
|
|
|
|
SyscallException,
|
|
|
|
/// Page fault exception
|
|
|
|
PagefaultException,
|
|
|
|
/// Write attempted to a page marked "read-only"
|
|
|
|
ReadOnlyException,
|
|
|
|
/// Translation resulted in an invalid physical address (mis-aligned or out-of-bounds)
|
|
|
|
BusErrorException,
|
|
|
|
/// Reference which was not mapped in the address space
|
|
|
|
AddressErrorException,
|
|
|
|
/// Integer overflow in add or sub
|
|
|
|
OverflowException,
|
|
|
|
/// Unimplemented or reserved instruction
|
|
|
|
IllegalInstrException,
|
|
|
|
NumExceptionTypes
|
2023-03-13 17:49:48 +01:00
|
|
|
}
|
|
|
|
|
2023-03-01 15:45:49 +01:00
|
|
|
pub const STACK_REG: usize = 2;
|
2023-03-24 18:11:37 +01:00
|
|
|
/// Number of available Integer registers
|
2023-02-28 14:43:40 +01:00
|
|
|
pub const NUM_INT_REGS: usize = 32;
|
2023-03-24 18:11:37 +01:00
|
|
|
/// Number of available Floating Point registers
|
2023-02-28 14:43:40 +01:00
|
|
|
pub const NUM_FP_REGS: usize = 32;
|
2023-03-23 20:54:05 +01:00
|
|
|
/// max number of physical pages
|
2023-03-14 22:55:48 +01:00
|
|
|
pub const NUM_PHY_PAGE : u64 = 400;
|
2023-03-23 20:54:05 +01:00
|
|
|
/// Must be 2^x
|
2023-03-13 22:52:27 +01:00
|
|
|
pub const PAGE_SIZE : u64 = 128;
|
2023-03-23 20:54:05 +01:00
|
|
|
/// Must be a multiple of PAGE_SIZE
|
2023-03-15 15:12:47 +01:00
|
|
|
pub const MEM_SIZE : usize = (PAGE_SIZE*NUM_PHY_PAGE*100) as usize;
|
2023-03-14 22:55:48 +01:00
|
|
|
|
2023-03-24 18:11:37 +01:00
|
|
|
/// RISC-V Simulator
|
2022-11-09 15:59:05 +01:00
|
|
|
pub struct Machine {
|
2023-03-24 18:11:37 +01:00
|
|
|
/// Program counter
|
2022-11-23 16:29:02 +01:00
|
|
|
pub pc : u64,
|
2023-03-24 18:11:37 +01:00
|
|
|
/// Stack pointer
|
2023-02-08 14:46:56 +01:00
|
|
|
pub sp: usize,
|
2023-03-24 18:11:37 +01:00
|
|
|
/// Integer register
|
2023-02-01 16:39:40 +01:00
|
|
|
pub int_reg : Register<i64>,
|
2023-03-24 18:11:37 +01:00
|
|
|
/// Floating point register
|
2023-02-01 17:26:34 +01:00
|
|
|
pub fp_reg : Register<f32>,
|
2023-03-24 18:11:37 +01:00
|
|
|
/// Heap memory
|
2023-03-11 23:49:20 +01:00
|
|
|
pub main_memory : Vec<u8>,
|
2023-03-24 18:11:37 +01:00
|
|
|
/// Shiftmask
|
2023-03-08 13:21:08 +01:00
|
|
|
pub shiftmask : [u64 ; 64],
|
2023-03-24 18:11:37 +01:00
|
|
|
/// Debug data
|
2023-03-09 12:08:33 +01:00
|
|
|
pub registers_trace : String, // for tests
|
2023-03-24 18:11:37 +01:00
|
|
|
/// todo: document Interrupts
|
2023-03-09 12:08:33 +01:00
|
|
|
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-23 21:55:46 +01:00
|
|
|
/// Machine constructor
|
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-25 15:37:14 +01:00
|
|
|
Machine {
|
2022-11-09 16:47:26 +01:00
|
|
|
pc : 0,
|
2023-02-08 14:46:56 +01:00
|
|
|
sp: 0,
|
2023-03-25 15:37:14 +01:00
|
|
|
int_reg : { let mut r = Register::<i64>::init(); r.set_reg(10, -1); r },
|
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("")
|
2023-03-25 15:37:14 +01:00
|
|
|
}
|
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
|
2023-03-24 18:34:06 +01:00
|
|
|
pub fn read_memory(&self, 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-03-24 18:34:06 +01:00
|
|
|
ret += self.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
|
2023-03-24 18:36:02 +01:00
|
|
|
pub fn write_memory(&mut self, size: i32, address: usize, value: 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 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-03-24 18:36:02 +01:00
|
|
|
self.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-24 18:48:07 +01:00
|
|
|
pub fn extract_memory(&self){
|
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)?;
|
2023-03-24 18:48:07 +01:00
|
|
|
file.write_all(&self.main_memory)?;
|
2023-03-10 10:32:20 +01:00
|
|
|
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-23 21:55:46 +01:00
|
|
|
/// Print the status of the machine to the standard output
|
|
|
|
///
|
|
|
|
/// ### Parameters
|
|
|
|
///
|
|
|
|
/// - **machine** the machine to get the status from
|
2023-03-24 18:48:07 +01:00
|
|
|
pub fn print_status(&self) {
|
2023-03-05 23:49:28 +01:00
|
|
|
println!("######### Machine status #########");
|
2023-03-24 18:48:07 +01:00
|
|
|
for i in (0..32).step_by(3) {
|
|
|
|
print!(">{0: <4} : {1:<16x} ", print::REG_X[i], self.int_reg.get_reg(i as u8));
|
|
|
|
print!(">{0: <4} : {1:<16x} ", print::REG_X[i+1], self.int_reg.get_reg((i+1) as u8));
|
2023-03-06 13:50:45 +01:00
|
|
|
if i+2 < 32 {
|
2023-03-24 18:48:07 +01:00
|
|
|
print!(">{0: <4} : {1:<16x} ", print::REG_X[i+2], self.int_reg.get_reg((i+2) as u8));
|
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________________");
|
2023-03-24 18:48:07 +01:00
|
|
|
let sp_index = self.int_reg.get_reg(2);
|
2023-03-06 13:50:45 +01:00
|
|
|
for i in 0..5 {
|
2023-03-24 18:48:07 +01:00
|
|
|
println!("SP+{:<2} : {:16x}", i*8, self.read_memory(8, (sp_index + i*8) as usize));
|
2023-03-06 13:50:45 +01:00
|
|
|
}
|
2023-03-05 23:49:28 +01:00
|
|
|
println!("##################################");
|
|
|
|
}
|
|
|
|
|
2023-03-23 21:55:46 +01:00
|
|
|
/// Get the state of the registers as a string
|
|
|
|
///
|
|
|
|
/// ### Parameters
|
|
|
|
///
|
|
|
|
/// - **machine** the machine to read the registers from
|
2023-03-24 18:48:07 +01:00
|
|
|
pub fn string_registers(&self) -> String {
|
2023-03-08 17:58:38 +01:00
|
|
|
let mut s = String::from("");
|
|
|
|
for i in 0..32 {
|
2023-03-24 18:48:07 +01:00
|
|
|
s.push_str(format!("{} ", self.int_reg.get_reg(i)).as_str());
|
2023-03-08 17:58:38 +01:00
|
|
|
}
|
|
|
|
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-27 10:21:18 +02:00
|
|
|
pub fn run(&mut self) {
|
|
|
|
loop {
|
|
|
|
match Machine::one_instruction(self) {
|
|
|
|
Ok(()) => (),
|
|
|
|
Err(e) => panic!("FATAL at pc {} -> {}", self.pc, e)
|
|
|
|
}
|
|
|
|
}
|
2022-11-21 13:17:42 +01:00
|
|
|
}
|
|
|
|
|
2023-03-23 20:05:46 +01:00
|
|
|
/// Execute the current instruction
|
2022-11-23 16:04:21 +01:00
|
|
|
///
|
|
|
|
/// ### Parameters
|
|
|
|
///
|
|
|
|
/// - **machine** which contains a table of instructions and a pc to the actual instruction
|
2023-03-24 18:48:07 +01:00
|
|
|
pub fn one_instruction(&mut self) -> Result<(), MachineError> {
|
2022-11-15 21:21:24 +01:00
|
|
|
|
2023-03-24 18:48:07 +01:00
|
|
|
if self.main_memory.len() <= self.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-15 15:12:47 +01:00
|
|
|
for i in 0..4 {
|
2023-03-24 18:48:07 +01:00
|
|
|
val[i] = self.main_memory[self.pc as usize + i];
|
2023-02-08 14:46:56 +01:00
|
|
|
}
|
2023-03-15 15:12:47 +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-24 18:48:07 +01:00
|
|
|
self.print_status();
|
|
|
|
println!("executing instruction : {:016x} at pc {:x}", val, self.pc);
|
|
|
|
println!("{}", print::print(decode(val), self.pc as i32));
|
|
|
|
let trace = Self::string_registers(self);
|
|
|
|
self.registers_trace.push_str(format!("{}\n", trace).as_str());
|
2023-03-07 17:32:59 +01:00
|
|
|
|
2023-03-24 18:48:07 +01:00
|
|
|
self.pc += 4;
|
2022-12-07 17:09:53 +01:00
|
|
|
|
2022-11-15 21:21:24 +01:00
|
|
|
match inst.opcode {
|
2023-03-25 15:37:14 +01:00
|
|
|
// Treatment for: LOAD UPPER IMMEDIATE INSTRUCTION
|
2022-11-09 16:47:26 +01:00
|
|
|
RISCV_LUI => {
|
2023-03-24 18:48:07 +01:00
|
|
|
self.int_reg.set_reg(inst.rd, inst.imm31_12 as i64);
|
2023-03-23 20:05:46 +01:00
|
|
|
Ok(())
|
2022-11-09 16:47:26 +01:00
|
|
|
},
|
2023-03-25 15:37:14 +01:00
|
|
|
|
|
|
|
// Treatment for: ADD UPPER IMMEDIATE TO PC INSTRUCTION
|
2022-11-23 16:29:02 +01:00
|
|
|
RISCV_AUIPC => {
|
2023-03-24 18:48:07 +01:00
|
|
|
self.int_reg.set_reg(inst.rd, self.pc as i64 - 4 + inst.imm31_12 as i64);
|
2023-03-23 20:05:46 +01:00
|
|
|
Ok(())
|
2022-11-23 16:29:02 +01:00
|
|
|
},
|
2023-03-25 15:37:14 +01:00
|
|
|
|
|
|
|
// Treatement for: JUMP AND LINK INSTRUCTIONS (direct jump)
|
2022-11-23 16:29:02 +01:00
|
|
|
RISCV_JAL => {
|
2023-03-24 18:48:07 +01:00
|
|
|
self.int_reg.set_reg(inst.rd, self.pc as i64);
|
|
|
|
self.pc = (self.pc as i64 + inst.imm21_1_signed as i64 - 4) as u64;
|
2023-03-23 20:05:46 +01:00
|
|
|
Ok(())
|
2022-11-23 16:29:02 +01:00
|
|
|
},
|
2023-03-25 15:37:14 +01:00
|
|
|
|
|
|
|
// Treatment for: JUMP AND LINK REGISTER INSTRUCTIONS (indirect jump)
|
2022-11-23 16:29:02 +01:00
|
|
|
RISCV_JALR => {
|
2023-03-24 18:48:07 +01:00
|
|
|
let tmp = self.pc;
|
|
|
|
self.pc = (self.int_reg.get_reg(inst.rs1) + inst.imm12_I_signed as i64) as u64 & 0xfffffffe;
|
|
|
|
self.int_reg.set_reg(inst.rd, tmp as i64);
|
2023-03-23 20:05:46 +01:00
|
|
|
Ok(())
|
2022-11-23 18:04:35 +01:00
|
|
|
},
|
2023-03-25 15:37:14 +01:00
|
|
|
|
2022-11-23 18:04:35 +01:00
|
|
|
// Treatment for: BRANCH INSTRUCTIONS
|
2023-03-25 15:37:14 +01:00
|
|
|
RISCV_BR => self.branch_instruction(inst),
|
|
|
|
|
2022-11-23 18:04:35 +01:00
|
|
|
// Treatment for: LOAD INSTRUCTIONS
|
2023-03-25 15:37:14 +01:00
|
|
|
RISCV_LD => self.load_instruction(inst),
|
|
|
|
|
|
|
|
// Treatment for: STORE INSTRUCTIONS
|
|
|
|
RISCV_ST => self.store_instruction(inst),
|
|
|
|
|
2022-11-09 17:35:16 +01:00
|
|
|
// Treatment for: OPI INSTRUCTIONS
|
2023-03-25 15:37:14 +01:00
|
|
|
RISCV_OPI => self.opi_instruction(inst),
|
|
|
|
|
|
|
|
// Treatment for: OP INSTRUCTIONS
|
|
|
|
RISCV_OP => self.op_instruction(inst),
|
|
|
|
|
2023-02-04 18:16:52 +01:00
|
|
|
// Treatment for OPIW INSTRUCTIONS
|
2023-03-25 15:37:14 +01:00
|
|
|
RISCV_OPIW => self.opiw_instruction(inst),
|
|
|
|
|
2022-11-16 17:59:09 +01:00
|
|
|
// Treatment for: OPW INSTRUCTIONS
|
2023-03-25 15:37:14 +01:00
|
|
|
RISCV_OPW => self.opw_instruction(inst),
|
|
|
|
|
|
|
|
// Treatment for: FLOATING POINT INSTRUCTIONS
|
|
|
|
RISCV_FP => self.fp_instruction(inst),
|
|
|
|
|
|
|
|
// Treatment for: SYSTEM CALLS
|
|
|
|
RISCV_SYSTEM => Err(MachineError::new(format!("{:x}: System opcode\npc: {:x}", inst.opcode, self.pc).as_str())),
|
|
|
|
|
|
|
|
// Default case
|
2023-03-24 18:48:07 +01:00
|
|
|
_ => Err(MachineError::new(format!("{:x}: Unknown opcode\npc: {:x}", inst.opcode, self.pc).as_str()))
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Treatement for Branch instructions
|
2023-03-24 18:48:07 +01:00
|
|
|
fn branch_instruction(&mut self, inst: Instruction) -> Result<(), MachineError> {
|
2023-03-23 20:05:46 +01:00
|
|
|
match inst.funct3 {
|
|
|
|
RISCV_BR_BEQ => {
|
2023-03-24 18:48:07 +01:00
|
|
|
if self.int_reg.get_reg(inst.rs1) == self.int_reg.get_reg(inst.rs2) {
|
|
|
|
self.pc = (self.pc as i64 + inst.imm13_signed as i64 - 4) as u64;
|
2022-11-16 17:59:09 +01:00
|
|
|
}
|
2023-02-01 16:41:49 +01:00
|
|
|
},
|
2023-03-23 20:05:46 +01:00
|
|
|
RISCV_BR_BNE => {
|
2023-03-24 18:48:07 +01:00
|
|
|
if self.int_reg.get_reg(inst.rs1) != self.int_reg.get_reg(inst.rs2) {
|
|
|
|
self.pc = (self.pc as i64 + inst.imm13_signed as i64 - 4) as u64;
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
RISCV_BR_BLT => {
|
2023-03-24 18:48:07 +01:00
|
|
|
if self.int_reg.get_reg(inst.rs1) < self.int_reg.get_reg(inst.rs2) {
|
|
|
|
self.pc = (self.pc as i64 + inst.imm13_signed as i64 - 4) as u64;
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
RISCV_BR_BGE => {
|
2023-03-24 18:48:07 +01:00
|
|
|
if self.int_reg.get_reg(inst.rs1) >= self.int_reg.get_reg(inst.rs2) {
|
|
|
|
self.pc = (self.pc as i64 + inst.imm13_signed as i64 - 4) as u64;
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
RISCV_BR_BLTU => {
|
2023-03-24 18:48:07 +01:00
|
|
|
if self.int_reg.get_reg(inst.rs1) < self.int_reg.get_reg(inst.rs2) {
|
|
|
|
self.pc = (self.pc as i64 + inst.imm13_signed as i64 - 4) as u64;
|
2023-02-01 16:41:49 +01:00
|
|
|
}
|
2023-03-23 20:05:46 +01:00
|
|
|
},
|
|
|
|
RISCV_BR_BGEU => {
|
2023-03-24 18:48:07 +01:00
|
|
|
if self.int_reg.get_reg(inst.rs1) >= self.int_reg.get_reg(inst.rs2) {
|
|
|
|
self.pc = (self.pc as i64 + inst.imm13_signed as i64 - 4) as u64;
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
panic!("In BR switch case, this should never happen... Instr was {}", inst.value);
|
2022-11-16 15:48:46 +01:00
|
|
|
}
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes RISC-V Load Instructions on the machine
|
2023-03-24 18:48:07 +01:00
|
|
|
fn load_instruction(&mut self, inst: Instruction) -> Result<(), MachineError> {
|
2023-03-25 15:57:28 +01:00
|
|
|
let mut set_reg = |rd, size| {
|
|
|
|
let val = self.read_memory(size, (self.int_reg.get_reg(inst.rs1) + inst.imm12_I_signed as i64) as usize) as i64;
|
|
|
|
self.int_reg.set_reg(rd, val);
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
|
2023-03-23 20:05:46 +01:00
|
|
|
match inst.funct3 {
|
2023-03-25 15:57:28 +01:00
|
|
|
RISCV_LD_LB | RISCV_LD_LBU => set_reg(inst.rd, 1),
|
|
|
|
RISCV_LD_LH | RISCV_LD_LHU => set_reg(inst.rd, 2),
|
|
|
|
RISCV_LD_LW | RISCV_LD_LWU => set_reg(inst.rd, 4),
|
|
|
|
RISCV_LD_LD => set_reg(inst.rd, 8),
|
|
|
|
_ => Err(MachineError::new(format!("In LD switch case, this should never happen... Instr was {}", inst.value).as_str()))
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes RISC-V Store Instructions on the machine
|
2023-03-24 18:48:07 +01:00
|
|
|
fn store_instruction(&mut self, inst: Instruction) -> Result<(), MachineError> {
|
2023-03-25 15:57:28 +01:00
|
|
|
let mut store = |size| {
|
2023-03-25 15:43:33 +01:00
|
|
|
self.write_memory(
|
|
|
|
size,
|
|
|
|
(self.int_reg.get_reg(inst.rs1) + inst.imm12_S_signed as i64) as usize,
|
|
|
|
self.int_reg.get_reg(inst.rs2) as u64
|
|
|
|
);
|
2023-03-25 15:57:28 +01:00
|
|
|
Ok(())
|
|
|
|
};
|
2023-03-25 15:43:33 +01:00
|
|
|
|
2023-03-23 20:05:46 +01:00
|
|
|
match inst.funct3 {
|
2023-03-25 15:43:33 +01:00
|
|
|
RISCV_ST_STB => store(1),
|
|
|
|
RISCV_ST_STH => store(2),
|
|
|
|
RISCV_ST_STW => store(4),
|
|
|
|
RISCV_ST_STD => store(8),
|
2023-03-25 15:57:28 +01:00
|
|
|
_ => Err(MachineError::new(format!("In ST switch case, this should never happen... Instr was {}", inst.value).as_str()))
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes RISC-V Integer Register-Immediate Instructions on the machine
|
2023-03-24 18:48:07 +01:00
|
|
|
fn opi_instruction(&mut self, inst: Instruction) -> Result<(), MachineError> {
|
2023-03-23 20:05:46 +01:00
|
|
|
match inst.funct3 {
|
2023-03-24 18:48:07 +01:00
|
|
|
RISCV_OPI_ADDI => self.int_reg.set_reg(inst.rd, self.int_reg.get_reg(inst.rs1) + inst.imm12_I_signed as i64),
|
|
|
|
RISCV_OPI_SLTI => self.int_reg.set_reg(inst.rd, (self.int_reg.get_reg(inst.rs1) < inst.imm12_I_signed as i64) as i64),
|
|
|
|
RISCV_OPI_XORI => self.int_reg.set_reg(inst.rd, self.int_reg.get_reg(inst.rs1) ^ inst.imm12_I_signed as i64),
|
|
|
|
RISCV_OPI_ORI => self.int_reg.set_reg(inst.rd, self.int_reg.get_reg(inst.rs1) | inst.imm12_I_signed as i64),
|
|
|
|
RISCV_OPI_ANDI => self.int_reg.set_reg(inst.rd, self.int_reg.get_reg(inst.rs1) & inst.imm12_I_signed as i64),
|
|
|
|
RISCV_OPI_SLLI => self.int_reg.set_reg(inst.rd, self.int_reg.get_reg(inst.rs1) << inst.shamt),
|
2023-03-23 20:05:46 +01:00
|
|
|
RISCV_OPI_SRI => if inst.funct7_smaller == RISCV_OPI_SRI_SRLI {
|
2023-03-24 18:48:07 +01:00
|
|
|
self.int_reg.set_reg(inst.rd, (self.int_reg.get_reg(inst.rs1) >> inst.shamt) & self.shiftmask[inst.shamt as usize] as i64);
|
2023-03-23 20:05:46 +01:00
|
|
|
} else { // SRAI
|
2023-03-24 18:48:07 +01:00
|
|
|
self.int_reg.set_reg(inst.rd, self.int_reg.get_reg(inst.rs1) >> inst.shamt);
|
2023-03-23 20:05:46 +01:00
|
|
|
},
|
|
|
|
_ => panic!("In OPI switch case, this should never happen... Instr was %x\n {}", inst.value)
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes simple RISC-V mathematical operations on the machine
|
2023-03-24 18:48:07 +01:00
|
|
|
fn op_instruction(&mut self, inst: Instruction) -> Result<(), MachineError> {
|
2023-03-23 20:05:46 +01:00
|
|
|
let long_result: i128;
|
|
|
|
let unsigned_reg1: u64;
|
|
|
|
let unsigned_reg2: u64;
|
|
|
|
if inst.funct7 == 1 {
|
|
|
|
match inst.funct3 {
|
|
|
|
RISCV_OP_M_MUL => {
|
2023-03-24 18:48:07 +01:00
|
|
|
long_result = (self.int_reg.get_reg(inst.rs1) * self.int_reg.get_reg(inst.rs2)) as i128;
|
|
|
|
self.int_reg.set_reg(inst.rd, (long_result & 0xffffffffffffffff) as i64)
|
2023-03-23 20:05:46 +01:00
|
|
|
},
|
|
|
|
RISCV_OP_M_MULH => {
|
2023-03-24 18:48:07 +01:00
|
|
|
long_result = (self.int_reg.get_reg(inst.rs1) * self.int_reg.get_reg(inst.rs2)) as i128;
|
|
|
|
self.int_reg.set_reg(inst.rd, ((long_result >> 64) & 0xffffffffffffffff) as i64)
|
2023-03-23 20:05:46 +01:00
|
|
|
},
|
|
|
|
RISCV_OP_M_MULHSU => {
|
2023-03-24 18:48:07 +01:00
|
|
|
unsigned_reg2 = self.int_reg.get_reg(inst.rs2) as u64;
|
|
|
|
long_result = (self.int_reg.get_reg(inst.rs1) as u64 * unsigned_reg2) as i128;
|
|
|
|
self.int_reg.set_reg(inst.rd, ((long_result >> 64) & 0xffffffffffffffff) as i64)
|
2023-03-23 20:05:46 +01:00
|
|
|
},
|
|
|
|
RISCV_OP_M_MULHU => {
|
2023-03-24 18:48:07 +01:00
|
|
|
unsigned_reg1 = self.int_reg.get_reg(inst.rs1) as u64;
|
|
|
|
unsigned_reg2 = self.int_reg.get_reg(inst.rs2) as u64;
|
2023-03-23 20:05:46 +01:00
|
|
|
long_result = (unsigned_reg1 * unsigned_reg2) as i128;
|
2023-03-24 18:48:07 +01:00
|
|
|
self.int_reg.set_reg(inst.rd, ((long_result >> 64) & 0xffffffffffffffff) as i64);
|
2023-03-23 20:05:46 +01:00
|
|
|
},
|
2023-03-24 18:48:07 +01:00
|
|
|
RISCV_OP_M_DIV => self.int_reg.set_reg(inst.rd, self.int_reg.get_reg(inst.rs1) / self.int_reg.get_reg(inst.rs2)),
|
2023-03-23 20:05:46 +01:00
|
|
|
_ => panic!("RISCV_OP : funct7 = 1 (Multiplication) :: Error\n")
|
2023-03-07 17:32:59 +01:00
|
|
|
}
|
2023-03-23 20:05:46 +01:00
|
|
|
} else {
|
|
|
|
match inst.funct3 {
|
|
|
|
RISCV_OP_ADD => if inst.funct7 == RISCV_OP_ADD_ADD {
|
2023-03-24 18:48:07 +01:00
|
|
|
self.int_reg.set_reg(inst.rd, self.int_reg.get_reg(inst.rs1) + self.int_reg.get_reg(inst.rs2))
|
2023-03-23 20:05:46 +01:00
|
|
|
} else {
|
2023-03-24 18:48:07 +01:00
|
|
|
self.int_reg.set_reg(inst.rd, self.int_reg.get_reg(inst.rs1) - self.int_reg.get_reg(inst.rs2))
|
2023-03-23 20:05:46 +01:00
|
|
|
},
|
2023-03-24 18:48:07 +01:00
|
|
|
RISCV_OP_SLL => self.int_reg.set_reg(inst.rd, self.int_reg.get_reg(inst.rs1) << (self.int_reg.get_reg(inst.rs2) & 0x3f)),
|
|
|
|
RISCV_OP_SLT => if self.int_reg.get_reg(inst.rs1) < self.int_reg.get_reg(inst.rs2) {
|
|
|
|
self.int_reg.set_reg(inst.rd, 1)
|
2023-03-23 20:05:46 +01:00
|
|
|
} else {
|
2023-03-24 18:48:07 +01:00
|
|
|
self.int_reg.set_reg(inst.rd, 0)
|
2023-03-23 20:05:46 +01:00
|
|
|
},
|
|
|
|
RISCV_OP_SLTU => {
|
2023-03-24 18:48:07 +01:00
|
|
|
unsigned_reg1 = self.int_reg.get_reg(inst.rs1) as u64;
|
|
|
|
unsigned_reg2 = self.int_reg.get_reg(inst.rs2) as u64;
|
2023-03-23 20:05:46 +01:00
|
|
|
if unsigned_reg1 < unsigned_reg2 {
|
2023-03-24 18:48:07 +01:00
|
|
|
self.int_reg.set_reg(inst.rd, 1)
|
2023-03-23 20:05:46 +01:00
|
|
|
} else {
|
2023-03-24 18:48:07 +01:00
|
|
|
self.int_reg.set_reg(inst.rd, 0)
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
|
|
|
},
|
2023-03-24 18:48:07 +01:00
|
|
|
RISCV_OP_XOR => self.int_reg.set_reg(inst.rd, self.int_reg.get_reg(inst.rs1) ^ self.int_reg.get_reg(inst.rs2)),
|
|
|
|
RISCV_OP_SR => self.int_reg.set_reg(inst.rd, self.int_reg.get_reg(inst.rs1) >> self.int_reg.get_reg(inst.rs2)), // RISCV_OP_SR_SRL inaccessible
|
|
|
|
RISCV_OP_OR => self.int_reg.set_reg(inst.rd, self.int_reg.get_reg(inst.rs1) | self.int_reg.get_reg(inst.rs2)),
|
|
|
|
RISCV_OP_AND => self.int_reg.set_reg(inst.rd, self.int_reg.get_reg(inst.rs1) & self.int_reg.get_reg(inst.rs2)),
|
2023-03-23 20:05:46 +01:00
|
|
|
_ => panic!("RISCV_OP undefined case\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Exectutes simple RISC-V *iw instructions on the machine
|
2023-03-24 18:48:07 +01:00
|
|
|
fn opiw_instruction(&mut self, inst: Instruction) -> Result<(), MachineError> {
|
|
|
|
let local_data = self.int_reg.get_reg(inst.rs1);
|
2023-03-23 20:05:46 +01:00
|
|
|
match inst.funct3 {
|
|
|
|
RISCV_OPIW_ADDIW => {
|
|
|
|
let result = local_data + inst.imm12_I_signed as i64;
|
2023-03-24 18:48:07 +01:00
|
|
|
self.int_reg.set_reg(inst.rd, result)
|
2023-03-23 20:05:46 +01:00
|
|
|
},
|
|
|
|
RISCV_OPIW_SLLIW => {
|
|
|
|
let result = local_data << inst.shamt;
|
2023-03-24 18:48:07 +01:00
|
|
|
self.int_reg.set_reg(inst.rd, result)
|
2023-03-23 20:05:46 +01:00
|
|
|
},
|
|
|
|
RISCV_OPIW_SRW => {
|
|
|
|
let result = if inst.funct7 == RISCV_OPIW_SRW_SRLIW {
|
2023-03-24 18:48:07 +01:00
|
|
|
(local_data >> inst.shamt) & self.shiftmask[32 + inst.shamt as usize] as i64
|
2023-03-23 20:05:46 +01:00
|
|
|
} else { // SRAIW
|
|
|
|
local_data >> inst.shamt
|
|
|
|
};
|
2023-03-24 18:48:07 +01:00
|
|
|
self.int_reg.set_reg(inst.rd, result)
|
2023-03-23 20:05:46 +01:00
|
|
|
},
|
|
|
|
_ => panic!("In OPI switch case, this should never happen... Instr was {}\n", inst.value),
|
2022-11-09 16:47:26 +01:00
|
|
|
}
|
2023-03-23 20:05:46 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-11-09 16:47:26 +01:00
|
|
|
|
2023-03-23 20:05:46 +01:00
|
|
|
/// Executes simple RISC-V *w instructions on the machine
|
2023-03-24 18:48:07 +01:00
|
|
|
fn opw_instruction(&mut self, inst: Instruction) -> Result<(), MachineError> {
|
2023-03-23 20:05:46 +01:00
|
|
|
if inst.funct7 == 1 { // rv64m
|
2023-03-24 18:48:07 +01:00
|
|
|
let local_data_a = self.int_reg.get_reg(inst.rs1) & 0xffffffff;
|
|
|
|
let local_data_b = self.int_reg.get_reg(inst.rs2) & 0xffffffff;
|
|
|
|
let local_data_a_unsigned = self.int_reg.get_reg(inst.rs1) & 0xffffffff;
|
|
|
|
let local_data_b_unsigned = self.int_reg.get_reg(inst.rs2) & 0xffffffff;
|
2023-03-23 20:05:46 +01:00
|
|
|
|
|
|
|
// Match case for multiplication operations (in standard extension RV32M)
|
|
|
|
match inst.funct3 {
|
2023-03-24 18:48:07 +01:00
|
|
|
RISCV_OPW_M_MULW => self.int_reg.set_reg(inst.rd, local_data_a * local_data_b),
|
|
|
|
RISCV_OPW_M_DIVW => self.int_reg.set_reg(inst.rd, local_data_a / local_data_b),
|
|
|
|
RISCV_OPW_M_DIVUW => self.int_reg.set_reg(inst.rd, local_data_a_unsigned / local_data_b_unsigned),
|
|
|
|
RISCV_OPW_M_REMW => self.int_reg.set_reg(inst.rd, local_data_a % local_data_b),
|
|
|
|
RISCV_OPW_M_REMUW => self.int_reg.set_reg(inst.rd, local_data_a_unsigned % local_data_b_unsigned),
|
2023-03-23 20:05:46 +01:00
|
|
|
_ => panic!("this instruction ({}) doesn't exists", inst.value)
|
|
|
|
}
|
|
|
|
} else { // others rv64 OPW operations
|
2023-03-24 18:48:07 +01:00
|
|
|
let local_dataa = self.int_reg.get_reg(inst.rs1) & 0xffffffff;
|
|
|
|
let local_datab = self.int_reg.get_reg(inst.rs2) & 0xffffffff;
|
2023-03-23 20:05:46 +01:00
|
|
|
// Match case for base OP operation
|
|
|
|
match inst.funct3 {
|
|
|
|
RISCV_OPW_ADDSUBW => if inst.funct7 == RISCV_OPW_ADDSUBW_ADDW {
|
2023-03-24 18:48:07 +01:00
|
|
|
self.int_reg.set_reg(inst.rd, local_dataa + local_datab);
|
2023-03-23 20:05:46 +01:00
|
|
|
} else { // SUBW
|
2023-03-24 18:48:07 +01:00
|
|
|
self.int_reg.set_reg(inst.rd, local_dataa - local_datab);
|
2023-03-23 20:05:46 +01:00
|
|
|
},
|
2023-03-24 18:48:07 +01:00
|
|
|
RISCV_OPW_SLLW => self.int_reg.set_reg(inst.rd, local_dataa << (local_datab & 0x1f)),
|
2023-03-23 20:05:46 +01:00
|
|
|
RISCV_OPW_SRW => if inst.funct7 == RISCV_OPW_SRW_SRLW {
|
2023-03-24 18:48:07 +01:00
|
|
|
self.int_reg.set_reg(inst.rd, local_dataa >> (local_datab & 0x1f) & self.shiftmask[32 + local_datab as usize] as i64)
|
2023-03-23 20:05:46 +01:00
|
|
|
} else { // SRAW
|
2023-03-24 18:48:07 +01:00
|
|
|
self.int_reg.set_reg(inst.rd, local_dataa >> (local_datab & 0x1f))
|
2023-03-23 20:05:46 +01:00
|
|
|
},
|
|
|
|
_ => panic!("this instruction ({}) doesn't exists", inst.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-11-09 16:47:26 +01:00
|
|
|
|
2023-03-23 20:05:46 +01:00
|
|
|
/// Executes simple RISC-V floating point instructions on the machine
|
2023-03-24 18:48:07 +01:00
|
|
|
fn fp_instruction(&mut self, inst: Instruction) -> Result<(), MachineError> {
|
2023-03-23 20:05:46 +01:00
|
|
|
match inst.funct7 {
|
2023-03-24 18:48:07 +01:00
|
|
|
RISCV_FP_ADD => self.fp_reg.set_reg(inst.rd, self.fp_reg.get_reg(inst.rs1) + self.fp_reg.get_reg(inst.rs2)),
|
|
|
|
RISCV_FP_SUB => self.fp_reg.set_reg(inst.rd, self.fp_reg.get_reg(inst.rs1) - self.fp_reg.get_reg(inst.rs2)),
|
|
|
|
RISCV_FP_MUL => self.fp_reg.set_reg(inst.rd, self.fp_reg.get_reg(inst.rs1) * self.fp_reg.get_reg(inst.rs2)),
|
|
|
|
RISCV_FP_DIV => self.fp_reg.set_reg(inst.rd, self.fp_reg.get_reg(inst.rs1) / self.fp_reg.get_reg(inst.rs2)),
|
|
|
|
RISCV_FP_SQRT => self.fp_reg.set_reg(inst.rd, self.fp_reg.get_reg(inst.rs1).sqrt()),
|
2023-03-23 20:05:46 +01:00
|
|
|
RISCV_FP_FSGN => {
|
2023-03-24 18:48:07 +01:00
|
|
|
let local_float = self.fp_reg.get_reg(inst.rs1);
|
2023-03-23 20:05:46 +01:00
|
|
|
match inst.funct3 {
|
2023-03-24 18:48:07 +01:00
|
|
|
RISCV_FP_FSGN_J => if self.fp_reg.get_reg(inst.rs2) < 0f32 {
|
|
|
|
self.fp_reg.set_reg(inst.rd, -local_float)
|
2023-03-23 20:05:46 +01:00
|
|
|
} else {
|
2023-03-24 18:48:07 +01:00
|
|
|
self.fp_reg.set_reg(inst.rd, local_float)
|
2023-03-23 20:05:46 +01:00
|
|
|
},
|
2023-03-24 18:48:07 +01:00
|
|
|
RISCV_FP_FSGN_JN => if self.fp_reg.get_reg(inst.rs2) < 0f32 {
|
|
|
|
self.fp_reg.set_reg(inst.rd, local_float)
|
2023-03-23 20:05:46 +01:00
|
|
|
} else {
|
2023-03-24 18:48:07 +01:00
|
|
|
self.fp_reg.set_reg(inst.rd, -local_float)
|
2023-03-23 20:05:46 +01:00
|
|
|
},
|
2023-03-24 18:48:07 +01:00
|
|
|
RISCV_FP_FSGN_JX => if (self.fp_reg.get_reg(inst.rs2) < 0.0 && self.fp_reg.get_reg(inst.rs1) >= 0.0) ||
|
|
|
|
(self.fp_reg.get_reg(inst.rs2) >= 0.0 && self.fp_reg.get_reg(inst.rs1) < 0.0) {
|
|
|
|
self.fp_reg.set_reg(inst.rd, -local_float)
|
2023-03-23 20:05:46 +01:00
|
|
|
} else {
|
2023-03-24 18:48:07 +01:00
|
|
|
self.fp_reg.set_reg(inst.rd, local_float)
|
2023-03-23 20:05:46 +01:00
|
|
|
},
|
|
|
|
_ => panic!("this instruction ({}) doesn't exists", inst.value)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
RISCV_FP_MINMAX => {
|
2023-03-24 18:48:07 +01:00
|
|
|
let r1 = self.fp_reg.get_reg(inst.rs1);
|
|
|
|
let r2 = self.fp_reg.get_reg(inst.rs2);
|
2023-03-23 20:05:46 +01:00
|
|
|
match inst.funct3 {
|
2023-03-24 18:48:07 +01:00
|
|
|
RISCV_FP_MINMAX_MIN => self.fp_reg.set_reg(inst.rd, if r1 < r2 {r1} else {r2}),
|
|
|
|
RISCV_FP_MINMAX_MAX => self.fp_reg.set_reg(inst.rd, if r1 > r2 {r1} else {r2}),
|
2023-03-23 20:05:46 +01:00
|
|
|
_ => panic!("this instruction ({}) doesn't exists", inst.value)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
RISCV_FP_FCVTW => {
|
|
|
|
if inst.rs2 == RISCV_FP_FCVTW_W {
|
2023-03-24 18:48:07 +01:00
|
|
|
self.int_reg.set_reg(inst.rd, self.fp_reg.get_reg(inst.rs1) as i64)
|
2023-03-23 20:05:46 +01:00
|
|
|
} else {
|
2023-03-24 18:48:07 +01:00
|
|
|
self.int_reg.set_reg(inst.rd, (self.fp_reg.get_reg(inst.rs1) as u64) as i64)
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
RISCV_FP_FCVTS => {
|
|
|
|
if inst.rs2 == RISCV_FP_FCVTS_W {
|
2023-03-24 18:48:07 +01:00
|
|
|
self.fp_reg.set_reg(inst.rd, self.int_reg.get_reg(inst.rs1) as f32);
|
2023-03-23 20:05:46 +01:00
|
|
|
} else {
|
2023-03-24 18:48:07 +01:00
|
|
|
self.fp_reg.set_reg(inst.rd, (self.int_reg.get_reg(inst.rs1) as u32) as f32);
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
|
|
|
},
|
2023-03-24 18:48:07 +01:00
|
|
|
RISCV_FP_FMVW => self.fp_reg.set_reg(inst.rd, self.int_reg.get_reg(inst.rs1) as f32),
|
2023-03-23 20:05:46 +01:00
|
|
|
RISCV_FP_FMVXFCLASS => {
|
|
|
|
if inst.funct3 == RISCV_FP_FMVXFCLASS_FMVX {
|
2023-03-24 18:48:07 +01:00
|
|
|
self.int_reg.set_reg(inst.rd, self.fp_reg.get_reg(inst.rs1) as i64);
|
2023-03-23 20:05:46 +01:00
|
|
|
} else {
|
|
|
|
panic!("Fclass instruction is not handled in riscv simulator");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
RISCV_FP_FCMP => {
|
|
|
|
match inst.funct3 {
|
2023-03-24 18:48:07 +01:00
|
|
|
RISCV_FP_FCMP_FEQ => self.int_reg.set_reg(inst.rd, (self.fp_reg.get_reg(inst.rs1) == self.fp_reg.get_reg(inst.rs2)) as i64),
|
|
|
|
RISCV_FP_FCMP_FLT => self.int_reg.set_reg(inst.rd, (self.fp_reg.get_reg(inst.rs1) < self.fp_reg.get_reg(inst.rs2)) as i64),
|
|
|
|
RISCV_FP_FCMP_FLE => self.int_reg.set_reg(inst.rd, (self.fp_reg.get_reg(inst.rs1) <= self.fp_reg.get_reg(inst.rs2)) as i64),
|
2023-03-23 20:05:46 +01:00
|
|
|
_ => panic!("this instruction ({}) doesn't exists", inst.value)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => panic!("this instruction ({}) doesn't exists", inst.value)
|
|
|
|
}
|
|
|
|
Ok(())
|
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
|
|
|
|
///
|
2023-03-23 21:55:46 +01:00
|
|
|
/// "@"adress [16 bytes]
|
2023-03-24 18:48:07 +01:00
|
|
|
pub fn print_memory(&self, 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);
|
|
|
|
}
|
2023-03-24 18:48:07 +01:00
|
|
|
print!("{:02x}", self.main_memory[i]);
|
2023-03-01 15:11:35 +01:00
|
|
|
}
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
|
2023-03-23 21:55:46 +01:00
|
|
|
/// Get value from int register
|
2023-03-09 12:08:33 +01:00
|
|
|
pub fn read_int_register(&self, index: usize) -> i64 {
|
2023-03-24 17:32:04 +01:00
|
|
|
self.int_reg.get_reg(index as u8)
|
2023-03-09 12:08:33 +01:00
|
|
|
}
|
|
|
|
|
2023-03-23 21:55:46 +01:00
|
|
|
/// Get value from float register
|
2023-03-09 12:08:33 +01:00
|
|
|
pub fn read_fp_register(&self, index: usize) -> f32 {
|
2023-03-24 17:32:04 +01:00
|
|
|
self.fp_reg.get_reg(index as u8)
|
2023-03-09 12:08:33 +01:00
|
|
|
}
|
|
|
|
|
2023-03-23 21:55:46 +01:00
|
|
|
/// Write into int register
|
2023-03-09 12:08:33 +01:00
|
|
|
pub fn write_int_register(&mut self, index: usize, value: i64) {
|
2023-03-24 18:20:59 +01:00
|
|
|
self.int_reg.set_reg(index as u8, value);
|
2023-03-09 12:08:33 +01:00
|
|
|
}
|
|
|
|
|
2023-03-23 21:55:46 +01:00
|
|
|
/// Write info float register
|
2023-03-09 12:08:33 +01:00
|
|
|
pub fn write_fp_register(&mut self, index: usize, value: f32) {
|
2023-03-24 18:20:59 +01:00
|
|
|
self.fp_reg.set_reg(index as u8, value);
|
2023-03-09 12:08:33 +01:00
|
|
|
}
|
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-23 17:42:36 +01:00
|
|
|
macro_rules! get_full_path {
|
|
|
|
($prefix: expr, $test_name:expr) => {{
|
|
|
|
let mut s = String::from("test/machine/");
|
|
|
|
s.push_str($prefix);
|
|
|
|
s.push_str($test_name);
|
|
|
|
s.push_str(".txt");
|
|
|
|
&s.to_owned()
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! init_test {
|
|
|
|
($a:expr) => {{
|
|
|
|
let mut m = Machine::init_machine();
|
|
|
|
let end_file_name = { let mut s = String::from($a); s.push_str("End"); s };
|
|
|
|
let memory_before = mem_cmp::MemChecker::from(get_full_path!("memory", $a)).unwrap();
|
|
|
|
let memory_after = mem_cmp::MemChecker::from(get_full_path!("memory", &end_file_name)).unwrap();
|
|
|
|
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
|
|
|
Machine::run(&mut m);
|
|
|
|
let expected_trace = fs::read_to_string(get_full_path!("reg_trace", $a)).unwrap();
|
2023-03-24 18:27:28 +01:00
|
|
|
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
|
|
|
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
2023-03-23 17:42:36 +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;
|
2023-03-24 18:34:06 +01:00
|
|
|
assert_eq!((43 << 8) + 150, m.read_memory(2, 4));
|
2023-01-18 17:01:48 +01:00
|
|
|
}
|
|
|
|
|
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-03-24 18:36:02 +01:00
|
|
|
m.write_memory(2, 6, (43 << 8) + 150);
|
2023-01-18 17:01:48 +01:00
|
|
|
assert_eq!(43, m.main_memory[6]);
|
|
|
|
assert_eq!(150, m.main_memory[7]);
|
2023-03-24 18:36:02 +01:00
|
|
|
m.write_memory(4, 8, (52 << 24) + (20 << 16) + (43 << 8) + 150);
|
2023-02-15 18:09:18 +01:00
|
|
|
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-24 18:27:28 +01:00
|
|
|
init_test!("Comp")
|
2023-03-08 17:58:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-03-15 15:35:28 +01:00
|
|
|
fn test_add() {
|
2023-03-24 18:27:28 +01:00
|
|
|
init_test!("Add")
|
2023-03-15 15:35:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-03-08 17:58:38 +01:00
|
|
|
fn test_div() {
|
2023-03-24 18:27:28 +01:00
|
|
|
init_test!("Div")
|
2023-03-08 17:58:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_if() {
|
2023-03-24 18:27:28 +01:00
|
|
|
init_test!("If")
|
2023-03-08 17:58:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_jump() {
|
2023-03-24 18:27:28 +01:00
|
|
|
init_test!("Jump")
|
2023-03-08 17:58:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mul() {
|
2023-03-24 18:27:28 +01:00
|
|
|
init_test!("Mult")
|
2023-03-08 17:58:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ret() {
|
2023-03-24 18:27:28 +01:00
|
|
|
init_test!("Ret")
|
2023-03-08 17:58:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_sub() {
|
2023-03-24 18:27:28 +01:00
|
|
|
init_test!("Sub")
|
2023-03-08 17:58:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_switch() {
|
2023-03-24 18:27:28 +01:00
|
|
|
init_test!("Switch")
|
2023-03-08 17:58:38 +01:00
|
|
|
}
|
2023-01-11 15:36:27 +01:00
|
|
|
}
|