EXCEPTION SYSTEM (initial version - may change later)
This commit is contained in:
50
src/core.rs
50
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user