From aa41e096553e534b68ae3961b0f058b9478b47f9 Mon Sep 17 00:00:00 2001 From: Danny Willems Date: Mon, 30 Dec 2024 20:44:21 +0100 Subject: [PATCH] o1vm/riscv32im: helper to read a uint32 from memory --- o1vm/src/interpreters/riscv32im/witness.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/o1vm/src/interpreters/riscv32im/witness.rs b/o1vm/src/interpreters/riscv32im/witness.rs index 256de81687..d6085922ad 100644 --- a/o1vm/src/interpreters/riscv32im/witness.rs +++ b/o1vm/src/interpreters/riscv32im/witness.rs @@ -945,4 +945,15 @@ impl Env { pub fn normalized_instruction_counter(&self) -> u64 { self.instruction_counter / MAX_ACC } + + /// Get a u32 from memory, starting from address `addr`. + pub fn get_uint32(&mut self, addr: u32) -> u32 { + let page = addr >> PAGE_ADDRESS_SIZE; + let page_address = (addr & PAGE_ADDRESS_MASK) as usize; + let memory_page_idx = self.get_memory_page_index(page); + let value = self.memory[memory_page_idx].1[page_address..page_address + 4] + .try_into() + .expect("get_uint32 values fit in a u32"); + u32::from_be_bytes(value) + } }