aboutsummaryrefslogtreecommitdiff
path: root/src/simulator.rs
diff options
context:
space:
mode:
authorDavid Li <li.davidm96@gmail.com>2015-12-20 15:14:52 -0500
committerDavid Li <li.davidm96@gmail.com>2015-12-20 15:14:52 -0500
commit9888788cd4894c0c00f7373fc0cd6915941d638d (patch)
treeeca4a6b87a6c25a12e43e1e2daf5d5757bf8b683 /src/simulator.rs
parent24f8673dce3f8ac8658681f16d856c45dded377a (diff)
Implement logic for RV32I branch instructions
Diffstat (limited to 'src/simulator.rs')
-rw-r--r--src/simulator.rs22
1 files changed, 21 insertions, 1 deletions
diff --git a/src/simulator.rs b/src/simulator.rs
index bf39c5e..3ca11d9 100644
--- a/src/simulator.rs
+++ b/src/simulator.rs
@@ -120,7 +120,27 @@ impl Simulator {
return;
}
isa::opcodes::BRANCH => {
-
+ let target = ((pc as i32) + inst.sb_imm()) as usize;
+ let rs1 = core.registers.read_word(inst.rs1());
+ let rs2 = core.registers.read_word(inst.rs2());
+ if match inst.funct3() {
+ isa::funct3::BEQ => rs1 == rs2,
+ isa::funct3::BNE => rs1 != rs2,
+ isa::funct3::BLT => (rs1 as i32) < (rs2 as i32),
+ isa::funct3::BGE => (rs1 as i32) > (rs2 as i32),
+ isa::funct3::BLTU => rs1 < rs2,
+ isa::funct3::BGEU => rs1 > rs2,
+ _ => {
+ self.trap(core, Trap::IllegalInstruction {
+ address: pc,
+ instruction: inst,
+ });
+ false
+ }
+ } {
+ core.pc = target;
+ return;
+ }
},
isa::opcodes::INTEGER_IMMEDIATE => {
let imm = inst.i_imm();