Implement SH

This commit is contained in:
2025-12-23 18:31:04 +01:00
parent f38114dbd7
commit 23392a55df
2 changed files with 21 additions and 0 deletions

View File

@@ -36,6 +36,7 @@ pub(crate) fn find_and_exec(instr: Instruction, core: &mut Core) -> Option<Instr
0b01000 => match instr.funct3() {
// STORE
0b000 => Some(rvi::sb(core, instr)),
0b001 => Some(rvi::sh(core, instr)),
0b010 => Some(rvi::sw(core, instr)),
0b011 => Some(rvi::sd(core, instr)),
_ => None,

View File

@@ -86,6 +86,26 @@ pub fn lw(core: &mut Core, instr: Instruction) -> InstructionResult {
}
}
pub fn sh(core: &mut Core, instr: Instruction) -> InstructionResult {
let addr = core.reg_read(instr.rs1()).wrapping_add(instr.imm_s());
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;
let value = core.reg_read(instr.rs2()) as HWord;
match core.mem.write_hword(page, offset, value) {
Ok(_) => {
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());