Skip to content

Commit

Permalink
Merge pull request #51 from dxrcy/implicit-halt
Browse files Browse the repository at this point in the history
Automatically insert `HALT` at end of bytecode, before running
  • Loading branch information
rozukke authored Nov 28, 2024
2 parents a272e48 + ba1fa03 commit 7ba70b9
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use core::panic;
use std::{
cmp::Ordering,
i16,
Expand All @@ -11,6 +10,16 @@ use colored::Colorize;
use console::Term;
use miette::Result;

macro_rules! exception {
( $fmt:literal $($tt:tt)* ) => {{
eprintln!(
concat!("exception: ", $fmt, ", exiting")
$($tt)*
);
std::process::exit(0xEE);
}};
}

/// LC3 can address 128KB of memory.
const MEMORY_MAX: usize = 0x10000;

Expand Down Expand Up @@ -53,13 +62,16 @@ impl RunState {
pub fn from_raw(raw: &[u16]) -> Result<RunState> {
let orig = raw[0] as usize;
if orig as usize + raw.len() > MEMORY_MAX {
panic!("Assembly file is too long and cannot fit in memory.");
exception!("assembly file is too long and cannot fit in memory");
}

let mut mem = [0; MEMORY_MAX];
let raw = &raw[1..];

mem[orig..orig + raw.len()].clone_from_slice(&raw);
// Add `HALT` at end of code and data
// Prevents PC running through no-ops to the end of memory
mem[orig + raw.len()] = 0xF025;

Ok(RunState {
mem: Box::new(mem),
Expand Down Expand Up @@ -92,9 +104,11 @@ impl RunState {
/// Run with preset memory
pub fn run(&mut self) {
loop {
if self.pc == u16::MAX {
break; // Halt was triggered
}
if self.pc >= 0xFE00 {
// Entering device address space
break;
exception!("entered protected memory area >= 0xFE00");
}
let instr = self.mem[self.pc as usize];
let opcode = (instr >> 12) as usize;
Expand Down Expand Up @@ -371,7 +385,10 @@ impl RunState {
println!("-----------------------");
}
// unknown
_ => panic!("You called a trap with an unknown vector of {}", trap_vect),
_ => exception!(
"called a trap with an unknown vector of 0x{:02x}",
trap_vect
),
}
}
}
Expand All @@ -398,7 +415,7 @@ mod test {
fn expect(input: u16, bits: u32, expected: u16) {
let actual = RunState::s_ext(input, bits);
if actual != expected {
std::panic!(
panic!(
"\ns_ext(0x{input:04x}, {bits})\n Expected: 0x{expected:04x}\n Actual: 0x{actual:04x}\n"
);
}
Expand Down

0 comments on commit 7ba70b9

Please sign in to comment.