From 81f79dc7a55cce306a88643940c2d6da74c07e37 Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 11 Jul 2023 10:29:23 +0200 Subject: [PATCH 1/4] Implement timer --- hbvm/src/main.rs | 2 +- hbvm/src/vm/mod.rs | 26 ++++++++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/hbvm/src/main.rs b/hbvm/src/main.rs index 255384e8..5f9b27f0 100644 --- a/hbvm/src/main.rs +++ b/hbvm/src/main.rs @@ -18,7 +18,7 @@ fn main() -> Result<(), Box> { return Ok(()); } else { unsafe { - let mut vm = Vm::new_unchecked(&prog, TestTrapHandler); + let mut vm = Vm::<_, 0>::new_unchecked(&prog, TestTrapHandler); vm.memory.insert_test_page(); println!("Program interrupt: {:?}", vm.run()); println!("{:?}", vm.registers); diff --git a/hbvm/src/vm/mod.rs b/hbvm/src/vm/mod.rs index 0f6fa2f0..65281611 100644 --- a/hbvm/src/vm/mod.rs +++ b/hbvm/src/vm/mod.rs @@ -79,7 +79,7 @@ macro_rules! cond_jump { } /// HoleyBytes Virtual Machine -pub struct Vm<'a, T> { +pub struct Vm<'a, T, const TIMER_QUOTIENT: usize> { /// Holds 256 registers /// /// Writing to register 0 is considered undefined behaviour @@ -97,9 +97,12 @@ pub struct Vm<'a, T> { /// Program program: &'a [u8], + + /// Program timer + timer: usize, } -impl<'a, T: HandleTrap> Vm<'a, T> { +impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> { /// Create a new VM with program and trap handler /// /// # Safety @@ -111,6 +114,7 @@ impl<'a, T: HandleTrap> Vm<'a, T> { traph, pc: 0, program, + timer: 0, } } @@ -123,12 +127,19 @@ impl<'a, T: HandleTrap> Vm<'a, T> { /// Execute program /// /// Program can return [`VmRunError`] if a trap handling failed - pub fn run(&mut self) -> Result<(), VmRunError> { + pub fn run(&mut self) -> Result { use hbbytecode::opcode::*; loop { // Fetch instruction let Some(&opcode) = self.program.get(self.pc) - else { return Ok(()) }; + else { return Ok(VmRunOk::End) }; + + if TIMER_QUOTIENT != 0 { + self.timer += 1; + if self.timer % TIMER_QUOTIENT == 0 { + return Ok(VmRunOk::Timer); + } + } // Big match unsafe { @@ -350,3 +361,10 @@ pub enum VmRunError { /// Unhandled store access exception StoreAccessEx(u64), } + +/// Virtual machine halt ok +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub enum VmRunOk { + End, + Timer, +} From e32f0d1e6102d2cf5e5199ca98f65b73bbc7015e Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 11 Jul 2023 10:31:03 +0200 Subject: [PATCH 2/4] wrap around timer --- hbvm/src/vm/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hbvm/src/vm/mod.rs b/hbvm/src/vm/mod.rs index 65281611..8505ff1f 100644 --- a/hbvm/src/vm/mod.rs +++ b/hbvm/src/vm/mod.rs @@ -135,7 +135,7 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> { else { return Ok(VmRunOk::End) }; if TIMER_QUOTIENT != 0 { - self.timer += 1; + self.timer = self.timer.wrapping_add(1); if self.timer % TIMER_QUOTIENT == 0 { return Ok(VmRunOk::Timer); } From 0351a954d065c2310582fc33f32394efd7a59ab3 Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 11 Jul 2023 10:32:26 +0200 Subject: [PATCH 3/4] Moved --- hbvm/src/vm/mod.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/hbvm/src/vm/mod.rs b/hbvm/src/vm/mod.rs index 8505ff1f..4997c5a6 100644 --- a/hbvm/src/vm/mod.rs +++ b/hbvm/src/vm/mod.rs @@ -134,13 +134,6 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> { let Some(&opcode) = self.program.get(self.pc) else { return Ok(VmRunOk::End) }; - if TIMER_QUOTIENT != 0 { - self.timer = self.timer.wrapping_add(1); - if self.timer % TIMER_QUOTIENT == 0 { - return Ok(VmRunOk::Timer); - } - } - // Big match unsafe { match opcode { @@ -329,6 +322,13 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> { } } } + + if TIMER_QUOTIENT != 0 { + self.timer = self.timer.wrapping_add(1); + if self.timer % TIMER_QUOTIENT == 0 { + return Ok(VmRunOk::Timer); + } + } } } From b218aa4a00e3d26db63a14ac7dd61b834fa2f653 Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 11 Jul 2023 10:33:55 +0200 Subject: [PATCH 4/4] doc --- hbvm/src/vm/mod.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hbvm/src/vm/mod.rs b/hbvm/src/vm/mod.rs index 4997c5a6..182b574d 100644 --- a/hbvm/src/vm/mod.rs +++ b/hbvm/src/vm/mod.rs @@ -365,6 +365,9 @@ pub enum VmRunError { /// Virtual machine halt ok #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum VmRunOk { + /// Program has eached its end End, + + /// Program was interrupted by a timer Timer, }