Skip to content

Commit

Permalink
feat: Add LBU, LHU, LWU
Browse files Browse the repository at this point in the history
  • Loading branch information
howjmay committed Feb 21, 2024
1 parent be76276 commit 82971be
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl MEMORY {
self.mem[index] = (value & (std::u8::MAX as u32)) as u8;
self.mem[index + 1] = ((value >> 8) & (std::u8::MAX as u32)) as u8;
self.mem[index + 2] = ((value >> 16) & (std::u8::MAX as u32)) as u8;
self.mem[index + 3] = ((value >> 24) & (std::u8::MAX as u32)) as u8;
}
// fn store64(&mut self, addr: u32, value: u32) {
// let index = (addr - MEM_BASE) as usize;
Expand Down
34 changes: 29 additions & 5 deletions tests/cpu_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ mod tests {
fn test_exec_lw() {
let mut cpu_test = cpu::CPU::new();
let offset = 3;
let val = 11;
let val = (-2 as i32) as u32;
let rd = 5 + MEM_BASE;
cpu_test.bus.store(rd + offset, 32, val);
// set x1=5+MEM_BASE
Expand All @@ -247,20 +247,44 @@ mod tests {
fn test_exec_lbu() {
let mut cpu_test = cpu::CPU::new();
let offset = 3;
let val = 11;
let val = (-2 as i32) as u32;
let rd = 5 + MEM_BASE;
cpu_test.bus.store(rd + offset, 8, val);
// set x1=5+MEM_BASE
helper::set_register_val(&mut cpu_test, 1, rd as i32);
// lbu x31, x1, 3
let instr: u32 = helper::set_load_type_instruction(offset as i16, 1, LBU as u8, 31);
cpu::exec_lbu(&mut cpu_test, instr);
assert_eq!(cpu_test.xregs.regs[31], val);
assert_eq!(cpu_test.xregs.regs[31], val & std::u8::MAX as u32);
}
#[test]
fn test_exec_lhu() {}
fn test_exec_lhu() {
let mut cpu_test = cpu::CPU::new();
let offset = 3;
let val = (-2 as i32) as u32;
let rd = 5 + MEM_BASE;
cpu_test.bus.store(rd + offset, 16, val);
// set x1=5+MEM_BASE
helper::set_register_val(&mut cpu_test, 1, rd as i32);
// lhu x31, x1, 3
let instr: u32 = helper::set_load_type_instruction(offset as i16, 1, LHU as u8, 31);
cpu::exec_lhu(&mut cpu_test, instr);
assert_eq!(cpu_test.xregs.regs[31], val & std::u16::MAX as u32);
}
#[test]
fn test_exec_lwu() {}
fn test_exec_lwu() {
let mut cpu_test = cpu::CPU::new();
let offset = 3;
let val = (-2 as i32) as u32;
let rd = 5 + MEM_BASE;
cpu_test.bus.store(rd + offset, 32, val);
// set x1=5+MEM_BASE
helper::set_register_val(&mut cpu_test, 1, rd as i32);
// lwu x31, x1, 3
let instr: u32 = helper::set_load_type_instruction(offset as i16, 1, LWU as u8, 31);
cpu::exec_lwu(&mut cpu_test, instr);
assert_eq!(cpu_test.xregs.regs[31], val & std::u32::MAX);
}
#[test]
fn test_exec_sb() {}
#[test]
Expand Down

0 comments on commit 82971be

Please sign in to comment.