From 655bf9eab77e7c8692af5418da5fbb022e12074e Mon Sep 17 00:00:00 2001 From: Samy Solhi Date: Mon, 3 Apr 2023 23:08:13 +0200 Subject: [PATCH] added tests for SC_SHUTDOWN and SC_WRITE (print) --- src/kernel/exception.rs | 56 +++++++++++++++++++++++++++++++++++++--- src/simulator/machine.rs | 4 +-- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/kernel/exception.rs b/src/kernel/exception.rs index 61b1544..24555fe 100644 --- a/src/kernel/exception.rs +++ b/src/kernel/exception.rs @@ -78,10 +78,7 @@ fn syscall(machine: &Machine) -> Result { // load buffer let mut buffer = "".to_string(); for i in 0..size { - match char::from_digit(machine.read_memory(1, (address + i) as usize) as u32, 2) { - Some(c) => buffer.push(c), - None => todo!() // Throw a proper error - } + buffer.push((machine.read_memory(1, (address + i) as usize)) as u8 as char); } if f as u8 == CONSOLE_OUTPUT { @@ -121,4 +118,55 @@ fn syscall(machine: &Machine) -> Result { SC_DEBUG => todo!(), _ => todo!() } +} + +#[cfg(test)] +mod test { + use crate::kernel::exception::{SC_SHUTDOWN, SC_WRITE}; + use crate::simulator::instruction::Instruction; + use crate::simulator::machine::Machine; + + #[test] + fn test_sc_shutdown() { + let mut machine = Machine::init_machine(); + machine.write_int_register(17, SC_SHUTDOWN as i64); // Set type to shutdown + let ecall = Instruction::new(0b000000000000_00000_000_00000_1110011); + + machine.write_memory(4, 0, 0b000000000000_00000_000_00000_1110011); // ecall + machine.write_memory(4, 4, 0b000000001010_00000_000_00001_0010011); // r1 <- 10 + + machine.run(); + // If the machine was stopped with no error, the shutdown worked + assert_ne!(machine.read_int_register(1), 10); // Check if the next instruction was executed + } + + // This test print HELLO in the console + #[test] + #[ignore] + fn test_sc_print() { + let mut machine = Machine::init_machine(); + + let address = machine.read_int_register(10); + // Write string 'HELLO' in memory + machine.write_memory(1, 4000, 72); + machine.write_memory(1, 4001, 69); + machine.write_memory(1, 4002, 76); + machine.write_memory(1, 4003, 76); + machine.write_memory(1, 4004, 79); + + + machine.write_int_register(10, 4000); // String address + machine.write_int_register(11, 5); // String size + machine.write_int_register(12, 1); // Console output + + machine.write_memory(4, 0, 0b000000000000_00000_000_00000_1110011); // ecall + machine.write_int_register(17, SC_WRITE as i64); // Set type to write + + machine.write_memory(4, 4, 0b000000000000_00000_000_10001_0010011); // r17 <- SC_SHUTDOWN + machine.write_memory(4, 8, 0b000000000000_00000_000_00000_1110011); // ecall + + + machine.run(); + } + } \ No newline at end of file diff --git a/src/simulator/machine.rs b/src/simulator/machine.rs index 9e4b4af..7862785 100644 --- a/src/simulator/machine.rs +++ b/src/simulator/machine.rs @@ -298,7 +298,7 @@ impl Machine { RISCV_JAL => { 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; - Ok((MachineOk::Ok)) + Ok(MachineOk::Ok) }, // Treatment for: JUMP AND LINK REGISTER INSTRUCTIONS (indirect jump) @@ -306,7 +306,7 @@ impl Machine { 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); - Ok((MachineOk::Ok)) + Ok(MachineOk::Ok) }, // Treatment for: BRANCH INSTRUCTIONS