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,
|
2023-03-27 11:35:04 +02:00
|
|
|
fs::File
|
2023-03-24 17:44:24 +01:00
|
|
|
};
|
2023-04-05 12:01:31 +02:00
|
|
|
use crate::{simulator::{
|
2023-03-24 17:44:24 +01:00
|
|
|
error::MachineError,
|
2023-03-29 16:08:06 +02:00
|
|
|
instruction::{*, self},
|
2023-03-24 17:44:24 +01:00
|
|
|
interrupt::Interrupt,
|
|
|
|
global::*,
|
|
|
|
register::*
|
2023-04-19 18:09:08 +02:00
|
|
|
}, kernel::system::System, utility::cfg::{Settings, MachineSettingKey}};
|
2023-02-08 14:34:09 +01:00
|
|
|
|
2023-03-29 16:13:57 +02:00
|
|
|
use crate::kernel::{
|
|
|
|
exception
|
|
|
|
};
|
|
|
|
|
2023-03-29 17:01:28 +02:00
|
|
|
use super::error::MachineOk;
|
|
|
|
|
2023-03-27 11:35:04 +02:00
|
|
|
/// # Exceptions
|
|
|
|
///
|
|
|
|
/// Textual names of the exceptions that can be generated by user program
|
|
|
|
/// execution, for debugging purpose.
|
2023-03-23 20:58:10 +01:00
|
|
|
/// todo: is this really supposed to stand in machine.rs?
|
2023-04-05 13:49:32 +02:00
|
|
|
#[derive(Debug)]
|
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-29 16:13:57 +02:00
|
|
|
/// # Machine Status
|
|
|
|
///
|
|
|
|
/// The machine can be running kernel code (SystemMode), user code (UserMode),
|
|
|
|
/// or there can be no running thread if the ready list is empty (IdleMode).
|
|
|
|
pub enum MachineStatus {
|
|
|
|
IdleMode,
|
|
|
|
SystemMode,
|
|
|
|
UserMode
|
|
|
|
}
|
|
|
|
|
2023-03-27 11:35:04 +02:00
|
|
|
/// ID of the stack register
|
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-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-04-06 13:27:03 +02:00
|
|
|
/// Debug mode of the machine
|
|
|
|
debug: bool,
|
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-29 16:13:57 +02: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
|
2023-04-19 18:09:08 +02:00
|
|
|
num_phy_page: u64,
|
2023-04-23 15:11:06 +02:00
|
|
|
pub page_size: u64,
|
2023-03-29 16:13:57 +02:00
|
|
|
/// Current machine status
|
|
|
|
pub status: MachineStatus
|
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-04-19 18:09:08 +02:00
|
|
|
pub fn new(debug: bool, settings: Settings) -> Self {
|
2023-04-06 13:27:03 +02:00
|
|
|
let mut shiftmask : [u64 ; 64] = [0 ; 64];
|
|
|
|
let mut value : u64 = 0xffffffff;
|
|
|
|
value = (value << 32) + value;
|
|
|
|
for item in &mut shiftmask {
|
|
|
|
*item = value;
|
|
|
|
value >>= 1;
|
|
|
|
}
|
|
|
|
|
2023-04-19 18:09:08 +02:00
|
|
|
let num_phy_page = *settings.get(&MachineSettingKey::NumPhysPages).unwrap();
|
|
|
|
let page_size = *settings.get(&MachineSettingKey::PageSize).unwrap();
|
|
|
|
let mem_size = (page_size*num_phy_page*100_000) as usize;
|
|
|
|
|
2023-04-06 13:27:03 +02:00
|
|
|
Machine {
|
|
|
|
debug,
|
|
|
|
pc : 0,
|
|
|
|
sp: 0,
|
|
|
|
int_reg : { let mut r = Register::<i64>::init(); r.set_reg(10, -1); r },
|
|
|
|
fp_reg : Register::<f32>::init(),
|
2023-04-19 18:09:08 +02:00
|
|
|
main_memory : vec![0_u8; mem_size],
|
2023-04-06 13:27:03 +02:00
|
|
|
shiftmask,
|
|
|
|
interrupt: Interrupt::new(),
|
|
|
|
registers_trace : String::from(""),
|
2023-04-19 18:09:08 +02:00
|
|
|
status: MachineStatus::SystemMode,
|
|
|
|
num_phy_page,
|
|
|
|
page_size
|
2023-04-06 13:27:03 +02: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-27 18:10:11 +02:00
|
|
|
pub fn _extract_memory(&mut 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) {
|
2023-03-29 16:08:06 +02:00
|
|
|
print!(">{0: <4} : {1:<16x} ", instruction::REG_X[i], self.int_reg.get_reg(i as u8));
|
|
|
|
print!(">{0: <4} : {1:<16x} ", instruction::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-29 16:08:06 +02:00
|
|
|
print!(">{0: <4} : {1:<16x} ", instruction::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-04-06 13:27:03 +02:00
|
|
|
let sp = self.int_reg.get_reg(2);
|
|
|
|
println!("SP: {:16x}", self.read_memory(8, sp as usize));
|
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
|
|
|
|
}
|
|
|
|
|
2023-04-05 12:01:31 +02:00
|
|
|
pub fn raise_exception(&mut self, exception: ExceptionType, address : u64, system: &mut System) -> Result<MachineOk, MachineError>{
|
2023-03-29 16:13:57 +02:00
|
|
|
self.set_status(MachineStatus::SystemMode);
|
|
|
|
// Handle the interruption
|
2023-04-05 13:49:32 +02:00
|
|
|
match exception::call(&exception, self, system) {
|
2023-04-05 16:02:54 +02:00
|
|
|
Ok(r) => {
|
2023-03-29 17:21:34 +02:00
|
|
|
self.set_status(MachineStatus::UserMode);
|
2023-04-05 16:02:54 +02:00
|
|
|
Ok(r)
|
|
|
|
},
|
|
|
|
Err(e) => Err(format!("Syscall {:?} invalid or not implemented", e))?
|
|
|
|
}
|
2023-03-29 16:13:57 +02:00
|
|
|
}
|
|
|
|
|
2023-04-05 16:44:26 +02:00
|
|
|
/// Execute the instructions table of a machine put in param
|
2022-11-23 16:04:21 +01:00
|
|
|
///
|
|
|
|
/// ### Parameters
|
|
|
|
///
|
|
|
|
/// - **machine** which contains a table of instructions
|
2023-04-05 12:01:31 +02:00
|
|
|
pub fn run(&mut self, system: &mut System) {
|
2023-03-27 10:21:18 +02:00
|
|
|
loop {
|
2023-04-05 12:01:31 +02:00
|
|
|
match self.one_instruction(system) {
|
2023-04-05 16:02:54 +02:00
|
|
|
Ok(MachineOk::Ok) => {},
|
2023-03-29 17:21:34 +02:00
|
|
|
Ok(MachineOk::Shutdown) => break,
|
2023-04-05 13:41:56 +02:00
|
|
|
Err(e) => panic!("FATAL at pc {} -> {}", self.pc, e)
|
2023-03-27 10:21:18 +02:00
|
|
|
}
|
2023-03-28 21:08:00 +02:00
|
|
|
self.write_int_register(0, 0); // In case an instruction write on register 0
|
2023-03-27 10:21:18 +02:00
|
|
|
}
|
2022-11-21 13:17:42 +01:00
|
|
|
}
|
|
|
|
|
2023-04-05 16:44:26 +02:00
|
|
|
/// Execute the instructions table of a machine put in param
|
|
|
|
/// **WITHOUT INTERPRETING SYSCALLS**
|
|
|
|
///
|
|
|
|
/// For debug purposes
|
|
|
|
pub fn _run_debug(&mut self, system: &mut System) {
|
|
|
|
loop {
|
|
|
|
match self.one_instruction(system) {
|
|
|
|
Ok(_) => (),
|
|
|
|
_ => break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-05 12:01:31 +02:00
|
|
|
pub fn one_instruction(&mut self, system: &mut System) -> Result<MachineOk, 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-27 11:35:04 +02:00
|
|
|
for (i, elem) in val.iter_mut().enumerate() {
|
|
|
|
*elem = 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-29 16:26:27 +02:00
|
|
|
let inst : Instruction = Instruction::new(val);
|
2023-04-06 13:27:03 +02:00
|
|
|
if self.debug {
|
|
|
|
self.print_status();
|
|
|
|
println!("executing instruction : {:016x} at pc {:x}", val, self.pc);
|
|
|
|
println!("{}", instruction::instruction_debug(&inst, 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-29 17:01:28 +02:00
|
|
|
Ok(MachineOk::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-29 17:01:28 +02:00
|
|
|
Ok(MachineOk::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-04-03 23:08:13 +02:00
|
|
|
Ok(MachineOk::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-04-03 23:08:13 +02:00
|
|
|
Ok(MachineOk::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-29 15:25:58 +02:00
|
|
|
RISCV_BR => self.branch_instruction(inst),
|
2023-03-25 15:37:14 +01:00
|
|
|
|
2022-11-23 18:04:35 +01:00
|
|
|
// Treatment for: LOAD INSTRUCTIONS
|
2023-03-29 15:25:58 +02:00
|
|
|
RISCV_LD => self.load_instruction(inst),
|
2023-03-25 15:37:14 +01:00
|
|
|
|
|
|
|
// Treatment for: STORE INSTRUCTIONS
|
2023-03-29 15:25:58 +02:00
|
|
|
RISCV_ST => self.store_instruction(inst),
|
2023-03-25 15:37:14 +01:00
|
|
|
|
|
|
|
// Treatment for: OP INSTRUCTIONS
|
2023-03-29 15:25:58 +02:00
|
|
|
RISCV_OP => self.op_instruction(inst),
|
2023-03-25 15:37:14 +01:00
|
|
|
|
2023-03-29 15:25:58 +02:00
|
|
|
// Treatment for: OPI INSTRUCTIONS
|
|
|
|
RISCV_OPI => self.opi_instruction(inst),
|
|
|
|
|
2022-11-16 17:59:09 +01:00
|
|
|
// Treatment for: OPW INSTRUCTIONS
|
2023-03-29 15:25:58 +02:00
|
|
|
RISCV_OPW => self.opw_instruction(inst),
|
|
|
|
|
|
|
|
// Treatment for OPIW INSTRUCTIONS
|
|
|
|
RISCV_OPIW => self.opiw_instruction(inst),
|
2023-03-25 15:37:14 +01:00
|
|
|
|
|
|
|
// Treatment for: FLOATING POINT INSTRUCTIONS
|
2023-03-29 15:25:58 +02:00
|
|
|
RISCV_FP => self.fp_instruction(inst),
|
2023-03-25 15:37:14 +01:00
|
|
|
|
|
|
|
// Treatment for: SYSTEM CALLS
|
2023-04-05 12:01:31 +02:00
|
|
|
RISCV_SYSTEM => self.raise_exception(ExceptionType::SyscallException, self.pc, system),
|
2023-03-25 15:37:14 +01:00
|
|
|
|
|
|
|
// Default case
|
2023-03-27 15:56:23 +02:00
|
|
|
_ => Err(format!("{:x}: Unknown opcode\npc: {:x}", inst.opcode, self.pc))?
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Treatement for Branch instructions
|
2023-03-29 17:01:28 +02:00
|
|
|
fn branch_instruction(&mut self, inst: Instruction) -> Result<MachineOk, MachineError> {
|
2023-03-27 15:37:30 +02:00
|
|
|
let op = match inst.funct3 {
|
|
|
|
RISCV_BR_BEQ => |a, b| a == b,
|
|
|
|
RISCV_BR_BNE => |a, b| a != b,
|
|
|
|
RISCV_BR_BLT => |a, b| a < b,
|
|
|
|
RISCV_BR_BGE => |a, b| a >= b,
|
|
|
|
RISCV_BR_BLTU => |a, b| a < b,
|
|
|
|
RISCV_BR_BGEU => |a, b| a >= b,
|
2023-03-29 15:25:58 +02:00
|
|
|
_ => Err(format!("Unreachable in branch_instruction match! Instruction was {:?}", inst))?
|
2023-03-27 15:37:30 +02:00
|
|
|
};
|
|
|
|
let rs1 = self.int_reg.get_reg(inst.rs1);
|
|
|
|
let rs2 = self.int_reg.get_reg(inst.rs2);
|
|
|
|
if op(rs1, rs2) {
|
|
|
|
self.pc = (self.pc as i64 + inst.imm13_signed as i64 - 4) as u64;
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
2023-03-29 17:01:28 +02:00
|
|
|
Ok(MachineOk::Ok)
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes RISC-V Load Instructions on the machine
|
2023-03-29 17:01:28 +02:00
|
|
|
fn load_instruction(&mut self, inst: Instruction) -> Result<MachineOk, 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);
|
2023-03-29 17:01:28 +02:00
|
|
|
Ok(MachineOk::Ok)
|
2023-03-25 15:57:28 +01:00
|
|
|
};
|
2023-03-23 20:05:46 +01:00
|
|
|
match inst.funct3 {
|
2023-03-29 15:25:58 +02: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(format!("Unreachable in load_instruction match! Instruction was {:?}", inst))?
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes RISC-V Store Instructions on the machine
|
2023-03-29 17:01:28 +02:00
|
|
|
fn store_instruction(&mut self, inst: Instruction) -> Result<MachineOk, 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-29 17:01:28 +02:00
|
|
|
Ok(MachineOk::Ok)
|
2023-03-25 15:57:28 +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-29 15:25:58 +02:00
|
|
|
_ => Err(format!("Unreachable in store_instruction match! Instruction was {:?}", inst))?
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes RISC-V Integer Register-Immediate Instructions on the machine
|
2023-03-29 17:01:28 +02:00
|
|
|
fn opi_instruction(&mut self, inst: Instruction) -> Result<MachineOk, MachineError> {
|
2023-03-27 14:59:22 +02:00
|
|
|
let rs1 = self.int_reg.get_reg(inst.rs1);
|
|
|
|
let imm12 = inst.imm12_I_signed as i64;
|
|
|
|
let shamt = inst.shamt as i64;
|
|
|
|
let mut compute = |operation: &dyn Fn (i64, i64) -> i64, a, b| {
|
|
|
|
self.int_reg.set_reg(inst.rd, operation(a, b));
|
2023-03-29 17:01:28 +02:00
|
|
|
Ok(MachineOk::Ok)
|
2023-03-27 11:22:53 +02:00
|
|
|
};
|
2023-03-23 20:05:46 +01:00
|
|
|
match inst.funct3 {
|
2023-03-29 15:25:58 +02:00
|
|
|
RISCV_OPI_ADDI => compute(&std::ops::Add::add, rs1, imm12),
|
|
|
|
RISCV_OPI_SLTI => compute(&|a, b| (a < b) as i64, rs1, imm12),
|
|
|
|
RISCV_OPI_XORI => compute(&core::ops::BitXor::bitxor, rs1, imm12),
|
|
|
|
RISCV_OPI_ORI => compute(&core::ops::BitOr::bitor, rs1, imm12),
|
|
|
|
RISCV_OPI_ANDI => compute(&core::ops::BitAnd::bitand, rs1, imm12),
|
|
|
|
RISCV_OPI_SLLI => compute(&core::ops::Shl::shl, rs1, imm12),
|
|
|
|
RISCV_OPI_SRI => if inst.funct7_smaller == RISCV_OPI_SRI_SRLI {
|
|
|
|
compute(&|a, b| { (a >> b) & self.shiftmask[inst.shamt as usize] as i64 }, rs1, shamt)
|
|
|
|
} else {
|
|
|
|
compute(&core::ops::Shr::shr, rs1, shamt)
|
|
|
|
}
|
|
|
|
_ => Err(format!("Unreachable in opi_instruction match! Instruction was {:?}", inst))?
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes simple RISC-V mathematical operations on the machine
|
2023-03-29 17:01:28 +02:00
|
|
|
fn op_instruction(&mut self, inst: Instruction) -> Result<MachineOk, 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 {
|
2023-03-29 15:25:58 +02:00
|
|
|
RISCV_OP_M_MUL => {
|
|
|
|
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)
|
|
|
|
},
|
|
|
|
RISCV_OP_M_MULH => {
|
|
|
|
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)
|
|
|
|
},
|
|
|
|
RISCV_OP_M_MULHSU => {
|
|
|
|
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)
|
|
|
|
},
|
|
|
|
RISCV_OP_M_MULHU => {
|
|
|
|
unsigned_reg1 = self.int_reg.get_reg(inst.rs1) as u64;
|
|
|
|
unsigned_reg2 = self.int_reg.get_reg(inst.rs2) as u64;
|
|
|
|
long_result = (unsigned_reg1 * unsigned_reg2) as i128;
|
|
|
|
self.int_reg.set_reg(inst.rd, ((long_result >> 64) & 0xffffffffffffffff) as i64);
|
|
|
|
},
|
|
|
|
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)),
|
|
|
|
_ => Err(format!("Unreachable in op_instruction match! Instruction was {:?}", inst))?
|
2023-03-07 17:32:59 +01:00
|
|
|
}
|
2023-03-23 20:05:46 +01:00
|
|
|
} else {
|
|
|
|
match inst.funct3 {
|
2023-03-29 15:25:58 +02:00
|
|
|
RISCV_OP_ADD => if inst.funct7 == RISCV_OP_ADD_ADD {
|
|
|
|
self.int_reg.set_reg(inst.rd, self.int_reg.get_reg(inst.rs1) + self.int_reg.get_reg(inst.rs2))
|
|
|
|
} else {
|
|
|
|
self.int_reg.set_reg(inst.rd, self.int_reg.get_reg(inst.rs1) - self.int_reg.get_reg(inst.rs2))
|
|
|
|
},
|
|
|
|
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)
|
|
|
|
} else {
|
|
|
|
self.int_reg.set_reg(inst.rd, 0)
|
|
|
|
},
|
|
|
|
RISCV_OP_SLTU => {
|
|
|
|
unsigned_reg1 = self.int_reg.get_reg(inst.rs1) as u64;
|
|
|
|
unsigned_reg2 = self.int_reg.get_reg(inst.rs2) as u64;
|
|
|
|
if unsigned_reg1 < unsigned_reg2 {
|
|
|
|
self.int_reg.set_reg(inst.rd, 1)
|
|
|
|
} else {
|
|
|
|
self.int_reg.set_reg(inst.rd, 0)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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)),
|
|
|
|
_ => Err(format!("Unreachable in op_instruction match! Instruction was {:?}", inst))?
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
|
|
|
}
|
2023-03-29 17:01:28 +02:00
|
|
|
Ok(MachineOk::Ok)
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Exectutes simple RISC-V *iw instructions on the machine
|
2023-03-29 17:01:28 +02:00
|
|
|
fn opiw_instruction(&mut self, inst: Instruction) -> Result<MachineOk, MachineError> {
|
2023-03-24 18:48:07 +01:00
|
|
|
let local_data = self.int_reg.get_reg(inst.rs1);
|
2023-03-27 15:56:23 +02:00
|
|
|
let result = match inst.funct3 {
|
|
|
|
RISCV_OPIW_ADDIW => local_data + inst.imm12_I_signed as i64,
|
|
|
|
RISCV_OPIW_SLLIW => local_data << inst.shamt,
|
2023-03-29 15:25:58 +02:00
|
|
|
RISCV_OPIW_SRW => (local_data >> inst.shamt) & if inst.funct7 == RISCV_OPIW_SRW_SRLIW { self.shiftmask[32 + inst.shamt as usize] as i64 } else { 1 },
|
|
|
|
_ => Err(format!("Unreachable in op_instruction match! Instruction was {:?}", inst))?
|
2023-03-27 15:56:23 +02:00
|
|
|
};
|
|
|
|
self.int_reg.set_reg(inst.rd, result);
|
2023-03-29 17:01:28 +02:00
|
|
|
Ok(MachineOk::Ok)
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
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-29 17:01:28 +02:00
|
|
|
fn opw_instruction(&mut self, inst: Instruction) -> Result<MachineOk, 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-29 15:25:58 +02: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),
|
|
|
|
_ => Err(format!("Unreachable in opw_instruction match! Instruction was {:?}", inst))?
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
|
|
|
} 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 {
|
2023-03-29 15:25:58 +02:00
|
|
|
RISCV_OPW_ADDSUBW => if inst.funct7 == RISCV_OPW_ADDSUBW_ADDW {
|
|
|
|
self.int_reg.set_reg(inst.rd, local_dataa + local_datab);
|
|
|
|
} else { // SUBW
|
|
|
|
self.int_reg.set_reg(inst.rd, local_dataa - local_datab);
|
|
|
|
},
|
|
|
|
RISCV_OPW_SLLW => self.int_reg.set_reg(inst.rd, local_dataa << (local_datab & 0x1f)),
|
|
|
|
RISCV_OPW_SRW => if inst.funct7 == RISCV_OPW_SRW_SRLW {
|
|
|
|
self.int_reg.set_reg(inst.rd, local_dataa >> (local_datab & 0x1f) & self.shiftmask[32 + local_datab as usize] as i64)
|
|
|
|
} else { // SRAW
|
|
|
|
self.int_reg.set_reg(inst.rd, local_dataa >> (local_datab & 0x1f))
|
|
|
|
},
|
|
|
|
_ => Err(format!("Unreachable in opw_instruction match! Instruction was {:?}", inst))?
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
|
|
|
}
|
2023-03-29 17:01:28 +02:00
|
|
|
Ok(MachineOk::Ok)
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
2022-11-09 16:47:26 +01:00
|
|
|
|
2023-03-29 15:25:58 +02:00
|
|
|
/// Executes simple RISC-V floating point instructions on the machine.
|
|
|
|
///
|
|
|
|
/// See Risc-V Spec v2.2 Chapter 8: “F” Standard Extension for Single-Precision Floating-Point, Version 2.0.
|
2023-03-29 17:01:28 +02:00
|
|
|
fn fp_instruction(&mut self, inst: Instruction) -> Result<MachineOk, MachineError> {
|
2023-03-29 15:25:58 +02:00
|
|
|
let mut set_reg = |operation: &dyn Fn (f32, f32) -> f32| {
|
|
|
|
let a = self.fp_reg.get_reg(inst.rs1);
|
|
|
|
let b = self.fp_reg.get_reg(inst.rs2);
|
|
|
|
self.fp_reg.set_reg(inst.rd, operation(a, b));
|
2023-03-29 17:01:28 +02:00
|
|
|
Ok(MachineOk::Ok)
|
2023-03-29 15:25:58 +02:00
|
|
|
};
|
2023-03-23 20:05:46 +01:00
|
|
|
match inst.funct7 {
|
2023-03-29 15:25:58 +02:00
|
|
|
RISCV_FP_ADD => set_reg(&core::ops::Add::add),
|
|
|
|
RISCV_FP_SUB => set_reg(&core::ops::Sub::sub),
|
|
|
|
RISCV_FP_MUL => set_reg(&core::ops::Mul::mul),
|
|
|
|
RISCV_FP_DIV => set_reg(&core::ops::Div::div),
|
2023-03-29 17:01:28 +02:00
|
|
|
RISCV_FP_SQRT => { self.fp_reg.set_reg(inst.rd, self.fp_reg.get_reg(inst.rs1).sqrt()); Ok(MachineOk::Ok) },
|
2023-03-29 15:25:58 +02:00
|
|
|
RISCV_FP_FSGN => self.fp_fsgn_instruction(inst),
|
|
|
|
RISCV_FP_MINMAX => self.fp_minmax_instruction(inst),
|
|
|
|
RISCV_FP_FCVTW => self.fp_fcvtw_instruction(inst),
|
|
|
|
RISCV_FP_FCVTS => self.fp_fcvts_instruction(inst),
|
|
|
|
RISCV_FP_FMVW => self.fp_fmvw_instruction(inst),
|
|
|
|
RISCV_FP_FMVXFCLASS => self.fp_fmvxfclass_instruction(inst),
|
|
|
|
RISCV_FP_FCMP => self.fp_fcmp_instruction(inst),
|
|
|
|
_ => Err(format!("Unreachable in fp_instruction match! Instruction was {:?}", inst))?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes RISC-V sign-injection instruction on floating point values on the machine.
|
2023-03-29 17:01:28 +02:00
|
|
|
fn fp_fsgn_instruction(&mut self, inst: Instruction) -> Result<MachineOk, MachineError> {
|
2023-03-29 15:25:58 +02:00
|
|
|
let local_float = self.fp_reg.get_reg(inst.rs1);
|
|
|
|
match inst.funct3 {
|
|
|
|
RISCV_FP_FSGN_J => if self.fp_reg.get_reg(inst.rs2) < 0f32 {
|
|
|
|
self.fp_reg.set_reg(inst.rd, -local_float);
|
|
|
|
} else {
|
|
|
|
self.fp_reg.set_reg(inst.rd, local_float);
|
2023-03-23 20:05:46 +01:00
|
|
|
},
|
2023-03-29 15:25:58 +02:00
|
|
|
RISCV_FP_FSGN_JN => if self.fp_reg.get_reg(inst.rs2) < 0f32 {
|
|
|
|
self.fp_reg.set_reg(inst.rd, local_float);
|
|
|
|
} else {
|
|
|
|
self.fp_reg.set_reg(inst.rd, -local_float);
|
2023-03-23 20:05:46 +01:00
|
|
|
},
|
2023-03-29 15:25:58 +02: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);
|
|
|
|
} else {
|
|
|
|
self.fp_reg.set_reg(inst.rd, local_float);
|
2023-03-23 20:05:46 +01:00
|
|
|
},
|
2023-03-29 15:25:58 +02:00
|
|
|
_ => Err(format!("Unreachable in fp_fsgn_instruction! Instruction was {:?}", inst))?
|
|
|
|
}
|
2023-03-29 17:01:28 +02:00
|
|
|
Ok(MachineOk::Ok)
|
2023-03-29 15:25:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes RISC-V min / max instruction on floating point values on the machine.
|
2023-03-29 17:01:28 +02:00
|
|
|
fn fp_minmax_instruction(&mut self, inst: Instruction) -> Result<MachineOk, MachineError> {
|
2023-03-29 15:25:58 +02:00
|
|
|
let r1 = self.fp_reg.get_reg(inst.rs1);
|
|
|
|
let r2 = self.fp_reg.get_reg(inst.rs2);
|
|
|
|
match inst.funct3 {
|
|
|
|
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}),
|
|
|
|
_ => Err(format!("Unreachable in fp_minmax_instruction! Instruction was {:?}", inst))?
|
|
|
|
};
|
2023-03-29 17:01:28 +02:00
|
|
|
Ok(MachineOk::Ok)
|
2023-03-29 15:25:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes RISC-V floating-point to integer conversion instruction on the machine.
|
2023-03-29 17:01:28 +02:00
|
|
|
fn fp_fcvtw_instruction(&mut self, inst: Instruction) -> Result<MachineOk, MachineError> {
|
2023-03-29 15:25:58 +02:00
|
|
|
if inst.rs2 == RISCV_FP_FCVTW_W {
|
|
|
|
self.int_reg.set_reg(inst.rd, self.fp_reg.get_reg(inst.rs1) as i64)
|
|
|
|
} else {
|
|
|
|
self.int_reg.set_reg(inst.rd, (self.fp_reg.get_reg(inst.rs1) as u64) as i64)
|
|
|
|
}
|
2023-03-29 17:01:28 +02:00
|
|
|
Ok(MachineOk::Ok)
|
2023-03-29 15:25:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes RISC-V integer to floating-point conversion instruction on the machine.
|
2023-03-29 17:01:28 +02:00
|
|
|
fn fp_fcvts_instruction(&mut self, inst: Instruction) -> Result<MachineOk, MachineError> {
|
2023-03-29 15:25:58 +02:00
|
|
|
if inst.rs2 == RISCV_FP_FCVTS_W {
|
|
|
|
self.fp_reg.set_reg(inst.rd, self.int_reg.get_reg(inst.rs1) as f32);
|
|
|
|
} else {
|
|
|
|
self.fp_reg.set_reg(inst.rd, (self.int_reg.get_reg(inst.rs1) as u32) as f32);
|
|
|
|
}
|
2023-03-29 17:01:28 +02:00
|
|
|
Ok(MachineOk::Ok)
|
2023-03-29 15:25:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes RISC-V move from int_reg to fp_reg instruction on the machine.
|
2023-03-29 17:01:28 +02:00
|
|
|
fn fp_fmvw_instruction(&mut self, inst: Instruction) -> Result<MachineOk, MachineError> {
|
2023-03-29 15:25:58 +02:00
|
|
|
self.fp_reg.set_reg(inst.rd, self.int_reg.get_reg(inst.rs1) as f32);
|
2023-03-29 17:01:28 +02:00
|
|
|
Ok(MachineOk::Ok)
|
2023-03-29 15:25:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes RISC-V move from fp_reg to int_reg instruction on the machine.
|
2023-03-29 17:01:28 +02:00
|
|
|
fn fp_fmvxfclass_instruction(&mut self, inst: Instruction) -> Result<MachineOk, MachineError> {
|
2023-03-29 15:25:58 +02:00
|
|
|
if inst.funct3 == RISCV_FP_FMVXFCLASS_FMVX {
|
|
|
|
self.int_reg.set_reg(inst.rd, self.fp_reg.get_reg(inst.rs1) as i64);
|
2023-03-29 17:01:28 +02:00
|
|
|
Ok(MachineOk::Ok)
|
2023-03-29 15:25:58 +02:00
|
|
|
} else {
|
|
|
|
Err(format!("Unreachable in fp_fmvxfclass_instruction! Instruction was {:?}", inst))?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes RISC-V floating point values comparaison instructions on the machine.
|
2023-03-29 17:01:28 +02:00
|
|
|
fn fp_fcmp_instruction(&mut self, inst: Instruction) -> Result<MachineOk, MachineError> {
|
2023-03-29 15:25:58 +02:00
|
|
|
match inst.funct3 {
|
|
|
|
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),
|
|
|
|
_ => Err(format!("Unreachable in fp_fcmp_instruction match! Instruction was {:?}", inst))?
|
2023-03-23 20:05:46 +01:00
|
|
|
}
|
2023-03-29 17:01:28 +02:00
|
|
|
Ok(MachineOk::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
|
|
|
}
|
2023-03-29 16:13:57 +02:00
|
|
|
|
|
|
|
pub fn get_status(&self) -> MachineStatus {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_status(&mut self, new_status: MachineStatus) {
|
|
|
|
self.status = new_status;
|
|
|
|
}
|
2022-11-09 15:59:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2023-04-05 16:44:26 +02:00
|
|
|
|
2023-03-08 17:58:38 +01:00
|
|
|
mod test {
|
|
|
|
use std::fs;
|
|
|
|
|
|
|
|
use crate::simulator::{machine::Machine, mem_cmp};
|
2023-04-19 18:09:08 +02:00
|
|
|
use crate::utility::cfg::get_debug_configuration;
|
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) => {{
|
2023-04-19 18:09:08 +02:00
|
|
|
let mut m = Machine::new(true, get_debug_configuration());
|
2023-03-23 17:42:36 +01:00
|
|
|
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);
|
2023-04-13 02:05:21 +02:00
|
|
|
let mut system = crate::kernel::system::System::new(true);
|
2023-04-05 16:44:26 +02:00
|
|
|
m._run_debug(&mut system);
|
2023-03-23 17:42:36 +01:00
|
|
|
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() {
|
2023-04-19 18:09:08 +02:00
|
|
|
let _ = Machine::new(true, get_debug_configuration());
|
2023-03-11 23:49:20 +01:00
|
|
|
}
|
|
|
|
|
2023-01-18 17:01:48 +01:00
|
|
|
#[test]
|
|
|
|
fn test_read_memory() {
|
2023-04-19 18:09:08 +02:00
|
|
|
let mut m = Machine::new(true, get_debug_configuration());
|
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-04-19 18:09:08 +02:00
|
|
|
let mut m = Machine::new(true, get_debug_configuration());
|
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
|
|
|
}
|