1 |
2 |
zero_gravi |
// #################################################################################################
|
2 |
|
|
// # << NEORV32: neorv32_cpu.c - CPU Core Functions HW Driver >> #
|
3 |
|
|
// # ********************************************************************************************* #
|
4 |
|
|
// # BSD 3-Clause License #
|
5 |
|
|
// # #
|
6 |
42 |
zero_gravi |
// # Copyright (c) 2021, Stephan Nolting. All rights reserved. #
|
7 |
2 |
zero_gravi |
// # #
|
8 |
|
|
// # Redistribution and use in source and binary forms, with or without modification, are #
|
9 |
|
|
// # permitted provided that the following conditions are met: #
|
10 |
|
|
// # #
|
11 |
|
|
// # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
12 |
|
|
// # conditions and the following disclaimer. #
|
13 |
|
|
// # #
|
14 |
|
|
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
15 |
|
|
// # conditions and the following disclaimer in the documentation and/or other materials #
|
16 |
|
|
// # provided with the distribution. #
|
17 |
|
|
// # #
|
18 |
|
|
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
19 |
|
|
// # endorse or promote products derived from this software without specific prior written #
|
20 |
|
|
// # permission. #
|
21 |
|
|
// # #
|
22 |
|
|
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
23 |
|
|
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
24 |
|
|
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
25 |
|
|
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
26 |
|
|
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
27 |
|
|
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
28 |
|
|
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
29 |
|
|
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
30 |
|
|
// # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
31 |
|
|
// # ********************************************************************************************* #
|
32 |
|
|
// # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
33 |
|
|
// #################################################################################################
|
34 |
|
|
|
35 |
|
|
|
36 |
|
|
/**********************************************************************//**
|
37 |
|
|
* @file neorv32_cpu.c
|
38 |
|
|
* @author Stephan Nolting
|
39 |
|
|
* @brief CPU Core Functions HW driver source file.
|
40 |
|
|
**************************************************************************/
|
41 |
|
|
|
42 |
|
|
#include "neorv32.h"
|
43 |
|
|
#include "neorv32_cpu.h"
|
44 |
|
|
|
45 |
|
|
|
46 |
53 |
zero_gravi |
/**********************************************************************//**
|
47 |
|
|
* Unavailable extensions warning.
|
48 |
|
|
**************************************************************************/
|
49 |
|
|
#if defined __riscv_f || (__riscv_flen == 32)
|
50 |
|
|
#warning Single-precision floating-point extension <F/Zfinx> is WORK-IN-PROGRESS and NOT FULLY OPERATIONAL yet!
|
51 |
|
|
#endif
|
52 |
45 |
zero_gravi |
|
53 |
53 |
zero_gravi |
#if defined __riscv_d || (__riscv_flen == 64)
|
54 |
|
|
#error Double-precision floating-point extension <D/Zdinx> is NOT supported!
|
55 |
|
|
#endif
|
56 |
|
|
|
57 |
|
|
#if (__riscv_xlen > 32)
|
58 |
|
|
#error Only 32-bit <rv32> is supported!
|
59 |
|
|
#endif
|
60 |
|
|
|
61 |
|
|
#ifdef __riscv_b
|
62 |
|
|
#warning Bit-manipulation extension <B> is still experimental (non-ratified) and does not support all <Zb*> subsets yet.
|
63 |
|
|
#endif
|
64 |
|
|
|
65 |
|
|
#ifdef __riscv_fdiv
|
66 |
|
|
#warning Floating-point division instruction <FDIV> is NOT supported yet!
|
67 |
|
|
#endif
|
68 |
|
|
|
69 |
|
|
#ifdef __riscv_fsqrt
|
70 |
|
|
#warning Floating-point square root instruction <FSQRT> is NOT supported yet!
|
71 |
|
|
#endif
|
72 |
|
|
|
73 |
|
|
|
74 |
2 |
zero_gravi |
/**********************************************************************//**
|
75 |
45 |
zero_gravi |
* >Private< helper functions.
|
76 |
|
|
**************************************************************************/
|
77 |
47 |
zero_gravi |
static int __neorv32_cpu_irq_id_check(uint8_t irq_sel);
|
78 |
45 |
zero_gravi |
static uint32_t __neorv32_cpu_pmp_cfg_read(uint32_t index);
|
79 |
|
|
static void __neorv32_cpu_pmp_cfg_write(uint32_t index, uint32_t data);
|
80 |
|
|
|
81 |
|
|
|
82 |
|
|
/**********************************************************************//**
|
83 |
47 |
zero_gravi |
* Private function: Check IRQ id.
|
84 |
|
|
*
|
85 |
|
|
* @param[in] irq_sel CPU interrupt select. See #NEORV32_CSR_MIE_enum.
|
86 |
|
|
* @return 0 if success, 1 if error (invalid irq_sel).
|
87 |
|
|
**************************************************************************/
|
88 |
|
|
static int __neorv32_cpu_irq_id_check(uint8_t irq_sel) {
|
89 |
|
|
|
90 |
48 |
zero_gravi |
if ((irq_sel == CSR_MIE_MSIE) || (irq_sel == CSR_MIE_MTIE) || (irq_sel == CSR_MIE_MEIE) ||
|
91 |
|
|
((irq_sel >= CSR_MIE_FIRQ0E) && (irq_sel <= CSR_MIE_FIRQ15E))) {
|
92 |
47 |
zero_gravi |
return 0;
|
93 |
|
|
}
|
94 |
|
|
else {
|
95 |
|
|
return 1;
|
96 |
|
|
}
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
|
100 |
|
|
/**********************************************************************//**
|
101 |
2 |
zero_gravi |
* Enable specific CPU interrupt.
|
102 |
|
|
*
|
103 |
|
|
* @note Interrupts have to be globally enabled via neorv32_cpu_eint(void), too.
|
104 |
|
|
*
|
105 |
42 |
zero_gravi |
* @param[in] irq_sel CPU interrupt select. See #NEORV32_CSR_MIE_enum.
|
106 |
12 |
zero_gravi |
* @return 0 if success, 1 if error (invalid irq_sel).
|
107 |
2 |
zero_gravi |
**************************************************************************/
|
108 |
|
|
int neorv32_cpu_irq_enable(uint8_t irq_sel) {
|
109 |
|
|
|
110 |
47 |
zero_gravi |
// check IRQ id
|
111 |
|
|
if (__neorv32_cpu_irq_id_check(irq_sel)) {
|
112 |
2 |
zero_gravi |
return 1;
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
register uint32_t mask = (uint32_t)(1 << irq_sel);
|
116 |
|
|
asm volatile ("csrrs zero, mie, %0" : : "r" (mask));
|
117 |
|
|
return 0;
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
|
121 |
|
|
/**********************************************************************//**
|
122 |
|
|
* Disable specific CPU interrupt.
|
123 |
|
|
*
|
124 |
42 |
zero_gravi |
* @param[in] irq_sel CPU interrupt select. See #NEORV32_CSR_MIE_enum.
|
125 |
12 |
zero_gravi |
* @return 0 if success, 1 if error (invalid irq_sel).
|
126 |
2 |
zero_gravi |
**************************************************************************/
|
127 |
|
|
int neorv32_cpu_irq_disable(uint8_t irq_sel) {
|
128 |
|
|
|
129 |
47 |
zero_gravi |
// check IRQ id
|
130 |
|
|
if (__neorv32_cpu_irq_id_check(irq_sel)) {
|
131 |
2 |
zero_gravi |
return 1;
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
register uint32_t mask = (uint32_t)(1 << irq_sel);
|
135 |
|
|
asm volatile ("csrrc zero, mie, %0" : : "r" (mask));
|
136 |
|
|
return 0;
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
|
140 |
|
|
/**********************************************************************//**
|
141 |
12 |
zero_gravi |
* Get cycle count from cycle[h].
|
142 |
|
|
*
|
143 |
|
|
* @note The cycle[h] CSR is shadowed copy of the mcycle[h] CSR.
|
144 |
|
|
*
|
145 |
|
|
* @return Current cycle counter (64 bit).
|
146 |
|
|
**************************************************************************/
|
147 |
|
|
uint64_t neorv32_cpu_get_cycle(void) {
|
148 |
|
|
|
149 |
|
|
union {
|
150 |
|
|
uint64_t uint64;
|
151 |
|
|
uint32_t uint32[sizeof(uint64_t)/2];
|
152 |
|
|
} cycles;
|
153 |
|
|
|
154 |
|
|
uint32_t tmp1, tmp2, tmp3;
|
155 |
|
|
while(1) {
|
156 |
|
|
tmp1 = neorv32_cpu_csr_read(CSR_CYCLEH);
|
157 |
|
|
tmp2 = neorv32_cpu_csr_read(CSR_CYCLE);
|
158 |
|
|
tmp3 = neorv32_cpu_csr_read(CSR_CYCLEH);
|
159 |
|
|
if (tmp1 == tmp3) {
|
160 |
|
|
break;
|
161 |
|
|
}
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
cycles.uint32[0] = tmp2;
|
165 |
|
|
cycles.uint32[1] = tmp3;
|
166 |
|
|
|
167 |
|
|
return cycles.uint64;
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
|
171 |
|
|
/**********************************************************************//**
|
172 |
|
|
* Set mcycle[h] counter.
|
173 |
|
|
*
|
174 |
|
|
* @param[in] value New value for mcycle[h] CSR (64-bit).
|
175 |
|
|
**************************************************************************/
|
176 |
|
|
void neorv32_cpu_set_mcycle(uint64_t value) {
|
177 |
|
|
|
178 |
|
|
union {
|
179 |
|
|
uint64_t uint64;
|
180 |
|
|
uint32_t uint32[sizeof(uint64_t)/2];
|
181 |
|
|
} cycles;
|
182 |
|
|
|
183 |
|
|
cycles.uint64 = value;
|
184 |
|
|
|
185 |
|
|
neorv32_cpu_csr_write(CSR_MCYCLE, 0);
|
186 |
|
|
neorv32_cpu_csr_write(CSR_MCYCLEH, cycles.uint32[1]);
|
187 |
|
|
neorv32_cpu_csr_write(CSR_MCYCLE, cycles.uint32[0]);
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
|
191 |
|
|
/**********************************************************************//**
|
192 |
|
|
* Get retired instructions counter from instret[h].
|
193 |
|
|
*
|
194 |
|
|
* @note The instret[h] CSR is shadowed copy of the instret[h] CSR.
|
195 |
|
|
*
|
196 |
|
|
* @return Current instructions counter (64 bit).
|
197 |
|
|
**************************************************************************/
|
198 |
|
|
uint64_t neorv32_cpu_get_instret(void) {
|
199 |
|
|
|
200 |
|
|
union {
|
201 |
|
|
uint64_t uint64;
|
202 |
|
|
uint32_t uint32[sizeof(uint64_t)/2];
|
203 |
|
|
} cycles;
|
204 |
|
|
|
205 |
|
|
uint32_t tmp1, tmp2, tmp3;
|
206 |
|
|
while(1) {
|
207 |
|
|
tmp1 = neorv32_cpu_csr_read(CSR_INSTRETH);
|
208 |
|
|
tmp2 = neorv32_cpu_csr_read(CSR_INSTRET);
|
209 |
|
|
tmp3 = neorv32_cpu_csr_read(CSR_INSTRETH);
|
210 |
|
|
if (tmp1 == tmp3) {
|
211 |
|
|
break;
|
212 |
|
|
}
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
cycles.uint32[0] = tmp2;
|
216 |
|
|
cycles.uint32[1] = tmp3;
|
217 |
|
|
|
218 |
|
|
return cycles.uint64;
|
219 |
|
|
}
|
220 |
|
|
|
221 |
|
|
|
222 |
|
|
/**********************************************************************//**
|
223 |
|
|
* Set retired instructions counter minstret[h].
|
224 |
|
|
*
|
225 |
|
|
* @param[in] value New value for mcycle[h] CSR (64-bit).
|
226 |
|
|
**************************************************************************/
|
227 |
|
|
void neorv32_cpu_set_minstret(uint64_t value) {
|
228 |
|
|
|
229 |
|
|
union {
|
230 |
|
|
uint64_t uint64;
|
231 |
|
|
uint32_t uint32[sizeof(uint64_t)/2];
|
232 |
|
|
} cycles;
|
233 |
|
|
|
234 |
|
|
cycles.uint64 = value;
|
235 |
|
|
|
236 |
|
|
neorv32_cpu_csr_write(CSR_MINSTRET, 0);
|
237 |
|
|
neorv32_cpu_csr_write(CSR_MINSTRETH, cycles.uint32[1]);
|
238 |
|
|
neorv32_cpu_csr_write(CSR_MINSTRET, cycles.uint32[0]);
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
|
242 |
|
|
/**********************************************************************//**
|
243 |
|
|
* Get current system time from time[h] CSR.
|
244 |
|
|
*
|
245 |
|
|
* @note This function requires the MTIME system timer to be implemented.
|
246 |
|
|
*
|
247 |
|
|
* @return Current system time (64 bit).
|
248 |
|
|
**************************************************************************/
|
249 |
|
|
uint64_t neorv32_cpu_get_systime(void) {
|
250 |
|
|
|
251 |
|
|
union {
|
252 |
|
|
uint64_t uint64;
|
253 |
|
|
uint32_t uint32[sizeof(uint64_t)/2];
|
254 |
|
|
} cycles;
|
255 |
|
|
|
256 |
|
|
uint32_t tmp1, tmp2, tmp3;
|
257 |
|
|
while(1) {
|
258 |
|
|
tmp1 = neorv32_cpu_csr_read(CSR_TIMEH);
|
259 |
|
|
tmp2 = neorv32_cpu_csr_read(CSR_TIME);
|
260 |
|
|
tmp3 = neorv32_cpu_csr_read(CSR_TIMEH);
|
261 |
|
|
if (tmp1 == tmp3) {
|
262 |
|
|
break;
|
263 |
|
|
}
|
264 |
|
|
}
|
265 |
|
|
|
266 |
|
|
cycles.uint32[0] = tmp2;
|
267 |
|
|
cycles.uint32[1] = tmp3;
|
268 |
|
|
|
269 |
|
|
return cycles.uint64;
|
270 |
|
|
}
|
271 |
|
|
|
272 |
|
|
|
273 |
|
|
/**********************************************************************//**
|
274 |
56 |
zero_gravi |
* Simple delay function using busy wait (simple loop).
|
275 |
2 |
zero_gravi |
*
|
276 |
56 |
zero_gravi |
* @warning This function is not really precise (especially if there is no M extension available)! Use a timer-based approach (using cycle or time CSRs) for precise timings.
|
277 |
39 |
zero_gravi |
*
|
278 |
56 |
zero_gravi |
* @param[in] time_ms Time in ms to wait (max 32767ms).
|
279 |
2 |
zero_gravi |
**************************************************************************/
|
280 |
56 |
zero_gravi |
void neorv32_cpu_delay_ms(int16_t time_ms) {
|
281 |
2 |
zero_gravi |
|
282 |
56 |
zero_gravi |
const uint32_t loop_cycles_c = 16; // clock cycles per iteration of the ASM loop
|
283 |
2 |
zero_gravi |
|
284 |
56 |
zero_gravi |
// check input
|
285 |
|
|
if (time_ms < 0) {
|
286 |
|
|
time_ms = -time_ms;
|
287 |
|
|
}
|
288 |
|
|
|
289 |
39 |
zero_gravi |
uint32_t clock = SYSINFO_CLK; // clock ticks per second
|
290 |
|
|
clock = clock / 1000; // clock ticks per ms
|
291 |
|
|
|
292 |
|
|
uint64_t wait_cycles = ((uint64_t)clock) * ((uint64_t)time_ms);
|
293 |
56 |
zero_gravi |
uint32_t ticks = (uint32_t)(wait_cycles / loop_cycles_c);
|
294 |
39 |
zero_gravi |
|
295 |
56 |
zero_gravi |
asm volatile (" .balign 4 \n" // make sure this is 32-bit aligned
|
296 |
|
|
" __neorv32_cpu_delay_ms_start: \n"
|
297 |
|
|
" beq %[cnt_r], zero, __neorv32_cpu_delay_ms_end \n" // 3 cycles (not taken)
|
298 |
|
|
" beq %[cnt_r], zero, __neorv32_cpu_delay_ms_end \n" // 3 cycles (never taken)
|
299 |
|
|
" addi %[cnt_w], %[cnt_r], -1 \n" // 2 cycles
|
300 |
|
|
" nop \n" // 2 cycles
|
301 |
|
|
" j __neorv32_cpu_delay_ms_start \n" // 6 cycles
|
302 |
|
|
" __neorv32_cpu_delay_ms_end: "
|
303 |
|
|
: [cnt_w] "=r" (ticks) : [cnt_r] "r" (ticks));
|
304 |
2 |
zero_gravi |
}
|
305 |
|
|
|
306 |
15 |
zero_gravi |
|
307 |
|
|
/**********************************************************************//**
|
308 |
|
|
* Switch from privilege mode MACHINE to privilege mode USER.
|
309 |
|
|
*
|
310 |
39 |
zero_gravi |
* @warning This function requires the U extension to be implemented.
|
311 |
15 |
zero_gravi |
**************************************************************************/
|
312 |
|
|
void __attribute__((naked)) neorv32_cpu_goto_user_mode(void) {
|
313 |
|
|
|
314 |
35 |
zero_gravi |
// make sure to use NO registers in here! -> naked
|
315 |
15 |
zero_gravi |
|
316 |
56 |
zero_gravi |
asm volatile ("csrw mepc, ra \n" // move return address to mepc so we can return using "mret". also, we can now use ra as general purpose register in here
|
317 |
|
|
"li ra, %[input_imm] \n" // bit mask to clear the two MPP bits
|
318 |
|
|
"csrrc zero, mstatus, ra \n" // clear MPP bits -> MPP=u-mode
|
319 |
|
|
"mret \n" // return and switch to user mode
|
320 |
42 |
zero_gravi |
: : [input_imm] "i" ((1<<CSR_MSTATUS_MPP_H) | (1<<CSR_MSTATUS_MPP_L)));
|
321 |
15 |
zero_gravi |
}
|
322 |
39 |
zero_gravi |
|
323 |
|
|
|
324 |
|
|
/**********************************************************************//**
|
325 |
42 |
zero_gravi |
* Physical memory protection (PMP): Get number of available regions.
|
326 |
|
|
*
|
327 |
|
|
* @warning This function overrides all available PMPCFG* CSRs.
|
328 |
|
|
* @warning This function requires the PMP CPU extension.
|
329 |
|
|
*
|
330 |
|
|
* @return Returns number of available PMP regions.
|
331 |
|
|
**************************************************************************/
|
332 |
|
|
uint32_t neorv32_cpu_pmp_get_num_regions(void) {
|
333 |
|
|
|
334 |
45 |
zero_gravi |
uint32_t i = 0;
|
335 |
|
|
|
336 |
42 |
zero_gravi |
// try setting R bit in all PMPCFG CSRs
|
337 |
45 |
zero_gravi |
const uint32_t tmp = 0x01010101;
|
338 |
|
|
for (i=0; i<16; i++) {
|
339 |
|
|
__neorv32_cpu_pmp_cfg_write(i, tmp);
|
340 |
|
|
}
|
341 |
42 |
zero_gravi |
|
342 |
|
|
// sum up all written ones (only available PMPCFG* CSRs/entries will return =! 0)
|
343 |
|
|
union {
|
344 |
|
|
uint32_t uint32;
|
345 |
|
|
uint8_t uint8[sizeof(uint32_t)/sizeof(uint8_t)];
|
346 |
|
|
} cnt;
|
347 |
|
|
|
348 |
|
|
cnt.uint32 = 0;
|
349 |
45 |
zero_gravi |
for (i=0; i<16; i++) {
|
350 |
|
|
cnt.uint32 += __neorv32_cpu_pmp_cfg_read(i);
|
351 |
|
|
}
|
352 |
42 |
zero_gravi |
|
353 |
|
|
// sum up bytes
|
354 |
|
|
uint32_t num_regions = 0;
|
355 |
|
|
num_regions += (uint32_t)cnt.uint8[0];
|
356 |
|
|
num_regions += (uint32_t)cnt.uint8[1];
|
357 |
|
|
num_regions += (uint32_t)cnt.uint8[2];
|
358 |
|
|
num_regions += (uint32_t)cnt.uint8[3];
|
359 |
|
|
|
360 |
|
|
return num_regions;
|
361 |
|
|
}
|
362 |
|
|
|
363 |
|
|
|
364 |
|
|
/**********************************************************************//**
|
365 |
40 |
zero_gravi |
* Physical memory protection (PMP): Get minimal region size (granularity).
|
366 |
|
|
*
|
367 |
|
|
* @warning This function overrides PMPCFG0[0] and PMPADDR0 CSRs.
|
368 |
|
|
* @warning This function requires the PMP CPU extension.
|
369 |
|
|
*
|
370 |
42 |
zero_gravi |
* @return Returns minimal region size in bytes.
|
371 |
40 |
zero_gravi |
**************************************************************************/
|
372 |
|
|
uint32_t neorv32_cpu_pmp_get_granularity(void) {
|
373 |
|
|
|
374 |
|
|
// check min granulartiy
|
375 |
|
|
uint32_t tmp = neorv32_cpu_csr_read(CSR_PMPCFG0);
|
376 |
|
|
tmp &= 0xffffff00; // disable entry 0
|
377 |
|
|
neorv32_cpu_csr_write(CSR_PMPCFG0, tmp);
|
378 |
|
|
neorv32_cpu_csr_write(CSR_PMPADDR0, 0xffffffff);
|
379 |
|
|
uint32_t tmp_a = neorv32_cpu_csr_read(CSR_PMPADDR0);
|
380 |
|
|
|
381 |
|
|
uint32_t i;
|
382 |
|
|
|
383 |
|
|
// find least-significat set bit
|
384 |
|
|
for (i=31; i!=0; i--) {
|
385 |
|
|
if (((tmp_a >> i) & 1) == 0) {
|
386 |
|
|
break;
|
387 |
|
|
}
|
388 |
|
|
}
|
389 |
|
|
|
390 |
|
|
return (uint32_t)(1 << (i+1+2));
|
391 |
|
|
}
|
392 |
|
|
|
393 |
|
|
|
394 |
|
|
/**********************************************************************//**
|
395 |
|
|
* Physical memory protection (PMP): Configure region.
|
396 |
|
|
*
|
397 |
|
|
* @note Using NAPOT mode - page base address has to be naturally aligned.
|
398 |
|
|
*
|
399 |
|
|
* @warning This function requires the PMP CPU extension.
|
400 |
42 |
zero_gravi |
* @warning Only use available PMP regions. Check before using neorv32_cpu_pmp_get_regions(void).
|
401 |
40 |
zero_gravi |
*
|
402 |
42 |
zero_gravi |
* @param[in] index Region number (index, 0..PMP_NUM_REGIONS-1).
|
403 |
40 |
zero_gravi |
* @param[in] base Region base address (has to be naturally aligned!).
|
404 |
|
|
* @param[in] size Region size, has to be a power of 2 (min 8 bytes or according to HW's PMP.granularity configuration).
|
405 |
|
|
* @param[in] config Region configuration (attributes) byte (for PMPCFGx).
|
406 |
|
|
* @return Returns 0 on success, 1 on failure.
|
407 |
|
|
**************************************************************************/
|
408 |
|
|
int neorv32_cpu_pmp_configure_region(uint32_t index, uint32_t base, uint32_t size, uint8_t config) {
|
409 |
|
|
|
410 |
|
|
if (size < 8) {
|
411 |
|
|
return 1; // minimal region size is 8 bytes
|
412 |
|
|
}
|
413 |
|
|
|
414 |
|
|
if ((size & (size - 1)) != 0) {
|
415 |
|
|
return 1; // region size is not a power of two
|
416 |
|
|
}
|
417 |
|
|
|
418 |
45 |
zero_gravi |
// pmpcfg register index
|
419 |
|
|
uint32_t pmpcfg_index = index >> 4; // 4 entries per pmpcfg csr
|
420 |
|
|
|
421 |
40 |
zero_gravi |
// setup configuration
|
422 |
|
|
uint32_t tmp;
|
423 |
|
|
uint32_t config_int = ((uint32_t)config) << ((index%4)*8);
|
424 |
|
|
uint32_t config_mask = ((uint32_t)0xFF) << ((index%4)*8);
|
425 |
|
|
config_mask = ~config_mask;
|
426 |
|
|
|
427 |
|
|
// clear old configuration
|
428 |
45 |
zero_gravi |
__neorv32_cpu_pmp_cfg_write(pmpcfg_index, __neorv32_cpu_pmp_cfg_read(pmpcfg_index) & config_mask);
|
429 |
40 |
zero_gravi |
|
430 |
45 |
zero_gravi |
|
431 |
40 |
zero_gravi |
// set base address and region size
|
432 |
|
|
uint32_t addr_mask = ~((size - 1) >> 2);
|
433 |
|
|
uint32_t size_mask = (size - 1) >> 3;
|
434 |
|
|
|
435 |
|
|
tmp = base & addr_mask;
|
436 |
|
|
tmp = tmp | size_mask;
|
437 |
|
|
|
438 |
42 |
zero_gravi |
switch(index & 63) {
|
439 |
|
|
case 0: neorv32_cpu_csr_write(CSR_PMPADDR0, tmp); break;
|
440 |
|
|
case 1: neorv32_cpu_csr_write(CSR_PMPADDR1, tmp); break;
|
441 |
|
|
case 2: neorv32_cpu_csr_write(CSR_PMPADDR2, tmp); break;
|
442 |
|
|
case 3: neorv32_cpu_csr_write(CSR_PMPADDR3, tmp); break;
|
443 |
|
|
case 4: neorv32_cpu_csr_write(CSR_PMPADDR4, tmp); break;
|
444 |
|
|
case 5: neorv32_cpu_csr_write(CSR_PMPADDR5, tmp); break;
|
445 |
|
|
case 6: neorv32_cpu_csr_write(CSR_PMPADDR6, tmp); break;
|
446 |
|
|
case 7: neorv32_cpu_csr_write(CSR_PMPADDR7, tmp); break;
|
447 |
|
|
case 8: neorv32_cpu_csr_write(CSR_PMPADDR8, tmp); break;
|
448 |
|
|
case 9: neorv32_cpu_csr_write(CSR_PMPADDR9, tmp); break;
|
449 |
|
|
case 10: neorv32_cpu_csr_write(CSR_PMPADDR10, tmp); break;
|
450 |
|
|
case 11: neorv32_cpu_csr_write(CSR_PMPADDR11, tmp); break;
|
451 |
|
|
case 12: neorv32_cpu_csr_write(CSR_PMPADDR12, tmp); break;
|
452 |
|
|
case 13: neorv32_cpu_csr_write(CSR_PMPADDR13, tmp); break;
|
453 |
|
|
case 14: neorv32_cpu_csr_write(CSR_PMPADDR14, tmp); break;
|
454 |
|
|
case 15: neorv32_cpu_csr_write(CSR_PMPADDR15, tmp); break;
|
455 |
|
|
case 16: neorv32_cpu_csr_write(CSR_PMPADDR16, tmp); break;
|
456 |
|
|
case 17: neorv32_cpu_csr_write(CSR_PMPADDR17, tmp); break;
|
457 |
|
|
case 18: neorv32_cpu_csr_write(CSR_PMPADDR18, tmp); break;
|
458 |
|
|
case 19: neorv32_cpu_csr_write(CSR_PMPADDR19, tmp); break;
|
459 |
|
|
case 20: neorv32_cpu_csr_write(CSR_PMPADDR20, tmp); break;
|
460 |
|
|
case 21: neorv32_cpu_csr_write(CSR_PMPADDR21, tmp); break;
|
461 |
|
|
case 22: neorv32_cpu_csr_write(CSR_PMPADDR22, tmp); break;
|
462 |
|
|
case 23: neorv32_cpu_csr_write(CSR_PMPADDR23, tmp); break;
|
463 |
|
|
case 24: neorv32_cpu_csr_write(CSR_PMPADDR24, tmp); break;
|
464 |
|
|
case 25: neorv32_cpu_csr_write(CSR_PMPADDR25, tmp); break;
|
465 |
|
|
case 26: neorv32_cpu_csr_write(CSR_PMPADDR26, tmp); break;
|
466 |
|
|
case 27: neorv32_cpu_csr_write(CSR_PMPADDR27, tmp); break;
|
467 |
|
|
case 28: neorv32_cpu_csr_write(CSR_PMPADDR28, tmp); break;
|
468 |
|
|
case 29: neorv32_cpu_csr_write(CSR_PMPADDR29, tmp); break;
|
469 |
|
|
case 30: neorv32_cpu_csr_write(CSR_PMPADDR30, tmp); break;
|
470 |
|
|
case 31: neorv32_cpu_csr_write(CSR_PMPADDR31, tmp); break;
|
471 |
|
|
case 32: neorv32_cpu_csr_write(CSR_PMPADDR32, tmp); break;
|
472 |
|
|
case 33: neorv32_cpu_csr_write(CSR_PMPADDR33, tmp); break;
|
473 |
|
|
case 34: neorv32_cpu_csr_write(CSR_PMPADDR34, tmp); break;
|
474 |
|
|
case 35: neorv32_cpu_csr_write(CSR_PMPADDR35, tmp); break;
|
475 |
|
|
case 36: neorv32_cpu_csr_write(CSR_PMPADDR36, tmp); break;
|
476 |
|
|
case 37: neorv32_cpu_csr_write(CSR_PMPADDR37, tmp); break;
|
477 |
|
|
case 38: neorv32_cpu_csr_write(CSR_PMPADDR38, tmp); break;
|
478 |
|
|
case 39: neorv32_cpu_csr_write(CSR_PMPADDR39, tmp); break;
|
479 |
|
|
case 40: neorv32_cpu_csr_write(CSR_PMPADDR40, tmp); break;
|
480 |
|
|
case 41: neorv32_cpu_csr_write(CSR_PMPADDR41, tmp); break;
|
481 |
|
|
case 42: neorv32_cpu_csr_write(CSR_PMPADDR42, tmp); break;
|
482 |
|
|
case 43: neorv32_cpu_csr_write(CSR_PMPADDR43, tmp); break;
|
483 |
|
|
case 44: neorv32_cpu_csr_write(CSR_PMPADDR44, tmp); break;
|
484 |
|
|
case 45: neorv32_cpu_csr_write(CSR_PMPADDR45, tmp); break;
|
485 |
|
|
case 46: neorv32_cpu_csr_write(CSR_PMPADDR46, tmp); break;
|
486 |
|
|
case 47: neorv32_cpu_csr_write(CSR_PMPADDR47, tmp); break;
|
487 |
|
|
case 48: neorv32_cpu_csr_write(CSR_PMPADDR48, tmp); break;
|
488 |
|
|
case 49: neorv32_cpu_csr_write(CSR_PMPADDR49, tmp); break;
|
489 |
|
|
case 50: neorv32_cpu_csr_write(CSR_PMPADDR50, tmp); break;
|
490 |
|
|
case 51: neorv32_cpu_csr_write(CSR_PMPADDR51, tmp); break;
|
491 |
|
|
case 52: neorv32_cpu_csr_write(CSR_PMPADDR52, tmp); break;
|
492 |
|
|
case 53: neorv32_cpu_csr_write(CSR_PMPADDR53, tmp); break;
|
493 |
|
|
case 54: neorv32_cpu_csr_write(CSR_PMPADDR54, tmp); break;
|
494 |
|
|
case 55: neorv32_cpu_csr_write(CSR_PMPADDR55, tmp); break;
|
495 |
|
|
case 56: neorv32_cpu_csr_write(CSR_PMPADDR56, tmp); break;
|
496 |
|
|
case 57: neorv32_cpu_csr_write(CSR_PMPADDR57, tmp); break;
|
497 |
|
|
case 58: neorv32_cpu_csr_write(CSR_PMPADDR58, tmp); break;
|
498 |
|
|
case 59: neorv32_cpu_csr_write(CSR_PMPADDR59, tmp); break;
|
499 |
|
|
case 60: neorv32_cpu_csr_write(CSR_PMPADDR60, tmp); break;
|
500 |
|
|
case 61: neorv32_cpu_csr_write(CSR_PMPADDR61, tmp); break;
|
501 |
|
|
case 62: neorv32_cpu_csr_write(CSR_PMPADDR62, tmp); break;
|
502 |
|
|
case 63: neorv32_cpu_csr_write(CSR_PMPADDR63, tmp); break;
|
503 |
40 |
zero_gravi |
default: break;
|
504 |
|
|
}
|
505 |
|
|
|
506 |
42 |
zero_gravi |
// wait for HW to compute PMP-internal stuff (address masks)
|
507 |
40 |
zero_gravi |
for (tmp=0; tmp<16; tmp++) {
|
508 |
|
|
asm volatile ("nop");
|
509 |
|
|
}
|
510 |
|
|
|
511 |
|
|
// set new configuration
|
512 |
45 |
zero_gravi |
__neorv32_cpu_pmp_cfg_write(pmpcfg_index, __neorv32_cpu_pmp_cfg_read(pmpcfg_index) | config_int);
|
513 |
|
|
|
514 |
|
|
return 0;
|
515 |
|
|
}
|
516 |
|
|
|
517 |
|
|
|
518 |
|
|
/**********************************************************************//**
|
519 |
|
|
* Internal helper function: Read PMP configuration register 0..15
|
520 |
|
|
*
|
521 |
|
|
* @warning This function requires the PMP CPU extension.
|
522 |
|
|
*
|
523 |
|
|
* @param[in] index PMP CFG configuration register ID (0..15).
|
524 |
|
|
* @return PMP CFG read data.
|
525 |
|
|
**************************************************************************/
|
526 |
|
|
static uint32_t __neorv32_cpu_pmp_cfg_read(uint32_t index) {
|
527 |
|
|
|
528 |
|
|
uint32_t tmp = 0;
|
529 |
42 |
zero_gravi |
switch(index & 15) {
|
530 |
45 |
zero_gravi |
case 0: tmp = neorv32_cpu_csr_read(CSR_PMPCFG0); break;
|
531 |
|
|
case 1: tmp = neorv32_cpu_csr_read(CSR_PMPCFG1); break;
|
532 |
|
|
case 2: tmp = neorv32_cpu_csr_read(CSR_PMPCFG2); break;
|
533 |
|
|
case 3: tmp = neorv32_cpu_csr_read(CSR_PMPCFG3); break;
|
534 |
|
|
case 4: tmp = neorv32_cpu_csr_read(CSR_PMPCFG4); break;
|
535 |
|
|
case 5: tmp = neorv32_cpu_csr_read(CSR_PMPCFG5); break;
|
536 |
|
|
case 6: tmp = neorv32_cpu_csr_read(CSR_PMPCFG6); break;
|
537 |
|
|
case 7: tmp = neorv32_cpu_csr_read(CSR_PMPCFG7); break;
|
538 |
|
|
case 8: tmp = neorv32_cpu_csr_read(CSR_PMPCFG8); break;
|
539 |
|
|
case 9: tmp = neorv32_cpu_csr_read(CSR_PMPCFG9); break;
|
540 |
|
|
case 10: tmp = neorv32_cpu_csr_read(CSR_PMPCFG10); break;
|
541 |
|
|
case 11: tmp = neorv32_cpu_csr_read(CSR_PMPCFG11); break;
|
542 |
|
|
case 12: tmp = neorv32_cpu_csr_read(CSR_PMPCFG12); break;
|
543 |
|
|
case 13: tmp = neorv32_cpu_csr_read(CSR_PMPCFG13); break;
|
544 |
|
|
case 14: tmp = neorv32_cpu_csr_read(CSR_PMPCFG14); break;
|
545 |
|
|
case 15: tmp = neorv32_cpu_csr_read(CSR_PMPCFG15); break;
|
546 |
42 |
zero_gravi |
default: break;
|
547 |
40 |
zero_gravi |
}
|
548 |
|
|
|
549 |
45 |
zero_gravi |
return tmp;
|
550 |
40 |
zero_gravi |
}
|
551 |
42 |
zero_gravi |
|
552 |
|
|
|
553 |
|
|
/**********************************************************************//**
|
554 |
45 |
zero_gravi |
* Internal helper function: Write PMP configuration register 0..15
|
555 |
|
|
*
|
556 |
|
|
* @warning This function requires the PMP CPU extension.
|
557 |
|
|
*
|
558 |
|
|
* @param[in] index PMP CFG configuration register ID (0..15).
|
559 |
|
|
* @param[in] data PMP CFG write data.
|
560 |
|
|
**************************************************************************/
|
561 |
|
|
static void __neorv32_cpu_pmp_cfg_write(uint32_t index, uint32_t data) {
|
562 |
|
|
|
563 |
|
|
switch(index & 15) {
|
564 |
|
|
case 0: neorv32_cpu_csr_write(CSR_PMPCFG0, data); break;
|
565 |
|
|
case 1: neorv32_cpu_csr_write(CSR_PMPCFG1, data); break;
|
566 |
|
|
case 2: neorv32_cpu_csr_write(CSR_PMPCFG2, data); break;
|
567 |
|
|
case 3: neorv32_cpu_csr_write(CSR_PMPCFG3, data); break;
|
568 |
|
|
case 4: neorv32_cpu_csr_write(CSR_PMPCFG4, data); break;
|
569 |
|
|
case 5: neorv32_cpu_csr_write(CSR_PMPCFG5, data); break;
|
570 |
|
|
case 6: neorv32_cpu_csr_write(CSR_PMPCFG6, data); break;
|
571 |
|
|
case 7: neorv32_cpu_csr_write(CSR_PMPCFG7, data); break;
|
572 |
|
|
case 8: neorv32_cpu_csr_write(CSR_PMPCFG8, data); break;
|
573 |
|
|
case 9: neorv32_cpu_csr_write(CSR_PMPCFG9, data); break;
|
574 |
|
|
case 10: neorv32_cpu_csr_write(CSR_PMPCFG10, data); break;
|
575 |
|
|
case 11: neorv32_cpu_csr_write(CSR_PMPCFG11, data); break;
|
576 |
|
|
case 12: neorv32_cpu_csr_write(CSR_PMPCFG12, data); break;
|
577 |
|
|
case 13: neorv32_cpu_csr_write(CSR_PMPCFG13, data); break;
|
578 |
|
|
case 14: neorv32_cpu_csr_write(CSR_PMPCFG14, data); break;
|
579 |
|
|
case 15: neorv32_cpu_csr_write(CSR_PMPCFG15, data); break;
|
580 |
|
|
default: break;
|
581 |
|
|
}
|
582 |
|
|
}
|
583 |
|
|
|
584 |
|
|
|
585 |
|
|
/**********************************************************************//**
|
586 |
42 |
zero_gravi |
* Hardware performance monitors (HPM): Get number of available HPM counters.
|
587 |
|
|
*
|
588 |
|
|
* @warning This function overrides all available mhpmcounter* CSRs.
|
589 |
|
|
*
|
590 |
|
|
* @return Returns number of available HPM counters (..29).
|
591 |
|
|
**************************************************************************/
|
592 |
|
|
uint32_t neorv32_cpu_hpm_get_counters(void) {
|
593 |
|
|
|
594 |
56 |
zero_gravi |
// inhibit all HPM counters
|
595 |
|
|
uint32_t tmp = neorv32_cpu_csr_read(CSR_MCOUNTINHIBIT);
|
596 |
|
|
tmp |= 0xfffffff8;
|
597 |
|
|
neorv32_cpu_csr_write(CSR_MCOUNTINHIBIT, tmp);
|
598 |
|
|
|
599 |
42 |
zero_gravi |
// try setting all mhpmcounter* CSRs to 1
|
600 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER3, 1);
|
601 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER4, 1);
|
602 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER5, 1);
|
603 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER6, 1);
|
604 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER7, 1);
|
605 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER8, 1);
|
606 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER9, 1);
|
607 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER10, 1);
|
608 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER11, 1);
|
609 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER12, 1);
|
610 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER13, 1);
|
611 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER14, 1);
|
612 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER15, 1);
|
613 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER16, 1);
|
614 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER17, 1);
|
615 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER18, 1);
|
616 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER19, 1);
|
617 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER20, 1);
|
618 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER21, 1);
|
619 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER22, 1);
|
620 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER23, 1);
|
621 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER24, 1);
|
622 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER25, 1);
|
623 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER26, 1);
|
624 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER27, 1);
|
625 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER28, 1);
|
626 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER29, 1);
|
627 |
|
|
|
628 |
56 |
zero_gravi |
// sum up all written ones (only available HPM counter CSRs will return =! 0)
|
629 |
42 |
zero_gravi |
uint32_t num_hpm_cnts = 0;
|
630 |
|
|
|
631 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER3);
|
632 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER4);
|
633 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER5);
|
634 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER6);
|
635 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER7);
|
636 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER8);
|
637 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER9);
|
638 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER10);
|
639 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER11);
|
640 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER12);
|
641 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER13);
|
642 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER14);
|
643 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER15);
|
644 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER16);
|
645 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER17);
|
646 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER18);
|
647 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER19);
|
648 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER20);
|
649 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER21);
|
650 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER22);
|
651 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER23);
|
652 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER24);
|
653 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER25);
|
654 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER26);
|
655 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER27);
|
656 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER28);
|
657 |
|
|
num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER29);
|
658 |
|
|
|
659 |
|
|
return num_hpm_cnts;
|
660 |
|
|
}
|
661 |
55 |
zero_gravi |
|
662 |
|
|
|
663 |
|
|
/**********************************************************************//**
|
664 |
56 |
zero_gravi |
* Hardware performance monitors (HPM): Get total counter width
|
665 |
|
|
*
|
666 |
|
|
* @warning This function overrides mhpmcounter3[h] CSRs.
|
667 |
|
|
*
|
668 |
|
|
* @return Size of HPM counter bits (1-64).
|
669 |
|
|
**************************************************************************/
|
670 |
|
|
uint32_t neorv32_cpu_hpm_get_size(void) {
|
671 |
|
|
|
672 |
|
|
// inhibt auto-update
|
673 |
|
|
asm volatile ("csrwi %[addr], %[imm]" : : [addr] "i" (CSR_MCOUNTINHIBIT), [imm] "i" (1<<CSR_MCOUNTEREN_HPM3));
|
674 |
|
|
|
675 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER3, 0xffffffff);
|
676 |
|
|
neorv32_cpu_csr_write(CSR_MHPMCOUNTER3H, 0xffffffff);
|
677 |
|
|
|
678 |
|
|
uint32_t tmp, size, i;
|
679 |
|
|
|
680 |
|
|
if (neorv32_cpu_csr_read(CSR_MHPMCOUNTER3H) == 0) {
|
681 |
|
|
size = 0;
|
682 |
|
|
tmp = neorv32_cpu_csr_read(CSR_MHPMCOUNTER3);
|
683 |
|
|
}
|
684 |
|
|
else {
|
685 |
|
|
size = 32;
|
686 |
|
|
tmp = neorv32_cpu_csr_read(CSR_MHPMCOUNTER3H);
|
687 |
|
|
}
|
688 |
|
|
|
689 |
|
|
for (i=0; i<32; i++) {
|
690 |
|
|
if (tmp & (1<<i)) {
|
691 |
|
|
size++;
|
692 |
|
|
}
|
693 |
|
|
}
|
694 |
|
|
|
695 |
|
|
return size;
|
696 |
|
|
}
|
697 |
|
|
|
698 |
|
|
|
699 |
|
|
/**********************************************************************//**
|
700 |
55 |
zero_gravi |
* Check if certain Z* extension is available
|
701 |
|
|
*
|
702 |
|
|
* @param[in] flag Index of the Z-extension to check from #NEORV32_CSR_MZEXT_enum
|
703 |
|
|
* @return 0 if extension is NOT available, != 0 if extension is available.
|
704 |
|
|
**************************************************************************/
|
705 |
|
|
int neorv32_check_zextension(uint32_t flag) {
|
706 |
|
|
|
707 |
|
|
// check if out of range
|
708 |
|
|
if (flag > 31) {
|
709 |
|
|
return 0;
|
710 |
|
|
}
|
711 |
|
|
|
712 |
|
|
uint32_t tmp = neorv32_cpu_csr_read(CSR_MZEXT);
|
713 |
|
|
if ((tmp & (1 << flag)) == 0) {
|
714 |
|
|
return 0;
|
715 |
|
|
}
|
716 |
|
|
else {
|
717 |
|
|
return 1;
|
718 |
|
|
}
|
719 |
|
|
}
|