Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add addiu #1368

Merged
merged 5 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -464,7 +476,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 @@ -483,8 +498,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 @@ -503,6 +518,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