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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [lib/] [source/] [neorv32_cpu.c] - Blame information for rev 9

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
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 9 zero_gravi
 * Enable/disable CPU extension during runtime via the 'misa' CSR.
48 2 zero_gravi
 *
49 9 zero_gravi
 * @warning This is still highly experimental! This function requires the Zicsr + Zifencei CPU extensions.
50
 *
51
 * @param[in] sel Bit to be set in misa CSR / extension to be enabled. See #NEORV32_CPU_MISA_enum.
52
 * @param[in] state Set 1 to enable the selected extension, set 0 to disable it;
53
 * return 0 if success, 1 if error (invalid sel or extension cannot be enabled).
54 2 zero_gravi
 **************************************************************************/
55 9 zero_gravi
int neorv32_cpu_switch_extension(int sel, int state) {
56 2 zero_gravi
 
57 9 zero_gravi
  // get current misa setting
58
  uint32_t misa_curr = neorv32_cpu_csr_read(CSR_MISA);
59
  uint32_t misa_prev = misa_curr;
60 2 zero_gravi
 
61 9 zero_gravi
  // abort if misa.z is cleared
62
  if ((misa_curr & (1 << CPU_MISA_Z_EXT)) == 0) {
63
    return 1;
64
  }
65 2 zero_gravi
 
66 9 zero_gravi
  // out of range?
67
  if (sel > 25) {
68
    return 1;
69
  }
70 2 zero_gravi
 
71 9 zero_gravi
  // enable/disable selected extension
72
  if (state & 1) {
73
    misa_curr |= (1 << sel);
74
  }
75
  else {
76
    misa_curr &= ~(1 << sel);
77
  }
78 2 zero_gravi
 
79 9 zero_gravi
  // try updating misa
80
  neorv32_cpu_csr_write(CSR_MISA, misa_curr);
81
  asm volatile("fence.i"); // required to flush prefetch buffers
82
  asm volatile("nop");
83 2 zero_gravi
 
84 9 zero_gravi
  // dit it work?
85
  if (neorv32_cpu_csr_read(CSR_MISA) == misa_prev) {
86
    return 1; // nope
87
  }
88
  else {
89
    return 0; // fine
90
  }
91 2 zero_gravi
}
92
 
93
 
94
/**********************************************************************//**
95
 * Enable specific CPU interrupt.
96
 *
97
 * @note Interrupts have to be globally enabled via neorv32_cpu_eint(void), too.
98
 *
99
 * @param[in] irq_sel CPU interrupt select. See #NEORV32_CPU_MIE_enum.
100
 * return 0 if success, 1 if error (invalid irq_sel).
101
 **************************************************************************/
102
int neorv32_cpu_irq_enable(uint8_t irq_sel) {
103
 
104
  if ((irq_sel != CPU_MIE_MSIE) && (irq_sel != CPU_MIE_MTIE) && (irq_sel != CPU_MIE_MEIE)) {
105
    return 1;
106
  }
107
 
108
  register uint32_t mask = (uint32_t)(1 << irq_sel);
109
  asm volatile ("csrrs zero, mie, %0" : : "r" (mask));
110
  return 0;
111
}
112
 
113
 
114
/**********************************************************************//**
115
 * Disable specific CPU interrupt.
116
 *
117
 * @param[in] irq_sel CPU interrupt select. See #NEORV32_CPU_MIE_enum.
118
 * return 0 if success, 1 if error (invalid irq_sel).
119
 **************************************************************************/
120
int neorv32_cpu_irq_disable(uint8_t irq_sel) {
121
 
122
  if ((irq_sel != CPU_MIE_MSIE) && (irq_sel != CPU_MIE_MTIE) && (irq_sel != CPU_MIE_MEIE)) {
123
    return 1;
124
  }
125
 
126
  register uint32_t mask = (uint32_t)(1 << irq_sel);
127
  asm volatile ("csrrc zero, mie, %0" : : "r" (mask));
128
  return 0;
129
}
130
 
131
 
132
/**********************************************************************//**
133
 * Simple delay function (not very precise) using busy wait.
134
 *
135
 * @param[in] time_ms Time in ms to wait.
136
 **************************************************************************/
137
void neorv32_cpu_delay_ms(uint32_t time_ms) {
138
 
139
  uint32_t clock_speed = neorv32_cpu_csr_read(CSR_MCLOCK) >> 10; // fake divide by 1000
140
  clock_speed = clock_speed >> 5; // divide by loop execution time (~30 cycles)
141
  uint32_t cnt = clock_speed * time_ms;
142
 
143
  // one iteration = ~30 cycles
144
  while (cnt) {
145
    asm volatile("nop");
146
    asm volatile("nop");
147
    asm volatile("nop");
148
    asm volatile("nop");
149
    cnt--;
150
  }
151
}
152
 

powered by: WebSVN 2.1.0

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