From 6393f638266f621ba173918c38e07598076b5538 Mon Sep 17 00:00:00 2001 From: svv232 Date: Sun, 1 Dec 2024 16:52:03 -0500 Subject: [PATCH] branch equal instruction implementation --- .../src/interpreters/riscv32im/interpreter.rs | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/o1vm/src/interpreters/riscv32im/interpreter.rs b/o1vm/src/interpreters/riscv32im/interpreter.rs index 462ded8981..904dbf67c9 100644 --- a/o1vm/src/interpreters/riscv32im/interpreter.rs +++ b/o1vm/src/interpreters/riscv32im/interpreter.rs @@ -2069,7 +2069,7 @@ pub fn interpret_sbtype(env: &mut Env, instr: SBInstruction /* fetch instruction pointer from the program state */ let instruction_pointer = env.get_instruction_pointer(); /* compute the next instruction ptr and add one, as well record raml lookup */ - let _next_instruction_pointer = env.get_next_instruction_pointer(); + let next_instruction_pointer = env.get_next_instruction_pointer(); /* read instruction from ip address */ let instruction = { let v0 = env.read_memory(&instruction_pointer); @@ -2157,7 +2157,29 @@ pub fn interpret_sbtype(env: &mut Env, instr: SBInstruction match instr { SBInstruction::BranchEq => { - unimplemented!("BranchEq") + // beq: if (x[rs1] == x[rs2]) pc += sext(offset) + let local_rs1 = env.read_register(&rs1); + let local_rs2 = env.read_register(&rs2); + + let equals = env.equal(&local_rs1, &local_rs2); + let offset = (Env::constant(1) - equals.clone()) * Env::constant(4) + equals * imm0_12; + let offset = env.sign_extend(&offset, 12); + let addr = { + let res_scratch = env.alloc_scratch(); + let overflow_scratch = env.alloc_scratch(); + let (res, _overflow) = unsafe { + env.add_witness( + &next_instruction_pointer, + &offset, + res_scratch, + overflow_scratch, + ) + }; + // FIXME: Requires a range check + res + }; + env.set_instruction_pointer(next_instruction_pointer); + env.set_next_instruction_pointer(addr); } SBInstruction::BranchNeq => { // bne: if (x[rs1] != x[rs2]) pc += sext(offset)