EXCEPTION SYSTEM (initial version - may change later)

This commit is contained in:
2025-12-24 13:56:41 +01:00
parent 3f789442c0
commit 09d9064372
11 changed files with 338 additions and 332 deletions

View File

@@ -7,19 +7,11 @@
use crate::{
consts::{Addr, RegId, RegValue},
decode::Instruction,
exceptions::ExceptionType,
instructions::find_and_exec,
mem::MemConfig,
};
// placeholder - change when exception system is in place
pub(crate) type Exception = ();
pub(crate) enum InstructionResult {
Normal,
Exception(Exception),
// Pause,
}
pub struct Core {
pub(crate) x_regs: [RegValue; 32],
pub(crate) pc: Addr,
@@ -40,50 +32,46 @@ impl Core {
let page = (self.pc / 4096) as usize;
let offset = (self.pc % 4096 / 4) as u16;
if !self.pc.is_multiple_of(4) {
//replace eprint with logging, replace break with exception
eprintln!("PC not aligned");
self.throw_exception(ExceptionType::InstructionAccessMisaligned);
break;
}
let instr = match self.mem.read_word(page, offset) {
Ok(i) => i,
Err(_) => {
eprintln!("Memory access fault while fetching instruction");
eprintln!("PC: {:x}", self.pc);
self.throw_exception(ExceptionType::InstructionAccessFault);
break;
}
};
if instr == 0 {
eprintln!("Executing 0 instruction at {:x}", self.pc);
self.throw_exception(ExceptionType::IllegalInstruction);
break;
}
assert_eq!(instr & 3, 3, "Compressed instructions not supported");
if instr & 3 != 3 {
self.throw_exception(ExceptionType::IllegalInstruction);
break;
}
let instr = Instruction(instr);
let res = find_and_exec(instr, self);
if let Some(res) = res {
match res {
InstructionResult::Normal => {}
InstructionResult::Exception(_e) => {
eprintln!("Exception from instruction");
eprintln!("PC: {:016x}, instr: {:08x}", self.pc, instr.0);
break;
} // InstructionResult::Pause => {
// eprintln!("Instruction asked for pause");
// break;
// }
match find_and_exec(instr, self) {
Ok(()) => {}
Err(e) => {
self.throw_exception(e);
eprintln!("instr: {:08x}", instr.0);
break;
}
} else {
eprintln!("Invalid Instruction {:08x} at PC: {:x}", instr.0, self.pc);
break;
}
}
}
fn throw_exception(&mut self, exception_type: ExceptionType) {
eprintln!("Exception: {exception_type:?}");
dbg!(self.pc, self.x_regs);
}
pub fn reset(&mut self, pc: Addr) {
self.pc = pc;
}