Compare commits

..

3 Commits

Author SHA1 Message Date
43bae12ea0 Comment out the unused 'Pause' instruction result 2025-12-23 18:46:38 +01:00
0c6a540a85 Implement SRLI 2025-12-23 18:42:50 +01:00
23392a55df Implement SH 2025-12-23 18:31:04 +01:00
4 changed files with 37 additions and 6 deletions

View File

@@ -17,7 +17,7 @@ pub(crate) type Exception = ();
pub(crate) enum InstructionResult { pub(crate) enum InstructionResult {
Normal, Normal,
Exception(Exception), Exception(Exception),
Pause, // Pause,
} }
pub struct Core { pub struct Core {
@@ -72,11 +72,10 @@ impl Core {
eprintln!("Exception from instruction"); eprintln!("Exception from instruction");
eprintln!("PC: {:016x}, instr: {:08x}", self.pc, instr.0); eprintln!("PC: {:016x}, instr: {:08x}", self.pc, instr.0);
break; break;
} } // InstructionResult::Pause => {
InstructionResult::Pause => { // eprintln!("Instruction asked for pause");
eprintln!("Instruction asked for pause"); // break;
break; // }
}
} }
} else { } else {
eprintln!("Invalid Instruction {:08x} at PC: {:x}", instr.0, self.pc); eprintln!("Invalid Instruction {:08x} at PC: {:x}", instr.0, self.pc);

View File

@@ -25,6 +25,11 @@ pub(crate) fn find_and_exec(instr: Instruction, core: &mut Core) -> Option<Instr
// OP_IMM // OP_IMM
0b000 => Some(rvi::addi(core, instr)), 0b000 => Some(rvi::addi(core, instr)),
0b001 => (instr.funct6() == 0).then(|| rvi::slli(core, instr)), 0b001 => (instr.funct6() == 0).then(|| rvi::slli(core, instr)),
0b101 => match instr.funct6() {
// immediate right-shift
0b000000 => Some(rvi::srli(core, instr)),
_ => None,
},
0b111 => Some(rvi::andi(core, instr)), 0b111 => Some(rvi::andi(core, instr)),
_ => None, _ => None,
}, },
@@ -36,6 +41,7 @@ pub(crate) fn find_and_exec(instr: Instruction, core: &mut Core) -> Option<Instr
0b01000 => match instr.funct3() { 0b01000 => match instr.funct3() {
// STORE // STORE
0b000 => Some(rvi::sb(core, instr)), 0b000 => Some(rvi::sb(core, instr)),
0b001 => Some(rvi::sh(core, instr)),
0b010 => Some(rvi::sw(core, instr)), 0b010 => Some(rvi::sw(core, instr)),
0b011 => Some(rvi::sd(core, instr)), 0b011 => Some(rvi::sd(core, instr)),
_ => None, _ => None,

View File

@@ -79,6 +79,12 @@ pub fn slli(core: &mut Core, instr: Instruction) -> InstructionResult {
InstructionResult::Normal InstructionResult::Normal
} }
pub fn srli(core: &mut Core, instr: Instruction) -> InstructionResult {
core.reg_write(instr.rd(), core.reg_read(instr.rs1()) >> instr.imm_shamt());
core.advance_pc();
InstructionResult::Normal
}
pub fn lui(core: &mut Core, instr: Instruction) -> InstructionResult { pub fn lui(core: &mut Core, instr: Instruction) -> InstructionResult {
core.reg_write(instr.rd(), instr.imm_u()); core.reg_write(instr.rd(), instr.imm_u());
core.advance_pc(); core.advance_pc();

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 { pub fn lh(core: &mut Core, instr: Instruction) -> InstructionResult {
let addr = core.reg_read(instr.rs1()).wrapping_add(instr.imm_i()); let addr = core.reg_read(instr.rs1()).wrapping_add(instr.imm_i());