Compare commits

...

6 Commits

Author SHA1 Message Date
0ac363e203 Implement LW 2025-12-22 22:48:57 +01:00
7a22570a0f Improve the debug messages when invalid instructions are found (again) 2025-12-22 22:46:45 +01:00
2b5eb96187 Implement BLTU 2025-12-22 21:17:38 +01:00
be1b1b9fe6 Implement LH 2025-12-22 21:15:24 +01:00
5cbaf2dc66 Implement BGEU 2025-12-22 20:08:16 +01:00
ae57cdc691 Improve the debug messages when invalid instructions are found 2025-12-22 19:57:33 +01:00
4 changed files with 71 additions and 2 deletions

View File

@@ -53,6 +53,11 @@ impl Core {
}
};
if instr == 0 {
eprintln!("Executing 0 instruction at {:X}", self.pc);
break;
}
assert_eq!(instr & 3, 3, "Compressed instructions not supported");
let instr = Instruction(instr);
@@ -73,7 +78,7 @@ impl Core {
}
}
} else {
eprintln!("Invalid Instruction 0x{:08x} 0b{:032b}", instr.0, instr.0);
eprintln!("Invalid Instruction {:08x} at PC: {:x}", instr.0, self.pc);
break;
}
}

View File

@@ -44,6 +44,8 @@ pub(crate) fn find_and_exec(instr: Instruction, core: &mut Core) -> Option<Instr
// LOAD
0b000 => Some(rvi::lb(core, instr)),
0b100 => Some(rvi::lbu(core, instr)),
0b001 => Some(rvi::lh(core, instr)),
0b010 => Some(rvi::lw(core, instr)),
0b011 => Some(rvi::ld(core, instr)),
_ => None,
},
@@ -51,6 +53,8 @@ pub(crate) fn find_and_exec(instr: Instruction, core: &mut Core) -> Option<Instr
// BRANCH
0b000 => Some(rvi::beq(core, instr)),
0b001 => Some(rvi::bne(core, instr)),
0b110 => Some(rvi::bltu(core, instr)),
0b111 => Some(rvi::bgeu(core, instr)),
_ => None,
},
0b01101 => Some(rvi::lui(core, instr)),

View File

@@ -122,3 +122,23 @@ pub fn bne(core: &mut Core, instr: Instruction) -> InstructionResult {
InstructionResult::Normal
}
pub fn bgeu(core: &mut Core, instr: Instruction) -> InstructionResult {
if core.reg_read(instr.rs1()) >= core.reg_read(instr.rs2()) {
core.pc = core.pc.wrapping_add(instr.imm_b());
} else {
core.advance_pc();
}
InstructionResult::Normal
}
pub fn bltu(core: &mut Core, instr: Instruction) -> InstructionResult {
if core.reg_read(instr.rs1()) < core.reg_read(instr.rs2()) {
core.pc = core.pc.wrapping_add(instr.imm_b());
} else {
core.advance_pc();
}
InstructionResult::Normal
}

View File

@@ -1,5 +1,5 @@
use crate::{
consts::{Addr, Byte, DWord, Word},
consts::{Addr, Byte, DWord, HWord, Word},
core::Core,
instructions::{Instruction, InstructionResult},
mem::PageNum,
@@ -66,6 +66,46 @@ pub fn sw(core: &mut Core, instr: Instruction) -> InstructionResult {
}
}
pub fn lw(core: &mut Core, instr: Instruction) -> InstructionResult {
let addr = core.reg_read(instr.rs1()).wrapping_add(instr.imm_i());
if !addr.is_multiple_of(std::mem::size_of::<Word>() as Addr) {
return InstructionResult::Exception(());
}
let page = (addr / 4096) as PageNum;
let offset = (addr / 4 & ((4096 / 4 as Addr) - 1)) as u16;
match core.mem.read_word(page, offset) {
Ok(x) => {
core.reg_write(instr.rd(), x as i32 as i64 as DWord);
core.advance_pc();
InstructionResult::Normal
}
Err(_) => InstructionResult::Exception(()),
}
}
pub fn lh(core: &mut Core, instr: Instruction) -> InstructionResult {
let addr = core.reg_read(instr.rs1()).wrapping_add(instr.imm_i());
if !addr.is_multiple_of(std::mem::size_of::<HWord>() as Addr) {
return InstructionResult::Exception(());
}
let page = (addr / 4096) as PageNum;
let offset = (addr / 2 & ((4096 / 2 as Addr) - 1)) as u16;
match core.mem.read_hword(page, offset) {
Ok(x) => {
core.reg_write(instr.rd(), x as i16 as i64 as DWord);
core.advance_pc();
InstructionResult::Normal
}
Err(_) => InstructionResult::Exception(()),
}
}
pub fn sb(core: &mut Core, instr: Instruction) -> InstructionResult {
let addr = core.reg_read(instr.rs1()).wrapping_add(instr.imm_s());