From 49a45d62c6f7b7cfe96fe70c25f026587bd8e0c0 Mon Sep 17 00:00:00 2001 From: able Date: Mon, 22 May 2023 22:47:29 -0500 Subject: [PATCH] add in test cases --- hbvm/src/engine/mod.rs | 23 ++++++++++++++----- hbvm/src/engine/tests/mod.rs | 44 ++++++++++++++++++++++++++++++++++-- hbvm/src/lib.rs | 1 + hbvm/src/main.rs | 32 +++++++++++++------------- 4 files changed, 76 insertions(+), 24 deletions(-) diff --git a/hbvm/src/engine/mod.rs b/hbvm/src/engine/mod.rs index e126928..09865ce 100644 --- a/hbvm/src/engine/mod.rs +++ b/hbvm/src/engine/mod.rs @@ -444,15 +444,26 @@ F5-F9 {:016X} {:016X} {:016X} {:016X} {:016X}", (255, int) => { trace!("Enviroment Call {}", int); - let ret = self.enviroment_call_table[int as usize](self); - match ret { - Ok(eng) => { - trace!("Resuming execution at {}", eng.index); + let ecall = self.enviroment_call_table[int as usize]; + + match ecall { + Some(call) => { + let ret = call(self); + + match ret { + Ok(eng) => { + trace!("Resuming execution at {}", eng.index); + } + Err(err) => { + return Err(HostError(err)); + } + } } - Err(err) => { - return Err(HostError(err)); + None => { + return Err(InvalidSystemCall(int)); } } + self.index += 2; } diff --git a/hbvm/src/engine/tests/mod.rs b/hbvm/src/engine/tests/mod.rs index 6e9aaf2..ff5c6c1 100644 --- a/hbvm/src/engine/tests/mod.rs +++ b/hbvm/src/engine/tests/mod.rs @@ -2,6 +2,7 @@ use { super::Engine, crate::{HaltStatus, RuntimeErrors}, alloc::vec, + RuntimeErrors::*, }; #[test] @@ -30,7 +31,7 @@ fn jump_out_of_bounds() { let prog = vec![JUMP as u8, 0, 0, 0, 0, 0, 0, 1, 0]; let mut eng = Engine::new(prog); let ret = eng.run(); - assert_eq!(ret, Err(RuntimeErrors::InvalidJumpAddress(256))); + assert_eq!(ret, Err(InvalidJumpAddress(256))); } #[test] @@ -38,5 +39,44 @@ fn invalid_system_call() { let prog = vec![255, 0]; let mut eng = Engine::new(prog); let ret = eng.run(); - // assert_eq!(ret, ); + assert_eq!(ret, Err(InvalidSystemCall(0))); +} + +#[test] +fn add_u8() { + use crate::engine::{Operations::ADD, SubTypes::EightBit}; + + let prog = vec![ADD as u8, EightBit as u8, 1, 1, 0xA0]; + let mut eng = Engine::new(prog); + let _ = eng.run(); + assert_eq!(eng.registers.a0, 2); +} + +#[test] +fn sub_u8() { + use crate::engine::{Operations::SUB, SubTypes::EightBit}; + + let prog = vec![SUB as u8, EightBit as u8, 2, 1, 0xA0]; + let mut eng = Engine::new(prog); + let _ = eng.run(); + assert_eq!(eng.registers.a0, 1); +} +#[test] +fn mul_u8() { + use crate::engine::{Operations::MUL, SubTypes::EightBit}; + + let prog = vec![MUL as u8, EightBit as u8, 1, 1, 0xA0]; + let mut eng = Engine::new(prog); + let _ = eng.run(); + assert_eq!(eng.registers.a0, 2); +} + +#[test] +fn div_u8() { + use crate::engine::{Operations::DIV, SubTypes::EightBit}; + + let prog = vec![DIV as u8, EightBit as u8, 1, 1, 0xA0]; + let mut eng = Engine::new(prog); + let _ = eng.run(); + assert_eq!(eng.registers.a0, 2); } diff --git a/hbvm/src/lib.rs b/hbvm/src/lib.rs index 705c25c..ebed013 100644 --- a/hbvm/src/lib.rs +++ b/hbvm/src/lib.rs @@ -12,6 +12,7 @@ pub enum RuntimeErrors { HostError(u64), PageNotMapped(u64), InvalidJumpAddress(u64), + InvalidSystemCall(u8), } // If you solve the halting problem feel free to remove this diff --git a/hbvm/src/main.rs b/hbvm/src/main.rs index 3c38426..583383a 100644 --- a/hbvm/src/main.rs +++ b/hbvm/src/main.rs @@ -7,28 +7,28 @@ use hbvm::{ fn main() -> Result<(), RuntimeErrors> { #[rustfmt::skip] let prog: Vec = vec![ - NOP as u8, NOP as u8, - 255, 10, + // NOP as u8, NOP as u8, + // 255, 10, ADD as u8, EightBit as u8, 100, 20, 0xA7, - ADD as u8, - EightBit as u8, 1, 0, 0xB0, - ADD as u8, - SixtyFourBit as u8, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 2, 0xD0, - SUB as u8, EightBit as u8, 255, 0, 0xA7, - DIV as u8, EightBit as u8, 12, 5, 0xA8, - ADD as u8, Register8 as u8, 0xA7, 0xB0, 0xA7, - LOAD as u8, AddrToReg as u8, - 0, 0, 0, 0, 0, 0, 0, 2, - 0xA0, - JUMP as u8, 0, 0, 0, 0, 0, 0, 0, 0, + // ADD as u8, + // EightBit as u8, 1, 0, 0xB0, + // ADD as u8, + // SixtyFourBit as u8, + // 0, 0, 0, 0, 0, 0, 0, 0, + // 0, 0, 0, 0, 0, 0, 0, 2, 0xD0, + // SUB as u8, EightBit as u8, 255, 0, 0xA7, + // DIV as u8, EightBit as u8, 12, 5, 0xA8, + // ADD as u8, Register8 as u8, 0xA7, 0xB0, 0xA7, + // LOAD as u8, AddrToReg as u8, + // 0, 0, 0, 0, 0, 0, 0, 2, + // 0xA0, + // JUMP as u8, 0, 0, 0, 0, 0, 0, 0, 0, ]; let mut eng = Engine::new(prog); // eng.set_timer_callback(time); - eng.enviroment_call_table[10] = print_fn; + eng.enviroment_call_table[10] = Some(print_fn); eng.run()?; eng.dump(); println!("{:#?}", eng.registers);