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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [lib/] [source/] [neorv32_rte.c] - Blame information for rev 48

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

Line No. Rev Author Line
1 2 zero_gravi
// #################################################################################################
2
// # << NEORV32: neorv32_rte.c - NEORV32 Runtime Environment >>                                    #
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_rte.c
38
 * @author Stephan Nolting
39
 * @brief NEORV32 Runtime Environment.
40
 **************************************************************************/
41
 
42
#include "neorv32.h"
43
#include "neorv32_rte.h"
44
 
45 14 zero_gravi
/**********************************************************************//**
46
 * The >private< trap vector look-up table of the NEORV32 RTE.
47
 **************************************************************************/
48 48 zero_gravi
static uint32_t __neorv32_rte_vector_lut[29] __attribute__((unused)); // trap handler vector table
49 14 zero_gravi
 
50
// private functions
51
static void __attribute__((__interrupt__)) __neorv32_rte_core(void) __attribute__((aligned(16))) __attribute__((unused));
52 6 zero_gravi
static void __neorv32_rte_debug_exc_handler(void)     __attribute__((unused));
53
static void __neorv32_rte_print_true_false(int state) __attribute__((unused));
54 33 zero_gravi
static void __neorv32_rte_print_hex_word(uint32_t num);
55 2 zero_gravi
 
56
 
57
/**********************************************************************//**
58 14 zero_gravi
 * Setup NEORV32 runtime environment.
59 2 zero_gravi
 *
60
 * @note This function installs a debug handler for ALL exception and interrupt sources, which
61 14 zero_gravi
 * gives detailed information about the exception/interrupt. Actual handler can be installed afterwards
62
 * via neorv32_rte_exception_install(uint8_t id, void (*handler)(void)).
63 2 zero_gravi
 **************************************************************************/
64 14 zero_gravi
void neorv32_rte_setup(void) {
65 2 zero_gravi
 
66 24 zero_gravi
  // check if CSR system is available at all
67
  if (neorv32_cpu_csr_read(CSR_MISA) == 0) {
68 33 zero_gravi
    neorv32_uart_print("<RTE> WARNING! CPU CSR system not available! </RTE>");
69 24 zero_gravi
  }
70
 
71 14 zero_gravi
  // configure trap handler base address
72
  uint32_t mtvec_base = (uint32_t)(&__neorv32_rte_core);
73
  neorv32_cpu_csr_write(CSR_MTVEC, mtvec_base);
74 2 zero_gravi
 
75
  // install debug handler for all sources
76 14 zero_gravi
  uint8_t id;
77
  for (id = 0; id < (sizeof(__neorv32_rte_vector_lut)/sizeof(__neorv32_rte_vector_lut[0])); id++) {
78
    neorv32_rte_exception_uninstall(id); // this will configure the debug handler
79 2 zero_gravi
  }
80
}
81
 
82
 
83
/**********************************************************************//**
84
 * Install exception handler function to NEORV32 runtime environment.
85
 *
86 17 zero_gravi
 * @note Interrupt sources have to be explicitly enabled by the user via the CSR.mie bits via neorv32_cpu_irq_enable(uint8_t irq_sel)
87
 * and the global interrupt enable bit mstatus.mie via neorv32_cpu_eint(void).
88 2 zero_gravi
 *
89 14 zero_gravi
 * @param[in] id Identifier (type) of the targeted exception. See #NEORV32_RTE_TRAP_enum.
90
 * @param[in] handler The actual handler function for the specified exception (function MUST be of type "void function(void);").
91 18 zero_gravi
 * @return 0 if success, 1 if error (invalid id or targeted exception not supported).
92 2 zero_gravi
 **************************************************************************/
93 14 zero_gravi
int neorv32_rte_exception_install(uint8_t id, void (*handler)(void)) {
94 2 zero_gravi
 
95
  // id valid?
96 48 zero_gravi
  if ((id >= RTE_TRAP_I_MISALIGNED) && (id <= CSR_MIE_FIRQ15E)) {
97 24 zero_gravi
    __neorv32_rte_vector_lut[id] = (uint32_t)handler; // install handler
98 2 zero_gravi
    return 0;
99
  }
100
  return 1;
101
}
102
 
103
 
104
/**********************************************************************//**
105
 * Uninstall exception handler function from NEORV32 runtime environment, which was
106 14 zero_gravi
 * previously installed via neorv32_rte_exception_install(uint8_t id, void (*handler)(void)).
107 2 zero_gravi
 *
108 17 zero_gravi
 * @note Interrupt sources have to be explicitly disabled by the user via the CSR.mie bits via neorv32_cpu_irq_disable(uint8_t irq_sel)
109
 * and/or the global interrupt enable bit mstatus.mie via neorv32_cpu_dint(void).
110 2 zero_gravi
 *
111 14 zero_gravi
 * @param[in] id Identifier (type) of the targeted exception. See #NEORV32_RTE_TRAP_enum.
112 18 zero_gravi
 * @return 0 if success, 1 if error (invalid id or targeted exception not supported).
113 2 zero_gravi
 **************************************************************************/
114 14 zero_gravi
int neorv32_rte_exception_uninstall(uint8_t id) {
115 2 zero_gravi
 
116
  // id valid?
117 48 zero_gravi
  if ((id >= RTE_TRAP_I_MISALIGNED) && (id <= CSR_MIE_FIRQ15E)) {
118 14 zero_gravi
    __neorv32_rte_vector_lut[id] = (uint32_t)(&__neorv32_rte_debug_exc_handler); // use dummy handler in case the exception is accidently triggered
119 2 zero_gravi
    return 0;
120
  }
121
  return 1;
122
}
123
 
124
 
125
/**********************************************************************//**
126 14 zero_gravi
 * This is the core of the NEORV32 RTE.
127
 *
128
 * @note This function must no be explicitly used by the user.
129 33 zero_gravi
 * @note The RTE core uses mscratch CSR to store the trap-causing mepc for further (user-defined) processing.
130
 *
131 14 zero_gravi
 * @warning When using the the RTE, this function is the ONLY function that can use the 'interrupt' attribute!
132 2 zero_gravi
 **************************************************************************/
133 14 zero_gravi
static void __attribute__((__interrupt__)) __attribute__((aligned(16)))  __neorv32_rte_core(void) {
134 2 zero_gravi
 
135 33 zero_gravi
  register uint32_t rte_mepc = neorv32_cpu_csr_read(CSR_MEPC);
136
  neorv32_cpu_csr_write(CSR_MSCRATCH, rte_mepc); // store for later
137 14 zero_gravi
  register uint32_t rte_mcause = neorv32_cpu_csr_read(CSR_MCAUSE);
138
 
139
  // compute return address
140
  if ((rte_mcause & 0x80000000) == 0) { // modify pc only if exception
141
 
142
    // get low half word of faulting instruction
143
    register uint32_t rte_trap_inst;
144
    asm volatile ("lh %[result], 0(%[input_i])" : [result] "=r" (rte_trap_inst) : [input_i] "r" (rte_mepc));
145
 
146
    if ((rte_trap_inst & 3) == 3) { // faulting instruction is uncompressed instruction
147
      rte_mepc += 4;
148
    }
149
    else { // faulting instruction is compressed instruction
150
      rte_mepc += 2;
151
    }
152
 
153
    // store new return address
154
    neorv32_cpu_csr_write(CSR_MEPC, rte_mepc);
155
  }
156
 
157
  // find according trap handler
158
  register uint32_t rte_handler = (uint32_t)(&__neorv32_rte_debug_exc_handler);
159
  switch (rte_mcause) {
160
    case TRAP_CODE_I_MISALIGNED: rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_I_MISALIGNED]; break;
161
    case TRAP_CODE_I_ACCESS:     rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_I_ACCESS]; break;
162
    case TRAP_CODE_I_ILLEGAL:    rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_I_ILLEGAL]; break;
163
    case TRAP_CODE_BREAKPOINT:   rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_BREAKPOINT]; break;
164
    case TRAP_CODE_L_MISALIGNED: rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_L_MISALIGNED]; break;
165
    case TRAP_CODE_L_ACCESS:     rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_L_ACCESS]; break;
166
    case TRAP_CODE_S_MISALIGNED: rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_S_MISALIGNED]; break;
167
    case TRAP_CODE_S_ACCESS:     rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_S_ACCESS]; break;
168 40 zero_gravi
    case TRAP_CODE_UENV_CALL:    rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_UENV_CALL]; break;
169 14 zero_gravi
    case TRAP_CODE_MENV_CALL:    rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_MENV_CALL]; break;
170
    case TRAP_CODE_MSI:          rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_MSI]; break;
171
    case TRAP_CODE_MTI:          rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_MTI]; break;
172
    case TRAP_CODE_MEI:          rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_MEI]; break;
173
    case TRAP_CODE_FIRQ_0:       rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_FIRQ_0]; break;
174
    case TRAP_CODE_FIRQ_1:       rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_FIRQ_1]; break;
175
    case TRAP_CODE_FIRQ_2:       rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_FIRQ_2]; break;
176
    case TRAP_CODE_FIRQ_3:       rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_FIRQ_3]; break;
177 47 zero_gravi
    case TRAP_CODE_FIRQ_4:       rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_FIRQ_4]; break;
178
    case TRAP_CODE_FIRQ_5:       rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_FIRQ_5]; break;
179
    case TRAP_CODE_FIRQ_6:       rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_FIRQ_6]; break;
180
    case TRAP_CODE_FIRQ_7:       rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_FIRQ_7]; break;
181 48 zero_gravi
    case TRAP_CODE_FIRQ_8:       rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_FIRQ_8]; break;
182
    case TRAP_CODE_FIRQ_9:       rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_FIRQ_9]; break;
183
    case TRAP_CODE_FIRQ_10:      rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_FIRQ_10]; break;
184
    case TRAP_CODE_FIRQ_11:      rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_FIRQ_11]; break;
185
    case TRAP_CODE_FIRQ_12:      rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_FIRQ_12]; break;
186
    case TRAP_CODE_FIRQ_13:      rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_FIRQ_13]; break;
187
    case TRAP_CODE_FIRQ_14:      rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_FIRQ_14]; break;
188
    case TRAP_CODE_FIRQ_15:      rte_handler = __neorv32_rte_vector_lut[RTE_TRAP_FIRQ_15]; break;
189 14 zero_gravi
    default: break;
190
  }
191
 
192
  // execute handler
193
  void (*handler_pnt)(void);
194
  handler_pnt = (void*)rte_handler;
195
  (*handler_pnt)();
196 2 zero_gravi
}
197
 
198
 
199
/**********************************************************************//**
200
 * NEORV32 runtime environment: Debug exception handler, printing various exception/interrupt information via UART.
201 14 zero_gravi
 * @note This function is used by neorv32_rte_exception_uninstall(void) only.
202 2 zero_gravi
 **************************************************************************/
203
static void __neorv32_rte_debug_exc_handler(void) {
204
 
205 48 zero_gravi
  char tmp;
206
 
207 15 zero_gravi
  // intro
208 33 zero_gravi
  neorv32_uart_print("<RTE> ");
209 2 zero_gravi
 
210 15 zero_gravi
  // cause
211 7 zero_gravi
  register uint32_t trap_cause = neorv32_cpu_csr_read(CSR_MCAUSE);
212 48 zero_gravi
  tmp = (char)(trap_cause & 0xf);
213
  if (tmp >= 10) {
214
    tmp = 'a' + (tmp - 10);
215
  }
216
  else {
217
    tmp = '0' + tmp;
218
  }
219 7 zero_gravi
  switch (trap_cause) {
220 33 zero_gravi
    case TRAP_CODE_I_MISALIGNED: neorv32_uart_print("Instruction address misaligned"); break;
221
    case TRAP_CODE_I_ACCESS:     neorv32_uart_print("Instruction access fault"); break;
222
    case TRAP_CODE_I_ILLEGAL:    neorv32_uart_print("Illegal instruction"); break;
223
    case TRAP_CODE_BREAKPOINT:   neorv32_uart_print("Breakpoint"); break;
224
    case TRAP_CODE_L_MISALIGNED: neorv32_uart_print("Load address misaligned"); break;
225
    case TRAP_CODE_L_ACCESS:     neorv32_uart_print("Load access fault"); break;
226
    case TRAP_CODE_S_MISALIGNED: neorv32_uart_print("Store address misaligned"); break;
227
    case TRAP_CODE_S_ACCESS:     neorv32_uart_print("Store access fault"); break;
228 40 zero_gravi
    case TRAP_CODE_UENV_CALL:    neorv32_uart_print("Environment call from U-mode"); break;
229
    case TRAP_CODE_MENV_CALL:    neorv32_uart_print("Environment call from M-mode"); break;
230 33 zero_gravi
    case TRAP_CODE_MSI:          neorv32_uart_print("Machine software interrupt"); break;
231
    case TRAP_CODE_MTI:          neorv32_uart_print("Machine timer interrupt"); break;
232
    case TRAP_CODE_MEI:          neorv32_uart_print("Machine external interrupt"); break;
233 47 zero_gravi
    case TRAP_CODE_FIRQ_0:
234
    case TRAP_CODE_FIRQ_1:
235
    case TRAP_CODE_FIRQ_2:
236
    case TRAP_CODE_FIRQ_3:
237
    case TRAP_CODE_FIRQ_4:
238
    case TRAP_CODE_FIRQ_5:
239
    case TRAP_CODE_FIRQ_6:
240 48 zero_gravi
    case TRAP_CODE_FIRQ_7:
241
    case TRAP_CODE_FIRQ_8:
242
    case TRAP_CODE_FIRQ_9:
243
    case TRAP_CODE_FIRQ_10:
244
    case TRAP_CODE_FIRQ_11:
245
    case TRAP_CODE_FIRQ_12:
246
    case TRAP_CODE_FIRQ_13:
247
    case TRAP_CODE_FIRQ_14:
248
    case TRAP_CODE_FIRQ_15:      neorv32_uart_print("Fast interrupt "); neorv32_uart_putc(tmp); break;
249 33 zero_gravi
    default:                     neorv32_uart_print("Unknown trap cause: "); __neorv32_rte_print_hex_word(trap_cause); break;
250 2 zero_gravi
  }
251
 
252 33 zero_gravi
  // instruction address
253 35 zero_gravi
  neorv32_uart_print(" @ PC=");
254 33 zero_gravi
  __neorv32_rte_print_hex_word(neorv32_cpu_csr_read(CSR_MSCRATCH)); // rte core stores actual mepc to mscratch
255 15 zero_gravi
 
256 33 zero_gravi
  // additional info
257
  neorv32_uart_print(", MTVAL=");
258
  __neorv32_rte_print_hex_word(neorv32_cpu_csr_read(CSR_MTVAL));
259
  neorv32_uart_print(" </RTE>");
260 6 zero_gravi
}
261
 
262
 
263
/**********************************************************************//**
264
 * NEORV32 runtime environment: Print hardware configuration information via UART
265
 **************************************************************************/
266
void neorv32_rte_print_hw_config(void) {
267
 
268
  uint32_t tmp;
269
  int i;
270
  char c;
271
 
272 32 zero_gravi
  neorv32_uart_printf("\n\n<< Hardware Configuration Overview >>\n");
273 6 zero_gravi
 
274
  // CPU configuration
275 40 zero_gravi
  neorv32_uart_printf("\n---- Central Processing Unit ----\n");
276 6 zero_gravi
 
277 23 zero_gravi
  // ID
278 30 zero_gravi
  neorv32_uart_printf("Hart ID:           0x%x\n", neorv32_cpu_csr_read(CSR_MHARTID));
279 6 zero_gravi
 
280 30 zero_gravi
  neorv32_uart_printf("Vendor ID:         0x%x\n", neorv32_cpu_csr_read(CSR_MVENDORID));
281 12 zero_gravi
 
282 23 zero_gravi
  tmp = neorv32_cpu_csr_read(CSR_MARCHID);
283 30 zero_gravi
  neorv32_uart_printf("Architecture ID:   0x%x", tmp);
284 32 zero_gravi
  if (tmp == NEORV32_ARCHID) {
285
    neorv32_uart_printf(" (NEORV32)");
286
  }
287 23 zero_gravi
 
288 6 zero_gravi
  // HW version
289 30 zero_gravi
  neorv32_uart_printf("\nImplementation ID: 0x%x (", neorv32_cpu_csr_read(CSR_MIMPID));
290 12 zero_gravi
  neorv32_rte_print_hw_version();
291 30 zero_gravi
  neorv32_uart_printf(")\n");
292 6 zero_gravi
 
293
  // CPU architecture
294 30 zero_gravi
  neorv32_uart_printf("Architecture:      ");
295 6 zero_gravi
  tmp = neorv32_cpu_csr_read(CSR_MISA);
296
  tmp = (tmp >> 30) & 0x03;
297
  if (tmp == 0) {
298
    neorv32_uart_printf("unknown");
299
  }
300
  if (tmp == 1) {
301 41 zero_gravi
    neorv32_uart_printf("rv32");
302 6 zero_gravi
  }
303
  if (tmp == 2) {
304 41 zero_gravi
    neorv32_uart_printf("rv64");
305 6 zero_gravi
  }
306
  if (tmp == 3) {
307 41 zero_gravi
    neorv32_uart_printf("rv128");
308 6 zero_gravi
  }
309
 
310
  // CPU extensions
311 40 zero_gravi
  neorv32_uart_printf("\nEndianness:        ");
312 42 zero_gravi
  if (neorv32_cpu_csr_read(CSR_MSTATUSH) & (1<<CSR_MSTATUSH_MBE)) {
313 40 zero_gravi
    neorv32_uart_printf("big\n");
314
  }
315
  else {
316
    neorv32_uart_printf("little\n");
317
  }
318
 
319
  // CPU extensions
320 42 zero_gravi
  neorv32_uart_printf("Extensions:        ");
321 6 zero_gravi
  tmp = neorv32_cpu_csr_read(CSR_MISA);
322
  for (i=0; i<26; i++) {
323
    if (tmp & (1 << i)) {
324
      c = (char)('A' + i);
325
      neorv32_uart_putc(c);
326
      neorv32_uart_putc(' ');
327
    }
328
  }
329 22 zero_gravi
 
330 40 zero_gravi
  // Z* CPU extensions (from custom "mzext" CSR)
331 22 zero_gravi
  tmp = neorv32_cpu_csr_read(CSR_MZEXT);
332 42 zero_gravi
  if (tmp & (1<<CSR_MZEXT_ZICSR)) {
333 22 zero_gravi
    neorv32_uart_printf("Zicsr ");
334
  }
335 42 zero_gravi
  if (tmp & (1<<CSR_MZEXT_ZIFENCEI)) {
336 22 zero_gravi
    neorv32_uart_printf("Zifencei ");
337
  }
338 44 zero_gravi
  if (tmp & (1<<CSR_MZEXT_ZBB)) {
339
    neorv32_uart_printf("Zbb ");
340
  }
341 6 zero_gravi
 
342 34 zero_gravi
  // check physical memory protection
343 42 zero_gravi
  neorv32_uart_printf("\nPMP:               ");
344
  uint32_t pmp_num_regions = neorv32_cpu_pmp_get_num_regions();
345
  if (pmp_num_regions != 0)  {
346 43 zero_gravi
    neorv32_uart_printf("%u regions, %u bytes minimal granularity\n", pmp_num_regions, neorv32_cpu_pmp_get_granularity());
347 34 zero_gravi
  }
348
  else {
349
    neorv32_uart_printf("not implemented\n");
350
  }
351
 
352 43 zero_gravi
  // check hardware performance monitors
353 42 zero_gravi
  neorv32_uart_printf("HPM Counters:      %u\n", neorv32_cpu_hpm_get_counters());
354 34 zero_gravi
 
355 42 zero_gravi
 
356 34 zero_gravi
  // Misc - system
357 40 zero_gravi
  neorv32_uart_printf("\n\n---- Processor - General ----\n");
358 30 zero_gravi
  neorv32_uart_printf("Clock:   %u Hz\n", SYSINFO_CLK);
359
  neorv32_uart_printf("User ID: 0x%x\n", SYSINFO_USER_CODE);
360 15 zero_gravi
 
361
 
362 6 zero_gravi
  // Memory configuration
363 40 zero_gravi
  neorv32_uart_printf("\n---- Processor - Memory Configuration ----\n");
364 6 zero_gravi
 
365 23 zero_gravi
  neorv32_uart_printf("Instr. base address:  0x%x\n", SYSINFO_ISPACE_BASE);
366 6 zero_gravi
  neorv32_uart_printf("Internal IMEM:        ");
367 12 zero_gravi
  __neorv32_rte_print_true_false(SYSINFO_FEATURES & (1 << SYSINFO_FEATURES_MEM_INT_IMEM));
368 23 zero_gravi
  neorv32_uart_printf("IMEM size:            %u bytes\n", SYSINFO_IMEM_SIZE);
369 6 zero_gravi
  neorv32_uart_printf("Internal IMEM as ROM: ");
370 12 zero_gravi
  __neorv32_rte_print_true_false(SYSINFO_FEATURES & (1 << SYSINFO_FEATURES_MEM_INT_IMEM_ROM));
371 6 zero_gravi
 
372 42 zero_gravi
  neorv32_uart_printf("Data base address:    0x%x\n", SYSINFO_DSPACE_BASE);
373 6 zero_gravi
  neorv32_uart_printf("Internal DMEM:        ");
374 12 zero_gravi
  __neorv32_rte_print_true_false(SYSINFO_FEATURES & (1 << SYSINFO_FEATURES_MEM_INT_DMEM));
375 23 zero_gravi
  neorv32_uart_printf("DMEM size:            %u bytes\n", SYSINFO_DMEM_SIZE);
376 6 zero_gravi
 
377 42 zero_gravi
  neorv32_uart_printf("Internal i-cache:     ");
378 41 zero_gravi
  __neorv32_rte_print_true_false(SYSINFO_FEATURES & (1 << SYSINFO_FEATURES_ICACHE));
379
  if (SYSINFO_FEATURES & (1 << SYSINFO_FEATURES_ICACHE)) {
380
    neorv32_uart_printf("- ");
381
 
382
    uint32_t ic_block_size = (SYSINFO_CACHE >> SYSINFO_CACHE_IC_BLOCK_SIZE_0) & 0x0F;
383
    if (ic_block_size) {
384
      ic_block_size = 1 << ic_block_size;
385
    }
386
    else {
387
      ic_block_size = 0;
388
    }
389
 
390
    uint32_t ic_num_blocks = (SYSINFO_CACHE >> SYSINFO_CACHE_IC_NUM_BLOCKS_0) & 0x0F;
391
    if (ic_num_blocks) {
392
      ic_num_blocks = 1 << ic_num_blocks;
393
    }
394
    else {
395
      ic_num_blocks = 0;
396
    }
397
 
398
    uint32_t ic_associativity = (SYSINFO_CACHE >> SYSINFO_CACHE_IC_ASSOCIATIVITY_0) & 0x0F;
399
    ic_associativity = 1 << ic_associativity;
400
 
401 45 zero_gravi
    neorv32_uart_printf("%u bytes: %u set(s), %u block(s) per set, %u bytes per block", ic_associativity*ic_num_blocks*ic_block_size, ic_associativity, ic_num_blocks, ic_block_size);
402
    if (ic_associativity == 1) {
403
      neorv32_uart_printf(" (direct-mapped)\n");
404 41 zero_gravi
    }
405 45 zero_gravi
    else if (((SYSINFO_CACHE >> SYSINFO_CACHE_IC_REPLACEMENT_0) & 0x0F) == 1) {
406
      neorv32_uart_printf(" (LRU replacement policy)\n");
407 41 zero_gravi
    }
408
    else {
409 45 zero_gravi
      neorv32_uart_printf("\n");
410 41 zero_gravi
    }
411
  }
412
 
413 42 zero_gravi
  neorv32_uart_printf("Bootloader:           ");
414 12 zero_gravi
  __neorv32_rte_print_true_false(SYSINFO_FEATURES & (1 << SYSINFO_FEATURES_BOOTLOADER));
415 6 zero_gravi
 
416 42 zero_gravi
  neorv32_uart_printf("Ext. bus interface:   ");
417 12 zero_gravi
  __neorv32_rte_print_true_false(SYSINFO_FEATURES & (1 << SYSINFO_FEATURES_MEM_EXT));
418 42 zero_gravi
  neorv32_uart_printf("Ext. bus Endianness:  ");
419 40 zero_gravi
  if (SYSINFO_FEATURES & (1 << SYSINFO_FEATURES_MEM_EXT_ENDIAN)) {
420
    neorv32_uart_printf("big\n");
421
  }
422
  else {
423
    neorv32_uart_printf("little\n");
424
  }
425 6 zero_gravi
 
426
  // peripherals
427 40 zero_gravi
  neorv32_uart_printf("\n\n---- Processor - Peripherals ----\n");
428 15 zero_gravi
 
429 12 zero_gravi
  tmp = SYSINFO_FEATURES;
430 6 zero_gravi
 
431 39 zero_gravi
  neorv32_uart_printf("GPIO  - ");
432 12 zero_gravi
  __neorv32_rte_print_true_false(tmp & (1 << SYSINFO_FEATURES_IO_GPIO));
433 6 zero_gravi
 
434 39 zero_gravi
  neorv32_uart_printf("MTIME - ");
435 12 zero_gravi
  __neorv32_rte_print_true_false(tmp & (1 << SYSINFO_FEATURES_IO_MTIME));
436 6 zero_gravi
 
437 39 zero_gravi
  neorv32_uart_printf("UART  - ");
438 12 zero_gravi
  __neorv32_rte_print_true_false(tmp & (1 << SYSINFO_FEATURES_IO_UART));
439 6 zero_gravi
 
440 39 zero_gravi
  neorv32_uart_printf("SPI   - ");
441 12 zero_gravi
  __neorv32_rte_print_true_false(tmp & (1 << SYSINFO_FEATURES_IO_SPI));
442 6 zero_gravi
 
443 39 zero_gravi
  neorv32_uart_printf("TWI   - ");
444 12 zero_gravi
  __neorv32_rte_print_true_false(tmp & (1 << SYSINFO_FEATURES_IO_TWI));
445 6 zero_gravi
 
446 39 zero_gravi
  neorv32_uart_printf("PWM   - ");
447 12 zero_gravi
  __neorv32_rte_print_true_false(tmp & (1 << SYSINFO_FEATURES_IO_PWM));
448 6 zero_gravi
 
449 39 zero_gravi
  neorv32_uart_printf("WDT   - ");
450 12 zero_gravi
  __neorv32_rte_print_true_false(tmp & (1 << SYSINFO_FEATURES_IO_WDT));
451 6 zero_gravi
 
452 39 zero_gravi
  neorv32_uart_printf("TRNG  - ");
453 12 zero_gravi
  __neorv32_rte_print_true_false(tmp & (1 << SYSINFO_FEATURES_IO_TRNG));
454 6 zero_gravi
 
455 47 zero_gravi
  neorv32_uart_printf("CFS   - ");
456
  __neorv32_rte_print_true_false(tmp & (1 << SYSINFO_FEATURES_IO_CFS));
457 6 zero_gravi
}
458
 
459
 
460
/**********************************************************************//**
461
 * NEORV32 runtime environment: Private function to print true or false.
462
 * @note This function is used by neorv32_rte_print_hw_config(void) only.
463
 *
464
 * @param[in] state Print TRUE when !=0, print FALSE when 0
465
 **************************************************************************/
466
static void __neorv32_rte_print_true_false(int state) {
467
 
468
  if (state) {
469 33 zero_gravi
    neorv32_uart_print("True\n");
470 6 zero_gravi
  }
471 2 zero_gravi
  else {
472 33 zero_gravi
    neorv32_uart_print("False\n");
473 2 zero_gravi
  }
474 6 zero_gravi
}
475 2 zero_gravi
 
476
 
477 6 zero_gravi
/**********************************************************************//**
478 33 zero_gravi
 * NEORV32 runtime environment: Private function to print 32-bit number
479
 * as 8-digit hexadecimal value (with "0x" suffix).
480
 *
481
 * @param[in] num Number to print as hexadecimal.
482
 **************************************************************************/
483
void __neorv32_rte_print_hex_word(uint32_t num) {
484
 
485
  static const char hex_symbols[16] = "0123456789ABCDEF";
486
 
487
  neorv32_uart_print("0x");
488
 
489
  int i;
490
  for (i=0; i<8; i++) {
491
    uint32_t index = (num >> (28 - 4*i)) & 0xF;
492
    neorv32_uart_putc(hex_symbols[index]);
493
  }
494
}
495
 
496
 
497 47 zero_gravi
/**********************************************************************//**
498 41 zero_gravi
 * NEORV32 runtime environment: Print the processor version in human-readable format.
499 6 zero_gravi
 **************************************************************************/
500 12 zero_gravi
void neorv32_rte_print_hw_version(void) {
501 6 zero_gravi
 
502
  uint32_t i;
503
  char tmp, cnt;
504
 
505
  for (i=0; i<4; i++) {
506
 
507 33 zero_gravi
    tmp = (char)(neorv32_cpu_csr_read(CSR_MIMPID) >> (24 - 8*i));
508 6 zero_gravi
 
509
    // serial division
510
    cnt = 0;
511 35 zero_gravi
    while (tmp >= 16) {
512
      tmp = tmp - 16;
513 6 zero_gravi
      cnt++;
514
    }
515
 
516
    if (cnt) {
517
      neorv32_uart_putc('0' + cnt);
518
    }
519
    neorv32_uart_putc('0' + tmp);
520
    if (i < 3) {
521
      neorv32_uart_putc('.');
522
    }
523
  }
524 2 zero_gravi
}
525 11 zero_gravi
 
526
 
527
/**********************************************************************//**
528
 * NEORV32 runtime environment: Print project credits
529
 **************************************************************************/
530
void neorv32_rte_print_credits(void) {
531
 
532 42 zero_gravi
  neorv32_uart_print("The NEORV32 Processor Project\n"
533
                     "Copyright 2021, Stephan Nolting\n"
534
                     "BSD 3-Clause License\n"
535 39 zero_gravi
                     "https://github.com/stnolting/neorv32\n\n");
536 11 zero_gravi
}
537
 
538 22 zero_gravi
 
539
/**********************************************************************//**
540 41 zero_gravi
 * NEORV32 runtime environment: Print project logo
541 37 zero_gravi
 **************************************************************************/
542
void neorv32_rte_print_logo(void) {
543
 
544 40 zero_gravi
  const uint32_t logo_data_c[11][4] =
545
  {
546
    {0b00000000000000000000000000000000,0b00000000000000000000000000000000,0b00000000000000000000000110000000,0b00000000000000000000000000000000},
547
    {0b00000000000000000000000000000000,0b00000000000000000000000000000000,0b00000000000000000000000110000000,0b00110001100011000000000000000000},
548
    {0b01100000110001111111110001111111,0b10000111111110001100000011000111,0b11111000011111111000000110000000,0b11111111111111110000000000000000},
549
    {0b11110000110011000000000011000000,0b11001100000011001100000011001100,0b00001100110000001100000110000011,0b11000000000000111100000000000000},
550
    {0b11011000110011000000000011000000,0b11001100000011001100000011000000,0b00001100000000011000000110000000,0b11000111111000110000000000000000},
551
    {0b11001100110011111111100011000000,0b11001111111110001100000011000000,0b11111000000001100000000110000011,0b11000111111000111100000000000000},
552
    {0b11000110110011000000000011000000,0b11001100001100000110000110000000,0b00001100000110000000000110000000,0b11000111111000110000000000000000},
553
    {0b11000011110011000000000011000000,0b11001100000110000011001100001100,0b00001100011000000000000110000011,0b11000000000000111100000000000000},
554
    {0b11000001100001111111110001111111,0b10001100000011000000110000000111,0b11111000111111111100000110000000,0b11111111111111110000000000000000},
555
    {0b00000000000000000000000000000000,0b00000000000000000000000000000000,0b00000000000000000000000110000000,0b00110001100011000000000000000000},
556
    {0b00000000000000000000000000000000,0b00000000000000000000000000000000,0b00000000000000000000000110000000,0b00000000000000000000000000000000}
557
  };
558
 
559
  int u,v,w;
560
  uint32_t tmp;
561
 
562
  for (u=0; u<11; u++) {
563
    neorv32_uart_print("\n");
564
    for (v=0; v<4; v++) {
565
      tmp = logo_data_c[u][v];
566
      for (w=0; w<32; w++){
567 47 zero_gravi
        if (tmp & 0x80000000UL) { // check MSB
568 40 zero_gravi
          neorv32_uart_putc('#');
569
        }
570
        else {
571
          neorv32_uart_putc(' ');
572
        }
573 47 zero_gravi
        tmp <<= 1;
574 40 zero_gravi
      }
575
    }
576
  }
577
  neorv32_uart_print("\n");
578 37 zero_gravi
}
579
 
580
 
581
/**********************************************************************//**
582 22 zero_gravi
 * NEORV32 runtime environment: Print project license
583
 **************************************************************************/
584
void neorv32_rte_print_license(void) {
585
 
586
  neorv32_uart_print(
587
  "\n"
588
  "BSD 3-Clause License\n"
589
  "\n"
590 42 zero_gravi
  "Copyright (c) 2021, Stephan Nolting. All rights reserved.\n"
591 22 zero_gravi
  "\n"
592
  "Redistribution and use in source and binary forms, with or without modification, are\n"
593
  "permitted provided that the following conditions are met:\n"
594
  "\n"
595
  "1. Redistributions of source code must retain the above copyright notice, this list of\n"
596
  "   conditions and the following disclaimer.\n"
597
  "\n"
598
  "2. Redistributions in binary form must reproduce the above copyright notice, this list of\n"
599
  "   conditions and the following disclaimer in the documentation and/or other materials\n"
600
  "   provided with the distribution.\n"
601
  "\n"
602
  "3. Neither the name of the copyright holder nor the names of its contributors may be used to\n"
603
  "   endorse or promote products derived from this software without specific prior written\n"
604
  "   permission.\n"
605
  "\n"
606
  "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS\n"
607
  "OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n"
608
  "MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE\n"
609
  "COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\n"
610
  "EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE\n"
611
  "GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED\n"
612
  "AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\n"
613
  "NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\n"
614
  "OF THE POSSIBILITY OF SUCH DAMAGE.\n"
615
  "\n"
616
  "\n"
617
  );
618
}
619
 
620 44 zero_gravi
 
621
/**********************************************************************//**
622
 * NEORV32 runtime environment: Get MISA CSR value according to *compiler/toolchain configuration*.
623
 *
624
 * @return MISA content according to compiler configuration.
625
 **************************************************************************/
626
uint32_t neorv32_rte_get_compiler_isa(void) {
627
 
628
  uint32_t misa_cc = 0;
629
 
630
#ifdef __riscv_atomic
631
  misa_cc |= 1 << CSR_MISA_A_EXT;
632
#endif
633
 
634
#ifdef __riscv_compressed
635
  misa_cc |= 1 << CSR_MISA_C_EXT;
636
#endif
637
 
638
#ifdef __riscv_32e
639
  misa_cc |= 1 << CSR_MISA_E_EXT;
640
#else
641
  misa_cc |= 1 << CSR_MISA_I_EXT;
642
#endif
643
 
644
#ifdef __riscv_mul
645
  misa_cc |= 1 << CSR_MISA_M_EXT;
646
#endif
647
 
648
#if (__riscv_xlen == 32)
649
  misa_cc |= 1 << CSR_MISA_MXL_LO_EXT;
650
#elif (__riscv_xlen == 64)
651
  misa_cc |= 2 << CSR_MISA_MXL_LO_EXT;
652
#else
653
  misa_cc |= 3 << CSR_MISA_MXL_LO_EXT;
654
#endif
655
 
656
  return misa_cc;
657
}
658
 
659
 
660
/**********************************************************************//**
661
 * NEORV32 runtime environment: Check required ISA extensions (via compiler flags) against available ISA extensions (via MISA csr).
662
 *
663
 * @param[in] silent Show error message (via neorv32.uart) if isa_sw > isa_hw when != 0.
664
 * @return MISA content according to compiler configuration.
665
 **************************************************************************/
666
int neorv32_rte_check_isa(int silent) {
667
 
668
  uint32_t misa_sw = neorv32_rte_get_compiler_isa();
669
  uint32_t misa_hw = neorv32_cpu_csr_read(CSR_MISA);
670
 
671
  // mask hardware features that are not used by software
672
  uint32_t check = misa_hw & misa_sw;
673
 
674
  //
675
  if (check == misa_sw) {
676
    return 0;
677
  }
678
  else {
679
    if (silent == 0) {
680
      neorv32_uart_printf("\nWARNING! SW_ISA (features required) vs HW_ISA (features available) mismatch!\n"
681
                          "SW_ISA = 0x%x (compiler flags)\n"
682
                          "HW_ISA = 0x%x (misa csr)\n\n", misa_sw, misa_hw);
683
    }
684
    return 1;
685
  }
686
}
687
 

powered by: WebSVN 2.1.0

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