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

Subversion Repositories neorv32

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

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

powered by: WebSVN 2.1.0

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