| 1 |
2 |
zero_gravi |
// #################################################################################################
|
| 2 |
|
|
// # << NEORV32: neorv32_cpu.c - CPU Core Functions HW Driver >> #
|
| 3 |
|
|
// # ********************************************************************************************* #
|
| 4 |
|
|
// # BSD 3-Clause License #
|
| 5 |
|
|
// # #
|
| 6 |
|
|
// # Copyright (c) 2020, 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 |
|
|
/**********************************************************************//**
|
| 37 |
|
|
* @file neorv32_cpu.c
|
| 38 |
|
|
* @author Stephan Nolting
|
| 39 |
|
|
* @brief CPU Core Functions HW driver source file.
|
| 40 |
|
|
**************************************************************************/
|
| 41 |
|
|
|
| 42 |
|
|
#include "neorv32.h"
|
| 43 |
|
|
#include "neorv32_cpu.h"
|
| 44 |
|
|
|
| 45 |
|
|
|
| 46 |
|
|
/**********************************************************************//**
|
| 47 |
|
|
* Enable specific CPU interrupt.
|
| 48 |
|
|
*
|
| 49 |
|
|
* @note Interrupts have to be globally enabled via neorv32_cpu_eint(void), too.
|
| 50 |
|
|
*
|
| 51 |
|
|
* @param[in] irq_sel CPU interrupt select. See #NEORV32_CPU_MIE_enum.
|
| 52 |
12 |
zero_gravi |
* @return 0 if success, 1 if error (invalid irq_sel).
|
| 53 |
2 |
zero_gravi |
**************************************************************************/
|
| 54 |
|
|
int neorv32_cpu_irq_enable(uint8_t irq_sel) {
|
| 55 |
|
|
|
| 56 |
14 |
zero_gravi |
if ((irq_sel != CPU_MIE_MSIE) && (irq_sel != CPU_MIE_MTIE) && (irq_sel != CPU_MIE_MEIE) &&
|
| 57 |
|
|
(irq_sel != CPU_MIE_FIRQ0E) && (irq_sel != CPU_MIE_FIRQ1E) && (irq_sel != CPU_MIE_FIRQ2E) && (irq_sel != CPU_MIE_FIRQ3E)) {
|
| 58 |
2 |
zero_gravi |
return 1;
|
| 59 |
|
|
}
|
| 60 |
|
|
|
| 61 |
|
|
register uint32_t mask = (uint32_t)(1 << irq_sel);
|
| 62 |
|
|
asm volatile ("csrrs zero, mie, %0" : : "r" (mask));
|
| 63 |
|
|
return 0;
|
| 64 |
|
|
}
|
| 65 |
|
|
|
| 66 |
|
|
|
| 67 |
|
|
/**********************************************************************//**
|
| 68 |
|
|
* Disable specific CPU interrupt.
|
| 69 |
|
|
*
|
| 70 |
|
|
* @param[in] irq_sel CPU interrupt select. See #NEORV32_CPU_MIE_enum.
|
| 71 |
12 |
zero_gravi |
* @return 0 if success, 1 if error (invalid irq_sel).
|
| 72 |
2 |
zero_gravi |
**************************************************************************/
|
| 73 |
|
|
int neorv32_cpu_irq_disable(uint8_t irq_sel) {
|
| 74 |
|
|
|
| 75 |
14 |
zero_gravi |
if ((irq_sel != CPU_MIE_MSIE) && (irq_sel != CPU_MIE_MTIE) && (irq_sel != CPU_MIE_MEIE) &&
|
| 76 |
|
|
(irq_sel != CPU_MIE_FIRQ0E) && (irq_sel != CPU_MIE_FIRQ1E) && (irq_sel != CPU_MIE_FIRQ2E) && (irq_sel != CPU_MIE_FIRQ3E)) {
|
| 77 |
2 |
zero_gravi |
return 1;
|
| 78 |
|
|
}
|
| 79 |
|
|
|
| 80 |
|
|
register uint32_t mask = (uint32_t)(1 << irq_sel);
|
| 81 |
|
|
asm volatile ("csrrc zero, mie, %0" : : "r" (mask));
|
| 82 |
|
|
return 0;
|
| 83 |
|
|
}
|
| 84 |
|
|
|
| 85 |
|
|
|
| 86 |
|
|
/**********************************************************************//**
|
| 87 |
12 |
zero_gravi |
* Get cycle count from cycle[h].
|
| 88 |
|
|
*
|
| 89 |
|
|
* @note The cycle[h] CSR is shadowed copy of the mcycle[h] CSR.
|
| 90 |
|
|
*
|
| 91 |
|
|
* @return Current cycle counter (64 bit).
|
| 92 |
|
|
**************************************************************************/
|
| 93 |
|
|
uint64_t neorv32_cpu_get_cycle(void) {
|
| 94 |
|
|
|
| 95 |
|
|
union {
|
| 96 |
|
|
uint64_t uint64;
|
| 97 |
|
|
uint32_t uint32[sizeof(uint64_t)/2];
|
| 98 |
|
|
} cycles;
|
| 99 |
|
|
|
| 100 |
|
|
uint32_t tmp1, tmp2, tmp3;
|
| 101 |
|
|
while(1) {
|
| 102 |
|
|
tmp1 = neorv32_cpu_csr_read(CSR_CYCLEH);
|
| 103 |
|
|
tmp2 = neorv32_cpu_csr_read(CSR_CYCLE);
|
| 104 |
|
|
tmp3 = neorv32_cpu_csr_read(CSR_CYCLEH);
|
| 105 |
|
|
if (tmp1 == tmp3) {
|
| 106 |
|
|
break;
|
| 107 |
|
|
}
|
| 108 |
|
|
}
|
| 109 |
|
|
|
| 110 |
|
|
cycles.uint32[0] = tmp2;
|
| 111 |
|
|
cycles.uint32[1] = tmp3;
|
| 112 |
|
|
|
| 113 |
|
|
return cycles.uint64;
|
| 114 |
|
|
}
|
| 115 |
|
|
|
| 116 |
|
|
|
| 117 |
|
|
/**********************************************************************//**
|
| 118 |
|
|
* Set mcycle[h] counter.
|
| 119 |
|
|
*
|
| 120 |
|
|
* @param[in] value New value for mcycle[h] CSR (64-bit).
|
| 121 |
|
|
**************************************************************************/
|
| 122 |
|
|
void neorv32_cpu_set_mcycle(uint64_t value) {
|
| 123 |
|
|
|
| 124 |
|
|
union {
|
| 125 |
|
|
uint64_t uint64;
|
| 126 |
|
|
uint32_t uint32[sizeof(uint64_t)/2];
|
| 127 |
|
|
} cycles;
|
| 128 |
|
|
|
| 129 |
|
|
cycles.uint64 = value;
|
| 130 |
|
|
|
| 131 |
|
|
neorv32_cpu_csr_write(CSR_MCYCLE, 0);
|
| 132 |
|
|
neorv32_cpu_csr_write(CSR_MCYCLEH, cycles.uint32[1]);
|
| 133 |
|
|
neorv32_cpu_csr_write(CSR_MCYCLE, cycles.uint32[0]);
|
| 134 |
|
|
}
|
| 135 |
|
|
|
| 136 |
|
|
|
| 137 |
|
|
/**********************************************************************//**
|
| 138 |
|
|
* Get retired instructions counter from instret[h].
|
| 139 |
|
|
*
|
| 140 |
|
|
* @note The instret[h] CSR is shadowed copy of the instret[h] CSR.
|
| 141 |
|
|
*
|
| 142 |
|
|
* @return Current instructions counter (64 bit).
|
| 143 |
|
|
**************************************************************************/
|
| 144 |
|
|
uint64_t neorv32_cpu_get_instret(void) {
|
| 145 |
|
|
|
| 146 |
|
|
union {
|
| 147 |
|
|
uint64_t uint64;
|
| 148 |
|
|
uint32_t uint32[sizeof(uint64_t)/2];
|
| 149 |
|
|
} cycles;
|
| 150 |
|
|
|
| 151 |
|
|
uint32_t tmp1, tmp2, tmp3;
|
| 152 |
|
|
while(1) {
|
| 153 |
|
|
tmp1 = neorv32_cpu_csr_read(CSR_INSTRETH);
|
| 154 |
|
|
tmp2 = neorv32_cpu_csr_read(CSR_INSTRET);
|
| 155 |
|
|
tmp3 = neorv32_cpu_csr_read(CSR_INSTRETH);
|
| 156 |
|
|
if (tmp1 == tmp3) {
|
| 157 |
|
|
break;
|
| 158 |
|
|
}
|
| 159 |
|
|
}
|
| 160 |
|
|
|
| 161 |
|
|
cycles.uint32[0] = tmp2;
|
| 162 |
|
|
cycles.uint32[1] = tmp3;
|
| 163 |
|
|
|
| 164 |
|
|
return cycles.uint64;
|
| 165 |
|
|
}
|
| 166 |
|
|
|
| 167 |
|
|
|
| 168 |
|
|
/**********************************************************************//**
|
| 169 |
|
|
* Set retired instructions counter minstret[h].
|
| 170 |
|
|
*
|
| 171 |
|
|
* @param[in] value New value for mcycle[h] CSR (64-bit).
|
| 172 |
|
|
**************************************************************************/
|
| 173 |
|
|
void neorv32_cpu_set_minstret(uint64_t value) {
|
| 174 |
|
|
|
| 175 |
|
|
union {
|
| 176 |
|
|
uint64_t uint64;
|
| 177 |
|
|
uint32_t uint32[sizeof(uint64_t)/2];
|
| 178 |
|
|
} cycles;
|
| 179 |
|
|
|
| 180 |
|
|
cycles.uint64 = value;
|
| 181 |
|
|
|
| 182 |
|
|
neorv32_cpu_csr_write(CSR_MINSTRET, 0);
|
| 183 |
|
|
neorv32_cpu_csr_write(CSR_MINSTRETH, cycles.uint32[1]);
|
| 184 |
|
|
neorv32_cpu_csr_write(CSR_MINSTRET, cycles.uint32[0]);
|
| 185 |
|
|
}
|
| 186 |
|
|
|
| 187 |
|
|
|
| 188 |
|
|
/**********************************************************************//**
|
| 189 |
|
|
* Get current system time from time[h] CSR.
|
| 190 |
|
|
*
|
| 191 |
|
|
* @note This function requires the MTIME system timer to be implemented.
|
| 192 |
|
|
*
|
| 193 |
|
|
* @return Current system time (64 bit).
|
| 194 |
|
|
**************************************************************************/
|
| 195 |
|
|
uint64_t neorv32_cpu_get_systime(void) {
|
| 196 |
|
|
|
| 197 |
|
|
union {
|
| 198 |
|
|
uint64_t uint64;
|
| 199 |
|
|
uint32_t uint32[sizeof(uint64_t)/2];
|
| 200 |
|
|
} cycles;
|
| 201 |
|
|
|
| 202 |
|
|
uint32_t tmp1, tmp2, tmp3;
|
| 203 |
|
|
while(1) {
|
| 204 |
|
|
tmp1 = neorv32_cpu_csr_read(CSR_TIMEH);
|
| 205 |
|
|
tmp2 = neorv32_cpu_csr_read(CSR_TIME);
|
| 206 |
|
|
tmp3 = neorv32_cpu_csr_read(CSR_TIMEH);
|
| 207 |
|
|
if (tmp1 == tmp3) {
|
| 208 |
|
|
break;
|
| 209 |
|
|
}
|
| 210 |
|
|
}
|
| 211 |
|
|
|
| 212 |
|
|
cycles.uint32[0] = tmp2;
|
| 213 |
|
|
cycles.uint32[1] = tmp3;
|
| 214 |
|
|
|
| 215 |
|
|
return cycles.uint64;
|
| 216 |
|
|
}
|
| 217 |
|
|
|
| 218 |
|
|
|
| 219 |
|
|
/**********************************************************************//**
|
| 220 |
2 |
zero_gravi |
* Simple delay function (not very precise) using busy wait.
|
| 221 |
|
|
*
|
| 222 |
|
|
* @param[in] time_ms Time in ms to wait.
|
| 223 |
|
|
**************************************************************************/
|
| 224 |
|
|
void neorv32_cpu_delay_ms(uint32_t time_ms) {
|
| 225 |
|
|
|
| 226 |
12 |
zero_gravi |
uint32_t clock_speed = SYSINFO_CLK >> 10; // fake divide by 1000
|
| 227 |
2 |
zero_gravi |
clock_speed = clock_speed >> 5; // divide by loop execution time (~30 cycles)
|
| 228 |
|
|
uint32_t cnt = clock_speed * time_ms;
|
| 229 |
|
|
|
| 230 |
|
|
// one iteration = ~30 cycles
|
| 231 |
|
|
while (cnt) {
|
| 232 |
|
|
asm volatile("nop");
|
| 233 |
|
|
asm volatile("nop");
|
| 234 |
|
|
asm volatile("nop");
|
| 235 |
|
|
asm volatile("nop");
|
| 236 |
|
|
cnt--;
|
| 237 |
|
|
}
|
| 238 |
|
|
}
|
| 239 |
|
|
|
| 240 |
15 |
zero_gravi |
|
| 241 |
|
|
/**********************************************************************//**
|
| 242 |
|
|
* Switch from privilege mode MACHINE to privilege mode USER.
|
| 243 |
|
|
*
|
| 244 |
|
|
* @note This function requires the U extension to be implemented.
|
| 245 |
18 |
zero_gravi |
* @note Maybe you should do a fence.i after this.
|
| 246 |
15 |
zero_gravi |
**************************************************************************/
|
| 247 |
|
|
void __attribute__((naked)) neorv32_cpu_goto_user_mode(void) {
|
| 248 |
|
|
|
| 249 |
35 |
zero_gravi |
// make sure to use NO registers in here! -> naked
|
| 250 |
15 |
zero_gravi |
|
| 251 |
35 |
zero_gravi |
asm volatile ("csrw mepc, ra \n\t" // move return address to mepc so we can return using "mret". also, we can use ra as general purpose register in here
|
| 252 |
|
|
"li ra, %[input_imm] \n\t" // bit mask to clear the two MPP bits
|
| 253 |
|
|
"csrrc zero, mstatus, ra \n\t" // clear MPP bits -> MPP=u-mode
|
| 254 |
|
|
"mret \n\t" // return and switch to user mode
|
| 255 |
|
|
: : [input_imm] "i" ((1<<CPU_MSTATUS_MPP_H) | (1<<CPU_MSTATUS_MPP_L)));
|
| 256 |
15 |
zero_gravi |
}
|