1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use core::cell::UnsafeCell;
use risc0_zkvm::platform::{
io::{
IoDescriptor, GPIO_COMMIT, GPIO_SENDRECV_ADDR, GPIO_SENDRECV_CHANNEL, GPIO_SENDRECV_SIZE,
SENDRECV_CHANNEL_INITIAL_INPUT, SENDRECV_CHANNEL_STDOUT,
},
memory, WORD_SIZE,
};
static mut READ_PTR: UnsafeCell<usize> = UnsafeCell::new(0);
pub fn host_sendrecv(channel: u32, buf: &[u8]) -> (&'static [u32], usize) {
let read_ptr: &mut usize = unsafe { &mut *READ_PTR.get() };
unsafe {
GPIO_SENDRECV_CHANNEL.as_ptr().write_volatile(channel);
GPIO_SENDRECV_SIZE.as_ptr().write_volatile(buf.len());
GPIO_SENDRECV_ADDR.as_ptr().write_volatile(buf.as_ptr());
}
let read_start: *const u32 = memory::INPUT.start() as _;
let response_nbytes = unsafe { read_start.add(*read_ptr).read_volatile() } as usize;
*read_ptr += 1;
let response_nwords = (response_nbytes + WORD_SIZE - 1) / WORD_SIZE;
assert!(*read_ptr + response_nwords < memory::INPUT.len_words());
let response_data =
unsafe { core::slice::from_raw_parts(read_start.add(*read_ptr), response_nwords) };
*read_ptr += response_nwords;
(response_data, response_nbytes)
}