OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [lib/] [include/] [neorv32_cpu.h] - Blame information for rev 74

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.