NEORV32 - Software Framework Documentation
Loading...
Searching...
No Matches
neorv32_cpu.h
Go to the documentation of this file.
1// #################################################################################################
2// # << NEORV32: neorv32_cpu.h - CPU Core Functions HW Driver >> #
3// # ********************************************************************************************* #
4// # BSD 3-Clause License #
5// # #
6// # Copyright (c) 2022, 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/**********************************************************************/
41#ifndef neorv32_cpu_h
42#define neorv32_cpu_h
43
44// prototypes
45int neorv32_cpu_irq_enable(uint8_t irq_sel);
46int neorv32_cpu_irq_disable(uint8_t irq_sel);
47uint64_t neorv32_cpu_get_cycle(void);
48void neorv32_cpu_set_mcycle(uint64_t value);
49uint64_t neorv32_cpu_get_instret(void);
50void neorv32_cpu_set_minstret(uint64_t value);
51uint64_t neorv32_cpu_get_systime(void);
52void neorv32_cpu_delay_ms(uint32_t time_ms);
53uint32_t neorv32_cpu_get_clk_from_prsc(int prsc);
56int neorv32_cpu_pmp_configure_region(uint32_t index, uint32_t base, uint8_t config);
58uint32_t neorv32_cpu_hpm_get_size(void);
60
61
62/**********************************************************************/
67extern void __attribute__ ((weak)) __neorv32_crt0_after_main(int32_t return_code);
68
69
70/**********************************************************************/
78inline void __attribute__ ((always_inline)) neorv32_cpu_store_unsigned_word(uint32_t addr, uint32_t wdata) {
79
80 uint32_t reg_addr = addr;
81 uint32_t reg_data = wdata;
82
83 asm volatile ("sw %[da], 0(%[ad])" : : [da] "r" (reg_data), [ad] "r" (reg_addr));
84}
85
86
87/**********************************************************************/
95inline void __attribute__ ((always_inline)) neorv32_cpu_store_unsigned_half(uint32_t addr, uint16_t wdata) {
96
97 uint32_t reg_addr = addr;
98 uint32_t reg_data = (uint32_t)wdata;
99
100 asm volatile ("sh %[da], 0(%[ad])" : : [da] "r" (reg_data), [ad] "r" (reg_addr));
101}
102
103
104/**********************************************************************/
110inline void __attribute__ ((always_inline)) neorv32_cpu_store_unsigned_byte(uint32_t addr, uint8_t wdata) {
111
112 uint32_t reg_addr = addr;
113 uint32_t reg_data = (uint32_t)wdata;
114
115 asm volatile ("sb %[da], 0(%[ad])" : : [da] "r" (reg_data), [ad] "r" (reg_addr));
116}
117
118
119/**********************************************************************/
127inline uint32_t __attribute__ ((always_inline)) neorv32_cpu_load_unsigned_word(uint32_t addr) {
128
129 uint32_t reg_addr = addr;
130 uint32_t reg_data;
131
132 asm volatile ("lw %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
133
134 return reg_data;
135}
136
137
138/**********************************************************************/
146inline uint16_t __attribute__ ((always_inline)) neorv32_cpu_load_unsigned_half(uint32_t addr) {
147
148 uint32_t reg_addr = addr;
149 uint16_t reg_data;
150
151 asm volatile ("lhu %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
152
153 return reg_data;
154}
155
156
157/**********************************************************************/
165inline int16_t __attribute__ ((always_inline)) neorv32_cpu_load_signed_half(uint32_t addr) {
166
167 uint32_t reg_addr = addr;
168 int16_t reg_data;
169
170 asm volatile ("lh %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
171
172 return reg_data;
173}
174
175
176/**********************************************************************/
182inline uint8_t __attribute__ ((always_inline)) neorv32_cpu_load_unsigned_byte(uint32_t addr) {
183
184 uint32_t reg_addr = addr;
185 uint8_t reg_data;
186
187 asm volatile ("lbu %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
188
189 return reg_data;
190}
191
192
193/**********************************************************************/
199inline int8_t __attribute__ ((always_inline)) neorv32_cpu_load_signed_byte(uint32_t addr) {
200
201 uint32_t reg_addr = addr;
202 int8_t reg_data;
203
204 asm volatile ("lb %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
205
206 return reg_data;
207}
208
209
210/**********************************************************************/
216inline uint32_t __attribute__ ((always_inline)) neorv32_cpu_csr_read(const int csr_id) {
217
218 uint32_t csr_data;
219
220 asm volatile ("csrr %[result], %[input_i]" : [result] "=r" (csr_data) : [input_i] "i" (csr_id));
221
222 return csr_data;
223}
224
225
226/**********************************************************************/
232inline void __attribute__ ((always_inline)) neorv32_cpu_csr_write(const int csr_id, uint32_t data) {
233
234 uint32_t csr_data = data;
235
236 asm volatile ("csrw %[input_i], %[input_j]" : : [input_i] "i" (csr_id), [input_j] "r" (csr_data));
237}
238
239
240/**********************************************************************/
250inline void __attribute__ ((always_inline)) neorv32_cpu_sleep(void) {
251
252 asm volatile ("wfi");
253}
254
255
256/**********************************************************************/
259inline void __attribute__ ((always_inline)) neorv32_cpu_eint(void) {
260
261 asm volatile ("csrrsi zero, mstatus, %0" : : "i" (1 << CSR_MSTATUS_MIE));
262}
263
264
265/**********************************************************************/
268inline void __attribute__ ((always_inline)) neorv32_cpu_dint(void) {
269
270 asm volatile ("csrrci zero, mstatus, %0" : : "i" (1 << CSR_MSTATUS_MIE));
271}
272
273
274/**********************************************************************/
277inline void __attribute__ ((always_inline)) neorv32_cpu_breakpoint(void) {
278
279 asm volatile ("ebreak");
280}
281
282
283/**********************************************************************/
286inline void __attribute__ ((always_inline)) neorv32_cpu_env_call(void) {
287
288 asm volatile ("ecall");
289}
290
291
292#endif // neorv32_cpu_h
@ CSR_MSTATUS_MIE
Definition: neorv32.h:316
void __neorv32_crt0_after_main(int32_t return_code)
Definition: main.c:606
void neorv32_cpu_env_call(void)
Definition: neorv32_cpu.h:286
int neorv32_cpu_irq_enable(uint8_t irq_sel)
Definition: neorv32_cpu.c:99
void neorv32_cpu_store_unsigned_word(uint32_t addr, uint32_t wdata)
Definition: neorv32_cpu.h:78
void neorv32_cpu_set_mcycle(uint64_t value)
Definition: neorv32_cpu.c:167
uint32_t neorv32_cpu_pmp_get_num_regions(void)
Definition: neorv32_cpu.c:354
void neorv32_cpu_store_unsigned_half(uint32_t addr, uint16_t wdata)
Definition: neorv32_cpu.h:95
int8_t neorv32_cpu_load_signed_byte(uint32_t addr)
Definition: neorv32_cpu.h:199
uint16_t neorv32_cpu_load_unsigned_half(uint32_t addr)
Definition: neorv32_cpu.h:146
void neorv32_cpu_breakpoint(void)
Definition: neorv32_cpu.h:277
uint64_t neorv32_cpu_get_instret(void)
Definition: neorv32_cpu.c:191
uint32_t neorv32_cpu_load_unsigned_word(uint32_t addr)
Definition: neorv32_cpu.h:127
void neorv32_cpu_dint(void)
Definition: neorv32_cpu.h:268
int neorv32_cpu_irq_disable(uint8_t irq_sel)
Definition: neorv32_cpu.c:118
void neorv32_cpu_set_minstret(uint64_t value)
Definition: neorv32_cpu.c:220
uint32_t neorv32_cpu_hpm_get_num_counters(void)
Definition: neorv32_cpu.c:550
void neorv32_cpu_sleep(void)
Definition: neorv32_cpu.h:250
int neorv32_cpu_pmp_configure_region(uint32_t index, uint32_t base, uint8_t config)
Definition: neorv32_cpu.c:442
uint32_t neorv32_cpu_get_clk_from_prsc(int prsc)
Definition: neorv32_cpu.c:321
uint64_t neorv32_cpu_get_systime(void)
Definition: neorv32_cpu.c:244
void neorv32_cpu_delay_ms(uint32_t time_ms)
Definition: neorv32_cpu.c:276
uint32_t neorv32_cpu_csr_read(const int csr_id)
Definition: neorv32_cpu.h:216
uint64_t neorv32_cpu_get_cycle(void)
Definition: neorv32_cpu.c:138
uint8_t neorv32_cpu_load_unsigned_byte(uint32_t addr)
Definition: neorv32_cpu.h:182
void neorv32_cpu_goto_user_mode(void)
Definition: neorv32_cpu.c:674
uint32_t neorv32_cpu_pmp_get_granularity(void)
Definition: neorv32_cpu.c:399
uint32_t neorv32_cpu_hpm_get_size(void)
Definition: neorv32_cpu.c:633
void neorv32_cpu_eint(void)
Definition: neorv32_cpu.h:259
int16_t neorv32_cpu_load_signed_half(uint32_t addr)
Definition: neorv32_cpu.h:165
void neorv32_cpu_store_unsigned_byte(uint32_t addr, uint8_t wdata)
Definition: neorv32_cpu.h:110
void neorv32_cpu_csr_write(const int csr_id, uint32_t data)
Definition: neorv32_cpu.h:232