♻️ Simplified branch_instruction
This commit is contained in:
parent
ff93fab832
commit
2162232199
@ -308,41 +308,20 @@ impl Machine {
|
|||||||
|
|
||||||
/// Treatement for Branch instructions
|
/// Treatement for Branch instructions
|
||||||
fn branch_instruction(&mut self, inst: Instruction) -> Result<(), MachineError> {
|
fn branch_instruction(&mut self, inst: Instruction) -> Result<(), MachineError> {
|
||||||
match inst.funct3 {
|
let op = match inst.funct3 {
|
||||||
RISCV_BR_BEQ => {
|
RISCV_BR_BEQ => |a, b| a == b,
|
||||||
if self.int_reg.get_reg(inst.rs1) == self.int_reg.get_reg(inst.rs2) {
|
RISCV_BR_BNE => |a, b| a != b,
|
||||||
|
RISCV_BR_BLT => |a, b| a < b,
|
||||||
|
RISCV_BR_BGE => |a, b| a >= b,
|
||||||
|
RISCV_BR_BLTU => |a, b| a < b,
|
||||||
|
RISCV_BR_BGEU => |a, b| a >= b,
|
||||||
|
_ => unreachable!()
|
||||||
|
};
|
||||||
|
let rs1 = self.int_reg.get_reg(inst.rs1);
|
||||||
|
let rs2 = self.int_reg.get_reg(inst.rs2);
|
||||||
|
if op(rs1, rs2) {
|
||||||
self.pc = (self.pc as i64 + inst.imm13_signed as i64 - 4) as u64;
|
self.pc = (self.pc as i64 + inst.imm13_signed as i64 - 4) as u64;
|
||||||
}
|
}
|
||||||
},
|
|
||||||
RISCV_BR_BNE => {
|
|
||||||
if self.int_reg.get_reg(inst.rs1) != self.int_reg.get_reg(inst.rs2) {
|
|
||||||
self.pc = (self.pc as i64 + inst.imm13_signed as i64 - 4) as u64;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
RISCV_BR_BLT => {
|
|
||||||
if self.int_reg.get_reg(inst.rs1) < self.int_reg.get_reg(inst.rs2) {
|
|
||||||
self.pc = (self.pc as i64 + inst.imm13_signed as i64 - 4) as u64;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
RISCV_BR_BGE => {
|
|
||||||
if self.int_reg.get_reg(inst.rs1) >= self.int_reg.get_reg(inst.rs2) {
|
|
||||||
self.pc = (self.pc as i64 + inst.imm13_signed as i64 - 4) as u64;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
RISCV_BR_BLTU => {
|
|
||||||
if self.int_reg.get_reg(inst.rs1) < self.int_reg.get_reg(inst.rs2) {
|
|
||||||
self.pc = (self.pc as i64 + inst.imm13_signed as i64 - 4) as u64;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
RISCV_BR_BGEU => {
|
|
||||||
if self.int_reg.get_reg(inst.rs1) >= self.int_reg.get_reg(inst.rs2) {
|
|
||||||
self.pc = (self.pc as i64 + inst.imm13_signed as i64 - 4) as u64;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_ => {
|
|
||||||
panic!("In BR switch case, this should never happen... Instr was {}", inst.value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user