From 63b2dc7514cf435490e1f8210c88f1b0f0a77459 Mon Sep 17 00:00:00 2001 From: MunirG05 Date: Tue, 11 Jul 2023 14:03:25 +0530 Subject: [PATCH] tried to shove the timer back in --- hbasm/src/macros.rs | 1 - hbvm/src/vm/mem/mod.rs | 12 +++++++----- hbvm/src/vm/mem/paging.rs | 19 ++++++++++--------- hbvm/src/vm/mod.rs | 19 +++++++++++++++++-- 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/hbasm/src/macros.rs b/hbasm/src/macros.rs index d041ab20..0a3c93b6 100644 --- a/hbasm/src/macros.rs +++ b/hbasm/src/macros.rs @@ -66,7 +66,6 @@ macro_rules! impl_asm { }; } - pub(super) use {impl_asm, impl_asm_opcodes}; #[allow(clippy::single_component_path_imports)] diff --git a/hbvm/src/vm/mem/mod.rs b/hbvm/src/vm/mem/mod.rs index dd92875b..65f58001 100644 --- a/hbvm/src/vm/mem/mod.rs +++ b/hbvm/src/vm/mem/mod.rs @@ -2,11 +2,13 @@ pub mod paging; -use self::paging::{PageTable, Permission, PtEntry}; -use super::{trap::HandleTrap, VmRunError}; -use alloc::boxed::Box; -use core::mem::MaybeUninit; -use derive_more::Display; +use { + self::paging::{PageTable, Permission, PtEntry}, + super::{trap::HandleTrap, VmRunError}, + alloc::boxed::Box, + core::mem::MaybeUninit, + derive_more::Display, +}; /// HoleyBytes virtual memory #[derive(Clone, Debug)] diff --git a/hbvm/src/vm/mem/paging.rs b/hbvm/src/vm/mem/paging.rs index b1979ebb..c621ab4b 100644 --- a/hbvm/src/vm/mem/paging.rs +++ b/hbvm/src/vm/mem/paging.rs @@ -1,12 +1,14 @@ //! Page table and associated structures implementation -use core::{ - fmt::Debug, - mem::MaybeUninit, - ops::{Index, IndexMut}, - slice::SliceIndex, +use { + core::{ + fmt::Debug, + mem::MaybeUninit, + ops::{Index, IndexMut}, + slice::SliceIndex, + }, + delegate::delegate, }; -use delegate::delegate; /// Page entry permission #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] @@ -30,7 +32,7 @@ pub enum Permission { pub struct PtEntry(u64); impl PtEntry { /// Create new - /// + /// /// # Safety /// - `ptr` has to point to valid data and shall not be deallocated /// troughout the entry lifetime @@ -61,7 +63,6 @@ impl Debug for PtEntry { } } - /// Page table #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[repr(align(4096))] @@ -145,7 +146,7 @@ impl Default for PageTable { #[repr(C, align(4096))] pub union PtPointedData { /// Node - next page table - pub pt: PageTable, + pub pt: PageTable, /// Leaf pub page: u8, } diff --git a/hbvm/src/vm/mod.rs b/hbvm/src/vm/mod.rs index 0f6fa2f0..8b975bff 100644 --- a/hbvm/src/vm/mod.rs +++ b/hbvm/src/vm/mod.rs @@ -97,6 +97,10 @@ pub struct Vm<'a, T> { /// Program program: &'a [u8], + + pub last_tick_count: u32, + pub tick_callback: Option u32>, + pub tick_limit: u32, } impl<'a, T: HandleTrap> Vm<'a, T> { @@ -111,6 +115,9 @@ impl<'a, T: HandleTrap> Vm<'a, T> { traph, pc: 0, program, + last_tick_count: 0, + tick_callback: None, + tick_limit: 32, } } @@ -123,12 +130,12 @@ 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(2) }; // Big match unsafe { @@ -318,7 +325,15 @@ impl<'a, T: HandleTrap> Vm<'a, T> { } } } + if self.tick_callback.is_some() { + let ret = self.tick_callback.unwrap()(); + if (ret - self.last_tick_count) >= self.tick_limit { + return Ok(0); + } + } + return Ok(1); } + } /// Read register