|
| 1 | +// ################################################################################################# |
| 2 | +// # << NEORV32 - Demo program for emulating unaligned memory accesses using the NEORV32 RTE >> # |
| 3 | +// # ********************************************************************************************* # |
| 4 | +// # BSD 3-Clause License # |
| 5 | +// # # |
| 6 | +// # Copyright (c) 2023, 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 demo_emulate_unaligned/main.c |
| 38 | + * @author Stephan Nolting |
| 39 | + * @brief Demo program for emulating unaligned memory accesses using the NEORV32 |
| 40 | + * run-time environment (RTE). |
| 41 | + **************************************************************************/ |
| 42 | + |
| 43 | +#include <neorv32.h> |
| 44 | + |
| 45 | + |
| 46 | +/**********************************************************************//** |
| 47 | + * @name User configuration |
| 48 | + **************************************************************************/ |
| 49 | +/**@{*/ |
| 50 | +/** UART BAUD rate */ |
| 51 | +#define BAUD_RATE 19200 |
| 52 | +/**@}*/ |
| 53 | + |
| 54 | + |
| 55 | +/**********************************************************************//** |
| 56 | + * @name Global variables |
| 57 | + **************************************************************************/ |
| 58 | +volatile uint32_t data_block[2]; |
| 59 | + |
| 60 | + |
| 61 | +/**********************************************************************//** |
| 62 | + * Emulate unaligned load-word operation |
| 63 | + * |
| 64 | + * @note This is a RTE "second-level" trap handler. |
| 65 | + * |
| 66 | + * @warning Compressed load instructions are not supported here! |
| 67 | + **************************************************************************/ |
| 68 | +void trap_handler_emulate_unaligned_lw(void) { |
| 69 | + |
| 70 | + uint32_t mepc = neorv32_cpu_csr_read(CSR_MEPC); |
| 71 | + |
| 72 | + // this function assumes that the exception is raised by an UNCOMPRESSED load operation |
| 73 | + uint32_t inst = neorv32_cpu_load_unsigned_word(mepc); |
| 74 | + |
| 75 | + // decompose I-type instruction |
| 76 | + uint32_t opcode = (inst >> 0) & 0x007; |
| 77 | + uint32_t funct3 = (inst >> 12) & 0x003; |
| 78 | + uint32_t rs1_addr = (inst >> 15) & 0x01f; |
| 79 | + uint32_t rd_addr = (inst >> 7) & 0x01f; |
| 80 | + uint32_t imm12 = (inst >> 20) & 0xfff; |
| 81 | + |
| 82 | + // check if the trap-causing instruction is 'lw' instruction |
| 83 | + if ((opcode == 0b0000011) && (funct3 == 0b010)) { |
| 84 | + |
| 85 | +// neorv32_uart0_printf("\n<< emulating 'lw x%u, %i(x%u)' >>\n", rd_addr, imm12, rs1_addr); |
| 86 | + |
| 87 | + // get operands from main's context |
| 88 | + uint32_t rs1 = neorv32_rte_context_get(rs1_addr); |
| 89 | + |
| 90 | + // emulated function |
| 91 | + uint32_t addr = rs1 + imm12; |
| 92 | + uint32_t b0 = (uint32_t)neorv32_cpu_load_unsigned_byte(addr + 0); |
| 93 | + uint32_t b1 = (uint32_t)neorv32_cpu_load_unsigned_byte(addr + 1); |
| 94 | + uint32_t b2 = (uint32_t)neorv32_cpu_load_unsigned_byte(addr + 2); |
| 95 | + uint32_t b3 = (uint32_t)neorv32_cpu_load_unsigned_byte(addr + 3); |
| 96 | + uint32_t rd = (b3 << 24) | (b2 << 16) | (b1 << 8) | (b0 << 0); |
| 97 | + |
| 98 | + // write result back to main's context |
| 99 | + neorv32_rte_context_put(rd_addr, rd); |
| 100 | + |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | +/**********************************************************************//** |
| 106 | + * Load 32-bit data from memory. This wrapper function is used to ensure the emitted |
| 107 | + * load instruction is UNCOMPRESSED. |
| 108 | + * |
| 109 | + * @param[in] addr Address (32-bit). |
| 110 | + * @return Read data word (32-bit). |
| 111 | + **************************************************************************/ |
| 112 | +uint32_t lw32(uint32_t addr) { |
| 113 | + |
| 114 | + uint32_t reg_addr = addr; |
| 115 | + uint32_t reg_data; |
| 116 | + |
| 117 | + asm volatile ( |
| 118 | + ".option push \n" |
| 119 | + ".option norvc \n" // make sure this emits uncompressed code |
| 120 | + "lw %[da], 0(%[ad]) \n" |
| 121 | + ".option pop \n" |
| 122 | + : [da] "=r" (reg_data) : [ad] "r" (reg_addr) |
| 123 | + ); |
| 124 | + |
| 125 | + return reg_data; |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | +/**********************************************************************//** |
| 130 | + * Demo program to showcase RTE-based emulation of unaligned memory accesses. |
| 131 | + * |
| 132 | + * @return Irrelevant. |
| 133 | + **************************************************************************/ |
| 134 | +int main() { |
| 135 | + |
| 136 | + uint32_t addr, data; |
| 137 | + |
| 138 | + // setup NEORV32 runtime environment |
| 139 | + neorv32_rte_setup(); |
| 140 | + |
| 141 | + // setup UART at default baud rate, no interrupts |
| 142 | + neorv32_uart0_setup(BAUD_RATE, 0); |
| 143 | + |
| 144 | + // intro |
| 145 | + neorv32_uart0_printf("\n<<< Demo: Emulation of Unaligned Memory Accesses >>>\n"); |
| 146 | + |
| 147 | + // show source data block |
| 148 | + data_block[0] = 0x00112233; |
| 149 | + data_block[1] = 0x44556677; |
| 150 | + neorv32_uart0_printf("\nSource data:\n"); |
| 151 | + neorv32_uart0_printf("MEM[0x%x] = 0x%x\n", (uint32_t)&data_block[0], data_block[0]); |
| 152 | + neorv32_uart0_printf("MEM[0x%x] = 0x%x\n", (uint32_t)&data_block[1], data_block[1]); |
| 153 | + |
| 154 | + |
| 155 | + // ------------------------------------------ |
| 156 | + // Without emulation: RTE debug handler will show an error |
| 157 | + // ------------------------------------------ |
| 158 | + neorv32_uart0_printf("\nUnaligned load without emulation:\n"); |
| 159 | + |
| 160 | + addr = ((uint32_t)&data_block[0]) + 1; // = unaligned address |
| 161 | + neorv32_uart0_printf("MEM[0x%x] = ", addr); |
| 162 | + |
| 163 | + data = lw32(addr); // this will raise an exception |
| 164 | + |
| 165 | + if (data == 0x77001122) { |
| 166 | + neorv32_uart0_printf("0x%x [ok]\n", data); |
| 167 | + } |
| 168 | + else { |
| 169 | + neorv32_uart0_printf("[FAILED]\n"); |
| 170 | + } |
| 171 | + |
| 172 | + |
| 173 | + // ------------------------------------------ |
| 174 | + // With emulation: operation is handled by trap_handler_emulate_unaligned_lw |
| 175 | + // ------------------------------------------ |
| 176 | + neorv32_uart0_printf("\nUnaligned load with emulation:\n"); |
| 177 | + |
| 178 | + // install trap handler for "unaligned load address" exception |
| 179 | + neorv32_rte_handler_install(RTE_TRAP_L_MISALIGNED, trap_handler_emulate_unaligned_lw); |
| 180 | + |
| 181 | + addr = ((uint32_t)&data_block[0]) + 1; // = unaligned address |
| 182 | + neorv32_uart0_printf("MEM[0x%x] = ", addr); |
| 183 | + |
| 184 | + data = lw32(addr); // this will raise an exception |
| 185 | + |
| 186 | + if (data == 0x77001122) { |
| 187 | + neorv32_uart0_printf("0x%x [ok]\n", data); |
| 188 | + } |
| 189 | + else { |
| 190 | + neorv32_uart0_printf("[FAILED]\n"); |
| 191 | + } |
| 192 | + |
| 193 | + |
| 194 | + neorv32_uart0_printf("\nProgram completed.\n"); |
| 195 | + return 0; |
| 196 | +} |
0 commit comments