From 46eb4c17a002e975dc48cf480ab89a832162b71d Mon Sep 17 00:00:00 2001 From: Able Date: Sat, 22 Apr 2023 17:47:01 -0500 Subject: [PATCH] bizare page unmapped error --- src/engine/mod.rs | 20 +++++++++++++++++--- src/lib.rs | 1 + src/main.rs | 10 +++++----- src/memory.rs | 29 ++++++++++++++++++++++++----- 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/engine/mod.rs b/src/engine/mod.rs index e2f631a..27e2c0a 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -20,6 +20,13 @@ use self::call_stack::CallStack; pub struct Page { pub data: [u8; 4096 * 2], } +impl Page { + pub fn new() -> Self { + Self { + data: [0; 4096 * 2], + } + } +} pub type EnviromentCall = fn(Registers) -> Result; pub fn empty_enviroment_call(reg: Registers) -> Result { @@ -42,9 +49,9 @@ pub struct Engine { } impl Engine { - pub fn read_mem_addr(&mut self, address: u64) -> u8 { + pub fn read_mem_addr_8(&mut self, address: u64) -> Result { // println!("{}", address); - 255 + self.memory.read_addr8(address) } pub fn set_timer_callback(&mut self, func: fn() -> u32) { self.timer_callback = Some(func); @@ -53,6 +60,13 @@ impl Engine { impl Engine { pub fn new(program: Vec) -> Self { + let mut mem = memory::Memory::new(); + for (addr, byte) in program.clone().into_iter().enumerate() { + let ret = mem.set_addr8(addr as u64, byte); + println!("{:?}", ret); + } + println!("{:?}", mem.read_addr8(0)); + Self { index: 0, program, @@ -321,7 +335,7 @@ F5-F9 {:016X} {:016X} {:016X} {:016X} {:016X}", trace!("addr {}", addr); - let ret = self.read_mem_addr(addr); + let ret = self.read_mem_addr_8(addr)?; let reg = self.program[self.index + 10]; trace!("reg {}", reg); self.set_register_8(reg, ret); diff --git a/src/lib.rs b/src/lib.rs index 2dcb84e..8926402 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,7 @@ pub enum RuntimeErrors { InvalidOpcode(u8), RegisterTooSmall, HostError(u64), + PageUnmapped(u64), } // If you solve the halting problem feel free to remove this diff --git a/src/main.rs b/src/main.rs index ec894c9..d063aba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use holey_bytes::{ fn main() -> Result<(), RuntimeErrors> { #[rustfmt::skip] let prog: Vec = vec![ - // NOP as u8, NOP as u8, + NOP as u8, NOP as u8, // 10, 10, ADD as u8, EightBit as u8, 100, 20, 0xA7, @@ -26,11 +26,11 @@ fn main() -> Result<(), RuntimeErrors> { ]; let mut eng = Engine::new(prog); - + println!("{:?}", eng.read_mem_addr_8(4)); // eng.set_timer_callback(time); - eng.enviroment_call_table[10] = print_fn; - eng.run()?; - eng.dump(); + // eng.enviroment_call_table[10] = print_fn; + // eng.run()?; + // eng.dump(); Ok(()) } diff --git a/src/memory.rs b/src/memory.rs index 05422e1..bcb5a58 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -21,16 +21,35 @@ impl Memory { impl Memory { pub fn read_addr8(&mut self, address: u64) -> Result { let (page, offset) = addr_to_page(address); - let val: u8 = self.inner.get(&page).unwrap().data[offset as usize]; - trace!("Value read {} from page {} offset {}", val, page, offset); - Ok(val) + println!("page {} offset {}", page, offset); + match self.inner.get(&page) { + Some(page) => { + let val = page.data[offset as usize]; + Ok(val) + } + None => Err(RuntimeErrors::PageUnmapped(page)), + } + // trace!("Value read {} from page {} offset {}", val, page, offset); } pub fn read_addr64(&mut self, address: u64) -> u64 { unimplemented!() } - pub fn set_addr8(&self, address: u64, value: u8) -> Result<(), RuntimeErrors> { - unimplemented!() + pub fn set_addr8(&mut self, address: u64, value: u8) -> Result<(), RuntimeErrors> { + let (page, offset) = addr_to_page(address); + let ret: Option<(&u64, &mut Page)> = self.inner.get_key_value_mut(&page); + match ret { + Some((_, page)) => { + page.data[offset as usize] = value; + } + None => { + let mut pg = Page::new(); + pg.data[offset as usize] = value; + self.inner.insert(page, pg); + println!("Mapped page {}", page); + } + } + Ok(()) } pub fn set_addr64(&mut self, address: u64, value: u64) -> u64 { unimplemented!()