holey-bytes/hbvm/src/engine/mod.rs

101 lines
2.4 KiB
Rust
Raw Normal View History

2023-04-18 23:08:30 +00:00
pub mod call_stack;
pub mod config;
2023-05-06 12:33:40 +00:00
pub mod enviroment_calls;
2023-04-18 23:08:30 +00:00
pub mod regs;
2023-05-22 14:01:13 +00:00
#[cfg(test)]
pub mod tests;
2023-04-18 23:08:30 +00:00
2023-05-06 12:33:40 +00:00
use {
self::call_stack::CallStack,
crate::{memory, HaltStatus, RuntimeErrors},
2023-05-06 12:33:40 +00:00
alloc::vec::Vec,
config::EngineConfig,
log::trace,
regs::Registers,
};
// pub const PAGE_SIZE: usize = 8192;
pub struct RealPage {
pub ptr: *mut u8,
}
2023-04-22 22:17:49 +00:00
#[derive(Debug, Clone, Copy)]
pub struct VMPage {
2023-05-06 12:33:40 +00:00
pub data: [u8; 8192],
2023-04-22 18:00:19 +00:00
}
impl VMPage {
2023-04-22 22:47:01 +00:00
pub fn new() -> Self {
Self {
data: [0; 4096 * 2],
}
}
}
2023-04-22 21:06:33 +00:00
pub enum Page {
VMPage(VMPage),
RealPage(RealPage),
}
impl Page {
pub fn data(&self) -> [u8; 4096 * 2] {
match self {
Page::VMPage(vmpage) => vmpage.data,
Page::RealPage(_) => {
unimplemented!("Memmapped hw page not yet supported")
}
}
}
}
2023-05-06 12:33:40 +00:00
pub fn empty_enviroment_call(engine: &mut Engine) -> Result<&mut Engine, u64> {
trace!("Registers {:?}", engine.registers);
2023-04-22 22:17:49 +00:00
Err(0)
2023-04-22 21:06:33 +00:00
}
2023-04-22 18:00:19 +00:00
2023-04-18 23:08:30 +00:00
pub struct Engine {
pub index: usize,
pub program: Vec<u8>,
pub registers: Registers,
pub config: EngineConfig,
2023-04-18 23:08:30 +00:00
/// BUG: This DOES NOT account for overflowing
pub last_timer_count: u32,
pub timer_callback: Option<fn() -> u32>,
pub memory: memory::Memory,
2023-05-22 14:01:13 +00:00
pub enviroment_call_table: [Option<EnviromentCall>; 256],
pub call_stack: CallStack,
2023-04-18 23:08:30 +00:00
}
2023-05-06 12:33:40 +00:00
use crate::engine::enviroment_calls::EnviromentCall;
2023-04-22 18:00:19 +00:00
impl Engine {
2023-04-22 21:06:33 +00:00
pub fn set_timer_callback(&mut self, func: fn() -> u32) {
self.timer_callback = Some(func);
}
pub fn set_register(&mut self, register: u8, value: u64) {}
2023-04-22 18:00:19 +00:00
}
2023-04-18 23:08:30 +00:00
impl Engine {
pub fn new(program: Vec<u8>) -> Self {
2023-04-22 22:47:01 +00:00
let mut mem = memory::Memory::new();
for (addr, byte) in program.clone().into_iter().enumerate() {
2023-05-06 12:33:40 +00:00
let _ = mem.set_addr8(addr as u64, byte);
2023-04-22 22:47:01 +00:00
}
2023-05-06 12:33:40 +00:00
trace!("{:?}", mem.read_addr8(0));
2023-05-22 14:01:13 +00:00
let ecall_table: [Option<EnviromentCall>; 256] = [None; 256];
2023-04-18 23:08:30 +00:00
Self {
index: 0,
program,
registers: Registers::new(),
config: EngineConfig::default(),
last_timer_count: 0,
timer_callback: None,
2023-05-22 14:01:13 +00:00
enviroment_call_table: ecall_table,
2023-05-06 12:33:40 +00:00
memory: mem,
2023-04-22 21:06:33 +00:00
call_stack: Vec::new(),
2023-04-18 23:08:30 +00:00
}
}
2023-04-22 21:06:33 +00:00
pub fn dump(&self) {}
2023-04-18 23:08:30 +00:00
pub fn run(&mut self) -> Result<HaltStatus, RuntimeErrors> {
Ok(HaltStatus::Halted)
2023-04-19 02:41:27 +00:00
}
2023-04-18 23:08:30 +00:00
}