From 4632fe29ceec9aaacdaa8bb7ff1f1a4091c52622 Mon Sep 17 00:00:00 2001 From: taitep Date: Tue, 30 Sep 2025 18:39:14 +0200 Subject: Initial instruction execution code i guess --- src/instructions.rs | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 2 files changed, 70 insertions(+) create mode 100644 src/instructions.rs (limited to 'src') diff --git a/src/instructions.rs b/src/instructions.rs new file mode 100644 index 0000000..9f3a544 --- /dev/null +++ b/src/instructions.rs @@ -0,0 +1,69 @@ +use once_cell::sync::Lazy; + +static INSTRUCTIONS: Lazy<[Option; 32]> = Lazy::new(|| { + let mut instructions = std::array::from_fn(|_i| None); + + instructions +}); + +use crate::{consts::Word, core::Core, decode::Instruction}; + +type Runner = fn(&mut Core, Instruction) -> Result<(), ()>; + +#[derive(Clone, Copy)] +struct InstructionHandler { + runner: Runner, +} + +struct OpcodeHandler { + handler: Option, + splitter: Option, +} + +pub fn find_runner(instruction: Instruction) -> Option { + let opcode_handler = &INSTRUCTIONS[instruction.opcode_noncompressed() as usize]; + match opcode_handler { + Some(h) => h.find_runner(instruction), + None => None, + } +} + +impl OpcodeHandler { + fn find_runner(&self, instruction: Instruction) -> Option { + if let Some(splitter) = &self.splitter { + splitter.find_runner(instruction) + } else { + self.handler.map(|h| h.runner) + } + } +} + +enum Splitter { + Funct3Splitter(Box<[Option; 8]>), + GeneralSplitter(Box<[GeneralSplitterEntry]>), +} + +impl Splitter { + fn find_runner(&self, instruction: Instruction) -> Option { + match self { + Splitter::Funct3Splitter(f3s) => f3s[instruction.funct3() as usize] + .as_ref() + .and_then(|h| h.find_runner(instruction)), + Splitter::GeneralSplitter(entries) => { + for entry in entries { + if instruction.0 & entry.mask == entry.value { + return Some(entry.handler.runner); + } + } + + None + } + } + } +} + +struct GeneralSplitterEntry { + mask: Word, + value: Word, + handler: InstructionHandler, +} diff --git a/src/lib.rs b/src/lib.rs index 4020af3..3cc8c0b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ mod consts; pub mod core; mod decode; +mod instructions; pub mod mem; -- cgit v1.2.3