From 227a5246bd26e2944133abab23147cf86eb333ad Mon Sep 17 00:00:00 2001 From: darcy Date: Thu, 28 Nov 2024 12:38:53 +1100 Subject: [PATCH] feat: automatically insert `HALT` before running bytecode --- src/runtime.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/runtime.rs b/src/runtime.rs index 80b4adb..9d8842d 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -60,6 +60,9 @@ impl RunState { 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), @@ -92,9 +95,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; + panic!("Program counter entered device address space"); } let instr = self.mem[self.pc as usize]; let opcode = (instr >> 12) as usize;