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

Subversion Repositories neorv32

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

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
/**********************************************************************//**
47
 * Enable specific CPU interrupt.
48
 *
49
 * @note Interrupts have to be globally enabled via neorv32_cpu_eint(void), too.
50
 *
51 42 zero_gravi
 * @param[in] irq_sel CPU interrupt select. See #NEORV32_CSR_MIE_enum.
52 12 zero_gravi
 * @return 0 if success, 1 if error (invalid irq_sel).
53 2 zero_gravi
 **************************************************************************/
54
int neorv32_cpu_irq_enable(uint8_t irq_sel) {
55
 
56 42 zero_gravi
  if ((irq_sel != CSR_MIE_MSIE) && (irq_sel != CSR_MIE_MTIE) && (irq_sel != CSR_MIE_MEIE) &&
57
      (irq_sel != CSR_MIE_FIRQ0E) && (irq_sel != CSR_MIE_FIRQ1E) && (irq_sel != CSR_MIE_FIRQ2E) && (irq_sel != CSR_MIE_FIRQ3E)) {
58 2 zero_gravi
    return 1;
59
  }
60
 
61
  register uint32_t mask = (uint32_t)(1 << irq_sel);
62
  asm volatile ("csrrs zero, mie, %0" : : "r" (mask));
63
  return 0;
64
}
65
 
66
 
67
/**********************************************************************//**
68
 * Disable specific CPU interrupt.
69
 *
70 42 zero_gravi
 * @param[in] irq_sel CPU interrupt select. See #NEORV32_CSR_MIE_enum.
71 12 zero_gravi
 * @return 0 if success, 1 if error (invalid irq_sel).
72 2 zero_gravi
 **************************************************************************/
73
int neorv32_cpu_irq_disable(uint8_t irq_sel) {
74
 
75 42 zero_gravi
  if ((irq_sel != CSR_MIE_MSIE) && (irq_sel != CSR_MIE_MTIE) && (irq_sel != CSR_MIE_MEIE) &&
76
      (irq_sel != CSR_MIE_FIRQ0E) && (irq_sel != CSR_MIE_FIRQ1E) && (irq_sel != CSR_MIE_FIRQ2E) && (irq_sel != CSR_MIE_FIRQ3E)) {
77 2 zero_gravi
    return 1;
78
  }
79
 
80
  register uint32_t mask = (uint32_t)(1 << irq_sel);
81
  asm volatile ("csrrc zero, mie, %0" : : "r" (mask));
82
  return 0;
83
}
84
 
85
 
86
/**********************************************************************//**
87 12 zero_gravi
 * Get cycle count from cycle[h].
88
 *
89
 * @note The cycle[h] CSR is shadowed copy of the mcycle[h] CSR.
90
 *
91
 * @return Current cycle counter (64 bit).
92
 **************************************************************************/
93
uint64_t neorv32_cpu_get_cycle(void) {
94
 
95
  union {
96
    uint64_t uint64;
97
    uint32_t uint32[sizeof(uint64_t)/2];
98
  } cycles;
99
 
100
  uint32_t tmp1, tmp2, tmp3;
101
  while(1) {
102
    tmp1 = neorv32_cpu_csr_read(CSR_CYCLEH);
103
    tmp2 = neorv32_cpu_csr_read(CSR_CYCLE);
104
    tmp3 = neorv32_cpu_csr_read(CSR_CYCLEH);
105
    if (tmp1 == tmp3) {
106
      break;
107
    }
108
  }
109
 
110
  cycles.uint32[0] = tmp2;
111
  cycles.uint32[1] = tmp3;
112
 
113
  return cycles.uint64;
114
}
115
 
116
 
117
/**********************************************************************//**
118
 * Set mcycle[h] counter.
119
 *
120
 * @param[in] value New value for mcycle[h] CSR (64-bit).
121
 **************************************************************************/
122
void neorv32_cpu_set_mcycle(uint64_t value) {
123
 
124
  union {
125
    uint64_t uint64;
126
    uint32_t uint32[sizeof(uint64_t)/2];
127
  } cycles;
128
 
129
  cycles.uint64 = value;
130
 
131
  neorv32_cpu_csr_write(CSR_MCYCLE,  0);
132
  neorv32_cpu_csr_write(CSR_MCYCLEH, cycles.uint32[1]);
133
  neorv32_cpu_csr_write(CSR_MCYCLE,  cycles.uint32[0]);
134
}
135
 
136
 
137
/**********************************************************************//**
138
 * Get retired instructions counter from instret[h].
139
 *
140
 * @note The instret[h] CSR is shadowed copy of the instret[h] CSR.
141
 *
142
 * @return Current instructions counter (64 bit).
143
 **************************************************************************/
144
uint64_t neorv32_cpu_get_instret(void) {
145
 
146
  union {
147
    uint64_t uint64;
148
    uint32_t uint32[sizeof(uint64_t)/2];
149
  } cycles;
150
 
151
  uint32_t tmp1, tmp2, tmp3;
152
  while(1) {
153
    tmp1 = neorv32_cpu_csr_read(CSR_INSTRETH);
154
    tmp2 = neorv32_cpu_csr_read(CSR_INSTRET);
155
    tmp3 = neorv32_cpu_csr_read(CSR_INSTRETH);
156
    if (tmp1 == tmp3) {
157
      break;
158
    }
159
  }
160
 
161
  cycles.uint32[0] = tmp2;
162
  cycles.uint32[1] = tmp3;
163
 
164
  return cycles.uint64;
165
}
166
 
167
 
168
/**********************************************************************//**
169
 * Set retired instructions counter minstret[h].
170
 *
171
 * @param[in] value New value for mcycle[h] CSR (64-bit).
172
 **************************************************************************/
173
void neorv32_cpu_set_minstret(uint64_t value) {
174
 
175
  union {
176
    uint64_t uint64;
177
    uint32_t uint32[sizeof(uint64_t)/2];
178
  } cycles;
179
 
180
  cycles.uint64 = value;
181
 
182
  neorv32_cpu_csr_write(CSR_MINSTRET,  0);
183
  neorv32_cpu_csr_write(CSR_MINSTRETH, cycles.uint32[1]);
184
  neorv32_cpu_csr_write(CSR_MINSTRET,  cycles.uint32[0]);
185
}
186
 
187
 
188
/**********************************************************************//**
189
 * Get current system time from time[h] CSR.
190
 *
191
 * @note This function requires the MTIME system timer to be implemented.
192
 *
193
 * @return Current system time (64 bit).
194
 **************************************************************************/
195
uint64_t neorv32_cpu_get_systime(void) {
196
 
197
  union {
198
    uint64_t uint64;
199
    uint32_t uint32[sizeof(uint64_t)/2];
200
  } cycles;
201
 
202
  uint32_t tmp1, tmp2, tmp3;
203
  while(1) {
204
    tmp1 = neorv32_cpu_csr_read(CSR_TIMEH);
205
    tmp2 = neorv32_cpu_csr_read(CSR_TIME);
206
    tmp3 = neorv32_cpu_csr_read(CSR_TIMEH);
207
    if (tmp1 == tmp3) {
208
      break;
209
    }
210
  }
211
 
212
  cycles.uint32[0] = tmp2;
213
  cycles.uint32[1] = tmp3;
214
 
215
  return cycles.uint64;
216
}
217
 
218
 
219
/**********************************************************************//**
220 39 zero_gravi
 * Simple delay function using busy wait.
221 2 zero_gravi
 *
222 39 zero_gravi
 * @warning This function requires the cycle CSR(s). Hence, the Zicsr extension is mandatory.
223
 *
224 2 zero_gravi
 * @param[in] time_ms Time in ms to wait.
225
 **************************************************************************/
226
void neorv32_cpu_delay_ms(uint32_t time_ms) {
227
 
228 39 zero_gravi
  uint64_t time_resume = neorv32_cpu_get_cycle();
229 2 zero_gravi
 
230 39 zero_gravi
  uint32_t clock = SYSINFO_CLK; // clock ticks per second
231
  clock = clock / 1000; // clock ticks per ms
232
 
233
  uint64_t wait_cycles = ((uint64_t)clock) * ((uint64_t)time_ms);
234
  time_resume += wait_cycles;
235
 
236
  while(1) {
237
    if (neorv32_cpu_get_cycle() >= time_resume) {
238
      break;
239
    }
240 2 zero_gravi
  }
241
}
242
 
243 15 zero_gravi
 
244
/**********************************************************************//**
245
 * Switch from privilege mode MACHINE to privilege mode USER.
246
 *
247 39 zero_gravi
 * @warning This function requires the U extension to be implemented.
248 15 zero_gravi
 **************************************************************************/
249
void __attribute__((naked)) neorv32_cpu_goto_user_mode(void) {
250
 
251 35 zero_gravi
  // make sure to use NO registers in here! -> naked
252 15 zero_gravi
 
253 39 zero_gravi
  asm volatile ("csrw mepc, ra           \n\t" // move return address to mepc so we can return using "mret". also, we can now use ra as general purpose register in here
254 35 zero_gravi
                "li ra, %[input_imm]     \n\t" // bit mask to clear the two MPP bits
255
                "csrrc zero, mstatus, ra \n\t" // clear MPP bits -> MPP=u-mode
256
                "mret                    \n\t" // return and switch to user mode
257 42 zero_gravi
                :  : [input_imm] "i" ((1<<CSR_MSTATUS_MPP_H) | (1<<CSR_MSTATUS_MPP_L)));
258 15 zero_gravi
}
259 39 zero_gravi
 
260
 
261
/**********************************************************************//**
262
 * Atomic compare-and-swap operation (for implemeneting semaphores and mutexes).
263
 *
264
 * @warning This function requires the A (atomic) CPU extension.
265
 *
266
 * @param[in] addr Address of memory location.
267
 * @param[in] expected Expected value (for comparison).
268
 * @param[in] desired Desired value (new value).
269
 * @return Returns 0 on success, 1 on failure.
270
 **************************************************************************/
271
int __attribute__ ((noinline)) neorv32_cpu_atomic_cas(uint32_t addr, uint32_t expected, uint32_t desired) {
272
#ifdef __riscv_atomic
273
 
274
  register uint32_t addr_reg = addr;
275
  register uint32_t des_reg = desired;
276
  register uint32_t tmp_reg;
277
 
278
  // load original value + reservation (lock)
279
  asm volatile ("lr.w %[result], (%[input])" : [result] "=r" (tmp_reg) : [input] "r" (addr_reg));
280
 
281
  if (tmp_reg != expected) {
282
    asm volatile ("lw x0, 0(%[input])" : : [input] "r" (addr_reg)); // clear reservation lock
283
    return 1;
284
  }
285
 
286
  // store-conditional
287
  asm volatile ("sc.w %[result], %[input_i], (%[input_j])" : [result] "=r" (tmp_reg) : [input_i] "r" (des_reg), [input_j] "r" (addr_reg));
288
 
289
  if (tmp_reg) {
290
    return 1;
291
  }
292
 
293
  return 0;
294
#else
295
  return 1; // A extension not implemented -Y always fail
296
#endif
297
}
298 40 zero_gravi
 
299
 
300
/**********************************************************************//**
301 42 zero_gravi
 * Physical memory protection (PMP): Get number of available regions.
302
 *
303
 * @warning This function overrides all available PMPCFG* CSRs.
304
 * @warning This function requires the PMP CPU extension.
305
 *
306
 * @return Returns number of available PMP regions.
307
 **************************************************************************/
308
uint32_t neorv32_cpu_pmp_get_num_regions(void) {
309
 
310
  // try setting R bit in all PMPCFG CSRs
311
  neorv32_cpu_csr_write(CSR_PMPCFG0,  0x01010101);
312
  neorv32_cpu_csr_write(CSR_PMPCFG1,  0x01010101);
313
  neorv32_cpu_csr_write(CSR_PMPCFG2,  0x01010101);
314
  neorv32_cpu_csr_write(CSR_PMPCFG3,  0x01010101);
315
  neorv32_cpu_csr_write(CSR_PMPCFG4,  0x01010101);
316
  neorv32_cpu_csr_write(CSR_PMPCFG5,  0x01010101);
317
  neorv32_cpu_csr_write(CSR_PMPCFG6,  0x01010101);
318
  neorv32_cpu_csr_write(CSR_PMPCFG7,  0x01010101);
319
  neorv32_cpu_csr_write(CSR_PMPCFG8,  0x01010101);
320
  neorv32_cpu_csr_write(CSR_PMPCFG9,  0x01010101);
321
  neorv32_cpu_csr_write(CSR_PMPCFG10, 0x01010101);
322
  neorv32_cpu_csr_write(CSR_PMPCFG11, 0x01010101);
323
  neorv32_cpu_csr_write(CSR_PMPCFG12, 0x01010101);
324
  neorv32_cpu_csr_write(CSR_PMPCFG13, 0x01010101);
325
  neorv32_cpu_csr_write(CSR_PMPCFG14, 0x01010101);
326
  neorv32_cpu_csr_write(CSR_PMPCFG15, 0x01010101);
327
 
328
  // sum up all written ones (only available PMPCFG* CSRs/entries will return =! 0)
329
  union {
330
    uint32_t uint32;
331
    uint8_t  uint8[sizeof(uint32_t)/sizeof(uint8_t)];
332
  } cnt;
333
 
334
  cnt.uint32 = 0;
335
  cnt.uint32 += neorv32_cpu_csr_read(CSR_PMPCFG0);
336
  cnt.uint32 += neorv32_cpu_csr_read(CSR_PMPCFG1);
337
  cnt.uint32 += neorv32_cpu_csr_read(CSR_PMPCFG2);
338
  cnt.uint32 += neorv32_cpu_csr_read(CSR_PMPCFG3);
339
  cnt.uint32 += neorv32_cpu_csr_read(CSR_PMPCFG4);
340
  cnt.uint32 += neorv32_cpu_csr_read(CSR_PMPCFG5);
341
  cnt.uint32 += neorv32_cpu_csr_read(CSR_PMPCFG6);
342
  cnt.uint32 += neorv32_cpu_csr_read(CSR_PMPCFG7);
343
  cnt.uint32 += neorv32_cpu_csr_read(CSR_PMPCFG8);
344
  cnt.uint32 += neorv32_cpu_csr_read(CSR_PMPCFG9);
345
  cnt.uint32 += neorv32_cpu_csr_read(CSR_PMPCFG10);
346
  cnt.uint32 += neorv32_cpu_csr_read(CSR_PMPCFG11);
347
  cnt.uint32 += neorv32_cpu_csr_read(CSR_PMPCFG12);
348
  cnt.uint32 += neorv32_cpu_csr_read(CSR_PMPCFG13);
349
  cnt.uint32 += neorv32_cpu_csr_read(CSR_PMPCFG14);
350
  cnt.uint32 += neorv32_cpu_csr_read(CSR_PMPCFG15);
351
 
352
  // sum up bytes
353
  uint32_t num_regions = 0;
354
  num_regions += (uint32_t)cnt.uint8[0];
355
  num_regions += (uint32_t)cnt.uint8[1];
356
  num_regions += (uint32_t)cnt.uint8[2];
357
  num_regions += (uint32_t)cnt.uint8[3];
358
 
359
  return num_regions;
360
}
361
 
362
 
363
/**********************************************************************//**
364 40 zero_gravi
 * Physical memory protection (PMP): Get minimal region size (granularity).
365
 *
366
 * @warning This function overrides PMPCFG0[0] and PMPADDR0 CSRs.
367
 * @warning This function requires the PMP CPU extension.
368
 *
369 42 zero_gravi
 * @return Returns minimal region size in bytes.
370 40 zero_gravi
 **************************************************************************/
371
uint32_t neorv32_cpu_pmp_get_granularity(void) {
372
 
373
  // check min granulartiy
374
  uint32_t tmp = neorv32_cpu_csr_read(CSR_PMPCFG0);
375
  tmp &= 0xffffff00; // disable entry 0
376
  neorv32_cpu_csr_write(CSR_PMPCFG0, tmp);
377
  neorv32_cpu_csr_write(CSR_PMPADDR0, 0xffffffff);
378
  uint32_t tmp_a = neorv32_cpu_csr_read(CSR_PMPADDR0);
379
 
380
  uint32_t i;
381
 
382
  // find least-significat set bit
383
  for (i=31; i!=0; i--) {
384
    if (((tmp_a >> i) & 1) == 0) {
385
      break;
386
    }
387
  }
388
 
389
  return (uint32_t)(1 << (i+1+2));
390
}
391
 
392
 
393
/**********************************************************************//**
394
 * Physical memory protection (PMP): Configure region.
395
 *
396
 * @note Using NAPOT mode - page base address has to be naturally aligned.
397
 *
398
 * @warning This function requires the PMP CPU extension.
399 42 zero_gravi
 * @warning Only use available PMP regions. Check before using neorv32_cpu_pmp_get_regions(void).
400 40 zero_gravi
 *
401 42 zero_gravi
 * @param[in] index Region number (index, 0..PMP_NUM_REGIONS-1).
402 40 zero_gravi
 * @param[in] base Region base address (has to be naturally aligned!).
403
 * @param[in] size Region size, has to be a power of 2 (min 8 bytes or according to HW's PMP.granularity configuration).
404
 * @param[in] config Region configuration (attributes) byte (for PMPCFGx).
405
 * @return Returns 0 on success, 1 on failure.
406
 **************************************************************************/
407
int neorv32_cpu_pmp_configure_region(uint32_t index, uint32_t base, uint32_t size, uint8_t config) {
408
 
409
  if (size < 8) {
410
    return 1; // minimal region size is 8 bytes
411
  }
412
 
413
  if ((size & (size - 1)) != 0) {
414
    return 1; // region size is not a power of two
415
  }
416
 
417
  // setup configuration
418
  uint32_t tmp;
419
  uint32_t config_int  = ((uint32_t)config) << ((index%4)*8);
420
  uint32_t config_mask = ((uint32_t)0xFF)   << ((index%4)*8);
421
  config_mask = ~config_mask;
422
 
423
  // clear old configuration
424 42 zero_gravi
  switch(index & 15) {
425
    case 0:  neorv32_cpu_csr_write(CSR_PMPCFG0,  neorv32_cpu_csr_read(CSR_PMPCFG0)  & config_mask); break;
426
    case 1:  neorv32_cpu_csr_write(CSR_PMPCFG1,  neorv32_cpu_csr_read(CSR_PMPCFG1)  & config_mask); break;
427
    case 2:  neorv32_cpu_csr_write(CSR_PMPCFG2,  neorv32_cpu_csr_read(CSR_PMPCFG2)  & config_mask); break;
428
    case 3:  neorv32_cpu_csr_write(CSR_PMPCFG3,  neorv32_cpu_csr_read(CSR_PMPCFG3)  & config_mask); break;
429
    case 4:  neorv32_cpu_csr_write(CSR_PMPCFG4,  neorv32_cpu_csr_read(CSR_PMPCFG4)  & config_mask); break;
430
    case 5:  neorv32_cpu_csr_write(CSR_PMPCFG5,  neorv32_cpu_csr_read(CSR_PMPCFG5)  & config_mask); break;
431
    case 6:  neorv32_cpu_csr_write(CSR_PMPCFG6,  neorv32_cpu_csr_read(CSR_PMPCFG6)  & config_mask); break;
432
    case 7:  neorv32_cpu_csr_write(CSR_PMPCFG7,  neorv32_cpu_csr_read(CSR_PMPCFG7)  & config_mask); break;
433
    case 8:  neorv32_cpu_csr_write(CSR_PMPCFG8,  neorv32_cpu_csr_read(CSR_PMPCFG8)  & config_mask); break;
434
    case 9:  neorv32_cpu_csr_write(CSR_PMPCFG9,  neorv32_cpu_csr_read(CSR_PMPCFG9)  & config_mask); break;
435
    case 10: neorv32_cpu_csr_write(CSR_PMPCFG10, neorv32_cpu_csr_read(CSR_PMPCFG10) & config_mask); break;
436
    case 11: neorv32_cpu_csr_write(CSR_PMPCFG11, neorv32_cpu_csr_read(CSR_PMPCFG11) & config_mask); break;
437
    case 12: neorv32_cpu_csr_write(CSR_PMPCFG12, neorv32_cpu_csr_read(CSR_PMPCFG12) & config_mask); break;
438
    case 13: neorv32_cpu_csr_write(CSR_PMPCFG13, neorv32_cpu_csr_read(CSR_PMPCFG13) & config_mask); break;
439
    case 14: neorv32_cpu_csr_write(CSR_PMPCFG14, neorv32_cpu_csr_read(CSR_PMPCFG14) & config_mask); break;
440
    case 15: neorv32_cpu_csr_write(CSR_PMPCFG15, neorv32_cpu_csr_read(CSR_PMPCFG15) & config_mask); break;
441
    default: break;
442 40 zero_gravi
  }
443
 
444
  // set base address and region size
445
  uint32_t addr_mask = ~((size - 1) >> 2);
446
  uint32_t size_mask = (size - 1) >> 3;
447
 
448
  tmp = base & addr_mask;
449
  tmp = tmp | size_mask;
450
 
451 42 zero_gravi
  switch(index & 63) {
452
    case 0:  neorv32_cpu_csr_write(CSR_PMPADDR0,  tmp); break;
453
    case 1:  neorv32_cpu_csr_write(CSR_PMPADDR1,  tmp); break;
454
    case 2:  neorv32_cpu_csr_write(CSR_PMPADDR2,  tmp); break;
455
    case 3:  neorv32_cpu_csr_write(CSR_PMPADDR3,  tmp); break;
456
    case 4:  neorv32_cpu_csr_write(CSR_PMPADDR4,  tmp); break;
457
    case 5:  neorv32_cpu_csr_write(CSR_PMPADDR5,  tmp); break;
458
    case 6:  neorv32_cpu_csr_write(CSR_PMPADDR6,  tmp); break;
459
    case 7:  neorv32_cpu_csr_write(CSR_PMPADDR7,  tmp); break;
460
    case 8:  neorv32_cpu_csr_write(CSR_PMPADDR8,  tmp); break;
461
    case 9:  neorv32_cpu_csr_write(CSR_PMPADDR9,  tmp); break;
462
    case 10: neorv32_cpu_csr_write(CSR_PMPADDR10, tmp); break;
463
    case 11: neorv32_cpu_csr_write(CSR_PMPADDR11, tmp); break;
464
    case 12: neorv32_cpu_csr_write(CSR_PMPADDR12, tmp); break;
465
    case 13: neorv32_cpu_csr_write(CSR_PMPADDR13, tmp); break;
466
    case 14: neorv32_cpu_csr_write(CSR_PMPADDR14, tmp); break;
467
    case 15: neorv32_cpu_csr_write(CSR_PMPADDR15, tmp); break;
468
    case 16: neorv32_cpu_csr_write(CSR_PMPADDR16, tmp); break;
469
    case 17: neorv32_cpu_csr_write(CSR_PMPADDR17, tmp); break;
470
    case 18: neorv32_cpu_csr_write(CSR_PMPADDR18, tmp); break;
471
    case 19: neorv32_cpu_csr_write(CSR_PMPADDR19, tmp); break;
472
    case 20: neorv32_cpu_csr_write(CSR_PMPADDR20, tmp); break;
473
    case 21: neorv32_cpu_csr_write(CSR_PMPADDR21, tmp); break;
474
    case 22: neorv32_cpu_csr_write(CSR_PMPADDR22, tmp); break;
475
    case 23: neorv32_cpu_csr_write(CSR_PMPADDR23, tmp); break;
476
    case 24: neorv32_cpu_csr_write(CSR_PMPADDR24, tmp); break;
477
    case 25: neorv32_cpu_csr_write(CSR_PMPADDR25, tmp); break;
478
    case 26: neorv32_cpu_csr_write(CSR_PMPADDR26, tmp); break;
479
    case 27: neorv32_cpu_csr_write(CSR_PMPADDR27, tmp); break;
480
    case 28: neorv32_cpu_csr_write(CSR_PMPADDR28, tmp); break;
481
    case 29: neorv32_cpu_csr_write(CSR_PMPADDR29, tmp); break;
482
    case 30: neorv32_cpu_csr_write(CSR_PMPADDR30, tmp); break;
483
    case 31: neorv32_cpu_csr_write(CSR_PMPADDR31, tmp); break;
484
    case 32: neorv32_cpu_csr_write(CSR_PMPADDR32, tmp); break;
485
    case 33: neorv32_cpu_csr_write(CSR_PMPADDR33, tmp); break;
486
    case 34: neorv32_cpu_csr_write(CSR_PMPADDR34, tmp); break;
487
    case 35: neorv32_cpu_csr_write(CSR_PMPADDR35, tmp); break;
488
    case 36: neorv32_cpu_csr_write(CSR_PMPADDR36, tmp); break;
489
    case 37: neorv32_cpu_csr_write(CSR_PMPADDR37, tmp); break;
490
    case 38: neorv32_cpu_csr_write(CSR_PMPADDR38, tmp); break;
491
    case 39: neorv32_cpu_csr_write(CSR_PMPADDR39, tmp); break;
492
    case 40: neorv32_cpu_csr_write(CSR_PMPADDR40, tmp); break;
493
    case 41: neorv32_cpu_csr_write(CSR_PMPADDR41, tmp); break;
494
    case 42: neorv32_cpu_csr_write(CSR_PMPADDR42, tmp); break;
495
    case 43: neorv32_cpu_csr_write(CSR_PMPADDR43, tmp); break;
496
    case 44: neorv32_cpu_csr_write(CSR_PMPADDR44, tmp); break;
497
    case 45: neorv32_cpu_csr_write(CSR_PMPADDR45, tmp); break;
498
    case 46: neorv32_cpu_csr_write(CSR_PMPADDR46, tmp); break;
499
    case 47: neorv32_cpu_csr_write(CSR_PMPADDR47, tmp); break;
500
    case 48: neorv32_cpu_csr_write(CSR_PMPADDR48, tmp); break;
501
    case 49: neorv32_cpu_csr_write(CSR_PMPADDR49, tmp); break;
502
    case 50: neorv32_cpu_csr_write(CSR_PMPADDR50, tmp); break;
503
    case 51: neorv32_cpu_csr_write(CSR_PMPADDR51, tmp); break;
504
    case 52: neorv32_cpu_csr_write(CSR_PMPADDR52, tmp); break;
505
    case 53: neorv32_cpu_csr_write(CSR_PMPADDR53, tmp); break;
506
    case 54: neorv32_cpu_csr_write(CSR_PMPADDR54, tmp); break;
507
    case 55: neorv32_cpu_csr_write(CSR_PMPADDR55, tmp); break;
508
    case 56: neorv32_cpu_csr_write(CSR_PMPADDR56, tmp); break;
509
    case 57: neorv32_cpu_csr_write(CSR_PMPADDR57, tmp); break;
510
    case 58: neorv32_cpu_csr_write(CSR_PMPADDR58, tmp); break;
511
    case 59: neorv32_cpu_csr_write(CSR_PMPADDR59, tmp); break;
512
    case 60: neorv32_cpu_csr_write(CSR_PMPADDR60, tmp); break;
513
    case 61: neorv32_cpu_csr_write(CSR_PMPADDR61, tmp); break;
514
    case 62: neorv32_cpu_csr_write(CSR_PMPADDR62, tmp); break;
515
    case 63: neorv32_cpu_csr_write(CSR_PMPADDR63, tmp); break;
516 40 zero_gravi
    default: break;
517
  }
518
 
519 42 zero_gravi
  // wait for HW to compute PMP-internal stuff (address masks)
520 40 zero_gravi
  for (tmp=0; tmp<16; tmp++) {
521
    asm volatile ("nop");
522
  }
523
 
524
  // set new configuration
525 42 zero_gravi
  switch(index & 15) {
526
    case 0:  neorv32_cpu_csr_write(CSR_PMPCFG0,  neorv32_cpu_csr_read(CSR_PMPCFG0)  | config_int); break;
527
    case 1:  neorv32_cpu_csr_write(CSR_PMPCFG1,  neorv32_cpu_csr_read(CSR_PMPCFG1)  | config_int); break;
528
    case 2:  neorv32_cpu_csr_write(CSR_PMPCFG2,  neorv32_cpu_csr_read(CSR_PMPCFG2)  | config_int); break;
529
    case 3:  neorv32_cpu_csr_write(CSR_PMPCFG3,  neorv32_cpu_csr_read(CSR_PMPCFG3)  | config_int); break;
530
    case 4:  neorv32_cpu_csr_write(CSR_PMPCFG4,  neorv32_cpu_csr_read(CSR_PMPCFG4)  | config_int); break;
531
    case 5:  neorv32_cpu_csr_write(CSR_PMPCFG5,  neorv32_cpu_csr_read(CSR_PMPCFG5)  | config_int); break;
532
    case 6:  neorv32_cpu_csr_write(CSR_PMPCFG6,  neorv32_cpu_csr_read(CSR_PMPCFG6)  | config_int); break;
533
    case 7:  neorv32_cpu_csr_write(CSR_PMPCFG7,  neorv32_cpu_csr_read(CSR_PMPCFG7)  | config_int); break;
534
    case 8:  neorv32_cpu_csr_write(CSR_PMPCFG8,  neorv32_cpu_csr_read(CSR_PMPCFG8)  | config_int); break;
535
    case 9:  neorv32_cpu_csr_write(CSR_PMPCFG9,  neorv32_cpu_csr_read(CSR_PMPCFG9)  | config_int); break;
536
    case 10: neorv32_cpu_csr_write(CSR_PMPCFG10, neorv32_cpu_csr_read(CSR_PMPCFG10) | config_int); break;
537
    case 11: neorv32_cpu_csr_write(CSR_PMPCFG11, neorv32_cpu_csr_read(CSR_PMPCFG11) | config_int); break;
538
    case 12: neorv32_cpu_csr_write(CSR_PMPCFG12, neorv32_cpu_csr_read(CSR_PMPCFG12) | config_int); break;
539
    case 13: neorv32_cpu_csr_write(CSR_PMPCFG13, neorv32_cpu_csr_read(CSR_PMPCFG13) | config_int); break;
540
    case 14: neorv32_cpu_csr_write(CSR_PMPCFG14, neorv32_cpu_csr_read(CSR_PMPCFG14) | config_int); break;
541
    case 15: neorv32_cpu_csr_write(CSR_PMPCFG15, neorv32_cpu_csr_read(CSR_PMPCFG15) | config_int); break;
542
    default: break;
543 40 zero_gravi
  }
544
 
545
  return 0;
546
}
547 42 zero_gravi
 
548
 
549
/**********************************************************************//**
550
 * Hardware performance monitors (HPM): Get number of available HPM counters.
551
 *
552
 * @warning This function overrides all available mhpmcounter* CSRs.
553
 *
554
 * @return Returns number of available HPM counters (..29).
555
 **************************************************************************/
556
uint32_t neorv32_cpu_hpm_get_counters(void) {
557
 
558
  // try setting all mhpmcounter* CSRs to 1
559
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER3,  1);
560
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER4,  1);
561
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER5,  1);
562
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER6,  1);
563
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER7,  1);
564
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER8,  1);
565
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER9,  1);
566
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER10, 1);
567
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER11, 1);
568
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER12, 1);
569
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER13, 1);
570
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER14, 1);
571
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER15, 1);
572
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER16, 1);
573
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER17, 1);
574
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER18, 1);
575
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER19, 1);
576
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER20, 1);
577
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER21, 1);
578
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER22, 1);
579
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER23, 1);
580
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER24, 1);
581
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER25, 1);
582
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER26, 1);
583
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER27, 1);
584
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER28, 1);
585
  neorv32_cpu_csr_write(CSR_MHPMCOUNTER29, 1);
586
 
587
  // sum up all written ones (only available PMPCFG* CSRs/entries will return =! 0)
588
  uint32_t num_hpm_cnts = 0;
589
 
590
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER3);
591
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER4);
592
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER5);
593
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER6);
594
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER7);
595
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER8);
596
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER9);
597
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER10);
598
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER11);
599
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER12);
600
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER13);
601
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER14);
602
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER15);
603
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER16);
604
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER17);
605
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER18);
606
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER19);
607
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER20);
608
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER21);
609
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER22);
610
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER23);
611
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER24);
612
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER25);
613
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER26);
614
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER27);
615
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER28);
616
  num_hpm_cnts += neorv32_cpu_csr_read(CSR_MHPMCOUNTER29);
617
 
618
  return num_hpm_cnts;
619
}

powered by: WebSVN 2.1.0

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