URL
https://opencores.org/ocsvn/neorv32/neorv32/trunk
Subversion Repositories neorv32
Compare Revisions
- This comparison shows the changes necessary to convert path
/neorv32/trunk/sw/example
- from Rev 71 to Rev 72
- ↔ Reverse comparison
Rev 71 → Rev 72
/demo_cfu/main.c
0,0 → 1,183
// ################################################################################################# |
// # << NEORV32 - CFU Custom Instructions Example Program >> # |
// # ********************************************************************************************* # |
// # BSD 3-Clause License # |
// # # |
// # Copyright (c) 2022, 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 demo_cfu/main.c |
* @author Stephan Nolting |
* @brief Example program showing how to use the CFU's custom instructions. |
**************************************************************************/ |
#include <neorv32.h> |
|
|
/**********************************************************************//** |
* @name User configuration |
**************************************************************************/ |
/**@{*/ |
/** UART BAUD rate */ |
#define BAUD_RATE 19200 |
/** Number of test cases per CFU instruction */ |
#define TESTCASES 4 |
/**@}*/ |
|
|
/**********************************************************************//** |
* @name Prototypes |
**************************************************************************/ |
uint32_t xorshift32(void); |
|
|
/**********************************************************************//** |
* Main function |
* |
* @note This program requires the CFU and UART0. |
* |
* @return 0 if execution was successful |
**************************************************************************/ |
int main() { |
|
// initialize NEORV32 run-time environment |
neorv32_rte_setup(); |
|
// setup UART0 at default baud rate, no parity bits, no HW flow control |
neorv32_uart0_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE); |
|
// check if UART0 is implemented |
if (neorv32_uart0_available() == 0) { |
return 1; // UART0 not available, exit |
} |
|
// check if the CFU is implemented at all |
// note that the CFU is wrapped in the core's "Zxcfu" ISA extension |
if (neorv32_cpu_cfu_available() == 0) { |
neorv32_uart0_printf("ERROR! CFU ('Zxcfu' ISA extensions) not implemented!\n"); |
return 1; |
} |
|
|
// intro |
neorv32_uart0_printf("\n<<< NEORV32 Custom Functions Unit (CFU) 'Custom Instructions' Example Program >>>\n\n"); |
|
neorv32_uart0_printf("NOTE: This program assumes the _default_ CFU hardware module, which implements\n" |
" four simple data conversion instructions.\n\n"); |
|
neorv32_uart0_printf("NOTE: This program (and it's comments) just shows how to USE the CFU's custom\n" |
" instructions. The actual implementation of these instructions is done\n" |
" in the CFU hardware module (-> rtl/core/neorv32_cpu_cp_cfu.vhd).\n\n"); |
|
|
// custom instructions usage examples |
uint32_t i, opa, opb; |
|
neorv32_uart0_printf("\n--- CFU 'binary to gray' instruction (funct3 = 000) ---\n"); |
for (i=0; i<TESTCASES; i++) { |
opa = xorshift32(); // get random test data |
opb = 0; |
neorv32_uart0_printf("%u: neorv32_cfu_cmd0 - OPA = 0x%x, OPB = 0x%x, ", i, opa, opb); |
|
// The CFU custom instruction can be used as plain C functions! |
// |
// There are 8 "prototypes" for the CFU instructions: |
// - neorv32_cfu_cmd0(funct7, rs1, rs2) - sets the instruction's "funct3" bit field to 000 |
// - neorv32_cfu_cmd1(funct7, rs1, rs2) - sets the instruction's "funct3" bit field to 001 |
// - ... |
// - neorv32_cfu_cmd7(funct7, rs1, rs2) - sets the instruction's "funct3" bit field to 111 |
// |
// These functions are turned into 32-bit instruction words resembling a R2-type RISC-V instruction |
// (=> "intrinsics"). |
// |
// Each neorv32_cfu_cmd* function requires three arguments: |
// - funct7: a compile-time static 7-bit immediate (put in the instruction's "funct7" bit field) |
// - rs1: a 32-bit operand A (this is the first register file source rs1) |
// - rs2: a 32-bit operand B (this is the first register second source rs2) |
// |
// The operands can be literals, variables, function return values, ... you name it. |
// |
// Each neorv32_cfu_cmd* function returns a 32-bit uint32_t data word, which represents |
// the result of the according instruction. |
// |
// The 7-bit immediate ("funct7") can be used to pass small _static_ literals to the CFU |
// or to do a more fine-grained function selection - it all depends on your hardware implementation! ;) |
neorv32_uart0_printf("Result = 0x%x\n", neorv32_cfu_cmd0(0b0000000, opa, opb)); |
} |
|
neorv32_uart0_printf("\n--- CFU 'gray to binary' instruction (funct3 = 001) ---\n"); |
for (i=0; i<TESTCASES; i++) { |
opa = xorshift32(); |
neorv32_uart0_printf("%u: neorv32_cfu_cmd1 - OPA = 0x%x, OPB = 0x%x, ", i, opa, 0); |
// you can also pass literals instead of variables to the intrinsics (0 instead of opb): |
neorv32_uart0_printf("Result = 0x%x\n", neorv32_cfu_cmd1(0b0000000, opa, 0)); |
} |
|
neorv32_uart0_printf("\n--- CFU 'bit reversal' instruction (funct3 = 010) ---\n"); |
for (i=0; i<TESTCASES; i++) { |
opa = xorshift32(); |
neorv32_uart0_printf("%u: neorv32_cfu_cmd2 - OPA = 0x%x, OPB = 0x%x, ", i, opa, 0); |
// here we are setting the funct7 bit-field to all-one; however, this is not |
// used at all by the default CFU hardware module |
// note that all funct3/funct7 combinations are treated as "valid" by the CPU |
// - so there is no chance of causing an illegal instruction exception by using the CFU intrinsics |
neorv32_uart0_printf("Result = 0x%x\n", neorv32_cfu_cmd2(0b1111111, opa, 0)); |
} |
|
neorv32_uart0_printf("\n--- CFU 'XNOR' instruction (funct3 = 011) ---\n"); |
for (i=0; i<TESTCASES; i++) { |
opa = xorshift32(); |
opb = xorshift32(); |
neorv32_uart0_printf("%u: neorv32_cfu_cmd3 - OPA = 0x%x, OPB = 0x%x, ", i, opa, opb); |
neorv32_uart0_printf("Result = 0x%x\n", neorv32_cfu_cmd3(0b0000000, opa, opb)); |
} |
|
|
neorv32_uart0_printf("\nCFU demo program completed.\n"); |
|
return 0; |
} |
|
|
/**********************************************************************//** |
* Pseudo-random number generator (to generate deterministic test data). |
* |
* @return Random data (32-bit). |
**************************************************************************/ |
uint32_t xorshift32(void) { |
|
static uint32_t x32 = 314159265; |
|
x32 ^= x32 << 13; |
x32 ^= x32 >> 17; |
x32 ^= x32 << 5; |
|
return x32; |
} |
/demo_cfu/makefile
0,0 → 1,40
################################################################################################# |
# << NEORV32 - Application Makefile >> # |
# ********************************************************************************************* # |
# Make sure to add the RISC-V GCC compiler's bin folder to your PATH environment variable. # |
# ********************************************************************************************* # |
# BSD 3-Clause License # |
# # |
# Copyright (c) 2021, 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 # |
################################################################################################# |
|
# Modify this variable to fit your NEORV32 setup (neorv32 home folder) |
NEORV32_HOME ?= ../../.. |
|
include $(NEORV32_HOME)/sw/common/common.mk |
/demo_gptmr/main.c
3,7 → 3,7
// # ********************************************************************************************* # |
// # BSD 3-Clause License # |
// # # |
// # Copyright (c) 2021, Stephan Nolting. All rights reserved. # |
// # Copyright (c) 2022, 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: # |
58,7 → 58,7
/**********************************************************************//** |
* This program blinks an LED at GPIO.output(0) at 1Hz using the general purpose timer interrupt. |
* |
* @note This program requires the GPTMR unit to be synthesized. |
* @note This program requires the GPTMR unit to be synthesized (and UART0 and GPIO). |
* |
* @return Should not return; |
**************************************************************************/ |
67,13 → 67,13
// capture all exceptions and give debug info via UART |
neorv32_rte_setup(); |
|
// init UART at default baud rate, no parity bits, ho hw flow control |
// init UART at default baud rate, no parity bits, no HW flow control |
neorv32_uart0_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE); |
|
|
// check if GPTMR unit is implemented at all |
if (neorv32_gptmr_available() == 0) { |
neorv32_uart0_print("General purpose timer not implemented!\n"); |
neorv32_uart0_print("ERROR! General purpose timer not implemented!\n"); |
return 1; |
} |
|
89,18 → 89,18
// install GPTMR interrupt handler |
neorv32_rte_exception_install(GPTMR_RTE_ID, gptmr_firq_handler); |
|
// configure timer for 1Hz in continuous mode |
uint32_t soc_clock = NEORV32_SYSINFO.CLK; |
soc_clock = soc_clock / 2; // divide by two as we are using the 1/2 clock prescaler |
neorv32_gptmr_setup(CLK_PRSC_2, 1, soc_clock); |
// configure timer for 1Hz ticks in continuous mode (with clock divisor = 8) |
neorv32_gptmr_setup(CLK_PRSC_8, 1, NEORV32_SYSINFO.CLK / (8 * 2)); |
|
// enable interrupt |
neorv32_cpu_irq_enable(GPTMR_FIRQ_ENABLE); // enable GPTRM FIRQ channel |
neorv32_cpu_irq_enable(GPTMR_FIRQ_ENABLE); // enable GPTMR FIRQ channel |
neorv32_cpu_eint(); // enable global interrupt flag |
|
|
// do nothing, wait for interrupt |
while(1); |
// go to sleep mode and wait for interrupt |
while(1) { |
neorv32_cpu_sleep(); |
} |
|
return 0; |
} |
115,6 → 115,6
|
neorv32_cpu_csr_write(CSR_MIP, 1<<GPTMR_FIRQ_PENDING); // clear/ack pending FIRQ |
|
neorv32_uart0_putc('.'); // send tick symbol via UART |
neorv32_uart0_putc('.'); // send tick symbol via UART0 |
neorv32_gpio_pin_toggle(0); // toggle output port bit 0 |
} |
/demo_mtime/main.c
0,0 → 1,124
// ################################################################################################# |
// # << NEORV32 - RISC-V Machine Timer (MTIME) Demo Program >> # |
// # ********************************************************************************************* # |
// # BSD 3-Clause License # |
// # # |
// # Copyright (c) 2022, 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 demo_mtime/main.c |
* @author Stephan Nolting |
* @brief Simple machine timer (MTIME) usage example. |
**************************************************************************/ |
|
#include <neorv32.h> |
|
|
/**********************************************************************//** |
* @name User configuration |
**************************************************************************/ |
/**@{*/ |
/** UART BAUD rate */ |
#define BAUD_RATE 19200 |
/**@}*/ |
|
|
// Prototypes |
void mtime_irq_handler(void); |
|
|
/**********************************************************************//** |
* This program blinks an LED at GPIO.output(0) at 1Hz using the machine timer interrupt. |
* |
* @note This program requires the MTIME unit to be synthesized (and UART0 and GPIO). |
* |
* @return Should not return; |
**************************************************************************/ |
int main() { |
|
// capture all exceptions and give debug info via UART |
neorv32_rte_setup(); |
|
// init UART at default baud rate, no parity bits, no hw flow control |
neorv32_uart0_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE); |
|
|
// check if MTIME unit is implemented at all |
if (neorv32_mtime_available() == 0) { |
neorv32_uart0_print("ERROR! MTIME timer not implemented!\n"); |
return 1; |
} |
|
// Intro |
neorv32_uart0_print("RISC-V Machine System Timer (MTIME) demo Program.\n" |
"Toggles GPIO.output(0) at 1Hz using the RISC-V 'MTI' interrupt.\n\n"); |
|
|
// clear GPIO output port |
neorv32_gpio_port_set(0); |
|
|
// install MTIME interrupt handler to RTE |
neorv32_rte_exception_install(RTE_TRAP_MTI, mtime_irq_handler); |
|
// configure MTIME timer's first interrupt to appear after SYSTEM_CLOCK / 2 cycles (toggle at 2Hz) |
// starting from _now_ |
neorv32_mtime_set_timecmp(neorv32_mtime_get_time() + (NEORV32_SYSINFO.CLK / 2)); |
|
// enable interrupt |
neorv32_cpu_irq_enable(CSR_MIE_MTIE); // enable MTIME interrupt |
neorv32_cpu_eint(); // enable global interrupt flag |
|
|
// go to sleep mode and wait for interrupt |
while(1) { |
neorv32_cpu_sleep(); |
} |
|
return 0; |
} |
|
|
/**********************************************************************//** |
* MTIME IRQ handler. |
* |
* @warning This function has to be of type "void xyz(void)" and must not use any interrupt attributes! |
**************************************************************************/ |
void mtime_irq_handler(void) { |
|
// update MTIMECMP value for next IRQ (in SYSTEM_CLOCK / 2 cycles) |
// this will also ack/clear the current MTIME interrupt request |
neorv32_mtime_set_timecmp(neorv32_mtime_get_timecmp() + (NEORV32_SYSINFO.CLK / 2)); |
|
|
neorv32_uart0_putc('.'); // send tick symbol via UART |
neorv32_gpio_pin_toggle(0); // toggle output port bit 0 |
} |
/demo_mtime/makefile
0,0 → 1,40
################################################################################################# |
# << NEORV32 - Application Makefile >> # |
# ********************************************************************************************* # |
# Make sure to add the RISC-V GCC compiler's bin folder to your PATH environment variable. # |
# ********************************************************************************************* # |
# BSD 3-Clause License # |
# # |
# Copyright (c) 2021, 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 # |
################################################################################################# |
|
# Modify this variable to fit your NEORV32 setup (neorv32 home folder) |
NEORV32_HOME ?= ../../.. |
|
include $(NEORV32_HOME)/sw/common/common.mk |
/dhrystone/README.md
18,7 → 18,7
|
### Exemplary Output |
|
Output generated for processor HW version [v1.5.9.1](https://github.com/stnolting/neorv32/blob/master/CHANGELOG.md) |
Output generated for processor HW version [v1.5.9.1](https://github.com/stnolting/neorv32/blob/main/CHANGELOG.md) |
using performance-optimized configuration options. |
|
``` |
/floating_point_test/main.c
3,7 → 3,7
// # ********************************************************************************************* # |
// # BSD 3-Clause License # |
// # # |
// # Copyright (c) 2021, Stephan Nolting. All rights reserved. # |
// # Copyright (c) 2022, 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: # |
123,7 → 123,7
neorv32_rte_check_isa(0); // silent = 0 -> show message if isa mismatch |
|
// check if Zfinx extension is implemented at all |
if ((NEORV32_SYSINFO.CPU & (1<<SYSINFO_CPU_ZFINX)) == 0) { |
if ((neorv32_cpu_csr_read(CSR_MXISA) & (1<<CSR_MXISA_ZFINX)) == 0) { |
neorv32_uart0_print("Error! <Zfinx> extension not synthesized!\n"); |
return 1; |
} |
/newlib_demo/main.c
0,0 → 1,139
// ################################################################################################# |
// # << NEORV32 - Newlib Demo/Test Program >> # |
// # ********************************************************************************************* # |
// # BSD 3-Clause License # |
// # # |
// # Copyright (c) 2022, 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 newlib_demo/main.c |
* @author Stephan Nolting |
* @brief Demo/test program for NEORV32's newlib C standard library support. |
**************************************************************************/ |
#include <neorv32.h> |
#include <unistd.h> |
#include <stdlib.h> |
|
|
/**********************************************************************//** |
* @name User configuration |
**************************************************************************/ |
/**@{*/ |
/** UART BAUD rate */ |
#define BAUD_RATE 19200 |
/**@}*/ |
|
|
/**********************************************************************//** |
* Main function: Check some of newlib's core functions. |
* |
* @note This program requires UART. |
* |
* @return 0 if execution was successful |
**************************************************************************/ |
int main() { |
|
// setup NEORV32 runtime environment to keep us safe |
// -> catch all traps and give debug information via UART0 |
neorv32_rte_setup(); |
|
// setup UART0 at default baud rate, no parity bits, no HW flow control |
neorv32_uart0_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE); |
|
// check if UART0 is implemented at all |
if (neorv32_uart0_available() == 0) { |
neorv32_uart0_printf("Error! UART0 not synthesized!\n"); |
return 1; |
} |
|
|
// say hello |
neorv32_uart0_printf("<<< Newlib demo/test program >>>\n\n"); |
|
|
// check if newlib is really available |
#ifndef __NEWLIB__ |
neorv32_uart0_printf("ERROR! Seems like the compiler toolchain does not support newlib...\n"); |
return -1; |
#endif |
|
neorv32_uart0_printf("newlib version %i.%i\n\n", (int32_t)__NEWLIB__, (int32_t)__NEWLIB_MINOR__); |
|
|
char *char_buffer; // pointer for dynamic memory allocation |
|
neorv32_uart0_printf("<malloc> test... "); |
char_buffer = (char *) malloc(4 * sizeof(char)); // 4 bytes |
neorv32_uart0_printf("ok\n"); |
|
// do not test read & write in simulation as there would be no UART RX input |
if (NEORV32_SYSINFO.SOC & (1<<SYSINFO_SOC_IS_SIM)) { |
neorv32_uart0_printf("Skipping <read> & <write> tests as this seems to be a simulation.\n"); |
} |
else { |
neorv32_uart0_printf("<read> test (waiting for 4 chars via UART0)... "); |
read((int)STDIN_FILENO, char_buffer, 4 * sizeof(char)); // get 4 chars from "STDIN" (UART0.RX) |
neorv32_uart0_printf("ok\n"); |
|
neorv32_uart0_printf("<write> test to 'STDOUT'... (outputting the chars you have send)\n"); |
write((int)STDOUT_FILENO, char_buffer, 4 * sizeof(char)); // send 4 chars to "STDOUT" (UART0.TX) |
neorv32_uart0_printf("\nok\n"); |
|
neorv32_uart0_printf("<write> test to 'STDERR'... (outputting the chars you have send)\n"); |
write((int)STDERR_FILENO, char_buffer, 4 * sizeof(char)); // send 4 chars to "STDERR" (UART0.TX) |
neorv32_uart0_printf("\nok\n"); |
} |
|
neorv32_uart0_printf("<free> test... "); |
free(char_buffer); |
neorv32_uart0_printf("ok\n"); |
|
|
// NOTE: exit is highly oversized as it also includes clean-up functions (destructors), which |
// is not required for bare-metal or RTOS applications... better use the simple 'return' or even better |
// make sure main never returns. however, let's test that 'exit' works. |
neorv32_uart0_printf("<exit> test..."); |
exit(0); |
|
return 0; // should never be reached |
} |
|
|
/**********************************************************************//** |
* "after-main" handler that is executed after the application's |
* main function returns (called by crt0.S start-up code) |
**************************************************************************/ |
int __neorv32_crt0_after_main(int32_t return_code) { |
|
neorv32_uart0_printf("\n<RTE> main function returned with exit code %i. </RTE>\n", return_code); |
|
return 0; |
} |
/newlib_demo/makefile
0,0 → 1,40
################################################################################################# |
# << NEORV32 - Application Makefile >> # |
# ********************************************************************************************* # |
# Make sure to add the RISC-V GCC compiler's bin folder to your PATH environment variable. # |
# ********************************************************************************************* # |
# BSD 3-Clause License # |
# # |
# Copyright (c) 2022, 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 # |
################################################################################################# |
|
# Modify this variable to fit your NEORV32 setup (neorv32 home folder) |
NEORV32_HOME ?= ../../.. |
|
include $(NEORV32_HOME)/sw/common/common.mk |