From 15c3ce1f69da36146ff3a10de797eb48b42ab786 Mon Sep 17 00:00:00 2001 From: Able Date: Tue, 9 May 2023 03:36:37 -0500 Subject: [PATCH] rework architecture and add a "compiler" --- Cargo.lock | 4 + Cargo.toml | 12 +-- compiler/Cargo.toml | 8 ++ compiler/src/main.rs | 5 + hbvm/Cargo.toml | 10 ++ {src => hbvm/src}/bytecode/mod.rs | 0 {src => hbvm/src}/bytecode/ops.rs | 7 ++ {src => hbvm/src}/bytecode/types.rs | 0 {src => hbvm/src}/engine/call_stack.rs | 0 {src => hbvm/src}/engine/config.rs | 0 {src => hbvm/src}/engine/enviroment_calls.rs | 0 {src => hbvm/src}/engine/mod.rs | 99 ++++++++++++++++---- {src => hbvm/src}/engine/regs.rs | 0 {src => hbvm/src}/lib.rs | 13 +-- {src => hbvm/src}/main.rs | 1 + {src => hbvm/src}/memory.rs | 26 +++-- 16 files changed, 134 insertions(+), 51 deletions(-) create mode 100644 compiler/Cargo.toml create mode 100644 compiler/src/main.rs create mode 100644 hbvm/Cargo.toml rename {src => hbvm/src}/bytecode/mod.rs (100%) rename {src => hbvm/src}/bytecode/ops.rs (85%) rename {src => hbvm/src}/bytecode/types.rs (100%) rename {src => hbvm/src}/engine/call_stack.rs (100%) rename {src => hbvm/src}/engine/config.rs (100%) rename {src => hbvm/src}/engine/enviroment_calls.rs (100%) rename {src => hbvm/src}/engine/mod.rs (87%) rename {src => hbvm/src}/engine/regs.rs (100%) rename {src => hbvm/src}/lib.rs (60%) rename {src => hbvm/src}/main.rs (96%) rename {src => hbvm/src}/memory.rs (70%) diff --git a/Cargo.lock b/Cargo.lock index eadfaafe..e685f790 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,6 +19,10 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "compiler" +version = "0.1.0" + [[package]] name = "hashbrown" version = "0.13.2" diff --git a/Cargo.toml b/Cargo.toml index 485cfc9a..2d921c2b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,2 @@ -[package] -name = "holey_bytes" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -log = "*" -hashbrown = "0.13.2" +[workspace] +members = ["hbvm", "compiler"] diff --git a/compiler/Cargo.toml b/compiler/Cargo.toml new file mode 100644 index 00000000..41b7a452 --- /dev/null +++ b/compiler/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "compiler" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/compiler/src/main.rs b/compiler/src/main.rs new file mode 100644 index 00000000..8f14d179 --- /dev/null +++ b/compiler/src/main.rs @@ -0,0 +1,5 @@ +fn main() { + let prog = "load 1, A0 + jump 0"; + println!("Hello, world!"); +} diff --git a/hbvm/Cargo.toml b/hbvm/Cargo.toml new file mode 100644 index 00000000..485cfc9a --- /dev/null +++ b/hbvm/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "holey_bytes" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +log = "*" +hashbrown = "0.13.2" diff --git a/src/bytecode/mod.rs b/hbvm/src/bytecode/mod.rs similarity index 100% rename from src/bytecode/mod.rs rename to hbvm/src/bytecode/mod.rs diff --git a/src/bytecode/ops.rs b/hbvm/src/bytecode/ops.rs similarity index 85% rename from src/bytecode/ops.rs rename to hbvm/src/bytecode/ops.rs index f4fd524f..f82c868e 100644 --- a/src/bytecode/ops.rs +++ b/hbvm/src/bytecode/ops.rs @@ -30,6 +30,13 @@ pub enum Operations { EnviromentCall = 255, } +pub enum PageMapTypes { + // Have the host make a new VMPage + VMPage = 0, + // Ask the host to map a RealPage into memory + RealPage = 1, +} + pub enum SubTypes { EightBit = 1, SixtyFourBit = 2, diff --git a/src/bytecode/types.rs b/hbvm/src/bytecode/types.rs similarity index 100% rename from src/bytecode/types.rs rename to hbvm/src/bytecode/types.rs diff --git a/src/engine/call_stack.rs b/hbvm/src/engine/call_stack.rs similarity index 100% rename from src/engine/call_stack.rs rename to hbvm/src/engine/call_stack.rs diff --git a/src/engine/config.rs b/hbvm/src/engine/config.rs similarity index 100% rename from src/engine/config.rs rename to hbvm/src/engine/config.rs diff --git a/src/engine/enviroment_calls.rs b/hbvm/src/engine/enviroment_calls.rs similarity index 100% rename from src/engine/enviroment_calls.rs rename to hbvm/src/engine/enviroment_calls.rs diff --git a/src/engine/mod.rs b/hbvm/src/engine/mod.rs similarity index 87% rename from src/engine/mod.rs rename to hbvm/src/engine/mod.rs index 3a6b3f3d..23a11bfd 100644 --- a/src/engine/mod.rs +++ b/hbvm/src/engine/mod.rs @@ -3,27 +3,32 @@ pub mod config; pub mod enviroment_calls; pub mod regs; -use crate::bytecode::{ - ops::{Operations::*, *}, - types::*, -}; - use { - crate::{engine::call_stack::FnCall, memory, HaltStatus, RuntimeErrors}, + self::call_stack::CallStack, + crate::{ + bytecode::{ + ops::{Operations::*, *}, + types::*, + }, + engine::call_stack::FnCall, + memory, HaltStatus, RuntimeErrors, + }, alloc::vec::Vec, config::EngineConfig, log::trace, regs::Registers, }; - -use self::call_stack::CallStack; // pub const PAGE_SIZE: usize = 8192; +pub struct RealPage { + pub ptr: *mut u8, +} + #[derive(Debug, Clone, Copy)] -pub struct Page { +pub struct VMPage { pub data: [u8; 8192], } -impl Page { +impl VMPage { pub fn new() -> Self { Self { data: [0; 4096 * 2], @@ -31,23 +36,38 @@ impl Page { } } +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") + } + } + } +} + pub fn empty_enviroment_call(engine: &mut Engine) -> Result<&mut Engine, u64> { trace!("Registers {:?}", engine.registers); Err(0) } pub struct Engine { - pub index: usize, - program: Vec, - registers: Registers, - config: EngineConfig, + pub index: usize, + pub program: Vec, + pub registers: Registers, + pub config: EngineConfig, /// BUG: This DOES NOT account for overflowing - last_timer_count: u32, - timer_callback: Option u32>, - memory: memory::Memory, + pub last_timer_count: u32, + pub timer_callback: Option u32>, + pub memory: memory::Memory, pub enviroment_call_table: [EnviromentCall; 256], - call_stack: CallStack, + pub call_stack: CallStack, } use crate::engine::enviroment_calls::EnviromentCall; impl Engine { @@ -319,8 +339,49 @@ F5-F9 {:016X} {:016X} {:016X} {:016X} {:016X}", // TODO: Implement 64 bit register to register subtraction (2, 4) => { // 64 bit - self.index += 19; + + let mut lhs_array = [0; 8]; + let mut rhs_array = [0; 8]; + + for (index, byte) in self.program[self.index + 2..self.index + 10] + .into_iter() + .enumerate() + { + lhs_array[index] = *byte; + } + let lhs = u64::from_be_bytes(lhs_array); + + for (index, byte) in self.program[self.index + 10..self.index + 18] + .into_iter() + .enumerate() + { + rhs_array[index] = *byte; + } + + let rhs = u64::from_be_bytes(rhs_array); + + // println!("RHS 64BIT {}", rhs); + + let ret = lhs - rhs; + + let reg = self.program[self.index + 18]; + // println!("Store {} in {:02X}", ret, reg); + + match reg { + 0xA0..=0xC9 => { + panic!("Register undersized") + } + 0xD0..=0xF9 => { + self.set_register_64(reg, ret); + } + _ => { + panic!("Not a register.") + } + } + + self.index += 18; } + // Read from address to register (5, 0) => { let mut addr_array = [0; 8]; diff --git a/src/engine/regs.rs b/hbvm/src/engine/regs.rs similarity index 100% rename from src/engine/regs.rs rename to hbvm/src/engine/regs.rs diff --git a/src/lib.rs b/hbvm/src/lib.rs similarity index 60% rename from src/lib.rs rename to hbvm/src/lib.rs index 71969c74..1b042b02 100644 --- a/src/lib.rs +++ b/hbvm/src/lib.rs @@ -1,14 +1,10 @@ -// #![no_std] +#![no_std] extern crate alloc; pub mod bytecode; pub mod engine; pub mod memory; -use bytecode::ops::*; -use bytecode::types::{CONST_F64, CONST_U8}; -use engine::Engine; - #[derive(Debug)] pub enum RuntimeErrors { InvalidOpcode(u8), @@ -22,10 +18,3 @@ pub enum HaltStatus { Halted, Running, } - -pub struct HandSide { - signed: bool, - float: bool, - num8: Option, - num64: Option, -} diff --git a/src/main.rs b/hbvm/src/main.rs similarity index 96% rename from src/main.rs rename to hbvm/src/main.rs index 32e7534a..e84a17ee 100644 --- a/src/main.rs +++ b/hbvm/src/main.rs @@ -31,6 +31,7 @@ fn main() -> Result<(), RuntimeErrors> { eng.enviroment_call_table[10] = print_fn; eng.run()?; eng.dump(); + println!("{:#?}", eng.registers); Ok(()) } diff --git a/src/memory.rs b/hbvm/src/memory.rs similarity index 70% rename from src/memory.rs rename to hbvm/src/memory.rs index 1b97c4a5..4477c1ea 100644 --- a/src/memory.rs +++ b/hbvm/src/memory.rs @@ -1,10 +1,13 @@ -use hashbrown::HashMap; -use log::trace; +use crate::engine::VMPage; -use crate::{engine::Page, RuntimeErrors}; +use { + crate::{engine::Page, RuntimeErrors}, + alloc::vec::Vec, + hashbrown::HashMap, + log::trace, +}; pub struct Memory { - //TODO: hashmap with the start bytes as key and end bytes as offset inner: HashMap, } @@ -13,9 +16,12 @@ impl Memory { Self { inner: HashMap::new(), } + // } - pub fn map_vec(&mut self, address: u64, vec: Vec) {} + pub fn map_vec(&mut self, address: u64, vec: Vec) { + panic!("Mapping vectors into pages is not supported yet"); + } } impl Memory { @@ -24,7 +30,7 @@ impl Memory { trace!("page {} offset {}", page, offset); match self.inner.get(&page) { Some(page) => { - let val = page.data[offset as usize]; + let val = page.data()[offset as usize]; trace!("Value {}", val); Ok(val) } @@ -43,13 +49,13 @@ impl Memory { let ret: Option<(&u64, &mut Page)> = self.inner.get_key_value_mut(&page); match ret { Some((_, page)) => { - page.data[offset as usize] = value; + page.data()[offset as usize] = value; } None => { - let mut pg = Page::new(); + let mut pg = VMPage::new(); pg.data[offset as usize] = value; - self.inner.insert(page, pg); - println!("Mapped page {}", page); + self.inner.insert(page, Page::VMPage(pg)); + trace!("Mapped page {}", page); } } Ok(())