Instruction decoding is almost done but there's a few issues

This commit is contained in:
Benjamin Kyd
2023-07-04 00:11:55 +01:00
parent 2720022e11
commit ce08ab6d06
2 changed files with 63 additions and 22 deletions

View File

@@ -74,13 +74,13 @@ pub union GenInstruction {
} }
trait Instruction { trait Instruction {
fn register(&self, ext: &mut Extension) where Self: Sized {
}
fn match_inst(&self, inst: rv32::Word) -> bool; fn match_inst(&self, inst: rv32::Word) -> bool;
fn decode(&self); fn decode(&self);
fn step(&self, inst: rv32::Word, state: &mut cpu::CPU); fn step(&self, inst: rv32::Word, state: &mut cpu::CPU);
} }
type SomeInstruction = impl Instruction;
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
struct ADDI; struct ADDI;
impl ADDI { impl ADDI {
@@ -88,31 +88,41 @@ impl ADDI {
ADDI ADDI
} }
} }
impl Instruction for ADDI { impl Instruction for ADDI {
fn match_inst(&self, inst: rv32::Word) -> bool { fn match_inst(&self, inst: rv32::Word) -> bool {
match_mask!(inst, "xxxxxxxxxxxxxxxxxx000xxxx0010011") match_mask!(inst, "xxxxxxxxxxxxxxxxxx000xxxx0010011")
} }
fn decode(&self) { fn decode(&self) {
} }
fn step(&self, inst: rv32::Word, state: &mut cpu::CPU) { fn step(&self, inst: rv32::Word, state: &mut cpu::CPU) {
} }
} }
#[derive(Copy, Clone)]
struct ADD;
impl ADD {
fn new() -> ADD {
ADD
}
}
trait GenExtension { impl Instruction for ADD {
fn register(&mut self, inst: Box<dyn Instruction>) { 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 { struct Extension {
instruction_set: Vec<Box<dyn Instruction>>, instruction_set: Vec<SomeInstruction>,
}
impl GenExtension for Extension {
} }
impl Extension { impl Extension {
@@ -121,20 +131,48 @@ impl Extension {
instruction_set: Vec::new(), instruction_set: Vec::new(),
} }
} }
}
struct ExtensionI; pub fn register_inst(&mut self, inst: SomeInstruction) {
impl GenExtension for ExtensionI { self.instruction_set.push(inst);
}
}
pub fn load_extensions(ext: Vec<char>) {
for extension in ext {
pub fn match_inst(&self, inst: rv32::Word) -> Option<SomeInstruction> {
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() { pub struct DecodeCycle {
// // we need to go over every instruction and see if it matches extensions: Vec<Extension>,
// // we can do smarter things with cacheing later - this aint blazin }
// }
impl DecodeCycle {
pub fn new(&mut self, ext: Vec<char>) {
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<SomeInstruction> {
// 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
}
}

View File

@@ -1,3 +1,6 @@
#![feature(type_alias_impl_trait)]
#![feature(return_position_impl_trait_in_trait)]
use std::fs::File; use std::fs::File;
use std::io::BufReader; use std::io::BufReader;
use std::io::Read; use std::io::Read;