From 2ef9213f4ad0a530a5df969e08881225291eaa73 Mon Sep 17 00:00:00 2001 From: Danny Willems Date: Fri, 1 Dec 2023 15:59:36 +0100 Subject: [PATCH 1/5] Fix overwrite_register_checked --- optimism/src/mips/witness.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/optimism/src/mips/witness.rs b/optimism/src/mips/witness.rs index 85ceb4feeb..547b0c806d 100644 --- a/optimism/src/mips/witness.rs +++ b/optimism/src/mips/witness.rs @@ -105,7 +105,15 @@ impl InterpreterEnv for Env { 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 { From d8df87d39b4d3c8196ee1cc63cde38f20272c2b8 Mon Sep 17 00:00:00 2001 From: Danny Willems Date: Fri, 1 Dec 2023 16:01:36 +0100 Subject: [PATCH 2/5] Implement addiu --- optimism/src/mips/interpreter.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/optimism/src/mips/interpreter.rs b/optimism/src/mips/interpreter.rs index 57e279ac13..1f7af10a23 100644 --- a/optimism/src/mips/interpreter.rs +++ b/optimism/src/mips/interpreter.rs @@ -323,7 +323,19 @@ pub fn interpret_itype(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 => (), From 6637576d7d693524cdc87f80a956535cf3366e3f Mon Sep 17 00:00:00 2001 From: Danny Willems Date: Fri, 1 Dec 2023 16:02:34 +0100 Subject: [PATCH 3/5] Fix typo --- optimism/src/mips/interpreter.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optimism/src/mips/interpreter.rs b/optimism/src/mips/interpreter.rs index 1f7af10a23..7bd8c58e22 100644 --- a/optimism/src/mips/interpreter.rs +++ b/optimism/src/mips/interpreter.rs @@ -515,6 +515,6 @@ 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[REGISTER_AT as usize], 0xa0000); } } From d06a168506eee26991b60f87974a923b3bc38114 Mon Sep 17 00:00:00 2001 From: Danny Willems Date: Fri, 1 Dec 2023 16:08:08 +0100 Subject: [PATCH 4/5] Add test for addiu --- optimism/src/mips/interpreter.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/optimism/src/mips/interpreter.rs b/optimism/src/mips/interpreter.rs index 7bd8c58e22..06c926d9c0 100644 --- a/optimism/src/mips/interpreter.rs +++ b/optimism/src/mips/interpreter.rs @@ -517,4 +517,23 @@ mod tests { interpret_itype(&mut dummy_env, ITypeInstruction::LoadUpperImmediate); assert_eq!(dummy_env.registers[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[REGISTER_AT as usize], exp_res); + } } From 12ad9d7a44ecca399deba1b845b4d841599719de Mon Sep 17 00:00:00 2001 From: Danny Willems Date: Fri, 1 Dec 2023 16:52:06 +0100 Subject: [PATCH 5/5] Fix tests by using correct structure on registers --- optimism/src/mips/interpreter.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/optimism/src/mips/interpreter.rs b/optimism/src/mips/interpreter.rs index 06c926d9c0..6607fddf3b 100644 --- a/optimism/src/mips/interpreter.rs +++ b/optimism/src/mips/interpreter.rs @@ -476,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] @@ -495,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 ); } @@ -515,7 +518,10 @@ 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] @@ -534,6 +540,9 @@ mod tests { }; let exp_res = dummy_env.registers[REGISTER_AT as usize] + 27880; interpret_itype(&mut dummy_env, ITypeInstruction::AddImmediateUnsigned); - assert_eq!(dummy_env.registers[REGISTER_AT as usize], exp_res); + assert_eq!( + dummy_env.registers.general_purpose[REGISTER_AT as usize], + exp_res + ); } }