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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#[cfg(zisk_guest)]
use core::arch::asm;
// fcall_get 0xFFE
#[cfg(zisk_guest)]
pub fn ziskos_fcall_get() -> u64 {
let value: u64;
unsafe {
asm!("csrr {}, 0xFFE", out(reg) value);
}
value
}
#[macro_export]
macro_rules! ziskos_fcall_param {
( $addr:expr, $words:literal) => {{
// Computes the index based on log2 of the number of words
const fn words_to_port(words: usize) -> usize {
match words {
1 => 0, /* direct value */
2 => 1, /* 2 x 8 = 16 */
4 => 2, /* 4 x 8 = 32 */
8 => 3, /* 8 x 8 = 64 */
12 => 4, /* 12 x 8 = 96 */
16 => 5, /* 16 x 8 = 128 */
20 => 6, /* 20 x 8 = 160 */
24 => 7, /* 24 x 8 = 192 */
25 => 8, /* 25 x 8 = 200, Keccak-f state */
32 => 9, /* 32 x 8 = 256 */
48 => 10, /* 48 x 8 = 384 */
64 => 11, /* 64 x 8 = 512 */
80 => 12, /* 80 x 8 = 640 */
96 => 13, /* 96 x 8 = 768 */
128 => 14, /* 128 x 8 = 1024 */
256 => 15, /* 256 x 8 = 2048 */
_ => panic!("number of words no supported, must be 2, 4, 8, 12, 16, 20, 24, 25, 32, 48, 64, 80, 96, 128 or 256"),
}
}
unsafe {
asm!(
"csrs {port}, {value}",
port = const 0x8F0 + words_to_port($words),
value = in(reg) $addr
);
}
}};
}
#[macro_export]
macro_rules! ziskos_fcall {
($func_id:expr) => {{
const _: () = assert!($func_id < 1024, "func_id must be less than 1024");
unsafe {
asm!(
"csrwi {port}, {imm}",
port = const 0x8C0 + ($func_id >> 5),
imm = const $func_id & 0x1f
);
}
}};
}
#[macro_export]
macro_rules! ziskos_fcall_mget {
() => {
read_csr_ffe()
};
(1) => {
read_csr_ffe()
};
(2) => {
[read_csr_ffe(), read_csr_ffe()]
};
(3) => {
[read_csr_ffe(), read_csr_ffe(), read_csr_ffe()]
};
(4) => {
[
$crate::read_csr_ffe(),
$crate::read_csr_ffe(),
$crate::read_csr_ffe(),
$crate::read_csr_ffe(),
]
};
(8) => {
[
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
]
}; // add more if needed
(12) => {
[
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
]
}; // add more if needed
(16) => {
[
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
read_csr_ffe(),
]
}; // add more if needed
($len:expr) => {{
let mut arr = [0u64; $len];
let mut i = 0;
while i < $len {
arr[i] = read_csr_ffe();
i += 1;
}
arr
}};
}