Add execution to VM via file, starting to iron out bugs

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-02-09 15:43:07 -05:00
parent 00027c4542
commit c982be553f
5 changed files with 93 additions and 16 deletions

View File

@@ -6,6 +6,15 @@ macro_rules! instructions {
$(
pub const $variant: InstOp = $value;
)*
pub fn inst_name(op: InstOp) -> Option<&'static str> {
match op {
$(
$value => Some(stringify!($variant)),
)*
_ => None,
}
}
};
}
pub type InstOp = u16;

View File

@@ -1,4 +1,5 @@
use crate::vm::{error::*, flags::Flags, inst::*, reg::*, vm::*};
use std::io::stdin;
impl Vm {
pub fn tick(&mut self) -> Result<()> {
@@ -120,6 +121,12 @@ impl Vm {
_ => panic!("unknown instruction opcode: 0x{:04x}", op),
}
println!("op: {:04x} {}", op, inst_name(op).unwrap());
println!("ip: {:05x}", self.ip());
println!("next_ip: {:05x}", next_ip);
let mut _line = String::new();
stdin().read_line(&mut _line).unwrap();
self.set_reg(IP, next_ip);
Ok(())

View File

@@ -1,4 +1,4 @@
use crate::vm::{error::*, flags::*, mem::*, reg::*};
use crate::vm::{error::*, flags::*, mem::*, obj::obj::*, reg::*};
use byteorder::{WriteBytesExt, LE};
use std::{io::Cursor, mem};
@@ -13,12 +13,67 @@ pub struct Vm {
}
impl Vm {
pub fn new(mem: Vec<u8>, registers: Registers) -> Self {
Vm { mem, registers }
pub fn new() -> Self {
Vm {
mem: Default::default(),
registers: [0; 64],
}
}
/// Loads an object into this VM, clearing out all previous memory and resetting the registers.
pub fn load_object(&mut self, object: Object, max_mem: usize) -> Result<()> {
self.registers = [0; 64];
// determine memory spread
let mem_size = object
.sections
.iter()
.filter_map(|s| match s {
Section::Data { end, .. } | Section::Code { end, .. } => Some(*end),
Section::Meta { .. } => None,
})
.max()
.unwrap_or(0);
if mem_size > (max_mem as u64) {
todo!("raise max memory error");
}
self.mem = vec![0; mem_size as usize];
let mut entry = 0;
// write sections to memory
for section in object.sections.into_iter() {
match section {
Section::Data {
start,
contents,
..
}
| Section::Code {
start,
contents,
..
} => {
let start = start as usize;
for (value, dest) in contents.into_iter().zip(&mut self.mem[start..])
{
*dest = value;
}
}
Section::Meta { entries } => {
if let Some(e) = entries.get("entry") {
// set the entry point
entry = *e;
}
}
}
}
self.set_reg(IP, entry);
Ok(())
}
pub fn mem_cursor(&self, at: usize) -> MemCursor {
MemCursor::new(&self.mem[at..])
let mut cursor = MemCursor::new(&self.mem);
cursor.set_position(at as u64);
cursor
}
pub fn run(&mut self) -> Result<u64> {