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

Subversion Repositories neorv32

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

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

powered by: WebSVN 2.1.0

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