diff options
| author | taitep <taitep@taitep.se> | 2025-12-24 13:56:41 +0100 |
|---|---|---|
| committer | taitep <taitep@taitep.se> | 2025-12-24 13:56:41 +0100 |
| commit | 09d90643726d2a86952473e27b6e5d64543e41a0 (patch) | |
| tree | 52d0ebf404675e1ae359dd77af406131a0623111 /src/core.rs | |
| parent | 3f789442c0be7d0222209d98dde21efcff7602d0 (diff) | |
EXCEPTION SYSTEM (initial version - may change later)
Diffstat (limited to 'src/core.rs')
| -rw-r--r-- | src/core.rs | 50 |
1 files changed, 19 insertions, 31 deletions
diff --git a/src/core.rs b/src/core.rs index 8fe5520..7e477c1 100644 --- a/src/core.rs +++ b/src/core.rs @@ -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; } |
