Skip to content

Commit

Permalink
Merge pull request #1368 from o1-labs/dw/addiu
Browse files Browse the repository at this point in the history
Add addiu
  • Loading branch information
dannywillems authored Dec 1, 2023
2 parents 0f33a7c + 12ad9d7 commit da58501
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
50 changes: 45 additions & 5 deletions optimism/src/mips/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,19 @@ pub fn interpret_itype<Env: InterpreterEnv>(env: &mut Env, instr: ITypeInstructi
// REMOVEME: when all itype instructions are implemented.
return;
}
ITypeInstruction::AddImmediateUnsigned => (),
ITypeInstruction::AddImmediateUnsigned => {
let rs = env.get_instruction_part(InstructionPart::RS);
let rt = env.get_instruction_part(InstructionPart::RT);
debug!("Fetching register: {}", rs);
let register_rs = env.fetch_register_checked(&rs);
let immediate = env.get_immediate();
let res = register_rs + immediate;
env.overwrite_register_checked(&rt, &res);
env.set_instruction_pointer(env.get_instruction_pointer() + Env::constant(4u32));
// TODO: update next_instruction_pointer
// REMOVEME: when all itype instructions are implemented.
return;
}
ITypeInstruction::SetLessThanImmediate => (),
ITypeInstruction::SetLessThanImmediateUnsigned => (),
ITypeInstruction::AndImmediate => (),
Expand Down Expand Up @@ -463,7 +475,10 @@ mod tests {
funct: 0b000000,
};
interpret_itype(&mut dummy_env, ITypeInstruction::Load32);
assert_eq!(dummy_env.registers[REGISTER_A0 as usize], exp_v);
assert_eq!(
dummy_env.registers.general_purpose[REGISTER_A0 as usize],
exp_v
);
}

#[test]
Expand All @@ -482,8 +497,8 @@ mod tests {
};
interpret_itype(&mut dummy_env, ITypeInstruction::AddImmediate);
assert_eq!(
dummy_env.registers[REGISTER_A1 as usize],
dummy_env.registers[REGISTER_SP as usize] + 4
dummy_env.registers.general_purpose[REGISTER_A1 as usize],
dummy_env.registers.general_purpose[REGISTER_SP as usize] + 4
);
}

Expand All @@ -502,6 +517,31 @@ mod tests {
funct: 0b001010,
};
interpret_itype(&mut dummy_env, ITypeInstruction::LoadUpperImmediate);
assert_eq!(dummy_env.registers[REGISTER_AT as usize], 0xa0000,);
assert_eq!(
dummy_env.registers.general_purpose[REGISTER_AT as usize],
0xa0000
);
}

#[test]
fn test_unit_addiu_instruction() {
// We only care about instruction parts and instruction pointer
let mut dummy_env = dummy_env();
// Instruction: 0b00100100001000010110110011101000
// lui at, 0xa
dummy_env.instruction_parts = InstructionParts {
op_code: 0b001001,
rs: 0b00001,
rt: 0b00001,
rd: 0b01101,
shamt: 0b10011,
funct: 0b101000,
};
let exp_res = dummy_env.registers[REGISTER_AT as usize] + 27880;
interpret_itype(&mut dummy_env, ITypeInstruction::AddImmediateUnsigned);
assert_eq!(
dummy_env.registers.general_purpose[REGISTER_AT as usize],
exp_res
);
}
}
10 changes: 9 additions & 1 deletion optimism/src/mips/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,15 @@ impl<Fp: Field> InterpreterEnv for Env<Fp> {
register_idx: &Self::Variable,
value: &Self::Variable,
) {
self.registers[*register_idx as usize] = *value
if *register_idx < 32 {
self.registers.general_purpose[*register_idx as usize] = *value
} else if *register_idx == 32 {
self.registers.hi = *value
} else if *register_idx == 33 {
self.registers.lo = *value
} else {
panic!("Impossible to fetch register idx: {}", register_idx);
}
}

fn fetch_register_checked(&self, register_idx: &Self::Variable) -> Self::Variable {
Expand Down

0 comments on commit da58501

Please sign in to comment.