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

Subversion Repositories neorv32

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

Go to most recent revision | 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 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 56 zero_gravi
void neorv32_cpu_delay_ms(int16_t time_ms);
54 15 zero_gravi
void __attribute__((naked)) neorv32_cpu_goto_user_mode(void);
55 39 zero_gravi
int neorv32_cpu_atomic_cas(uint32_t addr, uint32_t expected, uint32_t desired);
56 42 zero_gravi
uint32_t neorv32_cpu_pmp_get_num_regions(void);
57 40 zero_gravi
uint32_t neorv32_cpu_pmp_get_granularity(void);
58
int neorv32_cpu_pmp_configure_region(uint32_t index, uint32_t base, uint32_t size, uint8_t config);
59 42 zero_gravi
uint32_t neorv32_cpu_hpm_get_counters(void);
60 56 zero_gravi
uint32_t neorv32_cpu_hpm_get_size(void);
61 55 zero_gravi
int neorv32_check_zextension(uint32_t);
62 2 zero_gravi
 
63
 
64
/**********************************************************************//**
65 56 zero_gravi
 * Store unsigned word to address space.
66
 *
67
 * @note An unaligned access address will raise an alignment exception.
68
 *
69
 * @param[in] addr Address (32-bit).
70
 * @param[in] wdata Data word (32-bit) to store.
71
 **************************************************************************/
72
inline void __attribute__ ((always_inline)) neorv32_cpu_store_unsigned_word(uint32_t addr, uint32_t wdata) {
73
 
74
  register uint32_t reg_addr = addr;
75
  register uint32_t reg_data = wdata;
76
 
77
  asm volatile ("sw %[da], 0(%[ad])" : : [da] "r" (reg_data), [ad] "r" (reg_addr));
78
}
79
 
80
 
81
/**********************************************************************//**
82
 * Store unsigned half-word to address space.
83
 *
84
 * @note An unaligned access address will raise an alignment exception.
85
 *
86
 * @param[in] addr Address (32-bit).
87
 * @param[in] wdata Data half-word (16-bit) to store.
88
 **************************************************************************/
89
inline void __attribute__ ((always_inline)) neorv32_cpu_store_unsigned_half(uint32_t addr, uint16_t wdata) {
90
 
91
  register uint32_t reg_addr = addr;
92
  register uint32_t reg_data = (uint32_t)wdata;
93
 
94
  asm volatile ("sh %[da], 0(%[ad])" : : [da] "r" (reg_data), [ad] "r" (reg_addr));
95
}
96
 
97
 
98
/**********************************************************************//**
99
 * Store unsigned byte to address space.
100
 *
101
 * @param[in] addr Address (32-bit).
102
 * @param[in] wdata Data byte (8-bit) to store.
103
 **************************************************************************/
104
inline void __attribute__ ((always_inline)) neorv32_cpu_store_unsigned_byte(uint32_t addr, uint8_t wdata) {
105
 
106
  register uint32_t reg_addr = addr;
107
  register uint32_t reg_data = (uint32_t)wdata;
108
 
109
  asm volatile ("sb %[da], 0(%[ad])" : : [da] "r" (reg_data), [ad] "r" (reg_addr));
110
}
111
 
112
 
113
/**********************************************************************//**
114
 * Load unsigned word from address space.
115
 *
116
 * @note An unaligned access address will raise an alignment exception.
117
 *
118
 * @param[in] addr Address (32-bit).
119
 * @return Read data word (32-bit).
120
 **************************************************************************/
121
inline uint32_t __attribute__ ((always_inline)) neorv32_cpu_load_unsigned_word(uint32_t addr) {
122
 
123
  register uint32_t reg_addr = addr;
124
  register uint32_t reg_data;
125
 
126
  asm volatile ("lw %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
127
 
128
  return (uint32_t)reg_data;
129
}
130
 
131
 
132
/**********************************************************************//**
133
 * Load unsigned half-word from address space.
134
 *
135
 * @note An unaligned access address will raise an alignment exception.
136
 *
137
 * @param[in] addr Address (32-bit).
138
 * @return Read data half-word (16-bit).
139
 **************************************************************************/
140
inline uint16_t __attribute__ ((always_inline)) neorv32_cpu_load_unsigned_half(uint32_t addr) {
141
 
142
  register uint32_t reg_addr = addr;
143
  register uint32_t reg_data;
144
 
145
  asm volatile ("lhu %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
146
 
147
  return (uint16_t)reg_data;
148
}
149
 
150
 
151
/**********************************************************************//**
152
 * Load unsigned byte from address space.
153
 *
154
 * @param[in] addr Address (32-bit).
155
 * @return Read data byte (8-bit).
156
 **************************************************************************/
157
inline uint8_t __attribute__ ((always_inline)) neorv32_cpu_load_unsigned_byte(uint32_t addr) {
158
 
159
  register uint32_t reg_addr = addr;
160
  register uint32_t reg_data;
161
 
162
  asm volatile ("lbu %[da], 0(%[ad])" : [da] "=r" (reg_data) : [ad] "r" (reg_addr));
163
 
164
  return (uint8_t)reg_data;
165
}
166
 
167
 
168
/**********************************************************************//**
169 2 zero_gravi
 * Read data from CPU configuration and status register (CSR).
170
 *
171 42 zero_gravi
 * @param[in] csr_id ID of CSR to read. See #NEORV32_CSR_enum.
172 2 zero_gravi
 * @return Read data (uint32_t).
173
 **************************************************************************/
174
inline uint32_t __attribute__ ((always_inline)) neorv32_cpu_csr_read(const int csr_id) {
175
 
176
  register uint32_t csr_data;
177
 
178 6 zero_gravi
  asm volatile ("csrr %[result], %[input_i]" : [result] "=r" (csr_data) : [input_i] "i" (csr_id));
179 2 zero_gravi
 
180
  return csr_data;
181
}
182
 
183
 
184
/**********************************************************************//**
185
 * Write data to CPU configuration and status register (CSR).
186
 *
187 42 zero_gravi
 * @param[in] csr_id ID of CSR to write. See #NEORV32_CSR_enum.
188 2 zero_gravi
 * @param[in] data Data to write (uint32_t).
189
 **************************************************************************/
190
inline void __attribute__ ((always_inline)) neorv32_cpu_csr_write(const int csr_id, uint32_t data) {
191
 
192
  register uint32_t csr_data = data;
193
 
194 6 zero_gravi
  asm volatile ("csrw %[input_i], %[input_j]" :  : [input_i] "i" (csr_id), [input_j] "r" (csr_data));
195 2 zero_gravi
}
196
 
197 9 zero_gravi
 
198
/**********************************************************************//**
199
 * Put CPU into "sleep" mode.
200
 *
201
 * @note This function executes the WFI insstruction.
202
 * The WFI (wait for interrupt) instruction will make the CPU stall until
203
 * an interupt request is detected. Interrupts have to be globally enabled
204 56 zero_gravi
 * and at least one external source must be enabled (like the MTI machine
205
 * timer interrupt) to allow the CPU to wake up again. If 'Zicsr' CPU extension is disabled,
206 9 zero_gravi
 * this will permanently stall the CPU.
207
 **************************************************************************/
208
inline void __attribute__ ((always_inline)) neorv32_cpu_sleep(void) {
209
 
210
  asm volatile ("wfi");
211
}
212
 
213
 
214
/**********************************************************************//**
215
 * Enable global CPU interrupts (via MIE flag in mstatus CSR).
216
 **************************************************************************/
217
inline void __attribute__ ((always_inline)) neorv32_cpu_eint(void) {
218
 
219 42 zero_gravi
  asm volatile ("csrrsi zero, mstatus, %0" : : "i" (1 << CSR_MSTATUS_MIE));
220 40 zero_gravi
  asm volatile ("nop");
221
  asm volatile ("nop");
222 9 zero_gravi
}
223
 
224
 
225
/**********************************************************************//**
226
 * Disable global CPU interrupts (via MIE flag in mstatus CSR).
227
 **************************************************************************/
228
inline void __attribute__ ((always_inline)) neorv32_cpu_dint(void) {
229
 
230 42 zero_gravi
  asm volatile ("csrrci zero, mstatus, %0" : : "i" (1 << CSR_MSTATUS_MIE));
231 40 zero_gravi
  asm volatile ("nop");
232
  asm volatile ("nop");
233 9 zero_gravi
}
234
 
235
 
236
/**********************************************************************//**
237
 * Trigger breakpoint exception (via EBREAK instruction).
238
 **************************************************************************/
239
inline void __attribute__ ((always_inline)) neorv32_cpu_breakpoint(void) {
240
 
241
  asm volatile ("ebreak");
242
}
243
 
244
 
245
/**********************************************************************//**
246
 * Trigger "environment call" exception (via ECALL instruction).
247
 **************************************************************************/
248
inline void __attribute__ ((always_inline)) neorv32_cpu_env_call(void) {
249
 
250
  asm volatile ("ecall");
251
}
252
 
253
 
254 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.