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

Subversion Repositories neorv32

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

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
// # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
7
// #                                                                                               #
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
// Privates
46 6 zero_gravi
static void __neorv32_rte_dummy_exc_handler(void)     __attribute__((unused));
47
static void __neorv32_rte_debug_exc_handler(void)     __attribute__((unused));
48
static void __neorv32_rte_print_true_false(int state) __attribute__((unused));
49
static void __neorv32_rte_print_hw_version(void)      __attribute__((unused));
50 2 zero_gravi
 
51
 
52
/**********************************************************************//**
53
 * Setup NEORV32 runtime environment in debug mode.
54
 *
55
 * @note This function installs a debug handler for ALL exception and interrupt sources, which
56
 * gives detailed information about the exception/interrupt. Call this function before you
57
 * install custom handler functions via neorv32_rte_exception_install(uint8_t exc_id, void (*handler)(void)),
58
 * since this function will override all installed exception handlers.
59
 *
60
 * @warning This function should be used for debugging only, since it only shows the uninitialize exception/interrupt, but
61
 * does not resolve the cause. Hence, it cannot guarantee to resume normal application execution after showing the debug messages.
62
 **************************************************************************/
63
void neorv32_rte_enable_debug_mode(void) {
64
 
65
  uint8_t id;
66
 
67
  // install debug handler for all sources
68
  for (id=0; id<32; id++) {
69
    neorv32_rte_exception_install(id, __neorv32_rte_debug_exc_handler);
70
  }
71
}
72
 
73
 
74
/**********************************************************************//**
75
 * Install exception handler function to NEORV32 runtime environment.
76
 *
77
 * @note This function automatically activates the according CSR.mie bits when installing handlers for
78
 * the MTIME (MTI), CLIC (MEI) or machine software interrupt (MSI). The global interrupt enable bit mstatus.mie has
79
 * to be set by the user via neorv32_cpu_eint(void).
80
 *
81
 * @param[in] exc_id Identifier (type) of the targeted exception. See #NEORV32_EXCEPTION_IDS_enum.
82
 * @param[in] handler The actual handler function for the specified exception (function must be of type "void function(void);").
83
 * return 0 if success, 1 if error (invalid exc_id or targeted exception not supported).
84
 **************************************************************************/
85
int neorv32_rte_exception_install(uint8_t exc_id, void (*handler)(void)) {
86
 
87
  // id valid?
88
  if ((exc_id == EXCID_I_MISALIGNED) || (exc_id == EXCID_I_ACCESS)     || (exc_id == EXCID_I_ILLEGAL) ||
89
      (exc_id == EXCID_BREAKPOINT)   || (exc_id == EXCID_L_MISALIGNED) || (exc_id == EXCID_L_ACCESS)  ||
90
      (exc_id == EXCID_S_MISALIGNED) || (exc_id == EXCID_S_ACCESS)     || (exc_id == EXCID_MENV_CALL) ||
91
      (exc_id == EXCID_MSI)          || (exc_id == EXCID_MTI)          || (exc_id == EXCID_MEI)) {
92
 
93
    if (exc_id == EXCID_MSI) { neorv32_cpu_irq_enable(CPU_MIE_MSIE); } // activate software interrupt
94
    if (exc_id == EXCID_MTI) { neorv32_cpu_irq_enable(CPU_MIE_MTIE); } // activate timer interrupt
95
    if (exc_id == EXCID_MEI) { neorv32_cpu_irq_enable(CPU_MIE_MEIE); } // activate external interrupt
96
 
97
    uint32_t vt_base = neorv32_cpu_csr_read(CSR_MDSPACEBASE); // base address of vector table
98
    vt_base = vt_base + (((uint32_t)exc_id) << 2);
99
    (*(IO_REG32 (vt_base))) = (uint32_t)handler;
100
 
101
    return 0;
102
  }
103
  return 1;
104
}
105
 
106
 
107
/**********************************************************************//**
108
 * Uninstall exception handler function from NEORV32 runtime environment, which was
109
 * previously installed via neorv32_rte_exception_install(uint8_t exc_id, void (*handler)(void)).
110
 *
111
 * @note This function automatically clears the according CSR.mie bits when uninstalling handlers for
112
 * the MTIME (MTI), CLIC (MEI) or machine software interrupt (MSI). The global interrupt enable bit mstatus.mie has
113
 * to be cleared by the user via neorv32_cpu_dint(void).
114
 *
115
 * @param[in] exc_id Identifier (type) of the targeted exception. See #NEORV32_EXCEPTION_IDS_enum.
116
 * return 0 if success, 1 if error (invalid exc_id or targeted exception not supported).
117
 **************************************************************************/
118
int neorv32_rte_exception_uninstall(uint8_t exc_id) {
119
 
120
  // id valid?
121
  if ((exc_id == EXCID_I_MISALIGNED) || (exc_id == EXCID_I_ACCESS)     || (exc_id == EXCID_I_ILLEGAL) ||
122
      (exc_id == EXCID_BREAKPOINT)   || (exc_id == EXCID_L_MISALIGNED) || (exc_id == EXCID_L_ACCESS)  ||
123
      (exc_id == EXCID_S_MISALIGNED) || (exc_id == EXCID_S_ACCESS)     || (exc_id == EXCID_MENV_CALL) ||
124
      (exc_id == EXCID_MSI)          || (exc_id == EXCID_MTI)          || (exc_id == EXCID_MEI)) {
125
 
126
    if (exc_id == EXCID_MSI) { neorv32_cpu_irq_disable(CPU_MIE_MSIE); } // deactivate software interrupt
127
    if (exc_id == EXCID_MTI) { neorv32_cpu_irq_disable(CPU_MIE_MTIE); } // deactivate timer interrupt
128
    if (exc_id == EXCID_MEI) { neorv32_cpu_irq_disable(CPU_MIE_MEIE); } // deactivate external interrupt
129
 
130
    uint32_t vt_base = neorv32_cpu_csr_read(CSR_MDSPACEBASE); // base address of vector table
131
    vt_base = vt_base + (((uint32_t)exc_id) << 2);
132
    (*(IO_REG32 (vt_base))) = (uint32_t)(&__neorv32_rte_dummy_exc_handler); // use dummy handler in case the exception is triggered
133
 
134
    return 0;
135
  }
136
  return 1;
137
}
138
 
139
 
140
/**********************************************************************//**
141
 * NEORV32 runtime environment: Dummy exception handler (does nothing).
142
 * @note This function is used by neorv32_rte_exception_uninstall(uint8_t exc_id) only.
143
 **************************************************************************/
144
static void __neorv32_rte_dummy_exc_handler(void) {
145
 
146
  asm volatile("nop");
147
}
148
 
149
 
150
/**********************************************************************//**
151
 * NEORV32 runtime environment: Debug exception handler, printing various exception/interrupt information via UART.
152
 * @note This function is used by neorv32_rte_enable_debug_mode(void) only.
153
 **************************************************************************/
154
static void __neorv32_rte_debug_exc_handler(void) {
155
 
156 6 zero_gravi
  neorv32_uart_printf("\n\n<< NEORV32 Runtime Environment >>\n");
157 2 zero_gravi
 
158
  neorv32_uart_printf("System time: 0x%x_%x\n", neorv32_cpu_csr_read(CSR_TIMEH), neorv32_cpu_csr_read(CSR_TIME));
159
 
160 7 zero_gravi
  register uint32_t trap_cause = neorv32_cpu_csr_read(CSR_MCAUSE);
161
  register uint32_t trap_addr  = neorv32_cpu_csr_read(CSR_MEPC);
162
  register uint32_t trap_inst;
163 2 zero_gravi
 
164 7 zero_gravi
  // get faulting instruction
165
  asm volatile ("lh %[result], 0(%[input_i])" : [result] "=r" (trap_inst) : [input_i] "r" (trap_addr));
166
 
167
  if (trap_cause & 0x80000000) {
168 2 zero_gravi
    neorv32_uart_printf("INTERRUPT");
169
  }
170
  else {
171
    neorv32_uart_printf("EXCEPTION");
172 7 zero_gravi
    if ((trap_inst & 3) == 3) {
173
      trap_addr -= 4;
174 6 zero_gravi
    }
175
    else {
176 7 zero_gravi
      trap_addr -= 2;
177 6 zero_gravi
    }
178 2 zero_gravi
  }
179 7 zero_gravi
  neorv32_uart_printf(" at instruction address: 0x%x\n", trap_addr);
180 2 zero_gravi
 
181
  neorv32_uart_printf("Cause: ");
182 7 zero_gravi
  switch (trap_cause) {
183 2 zero_gravi
    case 0x00000000: neorv32_uart_printf("Instruction address misaligned"); break;
184
    case 0x00000001: neorv32_uart_printf("Instruction access fault"); break;
185
    case 0x00000002: neorv32_uart_printf("Illegal instruction"); break;
186
    case 0x00000003: neorv32_uart_printf("Breakpoint (EBREAK)"); break;
187
    case 0x00000004: neorv32_uart_printf("Load address misaligned"); break;
188
    case 0x00000005: neorv32_uart_printf("Load access fault"); break;
189
    case 0x00000006: neorv32_uart_printf("Store address misaligned"); break;
190
    case 0x00000007: neorv32_uart_printf("Store access fault"); break;
191
    case 0x0000000B: neorv32_uart_printf("Environment call (ECALL)"); break;
192
    case 0x80000003: neorv32_uart_printf("Machine software interrupt"); break;
193
    case 0x80000007: neorv32_uart_printf("Machine timer interrupt (via MTIME)"); break;
194
    case 0x8000000B: neorv32_uart_printf("Machine external interrupt (via CLIC)"); break;
195 7 zero_gravi
    default:         neorv32_uart_printf("Unknown (0x%x)", trap_cause); break;
196 2 zero_gravi
  }
197
 
198
  // fault address
199 7 zero_gravi
  neorv32_uart_printf("\nFaulting instruction: 0x%x\n", trap_inst);
200
  neorv32_uart_printf("MTVAL: 0x%x\n", neorv32_cpu_csr_read(CSR_MTVAL));
201 2 zero_gravi
 
202 7 zero_gravi
  if ((trap_inst & 3) != 3) {
203 6 zero_gravi
    neorv32_uart_printf("(decompressed)\n");
204 2 zero_gravi
  }
205 6 zero_gravi
 
206
  neorv32_uart_printf("Trying to resume application @ 0x%x...", neorv32_cpu_csr_read(CSR_MEPC));
207
 
208
  neorv32_uart_printf("\n<</NEORV32 Runtime Environment >>\n\n");
209
}
210
 
211
 
212
/**********************************************************************//**
213
 * NEORV32 runtime environment: Print hardware configuration information via UART
214
 **************************************************************************/
215
void neorv32_rte_print_hw_config(void) {
216
 
217
  uint32_t tmp;
218
  int i;
219
  char c;
220
 
221
  neorv32_uart_printf("\n\n<< NEORV32 Hardware Configuration Overview >>\n");
222
 
223
  // CPU configuration
224
  neorv32_uart_printf("\n-- Central Processing Unit --\n");
225
 
226
  // Hart ID
227
  neorv32_uart_printf("Hart ID:          0x%x\n", neorv32_cpu_csr_read(CSR_MHARTID));
228
 
229
  // HW version
230
  neorv32_uart_printf("Hardware version: ");
231
  __neorv32_rte_print_hw_version();
232
  neorv32_uart_printf(" (0x%x)\n", neorv32_cpu_csr_read(CSR_MIMPID));
233
 
234
  // CPU architecture
235
  neorv32_uart_printf("Architecture:     ");
236
  tmp = neorv32_cpu_csr_read(CSR_MISA);
237
  tmp = (tmp >> 30) & 0x03;
238
  if (tmp == 0) {
239
    neorv32_uart_printf("unknown");
240
  }
241
  if (tmp == 1) {
242
    neorv32_uart_printf("RV32");
243
  }
244
  if (tmp == 2) {
245
    neorv32_uart_printf("RV64");
246
  }
247
  if (tmp == 3) {
248
    neorv32_uart_printf("RV128");
249
  }
250
 
251
  // CPU extensions
252
  neorv32_uart_printf("\nCPU extensions:   ");
253
  tmp = neorv32_cpu_csr_read(CSR_MISA);
254
  for (i=0; i<26; i++) {
255
    if (tmp & (1 << i)) {
256
      c = (char)('A' + i);
257
      neorv32_uart_putc(c);
258
      neorv32_uart_putc(' ');
259
    }
260
  }
261
  neorv32_uart_printf("(0x%x)\n", tmp);
262
 
263
  // Performance counters
264
  neorv32_uart_printf("CNT & time CSRs:  ");
265
  __neorv32_rte_print_true_false(neorv32_cpu_csr_read(CSR_MFEATURES) & (1 << CPU_MFEATURES_CSR_COUNTERS));
266
 
267
  // Clock speed
268
  neorv32_uart_printf("Clock speed:      %u Hz\n", neorv32_cpu_csr_read(CSR_MCLOCK));
269
 
270
  // Memory configuration
271
  neorv32_uart_printf("\n-- Memory Configuration --\n");
272
 
273
  uint32_t size = neorv32_cpu_csr_read(CSR_MISPACESIZE);
274
  uint32_t base = neorv32_cpu_csr_read(CSR_MISPACEBASE);
275
  neorv32_uart_printf("Instruction memory:   %u bytes @ 0x%x\n", size, base);
276
  neorv32_uart_printf("Internal IMEM:        ");
277
  __neorv32_rte_print_true_false(neorv32_cpu_csr_read(CSR_MFEATURES) & (1 << CPU_MFEATURES_MEM_INT_IMEM));
278
  neorv32_uart_printf("Internal IMEM as ROM: ");
279
  __neorv32_rte_print_true_false(neorv32_cpu_csr_read(CSR_MFEATURES) & (1 << CPU_MFEATURES_MEM_INT_IMEM_ROM));
280
 
281
  size = neorv32_cpu_csr_read(CSR_MDSPACESIZE);
282
  base = neorv32_cpu_csr_read(CSR_MDSPACEBASE);
283
  neorv32_uart_printf("Data memory:          %u bytes @ 0x%x\n", size, base);
284
  neorv32_uart_printf("Internal DMEM:        ");
285
  __neorv32_rte_print_true_false(neorv32_cpu_csr_read(CSR_MFEATURES) & (1 << CPU_MFEATURES_MEM_INT_DMEM));
286
 
287
  neorv32_uart_printf("Bootloader:           ");
288
  __neorv32_rte_print_true_false(neorv32_cpu_csr_read(CSR_MFEATURES) & (1 << CPU_MFEATURES_BOOTLOADER));
289
 
290
  neorv32_uart_printf("External interface:   ");
291
  __neorv32_rte_print_true_false(neorv32_cpu_csr_read(CSR_MFEATURES) & (1 << CPU_MFEATURES_MEM_EXT));
292
 
293
  // peripherals
294
  neorv32_uart_printf("\n-- Peripherals --\n");
295
  tmp = neorv32_cpu_csr_read(CSR_MFEATURES);
296
 
297
  neorv32_uart_printf("GPIO:    ");
298
  __neorv32_rte_print_true_false(tmp & (1 << CPU_MFEATURES_IO_GPIO));
299
 
300
  neorv32_uart_printf("MTIME:   ");
301
  __neorv32_rte_print_true_false(tmp & (1 << CPU_MFEATURES_IO_MTIME));
302
 
303
  neorv32_uart_printf("UART:    ");
304
  __neorv32_rte_print_true_false(tmp & (1 << CPU_MFEATURES_IO_UART));
305
 
306
  neorv32_uart_printf("SPI:     ");
307
  __neorv32_rte_print_true_false(tmp & (1 << CPU_MFEATURES_IO_SPI));
308
 
309
  neorv32_uart_printf("TWI:     ");
310
  __neorv32_rte_print_true_false(tmp & (1 << CPU_MFEATURES_IO_TWI));
311
 
312
  neorv32_uart_printf("PWM:     ");
313
  __neorv32_rte_print_true_false(tmp & (1 << CPU_MFEATURES_IO_PWM));
314
 
315
  neorv32_uart_printf("WDT:     ");
316
  __neorv32_rte_print_true_false(tmp & (1 << CPU_MFEATURES_IO_WDT));
317
 
318
  neorv32_uart_printf("CLIC:    ");
319
  __neorv32_rte_print_true_false(tmp & (1 << CPU_MFEATURES_IO_CLIC));
320
 
321
  neorv32_uart_printf("TRNG:    ");
322
  __neorv32_rte_print_true_false(tmp & (1 << CPU_MFEATURES_IO_TRNG));
323
 
324
  neorv32_uart_printf("DEVNULL: ");
325
  __neorv32_rte_print_true_false(tmp & (1 << CPU_MFEATURES_IO_DEVNULL));
326
}
327
 
328
 
329
/**********************************************************************//**
330
 * NEORV32 runtime environment: Private function to print true or false.
331
 * @note This function is used by neorv32_rte_print_hw_config(void) only.
332
 *
333
 * @param[in] state Print TRUE when !=0, print FALSE when 0
334
 **************************************************************************/
335
static void __neorv32_rte_print_true_false(int state) {
336
 
337
  if (state) {
338
    neorv32_uart_printf("True\n");
339
  }
340 2 zero_gravi
  else {
341 6 zero_gravi
    neorv32_uart_printf("False\n");
342 2 zero_gravi
  }
343 6 zero_gravi
}
344 2 zero_gravi
 
345
 
346 6 zero_gravi
/**********************************************************************//**
347
 * NEORV32 runtime environment: Private function to show the processor version in human-readable format.
348
 * @note This function is used by neorv32_rte_print_hw_config(void) only.
349
 **************************************************************************/
350
static void __neorv32_rte_print_hw_version(void) {
351
 
352
  uint32_t i;
353
  char tmp, cnt;
354
  uint32_t version = neorv32_cpu_csr_read(CSR_MIMPID);
355
 
356
  for (i=0; i<4; i++) {
357
 
358
    tmp = (char)(version >> (24 - 8*i));
359
 
360
    // serial division
361
    cnt = 0;
362
    while (tmp >= 10) {
363
      tmp = tmp - 10;
364
      cnt++;
365
    }
366
 
367
    if (cnt) {
368
      neorv32_uart_putc('0' + cnt);
369
    }
370
    neorv32_uart_putc('0' + tmp);
371
    if (i < 3) {
372
      neorv32_uart_putc('.');
373
    }
374
  }
375 2 zero_gravi
}

powered by: WebSVN 2.1.0

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