| 1 |
2 |
zero_gravi |
// #################################################################################################
|
| 2 |
|
|
// # << NEORV32: neorv32_cpu.h - CPU Core Functions HW Driver >> #
|
| 3 |
|
|
// # ********************************************************************************************* #
|
| 4 |
|
|
// # BSD 3-Clause License #
|
| 5 |
|
|
// # #
|
| 6 |
42 |
zero_gravi |
// # Copyright (c) 2021, Stephan Nolting. All rights reserved. #
|
| 7 |
2 |
zero_gravi |
// # #
|
| 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_cpu.h
|
| 38 |
|
|
* @author Stephan Nolting
|
| 39 |
|
|
* @brief CPU Core Functions HW driver header file.
|
| 40 |
|
|
**************************************************************************/
|
| 41 |
|
|
|
| 42 |
|
|
#ifndef neorv32_cpu_h
|
| 43 |
|
|
#define neorv32_cpu_h
|
| 44 |
|
|
|
| 45 |
|
|
// prototypes
|
| 46 |
|
|
int neorv32_cpu_irq_enable(uint8_t irq_sel);
|
| 47 |
|
|
int neorv32_cpu_irq_disable(uint8_t irq_sel);
|
| 48 |
12 |
zero_gravi |
uint64_t neorv32_cpu_get_cycle(void);
|
| 49 |
|
|
void neorv32_cpu_set_mcycle(uint64_t value);
|
| 50 |
|
|
uint64_t neorv32_cpu_get_instret(void);
|
| 51 |
|
|
void neorv32_cpu_set_minstret(uint64_t value);
|
| 52 |
|
|
uint64_t neorv32_cpu_get_systime(void);
|
| 53 |
64 |
zero_gravi |
void neorv32_cpu_delay_ms(uint32_t time_ms);
|
| 54 |
15 |
zero_gravi |
void __attribute__((naked)) neorv32_cpu_goto_user_mode(void);
|
| 55 |
42 |
zero_gravi |
uint32_t neorv32_cpu_pmp_get_num_regions(void);
|
| 56 |
40 |
zero_gravi |
uint32_t neorv32_cpu_pmp_get_granularity(void);
|
| 57 |
|
|
int neorv32_cpu_pmp_configure_region(uint32_t index, uint32_t base, uint32_t size, uint8_t config);
|
| 58 |
42 |
zero_gravi |
uint32_t neorv32_cpu_hpm_get_counters(void);
|
| 59 |
56 |
zero_gravi |
uint32_t neorv32_cpu_hpm_get_size(void);
|
| 60 |
2 |
zero_gravi |
|
| 61 |
|
|
|
| 62 |
|
|
/**********************************************************************//**
|
| 63 |
61 |
zero_gravi |
* Prototype for "after-main handler". This function is called if main() returns.
|
| 64 |
|
|
*
|
| 65 |
|
|
* @param[in] return_code Return value of main() function.
|
| 66 |
|
|
* @return Return value is irrelevant (there is no one left to check for it...).
|
| 67 |
|
|
**************************************************************************/
|
| 68 |
62 |
zero_gravi |
extern int __neorv32_crt0_after_main(int32_t return_code) __attribute__ ((weak));
|
| 69 |
61 |
zero_gravi |
|
| 70 |
|
|
|
| 71 |
|
|
/**********************************************************************//**
|
| 72 |
65 |
zero_gravi |
* Store unsigned word to address space if atomic access reservation is still valid.
|
| 73 |
56 |
zero_gravi |
*
|
| 74 |
|
|
* @note An unaligned access address will raise an alignment exception.
|
| 75 |
|
|
*
|
| 76 |
|
|
* @param[in] addr Address (32-bit).
|
| 77 |
|
|
* @param[in] wdata Data word (32-bit) to store.
|
| 78 |
65 |
zero_gravi |
* @return Operation status (32-bit, zero if success).
|
| 79 |
56 |
zero_gravi |
**************************************************************************/
|
| 80 |
57 |
zero_gravi |
inline uint32_t __attribute__ ((always_inline)) neorv32_cpu_store_conditional(uint32_t addr, uint32_t wdata) {
|
| 81 |
|
|
|
| 82 |
|
|
#if defined __riscv_atomic || defined __riscv_a
|
| 83 |
|
|
register uint32_t reg_addr = addr;
|
| 84 |
|
|
register uint32_t reg_data = wdata;
|
| 85 |
|
|
register uint32_t reg_status;
|
| 86 |
|
|
|
| 87 |
|
|
asm volatile ("sc.w %[status], %[da], (%[ad])" : [status] "=r" (reg_status) : [da] "r" (reg_data), [ad] "r" (reg_addr));
|
| 88 |
|
|
|
| 89 |
|
|
return reg_status;
|
| 90 |
|
|
#else
|
| 91 |
|
|
return 1; // always failing
|
| 92 |
|
|
#endif
|
| 93 |
|
|
}
|
| 94 |
|
|
|
| 95 |
|
|
|
| 96 |
|
|
/**********************************************************************//**
|
| 97 |
65 |
zero_gravi |
* Conditional store unsigned word to address space.
|
| 98 |
57 |
zero_gravi |
*
|
| 99 |
|
|
* @note An unaligned access address will raise an alignment exception.
|
| 100 |
|
|
*
|
| 101 |
|
|
* @param[in] addr Address (32-bit).
|
| 102 |
|
|
* @param[in] wdata Data word (32-bit) to store.
|
| 103 |
|
|
**************************************************************************/
|
| 104 |
56 |
zero_gravi |
inline void __attribute__ ((always_inline)) neorv32_cpu_store_unsigned_word(uint32_t addr, uint32_t wdata) {
|
| 105 |
|
|
|
| 106 |
|
|
register uint32_t reg_addr = addr;
|
| 107 |
|
|
register uint32_t reg_data = wdata;
|
| 108 |
|
|
|
| 109 |
|
|
asm volatile ("sw %[da], 0(%[ad])" : : [da] "r" (reg_data), [ad] "r" (reg_addr));
|
| 110 |
|
|
}
|
| 111 |
|
|
|
| 112 |
|
|
|
| 113 |
|
|
/**********************************************************************//**
|
| 114 |
|
|
* Store unsigned half-word to address space.
|
| 115 |
|
|
*
|
| 116 |
|
|
* @note An unaligned access address will raise an alignment exception.
|
| 117 |
|
|
*
|
| 118 |
|
|
* @param[in] addr Address (32-bit).
|
| 119 |
|
|
* @param[in] wdata Data half-word (16-bit) to store.
|
| 120 |
|
|
**************************************************************************/
|
| 121 |
|
|
inline void __attribute__ ((always_inline)) neorv32_cpu_store_unsigned_half(uint32_t addr, uint16_t wdata) {
|
| 122 |
|
|
|
| 123 |
|
|
register uint32_t reg_addr = addr;
|
| 124 |
|
|
register uint32_t reg_data = (uint32_t)wdata;
|
| 125 |
|
|
|
| 126 |
|
|
asm volatile ("sh %[da], 0(%[ad])" : : [da] "r" (reg_data), [ad] "r" (reg_addr));
|
| 127 |
|
|
}
|
| 128 |
|
|
|
| 129 |
|
|
|
| 130 |
|
|
/**********************************************************************//**
|
| 131 |
|
|
* Store unsigned byte to address space.
|
| 132 |
|
|
*
|
| 133 |
|
|
* @param[in] addr Address (32-bit).
|
| 134 |
|
|
* @param[in] wdata Data byte (8-bit) to store.
|
| 135 |
|
|
**************************************************************************/
|
| 136 |
|
|
inline void __attribute__ ((always_inline)) neorv32_cpu_store_unsigned_byte(uint32_t addr, uint8_t wdata) {
|
| 137 |
|
|
|
| 138 |
|
|
register uint32_t reg_addr = addr;
|
| 139 |
|
|
register uint32_t reg_data = (uint32_t)wdata;
|
| 140 |
|
|
|
| 141 |
|
|
asm volatile ("sb %[da], 0(%[ad])" : : [da] "r" (reg_data), [ad] "r" (reg_addr));
|
| 142 |
|
|
}
|
| 143 |
|
|
|
| 144 |
|
|
|
| 145 |
|
|
/**********************************************************************//**
|
| 146 |
57 |
zero_gravi |
* Load unsigned word from address space and make reservation for atomic access.
|
| 147 |
|
|
*
|
| 148 |
|
|
* @note An unaligned access address will raise an alignment exception.
|
| 149 |
|
|
*
|
| 150 |
|
|
* @param[in] addr Address (32-bit).
|
| 151 |
|
|
* @return Read data word (32-bit).
|
| 152 |
|
|
**************************************************************************/
|
| 153 |
|
|
inline uint32_t __attribute__ ((always_inline)) neorv32_cpu_load_reservate_word(uint32_t addr) {
|
| 154 |
|
|
|
| 155 |
|
|
register uint32_t reg_addr = addr;
|
| 156 |
|
|
register uint32_t reg_data;
|
| 157 |
|
|
|
| 158 |
|
|
#if defined __riscv_atomic || defined __riscv_a
|
| 159 |
|
|
asm volatile ("lr.w %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
|
| 160 |
|
|
#else
|
| 161 |
|
|
asm volatile ("lw %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
|
| 162 |
|
|
#endif
|
| 163 |
|
|
|
| 164 |
|
|
return (uint32_t)reg_data;
|
| 165 |
|
|
}
|
| 166 |
|
|
|
| 167 |
|
|
|
| 168 |
|
|
|
| 169 |
|
|
/**********************************************************************//**
|
| 170 |
56 |
zero_gravi |
* Load unsigned word from address space.
|
| 171 |
|
|
*
|
| 172 |
|
|
* @note An unaligned access address will raise an alignment exception.
|
| 173 |
|
|
*
|
| 174 |
|
|
* @param[in] addr Address (32-bit).
|
| 175 |
|
|
* @return Read data word (32-bit).
|
| 176 |
|
|
**************************************************************************/
|
| 177 |
|
|
inline uint32_t __attribute__ ((always_inline)) neorv32_cpu_load_unsigned_word(uint32_t addr) {
|
| 178 |
|
|
|
| 179 |
|
|
register uint32_t reg_addr = addr;
|
| 180 |
|
|
register uint32_t reg_data;
|
| 181 |
|
|
|
| 182 |
|
|
asm volatile ("lw %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
|
| 183 |
|
|
|
| 184 |
|
|
return (uint32_t)reg_data;
|
| 185 |
|
|
}
|
| 186 |
|
|
|
| 187 |
|
|
|
| 188 |
|
|
/**********************************************************************//**
|
| 189 |
|
|
* Load unsigned half-word from address space.
|
| 190 |
|
|
*
|
| 191 |
|
|
* @note An unaligned access address will raise an alignment exception.
|
| 192 |
|
|
*
|
| 193 |
|
|
* @param[in] addr Address (32-bit).
|
| 194 |
|
|
* @return Read data half-word (16-bit).
|
| 195 |
|
|
**************************************************************************/
|
| 196 |
|
|
inline uint16_t __attribute__ ((always_inline)) neorv32_cpu_load_unsigned_half(uint32_t addr) {
|
| 197 |
|
|
|
| 198 |
|
|
register uint32_t reg_addr = addr;
|
| 199 |
|
|
register uint32_t reg_data;
|
| 200 |
|
|
|
| 201 |
|
|
asm volatile ("lhu %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
|
| 202 |
|
|
|
| 203 |
|
|
return (uint16_t)reg_data;
|
| 204 |
|
|
}
|
| 205 |
|
|
|
| 206 |
|
|
|
| 207 |
|
|
/**********************************************************************//**
|
| 208 |
|
|
* Load unsigned byte from address space.
|
| 209 |
|
|
*
|
| 210 |
|
|
* @param[in] addr Address (32-bit).
|
| 211 |
|
|
* @return Read data byte (8-bit).
|
| 212 |
|
|
**************************************************************************/
|
| 213 |
|
|
inline uint8_t __attribute__ ((always_inline)) neorv32_cpu_load_unsigned_byte(uint32_t addr) {
|
| 214 |
|
|
|
| 215 |
|
|
register uint32_t reg_addr = addr;
|
| 216 |
|
|
register uint32_t reg_data;
|
| 217 |
|
|
|
| 218 |
|
|
asm volatile ("lbu %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
|
| 219 |
|
|
|
| 220 |
|
|
return (uint8_t)reg_data;
|
| 221 |
|
|
}
|
| 222 |
|
|
|
| 223 |
|
|
|
| 224 |
|
|
/**********************************************************************//**
|
| 225 |
2 |
zero_gravi |
* Read data from CPU configuration and status register (CSR).
|
| 226 |
|
|
*
|
| 227 |
42 |
zero_gravi |
* @param[in] csr_id ID of CSR to read. See #NEORV32_CSR_enum.
|
| 228 |
2 |
zero_gravi |
* @return Read data (uint32_t).
|
| 229 |
|
|
**************************************************************************/
|
| 230 |
|
|
inline uint32_t __attribute__ ((always_inline)) neorv32_cpu_csr_read(const int csr_id) {
|
| 231 |
|
|
|
| 232 |
|
|
register uint32_t csr_data;
|
| 233 |
|
|
|
| 234 |
6 |
zero_gravi |
asm volatile ("csrr %[result], %[input_i]" : [result] "=r" (csr_data) : [input_i] "i" (csr_id));
|
| 235 |
2 |
zero_gravi |
|
| 236 |
|
|
return csr_data;
|
| 237 |
|
|
}
|
| 238 |
|
|
|
| 239 |
|
|
|
| 240 |
|
|
/**********************************************************************//**
|
| 241 |
|
|
* Write data to CPU configuration and status register (CSR).
|
| 242 |
|
|
*
|
| 243 |
42 |
zero_gravi |
* @param[in] csr_id ID of CSR to write. See #NEORV32_CSR_enum.
|
| 244 |
2 |
zero_gravi |
* @param[in] data Data to write (uint32_t).
|
| 245 |
|
|
**************************************************************************/
|
| 246 |
|
|
inline void __attribute__ ((always_inline)) neorv32_cpu_csr_write(const int csr_id, uint32_t data) {
|
| 247 |
|
|
|
| 248 |
|
|
register uint32_t csr_data = data;
|
| 249 |
|
|
|
| 250 |
6 |
zero_gravi |
asm volatile ("csrw %[input_i], %[input_j]" : : [input_i] "i" (csr_id), [input_j] "r" (csr_data));
|
| 251 |
2 |
zero_gravi |
}
|
| 252 |
|
|
|
| 253 |
9 |
zero_gravi |
|
| 254 |
|
|
/**********************************************************************//**
|
| 255 |
|
|
* Put CPU into "sleep" mode.
|
| 256 |
|
|
*
|
| 257 |
65 |
zero_gravi |
* @note This function executes the WFI instruction.
|
| 258 |
9 |
zero_gravi |
* The WFI (wait for interrupt) instruction will make the CPU stall until
|
| 259 |
65 |
zero_gravi |
* an interrupt request is detected. Interrupts have to be globally enabled
|
| 260 |
56 |
zero_gravi |
* and at least one external source must be enabled (like the MTI machine
|
| 261 |
|
|
* timer interrupt) to allow the CPU to wake up again. If 'Zicsr' CPU extension is disabled,
|
| 262 |
9 |
zero_gravi |
* this will permanently stall the CPU.
|
| 263 |
|
|
**************************************************************************/
|
| 264 |
|
|
inline void __attribute__ ((always_inline)) neorv32_cpu_sleep(void) {
|
| 265 |
|
|
|
| 266 |
|
|
asm volatile ("wfi");
|
| 267 |
|
|
}
|
| 268 |
|
|
|
| 269 |
|
|
|
| 270 |
|
|
/**********************************************************************//**
|
| 271 |
|
|
* Enable global CPU interrupts (via MIE flag in mstatus CSR).
|
| 272 |
|
|
**************************************************************************/
|
| 273 |
|
|
inline void __attribute__ ((always_inline)) neorv32_cpu_eint(void) {
|
| 274 |
|
|
|
| 275 |
42 |
zero_gravi |
asm volatile ("csrrsi zero, mstatus, %0" : : "i" (1 << CSR_MSTATUS_MIE));
|
| 276 |
40 |
zero_gravi |
asm volatile ("nop");
|
| 277 |
|
|
asm volatile ("nop");
|
| 278 |
9 |
zero_gravi |
}
|
| 279 |
|
|
|
| 280 |
|
|
|
| 281 |
|
|
/**********************************************************************//**
|
| 282 |
|
|
* Disable global CPU interrupts (via MIE flag in mstatus CSR).
|
| 283 |
|
|
**************************************************************************/
|
| 284 |
|
|
inline void __attribute__ ((always_inline)) neorv32_cpu_dint(void) {
|
| 285 |
|
|
|
| 286 |
42 |
zero_gravi |
asm volatile ("csrrci zero, mstatus, %0" : : "i" (1 << CSR_MSTATUS_MIE));
|
| 287 |
40 |
zero_gravi |
asm volatile ("nop");
|
| 288 |
|
|
asm volatile ("nop");
|
| 289 |
9 |
zero_gravi |
}
|
| 290 |
|
|
|
| 291 |
|
|
|
| 292 |
|
|
/**********************************************************************//**
|
| 293 |
|
|
* Trigger breakpoint exception (via EBREAK instruction).
|
| 294 |
|
|
**************************************************************************/
|
| 295 |
|
|
inline void __attribute__ ((always_inline)) neorv32_cpu_breakpoint(void) {
|
| 296 |
|
|
|
| 297 |
|
|
asm volatile ("ebreak");
|
| 298 |
|
|
}
|
| 299 |
|
|
|
| 300 |
|
|
|
| 301 |
|
|
/**********************************************************************//**
|
| 302 |
|
|
* Trigger "environment call" exception (via ECALL instruction).
|
| 303 |
|
|
**************************************************************************/
|
| 304 |
|
|
inline void __attribute__ ((always_inline)) neorv32_cpu_env_call(void) {
|
| 305 |
|
|
|
| 306 |
|
|
asm volatile ("ecall");
|
| 307 |
|
|
}
|
| 308 |
|
|
|
| 309 |
|
|
|
| 310 |
2 |
zero_gravi |
#endif // neorv32_cpu_h
|