-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Aurélien Nicolas
committed
Sep 2, 2024
1 parent
8285d59
commit c7ae04b
Showing
4 changed files
with
56 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,54 @@ | ||
#![no_main] | ||
#![no_std] | ||
use core::ptr::addr_of_mut; | ||
|
||
// Use volatile functions to prevent compiler optimizations. | ||
use core::ptr::{read_volatile, write_volatile}; | ||
|
||
#[allow(unused_imports)] | ||
use ceno_rt; | ||
const OUTPUT_ADDRESS: u32 = 0x8000_0000; | ||
|
||
#[no_mangle] | ||
#[inline(never)] | ||
fn main() { | ||
let y = my_recurse(3, 0); | ||
output(y); | ||
test_data_section(); | ||
|
||
let out = fibonacci_recurse(20, 0, 1); | ||
test_output(out); | ||
} | ||
|
||
/// Test the .data section is loaded and read/write works. | ||
#[inline(never)] | ||
fn test_data_section() { | ||
// Use X[1] to be sure it is not the same as *OUTPUT_ADDRESS. | ||
static mut X: [u32; 2] = [0, 42]; | ||
|
||
unsafe { | ||
assert_eq!(read_volatile(&X[1]), 42); | ||
write_volatile(&mut X[1], 99); | ||
assert_eq!(read_volatile(&X[1]), 99); | ||
} | ||
} | ||
|
||
// A sufficiently complicated function to test the stack. | ||
#[inline(never)] | ||
#[no_mangle] | ||
fn my_recurse(x: u32, y: u32) -> u32 { | ||
if x == 0 { | ||
y | ||
fn fibonacci_recurse(count: u32, a: u32, b: u32) -> u32 { | ||
let count = black_box(count); | ||
if count == 0 { | ||
a | ||
} else { | ||
my_recurse(x - 1, y * 3 + 5) | ||
fibonacci_recurse(count - 1, b, a + b) | ||
} | ||
} | ||
|
||
// A global variable to test writing to memory. | ||
static mut OUTPUT: u32 = 0; | ||
|
||
// Store the output to a specific memory location so the emulator tests can find it. | ||
#[inline(never)] | ||
fn output(out: u32) { | ||
fn test_output(out: u32) { | ||
unsafe { | ||
// Volatile write to prevent the compiler from optimizing this away. | ||
core::ptr::write_volatile(addr_of_mut!(OUTPUT), out); | ||
write_volatile(OUTPUT_ADDRESS as *mut u32, out); | ||
} | ||
} | ||
|
||
fn black_box<T>(x: T) -> T { | ||
unsafe { read_volatile(&x) } | ||
} |