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) 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/**********************************************************************/
41#ifndef neorv32_cpu_h
42#define neorv32_cpu_h
43
44
45/**********************************************************************/
49void neorv32_cpu_irq_enable(int irq_sel);
50void neorv32_cpu_irq_disable(int irq_sel);
51uint64_t neorv32_cpu_get_cycle(void);
52void neorv32_cpu_set_mcycle(uint64_t value);
53uint64_t neorv32_cpu_get_instret(void);
54void neorv32_cpu_set_minstret(uint64_t value);
55void neorv32_cpu_delay_ms(uint32_t time_ms);
56uint32_t neorv32_cpu_get_clk_from_prsc(int prsc);
59int neorv32_cpu_pmp_configure_region(int index, uint32_t addr, uint8_t config);
61uint32_t neorv32_cpu_hpm_get_size(void);
66/**********************************************************************/
71extern void __attribute__ ((weak)) __neorv32_crt0_after_main(int32_t return_code);
72
73
74/**********************************************************************/
82inline void __attribute__ ((always_inline)) neorv32_cpu_store_unsigned_word(uint32_t addr, uint32_t wdata) {
83
84 uint32_t reg_addr = addr;
85 uint32_t reg_data = wdata;
86
87 asm volatile ("sw %[da], 0(%[ad])" : : [da] "r" (reg_data), [ad] "r" (reg_addr));
88}
89
90
91/**********************************************************************/
99inline void __attribute__ ((always_inline)) neorv32_cpu_store_unsigned_half(uint32_t addr, uint16_t wdata) {
100
101 uint32_t reg_addr = addr;
102 uint32_t reg_data = (uint32_t)wdata;
103
104 asm volatile ("sh %[da], 0(%[ad])" : : [da] "r" (reg_data), [ad] "r" (reg_addr));
105}
106
107
108/**********************************************************************/
114inline void __attribute__ ((always_inline)) neorv32_cpu_store_unsigned_byte(uint32_t addr, uint8_t wdata) {
115
116 uint32_t reg_addr = addr;
117 uint32_t reg_data = (uint32_t)wdata;
118
119 asm volatile ("sb %[da], 0(%[ad])" : : [da] "r" (reg_data), [ad] "r" (reg_addr));
120}
121
122
123/**********************************************************************/
131inline uint32_t __attribute__ ((always_inline)) neorv32_cpu_load_unsigned_word(uint32_t addr) {
132
133 uint32_t reg_addr = addr;
134 uint32_t reg_data;
135
136 asm volatile ("lw %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
137
138 return reg_data;
139}
140
141
142/**********************************************************************/
150inline uint16_t __attribute__ ((always_inline)) neorv32_cpu_load_unsigned_half(uint32_t addr) {
151
152 uint32_t reg_addr = addr;
153 uint16_t reg_data;
154
155 asm volatile ("lhu %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
156
157 return reg_data;
158}
159
160
161/**********************************************************************/
169inline int16_t __attribute__ ((always_inline)) neorv32_cpu_load_signed_half(uint32_t addr) {
170
171 uint32_t reg_addr = addr;
172 int16_t reg_data;
173
174 asm volatile ("lh %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
175
176 return reg_data;
177}
178
179
180/**********************************************************************/
186inline uint8_t __attribute__ ((always_inline)) neorv32_cpu_load_unsigned_byte(uint32_t addr) {
187
188 uint32_t reg_addr = addr;
189 uint8_t reg_data;
190
191 asm volatile ("lbu %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
192
193 return reg_data;
194}
195
196
197/**********************************************************************/
203inline int8_t __attribute__ ((always_inline)) neorv32_cpu_load_signed_byte(uint32_t addr) {
204
205 uint32_t reg_addr = addr;
206 int8_t reg_data;
207
208 asm volatile ("lb %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
209
210 return reg_data;
211}
212
213
214/**********************************************************************/
220inline uint32_t __attribute__ ((always_inline)) neorv32_cpu_csr_read(const int csr_id) {
221
222 uint32_t csr_data;
223
224 asm volatile ("csrr %[result], %[input_i]" : [result] "=r" (csr_data) : [input_i] "i" (csr_id));
225
226 return csr_data;
227}
228
229
230/**********************************************************************/
236inline void __attribute__ ((always_inline)) neorv32_cpu_csr_write(const int csr_id, uint32_t data) {
237
238 uint32_t csr_data = data;
239
240 asm volatile ("csrw %[input_i], %[input_j]" : : [input_i] "i" (csr_id), [input_j] "r" (csr_data));
241}
242
243
244/**********************************************************************/
250inline void __attribute__ ((always_inline)) neorv32_cpu_csr_set(const int csr_id, uint32_t mask) {
251
252 uint32_t csr_data = mask;
253
254 asm volatile ("csrs %[input_i], %[input_j]" : : [input_i] "i" (csr_id), [input_j] "r" (csr_data));
255}
256
257
258/**********************************************************************/
264inline void __attribute__ ((always_inline)) neorv32_cpu_csr_clr(const int csr_id, uint32_t mask) {
265
266 uint32_t csr_data = mask;
267
268 asm volatile ("csrc %[input_i], %[input_j]" : : [input_i] "i" (csr_id), [input_j] "r" (csr_data));
269}
270
271
272/**********************************************************************/
278inline void __attribute__ ((always_inline)) neorv32_cpu_sleep(void) {
279
280 asm volatile ("wfi");
281}
282
283
284#endif // neorv32_cpu_h
void neorv32_cpu_irq_disable(int irq_sel)
Definition neorv32_cpu.c:84
void __neorv32_crt0_after_main(int32_t return_code)
Definition main.c:151
void neorv32_cpu_store_unsigned_word(uint32_t addr, uint32_t wdata)
Definition neorv32_cpu.h:82
void neorv32_cpu_set_mcycle(uint64_t value)
Definition neorv32_cpu.c:125
void neorv32_cpu_csr_set(const int csr_id, uint32_t mask)
Definition neorv32_cpu.h:250
uint32_t neorv32_cpu_pmp_get_num_regions(void)
Definition neorv32_cpu.c:283
void neorv32_cpu_store_unsigned_half(uint32_t addr, uint16_t wdata)
Definition neorv32_cpu.h:99
int8_t neorv32_cpu_load_signed_byte(uint32_t addr)
Definition neorv32_cpu.h:203
uint16_t neorv32_cpu_load_unsigned_half(uint32_t addr)
Definition neorv32_cpu.h:150
uint64_t neorv32_cpu_get_instret(void)
Definition neorv32_cpu.c:146
uint32_t neorv32_cpu_load_unsigned_word(uint32_t addr)
Definition neorv32_cpu.h:131
void neorv32_cpu_set_minstret(uint64_t value)
Definition neorv32_cpu.c:175
uint32_t neorv32_cpu_hpm_get_num_counters(void)
Definition neorv32_cpu.c:427
void neorv32_cpu_sleep(void)
Definition neorv32_cpu.h:278
void neorv32_cpu_irq_enable(int irq_sel)
Definition neorv32_cpu.c:71
uint32_t neorv32_cpu_get_clk_from_prsc(int prsc)
Definition neorv32_cpu.c:250
int neorv32_cpu_pmp_configure_region(int index, uint32_t addr, uint8_t config)
Definition neorv32_cpu.c:370
void neorv32_cpu_delay_ms(uint32_t time_ms)
Definition neorv32_cpu.c:200
uint32_t neorv32_cpu_csr_read(const int csr_id)
Definition neorv32_cpu.h:220
void neorv32_cpu_csr_clr(const int csr_id, uint32_t mask)
Definition neorv32_cpu.h:264
uint64_t neorv32_cpu_get_cycle(void)
Definition neorv32_cpu.c:96
uint8_t neorv32_cpu_load_unsigned_byte(uint32_t addr)
Definition neorv32_cpu.h:186
void neorv32_cpu_goto_user_mode(void)
Definition neorv32_cpu.c:502
uint32_t neorv32_cpu_pmp_get_granularity(void)
Definition neorv32_cpu.c:328
uint32_t neorv32_cpu_hpm_get_size(void)
Definition neorv32_cpu.c:462
int16_t neorv32_cpu_load_signed_half(uint32_t addr)
Definition neorv32_cpu.h:169
void neorv32_cpu_store_unsigned_byte(uint32_t addr, uint8_t wdata)
Definition neorv32_cpu.h:114
void neorv32_cpu_csr_write(const int csr_id, uint32_t data)
Definition neorv32_cpu.h:236