From 1c9586156a7f7c2179c588fab28e7c921750a141 Mon Sep 17 00:00:00 2001 From: Danny Willems Date: Mon, 23 Dec 2024 13:40:20 +0100 Subject: [PATCH] o1vm/riscv32im: add example and test for addi --- o1vm/resources/programs/riscv32im/bin/addi | Bin 0 -> 452 bytes o1vm/resources/programs/riscv32im/src/addi.S | 20 +++++++++++++++++++ o1vm/tests/test_riscv_elf.rs | 16 +++++++++++++++ 3 files changed, 36 insertions(+) create mode 100755 o1vm/resources/programs/riscv32im/bin/addi create mode 100644 o1vm/resources/programs/riscv32im/src/addi.S diff --git a/o1vm/resources/programs/riscv32im/bin/addi b/o1vm/resources/programs/riscv32im/bin/addi new file mode 100755 index 0000000000000000000000000000000000000000..4ad4f72babb9e2542acaa7a09fb1b83828c1b2ef GIT binary patch literal 452 zcma)2K~BRk5S#=GmAHWN0VFtZKoK?l00%g6LY#@*kP1r;ME15qy&&<0e87L?0Wh23 za%HU9nO#j3dp~aP@3JgILXIzJC#-YoIh%dz9H+=}LBGd?E^?Mo{Tj{bx5^P?7uhH$ zB#wbTkLW)iP;k(b#Q~Kp4yj}@rA}iW7{7U9@h;RGWRJ8HzqvNfbhD~8-Bo4PElp>t zXDwS-2e0$3TQ;wC!ytXzu2(ZP*0yh>S@}@Y0cCgz;>9jdO7ku#hkun;#Jj~>8ok4R ram1j$m`@q@C)7(aLHDCLA$5mC!xG7~L!G7lsWP|2H{IE;`^)_RiD5f$ literal 0 HcmV?d00001 diff --git a/o1vm/resources/programs/riscv32im/src/addi.S b/o1vm/resources/programs/riscv32im/src/addi.S new file mode 100644 index 0000000000..f4e50282bf --- /dev/null +++ b/o1vm/resources/programs/riscv32im/src/addi.S @@ -0,0 +1,20 @@ +.section .text +.globl _start + +_start: + # Initialize register + li t0, 10 # Load immediate value 10 into t0 + + # Perform addition + addi t0, t0, 5 # Add 5 to the value in t0 and store the result back in t0 + + # Custom exit syscall + li a0, 0 # Set a0 to 0 + li a1, 0 # Set a1 to 0 + li a2, 0 # Set a2 to 0 + li a3, 0 # Set a3 to 0 + li a4, 0 # Set a4 to 0 + li a5, 0 # Set a5 to 0 + li a6, 0 # Set a6 to 0 + li a7, 42 # Set a7 to 42 (custom ecall number) + ecall diff --git a/o1vm/tests/test_riscv_elf.rs b/o1vm/tests/test_riscv_elf.rs index c2925b7b10..444249b11b 100644 --- a/o1vm/tests/test_riscv_elf.rs +++ b/o1vm/tests/test_riscv_elf.rs @@ -104,3 +104,19 @@ fn test_sll() { // Expected output of the program assert_eq!(witness.registers.general_purpose[5], 1 << 14) } + +#[test] +fn test_addi() { + let curr_dir = std::env::current_dir().unwrap(); + let path = curr_dir.join(std::path::PathBuf::from( + "resources/programs/riscv32im/bin/addi", + )); + let state = o1vm::elf_loader::parse_riscv32(&path).unwrap(); + let mut witness = Env::::create(PAGE_SIZE.try_into().unwrap(), state); + + while !witness.halt { + witness.step(); + } + + assert_eq!(witness.registers[T0], 15); +}