OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [lib/] [source/] [neorv32_cpu.c] - Blame information for rev 56

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
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
 * Atomic compare-and-swap operation (for implemeneting semaphores and mutexes).
326
 *
327
 * @warning This function requires the A (atomic) CPU extension.
328
 *
329
 * @param[in] addr Address of memory location.
330
 * @param[in] expected Expected value (for comparison).
331
 * @param[in] desired Desired value (new value).
332
 * @return Returns 0 on success, 1 on failure.
333
 **************************************************************************/
334
int __attribute__ ((noinline)) neorv32_cpu_atomic_cas(uint32_t addr, uint32_t expected, uint32_t desired) {
335
#ifdef __riscv_atomic
336
 
337
  register uint32_t addr_reg = addr;
338
  register uint32_t des_reg = desired;
339
  register uint32_t tmp_reg;
340
 
341
  // load original value + reservation (lock)
342
  asm volatile ("lr.w %[result], (%[input])" : [result] "=r" (tmp_reg) : [input] "r" (addr_reg));
343
 
344
  if (tmp_reg != expected) {
345
    asm volatile ("lw x0, 0(%[input])" : : [input] "r" (addr_reg)); // clear reservation lock
346
    return 1;
347
  }
348
 
349
  // store-conditional
350
  asm volatile ("sc.w %[result], %[input_i], (%[input_j])" : [result] "=r" (tmp_reg) : [input_i] "r" (des_reg), [input_j] "r" (addr_reg));
351
 
352
  if (tmp_reg) {
353
    return 1;
354
  }
355
 
356
  return 0;
357
#else
358 45 zero_gravi
  return 1; // A extension not implemented - function always fails
359 39 zero_gravi
#endif
360
}
361 40 zero_gravi
 
362
 
363
/**********************************************************************//**
364 42 zero_gravi
 * Physical memory protection (PMP): Get number of available regions.
365
 *
366
 * @warning This function overrides all available PMPCFG* CSRs.
367
 * @warning This function requires the PMP CPU extension.
368
 *
369
 * @return Returns number of available PMP regions.
370
 **************************************************************************/
371
uint32_t neorv32_cpu_pmp_get_num_regions(void) {
372
 
373 45 zero_gravi
  uint32_t i = 0;
374
 
375 42 zero_gravi
  // try setting R bit in all PMPCFG CSRs
376 45 zero_gravi
  const uint32_t tmp = 0x01010101;
377
  for (i=0; i<16; i++) {
378
    __neorv32_cpu_pmp_cfg_write(i, tmp);
379
  }
380 42 zero_gravi
 
381
  // sum up all written ones (only available PMPCFG* CSRs/entries will return =! 0)
382
  union {
383
    uint32_t uint32;
384
    uint8_t  uint8[sizeof(uint32_t)/sizeof(uint8_t)];
385
  } cnt;
386
 
387
  cnt.uint32 = 0;
388 45 zero_gravi
  for (i=0; i<16; i++) {
389
    cnt.uint32 += __neorv32_cpu_pmp_cfg_read(i);
390
  }
391 42 zero_gravi
 
392
  // sum up bytes
393
  uint32_t num_regions = 0;
394
  num_regions += (uint32_t)cnt.uint8[0];
395
  num_regions += (uint32_t)cnt.uint8[1];
396
  num_regions += (uint32_t)cnt.uint8[2];
397
  num_regions += (uint32_t)cnt.uint8[3];
398
 
399
  return num_regions;
400
}
401
 
402
 
403
/**********************************************************************//**
404 40 zero_gravi
 * Physical memory protection (PMP): Get minimal region size (granularity).
405
 *
406
 * @warning This function overrides PMPCFG0[0] and PMPADDR0 CSRs.
407
 * @warning This function requires the PMP CPU extension.
408
 *
409 42 zero_gravi
 * @return Returns minimal region size in bytes.
410 40 zero_gravi
 **************************************************************************/
411
uint32_t neorv32_cpu_pmp_get_granularity(void) {
412
 
413
  // check min granulartiy
414
  uint32_t tmp = neorv32_cpu_csr_read(CSR_PMPCFG0);
415
  tmp &= 0xffffff00; // disable entry 0
416
  neorv32_cpu_csr_write(CSR_PMPCFG0, tmp);
417
  neorv32_cpu_csr_write(CSR_PMPADDR0, 0xffffffff);
418
  uint32_t tmp_a = neorv32_cpu_csr_read(CSR_PMPADDR0);
419
 
420
  uint32_t i;
421
 
422
  // find least-significat set bit
423
  for (i=31; i!=0; i--) {
424
    if (((tmp_a >> i) & 1) == 0) {
425
      break;
426
    }
427
  }
428
 
429
  return (uint32_t)(1 << (i+1+2));
430
}
431
 
432
 
433
/**********************************************************************//**
434
 * Physical memory protection (PMP): Configure region.
435
 *
436
 * @note Using NAPOT mode - page base address has to be naturally aligned.
437
 *
438
 * @warning This function requires the PMP CPU extension.
439 42 zero_gravi
 * @warning Only use available PMP regions. Check before using neorv32_cpu_pmp_get_regions(void).
440 40 zero_gravi
 *
441 42 zero_gravi
 * @param[in] index Region number (index, 0..PMP_NUM_REGIONS-1).
442 40 zero_gravi
 * @param[in] base Region base address (has to be naturally aligned!).
443
 * @param[in] size Region size, has to be a power of 2 (min 8 bytes or according to HW's PMP.granularity configuration).
444
 * @param[in] config Region configuration (attributes) byte (for PMPCFGx).
445
 * @return Returns 0 on success, 1 on failure.
446
 **************************************************************************/
447
int neorv32_cpu_pmp_configure_region(uint32_t index, uint32_t base, uint32_t size, uint8_t config) {
448
 
449
  if (size < 8) {
450
    return 1; // minimal region size is 8 bytes
451
  }
452
 
453
  if ((size & (size - 1)) != 0) {
454
    return 1; // region size is not a power of two
455
  }
456
 
457 45 zero_gravi
  // pmpcfg register index
458
  uint32_t pmpcfg_index = index >> 4; // 4 entries per pmpcfg csr
459
 
460 40 zero_gravi
  // setup configuration
461
  uint32_t tmp;
462
  uint32_t config_int  = ((uint32_t)config) << ((index%4)*8);
463
  uint32_t config_mask = ((uint32_t)0xFF)   << ((index%4)*8);
464
  config_mask = ~config_mask;
465
 
466
  // clear old configuration
467 45 zero_gravi
  __neorv32_cpu_pmp_cfg_write(pmpcfg_index, __neorv32_cpu_pmp_cfg_read(pmpcfg_index) & config_mask);
468 40 zero_gravi
 
469 45 zero_gravi
 
470 40 zero_gravi
  // set base address and region size
471
  uint32_t addr_mask = ~((size - 1) >> 2);
472
  uint32_t size_mask = (size - 1) >> 3;
473
 
474
  tmp = base & addr_mask;
475
  tmp = tmp | size_mask;
476
 
477 42 zero_gravi
  switch(index & 63) {
478
    case 0:  neorv32_cpu_csr_write(CSR_PMPADDR0,  tmp); break;
479
    case 1:  neorv32_cpu_csr_write(CSR_PMPADDR1,  tmp); break;
480
    case 2:  neorv32_cpu_csr_write(CSR_PMPADDR2,  tmp); break;
481
    case 3:  neorv32_cpu_csr_write(CSR_PMPADDR3,  tmp); break;
482
    case 4:  neorv32_cpu_csr_write(CSR_PMPADDR4,  tmp); break;
483
    case 5:  neorv32_cpu_csr_write(CSR_PMPADDR5,  tmp); break;
484
    case 6:  neorv32_cpu_csr_write(CSR_PMPADDR6,  tmp); break;
485
    case 7:  neorv32_cpu_csr_write(CSR_PMPADDR7,  tmp); break;
486
    case 8:  neorv32_cpu_csr_write(CSR_PMPADDR8,  tmp); break;
487
    case 9:  neorv32_cpu_csr_write(CSR_PMPADDR9,  tmp); break;
488
    case 10: neorv32_cpu_csr_write(CSR_PMPADDR10, tmp); break;
489
    case 11: neorv32_cpu_csr_write(CSR_PMPADDR11, tmp); break;
490
    case 12: neorv32_cpu_csr_write(CSR_PMPADDR12, tmp); break;
491
    case 13: neorv32_cpu_csr_write(CSR_PMPADDR13, tmp); break;
492
    case 14: neorv32_cpu_csr_write(CSR_PMPADDR14, tmp); break;
493
    case 15: neorv32_cpu_csr_write(CSR_PMPADDR15, tmp); break;
494
    case 16: neorv32_cpu_csr_write(CSR_PMPADDR16, tmp); break;
495
    case 17: neorv32_cpu_csr_write(CSR_PMPADDR17, tmp); break;
496
    case 18: neorv32_cpu_csr_write(CSR_PMPADDR18, tmp); break;
497
    case 19: neorv32_cpu_csr_write(CSR_PMPADDR19, tmp); break;
498
    case 20: neorv32_cpu_csr_write(CSR_PMPADDR20, tmp); break;
499
    case 21: neorv32_cpu_csr_write(CSR_PMPADDR21, tmp); break;
500
    case 22: neorv32_cpu_csr_write(CSR_PMPADDR22, tmp); break;
501
    case 23: neorv32_cpu_csr_write(CSR_PMPADDR23, tmp); break;
502
    case 24: neorv32_cpu_csr_write(CSR_PMPADDR24, tmp); break;
503
    case 25: neorv32_cpu_csr_write(CSR_PMPADDR25, tmp); break;
504
    case 26: neorv32_cpu_csr_write(CSR_PMPADDR26, tmp); break;
505
    case 27: neorv32_cpu_csr_write(CSR_PMPADDR27, tmp); break;
506
    case 28: neorv32_cpu_csr_write(CSR_PMPADDR28, tmp); break;
507
    case 29: neorv32_cpu_csr_write(CSR_PMPADDR29, tmp); break;
508
    case 30: neorv32_cpu_csr_write(CSR_PMPADDR30, tmp); break;
509
    case 31: neorv32_cpu_csr_write(CSR_PMPADDR31, tmp); break;
510
    case 32: neorv32_cpu_csr_write(CSR_PMPADDR32, tmp); break;
511
    case 33: neorv32_cpu_csr_write(CSR_PMPADDR33, tmp); break;
512
    case 34: neorv32_cpu_csr_write(CSR_PMPADDR34, tmp); break;
513
    case 35: neorv32_cpu_csr_write(CSR_PMPADDR35, tmp); break;
514
    case 36: neorv32_cpu_csr_write(CSR_PMPADDR36, tmp); break;
515
    case 37: neorv32_cpu_csr_write(CSR_PMPADDR37, tmp); break;
516
    case 38: neorv32_cpu_csr_write(CSR_PMPADDR38, tmp); break;
517
    case 39: neorv32_cpu_csr_write(CSR_PMPADDR39, tmp); break;
518
    case 40: neorv32_cpu_csr_write(CSR_PMPADDR40, tmp); break;
519
    case 41: neorv32_cpu_csr_write(CSR_PMPADDR41, tmp); break;
520
    case 42: neorv32_cpu_csr_write(CSR_PMPADDR42, tmp); break;
521
    case 43: neorv32_cpu_csr_write(CSR_PMPADDR43, tmp); break;
522
    case 44: neorv32_cpu_csr_write(CSR_PMPADDR44, tmp); break;
523
    case 45: neorv32_cpu_csr_write(CSR_PMPADDR45, tmp); break;
524
    case 46: neorv32_cpu_csr_write(CSR_PMPADDR46, tmp); break;
525
    case 47: neorv32_cpu_csr_write(CSR_PMPADDR47, tmp); break;
526
    case 48: neorv32_cpu_csr_write(CSR_PMPADDR48, tmp); break;
527
    case 49: neorv32_cpu_csr_write(CSR_PMPADDR49, tmp); break;
528
    case 50: neorv32_cpu_csr_write(CSR_PMPADDR50, tmp); break;
529
    case 51: neorv32_cpu_csr_write(CSR_PMPADDR51, tmp); break;
530
    case 52: neorv32_cpu_csr_write(CSR_PMPADDR52, tmp); break;
531
    case 53: neorv32_cpu_csr_write(CSR_PMPADDR53, tmp); break;
532
    case 54: neorv32_cpu_csr_write(CSR_PMPADDR54, tmp); break;
533
    case 55: neorv32_cpu_csr_write(CSR_PMPADDR55, tmp); break;
534
    case 56: neorv32_cpu_csr_write(CSR_PMPADDR56, tmp); break;
535
    case 57: neorv32_cpu_csr_write(CSR_PMPADDR57, tmp); break;
536
    case 58: neorv32_cpu_csr_write(CSR_PMPADDR58, tmp); break;
537
    case 59: neorv32_cpu_csr_write(CSR_PMPADDR59, tmp); break;
538
    case 60: neorv32_cpu_csr_write(CSR_PMPADDR60, tmp); break;
539
    case 61: neorv32_cpu_csr_write(CSR_PMPADDR61, tmp); break;
540
    case 62: neorv32_cpu_csr_write(CSR_PMPADDR62, tmp); break;
541
    case 63: neorv32_cpu_csr_write(CSR_PMPADDR63, tmp); break;
542 40 zero_gravi
    default: break;
543
  }
544
 
545 42 zero_gravi
  // wait for HW to compute PMP-internal stuff (address masks)
546 40 zero_gravi
  for (tmp=0; tmp<16; tmp++) {
547
    asm volatile ("nop");
548
  }
549
 
550
  // set new configuration
551 45 zero_gravi
  __neorv32_cpu_pmp_cfg_write(pmpcfg_index, __neorv32_cpu_pmp_cfg_read(pmpcfg_index) | config_int);
552
 
553
  return 0;
554
}
555
 
556
 
557
/**********************************************************************//**
558
 * Internal helper function: Read PMP configuration register 0..15
559
 *
560
 * @warning This function requires the PMP CPU extension.
561
 *
562
 * @param[in] index PMP CFG configuration register ID (0..15).
563
 * @return PMP CFG read data.
564
 **************************************************************************/
565
static uint32_t __neorv32_cpu_pmp_cfg_read(uint32_t index) {
566
 
567
  uint32_t tmp = 0;
568 42 zero_gravi
  switch(index & 15) {
569 45 zero_gravi
    case 0:  tmp = neorv32_cpu_csr_read(CSR_PMPCFG0);  break;
570
    case 1:  tmp = neorv32_cpu_csr_read(CSR_PMPCFG1);  break;
571
    case 2:  tmp = neorv32_cpu_csr_read(CSR_PMPCFG2);  break;
572
    case 3:  tmp = neorv32_cpu_csr_read(CSR_PMPCFG3);  break;
573
    case 4:  tmp = neorv32_cpu_csr_read(CSR_PMPCFG4);  break;
574
    case 5:  tmp = neorv32_cpu_csr_read(CSR_PMPCFG5);  break;
575
    case 6:  tmp = neorv32_cpu_csr_read(CSR_PMPCFG6);  break;
576
    case 7:  tmp = neorv32_cpu_csr_read(CSR_PMPCFG7);  break;
577
    case 8:  tmp = neorv32_cpu_csr_read(CSR_PMPCFG8);  break;
578
    case 9:  tmp = neorv32_cpu_csr_read(CSR_PMPCFG9);  break;
579
    case 10: tmp = neorv32_cpu_csr_read(CSR_PMPCFG10); break;
580
    case 11: tmp = neorv32_cpu_csr_read(CSR_PMPCFG11); break;
581
    case 12: tmp = neorv32_cpu_csr_read(CSR_PMPCFG12); break;
582
    case 13: tmp = neorv32_cpu_csr_read(CSR_PMPCFG13); break;
583
    case 14: tmp = neorv32_cpu_csr_read(CSR_PMPCFG14); break;
584
    case 15: tmp = neorv32_cpu_csr_read(CSR_PMPCFG15); break;
585 42 zero_gravi
    default: break;
586 40 zero_gravi
  }
587
 
588 45 zero_gravi
  return tmp;
589 40 zero_gravi
}
590 42 zero_gravi
 
591
 
592
/**********************************************************************//**
593 45 zero_gravi
 * Internal helper function: Write PMP configuration register 0..15
594
 *
595
 * @warning This function requires the PMP CPU extension.
596
 *
597
 * @param[in] index PMP CFG configuration register ID (0..15).
598
 * @param[in] data PMP CFG write data.
599
 **************************************************************************/
600
static void __neorv32_cpu_pmp_cfg_write(uint32_t index, uint32_t data) {
601
 
602
  switch(index & 15) {
603
    case 0:  neorv32_cpu_csr_write(CSR_PMPCFG0,  data); break;
604
    case 1:  neorv32_cpu_csr_write(CSR_PMPCFG1,  data); break;
605
    case 2:  neorv32_cpu_csr_write(CSR_PMPCFG2,  data); break;
606
    case 3:  neorv32_cpu_csr_write(CSR_PMPCFG3,  data); break;
607
    case 4:  neorv32_cpu_csr_write(CSR_PMPCFG4,  data); break;
608
    case 5:  neorv32_cpu_csr_write(CSR_PMPCFG5,  data); break;
609
    case 6:  neorv32_cpu_csr_write(CSR_PMPCFG6,  data); break;
610
    case 7:  neorv32_cpu_csr_write(CSR_PMPCFG7,  data); break;
611
    case 8:  neorv32_cpu_csr_write(CSR_PMPCFG8,  data); break;
612
    case 9:  neorv32_cpu_csr_write(CSR_PMPCFG9,  data); break;
613
    case 10: neorv32_cpu_csr_write(CSR_PMPCFG10, data); break;
614
    case 11: neorv32_cpu_csr_write(CSR_PMPCFG11, data); break;
615
    case 12: neorv32_cpu_csr_write(CSR_PMPCFG12, data); break;
616
    case 13: neorv32_cpu_csr_write(CSR_PMPCFG13, data); break;
617
    case 14: neorv32_cpu_csr_write(CSR_PMPCFG14, data); break;
618
    case 15: neorv32_cpu_csr_write(CSR_PMPCFG15, data); break;
619
    default: break;
620
  }
621
}
622
 
623
 
624
/**********************************************************************//**
625 42 zero_gravi
 * Hardware performance monitors (HPM): Get number of available HPM counters.
626
 *
627
 * @warning This function overrides all available mhpmcounter* CSRs.
628
 *
629
 * @return Returns number of available HPM counters (..29).
630
 **************************************************************************/
631
uint32_t neorv32_cpu_hpm_get_counters(void) {
632
 
633 56 zero_gravi
  // inhibit all HPM counters
634
  uint32_t tmp = neorv32_cpu_csr_read(CSR_MCOUNTINHIBIT);
635
  tmp |= 0xfffffff8;
636
  neorv32_cpu_csr_write(CSR_MCOUNTINHIBIT, tmp);
637
 
638 42 zero_gravi
  // try setting all mhpmcounter* CSRs to 1
639
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER3,  1);
640
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER4,  1);
641
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER5,  1);
642
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER6,  1);
643
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER7,  1);
644
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER8,  1);
645
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER9,  1);
646
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER10, 1);
647
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER11, 1);
648
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER12, 1);
649
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER13, 1);
650
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER14, 1);
651
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER15, 1);
652
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER16, 1);
653
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER17, 1);
654
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER18, 1);
655
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER19, 1);
656
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER20, 1);
657
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER21, 1);
658
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER22, 1);
659
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER23, 1);
660
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER24, 1);
661
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER25, 1);
662
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER26, 1);
663
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER27, 1);
664
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER28, 1);
665
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER29, 1);
666
 
667 56 zero_gravi
  // sum up all written ones (only available HPM counter CSRs will return =! 0)
668 42 zero_gravi
  uint32_t num_hpm_cnts = 0;
669
 
670
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER3);
671
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER4);
672
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER5);
673
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER6);
674
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER7);
675
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER8);
676
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER9);
677
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER10);
678
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER11);
679
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER12);
680
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER13);
681
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER14);
682
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER15);
683
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER16);
684
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER17);
685
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER18);
686
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER19);
687
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER20);
688
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER21);
689
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER22);
690
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER23);
691
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER24);
692
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER25);
693
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER26);
694
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER27);
695
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER28);
696
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER29);
697
 
698
  return num_hpm_cnts;
699
}
700 55 zero_gravi
 
701
 
702
/**********************************************************************//**
703 56 zero_gravi
 * Hardware performance monitors (HPM): Get total counter width
704
 *
705
 * @warning This function overrides mhpmcounter3[h] CSRs.
706
 *
707
 * @return Size of HPM counter bits (1-64).
708
 **************************************************************************/
709
uint32_t neorv32_cpu_hpm_get_size(void) {
710
 
711
  // inhibt auto-update
712
  asm volatile ("csrwi %[addr], %[imm]" : : [addr] "i" (CSR_MCOUNTINHIBIT), [imm] "i" (1<<CSR_MCOUNTEREN_HPM3));
713
 
714
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER3,  0xffffffff);
715
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER3H, 0xffffffff);
716
 
717
  uint32_t tmp, size, i;
718
 
719
  if (neorv32_cpu_csr_read(CSR_MHPMCOUNTER3H) == 0) {
720
    size = 0;
721
    tmp = neorv32_cpu_csr_read(CSR_MHPMCOUNTER3);
722
  }
723
  else {
724
    size = 32;
725
    tmp = neorv32_cpu_csr_read(CSR_MHPMCOUNTER3H);
726
  }
727
 
728
  for (i=0; i<32; i++) {
729
    if (tmp & (1<<i)) {
730
      size++;
731
    }
732
  }
733
 
734
  return size;
735
}
736
 
737
 
738
/**********************************************************************//**
739 55 zero_gravi
 * Check if certain Z* extension is available
740
 *
741
 * @param[in] flag Index of the Z-extension to check from #NEORV32_CSR_MZEXT_enum
742
 * @return 0 if extension is NOT available, != 0 if extension is available.
743
 **************************************************************************/
744
int neorv32_check_zextension(uint32_t flag) {
745
 
746
  // check if out of range
747
  if (flag > 31) {
748
    return 0;
749
  }
750
 
751
  uint32_t tmp = neorv32_cpu_csr_read(CSR_MZEXT);
752
  if ((tmp & (1 << flag)) == 0) {
753
    return 0;
754
  }
755
  else {
756
    return 1;
757
  }
758
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.