some debugging stuff and SECOND OPCODE!

This commit is contained in:
2025-10-10 19:01:04 +02:00
parent bf5562df54
commit 6bd31e73fb
3 changed files with 32 additions and 5 deletions

View File

@@ -68,7 +68,7 @@ impl Core {
} }
} }
} else { } else {
eprintln!("Invalid Instruction"); eprintln!("Invalid Instruction 0x{:08x} 0b{:032b}", instr.0, instr.0);
break; break;
} }
} }

View File

@@ -3,4 +3,8 @@
pub(super) const OP_IMM: u8 = 0b00100; pub(super) const OP_IMM: u8 = 0b00100;
pub(super) const FUNCT3_ADDI: u8 = 0x0; pub(super) const FUNCT3_ADDI: u8 = 0b000;
pub(super) const STORE: u8 = 0b01000;
pub(super) const FUNCT3_SD: u8 = 0b011;

View File

@@ -1,22 +1,25 @@
use crate::{ use crate::{
consts::{Addr, DWord},
core::{Core, InstructionResult}, core::{Core, InstructionResult},
decode::Instruction, decode::Instruction,
instructions::{ instructions::{
OpcodeHandler, OpcodeHandler,
gen_tools::insert_funct3_splitter, gen_tools::insert_funct3_splitter,
opcodes::{FUNCT3_ADDI, OP_IMM}, opcodes::{FUNCT3_ADDI, FUNCT3_SD, OP_IMM, STORE},
}, },
mem::PageNum,
}; };
pub(super) fn add_instrs(list: &mut [OpcodeHandler; 32]) { pub(super) fn add_instrs(list: &mut [OpcodeHandler; 32]) {
let funct3_split_op_imm = insert_funct3_splitter(&mut list[OP_IMM as usize].splitter); let funct3_split_op_imm = insert_funct3_splitter(&mut list[OP_IMM as usize].splitter);
funct3_split_op_imm[FUNCT3_ADDI as usize].handler = funct3_split_op_imm[FUNCT3_ADDI as usize].handler =
Some(super::InstructionHandler { runner: addi }); Some(super::InstructionHandler { runner: addi });
let funct3_split_store = insert_funct3_splitter(&mut list[STORE as usize].splitter);
funct3_split_store[FUNCT3_SD as usize].handler = Some(super::InstructionHandler { runner: sd })
} }
fn addi(core: &mut Core, instr: Instruction) -> InstructionResult { fn addi(core: &mut Core, instr: Instruction) -> InstructionResult {
eprintln!("Running ADDI");
core.reg_write( core.reg_write(
instr.rd(), instr.rd(),
core.reg_read(instr.rs1()).wrapping_add(instr.imm_i()), core.reg_read(instr.rs1()).wrapping_add(instr.imm_i()),
@@ -26,3 +29,23 @@ fn addi(core: &mut Core, instr: Instruction) -> InstructionResult {
InstructionResult::Normal InstructionResult::Normal
} }
fn sd(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::<DWord>() as Addr) {
return InstructionResult::Exception(());
}
let page = (addr / 4096) as PageNum;
let offset = (addr & ((4096 / std::mem::size_of::<DWord>() as Addr) - 1)) as u16;
let value = core.reg_read(instr.rs2());
match core.mem.write_dword(page, offset, value) {
Ok(_) => {
core.pc = core.pc.wrapping_add(4);
InstructionResult::Normal
}
Err(_) => InstructionResult::Exception(()),
}
}