Implement SW

This commit is contained in:
2025-12-22 19:44:37 +01:00
parent cb100e92ac
commit 8cce960b29
2 changed files with 22 additions and 1 deletions

View File

@@ -5,7 +5,7 @@
// See LICENSE file in the project root for full license text.
use crate::{
consts::{Addr, DWord},
consts::{Addr, DWord, Word},
core::{Core, InstructionResult},
decode::Instruction,
mem::PageNum,
@@ -118,6 +118,26 @@ pub fn ld(core: &mut Core, instr: Instruction) -> InstructionResult {
}
}
pub fn sw(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::<Word>() as Addr) {
return InstructionResult::Exception(());
}
let page = (addr / 4096) as PageNum;
let offset = (addr / 4 & ((4096 / 4 as Addr) - 1)) as u16;
let value = core.reg_read(instr.rs2()) as Word;
match core.mem.write_word(page, offset, value) {
Ok(_) => {
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());