1
0
Fork 0
forked from koniifer/ableos

bizare page unmapped error

This commit is contained in:
Able 2023-04-22 17:47:01 -05:00
parent 7d8b0bae78
commit 46eb4c17a0
4 changed files with 47 additions and 13 deletions

View file

@ -20,6 +20,13 @@ use self::call_stack::CallStack;
pub struct Page { pub struct Page {
pub data: [u8; 4096 * 2], pub data: [u8; 4096 * 2],
} }
impl Page {
pub fn new() -> Self {
Self {
data: [0; 4096 * 2],
}
}
}
pub type EnviromentCall = fn(Registers) -> Result<Registers, u64>; pub type EnviromentCall = fn(Registers) -> Result<Registers, u64>;
pub fn empty_enviroment_call(reg: Registers) -> Result<Registers, u64> { pub fn empty_enviroment_call(reg: Registers) -> Result<Registers, u64> {
@ -42,9 +49,9 @@ pub struct Engine {
} }
impl Engine { impl Engine {
pub fn read_mem_addr(&mut self, address: u64) -> u8 { pub fn read_mem_addr_8(&mut self, address: u64) -> Result<u8, RuntimeErrors> {
// println!("{}", address); // println!("{}", address);
255 self.memory.read_addr8(address)
} }
pub fn set_timer_callback(&mut self, func: fn() -> u32) { pub fn set_timer_callback(&mut self, func: fn() -> u32) {
self.timer_callback = Some(func); self.timer_callback = Some(func);
@ -53,6 +60,13 @@ impl Engine {
impl Engine { impl Engine {
pub fn new(program: Vec<u8>) -> Self { pub fn new(program: Vec<u8>) -> 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 { Self {
index: 0, index: 0,
program, program,
@ -321,7 +335,7 @@ F5-F9 {:016X} {:016X} {:016X} {:016X} {:016X}",
trace!("addr {}", addr); 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]; let reg = self.program[self.index + 10];
trace!("reg {}", reg); trace!("reg {}", reg);
self.set_register_8(reg, ret); self.set_register_8(reg, ret);

View file

@ -14,6 +14,7 @@ pub enum RuntimeErrors {
InvalidOpcode(u8), InvalidOpcode(u8),
RegisterTooSmall, RegisterTooSmall,
HostError(u64), HostError(u64),
PageUnmapped(u64),
} }
// If you solve the halting problem feel free to remove this // If you solve the halting problem feel free to remove this

View file

@ -7,7 +7,7 @@ use holey_bytes::{
fn main() -> Result<(), RuntimeErrors> { fn main() -> Result<(), RuntimeErrors> {
#[rustfmt::skip] #[rustfmt::skip]
let prog: Vec<u8> = vec![ let prog: Vec<u8> = vec![
// NOP as u8, NOP as u8, NOP as u8, NOP as u8,
// 10, 10, // 10, 10,
ADD as u8, EightBit as u8, 100, 20, 0xA7, ADD as u8, EightBit as u8, 100, 20, 0xA7,
@ -26,11 +26,11 @@ fn main() -> Result<(), RuntimeErrors> {
]; ];
let mut eng = Engine::new(prog); let mut eng = Engine::new(prog);
println!("{:?}", eng.read_mem_addr_8(4));
// eng.set_timer_callback(time); // eng.set_timer_callback(time);
eng.enviroment_call_table[10] = print_fn; // eng.enviroment_call_table[10] = print_fn;
eng.run()?; // eng.run()?;
eng.dump(); // eng.dump();
Ok(()) Ok(())
} }

View file

@ -21,16 +21,35 @@ impl Memory {
impl Memory { impl Memory {
pub fn read_addr8(&mut self, address: u64) -> Result<u8, RuntimeErrors> { pub fn read_addr8(&mut self, address: u64) -> Result<u8, RuntimeErrors> {
let (page, offset) = addr_to_page(address); let (page, offset) = addr_to_page(address);
let val: u8 = self.inner.get(&page).unwrap().data[offset as usize]; println!("page {} offset {}", page, offset);
trace!("Value read {} from page {} offset {}", val, page, offset); match self.inner.get(&page) {
Some(page) => {
let val = page.data[offset as usize];
Ok(val) 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 { pub fn read_addr64(&mut self, address: u64) -> u64 {
unimplemented!() unimplemented!()
} }
pub fn set_addr8(&self, address: u64, value: u8) -> Result<(), RuntimeErrors> { pub fn set_addr8(&mut self, address: u64, value: u8) -> Result<(), RuntimeErrors> {
unimplemented!() 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 { pub fn set_addr64(&mut self, address: u64, value: u64) -> u64 {
unimplemented!() unimplemented!()