NEORV32 Software Framework Documentation
The NEORV32 RISC-V Processor
Loading...
Searching...
No Matches
neorv32_cpu.h
Go to the documentation of this file.
1// ================================================================================ //
2// The NEORV32 RISC-V Processor - https://github.com/stnolting/neorv32 //
3// Copyright (c) NEORV32 contributors. //
4// Copyright (c) 2020 - 2024 Stephan Nolting. All rights reserved. //
5// Licensed under the BSD-3-Clause license, see LICENSE for details. //
6// SPDX-License-Identifier: BSD-3-Clause //
7// ================================================================================ //
8
16#ifndef neorv32_cpu_h
17#define neorv32_cpu_h
18
19#include <stdint.h>
20
21
22/**********************************************************************/
26uint64_t neorv32_cpu_get_cycle(void);
27void neorv32_cpu_set_mcycle(uint64_t value);
28uint64_t neorv32_cpu_get_instret(void);
29void neorv32_cpu_set_minstret(uint64_t value);
30void neorv32_cpu_delay_ms(uint32_t time_ms);
31uint32_t neorv32_cpu_get_clk_from_prsc(int prsc);
34int neorv32_cpu_pmp_configure_region(int index, uint32_t addr, uint8_t config);
36uint32_t neorv32_cpu_hpm_get_size(void);
41/**********************************************************************/
49inline void __attribute__ ((always_inline)) neorv32_cpu_store_unsigned_word(uint32_t addr, uint32_t wdata) {
50
51 uint32_t reg_addr = addr;
52 uint32_t reg_data = wdata;
53 asm volatile ("sw %[da], 0(%[ad])" : : [da] "r" (reg_data), [ad] "r" (reg_addr));
54}
55
56
57/**********************************************************************/
65inline void __attribute__ ((always_inline)) neorv32_cpu_store_unsigned_half(uint32_t addr, uint16_t wdata) {
66
67 uint32_t reg_addr = addr;
68 uint32_t reg_data = (uint32_t)wdata;
69 asm volatile ("sh %[da], 0(%[ad])" : : [da] "r" (reg_data), [ad] "r" (reg_addr));
70}
71
72
73/**********************************************************************/
79inline void __attribute__ ((always_inline)) neorv32_cpu_store_unsigned_byte(uint32_t addr, uint8_t wdata) {
80
81 uint32_t reg_addr = addr;
82 uint32_t reg_data = (uint32_t)wdata;
83 asm volatile ("sb %[da], 0(%[ad])" : : [da] "r" (reg_data), [ad] "r" (reg_addr));
84}
85
86
87/**********************************************************************/
95inline uint32_t __attribute__ ((always_inline)) neorv32_cpu_load_unsigned_word(uint32_t addr) {
96
97 uint32_t reg_addr = addr;
98 uint32_t reg_data;
99 asm volatile ("lw %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
100 return reg_data;
101}
102
103
104/**********************************************************************/
112inline uint16_t __attribute__ ((always_inline)) neorv32_cpu_load_unsigned_half(uint32_t addr) {
113
114 uint32_t reg_addr = addr;
115 uint16_t reg_data;
116 asm volatile ("lhu %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
117 return reg_data;
118}
119
120
121/**********************************************************************/
129inline int16_t __attribute__ ((always_inline)) neorv32_cpu_load_signed_half(uint32_t addr) {
130
131 uint32_t reg_addr = addr;
132 int16_t reg_data;
133 asm volatile ("lh %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
134 return reg_data;
135}
136
137
138/**********************************************************************/
144inline uint8_t __attribute__ ((always_inline)) neorv32_cpu_load_unsigned_byte(uint32_t addr) {
145
146 uint32_t reg_addr = addr;
147 uint8_t reg_data;
148 asm volatile ("lbu %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
149 return reg_data;
150}
151
152
153/**********************************************************************/
159inline int8_t __attribute__ ((always_inline)) neorv32_cpu_load_signed_byte(uint32_t addr) {
160
161 uint32_t reg_addr = addr;
162 int8_t reg_data;
163 asm volatile ("lb %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
164 return reg_data;
165}
166
167
168/**********************************************************************/
174inline uint32_t __attribute__ ((always_inline)) neorv32_cpu_csr_read(const int csr_id) {
175
176 uint32_t csr_data;
177 asm volatile ("csrr %[result], %[input_i]" : [result] "=r" (csr_data) : [input_i] "i" (csr_id));
178 return csr_data;
179}
180
181
182/**********************************************************************/
188inline void __attribute__ ((always_inline)) neorv32_cpu_csr_write(const int csr_id, uint32_t data) {
189
190 uint32_t csr_data = data;
191 asm volatile ("csrw %[input_i], %[input_j]" : : [input_i] "i" (csr_id), [input_j] "r" (csr_data));
192}
193
194
195/**********************************************************************/
201inline void __attribute__ ((always_inline)) neorv32_cpu_csr_set(const int csr_id, uint32_t mask) {
202
203 uint32_t csr_data = mask;
204 asm volatile ("csrs %[input_i], %[input_j]" : : [input_i] "i" (csr_id), [input_j] "r" (csr_data));
205}
206
207
208/**********************************************************************/
214inline void __attribute__ ((always_inline)) neorv32_cpu_csr_clr(const int csr_id, uint32_t mask) {
215
216 uint32_t csr_data = mask;
217 asm volatile ("csrc %[input_i], %[input_j]" : : [input_i] "i" (csr_id), [input_j] "r" (csr_data));
218}
219
220
221/**********************************************************************/
227inline void __attribute__ ((always_inline)) neorv32_cpu_sleep(void) {
228
229 asm volatile ("wfi");
230}
231
232
233#endif // neorv32_cpu_h
void neorv32_cpu_store_unsigned_word(uint32_t addr, uint32_t wdata)
Definition neorv32_cpu.h:49
void neorv32_cpu_set_mcycle(uint64_t value)
Definition neorv32_cpu.c:72
void neorv32_cpu_csr_set(const int csr_id, uint32_t mask)
Definition neorv32_cpu.h:201
uint32_t neorv32_cpu_pmp_get_num_regions(void)
Definition neorv32_cpu.c:221
void neorv32_cpu_store_unsigned_half(uint32_t addr, uint16_t wdata)
Definition neorv32_cpu.h:65
int8_t neorv32_cpu_load_signed_byte(uint32_t addr)
Definition neorv32_cpu.h:159
uint16_t neorv32_cpu_load_unsigned_half(uint32_t addr)
Definition neorv32_cpu.h:112
uint64_t neorv32_cpu_get_instret(void)
Definition neorv32_cpu.c:90
uint32_t neorv32_cpu_load_unsigned_word(uint32_t addr)
Definition neorv32_cpu.h:95
void neorv32_cpu_set_minstret(uint64_t value)
Definition neorv32_cpu.c:116
uint32_t neorv32_cpu_hpm_get_num_counters(void)
Definition neorv32_cpu.c:374
void neorv32_cpu_sleep(void)
Definition neorv32_cpu.h:227
uint32_t neorv32_cpu_get_clk_from_prsc(int prsc)
Definition neorv32_cpu.c:188
int neorv32_cpu_pmp_configure_region(int index, uint32_t addr, uint8_t config)
Definition neorv32_cpu.c:308
void neorv32_cpu_delay_ms(uint32_t time_ms)
Definition neorv32_cpu.c:138
uint32_t neorv32_cpu_csr_read(const int csr_id)
Definition neorv32_cpu.h:174
void neorv32_cpu_csr_clr(const int csr_id, uint32_t mask)
Definition neorv32_cpu.h:214
uint64_t neorv32_cpu_get_cycle(void)
Definition neorv32_cpu.c:46
uint8_t neorv32_cpu_load_unsigned_byte(uint32_t addr)
Definition neorv32_cpu.h:144
void neorv32_cpu_goto_user_mode(void)
Definition neorv32_cpu.c:447
uint32_t neorv32_cpu_pmp_get_granularity(void)
Definition neorv32_cpu.c:266
uint32_t neorv32_cpu_hpm_get_size(void)
Definition neorv32_cpu.c:409
int16_t neorv32_cpu_load_signed_half(uint32_t addr)
Definition neorv32_cpu.h:129
void neorv32_cpu_store_unsigned_byte(uint32_t addr, uint8_t wdata)
Definition neorv32_cpu.h:79
void neorv32_cpu_csr_write(const int csr_id, uint32_t data)
Definition neorv32_cpu.h:188