mirror of
https://github.com/stnolting/neorv32.git
synced 2025-04-23 13:47:33 -04:00
removed hw_analysis and exception_test projects; they are replaced by the cpu_test project
This commit is contained in:
parent
dbf1d47b01
commit
d7cff7622a
2 changed files with 901 additions and 0 deletions
579
sw/example/cpu_test/main.c
Normal file
579
sw/example/cpu_test/main.c
Normal file
|
@ -0,0 +1,579 @@
|
|||
// #################################################################################################
|
||||
// # << NEORV32 - CPU Test Program >> #
|
||||
// # ********************************************************************************************* #
|
||||
// # BSD 3-Clause License #
|
||||
// # #
|
||||
// # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
||||
// # #
|
||||
// # Redistribution and use in source and binary forms, with or without modification, are #
|
||||
// # permitted provided that the following conditions are met: #
|
||||
// # #
|
||||
// # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
||||
// # conditions and the following disclaimer. #
|
||||
// # #
|
||||
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
||||
// # conditions and the following disclaimer in the documentation and/or other materials #
|
||||
// # provided with the distribution. #
|
||||
// # #
|
||||
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
||||
// # endorse or promote products derived from this software without specific prior written #
|
||||
// # permission. #
|
||||
// # #
|
||||
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
||||
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
||||
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
||||
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
||||
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
||||
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
||||
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
||||
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
||||
// # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
||||
// # ********************************************************************************************* #
|
||||
// # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
||||
// #################################################################################################
|
||||
|
||||
|
||||
/**********************************************************************//**
|
||||
* @file cpu_test/main.c
|
||||
* @author Stephan Nolting
|
||||
* @brief Simple CPU interrupts and exceptions test program.
|
||||
**************************************************************************/
|
||||
|
||||
#include <neorv32.h>
|
||||
|
||||
|
||||
/**********************************************************************//**
|
||||
* @name User configuration
|
||||
**************************************************************************/
|
||||
/**@{*/
|
||||
/** UART BAUD rate */
|
||||
#define BAUD_RATE 19200
|
||||
//** Set 1 for detailed exception debug information */
|
||||
#define DETAILED_EXCEPTION_DEBUG 0
|
||||
//** Reachable unaligned address */
|
||||
#define ADDR_UNALIGNED 0x00000001
|
||||
//** Unreachable aligned address */
|
||||
#define ADDR_UNREACHABLE 0xFFFFFF00
|
||||
/**@}*/
|
||||
|
||||
|
||||
/**********************************************************************//**
|
||||
* @name Exception handler acknowledges
|
||||
**************************************************************************/
|
||||
/**@{*/
|
||||
/** Exception handler answers / identifiers */
|
||||
enum EXC_HANDLER_ANSWERS {
|
||||
ANSWER_I_MISALIGN = 0x12345678, /**< Answer for misaligned instruction address excetion */
|
||||
ANSWER_I_ACCESS = 0xAABB1133, /**< Answer for instruction access fault excetion */
|
||||
ANSWER_I_ILLEGAL = 0x0199203B, /**< Answer for illegal instruction excetion */
|
||||
ANSWER_BREAKPOINT = 0x12322330, /**< Answer for breakpoint excetion */
|
||||
ANSWER_L_MISALIGN = 0xBABCCCCC, /**< Answer for misaligned load address excetion */
|
||||
ANSWER_L_ACCESS = 0xDEF728AA, /**< Answer for load access fault excetion */
|
||||
ANSWER_S_MISALIGN = 0xFF0927DD, /**< Answer for misaligned store address excetion */
|
||||
ANSWER_S_ACCESS = 0x20091777, /**< Answer for store access fault excetion */
|
||||
ANSWER_ENVCALL = 0x55662244, /**< Answer for environment call excetion */
|
||||
ANSWER_MSI = 0xCDECDEA9, /**< Answer for machine software interrupt */
|
||||
ANSWER_MTI = 0x0012FA53, /**< Answer for machine timer interrupt */
|
||||
ANSWER_CLIC = 0xEEF33088 /**< Answer for machine external interrupt */
|
||||
};
|
||||
/** Gloabl volatile variable to store exception handler answer */
|
||||
volatile uint32_t exception_handler_answer;
|
||||
/**@}*/
|
||||
|
||||
|
||||
// Prototypes
|
||||
void exc_handler_i_misalign(void);
|
||||
void exc_handler_i_access(void);
|
||||
void exc_handler_i_illegal(void);
|
||||
void exc_handler_breakpoint(void);
|
||||
void exc_handler_l_misalign(void);
|
||||
void exc_handler_l_access(void);
|
||||
void exc_handler_s_misalign(void);
|
||||
void exc_handler_s_access(void);
|
||||
void exc_handler_envcall(void);
|
||||
void exc_handler_msi(void);
|
||||
void exc_handler_mti(void);
|
||||
void irq_handler_clic_ch0();
|
||||
|
||||
|
||||
/**********************************************************************//**
|
||||
* Unreachable memory-mapped register that should be always available
|
||||
**************************************************************************/
|
||||
#define MMR_UNREACHABLE (*(IO_REG32 (ADDR_UNREACHABLE)))
|
||||
|
||||
|
||||
/**********************************************************************//**
|
||||
* This program uses mostly synthetic case to trigger all implemented exceptions.
|
||||
* Each exception is captured and evaluated for correct detection.
|
||||
*
|
||||
* @note This program requires the UART, MTIME and CLIC to be synthesized.
|
||||
*
|
||||
* @return Irrelevant.
|
||||
**************************************************************************/
|
||||
int main() {
|
||||
|
||||
register uint32_t tmp_a;
|
||||
volatile uint32_t dummy_dst __attribute__((unused));
|
||||
|
||||
int cnt_fail = 0;
|
||||
int cnt_ok = 0;
|
||||
int cnt_test = 0;
|
||||
|
||||
// check if UART unit is implemented at all
|
||||
if (neorv32_uart_available() == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// check if CLIC unit is implemented at all
|
||||
if (neorv32_clic_available() == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// check if MTIME unit is implemented at all
|
||||
if (neorv32_mtime_available() == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// init UART at default baud rate, no rx interrupt, no tx interrupt
|
||||
neorv32_uart_setup(BAUD_RATE, 0, 0);
|
||||
|
||||
|
||||
// set CMP of machine system timer MTIME to max to prevent an IRQ
|
||||
uint64_t mtime_cmp_max = 0xFFFFFFFFFFFFFFFFL;
|
||||
neorv32_mtime_set_timecmp(mtime_cmp_max);
|
||||
|
||||
// intro
|
||||
neorv32_uart_printf("\n\n------ CPU TEST ------n\n");
|
||||
|
||||
// show full HW config report
|
||||
neorv32_rte_print_hw_config();
|
||||
|
||||
// intro2
|
||||
neorv32_uart_printf("\n\nNEORV32 exceptions and interrupts test program\n\n");
|
||||
|
||||
// install exception handler functions
|
||||
int install_err = 0;
|
||||
install_err += neorv32_rte_exception_install(EXCID_I_MISALIGNED, exc_handler_i_misalign);
|
||||
install_err += neorv32_rte_exception_install(EXCID_I_ACCESS, exc_handler_i_access);
|
||||
install_err += neorv32_rte_exception_install(EXCID_I_ILLEGAL, exc_handler_i_illegal);
|
||||
install_err += neorv32_rte_exception_install(EXCID_BREAKPOINT, exc_handler_breakpoint);
|
||||
install_err += neorv32_rte_exception_install(EXCID_L_MISALIGNED, exc_handler_l_misalign);
|
||||
install_err += neorv32_rte_exception_install(EXCID_L_ACCESS, exc_handler_l_access);
|
||||
install_err += neorv32_rte_exception_install(EXCID_S_MISALIGNED, exc_handler_s_misalign);
|
||||
install_err += neorv32_rte_exception_install(EXCID_S_ACCESS, exc_handler_s_access);
|
||||
install_err += neorv32_rte_exception_install(EXCID_MENV_CALL, exc_handler_envcall);
|
||||
install_err += neorv32_rte_exception_install(EXCID_MSI, exc_handler_msi);
|
||||
install_err += neorv32_rte_exception_install(EXCID_MTI, exc_handler_mti);
|
||||
//install_err += neorv32_rte_exception_install(EXCID_MEI, -); done by neorv32_clic_handler_install
|
||||
|
||||
if (install_err) {
|
||||
neorv32_uart_printf("install error!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// install interrupt handler for clic WDT channel
|
||||
install_err += neorv32_clic_handler_install(CLIC_CH_WDT, irq_handler_clic_ch0);
|
||||
|
||||
if (install_err) {
|
||||
neorv32_uart_printf("CLIC install error!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#if (DETAILED_EXCEPTION_DEBUG==1)
|
||||
// enable debug mode for uninitialized exception/interrupt vectors
|
||||
// and overwrite previous exception handler installations
|
||||
// -> any exception/interrupt will show a message from the neorv32 runtime environment
|
||||
neorv32_rte_enable_debug_mode();
|
||||
#endif
|
||||
|
||||
|
||||
// enable global interrupts
|
||||
neorv32_cpu_eint();
|
||||
|
||||
exception_handler_answer = 0;
|
||||
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// Unaligned instruction address
|
||||
// ----------------------------------------------------------
|
||||
neorv32_uart_printf("EXC I_ALIGN: ");
|
||||
cnt_test++;
|
||||
|
||||
// call unaligned address
|
||||
((void (*)(void))ADDR_UNALIGNED)();
|
||||
|
||||
#if (DETAILED_EXCEPTION_DEBUG==0)
|
||||
if (exception_handler_answer == ANSWER_I_MISALIGN) {
|
||||
neorv32_uart_printf("ok\n");
|
||||
cnt_ok++;
|
||||
}
|
||||
else {
|
||||
neorv32_uart_printf("fail\n");
|
||||
cnt_fail++;
|
||||
}
|
||||
exception_handler_answer = 0;
|
||||
#endif
|
||||
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// Instruction access fault
|
||||
// ----------------------------------------------------------
|
||||
neorv32_uart_printf("EXC I_ACC: ");
|
||||
cnt_test++;
|
||||
|
||||
// call unreachable aligned address
|
||||
((void (*)(void))ADDR_UNREACHABLE)();
|
||||
|
||||
#if (DETAILED_EXCEPTION_DEBUG==0)
|
||||
if (exception_handler_answer == ANSWER_I_ACCESS) {
|
||||
neorv32_uart_printf("ok\n");
|
||||
cnt_ok++;
|
||||
}
|
||||
else {
|
||||
neorv32_uart_printf("fail\n");
|
||||
cnt_fail++;
|
||||
}
|
||||
exception_handler_answer = 0;
|
||||
#endif
|
||||
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// Illegal instruction
|
||||
// ----------------------------------------------------------
|
||||
neorv32_uart_printf("EXC I_ILLEG: ");
|
||||
cnt_test++;
|
||||
|
||||
// create test program in RAM
|
||||
static const uint32_t dummy_sub_program[2] = {
|
||||
0xDEAD007F, // undefined 32-bit opcode -> illegal instruction exception
|
||||
0x00008067 // ret (32-bit)
|
||||
};
|
||||
|
||||
tmp_a = (uint32_t)&dummy_sub_program; // call the dummy sub program
|
||||
asm volatile ( "jalr ra, %0 " : "=r" (tmp_a) : "r" (tmp_a));
|
||||
|
||||
#if (DETAILED_EXCEPTION_DEBUG==0)
|
||||
if (exception_handler_answer == ANSWER_I_ILLEGAL) {
|
||||
neorv32_uart_printf("ok\n");
|
||||
cnt_ok++;
|
||||
}
|
||||
else {
|
||||
neorv32_uart_printf("fail\n");
|
||||
cnt_fail++;
|
||||
}
|
||||
exception_handler_answer = 0;
|
||||
#endif
|
||||
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// Breakpoint instruction
|
||||
// ----------------------------------------------------------
|
||||
neorv32_uart_printf("EXC BREAK: ");
|
||||
cnt_test++;
|
||||
|
||||
asm volatile("EBREAK");
|
||||
|
||||
#if (DETAILED_EXCEPTION_DEBUG==0)
|
||||
if (exception_handler_answer == ANSWER_BREAKPOINT) {
|
||||
neorv32_uart_printf("ok\n");
|
||||
cnt_ok++;
|
||||
}
|
||||
else {
|
||||
neorv32_uart_printf("fail\n");
|
||||
cnt_fail++;
|
||||
}
|
||||
exception_handler_answer = 0;
|
||||
#endif
|
||||
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// Unaligned load address
|
||||
// ----------------------------------------------------------
|
||||
neorv32_uart_printf("EXC L_ALIGN: ");
|
||||
cnt_test++;
|
||||
|
||||
// load from unaligned address
|
||||
asm volatile ("lh zero, %[input_i](zero)" : : [input_i] "i" (ADDR_UNALIGNED));
|
||||
|
||||
#if (DETAILED_EXCEPTION_DEBUG==0)
|
||||
if (exception_handler_answer == ANSWER_L_MISALIGN) {
|
||||
neorv32_uart_printf("ok\n");
|
||||
cnt_ok++;
|
||||
}
|
||||
else {
|
||||
neorv32_uart_printf("fail\n");
|
||||
cnt_fail++;
|
||||
}
|
||||
exception_handler_answer = 0;
|
||||
#endif
|
||||
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// Load access fault
|
||||
// ----------------------------------------------------------
|
||||
neorv32_uart_printf("EXC L_ACC: ");
|
||||
cnt_test++;
|
||||
|
||||
// load from unreachable aligned address
|
||||
dummy_dst = MMR_UNREACHABLE;
|
||||
|
||||
#if (DETAILED_EXCEPTION_DEBUG==0)
|
||||
if (exception_handler_answer == ANSWER_L_ACCESS) {
|
||||
neorv32_uart_printf("ok\n");
|
||||
cnt_ok++;
|
||||
}
|
||||
else {
|
||||
neorv32_uart_printf("fail\n");
|
||||
cnt_fail++;
|
||||
}
|
||||
exception_handler_answer = 0;
|
||||
#endif
|
||||
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// Unaligned store address
|
||||
// ----------------------------------------------------------
|
||||
neorv32_uart_printf("EXC S_ALIGN: ");
|
||||
cnt_test++;
|
||||
|
||||
// store to unaligned address
|
||||
asm volatile ("sh zero, %[input_i](zero)" : : [input_i] "i" (ADDR_UNALIGNED));
|
||||
|
||||
#if (DETAILED_EXCEPTION_DEBUG==0)
|
||||
if (exception_handler_answer == ANSWER_S_MISALIGN) {
|
||||
neorv32_uart_printf("ok\n");
|
||||
cnt_ok++;
|
||||
}
|
||||
else {
|
||||
neorv32_uart_printf("fail\n");
|
||||
cnt_fail++;
|
||||
}
|
||||
exception_handler_answer = 0;
|
||||
#endif
|
||||
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// Store access fault
|
||||
// ----------------------------------------------------------
|
||||
neorv32_uart_printf("EXC S_ACC: ");
|
||||
cnt_test++;
|
||||
|
||||
// store to unreachable aligned address
|
||||
MMR_UNREACHABLE = 0;
|
||||
|
||||
#if (DETAILED_EXCEPTION_DEBUG==0)
|
||||
if (exception_handler_answer == ANSWER_S_ACCESS) {
|
||||
neorv32_uart_printf("ok\n");
|
||||
cnt_ok++;
|
||||
}
|
||||
else {
|
||||
neorv32_uart_printf("fail\n");
|
||||
cnt_fail++;
|
||||
}
|
||||
exception_handler_answer = 0;
|
||||
#endif
|
||||
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// Environment call
|
||||
// ----------------------------------------------------------
|
||||
neorv32_uart_printf("EXC ENVCALL: ");
|
||||
cnt_test++;
|
||||
|
||||
asm volatile("ECALL");
|
||||
|
||||
#if (DETAILED_EXCEPTION_DEBUG==0)
|
||||
if (exception_handler_answer == ANSWER_ENVCALL) {
|
||||
neorv32_uart_printf("ok\n");
|
||||
cnt_ok++;
|
||||
}
|
||||
else {
|
||||
neorv32_uart_printf("fail\n");
|
||||
cnt_fail++;
|
||||
}
|
||||
exception_handler_answer = 0;
|
||||
#endif
|
||||
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// Machine software interrupt
|
||||
// ----------------------------------------------------------
|
||||
neorv32_uart_printf("IRQ MSI: ");
|
||||
cnt_test++;
|
||||
|
||||
// trigger machine software interrupt
|
||||
neorv32_cpu_sw_irq();
|
||||
|
||||
#if (DETAILED_EXCEPTION_DEBUG==0)
|
||||
if (exception_handler_answer == ANSWER_MSI) {
|
||||
neorv32_uart_printf("ok\n");
|
||||
cnt_ok++;
|
||||
}
|
||||
else {
|
||||
neorv32_uart_printf("fail\n");
|
||||
cnt_fail++;
|
||||
}
|
||||
exception_handler_answer = 0;
|
||||
#endif
|
||||
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// Machine timer interrupt (MTIME)
|
||||
// ----------------------------------------------------------
|
||||
neorv32_uart_printf("IRQ MTI: ");
|
||||
cnt_test++;
|
||||
|
||||
// force MTIME IRQ
|
||||
neorv32_mtime_set_timecmp(0);
|
||||
|
||||
// wait some time for the IRQ to arrive the CPU
|
||||
asm volatile("nop");
|
||||
asm volatile("nop");
|
||||
asm volatile("nop");
|
||||
asm volatile("nop");
|
||||
|
||||
#if (DETAILED_EXCEPTION_DEBUG==0)
|
||||
if (exception_handler_answer == ANSWER_MTI) {
|
||||
neorv32_uart_printf("ok\n");
|
||||
cnt_ok++;
|
||||
}
|
||||
else {
|
||||
neorv32_uart_printf("fail\n");
|
||||
cnt_fail++;
|
||||
}
|
||||
exception_handler_answer = 0;
|
||||
#endif
|
||||
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// Machine external interrupt (via CLIC)
|
||||
// ----------------------------------------------------------
|
||||
neorv32_uart_printf("IRQ MEI: ");
|
||||
cnt_test++;
|
||||
|
||||
// manually trigger CLIC channel (watchdog interrupt)
|
||||
neorv32_clic_trigger_irq(CLIC_CH_WDT);
|
||||
|
||||
// wait some time for the IRQ to arrive the CPU
|
||||
asm volatile("nop");
|
||||
asm volatile("nop");
|
||||
asm volatile("nop");
|
||||
asm volatile("nop");
|
||||
|
||||
#if (DETAILED_EXCEPTION_DEBUG==0)
|
||||
if (exception_handler_answer == ANSWER_CLIC) {
|
||||
neorv32_uart_printf("ok\n");
|
||||
cnt_ok++;
|
||||
}
|
||||
else {
|
||||
neorv32_uart_printf("fail\n");
|
||||
cnt_fail++;
|
||||
}
|
||||
exception_handler_answer = 0;
|
||||
#endif
|
||||
|
||||
|
||||
// error report
|
||||
neorv32_uart_printf("\n\nTests: %i\nOK: %i\nFAIL: %i\n\n", cnt_test, cnt_ok, cnt_fail);
|
||||
|
||||
// final result
|
||||
if (cnt_fail == 0) {
|
||||
neorv32_uart_printf("TEST OK!\n");
|
||||
}
|
||||
else {
|
||||
neorv32_uart_printf("TEST FAILED!\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**********************************************************************//**
|
||||
* Misaligned instruction address exception handler.
|
||||
**************************************************************************/
|
||||
void exc_handler_i_misalign(void) {
|
||||
exception_handler_answer = ANSWER_I_MISALIGN;
|
||||
}
|
||||
|
||||
/**********************************************************************//**
|
||||
* Instruction access fault exception handler.
|
||||
**************************************************************************/
|
||||
void exc_handler_i_access(void) {
|
||||
exception_handler_answer = ANSWER_I_ACCESS;
|
||||
}
|
||||
|
||||
/**********************************************************************//**
|
||||
* Illegal instruction exception handler.
|
||||
**************************************************************************/
|
||||
void exc_handler_i_illegal(void) {
|
||||
exception_handler_answer = ANSWER_I_ILLEGAL;
|
||||
}
|
||||
|
||||
/**********************************************************************//**
|
||||
* Breakpoint exception handler.
|
||||
**************************************************************************/
|
||||
void exc_handler_breakpoint(void) {
|
||||
exception_handler_answer = ANSWER_BREAKPOINT;
|
||||
}
|
||||
|
||||
/**********************************************************************//**
|
||||
* Misaligned load address exception handler.
|
||||
**************************************************************************/
|
||||
void exc_handler_l_misalign(void) {
|
||||
exception_handler_answer = ANSWER_L_MISALIGN;
|
||||
}
|
||||
|
||||
/**********************************************************************//**
|
||||
* Load instruction access fault exception handler.
|
||||
**************************************************************************/
|
||||
void exc_handler_l_access(void) {
|
||||
exception_handler_answer = ANSWER_L_ACCESS;
|
||||
}
|
||||
|
||||
/**********************************************************************//**
|
||||
* Misaligned store address exception handler.
|
||||
**************************************************************************/
|
||||
void exc_handler_s_misalign(void) {
|
||||
exception_handler_answer = ANSWER_S_MISALIGN;
|
||||
}
|
||||
|
||||
/**********************************************************************//**
|
||||
* Store address access fault exception handler.
|
||||
**************************************************************************/
|
||||
void exc_handler_s_access(void) {
|
||||
exception_handler_answer = ANSWER_S_ACCESS;
|
||||
}
|
||||
|
||||
/**********************************************************************//**
|
||||
* Environment call exception handler.
|
||||
**************************************************************************/
|
||||
void exc_handler_envcall(void) {
|
||||
exception_handler_answer = ANSWER_ENVCALL;
|
||||
}
|
||||
|
||||
/**********************************************************************//**
|
||||
* Machine software interrupt exception handler.
|
||||
**************************************************************************/
|
||||
void exc_handler_msi(void) {
|
||||
exception_handler_answer = ANSWER_MSI;
|
||||
}
|
||||
|
||||
/**********************************************************************//**
|
||||
* Machine timer interrupt exception handler.
|
||||
**************************************************************************/
|
||||
void exc_handler_mti(void) {
|
||||
exception_handler_answer = ANSWER_MTI;
|
||||
// set CMP of machine system timer MTIME to max to prevent an IRQ
|
||||
neorv32_mtime_set_timecmp(-1);
|
||||
}
|
||||
|
||||
/**********************************************************************//**
|
||||
* CLIC interrupt handler for channel 0.
|
||||
**************************************************************************/
|
||||
void irq_handler_clic_ch0(void) {
|
||||
exception_handler_answer = ANSWER_CLIC;
|
||||
}
|
||||
|
322
sw/example/cpu_test/makefile
Normal file
322
sw/example/cpu_test/makefile
Normal file
|
@ -0,0 +1,322 @@
|
|||
#################################################################################################
|
||||
# << NEORV32 - Application Makefile >> #
|
||||
# ********************************************************************************************* #
|
||||
# Make sure to add the riscv GCC compiler's bin folder to your PATH environment variable. #
|
||||
# ********************************************************************************************* #
|
||||
# BSD 3-Clause License #
|
||||
# #
|
||||
# Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
||||
# #
|
||||
# Redistribution and use in source and binary forms, with or without modification, are #
|
||||
# permitted provided that the following conditions are met: #
|
||||
# #
|
||||
# 1. Redistributions of source code must retain the above copyright notice, this list of #
|
||||
# conditions and the following disclaimer. #
|
||||
# #
|
||||
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
||||
# conditions and the following disclaimer in the documentation and/or other materials #
|
||||
# provided with the distribution. #
|
||||
# #
|
||||
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
||||
# endorse or promote products derived from this software without specific prior written #
|
||||
# permission. #
|
||||
# #
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
||||
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
||||
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
||||
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
||||
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
||||
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
||||
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
||||
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
||||
# OF THE POSSIBILITY OF SUCH DAMAGE. #
|
||||
# ********************************************************************************************* #
|
||||
# The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
||||
#################################################################################################
|
||||
|
||||
|
||||
# *****************************************************************************
|
||||
# USER CONFIGURATION
|
||||
# *****************************************************************************
|
||||
# Compiler effort
|
||||
EFFORT = -Os
|
||||
|
||||
# User's application sources (add additional files here)
|
||||
APP_SRC = $(wildcard *.c)
|
||||
|
||||
# User's application include folders (don't forget the '-I' before each entry)
|
||||
APP_INC = -I .
|
||||
|
||||
# Compiler toolchain (use default if not set by user)
|
||||
RISCV_TOOLCHAIN ?= riscv32-unknown-elf
|
||||
|
||||
# CPU architecture and ABI
|
||||
MARCH = -march=rv32i
|
||||
MABI = -mabi=ilp32
|
||||
|
||||
# Path to runtime c library (use default if not set by user)
|
||||
LIBC_PATH ?= $(dir $(shell which $(CC)))../$(RISCV_TOOLCHAIN)/lib/libc.a
|
||||
LIBGCC_PATH ?= $(dir $(shell which $(CC)))../lib/gcc/$(RISCV_TOOLCHAIN)/*/libgcc.a
|
||||
|
||||
# Relative or absolute path to the NEORV32 home folder (use default if not set by user)
|
||||
NEORV32_HOME ?= ../../..
|
||||
# *****************************************************************************
|
||||
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# NEORV32 framework
|
||||
# -----------------------------------------------------------------------------
|
||||
# Path to NEORV32 linker script and startup file
|
||||
NEORV32_COM_PATH=$(NEORV32_HOME)/sw/common
|
||||
# Path to main NEORV32 library include files
|
||||
NEORV32_INC_PATH=$(NEORV32_HOME)/sw/lib/include
|
||||
# Path to main NEORV32 library source files
|
||||
NEORV32_SRC_PATH=$(NEORV32_HOME)/sw/lib/source
|
||||
# Path to NEORV32 executable generator
|
||||
NEORV32_EXG_PATH=$(NEORV32_HOME)/sw/image_gen
|
||||
# Path to NEORV32 core rtl folder
|
||||
NEORV32_RTL_PATH=$(NEORV32_HOME)/rtl/core
|
||||
# Marker file to verify NEORV32 home folder
|
||||
NEORV32_HOME_MARKER=$(NEORV32_INC_PATH)/neorv32.h
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Add NEORV32 sources to input SRCs
|
||||
# -----------------------------------------------------------------------------
|
||||
APP_SRC += $(wildcard $(NEORV32_SRC_PATH)/*.c)
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Make defaults
|
||||
# -----------------------------------------------------------------------------
|
||||
.SUFFIXES:
|
||||
.PHONY: all
|
||||
.DEFAULT_GOAL := help
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Application output definitions
|
||||
# -----------------------------------------------------------------------------
|
||||
APP_EXE = neorv32_exe.bin
|
||||
APP_ASM = main.s
|
||||
|
||||
compile: $(APP_ASM) $(APP_EXE)
|
||||
install: $(APP_ASM) neorv32_application_image.vhd
|
||||
all: $(APP_ASM) $(APP_EXE) neorv32_application_image.vhd
|
||||
|
||||
# define all object files
|
||||
OBJ = $(APP_SRC:.c=.o)
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Tools and flags
|
||||
# -----------------------------------------------------------------------------
|
||||
# compiler tools
|
||||
CC = $(RISCV_TOOLCHAIN)-gcc
|
||||
LD = $(RISCV_TOOLCHAIN)-ld
|
||||
OBJDUMP = $(RISCV_TOOLCHAIN)-objdump
|
||||
OBJCOPY = $(RISCV_TOOLCHAIN)-objcopy
|
||||
SIZE = $(RISCV_TOOLCHAIN)-size
|
||||
|
||||
# NEORV32 executable image generator
|
||||
IMAGE_GEN = $(NEORV32_EXG_PATH)/image_gen
|
||||
|
||||
# Compiler flags
|
||||
CC_OPTS = $(MARCH) $(MABI) $(EFFORT) -Wall -ffunction-sections -fdata-sections -lm
|
||||
|
||||
# Linker flags
|
||||
LD_OPTS = $(EFFORT) --gc-sections
|
||||
|
||||
# User flags for additional config
|
||||
USER_FLAGS =
|
||||
CC_OPTS += $(USER_FLAGS)
|
||||
|
||||
# Use embedded RISC-V CPU extension?
|
||||
ifeq (,$(findstring rv32e,$(MARCH)))
|
||||
CC_OPTS +=
|
||||
else
|
||||
CC_OPTS += -D__RISCV_EMBEDDED_CPU__
|
||||
endif
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Host native compiler
|
||||
# -----------------------------------------------------------------------------
|
||||
CC_X86 = gcc -Wall -O -g
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Tool targets
|
||||
# -----------------------------------------------------------------------------
|
||||
# install/compile tools
|
||||
$(IMAGE_GEN): $(NEORV32_EXG_PATH)/image_gen.cpp
|
||||
@echo Compiling $(IMAGE_GEN)
|
||||
@$(CC_X86) $< -o $(IMAGE_GEN)
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Application targets: Assemble, compile, link, dump
|
||||
# -----------------------------------------------------------------------------
|
||||
# Assemble startup code
|
||||
crt0.elf: $(NEORV32_COM_PATH)/crt0.S
|
||||
@$(CC) $(CC_OPTS) -c $< -o $@
|
||||
|
||||
# Compile app sources
|
||||
$(OBJ): %.o : %.c crt0.elf
|
||||
@$(CC) -c $(CC_OPTS) -I $(NEORV32_INC_PATH) $(APP_INC) $< -o $@
|
||||
|
||||
# Link object files and show memory utilization
|
||||
main.elf: $(OBJ)
|
||||
@$(LD) $(LD_OPTS) -I $(NEORV32_INC_PATH) $(APP_INC) -T $(NEORV32_COM_PATH)/neorv32.ld $(OBJ) $(LIBC_PATH) $(LIBGCC_PATH) -o $@
|
||||
@echo "Memory utilization:"
|
||||
@$(SIZE) main.elf
|
||||
|
||||
# Assembly listing file (for debugging)
|
||||
$(APP_ASM): main.elf
|
||||
@$(OBJDUMP) -D -S -z $< > $@
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Application targets: Generate binary executable, install (as VHDL file)
|
||||
# -----------------------------------------------------------------------------
|
||||
# Generate final executable: text, rodata, data (in THIS order!)
|
||||
main.bin: main.elf
|
||||
@$(OBJCOPY) -I elf32-little $< -j .text -O binary text.bin
|
||||
@$(OBJCOPY) -I elf32-little $< -j .rodata -O binary rodata.bin
|
||||
@$(OBJCOPY) -I elf32-little $< -j .data -O binary data.bin
|
||||
@cat text.bin rodata.bin data.bin > $@
|
||||
@rm -f text.bin rodata.bin data.bin
|
||||
|
||||
# Generate NEORV32 executable image for bootloader update
|
||||
$(APP_EXE): main.bin $(IMAGE_GEN)
|
||||
@set -e
|
||||
@$(IMAGE_GEN) -app_bin $< $@ $(shell basename $(CURDIR))
|
||||
@echo "Executable ($(APP_EXE)) size in bytes:"
|
||||
@wc -c < $(APP_EXE)
|
||||
|
||||
# Generate NEORV32 executable VHDL boot image
|
||||
neorv32_application_image.vhd: main.bin $(IMAGE_GEN)
|
||||
@set -e
|
||||
@$(IMAGE_GEN) -app_img $< $@ $(shell basename $(CURDIR))
|
||||
@echo "Installing application image to $(NEORV32_RTL_PATH)/neorv32_application_image.vhd"
|
||||
@cp neorv32_application_image.vhd $(NEORV32_RTL_PATH)/.
|
||||
@rm -f neorv32_application_image.vhd
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Bootloader targets
|
||||
# -----------------------------------------------------------------------------
|
||||
# Assemble startup code
|
||||
bootloader_crt0.elf: $(NEORV32_COM_PATH)/bootloader_crt0.S
|
||||
@$(CC) $(CC_OPTS) -c $< -o $@
|
||||
|
||||
# Compile and install bootloader
|
||||
bootloader: bootloader_crt0.elf $(OBJ) $(IMAGE_GEN)
|
||||
@set -e
|
||||
@$(LD) $(LD_OPTS) -I $(NEORV32_INC_PATH) $(APP_INC) -T $(NEORV32_COM_PATH)/bootloader_neorv32.ld $(OBJ) $(LIBC_PATH) $(LIBGCC_PATH) -o bootloader.elf
|
||||
@echo "Memory utilization:"
|
||||
@$(SIZE) bootloader.elf
|
||||
@$(OBJDUMP) -D -S -z bootloader.elf > bootloader.s
|
||||
@$(OBJCOPY) -I elf32-little bootloader.elf -j .text -O binary text.bin
|
||||
@$(OBJCOPY) -I elf32-little bootloader.elf -j .rodata -O binary rodata.bin
|
||||
@$(OBJCOPY) -I elf32-little bootloader.elf -j .data -O binary data.bin
|
||||
@cat text.bin rodata.bin data.bin > bootloader.bin
|
||||
@$(IMAGE_GEN) -bld_img bootloader.bin neorv32_bootloader_image.vhd $(shell basename $(CURDIR))
|
||||
@echo "Installing bootloader image to $(NEORV32_RTL_PATH)/neorv32_bootloader_image.vhd"
|
||||
@cp neorv32_bootloader_image.vhd $(NEORV32_RTL_PATH)/.
|
||||
@rm -f neorv32_bootloader_image.vhd text.bin rodata.bin data.bin
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Check toolchain
|
||||
# -----------------------------------------------------------------------------
|
||||
check: $(IMAGE_GEN)
|
||||
@echo "---------------- Check: NEORV32_HOME folder ----------------"
|
||||
ifneq ($(shell [ -e $(NEORV32_HOME_MARKER) ] && echo 1 || echo 0 ), 1)
|
||||
$(error NEORV32_HOME folder not found!)
|
||||
endif
|
||||
@echo "NEORV32_HOME: $(NEORV32_HOME)"
|
||||
@echo "---------------- Check: $(CC) ----------------"
|
||||
@$(CC) -v
|
||||
@echo "---------------- Check: $(LD) ----------------"
|
||||
@$(LD) -V
|
||||
@echo "---------------- Check: $(OBJDUMP) ----------------"
|
||||
@$(OBJDUMP) -V
|
||||
@echo "---------------- Check: $(OBJCOPY) ----------------"
|
||||
@$(OBJCOPY) -V
|
||||
@echo "---------------- Check: $(SIZE) ----------------"
|
||||
@$(SIZE) -V
|
||||
@echo "---------------- Check: NEORV32 image_gen ----------------"
|
||||
@$(IMAGE_GEN) -help
|
||||
@echo "---------------- Check: native gcc ----------------"
|
||||
@$(CC_X86) -v
|
||||
@echo
|
||||
@echo "Toolchain check OK"
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Show configuration
|
||||
# -----------------------------------------------------------------------------
|
||||
info:
|
||||
@echo "---------------- Info: Project ----------------"
|
||||
@echo "Project: $(shell basename $(CURDIR))"
|
||||
@echo "Project source files: $(APP_SRC)"
|
||||
@echo "Project include folders: $(NEORV32_INC_PATH) $(APP_INC)"
|
||||
@echo "Project object files: $(OBJ)"
|
||||
@echo "---------------- Info: NEORV32 ----------------"
|
||||
@echo "NEORV32 home folder (NEORV32_HOME): $(NEORV32_HOME)"
|
||||
@echo "IMAGE_GEN: $(IMAGE_GEN)"
|
||||
@echo "---------------- Info: RISC-V CPU ----------------"
|
||||
@echo "MARCH: $(MARCH)"
|
||||
@echo "MABI: $(MABI)"
|
||||
@echo "---------------- Info: RISC-V Toolchain ----------------"
|
||||
@echo "Toolchain: $(RISCV_TOLLCHAIN)"
|
||||
@echo "CC: $(CC)"
|
||||
@echo "LD: $(LD)"
|
||||
@echo "OBJDUMP: $(OBJDUMP)"
|
||||
@echo "OBJCOPY: $(OBJCOPY)"
|
||||
@echo "SIZE: $(SIZE)"
|
||||
@echo "---------------- Info: C Lib ----------------"
|
||||
@echo "CLIB: $(LIBC_PATH)"
|
||||
@echo "GCCLIB: $(LIBGCC_PATH)"
|
||||
@echo "---------------- Info: Flags ----------------"
|
||||
@echo "CC_OPTS: $(CC_OPTS)"
|
||||
@echo "LD_OPTS: $(LD_OPTS)"
|
||||
@echo "---------------- Info: Host Native GCC ----------------"
|
||||
@echo "CC_X86: $(CC_X86)"
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Show final ELF details (just for debugging)
|
||||
# -----------------------------------------------------------------------------
|
||||
elf_info: main.elf
|
||||
@$(OBJDUMP) -x main.elf
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Help
|
||||
# -----------------------------------------------------------------------------
|
||||
help:
|
||||
@echo "<<< NEORV32 Application Makefile >>>"
|
||||
@echo "Make sure to add the bin folder of RISC-V GCC to your PATH variable."
|
||||
@echo "Targets:"
|
||||
@echo " help - show this text"
|
||||
@echo " check - check toolchain"
|
||||
@echo " info - show makefile/toolchain configuration"
|
||||
@echo " compile - compile and generate <neorv32_exe.bin> executable for upload via bootloader"
|
||||
@echo " install - compile, generate and install VHDL IMEM boot image"
|
||||
@echo " all - compile and generate <neorv32_exe.bin> executable for upload via bootloader and generate and install VHDL IMEM boot image"
|
||||
@echo " clean - clean up project"
|
||||
@echo " clean_all - clean up project, core libraries and image generator"
|
||||
@echo " bootloader - compile, generate and install VHDL BOOTROM bott image (for bootloader only!)"
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Clean up
|
||||
# -----------------------------------------------------------------------------
|
||||
clean:
|
||||
@rm -f *.elf *.o *.bin *.out *.s
|
||||
|
||||
clean_all: clean
|
||||
@rm -f $(OBJ) $(IMAGE_GEN)
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue