table
This commit is contained in:
parent
f53a42977d
commit
60ca26dcd2
|
@ -1,3 +1,5 @@
|
||||||
|
use hbvm::vm::trap::HandleTrap;
|
||||||
|
|
||||||
use {
|
use {
|
||||||
hbvm::{validate::validate, vm::Vm},
|
hbvm::{validate::validate, vm::Vm},
|
||||||
std::io::{stdin, Read},
|
std::io::{stdin, Read},
|
||||||
|
@ -6,13 +8,13 @@ use {
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let mut prog = vec![];
|
let mut prog = vec![];
|
||||||
stdin().read_to_end(&mut prog)?;
|
stdin().read_to_end(&mut prog)?;
|
||||||
|
|
||||||
if let Err(e) = validate(&prog) {
|
if let Err(e) = validate(&prog) {
|
||||||
eprintln!("Program validation error: {e:?}");
|
eprintln!("Program validation error: {e:?}");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
} else {
|
} else {
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut vm = Vm::new_unchecked(&prog);
|
let mut vm = Vm::new_unchecked(&prog, TestTrapHandler);
|
||||||
vm.memory.insert_test_page();
|
vm.memory.insert_test_page();
|
||||||
println!("Program interrupt: {:?}", vm.run());
|
println!("Program interrupt: {:?}", vm.run());
|
||||||
println!("{:?}", vm.registers);
|
println!("{:?}", vm.registers);
|
||||||
|
@ -24,3 +26,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
pub fn time() -> u32 {
|
pub fn time() -> u32 {
|
||||||
9
|
9
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct TestTrapHandler;
|
||||||
|
impl HandleTrap for TestTrapHandler {
|
||||||
|
#[inline]
|
||||||
|
fn page_fault(&mut self) {}
|
||||||
|
}
|
||||||
|
|
|
@ -11,6 +11,10 @@
|
||||||
// program size. If you are (rightfully) worried about the UB, for now just
|
// program size. If you are (rightfully) worried about the UB, for now just
|
||||||
// append your program with 11 zeroes.
|
// append your program with 11 zeroes.
|
||||||
|
|
||||||
|
use self::trap::HandleTrap;
|
||||||
|
|
||||||
|
pub mod trap;
|
||||||
|
|
||||||
mod mem;
|
mod mem;
|
||||||
mod value;
|
mod value;
|
||||||
|
|
||||||
|
@ -72,28 +76,30 @@ macro_rules! cond_jump {
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Vm<'a> {
|
pub struct Vm<'a, T> {
|
||||||
pub registers: [Value; 256],
|
pub registers: [Value; 256],
|
||||||
pub memory: Memory,
|
pub memory: Memory,
|
||||||
|
pub traph: T,
|
||||||
pc: usize,
|
pc: usize,
|
||||||
program: &'a [u8],
|
program: &'a [u8],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Vm<'a> {
|
impl<'a, T: HandleTrap> Vm<'a, T> {
|
||||||
/// # Safety
|
/// # Safety
|
||||||
/// Program code has to be validated
|
/// Program code has to be validated
|
||||||
pub unsafe fn new_unchecked(program: &'a [u8]) -> Self {
|
pub unsafe fn new_unchecked(program: &'a [u8], traph: T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
registers: [Value::from(0_u64); 256],
|
registers: [Value::from(0_u64); 256],
|
||||||
memory: Default::default(),
|
memory: Default::default(),
|
||||||
|
traph,
|
||||||
pc: 0,
|
pc: 0,
|
||||||
program,
|
program,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_validated(program: &'a [u8]) -> Result<Self, validate::Error> {
|
pub fn new_validated(program: &'a [u8], traph: T) -> Result<Self, validate::Error> {
|
||||||
validate::validate(program)?;
|
validate::validate(program)?;
|
||||||
Ok(unsafe { Self::new_unchecked(program) })
|
Ok(unsafe { Self::new_unchecked(program, traph) })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(&mut self) -> HaltReason {
|
pub fn run(&mut self) -> HaltReason {
|
||||||
|
|
3
hbvm/src/vm/trap.rs
Normal file
3
hbvm/src/vm/trap.rs
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
pub trait HandleTrap {
|
||||||
|
fn page_fault(&mut self) {}
|
||||||
|
}
|
Loading…
Reference in a new issue