From ce08ab6d06a88a7d75fe6aff4bfed83fe6bf269a Mon Sep 17 00:00:00 2001 From: Benjamin Kyd Date: Tue, 4 Jul 2023 00:11:55 +0100 Subject: [PATCH] Instruction decoding is almost done but there's a few issues --- src/ext/decode.rs | 82 ++++++++++++++++++++++++++++++++++------------- src/main.rs | 3 ++ 2 files changed, 63 insertions(+), 22 deletions(-) diff --git a/src/ext/decode.rs b/src/ext/decode.rs index c71e661..96c39fa 100644 --- a/src/ext/decode.rs +++ b/src/ext/decode.rs @@ -74,13 +74,13 @@ pub union GenInstruction { } trait Instruction { - fn register(&self, ext: &mut Extension) where Self: Sized { - } fn match_inst(&self, inst: rv32::Word) -> bool; fn decode(&self); fn step(&self, inst: rv32::Word, state: &mut cpu::CPU); } +type SomeInstruction = impl Instruction; + #[derive(Copy, Clone)] struct ADDI; impl ADDI { @@ -88,31 +88,41 @@ impl ADDI { ADDI } } + impl Instruction for ADDI { fn match_inst(&self, inst: rv32::Word) -> bool { match_mask!(inst, "xxxxxxxxxxxxxxxxxx000xxxx0010011") } fn decode(&self) { - } fn step(&self, inst: rv32::Word, state: &mut cpu::CPU) { } } +#[derive(Copy, Clone)] +struct ADD; +impl ADD { + fn new() -> ADD { + ADD + } +} -trait GenExtension { - fn register(&mut self, inst: Box) { +impl Instruction for ADD { + fn match_inst(&self, inst: rv32::Word) -> bool { + match_mask!(inst, "0000000xxxxxxxxxxx000xxxx0110011") + } + + fn decode(&self) { + } + + fn step(&self, inst: rv32::Word, state: &mut cpu::CPU) { } } struct Extension { - instruction_set: Vec>, -} - -impl GenExtension for Extension { - + instruction_set: Vec, } impl Extension { @@ -121,20 +131,48 @@ impl Extension { instruction_set: Vec::new(), } } -} -struct ExtensionI; -impl GenExtension for ExtensionI { - -} - -pub fn load_extensions(ext: Vec) { - for extension in ext { + pub fn register_inst(&mut self, inst: SomeInstruction) { + self.instruction_set.push(inst); + } + pub fn match_inst(&self, inst: rv32::Word) -> Option { + match self.instruction_set.iter().find(|instruction| instruction.match_inst(inst)) { + Some(instruction) => Some(instruction), + None => None + } } } -// pub fn decode_inst(inst: rv32::Word) -> fn() { -// // we need to go over every instruction and see if it matches -// // we can do smarter things with cacheing later - this aint blazin -// } +pub struct DecodeCycle { + extensions: Vec, +} + +impl DecodeCycle { + pub fn new(&mut self, ext: Vec) { + for extension in ext { + match extension { + 'i' => { + let ext = Extension::new(); + // let instructions = vec![ADDI, ADD]; + // self.extensions.push(); + } + _ => { + println!("VM > Unknown Extension '{}'", extension); + } + } + } + } + + pub fn decode_inst(&self, inst: rv32::Word) -> Option { + // we need to go over every instruction and see if it matches + // we can do smarter things with cacheing later - this aint blazin + for extension in &self.extensions { + match extension.match_inst(inst) { + Some(inst) => return Some(inst), + None => continue, + }; + }; + None + } +} diff --git a/src/main.rs b/src/main.rs index fa6ca77..92fc027 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +#![feature(type_alias_impl_trait)] +#![feature(return_position_impl_trait_in_trait)] + use std::fs::File; use std::io::BufReader; use std::io::Read;