Skip to content

Commit

Permalink
feat: Add JALR
Browse files Browse the repository at this point in the history
  • Loading branch information
howjmay committed Feb 13, 2024
1 parent 570ea7a commit 4ad2bc4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ pub fn exec_jal(cpu: &mut CPU, instr: u32) {
cpu.pc = (cpu.pc as i32 + imm) as u32;
}
pub fn exec_jalr(cpu: &mut CPU, instr: u32) {
let imm = imm_j(instr) as i32;
let imm = imm_i(instr) as i32;
cpu.xregs.regs[rd(instr) as usize] = cpu.pc + 4;
cpu.pc = (cpu.pc as i32 + imm) as u32;
// ignore the last 1 bit with 0xfffffffe
cpu.pc = (cpu.xregs.regs[rs1(instr) as usize] as i32).wrapping_add(imm) as u32 & 0xfffffffe;
}
pub fn exec_beq(cpu: &mut CPU, instr: u32) {}
pub fn exec_bne(cpu: &mut CPU, instr: u32) {}
Expand Down
23 changes: 18 additions & 5 deletions tests/cpu_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ mod tests {
fn test_exec_lui() {
let mut cpu_test = cpu::CPU::new();

// lui x5, 4
// lui x5, (4<<12)
let instr: u32 = helper::set_u_type_instruction(4 << 12, 5, LUI as u8);
cpu::exec_lui(&mut cpu_test, instr);
assert_eq!(cpu_test.xregs.regs[5], 4 << 12);

// lui x5, -4
// lui x5, (-4<<12)
let instr: u32 = helper::set_u_type_instruction(-4 << 12, 5, LUI as u8);
cpu::exec_lui(&mut cpu_test, instr);
assert_eq!(cpu_test.xregs.regs[5], (-4 << 12) as u32);
Expand All @@ -24,12 +24,12 @@ mod tests {
let mut cpu_test = cpu::CPU::new();

let ori_pc = cpu_test.pc;
// auipc x5, 4
// auipc x5, (4<<12)
let instr: u32 = helper::set_u_type_instruction(4 << 12, 5, AUIPC as u8);
cpu::exec_auipc(&mut cpu_test, instr);
assert_eq!(cpu_test.xregs.regs[5], ori_pc + (4 << 12));

// auipc x5, -4
// auipc x5, (-4<<12)
let instr: u32 = helper::set_u_type_instruction(-4 << 12, 5, AUIPC as u8);
cpu::exec_auipc(&mut cpu_test, instr);
assert_eq!(
Expand All @@ -39,6 +39,7 @@ mod tests {
}
#[test]
fn test_exec_jal() {
// TODO add test case for imm is a negative number
let mut cpu_test = cpu::CPU::new();

let ori_pc = cpu_test.pc;
Expand All @@ -49,7 +50,19 @@ mod tests {
assert_eq!(cpu_test.pc, ori_pc + 12);
}
#[test]
fn test_exec_jalr() {}
fn test_exec_jalr() {
// TODO add test case for imm is a negative number
let mut cpu_test = cpu::CPU::new();

let ori_pc = cpu_test.pc;
// set x1=3
helper::set_register_val(&mut cpu_test, 1, 3);
// jalr x5, 12
let instr: u32 = helper::set_i_type_instruction(12, 1, JALR as u8, 5);
cpu::exec_jalr(&mut cpu_test, instr);
assert_eq!(cpu_test.xregs.regs[5], ori_pc + 4);
assert_eq!(cpu_test.pc, (3 + 12) & 0xfffffffe);
}
#[test]
fn test_exec_beq() {}
#[test]
Expand Down

0 comments on commit 4ad2bc4

Please sign in to comment.