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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_package.vhd] - Blame information for rev 64

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

Line No. Rev Author Line
1 2 zero_gravi
-- #################################################################################################
2
-- # << NEORV32 - Main VHDL package file >>                                                        #
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
library ieee;
36
use ieee.std_logic_1164.all;
37
use ieee.numeric_std.all;
38
 
39
package neorv32_package is
40
 
41 36 zero_gravi
  -- Architecture Configuration -------------------------------------------------------------
42
  -- -------------------------------------------------------------------------------------------
43 40 zero_gravi
  -- address space --
44
  constant ispace_base_c : std_ulogic_vector(31 downto 0) := x"00000000"; -- default instruction memory address space base address
45
  constant dspace_base_c : std_ulogic_vector(31 downto 0) := x"80000000"; -- default data memory address space base address
46 36 zero_gravi
 
47 40 zero_gravi
  -- CPU core --
48 56 zero_gravi
  constant cp_timeout_en_c   : boolean := false; -- auto-terminate pending co-processor operations after 256 cycles (for debugging only), default = false
49
  constant dedicated_reset_c : boolean := false; -- use dedicated hardware reset value for UNCRITICAL registers (FALSE=reset value is irrelevant (might simplify HW), default; TRUE=defined LOW reset value)
50 40 zero_gravi
 
51 54 zero_gravi
  -- "critical" number of implemented PMP regions --
52
  -- if more PMP regions (> pmp_num_regions_critical_c) are defined, another register stage is automatically inserted into the memory interfaces
53
  -- increasing instruction fetch & data access latency by +1 cycle but also reducing critical path length
54
  constant pmp_num_regions_critical_c : natural := 8; -- default=8
55 47 zero_gravi
 
56 57 zero_gravi
  -- "response time window" for processor-internal memories and IO devices
57
  constant max_proc_int_response_time_c : natural := 15; -- cycles after which an *unacknowledged* internal bus access will timeout and trigger a bus fault exception (min 2)
58
 
59 59 zero_gravi
  -- jtag tap - identifier --
60
  constant jtag_tap_idcode_version_c : std_ulogic_vector(03 downto 0) := x"0"; -- version
61
  constant jtag_tap_idcode_partid_c  : std_ulogic_vector(15 downto 0) := x"cafe"; -- part number
62
  constant jtag_tap_idcode_manid_c   : std_ulogic_vector(10 downto 0) := "00000000000"; -- manufacturer id
63
 
64 61 zero_gravi
  -- Architecture Constants (do not modify!) ------------------------------------------------
65
  -- -------------------------------------------------------------------------------------------
66 62 zero_gravi
  constant data_width_c : natural := 32; -- native data path width - do not change!
67 64 zero_gravi
  constant hw_version_c : std_ulogic_vector(31 downto 0) := x"01060100"; -- no touchy!
68 62 zero_gravi
  constant archid_c     : natural := 19; -- official NEORV32 architecture ID - hands off!
69 61 zero_gravi
 
70
  -- External Interface Types ---------------------------------------------------------------
71
  -- -------------------------------------------------------------------------------------------
72
  type sdata_8x32_t  is array (0 to 7)  of std_ulogic_vector(31 downto 0);
73
  type sdata_8x32r_t is array (0 to 7)  of std_logic_vector(31 downto 0); -- resolved type
74
 
75
  -- Internal Interface Types ---------------------------------------------------------------
76
  -- -------------------------------------------------------------------------------------------
77
  type pmp_ctrl_if_t is array (0 to 63) of std_ulogic_vector(07 downto 0);
78
  type pmp_addr_if_t is array (0 to 63) of std_ulogic_vector(33 downto 0);
79
  type cp_data_if_t  is array (0 to 3)  of std_ulogic_vector(data_width_c-1 downto 0);
80
 
81
  -- Internal Memory Types Configuration Types ----------------------------------------------
82
  -- -------------------------------------------------------------------------------------------
83
  type mem32_t is array (natural range <>) of std_ulogic_vector(31 downto 0); -- memory with 32-bit entries
84
  type mem8_t  is array (natural range <>) of std_ulogic_vector(07 downto 0); -- memory with 8-bit entries
85
 
86 12 zero_gravi
  -- Helper Functions -----------------------------------------------------------------------
87 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
88
  function index_size_f(input : natural) return natural;
89
  function cond_sel_natural_f(cond : boolean; val_t : natural; val_f : natural) return natural;
90 56 zero_gravi
  function cond_sel_int_f(cond : boolean; val_t : integer; val_f : integer) return integer;
91 2 zero_gravi
  function cond_sel_stdulogicvector_f(cond : boolean; val_t : std_ulogic_vector; val_f : std_ulogic_vector) return std_ulogic_vector;
92 56 zero_gravi
  function cond_sel_stdulogic_f(cond : boolean; val_t : std_ulogic; val_f : std_ulogic) return std_ulogic;
93 50 zero_gravi
  function cond_sel_string_f(cond : boolean; val_t : string; val_f : string) return string;
94 2 zero_gravi
  function bool_to_ulogic_f(cond : boolean) return std_ulogic;
95 60 zero_gravi
  function or_reduce_f(a : std_ulogic_vector) return std_ulogic;
96
  function and_reduce_f(a : std_ulogic_vector) return std_ulogic;
97
  function xor_reduce_f(a : std_ulogic_vector) return std_ulogic;
98 6 zero_gravi
  function to_hexchar_f(input : std_ulogic_vector(3 downto 0)) return character;
99 40 zero_gravi
  function hexchar_to_stdulogicvector_f(input : character) return std_ulogic_vector;
100 32 zero_gravi
  function bit_rev_f(input : std_ulogic_vector) return std_ulogic_vector;
101 36 zero_gravi
  function is_power_of_two_f(input : natural) return boolean;
102 40 zero_gravi
  function bswap32_f(input : std_ulogic_vector) return std_ulogic_vector;
103 62 zero_gravi
  function char_to_lower_f(ch : character) return character;
104 61 zero_gravi
  function str_equal_f(str0 : string; str1 : string) return boolean;
105 63 zero_gravi
  function popcount_f(input : std_ulogic_vector) return natural;
106
  function leading_zeros_f(input : std_ulogic_vector) return natural;
107 61 zero_gravi
  impure function mem32_init_f(init : mem32_t; depth : natural) return mem32_t;
108 2 zero_gravi
 
109 61 zero_gravi
  -- Internal (auto-generated) Configurations -----------------------------------------------
110 56 zero_gravi
  -- -------------------------------------------------------------------------------------------
111 61 zero_gravi
  constant def_rst_val_c : std_ulogic := cond_sel_stdulogic_f(dedicated_reset_c, '0', '-');
112 56 zero_gravi
 
113 23 zero_gravi
  -- Processor-Internal Address Space Layout ------------------------------------------------
114
  -- -------------------------------------------------------------------------------------------
115 34 zero_gravi
  -- Internal Instruction Memory (IMEM) and Date Memory (DMEM) --
116 39 zero_gravi
  constant imem_base_c          : std_ulogic_vector(data_width_c-1 downto 0) := ispace_base_c; -- internal instruction memory base address
117
  constant dmem_base_c          : std_ulogic_vector(data_width_c-1 downto 0) := dspace_base_c; -- internal data memory base address
118 42 zero_gravi
  --> internal data/instruction memory sizes are configured via top's generics
119 2 zero_gravi
 
120 64 zero_gravi
  -- !!! IMPORTANT: The base address of each component/module has to be aligned to the !!!
121
  -- !!! total size of the module's occupied address space. The occupied address space !!!
122
  -- !!! has to be a power of two (minimum 4 bytes). Address spaces must not overlap.  !!!
123
 
124 23 zero_gravi
  -- Internal Bootloader ROM --
125 61 zero_gravi
  -- Actual bootloader size is determined during runtime via the length of the bootloader initialization image
126 56 zero_gravi
  constant boot_rom_base_c      : std_ulogic_vector(data_width_c-1 downto 0) := x"ffff0000"; -- bootloader base address, fixed!
127 61 zero_gravi
  constant boot_rom_max_size_c  : natural := 32*1024; -- max module's address space size in bytes, fixed!
128 23 zero_gravi
 
129 59 zero_gravi
  -- On-Chip Debugger: Debug Module --
130
  constant dm_base_c            : std_ulogic_vector(data_width_c-1 downto 0) := x"fffff800"; -- base address, fixed!
131 61 zero_gravi
  constant dm_size_c            : natural := 4*32*4; -- debug ROM address space size in bytes, fixed
132 59 zero_gravi
  constant dm_code_base_c       : std_ulogic_vector(data_width_c-1 downto 0) := x"fffff800";
133
  constant dm_pbuf_base_c       : std_ulogic_vector(data_width_c-1 downto 0) := x"fffff880";
134
  constant dm_data_base_c       : std_ulogic_vector(data_width_c-1 downto 0) := x"fffff900";
135
  constant dm_sreg_base_c       : std_ulogic_vector(data_width_c-1 downto 0) := x"fffff980";
136
 
137 2 zero_gravi
  -- IO: Peripheral Devices ("IO") Area --
138
  -- Control register(s) (including the device-enable) should be located at the base address of each device
139 60 zero_gravi
  constant io_base_c            : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe00";
140 61 zero_gravi
  constant io_size_c            : natural := 512; -- IO address space size in bytes, fixed!
141 2 zero_gravi
 
142 47 zero_gravi
  -- Custom Functions Subsystem (CFS) --
143 60 zero_gravi
  constant cfs_base_c           : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe00"; -- base address
144 61 zero_gravi
  constant cfs_size_c           : natural := 32*4; -- module's address space in bytes
145 60 zero_gravi
  constant cfs_reg0_addr_c      : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe00";
146
  constant cfs_reg1_addr_c      : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe04";
147
  constant cfs_reg2_addr_c      : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe08";
148
  constant cfs_reg3_addr_c      : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe0c";
149
  constant cfs_reg4_addr_c      : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe10";
150
  constant cfs_reg5_addr_c      : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe14";
151
  constant cfs_reg6_addr_c      : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe18";
152
  constant cfs_reg7_addr_c      : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe1c";
153
  constant cfs_reg8_addr_c      : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe20";
154
  constant cfs_reg9_addr_c      : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe24";
155
  constant cfs_reg10_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe28";
156
  constant cfs_reg11_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe2c";
157
  constant cfs_reg12_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe30";
158
  constant cfs_reg13_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe34";
159
  constant cfs_reg14_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe38";
160
  constant cfs_reg15_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe3c";
161
  constant cfs_reg16_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe40";
162
  constant cfs_reg17_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe44";
163
  constant cfs_reg18_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe48";
164
  constant cfs_reg19_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe4c";
165
  constant cfs_reg20_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe50";
166
  constant cfs_reg21_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe54";
167
  constant cfs_reg22_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe58";
168
  constant cfs_reg23_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe5c";
169
  constant cfs_reg24_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe60";
170
  constant cfs_reg25_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe64";
171
  constant cfs_reg26_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe68";
172
  constant cfs_reg27_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe6c";
173
  constant cfs_reg28_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe70";
174
  constant cfs_reg29_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe74";
175
  constant cfs_reg30_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe78";
176
  constant cfs_reg31_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe7c";
177 47 zero_gravi
 
178 60 zero_gravi
  -- Pulse-Width Modulation Controller (PWM) --
179
  constant pwm_base_c           : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe80"; -- base address
180 61 zero_gravi
  constant pwm_size_c           : natural := 16*4; -- module's address space size in bytes
181 60 zero_gravi
  constant pwm_ctrl_addr_c      : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe80";
182
  constant pwm_duty0_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe84";
183
  constant pwm_duty1_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe88";
184
  constant pwm_duty2_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe8c";
185
  constant pwm_duty3_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe90";
186
  constant pwm_duty4_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe94";
187
  constant pwm_duty5_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe98";
188
  constant pwm_duty6_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffe9c";
189
  constant pwm_duty7_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffea0";
190
  constant pwm_duty8_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffea4";
191
  constant pwm_duty9_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffea8";
192
  constant pwm_duty10_addr_c    : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffeac";
193
  constant pwm_duty11_addr_c    : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffeb0";
194
  constant pwm_duty12_addr_c    : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffeb4";
195
  constant pwm_duty13_addr_c    : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffeb8";
196
  constant pwm_duty14_addr_c    : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffebc";
197
 
198 63 zero_gravi
  -- Stream Link Interface (SLINK) --
199 61 zero_gravi
  constant slink_base_c         : std_ulogic_vector(data_width_c-1 downto 0) := x"fffffec0"; -- base address
200
  constant slink_size_c         : natural := 16*4; -- module's address space size in bytes
201 60 zero_gravi
 
202
  -- reserved --
203
--constant reserved_base_c      : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffff00"; -- base address
204 63 zero_gravi
--constant reserved_size_c      : natural := 16*4; -- module's address space size in bytes
205 60 zero_gravi
 
206 63 zero_gravi
  -- reserved --
207
--constant reserved_base_c      : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffff40"; -- base address
208
--constant reserved_size_c      : natural := 8*4; -- module's address space size in bytes
209
 
210
  -- reserved --
211
--constant reserved_base_c      : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffff60"; -- base address
212
--constant reserved_size_c      : natural := 4*4; -- module's address space size in bytes
213
 
214
  -- reserved --
215
--constant reserved_base_c      : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffff70"; -- base address
216
--constant reserved_size_c      : natural := 2*4; -- module's address space size in bytes
217
 
218
  -- reserved --
219
--constant reserved_base_c      : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffff78"; -- base address
220
--constant reserved_size_c      : natural := 2*4; -- module's address space size in bytes
221
 
222 61 zero_gravi
  -- External Interrupt Controller (XIRQ) --
223
  constant xirq_base_c          : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffff80"; -- base address
224
  constant xirq_size_c          : natural := 4*4; -- module's address space size in bytes
225
  constant xirq_enable_addr_c   : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffff80";
226
  constant xirq_pending_addr_c  : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffff84";
227
  constant xirq_source_addr_c   : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffff88";
228 62 zero_gravi
--constant xirq_reserved_addr_c : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffff8c";
229 2 zero_gravi
 
230
  -- Machine System Timer (MTIME) --
231 56 zero_gravi
  constant mtime_base_c         : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffff90"; -- base address
232 61 zero_gravi
  constant mtime_size_c         : natural := 4*4; -- module's address space size in bytes
233 56 zero_gravi
  constant mtime_time_lo_addr_c : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffff90";
234
  constant mtime_time_hi_addr_c : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffff94";
235
  constant mtime_cmp_lo_addr_c  : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffff98";
236
  constant mtime_cmp_hi_addr_c  : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffff9c";
237 2 zero_gravi
 
238 58 zero_gravi
  -- Primary Universal Asynchronous Receiver/Transmitter (UART0) --
239 56 zero_gravi
  constant uart0_base_c         : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffa0"; -- base address
240 61 zero_gravi
  constant uart0_size_c         : natural := 2*4; -- module's address space size in bytes
241 56 zero_gravi
  constant uart0_ctrl_addr_c    : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffa0";
242
  constant uart0_rtx_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffa4";
243 2 zero_gravi
 
244
  -- Serial Peripheral Interface (SPI) --
245 56 zero_gravi
  constant spi_base_c           : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffa8"; -- base address
246 61 zero_gravi
  constant spi_size_c           : natural := 2*4; -- module's address space size in bytes
247 56 zero_gravi
  constant spi_ctrl_addr_c      : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffa8";
248
  constant spi_rtx_addr_c       : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffac";
249 2 zero_gravi
 
250
  -- Two Wire Interface (TWI) --
251 56 zero_gravi
  constant twi_base_c           : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffb0"; -- base address
252 61 zero_gravi
  constant twi_size_c           : natural := 2*4; -- module's address space size in bytes
253 56 zero_gravi
  constant twi_ctrl_addr_c      : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffb0";
254
  constant twi_rtx_addr_c       : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffb4";
255 2 zero_gravi
 
256 61 zero_gravi
  -- True Random Number Generator (TRNG) --
257
  constant trng_base_c          : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffb8"; -- base address
258
  constant trng_size_c          : natural := 1*4; -- module's address space size in bytes
259
  constant trng_ctrl_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffb8";
260
 
261
  -- Watch Dog Timer (WDT) --
262
  constant wdt_base_c           : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffbc"; -- base address
263
  constant wdt_size_c           : natural := 1*4; -- module's address space size in bytes
264
  constant wdt_ctrl_addr_c      : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffbc";
265
 
266 63 zero_gravi
  -- General Purpose Input/Output Controller (GPIO) --
267 61 zero_gravi
  constant gpio_base_c          : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffc0"; -- base address
268
  constant gpio_size_c          : natural := 4*4; -- module's address space size in bytes
269
  constant gpio_in_lo_addr_c    : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffc0";
270
  constant gpio_in_hi_addr_c    : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffc4";
271
  constant gpio_out_lo_addr_c   : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffc8";
272
  constant gpio_out_hi_addr_c   : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffcc";
273 2 zero_gravi
 
274 58 zero_gravi
  -- Secondary Universal Asynchronous Receiver/Transmitter (UART1) --
275 56 zero_gravi
  constant uart1_base_c         : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffd0"; -- base address
276 61 zero_gravi
  constant uart1_size_c         : natural := 2*4; -- module's address space size in bytes
277 56 zero_gravi
  constant uart1_ctrl_addr_c    : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffd0";
278
  constant uart1_rtx_addr_c     : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffd4";
279 50 zero_gravi
 
280 52 zero_gravi
  -- Smart LED (WS2811/WS2812) Interface (NEOLED) --
281 56 zero_gravi
  constant neoled_base_c        : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffd8"; -- base address
282 61 zero_gravi
  constant neoled_size_c        : natural := 2*4; -- module's address space size in bytes
283 56 zero_gravi
  constant neoled_ctrl_addr_c   : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffd8";
284
  constant neoled_data_addr_c   : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffdc";
285 12 zero_gravi
 
286 23 zero_gravi
  -- System Information Memory (SYSINFO) --
287 56 zero_gravi
  constant sysinfo_base_c       : std_ulogic_vector(data_width_c-1 downto 0) := x"ffffffe0"; -- base address
288 61 zero_gravi
  constant sysinfo_size_c       : natural := 8*4; -- module's address space size in bytes
289 12 zero_gravi
 
290 59 zero_gravi
  -- Main CPU Control Bus -------------------------------------------------------------------
291 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
292
  -- register file --
293 49 zero_gravi
  constant ctrl_rf_in_mux_c     : natural :=  0; -- input source select lsb (0=MEM, 1=ALU)
294
  constant ctrl_rf_rs1_adr0_c   : natural :=  1; -- source register 1 address bit 0
295
  constant ctrl_rf_rs1_adr1_c   : natural :=  2; -- source register 1 address bit 1
296
  constant ctrl_rf_rs1_adr2_c   : natural :=  3; -- source register 1 address bit 2
297
  constant ctrl_rf_rs1_adr3_c   : natural :=  4; -- source register 1 address bit 3
298
  constant ctrl_rf_rs1_adr4_c   : natural :=  5; -- source register 1 address bit 4
299
  constant ctrl_rf_rs2_adr0_c   : natural :=  6; -- source register 2 address bit 0
300
  constant ctrl_rf_rs2_adr1_c   : natural :=  7; -- source register 2 address bit 1
301
  constant ctrl_rf_rs2_adr2_c   : natural :=  8; -- source register 2 address bit 2
302
  constant ctrl_rf_rs2_adr3_c   : natural :=  9; -- source register 2 address bit 3
303
  constant ctrl_rf_rs2_adr4_c   : natural := 10; -- source register 2 address bit 4
304 58 zero_gravi
  constant ctrl_rf_rd_adr0_c    : natural := 11; -- destination register address bit 0
305
  constant ctrl_rf_rd_adr1_c    : natural := 12; -- destination register address bit 1
306
  constant ctrl_rf_rd_adr2_c    : natural := 13; -- destination register address bit 2
307
  constant ctrl_rf_rd_adr3_c    : natural := 14; -- destination register address bit 3
308
  constant ctrl_rf_rd_adr4_c    : natural := 15; -- destination register address bit 4
309 49 zero_gravi
  constant ctrl_rf_wb_en_c      : natural := 16; -- write back enable
310 2 zero_gravi
  -- alu --
311 62 zero_gravi
  constant ctrl_alu_arith_c     : natural := 17; -- ALU arithmetic command
312
  constant ctrl_alu_logic0_c    : natural := 18; -- ALU logic command bit 0
313
  constant ctrl_alu_logic1_c    : natural := 19; -- ALU logic command bit 1
314
  constant ctrl_alu_func0_c     : natural := 20; -- ALU function select command bit 0
315
  constant ctrl_alu_func1_c     : natural := 21; -- ALU function select command bit 1
316
  constant ctrl_alu_addsub_c    : natural := 22; -- 0=ADD, 1=SUB
317
  constant ctrl_alu_opa_mux_c   : natural := 23; -- operand A select (0=rs1, 1=PC)
318
  constant ctrl_alu_opb_mux_c   : natural := 24; -- operand B select (0=rs2, 1=IMM)
319
  constant ctrl_alu_unsigned_c  : natural := 25; -- is unsigned ALU operation
320
  constant ctrl_alu_shift_dir_c : natural := 26; -- shift direction (0=left, 1=right)
321
  constant ctrl_alu_shift_ar_c  : natural := 27; -- is arithmetic shift
322
  constant ctrl_alu_frm0_c      : natural := 28; -- FPU rounding mode bit 0
323
  constant ctrl_alu_frm1_c      : natural := 29; -- FPU rounding mode bit 1
324
  constant ctrl_alu_frm2_c      : natural := 30; -- FPU rounding mode bit 2
325 2 zero_gravi
  -- bus interface --
326 62 zero_gravi
  constant ctrl_bus_size_lsb_c  : natural := 31; -- transfer size lsb (00=byte, 01=half-word)
327
  constant ctrl_bus_size_msb_c  : natural := 32; -- transfer size msb (10=word, 11=?)
328
  constant ctrl_bus_rd_c        : natural := 33; -- read data request
329
  constant ctrl_bus_wr_c        : natural := 34; -- write data request
330
  constant ctrl_bus_if_c        : natural := 35; -- instruction fetch request
331
  constant ctrl_bus_mo_we_c     : natural := 36; -- memory address and data output register write enable
332
  constant ctrl_bus_mi_we_c     : natural := 37; -- memory data input register write enable
333
  constant ctrl_bus_unsigned_c  : natural := 38; -- is unsigned load
334
  constant ctrl_bus_ierr_ack_c  : natural := 39; -- acknowledge instruction fetch bus exceptions
335
  constant ctrl_bus_derr_ack_c  : natural := 40; -- acknowledge data access bus exceptions
336
  constant ctrl_bus_fence_c     : natural := 41; -- executed fence operation
337
  constant ctrl_bus_fencei_c    : natural := 42; -- executed fencei operation
338
  constant ctrl_bus_lock_c      : natural := 43; -- make atomic/exclusive access lock
339
  constant ctrl_bus_de_lock_c   : natural := 44; -- remove atomic/exclusive access 
340
  constant ctrl_bus_ch_lock_c   : natural := 45; -- evaluate atomic/exclusive lock (SC operation)
341 26 zero_gravi
  -- co-processors --
342 62 zero_gravi
  constant ctrl_cp_id_lsb_c     : natural := 46; -- cp select ID lsb
343
  constant ctrl_cp_id_msb_c     : natural := 47; -- cp select ID msb
344 44 zero_gravi
  -- instruction's control blocks (used by cpu co-processors) --
345 62 zero_gravi
  constant ctrl_ir_funct3_0_c   : natural := 48; -- funct3 bit 0
346
  constant ctrl_ir_funct3_1_c   : natural := 49; -- funct3 bit 1
347
  constant ctrl_ir_funct3_2_c   : natural := 50; -- funct3 bit 2
348
  constant ctrl_ir_funct12_0_c  : natural := 51; -- funct12 bit 0
349
  constant ctrl_ir_funct12_1_c  : natural := 52; -- funct12 bit 1
350
  constant ctrl_ir_funct12_2_c  : natural := 53; -- funct12 bit 2
351
  constant ctrl_ir_funct12_3_c  : natural := 54; -- funct12 bit 3
352
  constant ctrl_ir_funct12_4_c  : natural := 55; -- funct12 bit 4
353
  constant ctrl_ir_funct12_5_c  : natural := 56; -- funct12 bit 5
354
  constant ctrl_ir_funct12_6_c  : natural := 57; -- funct12 bit 6
355
  constant ctrl_ir_funct12_7_c  : natural := 58; -- funct12 bit 7
356
  constant ctrl_ir_funct12_8_c  : natural := 59; -- funct12 bit 8
357
  constant ctrl_ir_funct12_9_c  : natural := 60; -- funct12 bit 9
358
  constant ctrl_ir_funct12_10_c : natural := 61; -- funct12 bit 10
359
  constant ctrl_ir_funct12_11_c : natural := 62; -- funct12 bit 11
360
  constant ctrl_ir_opcode7_0_c  : natural := 63; -- opcode7 bit 0
361
  constant ctrl_ir_opcode7_1_c  : natural := 64; -- opcode7 bit 1
362
  constant ctrl_ir_opcode7_2_c  : natural := 65; -- opcode7 bit 2
363
  constant ctrl_ir_opcode7_3_c  : natural := 66; -- opcode7 bit 3
364
  constant ctrl_ir_opcode7_4_c  : natural := 67; -- opcode7 bit 4
365
  constant ctrl_ir_opcode7_5_c  : natural := 68; -- opcode7 bit 5
366
  constant ctrl_ir_opcode7_6_c  : natural := 69; -- opcode7 bit 6
367 47 zero_gravi
  -- CPU status --
368 62 zero_gravi
  constant ctrl_priv_lvl_lsb_c  : natural := 70; -- privilege level lsb
369
  constant ctrl_priv_lvl_msb_c  : natural := 71; -- privilege level msb
370
  constant ctrl_sleep_c         : natural := 72; -- set when CPU is in sleep mode
371
  constant ctrl_trap_c          : natural := 73; -- set when CPU is entering trap execution
372
  constant ctrl_debug_running_c : natural := 74; -- CPU is in debug mode when set
373 2 zero_gravi
  -- control bus size --
374 62 zero_gravi
  constant ctrl_width_c         : natural := 75; -- control bus size
375 2 zero_gravi
 
376 47 zero_gravi
  -- Comparator Bus -------------------------------------------------------------------------
377 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
378 47 zero_gravi
  constant cmp_equal_c : natural := 0;
379
  constant cmp_less_c  : natural := 1; -- for signed and unsigned comparisons
380 2 zero_gravi
 
381
  -- RISC-V Opcode Layout -------------------------------------------------------------------
382
  -- -------------------------------------------------------------------------------------------
383
  constant instr_opcode_lsb_c  : natural :=  0; -- opcode bit 0
384
  constant instr_opcode_msb_c  : natural :=  6; -- opcode bit 6
385
  constant instr_rd_lsb_c      : natural :=  7; -- destination register address bit 0
386
  constant instr_rd_msb_c      : natural := 11; -- destination register address bit 4
387
  constant instr_funct3_lsb_c  : natural := 12; -- funct3 bit 0
388
  constant instr_funct3_msb_c  : natural := 14; -- funct3 bit 2
389
  constant instr_rs1_lsb_c     : natural := 15; -- source register 1 address bit 0
390
  constant instr_rs1_msb_c     : natural := 19; -- source register 1 address bit 4
391
  constant instr_rs2_lsb_c     : natural := 20; -- source register 2 address bit 0
392
  constant instr_rs2_msb_c     : natural := 24; -- source register 2 address bit 4
393
  constant instr_funct7_lsb_c  : natural := 25; -- funct7 bit 0
394
  constant instr_funct7_msb_c  : natural := 31; -- funct7 bit 6
395
  constant instr_funct12_lsb_c : natural := 20; -- funct12 bit 0
396
  constant instr_funct12_msb_c : natural := 31; -- funct12 bit 11
397
  constant instr_imm12_lsb_c   : natural := 20; -- immediate12 bit 0
398
  constant instr_imm12_msb_c   : natural := 31; -- immediate12 bit 11
399
  constant instr_imm20_lsb_c   : natural := 12; -- immediate20 bit 0
400
  constant instr_imm20_msb_c   : natural := 31; -- immediate20 bit 21
401
  constant instr_csr_id_lsb_c  : natural := 20; -- csr select bit 0
402
  constant instr_csr_id_msb_c  : natural := 31; -- csr select bit 11
403 39 zero_gravi
  constant instr_funct5_lsb_c  : natural := 27; -- funct5 select bit 0
404
  constant instr_funct5_msb_c  : natural := 31; -- funct5 select bit 4
405 2 zero_gravi
 
406
  -- RISC-V Opcodes -------------------------------------------------------------------------
407
  -- -------------------------------------------------------------------------------------------
408
  -- alu --
409
  constant opcode_lui_c    : std_ulogic_vector(6 downto 0) := "0110111"; -- load upper immediate
410
  constant opcode_auipc_c  : std_ulogic_vector(6 downto 0) := "0010111"; -- add upper immediate to PC
411
  constant opcode_alui_c   : std_ulogic_vector(6 downto 0) := "0010011"; -- ALU operation with immediate (operation via funct3 and funct7)
412
  constant opcode_alu_c    : std_ulogic_vector(6 downto 0) := "0110011"; -- ALU operation (operation via funct3 and funct7)
413
  -- control flow --
414
  constant opcode_jal_c    : std_ulogic_vector(6 downto 0) := "1101111"; -- jump and link
415 29 zero_gravi
  constant opcode_jalr_c   : std_ulogic_vector(6 downto 0) := "1100111"; -- jump and link with register
416 2 zero_gravi
  constant opcode_branch_c : std_ulogic_vector(6 downto 0) := "1100011"; -- branch (condition set via funct3)
417
  -- memory access --
418
  constant opcode_load_c   : std_ulogic_vector(6 downto 0) := "0000011"; -- load (data type via funct3)
419
  constant opcode_store_c  : std_ulogic_vector(6 downto 0) := "0100011"; -- store (data type via funct3)
420
  -- system/csr --
421 8 zero_gravi
  constant opcode_fence_c  : std_ulogic_vector(6 downto 0) := "0001111"; -- fence / fence.i
422 2 zero_gravi
  constant opcode_syscsr_c : std_ulogic_vector(6 downto 0) := "1110011"; -- system/csr access (type via funct3)
423 52 zero_gravi
  -- atomic memory access (A) --
424 39 zero_gravi
  constant opcode_atomic_c : std_ulogic_vector(6 downto 0) := "0101111"; -- atomic operations (A extension)
425 53 zero_gravi
  -- floating point operations (Zfinx-only) (F/D/H/Q) --
426
  constant opcode_fop_c    : std_ulogic_vector(6 downto 0) := "1010011"; -- dual/single opearand instruction
427 2 zero_gravi
 
428
  -- RISC-V Funct3 --------------------------------------------------------------------------
429
  -- -------------------------------------------------------------------------------------------
430
  -- control flow --
431
  constant funct3_beq_c    : std_ulogic_vector(2 downto 0) := "000"; -- branch if equal
432
  constant funct3_bne_c    : std_ulogic_vector(2 downto 0) := "001"; -- branch if not equal
433
  constant funct3_blt_c    : std_ulogic_vector(2 downto 0) := "100"; -- branch if less than
434
  constant funct3_bge_c    : std_ulogic_vector(2 downto 0) := "101"; -- branch if greater than or equal
435
  constant funct3_bltu_c   : std_ulogic_vector(2 downto 0) := "110"; -- branch if less than (unsigned)
436
  constant funct3_bgeu_c   : std_ulogic_vector(2 downto 0) := "111"; -- branch if greater than or equal (unsigned)
437
  -- memory access --
438
  constant funct3_lb_c     : std_ulogic_vector(2 downto 0) := "000"; -- load byte
439
  constant funct3_lh_c     : std_ulogic_vector(2 downto 0) := "001"; -- load half word
440
  constant funct3_lw_c     : std_ulogic_vector(2 downto 0) := "010"; -- load word
441
  constant funct3_lbu_c    : std_ulogic_vector(2 downto 0) := "100"; -- load byte (unsigned)
442
  constant funct3_lhu_c    : std_ulogic_vector(2 downto 0) := "101"; -- load half word (unsigned)
443
  constant funct3_sb_c     : std_ulogic_vector(2 downto 0) := "000"; -- store byte
444
  constant funct3_sh_c     : std_ulogic_vector(2 downto 0) := "001"; -- store half word
445
  constant funct3_sw_c     : std_ulogic_vector(2 downto 0) := "010"; -- store word
446
  -- alu --
447
  constant funct3_subadd_c : std_ulogic_vector(2 downto 0) := "000"; -- sub/add via funct7
448
  constant funct3_sll_c    : std_ulogic_vector(2 downto 0) := "001"; -- shift logical left
449
  constant funct3_slt_c    : std_ulogic_vector(2 downto 0) := "010"; -- set on less
450
  constant funct3_sltu_c   : std_ulogic_vector(2 downto 0) := "011"; -- set on less unsigned
451
  constant funct3_xor_c    : std_ulogic_vector(2 downto 0) := "100"; -- xor
452
  constant funct3_sr_c     : std_ulogic_vector(2 downto 0) := "101"; -- shift right via funct7
453
  constant funct3_or_c     : std_ulogic_vector(2 downto 0) := "110"; -- or
454
  constant funct3_and_c    : std_ulogic_vector(2 downto 0) := "111"; -- and
455
  -- system/csr --
456 59 zero_gravi
  constant funct3_env_c    : std_ulogic_vector(2 downto 0) := "000"; -- ecall, ebreak, mret, wfi, ...
457 2 zero_gravi
  constant funct3_csrrw_c  : std_ulogic_vector(2 downto 0) := "001"; -- atomic r/w
458
  constant funct3_csrrs_c  : std_ulogic_vector(2 downto 0) := "010"; -- atomic read & set bit
459
  constant funct3_csrrc_c  : std_ulogic_vector(2 downto 0) := "011"; -- atomic read & clear bit
460
  constant funct3_csrrwi_c : std_ulogic_vector(2 downto 0) := "101"; -- atomic r/w immediate
461
  constant funct3_csrrsi_c : std_ulogic_vector(2 downto 0) := "110"; -- atomic read & set bit immediate
462
  constant funct3_csrrci_c : std_ulogic_vector(2 downto 0) := "111"; -- atomic read & clear bit immediate
463 8 zero_gravi
  -- fence --
464
  constant funct3_fence_c  : std_ulogic_vector(2 downto 0) := "000"; -- fence - order IO/memory access (->NOP)
465
  constant funct3_fencei_c : std_ulogic_vector(2 downto 0) := "001"; -- fencei - instructon stream sync
466 2 zero_gravi
 
467 39 zero_gravi
  -- RISC-V Funct12 -------------------------------------------------------------------------
468 11 zero_gravi
  -- -------------------------------------------------------------------------------------------
469
  -- system --
470
  constant funct12_ecall_c  : std_ulogic_vector(11 downto 0) := x"000"; -- ECALL
471
  constant funct12_ebreak_c : std_ulogic_vector(11 downto 0) := x"001"; -- EBREAK
472
  constant funct12_mret_c   : std_ulogic_vector(11 downto 0) := x"302"; -- MRET
473
  constant funct12_wfi_c    : std_ulogic_vector(11 downto 0) := x"105"; -- WFI
474 59 zero_gravi
  constant funct12_dret_c   : std_ulogic_vector(11 downto 0) := x"7b2"; -- DRET
475 11 zero_gravi
 
476 39 zero_gravi
  -- RISC-V Funct5 --------------------------------------------------------------------------
477
  -- -------------------------------------------------------------------------------------------
478
  -- atomic operations --
479
  constant funct5_a_lr_c : std_ulogic_vector(4 downto 0) := "00010"; -- LR
480
  constant funct5_a_sc_c : std_ulogic_vector(4 downto 0) := "00011"; -- SC
481
 
482 54 zero_gravi
  -- RISC-V Floating-Point Stuff ------------------------------------------------------------
483 52 zero_gravi
  -- -------------------------------------------------------------------------------------------
484 54 zero_gravi
  -- formats --
485
  constant float_single_c : std_ulogic_vector(1 downto 0) := "00"; -- single-precision (32-bit)
486
  constant float_double_c : std_ulogic_vector(1 downto 0) := "01"; -- double-precision (64-bit)
487
  constant float_half_c   : std_ulogic_vector(1 downto 0) := "10"; -- half-precision (16-bit)
488
  constant float_quad_c   : std_ulogic_vector(1 downto 0) := "11"; -- quad-precision (128-bit)
489 52 zero_gravi
 
490 54 zero_gravi
  -- number class flags --
491
  constant fp_class_neg_inf_c    : natural := 0; -- negative infinity
492
  constant fp_class_neg_norm_c   : natural := 1; -- negative normal number
493
  constant fp_class_neg_denorm_c : natural := 2; -- negative subnormal number
494
  constant fp_class_neg_zero_c   : natural := 3; -- negative zero
495
  constant fp_class_pos_zero_c   : natural := 4; -- positive zero
496
  constant fp_class_pos_denorm_c : natural := 5; -- positive subnormal number
497
  constant fp_class_pos_norm_c   : natural := 6; -- positive normal number
498
  constant fp_class_pos_inf_c    : natural := 7; -- positive infinity
499
  constant fp_class_snan_c       : natural := 8; -- signaling NaN (sNaN)
500
  constant fp_class_qnan_c       : natural := 9; -- quiet NaN (qNaN)
501
 
502
  -- exception flags --
503
  constant fp_exc_nv_c : natural := 0; -- invalid operation
504
  constant fp_exc_dz_c : natural := 1; -- divide by zero
505
  constant fp_exc_of_c : natural := 2; -- overflow
506
  constant fp_exc_uf_c : natural := 3; -- underflow
507
  constant fp_exc_nx_c : natural := 4; -- inexact
508
 
509
  -- special values (single-precision) --
510
  constant fp_single_qnan_c     : std_ulogic_vector(31 downto 0) := x"7fc00000"; -- quiet NaN
511
  constant fp_single_snan_c     : std_ulogic_vector(31 downto 0) := x"7fa00000"; -- signaling NaN
512
  constant fp_single_pos_inf_c  : std_ulogic_vector(31 downto 0) := x"7f800000"; -- positive infinity
513
  constant fp_single_neg_inf_c  : std_ulogic_vector(31 downto 0) := x"ff800000"; -- negative infinity
514
  constant fp_single_pos_zero_c : std_ulogic_vector(31 downto 0) := x"00000000"; -- positive zero
515
  constant fp_single_neg_zero_c : std_ulogic_vector(31 downto 0) := x"80000000"; -- negative zero
516
 
517 29 zero_gravi
  -- RISC-V CSR Addresses -------------------------------------------------------------------
518
  -- -------------------------------------------------------------------------------------------
519 56 zero_gravi
  -- <<< standard read/write CSRs >>> --
520
  -- user floating-point CSRs --
521 52 zero_gravi
  constant csr_class_float_c    : std_ulogic_vector(07 downto 0) := x"00"; -- floating point
522
  constant csr_fflags_c         : std_ulogic_vector(11 downto 0) := x"001";
523
  constant csr_frm_c            : std_ulogic_vector(11 downto 0) := x"002";
524
  constant csr_fcsr_c           : std_ulogic_vector(11 downto 0) := x"003";
525 56 zero_gravi
  -- machine trap setup --
526 63 zero_gravi
  constant csr_class_setup_c    : std_ulogic_vector(08 downto 0) := x"30" & '0'; -- trap setup
527 42 zero_gravi
  constant csr_mstatus_c        : std_ulogic_vector(11 downto 0) := x"300";
528
  constant csr_misa_c           : std_ulogic_vector(11 downto 0) := x"301";
529
  constant csr_mie_c            : std_ulogic_vector(11 downto 0) := x"304";
530
  constant csr_mtvec_c          : std_ulogic_vector(11 downto 0) := x"305";
531
  constant csr_mcounteren_c     : std_ulogic_vector(11 downto 0) := x"306";
532 62 zero_gravi
  --
533
  constant csr_mstatush_c       : std_ulogic_vector(11 downto 0) := x"310";
534 64 zero_gravi
  -- machine configuration --
535
  constant csr_class_envcfg_c   : std_ulogic_vector(06 downto 0) := x"3" & "000"; -- configuration
536
  constant csr_menvcfg_c        : std_ulogic_vector(11 downto 0) := x"30a";
537
  constant csr_menvcfgh_c       : std_ulogic_vector(11 downto 0) := x"31a";
538 56 zero_gravi
  -- machine counter setup --
539
  constant csr_cnt_setup_c      : std_ulogic_vector(06 downto 0) := x"3" & "001"; -- counter setup
540 42 zero_gravi
  constant csr_mcountinhibit_c  : std_ulogic_vector(11 downto 0) := x"320";
541
  constant csr_mhpmevent3_c     : std_ulogic_vector(11 downto 0) := x"323";
542
  constant csr_mhpmevent4_c     : std_ulogic_vector(11 downto 0) := x"324";
543
  constant csr_mhpmevent5_c     : std_ulogic_vector(11 downto 0) := x"325";
544
  constant csr_mhpmevent6_c     : std_ulogic_vector(11 downto 0) := x"326";
545
  constant csr_mhpmevent7_c     : std_ulogic_vector(11 downto 0) := x"327";
546
  constant csr_mhpmevent8_c     : std_ulogic_vector(11 downto 0) := x"328";
547
  constant csr_mhpmevent9_c     : std_ulogic_vector(11 downto 0) := x"329";
548
  constant csr_mhpmevent10_c    : std_ulogic_vector(11 downto 0) := x"32a";
549
  constant csr_mhpmevent11_c    : std_ulogic_vector(11 downto 0) := x"32b";
550
  constant csr_mhpmevent12_c    : std_ulogic_vector(11 downto 0) := x"32c";
551
  constant csr_mhpmevent13_c    : std_ulogic_vector(11 downto 0) := x"32d";
552
  constant csr_mhpmevent14_c    : std_ulogic_vector(11 downto 0) := x"32e";
553
  constant csr_mhpmevent15_c    : std_ulogic_vector(11 downto 0) := x"32f";
554
  constant csr_mhpmevent16_c    : std_ulogic_vector(11 downto 0) := x"330";
555
  constant csr_mhpmevent17_c    : std_ulogic_vector(11 downto 0) := x"331";
556
  constant csr_mhpmevent18_c    : std_ulogic_vector(11 downto 0) := x"332";
557
  constant csr_mhpmevent19_c    : std_ulogic_vector(11 downto 0) := x"333";
558
  constant csr_mhpmevent20_c    : std_ulogic_vector(11 downto 0) := x"334";
559
  constant csr_mhpmevent21_c    : std_ulogic_vector(11 downto 0) := x"335";
560
  constant csr_mhpmevent22_c    : std_ulogic_vector(11 downto 0) := x"336";
561
  constant csr_mhpmevent23_c    : std_ulogic_vector(11 downto 0) := x"337";
562
  constant csr_mhpmevent24_c    : std_ulogic_vector(11 downto 0) := x"338";
563
  constant csr_mhpmevent25_c    : std_ulogic_vector(11 downto 0) := x"339";
564
  constant csr_mhpmevent26_c    : std_ulogic_vector(11 downto 0) := x"33a";
565
  constant csr_mhpmevent27_c    : std_ulogic_vector(11 downto 0) := x"33b";
566
  constant csr_mhpmevent28_c    : std_ulogic_vector(11 downto 0) := x"33c";
567
  constant csr_mhpmevent29_c    : std_ulogic_vector(11 downto 0) := x"33d";
568
  constant csr_mhpmevent30_c    : std_ulogic_vector(11 downto 0) := x"33e";
569
  constant csr_mhpmevent31_c    : std_ulogic_vector(11 downto 0) := x"33f";
570 56 zero_gravi
  -- machine trap handling --
571 64 zero_gravi
  constant csr_class_trap_c     : std_ulogic_vector(07 downto 0) := x"34"; -- machine trap handling
572 42 zero_gravi
  constant csr_mscratch_c       : std_ulogic_vector(11 downto 0) := x"340";
573
  constant csr_mepc_c           : std_ulogic_vector(11 downto 0) := x"341";
574
  constant csr_mcause_c         : std_ulogic_vector(11 downto 0) := x"342";
575
  constant csr_mtval_c          : std_ulogic_vector(11 downto 0) := x"343";
576
  constant csr_mip_c            : std_ulogic_vector(11 downto 0) := x"344";
577 56 zero_gravi
  -- physical memory protection - configuration --
578 52 zero_gravi
  constant csr_class_pmpcfg_c   : std_ulogic_vector(07 downto 0) := x"3a"; -- pmp configuration
579 42 zero_gravi
  constant csr_pmpcfg0_c        : std_ulogic_vector(11 downto 0) := x"3a0";
580
  constant csr_pmpcfg1_c        : std_ulogic_vector(11 downto 0) := x"3a1";
581
  constant csr_pmpcfg2_c        : std_ulogic_vector(11 downto 0) := x"3a2";
582
  constant csr_pmpcfg3_c        : std_ulogic_vector(11 downto 0) := x"3a3";
583
  constant csr_pmpcfg4_c        : std_ulogic_vector(11 downto 0) := x"3a4";
584
  constant csr_pmpcfg5_c        : std_ulogic_vector(11 downto 0) := x"3a5";
585
  constant csr_pmpcfg6_c        : std_ulogic_vector(11 downto 0) := x"3a6";
586
  constant csr_pmpcfg7_c        : std_ulogic_vector(11 downto 0) := x"3a7";
587
  constant csr_pmpcfg8_c        : std_ulogic_vector(11 downto 0) := x"3a8";
588
  constant csr_pmpcfg9_c        : std_ulogic_vector(11 downto 0) := x"3a9";
589
  constant csr_pmpcfg10_c       : std_ulogic_vector(11 downto 0) := x"3aa";
590
  constant csr_pmpcfg11_c       : std_ulogic_vector(11 downto 0) := x"3ab";
591
  constant csr_pmpcfg12_c       : std_ulogic_vector(11 downto 0) := x"3ac";
592
  constant csr_pmpcfg13_c       : std_ulogic_vector(11 downto 0) := x"3ad";
593
  constant csr_pmpcfg14_c       : std_ulogic_vector(11 downto 0) := x"3ae";
594
  constant csr_pmpcfg15_c       : std_ulogic_vector(11 downto 0) := x"3af";
595 56 zero_gravi
  -- physical memory protection - address --
596 42 zero_gravi
  constant csr_pmpaddr0_c       : std_ulogic_vector(11 downto 0) := x"3b0";
597
  constant csr_pmpaddr1_c       : std_ulogic_vector(11 downto 0) := x"3b1";
598
  constant csr_pmpaddr2_c       : std_ulogic_vector(11 downto 0) := x"3b2";
599
  constant csr_pmpaddr3_c       : std_ulogic_vector(11 downto 0) := x"3b3";
600
  constant csr_pmpaddr4_c       : std_ulogic_vector(11 downto 0) := x"3b4";
601
  constant csr_pmpaddr5_c       : std_ulogic_vector(11 downto 0) := x"3b5";
602
  constant csr_pmpaddr6_c       : std_ulogic_vector(11 downto 0) := x"3b6";
603
  constant csr_pmpaddr7_c       : std_ulogic_vector(11 downto 0) := x"3b7";
604
  constant csr_pmpaddr8_c       : std_ulogic_vector(11 downto 0) := x"3b8";
605
  constant csr_pmpaddr9_c       : std_ulogic_vector(11 downto 0) := x"3b9";
606
  constant csr_pmpaddr10_c      : std_ulogic_vector(11 downto 0) := x"3ba";
607
  constant csr_pmpaddr11_c      : std_ulogic_vector(11 downto 0) := x"3bb";
608
  constant csr_pmpaddr12_c      : std_ulogic_vector(11 downto 0) := x"3bc";
609
  constant csr_pmpaddr13_c      : std_ulogic_vector(11 downto 0) := x"3bd";
610
  constant csr_pmpaddr14_c      : std_ulogic_vector(11 downto 0) := x"3be";
611
  constant csr_pmpaddr15_c      : std_ulogic_vector(11 downto 0) := x"3bf";
612
  constant csr_pmpaddr16_c      : std_ulogic_vector(11 downto 0) := x"3c0";
613
  constant csr_pmpaddr17_c      : std_ulogic_vector(11 downto 0) := x"3c1";
614
  constant csr_pmpaddr18_c      : std_ulogic_vector(11 downto 0) := x"3c2";
615
  constant csr_pmpaddr19_c      : std_ulogic_vector(11 downto 0) := x"3c3";
616
  constant csr_pmpaddr20_c      : std_ulogic_vector(11 downto 0) := x"3c4";
617
  constant csr_pmpaddr21_c      : std_ulogic_vector(11 downto 0) := x"3c5";
618
  constant csr_pmpaddr22_c      : std_ulogic_vector(11 downto 0) := x"3c6";
619
  constant csr_pmpaddr23_c      : std_ulogic_vector(11 downto 0) := x"3c7";
620
  constant csr_pmpaddr24_c      : std_ulogic_vector(11 downto 0) := x"3c8";
621
  constant csr_pmpaddr25_c      : std_ulogic_vector(11 downto 0) := x"3c9";
622
  constant csr_pmpaddr26_c      : std_ulogic_vector(11 downto 0) := x"3ca";
623
  constant csr_pmpaddr27_c      : std_ulogic_vector(11 downto 0) := x"3cb";
624
  constant csr_pmpaddr28_c      : std_ulogic_vector(11 downto 0) := x"3cc";
625
  constant csr_pmpaddr29_c      : std_ulogic_vector(11 downto 0) := x"3cd";
626
  constant csr_pmpaddr30_c      : std_ulogic_vector(11 downto 0) := x"3ce";
627
  constant csr_pmpaddr31_c      : std_ulogic_vector(11 downto 0) := x"3cf";
628
  constant csr_pmpaddr32_c      : std_ulogic_vector(11 downto 0) := x"3d0";
629
  constant csr_pmpaddr33_c      : std_ulogic_vector(11 downto 0) := x"3d1";
630
  constant csr_pmpaddr34_c      : std_ulogic_vector(11 downto 0) := x"3d2";
631
  constant csr_pmpaddr35_c      : std_ulogic_vector(11 downto 0) := x"3d3";
632
  constant csr_pmpaddr36_c      : std_ulogic_vector(11 downto 0) := x"3d4";
633
  constant csr_pmpaddr37_c      : std_ulogic_vector(11 downto 0) := x"3d5";
634
  constant csr_pmpaddr38_c      : std_ulogic_vector(11 downto 0) := x"3d6";
635
  constant csr_pmpaddr39_c      : std_ulogic_vector(11 downto 0) := x"3d7";
636
  constant csr_pmpaddr40_c      : std_ulogic_vector(11 downto 0) := x"3d8";
637
  constant csr_pmpaddr41_c      : std_ulogic_vector(11 downto 0) := x"3d9";
638
  constant csr_pmpaddr42_c      : std_ulogic_vector(11 downto 0) := x"3da";
639
  constant csr_pmpaddr43_c      : std_ulogic_vector(11 downto 0) := x"3db";
640
  constant csr_pmpaddr44_c      : std_ulogic_vector(11 downto 0) := x"3dc";
641
  constant csr_pmpaddr45_c      : std_ulogic_vector(11 downto 0) := x"3dd";
642
  constant csr_pmpaddr46_c      : std_ulogic_vector(11 downto 0) := x"3de";
643
  constant csr_pmpaddr47_c      : std_ulogic_vector(11 downto 0) := x"3df";
644
  constant csr_pmpaddr48_c      : std_ulogic_vector(11 downto 0) := x"3e0";
645
  constant csr_pmpaddr49_c      : std_ulogic_vector(11 downto 0) := x"3e1";
646
  constant csr_pmpaddr50_c      : std_ulogic_vector(11 downto 0) := x"3e2";
647
  constant csr_pmpaddr51_c      : std_ulogic_vector(11 downto 0) := x"3e3";
648
  constant csr_pmpaddr52_c      : std_ulogic_vector(11 downto 0) := x"3e4";
649
  constant csr_pmpaddr53_c      : std_ulogic_vector(11 downto 0) := x"3e5";
650
  constant csr_pmpaddr54_c      : std_ulogic_vector(11 downto 0) := x"3e6";
651
  constant csr_pmpaddr55_c      : std_ulogic_vector(11 downto 0) := x"3e7";
652
  constant csr_pmpaddr56_c      : std_ulogic_vector(11 downto 0) := x"3e8";
653
  constant csr_pmpaddr57_c      : std_ulogic_vector(11 downto 0) := x"3e9";
654
  constant csr_pmpaddr58_c      : std_ulogic_vector(11 downto 0) := x"3ea";
655
  constant csr_pmpaddr59_c      : std_ulogic_vector(11 downto 0) := x"3eb";
656
  constant csr_pmpaddr60_c      : std_ulogic_vector(11 downto 0) := x"3ec";
657
  constant csr_pmpaddr61_c      : std_ulogic_vector(11 downto 0) := x"3ed";
658
  constant csr_pmpaddr62_c      : std_ulogic_vector(11 downto 0) := x"3ee";
659
  constant csr_pmpaddr63_c      : std_ulogic_vector(11 downto 0) := x"3ef";
660 59 zero_gravi
  -- debug mode registers --
661
  constant csr_class_debug_c    : std_ulogic_vector(09 downto 0) := x"7b" & "00"; -- debug registers
662
  constant csr_dcsr_c           : std_ulogic_vector(11 downto 0) := x"7b0";
663
  constant csr_dpc_c            : std_ulogic_vector(11 downto 0) := x"7b1";
664
  constant csr_dscratch0_c      : std_ulogic_vector(11 downto 0) := x"7b2";
665 56 zero_gravi
  -- machine counters/timers --
666 42 zero_gravi
  constant csr_mcycle_c         : std_ulogic_vector(11 downto 0) := x"b00";
667
  constant csr_minstret_c       : std_ulogic_vector(11 downto 0) := x"b02";
668
  --
669
  constant csr_mhpmcounter3_c   : std_ulogic_vector(11 downto 0) := x"b03";
670
  constant csr_mhpmcounter4_c   : std_ulogic_vector(11 downto 0) := x"b04";
671
  constant csr_mhpmcounter5_c   : std_ulogic_vector(11 downto 0) := x"b05";
672
  constant csr_mhpmcounter6_c   : std_ulogic_vector(11 downto 0) := x"b06";
673
  constant csr_mhpmcounter7_c   : std_ulogic_vector(11 downto 0) := x"b07";
674
  constant csr_mhpmcounter8_c   : std_ulogic_vector(11 downto 0) := x"b08";
675
  constant csr_mhpmcounter9_c   : std_ulogic_vector(11 downto 0) := x"b09";
676
  constant csr_mhpmcounter10_c  : std_ulogic_vector(11 downto 0) := x"b0a";
677
  constant csr_mhpmcounter11_c  : std_ulogic_vector(11 downto 0) := x"b0b";
678
  constant csr_mhpmcounter12_c  : std_ulogic_vector(11 downto 0) := x"b0c";
679
  constant csr_mhpmcounter13_c  : std_ulogic_vector(11 downto 0) := x"b0d";
680
  constant csr_mhpmcounter14_c  : std_ulogic_vector(11 downto 0) := x"b0e";
681
  constant csr_mhpmcounter15_c  : std_ulogic_vector(11 downto 0) := x"b0f";
682
  constant csr_mhpmcounter16_c  : std_ulogic_vector(11 downto 0) := x"b10";
683
  constant csr_mhpmcounter17_c  : std_ulogic_vector(11 downto 0) := x"b11";
684
  constant csr_mhpmcounter18_c  : std_ulogic_vector(11 downto 0) := x"b12";
685
  constant csr_mhpmcounter19_c  : std_ulogic_vector(11 downto 0) := x"b13";
686
  constant csr_mhpmcounter20_c  : std_ulogic_vector(11 downto 0) := x"b14";
687
  constant csr_mhpmcounter21_c  : std_ulogic_vector(11 downto 0) := x"b15";
688
  constant csr_mhpmcounter22_c  : std_ulogic_vector(11 downto 0) := x"b16";
689
  constant csr_mhpmcounter23_c  : std_ulogic_vector(11 downto 0) := x"b17";
690
  constant csr_mhpmcounter24_c  : std_ulogic_vector(11 downto 0) := x"b18";
691
  constant csr_mhpmcounter25_c  : std_ulogic_vector(11 downto 0) := x"b19";
692
  constant csr_mhpmcounter26_c  : std_ulogic_vector(11 downto 0) := x"b1a";
693
  constant csr_mhpmcounter27_c  : std_ulogic_vector(11 downto 0) := x"b1b";
694
  constant csr_mhpmcounter28_c  : std_ulogic_vector(11 downto 0) := x"b1c";
695
  constant csr_mhpmcounter29_c  : std_ulogic_vector(11 downto 0) := x"b1d";
696
  constant csr_mhpmcounter30_c  : std_ulogic_vector(11 downto 0) := x"b1e";
697
  constant csr_mhpmcounter31_c  : std_ulogic_vector(11 downto 0) := x"b1f";
698
  --
699
  constant csr_mcycleh_c        : std_ulogic_vector(11 downto 0) := x"b80";
700
  constant csr_minstreth_c      : std_ulogic_vector(11 downto 0) := x"b82";
701
  --
702
  constant csr_mhpmcounter3h_c  : std_ulogic_vector(11 downto 0) := x"b83";
703
  constant csr_mhpmcounter4h_c  : std_ulogic_vector(11 downto 0) := x"b84";
704
  constant csr_mhpmcounter5h_c  : std_ulogic_vector(11 downto 0) := x"b85";
705
  constant csr_mhpmcounter6h_c  : std_ulogic_vector(11 downto 0) := x"b86";
706
  constant csr_mhpmcounter7h_c  : std_ulogic_vector(11 downto 0) := x"b87";
707
  constant csr_mhpmcounter8h_c  : std_ulogic_vector(11 downto 0) := x"b88";
708
  constant csr_mhpmcounter9h_c  : std_ulogic_vector(11 downto 0) := x"b89";
709
  constant csr_mhpmcounter10h_c : std_ulogic_vector(11 downto 0) := x"b8a";
710
  constant csr_mhpmcounter11h_c : std_ulogic_vector(11 downto 0) := x"b8b";
711
  constant csr_mhpmcounter12h_c : std_ulogic_vector(11 downto 0) := x"b8c";
712
  constant csr_mhpmcounter13h_c : std_ulogic_vector(11 downto 0) := x"b8d";
713
  constant csr_mhpmcounter14h_c : std_ulogic_vector(11 downto 0) := x"b8e";
714
  constant csr_mhpmcounter15h_c : std_ulogic_vector(11 downto 0) := x"b8f";
715
  constant csr_mhpmcounter16h_c : std_ulogic_vector(11 downto 0) := x"b90";
716
  constant csr_mhpmcounter17h_c : std_ulogic_vector(11 downto 0) := x"b91";
717
  constant csr_mhpmcounter18h_c : std_ulogic_vector(11 downto 0) := x"b92";
718
  constant csr_mhpmcounter19h_c : std_ulogic_vector(11 downto 0) := x"b93";
719
  constant csr_mhpmcounter20h_c : std_ulogic_vector(11 downto 0) := x"b94";
720
  constant csr_mhpmcounter21h_c : std_ulogic_vector(11 downto 0) := x"b95";
721
  constant csr_mhpmcounter22h_c : std_ulogic_vector(11 downto 0) := x"b96";
722
  constant csr_mhpmcounter23h_c : std_ulogic_vector(11 downto 0) := x"b97";
723
  constant csr_mhpmcounter24h_c : std_ulogic_vector(11 downto 0) := x"b98";
724
  constant csr_mhpmcounter25h_c : std_ulogic_vector(11 downto 0) := x"b99";
725
  constant csr_mhpmcounter26h_c : std_ulogic_vector(11 downto 0) := x"b9a";
726
  constant csr_mhpmcounter27h_c : std_ulogic_vector(11 downto 0) := x"b9b";
727
  constant csr_mhpmcounter28h_c : std_ulogic_vector(11 downto 0) := x"b9c";
728
  constant csr_mhpmcounter29h_c : std_ulogic_vector(11 downto 0) := x"b9d";
729
  constant csr_mhpmcounter30h_c : std_ulogic_vector(11 downto 0) := x"b9e";
730
  constant csr_mhpmcounter31h_c : std_ulogic_vector(11 downto 0) := x"b9f";
731
 
732 56 zero_gravi
  -- <<< standard read-only CSRs >>> --
733
  -- user counters/timers --
734 42 zero_gravi
  constant csr_cycle_c          : std_ulogic_vector(11 downto 0) := x"c00";
735
  constant csr_time_c           : std_ulogic_vector(11 downto 0) := x"c01";
736
  constant csr_instret_c        : std_ulogic_vector(11 downto 0) := x"c02";
737
  constant csr_cycleh_c         : std_ulogic_vector(11 downto 0) := x"c80";
738
  constant csr_timeh_c          : std_ulogic_vector(11 downto 0) := x"c81";
739
  constant csr_instreth_c       : std_ulogic_vector(11 downto 0) := x"c82";
740 56 zero_gravi
  -- machine information registers --
741 42 zero_gravi
  constant csr_mvendorid_c      : std_ulogic_vector(11 downto 0) := x"f11";
742
  constant csr_marchid_c        : std_ulogic_vector(11 downto 0) := x"f12";
743
  constant csr_mimpid_c         : std_ulogic_vector(11 downto 0) := x"f13";
744
  constant csr_mhartid_c        : std_ulogic_vector(11 downto 0) := x"f14";
745 62 zero_gravi
  constant csr_mconfigptr_c     : std_ulogic_vector(11 downto 0) := x"f15";
746 42 zero_gravi
 
747 44 zero_gravi
  -- Co-Processor IDs -----------------------------------------------------------------------
748 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
749 63 zero_gravi
  constant cp_sel_shifter_c  : std_ulogic_vector(1 downto 0) := "00"; -- shift operations (base ISA)
750
  constant cp_sel_muldiv_c   : std_ulogic_vector(1 downto 0) := "01"; -- multiplication/division operations ('M' extensions)
751
  constant cp_sel_bitmanip_c : std_ulogic_vector(1 downto 0) := "10"; -- bit manipulation ('B' extensions)
752 61 zero_gravi
  constant cp_sel_fpu_c      : std_ulogic_vector(1 downto 0) := "11"; -- floating-point unit ('Zfinx' extension)
753 2 zero_gravi
 
754
  -- ALU Function Codes ---------------------------------------------------------------------
755
  -- -------------------------------------------------------------------------------------------
756 39 zero_gravi
  -- arithmetic core --
757
  constant alu_arith_cmd_addsub_c : std_ulogic := '0'; -- r.arith <= A +/- B
758
  constant alu_arith_cmd_slt_c    : std_ulogic := '1'; -- r.arith <= A < B
759
  -- logic core --
760
  constant alu_logic_cmd_movb_c   : std_ulogic_vector(1 downto 0) := "00"; -- r.logic <= B
761
  constant alu_logic_cmd_xor_c    : std_ulogic_vector(1 downto 0) := "01"; -- r.logic <= A xor B
762
  constant alu_logic_cmd_or_c     : std_ulogic_vector(1 downto 0) := "10"; -- r.logic <= A or B
763
  constant alu_logic_cmd_and_c    : std_ulogic_vector(1 downto 0) := "11"; -- r.logic <= A and B
764
  -- function select (actual alu result) --
765
  constant alu_func_cmd_arith_c   : std_ulogic_vector(1 downto 0) := "00"; -- r <= r.arith
766
  constant alu_func_cmd_logic_c   : std_ulogic_vector(1 downto 0) := "01"; -- r <= r.logic
767 61 zero_gravi
  constant alu_func_cmd_csrr_c    : std_ulogic_vector(1 downto 0) := "10"; -- r <= CSR read
768 60 zero_gravi
  constant alu_func_cmd_copro_c   : std_ulogic_vector(1 downto 0) := "11"; -- r <= CP result (multi-cycle)
769 2 zero_gravi
 
770 12 zero_gravi
  -- Trap ID Codes --------------------------------------------------------------------------
771
  -- -------------------------------------------------------------------------------------------
772 64 zero_gravi
  -- MSB:   1 = async exception (IRQ), 0 = sync exception (e.g. ebreak)
773
  -- MSB-1: 1 = entry to debug mode, 0 = normal trapping
774 48 zero_gravi
  -- RISC-V compliant sync. exceptions --
775 59 zero_gravi
  constant trap_ima_c      : std_ulogic_vector(6 downto 0) := "0" & "0" & "00000"; -- 0.0:  instruction misaligned
776
  constant trap_iba_c      : std_ulogic_vector(6 downto 0) := "0" & "0" & "00001"; -- 0.1:  instruction access fault
777
  constant trap_iil_c      : std_ulogic_vector(6 downto 0) := "0" & "0" & "00010"; -- 0.2:  illegal instruction
778
  constant trap_brk_c      : std_ulogic_vector(6 downto 0) := "0" & "0" & "00011"; -- 0.3:  breakpoint
779
  constant trap_lma_c      : std_ulogic_vector(6 downto 0) := "0" & "0" & "00100"; -- 0.4:  load address misaligned
780
  constant trap_lbe_c      : std_ulogic_vector(6 downto 0) := "0" & "0" & "00101"; -- 0.5:  load access fault
781
  constant trap_sma_c      : std_ulogic_vector(6 downto 0) := "0" & "0" & "00110"; -- 0.6:  store address misaligned
782
  constant trap_sbe_c      : std_ulogic_vector(6 downto 0) := "0" & "0" & "00111"; -- 0.7:  store access fault
783
  constant trap_uenv_c     : std_ulogic_vector(6 downto 0) := "0" & "0" & "01000"; -- 0.8:  environment call from u-mode
784
  constant trap_menv_c     : std_ulogic_vector(6 downto 0) := "0" & "0" & "01011"; -- 0.11: environment call from m-mode
785 48 zero_gravi
  -- RISC-V compliant interrupts (async. exceptions) --
786 59 zero_gravi
  constant trap_msi_c      : std_ulogic_vector(6 downto 0) := "1" & "0" & "00011"; -- 1.3:  machine software interrupt
787
  constant trap_mti_c      : std_ulogic_vector(6 downto 0) := "1" & "0" & "00111"; -- 1.7:  machine timer interrupt
788
  constant trap_mei_c      : std_ulogic_vector(6 downto 0) := "1" & "0" & "01011"; -- 1.11: machine external interrupt
789 48 zero_gravi
  -- NEORV32-specific (custom) interrupts (async. exceptions) --
790 59 zero_gravi
  constant trap_firq0_c    : std_ulogic_vector(6 downto 0) := "1" & "0" & "10000"; -- 1.16: fast interrupt 0
791
  constant trap_firq1_c    : std_ulogic_vector(6 downto 0) := "1" & "0" & "10001"; -- 1.17: fast interrupt 1
792
  constant trap_firq2_c    : std_ulogic_vector(6 downto 0) := "1" & "0" & "10010"; -- 1.18: fast interrupt 2
793
  constant trap_firq3_c    : std_ulogic_vector(6 downto 0) := "1" & "0" & "10011"; -- 1.19: fast interrupt 3
794
  constant trap_firq4_c    : std_ulogic_vector(6 downto 0) := "1" & "0" & "10100"; -- 1.20: fast interrupt 4
795
  constant trap_firq5_c    : std_ulogic_vector(6 downto 0) := "1" & "0" & "10101"; -- 1.21: fast interrupt 5
796
  constant trap_firq6_c    : std_ulogic_vector(6 downto 0) := "1" & "0" & "10110"; -- 1.22: fast interrupt 6
797
  constant trap_firq7_c    : std_ulogic_vector(6 downto 0) := "1" & "0" & "10111"; -- 1.23: fast interrupt 7
798
  constant trap_firq8_c    : std_ulogic_vector(6 downto 0) := "1" & "0" & "11000"; -- 1.24: fast interrupt 8
799
  constant trap_firq9_c    : std_ulogic_vector(6 downto 0) := "1" & "0" & "11001"; -- 1.25: fast interrupt 9
800
  constant trap_firq10_c   : std_ulogic_vector(6 downto 0) := "1" & "0" & "11010"; -- 1.26: fast interrupt 10
801
  constant trap_firq11_c   : std_ulogic_vector(6 downto 0) := "1" & "0" & "11011"; -- 1.27: fast interrupt 11
802
  constant trap_firq12_c   : std_ulogic_vector(6 downto 0) := "1" & "0" & "11100"; -- 1.28: fast interrupt 12
803
  constant trap_firq13_c   : std_ulogic_vector(6 downto 0) := "1" & "0" & "11101"; -- 1.29: fast interrupt 13
804
  constant trap_firq14_c   : std_ulogic_vector(6 downto 0) := "1" & "0" & "11110"; -- 1.30: fast interrupt 14
805
  constant trap_firq15_c   : std_ulogic_vector(6 downto 0) := "1" & "0" & "11111"; -- 1.31: fast interrupt 15
806
  -- entering debug mode - cause --
807
  constant trap_db_break_c : std_ulogic_vector(6 downto 0) := "0" & "1" & "00010"; -- break instruction (sync / EXCEPTION)
808
  constant trap_db_halt_c  : std_ulogic_vector(6 downto 0) := "1" & "1" & "00011"; -- external halt request (async / IRQ)
809
  constant trap_db_step_c  : std_ulogic_vector(6 downto 0) := "1" & "1" & "00100"; -- single-stepping (async / IRQ)
810 12 zero_gravi
 
811 2 zero_gravi
  -- CPU Control Exception System -----------------------------------------------------------
812
  -- -------------------------------------------------------------------------------------------
813
  -- exception source bits --
814 59 zero_gravi
  constant exception_iaccess_c   : natural :=  0; -- instruction access fault
815
  constant exception_iillegal_c  : natural :=  1; -- illegal instruction
816
  constant exception_ialign_c    : natural :=  2; -- instruction address misaligned
817 47 zero_gravi
  constant exception_m_envcall_c : natural :=  3; -- ENV call from m-mode
818
  constant exception_u_envcall_c : natural :=  4; -- ENV call from u-mode
819
  constant exception_break_c     : natural :=  5; -- breakpoint
820
  constant exception_salign_c    : natural :=  6; -- store address misaligned
821
  constant exception_lalign_c    : natural :=  7; -- load address misaligned
822
  constant exception_saccess_c   : natural :=  8; -- store access fault
823
  constant exception_laccess_c   : natural :=  9; -- load access fault
824 59 zero_gravi
  -- for debug mode only --
825
  constant exception_db_break_c  : natural := 10; -- enter debug mode via ebreak instruction ("sync EXCEPTION")
826 14 zero_gravi
  --
827 59 zero_gravi
  constant exception_width_c     : natural := 11; -- length of this list in bits
828 2 zero_gravi
  -- interrupt source bits --
829 64 zero_gravi
  constant interrupt_msw_irq_c   : natural :=  0; -- machine software interrupt
830
  constant interrupt_mtime_irq_c : natural :=  1; -- machine timer interrupt
831
  constant interrupt_mext_irq_c  : natural :=  2; -- machine external interrupt
832
  constant interrupt_firq_0_c    : natural :=  3; -- fast interrupt channel 0
833
  constant interrupt_firq_1_c    : natural :=  4; -- fast interrupt channel 1
834
  constant interrupt_firq_2_c    : natural :=  5; -- fast interrupt channel 2
835
  constant interrupt_firq_3_c    : natural :=  6; -- fast interrupt channel 3
836
  constant interrupt_firq_4_c    : natural :=  7; -- fast interrupt channel 4
837
  constant interrupt_firq_5_c    : natural :=  8; -- fast interrupt channel 5
838
  constant interrupt_firq_6_c    : natural :=  9; -- fast interrupt channel 6
839
  constant interrupt_firq_7_c    : natural := 10; -- fast interrupt channel 7
840
  constant interrupt_firq_8_c    : natural := 11; -- fast interrupt channel 8
841
  constant interrupt_firq_9_c    : natural := 12; -- fast interrupt channel 9
842
  constant interrupt_firq_10_c   : natural := 13; -- fast interrupt channel 10
843
  constant interrupt_firq_11_c   : natural := 14; -- fast interrupt channel 11
844
  constant interrupt_firq_12_c   : natural := 15; -- fast interrupt channel 12
845
  constant interrupt_firq_13_c   : natural := 16; -- fast interrupt channel 13
846
  constant interrupt_firq_14_c   : natural := 17; -- fast interrupt channel 14
847
  constant interrupt_firq_15_c   : natural := 18; -- fast interrupt channel 15
848 59 zero_gravi
  -- for debug mode only --
849 64 zero_gravi
  constant interrupt_db_halt_c   : natural := 19; -- enter debug mode via external halt request ("async IRQ")
850
  constant interrupt_db_step_c   : natural := 20; -- enter debug mode via single-stepping ("async IRQ")
851 14 zero_gravi
  --
852 64 zero_gravi
  constant interrupt_width_c     : natural := 21; -- length of this list in bits
853 2 zero_gravi
 
854 15 zero_gravi
  -- CPU Privilege Modes --------------------------------------------------------------------
855
  -- -------------------------------------------------------------------------------------------
856 29 zero_gravi
  constant priv_mode_m_c : std_ulogic_vector(1 downto 0) := "11"; -- machine mode
857
  constant priv_mode_u_c : std_ulogic_vector(1 downto 0) := "00"; -- user mode
858 15 zero_gravi
 
859 42 zero_gravi
  -- HPM Event System -----------------------------------------------------------------------
860
  -- -------------------------------------------------------------------------------------------
861
  constant hpmcnt_event_cy_c      : natural := 0;  -- Active cycle
862 56 zero_gravi
  constant hpmcnt_event_never_c   : natural := 1;  -- Unused / never (actually, this would be used for TIME)
863 42 zero_gravi
  constant hpmcnt_event_ir_c      : natural := 2;  -- Retired instruction
864
  constant hpmcnt_event_cir_c     : natural := 3;  -- Retired compressed instruction
865
  constant hpmcnt_event_wait_if_c : natural := 4;  -- Instruction fetch memory wait cycle
866
  constant hpmcnt_event_wait_ii_c : natural := 5;  -- Instruction issue wait cycle
867 45 zero_gravi
  constant hpmcnt_event_wait_mc_c : natural := 6;  -- Multi-cycle ALU-operation wait cycle
868
  constant hpmcnt_event_load_c    : natural := 7;  -- Load operation
869
  constant hpmcnt_event_store_c   : natural := 8;  -- Store operation
870
  constant hpmcnt_event_wait_ls_c : natural := 9;  -- Load/store memory wait cycle
871
  constant hpmcnt_event_jump_c    : natural := 10; -- Unconditional jump
872
  constant hpmcnt_event_branch_c  : natural := 11; -- Conditional branch (taken or not taken)
873
  constant hpmcnt_event_tbranch_c : natural := 12; -- Conditional taken branch
874
  constant hpmcnt_event_trap_c    : natural := 13; -- Entered trap
875
  constant hpmcnt_event_illegal_c : natural := 14; -- Illegal instruction exception
876 42 zero_gravi
  --
877 45 zero_gravi
  constant hpmcnt_event_size_c    : natural := 15; -- length of this list
878 42 zero_gravi
 
879 39 zero_gravi
  -- Clock Generator ------------------------------------------------------------------------
880 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
881
  constant clk_div2_c    : natural := 0;
882
  constant clk_div4_c    : natural := 1;
883
  constant clk_div8_c    : natural := 2;
884
  constant clk_div64_c   : natural := 3;
885
  constant clk_div128_c  : natural := 4;
886
  constant clk_div1024_c : natural := 5;
887
  constant clk_div2048_c : natural := 6;
888
  constant clk_div4096_c : natural := 7;
889
 
890
  -- Component: NEORV32 Processor Top Entity ------------------------------------------------
891
  -- -------------------------------------------------------------------------------------------
892
  component neorv32_top
893
    generic (
894
      -- General --
895 62 zero_gravi
      CLOCK_FREQUENCY              : natural;           -- clock frequency of clk_i in Hz
896 49 zero_gravi
      HW_THREAD_ID                 : natural := 0;      -- hardware thread id (32-bit)
897 62 zero_gravi
      INT_BOOTLOADER_EN            : boolean := false;  -- boot configuration: true = boot explicit bootloader; false = boot from int/ext (I)MEM
898 59 zero_gravi
      -- On-Chip Debugger (OCD) --
899
      ON_CHIP_DEBUGGER_EN          : boolean := false;  -- implement on-chip debugger
900 2 zero_gravi
      -- RISC-V CPU Extensions --
901 39 zero_gravi
      CPU_EXTENSION_RISCV_A        : boolean := false;  -- implement atomic extension?
902 18 zero_gravi
      CPU_EXTENSION_RISCV_C        : boolean := false;  -- implement compressed extension?
903 8 zero_gravi
      CPU_EXTENSION_RISCV_E        : boolean := false;  -- implement embedded RF extension?
904 61 zero_gravi
      CPU_EXTENSION_RISCV_M        : boolean := false;  -- implement mul/div extension?
905 18 zero_gravi
      CPU_EXTENSION_RISCV_U        : boolean := false;  -- implement user mode extension?
906 63 zero_gravi
      CPU_EXTENSION_RISCV_Zbb      : boolean := false;  -- implement basic bit-manipulation sub-extension?
907 57 zero_gravi
      CPU_EXTENSION_RISCV_Zfinx    : boolean := false;  -- implement 32-bit floating-point extension (using INT regs!)
908 8 zero_gravi
      CPU_EXTENSION_RISCV_Zicsr    : boolean := true;   -- implement CSR system?
909 39 zero_gravi
      CPU_EXTENSION_RISCV_Zifencei : boolean := false;  -- implement instruction stream sync.?
910 62 zero_gravi
      CPU_EXTENSION_RISCV_Zmmul    : boolean := false;  -- implement multiply-only M sub-extension?
911 19 zero_gravi
      -- Extension Options --
912 34 zero_gravi
      FAST_MUL_EN                  : boolean := false;  -- use DSPs for M extension's multiplier
913
      FAST_SHIFT_EN                : boolean := false;  -- use barrel shifter for shift operations
914 56 zero_gravi
      CPU_CNT_WIDTH                : natural := 64;     -- total width of CPU cycle and instret counters (0..64)
915 62 zero_gravi
      CPU_IPB_ENTRIES              : natural := 2;      -- entries is instruction prefetch buffer, has to be a power of 2
916 15 zero_gravi
      -- Physical Memory Protection (PMP) --
917 42 zero_gravi
      PMP_NUM_REGIONS              : natural := 0;      -- number of regions (0..64)
918
      PMP_MIN_GRANULARITY          : natural := 64*1024; -- minimal region granularity in bytes, has to be a power of 2, min 8 bytes
919
      -- Hardware Performance Monitors (HPM) --
920 47 zero_gravi
      HPM_NUM_CNTS                 : natural := 0;      -- number of implemented HPM counters (0..29)
921 60 zero_gravi
      HPM_CNT_WIDTH                : natural := 40;     -- total size of HPM counters (0..64)
922 61 zero_gravi
      -- Internal Instruction memory (IMEM) --
923 62 zero_gravi
      MEM_INT_IMEM_EN              : boolean := false;  -- implement processor-internal instruction memory
924 8 zero_gravi
      MEM_INT_IMEM_SIZE            : natural := 16*1024; -- size of processor-internal instruction memory in bytes
925 61 zero_gravi
      -- Internal Data memory (DMEM) --
926 62 zero_gravi
      MEM_INT_DMEM_EN              : boolean := false;  -- implement processor-internal data memory
927 8 zero_gravi
      MEM_INT_DMEM_SIZE            : natural := 8*1024; -- size of processor-internal data memory in bytes
928 61 zero_gravi
      -- Internal Cache memory (iCACHE) --
929 44 zero_gravi
      ICACHE_EN                    : boolean := false;  -- implement instruction cache
930 41 zero_gravi
      ICACHE_NUM_BLOCKS            : natural := 4;      -- i-cache: number of blocks (min 1), has to be a power of 2
931
      ICACHE_BLOCK_SIZE            : natural := 64;     -- i-cache: block size in bytes (min 4), has to be a power of 2
932 45 zero_gravi
      ICACHE_ASSOCIATIVITY         : natural := 1;      -- i-cache: associativity / number of sets (1=direct_mapped), has to be a power of 2
933 61 zero_gravi
      -- External memory interface (WISHBONE) --
934 44 zero_gravi
      MEM_EXT_EN                   : boolean := false;  -- implement external memory bus interface?
935 57 zero_gravi
      MEM_EXT_TIMEOUT              : natural := 255;    -- cycles after a pending bus access auto-terminates (0 = disabled)
936 62 zero_gravi
      MEM_EXT_PIPE_MODE            : boolean := false;  -- protocol: false=classic/standard wishbone mode, true=pipelined wishbone mode
937
      MEM_EXT_BIG_ENDIAN           : boolean := false;  -- byte order: true=big-endian, false=little-endian
938
      MEM_EXT_ASYNC_RX             : boolean := false;  -- use register buffer for RX data when false
939 61 zero_gravi
      -- Stream link interface (SLINK) --
940
      SLINK_NUM_TX                 : natural := 0;      -- number of TX links (0..8)
941
      SLINK_NUM_RX                 : natural := 0;      -- number of TX links (0..8)
942
      SLINK_TX_FIFO                : natural := 1;      -- TX fifo depth, has to be a power of two
943
      SLINK_RX_FIFO                : natural := 1;      -- RX fifo depth, has to be a power of two
944
      -- External Interrupts Controller (XIRQ) --
945
      XIRQ_NUM_CH                  : natural := 0;      -- number of external IRQ channels (0..32)
946 62 zero_gravi
      XIRQ_TRIGGER_TYPE            : std_ulogic_vector(31 downto 0) := x"FFFFFFFF"; -- trigger type: 0=level, 1=edge
947
      XIRQ_TRIGGER_POLARITY        : std_ulogic_vector(31 downto 0) := x"FFFFFFFF"; -- trigger polarity: 0=low-level/falling-edge, 1=high-level/rising-edge
948 2 zero_gravi
      -- Processor peripherals --
949 62 zero_gravi
      IO_GPIO_EN                   : boolean := false;  -- implement general purpose input/output port unit (GPIO)?
950
      IO_MTIME_EN                  : boolean := false;  -- implement machine system timer (MTIME)?
951
      IO_UART0_EN                  : boolean := false;  -- implement primary universal asynchronous receiver/transmitter (UART0)?
952
      IO_UART1_EN                  : boolean := false;  -- implement secondary universal asynchronous receiver/transmitter (UART1)?
953
      IO_SPI_EN                    : boolean := false;  -- implement serial peripheral interface (SPI)?
954
      IO_TWI_EN                    : boolean := false;  -- implement two-wire interface (TWI)?
955
      IO_PWM_NUM_CH                : natural := 0;      -- number of PWM channels to implement (0..60); 0 = disabled
956
      IO_WDT_EN                    : boolean := false;  -- implement watch dog timer (WDT)?
957 44 zero_gravi
      IO_TRNG_EN                   : boolean := false;  -- implement true random number generator (TRNG)?
958 47 zero_gravi
      IO_CFS_EN                    : boolean := false;  -- implement custom functions subsystem (CFS)?
959 56 zero_gravi
      IO_CFS_CONFIG                : std_ulogic_vector(31 downto 0) := x"00000000"; -- custom CFS configuration generic
960 52 zero_gravi
      IO_CFS_IN_SIZE               : positive := 32;    -- size of CFS input conduit in bits
961
      IO_CFS_OUT_SIZE              : positive := 32;    -- size of CFS output conduit in bits
962 62 zero_gravi
      IO_NEOLED_EN                 : boolean := false;  -- implement NeoPixel-compatible smart LED interface (NEOLED)?
963
      IO_NEOLED_TX_FIFO            : natural := 1       -- NEOLED TX FIFO depth, 1..32k, has to be a power of two
964 2 zero_gravi
    );
965
    port (
966
      -- Global control --
967 62 zero_gravi
      clk_i          : in  std_ulogic; -- global clock, rising edge
968
      rstn_i         : in  std_ulogic; -- global reset, low-active, async
969 59 zero_gravi
      -- JTAG on-chip debugger interface --
970 62 zero_gravi
      jtag_trst_i    : in  std_ulogic := 'U'; -- low-active TAP reset (optional)
971
      jtag_tck_i     : in  std_ulogic := 'U'; -- serial clock
972
      jtag_tdi_i     : in  std_ulogic := 'U'; -- serial data input
973 61 zero_gravi
      jtag_tdo_o     : out std_ulogic;        -- serial data output
974 62 zero_gravi
      jtag_tms_i     : in  std_ulogic := 'U'; -- mode select
975 49 zero_gravi
      -- Wishbone bus interface (available if MEM_EXT_EN = true) --
976 61 zero_gravi
      wb_tag_o       : out std_ulogic_vector(02 downto 0); -- request tag
977
      wb_adr_o       : out std_ulogic_vector(31 downto 0); -- address
978 62 zero_gravi
      wb_dat_i       : in  std_ulogic_vector(31 downto 0) := (others => 'U'); -- read data
979 61 zero_gravi
      wb_dat_o       : out std_ulogic_vector(31 downto 0); -- write data
980
      wb_we_o        : out std_ulogic; -- read/write
981
      wb_sel_o       : out std_ulogic_vector(03 downto 0); -- byte enable
982
      wb_stb_o       : out std_ulogic; -- strobe
983
      wb_cyc_o       : out std_ulogic; -- valid cycle
984
      wb_lock_o      : out std_ulogic; -- exclusive access request
985 62 zero_gravi
      wb_ack_i       : in  std_ulogic := 'L'; -- transfer acknowledge
986
      wb_err_i       : in  std_ulogic := 'L'; -- transfer error
987 44 zero_gravi
      -- Advanced memory control signals (available if MEM_EXT_EN = true) --
988 61 zero_gravi
      fence_o        : out std_ulogic; -- indicates an executed FENCE operation
989
      fencei_o       : out std_ulogic; -- indicates an executed FENCEI operation
990
      -- TX stream interfaces (available if SLINK_NUM_TX > 0) --
991
      slink_tx_dat_o : out sdata_8x32_t; -- output data
992
      slink_tx_val_o : out std_ulogic_vector(7 downto 0); -- valid output
993 62 zero_gravi
      slink_tx_rdy_i : in  std_ulogic_vector(7 downto 0) := (others => 'L'); -- ready to send
994 61 zero_gravi
      -- RX stream interfaces (available if SLINK_NUM_RX > 0) --
995 62 zero_gravi
      slink_rx_dat_i : in  sdata_8x32_t := (others => (others => 'U')); -- input data
996
      slink_rx_val_i : in  std_ulogic_vector(7 downto 0) := (others => 'L'); -- valid input
997 61 zero_gravi
      slink_rx_rdy_o : out std_ulogic_vector(7 downto 0); -- ready to receive
998 49 zero_gravi
      -- GPIO (available if IO_GPIO_EN = true) --
999 61 zero_gravi
      gpio_o         : out std_ulogic_vector(63 downto 0); -- parallel output
1000 62 zero_gravi
      gpio_i         : in  std_ulogic_vector(63 downto 0) := (others => 'U'); -- parallel input
1001 50 zero_gravi
      -- primary UART0 (available if IO_UART0_EN = true) --
1002 61 zero_gravi
      uart0_txd_o    : out std_ulogic; -- UART0 send data
1003 62 zero_gravi
      uart0_rxd_i    : in  std_ulogic := 'U'; -- UART0 receive data
1004 61 zero_gravi
      uart0_rts_o    : out std_ulogic; -- hw flow control: UART0.RX ready to receive ("RTR"), low-active, optional
1005 62 zero_gravi
      uart0_cts_i    : in  std_ulogic := 'L'; -- hw flow control: UART0.TX allowed to transmit, low-active, optional
1006 50 zero_gravi
      -- secondary UART1 (available if IO_UART1_EN = true) --
1007 61 zero_gravi
      uart1_txd_o    : out std_ulogic; -- UART1 send data
1008 62 zero_gravi
      uart1_rxd_i    : in  std_ulogic := 'U'; -- UART1 receive data
1009 61 zero_gravi
      uart1_rts_o    : out std_ulogic; -- hw flow control: UART1.RX ready to receive ("RTR"), low-active, optional
1010 62 zero_gravi
      uart1_cts_i    : in  std_ulogic := 'L'; -- hw flow control: UART1.TX allowed to transmit, low-active, optional
1011 49 zero_gravi
      -- SPI (available if IO_SPI_EN = true) --
1012 61 zero_gravi
      spi_sck_o      : out std_ulogic; -- SPI serial clock
1013
      spi_sdo_o      : out std_ulogic; -- controller data out, peripheral data in
1014 62 zero_gravi
      spi_sdi_i      : in  std_ulogic := 'U'; -- controller data in, peripheral data out
1015 61 zero_gravi
      spi_csn_o      : out std_ulogic_vector(07 downto 0); -- SPI CS
1016 49 zero_gravi
      -- TWI (available if IO_TWI_EN = true) --
1017 62 zero_gravi
      twi_sda_io     : inout std_logic := 'U'; -- twi serial data line
1018
      twi_scl_io     : inout std_logic := 'U'; -- twi serial clock line
1019 60 zero_gravi
      -- PWM (available if IO_PWM_NUM_CH > 0) --
1020 61 zero_gravi
      pwm_o          : out std_ulogic_vector(IO_PWM_NUM_CH-1 downto 0); -- pwm channels
1021 47 zero_gravi
      -- Custom Functions Subsystem IO --
1022 62 zero_gravi
      cfs_in_i       : in  std_ulogic_vector(IO_CFS_IN_SIZE-1  downto 0) := (others => 'U'); -- custom CFS inputs conduit
1023 61 zero_gravi
      cfs_out_o      : out std_ulogic_vector(IO_CFS_OUT_SIZE-1 downto 0); -- custom CFS outputs conduit
1024 52 zero_gravi
      -- NeoPixel-compatible smart LED interface (available if IO_NEOLED_EN = true) --
1025 61 zero_gravi
      neoled_o       : out std_ulogic; -- async serial data line
1026 59 zero_gravi
      -- System time --
1027 62 zero_gravi
      mtime_i        : in  std_ulogic_vector(63 downto 0) := (others => 'U'); -- current system time from ext. MTIME (if IO_MTIME_EN = false)
1028 61 zero_gravi
      mtime_o        : out std_ulogic_vector(63 downto 0); -- current system time from int. MTIME (if IO_MTIME_EN = true)
1029
      -- External platform interrupts (available if XIRQ_NUM_CH > 0) --
1030 62 zero_gravi
      xirq_i         : in  std_ulogic_vector(XIRQ_NUM_CH-1 downto 0) := (others => 'L'); -- IRQ channels
1031 61 zero_gravi
      -- CPU Interrupts --
1032 62 zero_gravi
      mtime_irq_i    : in  std_ulogic := 'L'; -- machine timer interrupt, available if IO_MTIME_EN = false
1033
      msw_irq_i      : in  std_ulogic := 'L'; -- machine software interrupt
1034
      mext_irq_i     : in  std_ulogic := 'L'  -- machine external interrupt
1035 2 zero_gravi
    );
1036
  end component;
1037
 
1038 4 zero_gravi
  -- Component: CPU Top Entity --------------------------------------------------------------
1039
  -- -------------------------------------------------------------------------------------------
1040
  component neorv32_cpu
1041
    generic (
1042
      -- General --
1043 62 zero_gravi
      HW_THREAD_ID                 : natural; -- hardware thread id (32-bit)
1044
      CPU_BOOT_ADDR                : std_ulogic_vector(31 downto 0); -- cpu boot address
1045
      CPU_DEBUG_ADDR               : std_ulogic_vector(31 downto 0); -- cpu debug mode start address
1046 4 zero_gravi
      -- RISC-V CPU Extensions --
1047 62 zero_gravi
      CPU_EXTENSION_RISCV_A        : boolean; -- implement atomic extension?
1048
      CPU_EXTENSION_RISCV_C        : boolean; -- implement compressed extension?
1049
      CPU_EXTENSION_RISCV_E        : boolean; -- implement embedded RF extension?
1050
      CPU_EXTENSION_RISCV_M        : boolean; -- implement mul/div extension?
1051
      CPU_EXTENSION_RISCV_U        : boolean; -- implement user mode extension?
1052 63 zero_gravi
      CPU_EXTENSION_RISCV_Zbb      : boolean; -- implement basic bit-manipulation sub-extension?
1053 62 zero_gravi
      CPU_EXTENSION_RISCV_Zfinx    : boolean; -- implement 32-bit floating-point extension (using INT reg!)
1054
      CPU_EXTENSION_RISCV_Zicsr    : boolean; -- implement CSR system?
1055
      CPU_EXTENSION_RISCV_Zifencei : boolean; -- implement instruction stream sync.?
1056
      CPU_EXTENSION_RISCV_Zmmul    : boolean; -- implement multiply-only M sub-extension?
1057
      CPU_EXTENSION_RISCV_DEBUG    : boolean; -- implement CPU debug mode?
1058 19 zero_gravi
      -- Extension Options --
1059 62 zero_gravi
      FAST_MUL_EN                  : boolean; -- use DSPs for M extension's multiplier
1060
      FAST_SHIFT_EN                : boolean; -- use barrel shifter for shift operations
1061
      CPU_CNT_WIDTH                : natural; -- total width of CPU cycle and instret counters (0..64)
1062
      CPU_IPB_ENTRIES              : natural; -- entries is instruction prefetch buffer, has to be a power of 2
1063 15 zero_gravi
      -- Physical Memory Protection (PMP) --
1064 62 zero_gravi
      PMP_NUM_REGIONS              : natural; -- number of regions (0..64)
1065
      PMP_MIN_GRANULARITY          : natural; -- minimal region granularity in bytes, has to be a power of 2, min 8 bytes
1066 42 zero_gravi
      -- Hardware Performance Monitors (HPM) --
1067 62 zero_gravi
      HPM_NUM_CNTS                 : natural; -- number of implemented HPM counters (0..29)
1068
      HPM_CNT_WIDTH                : natural  -- total size of HPM counters (0..64)
1069 4 zero_gravi
    );
1070
    port (
1071
      -- global control --
1072 62 zero_gravi
      clk_i          : in  std_ulogic; -- global clock, rising edge
1073
      rstn_i         : in  std_ulogic; -- global reset, low-active, async
1074 47 zero_gravi
      sleep_o        : out std_ulogic; -- cpu is in sleep mode when set
1075 12 zero_gravi
      -- instruction bus interface --
1076
      i_bus_addr_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
1077 62 zero_gravi
      i_bus_rdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
1078 12 zero_gravi
      i_bus_wdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
1079
      i_bus_ben_o    : out std_ulogic_vector(03 downto 0); -- byte enable
1080
      i_bus_we_o     : out std_ulogic; -- write enable
1081
      i_bus_re_o     : out std_ulogic; -- read enable
1082 57 zero_gravi
      i_bus_lock_o   : out std_ulogic; -- exclusive access request
1083 62 zero_gravi
      i_bus_ack_i    : in  std_ulogic; -- bus transfer acknowledge
1084
      i_bus_err_i    : in  std_ulogic; -- bus transfer error
1085 12 zero_gravi
      i_bus_fence_o  : out std_ulogic; -- executed FENCEI operation
1086 35 zero_gravi
      i_bus_priv_o   : out std_ulogic_vector(1 downto 0); -- privilege level
1087 12 zero_gravi
      -- data bus interface --
1088
      d_bus_addr_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
1089 62 zero_gravi
      d_bus_rdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
1090 12 zero_gravi
      d_bus_wdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
1091
      d_bus_ben_o    : out std_ulogic_vector(03 downto 0); -- byte enable
1092
      d_bus_we_o     : out std_ulogic; -- write enable
1093
      d_bus_re_o     : out std_ulogic; -- read enable
1094 57 zero_gravi
      d_bus_lock_o   : out std_ulogic; -- exclusive access request
1095 62 zero_gravi
      d_bus_ack_i    : in  std_ulogic; -- bus transfer acknowledge
1096
      d_bus_err_i    : in  std_ulogic; -- bus transfer error
1097 12 zero_gravi
      d_bus_fence_o  : out std_ulogic; -- executed FENCE operation
1098 35 zero_gravi
      d_bus_priv_o   : out std_ulogic_vector(1 downto 0); -- privilege level
1099 11 zero_gravi
      -- system time input from MTIME --
1100 62 zero_gravi
      time_i         : in  std_ulogic_vector(63 downto 0); -- current system time
1101 14 zero_gravi
      -- interrupts (risc-v compliant) --
1102 62 zero_gravi
      msw_irq_i      : in  std_ulogic; -- machine software interrupt
1103
      mext_irq_i     : in  std_ulogic; -- machine external interrupt
1104
      mtime_irq_i    : in  std_ulogic; -- machine timer interrupt
1105 14 zero_gravi
      -- fast interrupts (custom) --
1106 62 zero_gravi
      firq_i         : in  std_ulogic_vector(15 downto 0);
1107 59 zero_gravi
      -- debug mode (halt) request --
1108 62 zero_gravi
      db_halt_req_i  : in  std_ulogic
1109 4 zero_gravi
    );
1110
  end component;
1111
 
1112 2 zero_gravi
  -- Component: CPU Control -----------------------------------------------------------------
1113
  -- -------------------------------------------------------------------------------------------
1114
  component neorv32_cpu_control
1115
    generic (
1116
      -- General --
1117 62 zero_gravi
      HW_THREAD_ID                 : natural;     -- hardware thread id (32-bit)
1118
      CPU_BOOT_ADDR                : std_ulogic_vector(31 downto 0); -- cpu boot address
1119
      CPU_DEBUG_ADDR               : std_ulogic_vector(31 downto 0); -- cpu debug mode start address
1120 2 zero_gravi
      -- RISC-V CPU Extensions --
1121 62 zero_gravi
      CPU_EXTENSION_RISCV_A        : boolean; -- implement atomic extension?
1122
      CPU_EXTENSION_RISCV_C        : boolean; -- implement compressed extension?
1123
      CPU_EXTENSION_RISCV_E        : boolean; -- implement embedded RF extension?
1124
      CPU_EXTENSION_RISCV_M        : boolean; -- implement mul/div extension?
1125
      CPU_EXTENSION_RISCV_U        : boolean; -- implement user mode extension?
1126 63 zero_gravi
      CPU_EXTENSION_RISCV_Zbb      : boolean; -- implement basic bit-manipulation sub-extension?
1127 62 zero_gravi
      CPU_EXTENSION_RISCV_Zfinx    : boolean; -- implement 32-bit floating-point extension (using INT reg!)
1128
      CPU_EXTENSION_RISCV_Zicsr    : boolean; -- implement CSR system?
1129
      CPU_EXTENSION_RISCV_Zifencei : boolean; -- implement instruction stream sync.?
1130
      CPU_EXTENSION_RISCV_Zmmul    : boolean; -- implement multiply-only M sub-extension?
1131
      CPU_EXTENSION_RISCV_DEBUG    : boolean; -- implement CPU debug mode?
1132 56 zero_gravi
      -- Extension Options --
1133 62 zero_gravi
      CPU_CNT_WIDTH                : natural; -- total width of CPU cycle and instret counters (0..64)
1134
      CPU_IPB_ENTRIES              : natural; -- entries is instruction prefetch buffer, has to be a power of 2
1135 15 zero_gravi
      -- Physical memory protection (PMP) --
1136 62 zero_gravi
      PMP_NUM_REGIONS              : natural; -- number of regions (0..64)
1137
      PMP_MIN_GRANULARITY          : natural; -- minimal region granularity in bytes, has to be a power of 2, min 8 bytes
1138 42 zero_gravi
      -- Hardware Performance Monitors (HPM) --
1139 62 zero_gravi
      HPM_NUM_CNTS                 : natural; -- number of implemented HPM counters (0..29)
1140
      HPM_CNT_WIDTH                : natural  -- total size of HPM counters (0..64)
1141 2 zero_gravi
    );
1142
    port (
1143
      -- global control --
1144
      clk_i         : in  std_ulogic; -- global clock, rising edge
1145
      rstn_i        : in  std_ulogic; -- global reset, low-active, async
1146
      ctrl_o        : out std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
1147
      -- status input --
1148 61 zero_gravi
      alu_idone_i   : in  std_ulogic; -- ALU iterative operation done
1149 12 zero_gravi
      bus_i_wait_i  : in  std_ulogic; -- wait for bus
1150
      bus_d_wait_i  : in  std_ulogic; -- wait for bus
1151 57 zero_gravi
      excl_state_i  : in  std_ulogic; -- atomic/exclusive access lock status
1152 2 zero_gravi
      -- data input --
1153
      instr_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- instruction
1154
      cmp_i         : in  std_ulogic_vector(1 downto 0); -- comparator status
1155 36 zero_gravi
      alu_add_i     : in  std_ulogic_vector(data_width_c-1 downto 0); -- ALU address result
1156 52 zero_gravi
      rs1_i         : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
1157 2 zero_gravi
      -- data output --
1158
      imm_o         : out std_ulogic_vector(data_width_c-1 downto 0); -- immediate
1159 6 zero_gravi
      fetch_pc_o    : out std_ulogic_vector(data_width_c-1 downto 0); -- PC for instruction fetch
1160
      curr_pc_o     : out std_ulogic_vector(data_width_c-1 downto 0); -- current PC (corresponding to current instruction)
1161 2 zero_gravi
      csr_rdata_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- CSR read data
1162 52 zero_gravi
      -- FPU interface --
1163
      fpu_flags_i   : in  std_ulogic_vector(04 downto 0); -- exception flags
1164 59 zero_gravi
      -- debug mode (halt) request --
1165
      db_halt_req_i : in  std_ulogic;
1166 14 zero_gravi
      -- interrupts (risc-v compliant) --
1167
      msw_irq_i     : in  std_ulogic; -- machine software interrupt
1168
      mext_irq_i    : in  std_ulogic; -- machine external interrupt
1169 2 zero_gravi
      mtime_irq_i   : in  std_ulogic; -- machine timer interrupt
1170 14 zero_gravi
      -- fast interrupts (custom) --
1171 48 zero_gravi
      firq_i        : in  std_ulogic_vector(15 downto 0);
1172 11 zero_gravi
      -- system time input from MTIME --
1173
      time_i        : in  std_ulogic_vector(63 downto 0); -- current system time
1174 15 zero_gravi
      -- physical memory protection --
1175
      pmp_addr_o    : out pmp_addr_if_t; -- addresses
1176
      pmp_ctrl_o    : out pmp_ctrl_if_t; -- configs
1177 2 zero_gravi
      -- bus access exceptions --
1178
      mar_i         : in  std_ulogic_vector(data_width_c-1 downto 0); -- memory address register
1179
      ma_instr_i    : in  std_ulogic; -- misaligned instruction address
1180
      ma_load_i     : in  std_ulogic; -- misaligned load data address
1181
      ma_store_i    : in  std_ulogic; -- misaligned store data address
1182
      be_instr_i    : in  std_ulogic; -- bus error on instruction access
1183
      be_load_i     : in  std_ulogic; -- bus error on load data access
1184 12 zero_gravi
      be_store_i    : in  std_ulogic  -- bus error on store data access
1185 2 zero_gravi
    );
1186
  end component;
1187
 
1188
  -- Component: CPU Register File -----------------------------------------------------------
1189
  -- -------------------------------------------------------------------------------------------
1190
  component neorv32_cpu_regfile
1191
    generic (
1192 62 zero_gravi
      CPU_EXTENSION_RISCV_E : boolean -- implement embedded RF extension?
1193 2 zero_gravi
    );
1194
    port (
1195
      -- global control --
1196
      clk_i  : in  std_ulogic; -- global clock, rising edge
1197
      ctrl_i : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
1198
      -- data input --
1199
      mem_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- memory read data
1200
      alu_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
1201
      -- data output --
1202
      rs1_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- operand 1
1203 47 zero_gravi
      rs2_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- operand 2
1204
      cmp_o  : out std_ulogic_vector(1 downto 0) -- comparator status
1205 2 zero_gravi
    );
1206
  end component;
1207
 
1208
  -- Component: CPU ALU ---------------------------------------------------------------------
1209
  -- -------------------------------------------------------------------------------------------
1210
  component neorv32_cpu_alu
1211 11 zero_gravi
    generic (
1212 61 zero_gravi
      -- RISC-V CPU Extensions --
1213 62 zero_gravi
      CPU_EXTENSION_RISCV_M     : boolean; -- implement mul/div extension?
1214 63 zero_gravi
      CPU_EXTENSION_RISCV_Zbb   : boolean; -- implement basic bit-manipulation sub-extension?
1215 62 zero_gravi
      CPU_EXTENSION_RISCV_Zmmul : boolean; -- implement multiply-only M sub-extension?
1216
      CPU_EXTENSION_RISCV_Zfinx : boolean; -- implement 32-bit floating-point extension (using INT reg!)
1217 61 zero_gravi
      -- Extension Options --
1218 62 zero_gravi
      FAST_MUL_EN               : boolean; -- use DSPs for M extension's multiplier
1219
      FAST_SHIFT_EN             : boolean  -- use barrel shifter for shift operations
1220 11 zero_gravi
    );
1221 2 zero_gravi
    port (
1222
      -- global control --
1223
      clk_i       : in  std_ulogic; -- global clock, rising edge
1224
      rstn_i      : in  std_ulogic; -- global reset, low-active, async
1225
      ctrl_i      : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
1226
      -- data input --
1227
      rs1_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
1228
      rs2_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
1229
      pc2_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- delayed PC
1230
      imm_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- immediate
1231 61 zero_gravi
      csr_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- CSR read data
1232
      cmp_i       : in  std_ulogic_vector(1 downto 0); -- comparator status
1233 2 zero_gravi
      -- data output --
1234
      res_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
1235 36 zero_gravi
      add_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- address computation result
1236 61 zero_gravi
      fpu_flags_o : out std_ulogic_vector(4 downto 0); -- FPU exception flags
1237 2 zero_gravi
      -- status --
1238 61 zero_gravi
      idone_o     : out std_ulogic -- iterative processing units done?
1239 2 zero_gravi
    );
1240
  end component;
1241
 
1242 61 zero_gravi
  -- Component: CPU Co-Processor SHIFTER ----------------------------------------------------
1243
  -- -------------------------------------------------------------------------------------------
1244
  component neorv32_cpu_cp_shifter
1245
    generic (
1246 62 zero_gravi
      FAST_SHIFT_EN : boolean -- use barrel shifter for shift operations
1247 61 zero_gravi
    );
1248
    port (
1249
      -- global control --
1250
      clk_i   : in  std_ulogic; -- global clock, rising edge
1251
      rstn_i  : in  std_ulogic; -- global reset, low-active, async
1252
      ctrl_i  : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
1253
      start_i : in  std_ulogic; -- trigger operation
1254
      -- data input --
1255
      rs1_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
1256
      rs2_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
1257
      imm_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- immediate
1258
      -- result and status --
1259
      res_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- operation result
1260
      valid_o : out std_ulogic -- data output valid
1261
    );
1262
  end component;
1263
 
1264 44 zero_gravi
  -- Component: CPU Co-Processor MULDIV ('M' extension) -------------------------------------
1265 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
1266
  component neorv32_cpu_cp_muldiv
1267 19 zero_gravi
    generic (
1268 62 zero_gravi
      FAST_MUL_EN : boolean; -- use DSPs for faster multiplication
1269
      DIVISION_EN : boolean  -- implement divider hardware
1270 19 zero_gravi
    );
1271 2 zero_gravi
    port (
1272
      -- global control --
1273
      clk_i   : in  std_ulogic; -- global clock, rising edge
1274
      rstn_i  : in  std_ulogic; -- global reset, low-active, async
1275
      ctrl_i  : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
1276 36 zero_gravi
      start_i : in  std_ulogic; -- trigger operation
1277 2 zero_gravi
      -- data input --
1278
      rs1_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
1279
      rs2_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
1280
      -- result and status --
1281
      res_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- operation result
1282
      valid_o : out std_ulogic -- data output valid
1283
    );
1284
  end component;
1285
 
1286 63 zero_gravi
  -- Component: CPU Co-Processor Bit-Manipulation Unit ('B' extension) ----------------------
1287
  -- -------------------------------------------------------------------------------------------
1288
  component neorv32_cpu_cp_bitmanip is
1289
    generic (
1290
      FAST_SHIFT_EN : boolean -- use barrel shifter for shift operations
1291
    );
1292
    port (
1293
      -- global control --
1294
      clk_i   : in  std_ulogic; -- global clock, rising edge
1295
      rstn_i  : in  std_ulogic; -- global reset, low-active, async
1296
      ctrl_i  : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
1297
      start_i : in  std_ulogic; -- trigger operation
1298
      -- data input --
1299
      cmp_i   : in  std_ulogic_vector(1 downto 0); -- comparator status
1300
      rs1_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
1301
      rs2_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
1302
      -- result and status --
1303
      res_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- operation result
1304
      valid_o : out std_ulogic -- data output valid
1305
    );
1306
  end component;
1307
 
1308 53 zero_gravi
  -- Component: CPU Co-Processor 32-bit FPU ('Zfinx' extension) -----------------------------
1309 52 zero_gravi
  -- -------------------------------------------------------------------------------------------
1310
  component neorv32_cpu_cp_fpu
1311
    port (
1312
      -- global control --
1313 53 zero_gravi
      clk_i    : in  std_ulogic; -- global clock, rising edge
1314
      rstn_i   : in  std_ulogic; -- global reset, low-active, async
1315
      ctrl_i   : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
1316
      start_i  : in  std_ulogic; -- trigger operation
1317 52 zero_gravi
      -- data input --
1318 56 zero_gravi
      cmp_i    : in  std_ulogic_vector(1 downto 0); -- comparator status
1319 53 zero_gravi
      rs1_i    : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
1320
      rs2_i    : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
1321 52 zero_gravi
      -- result and status --
1322 53 zero_gravi
      res_o    : out std_ulogic_vector(data_width_c-1 downto 0); -- operation result
1323
      fflags_o : out std_ulogic_vector(4 downto 0); -- exception flags
1324
      valid_o  : out std_ulogic -- data output valid
1325 52 zero_gravi
    );
1326
  end component;
1327
 
1328 2 zero_gravi
  -- Component: CPU Bus Interface -----------------------------------------------------------
1329
  -- -------------------------------------------------------------------------------------------
1330
  component neorv32_cpu_bus
1331
    generic (
1332 62 zero_gravi
      CPU_EXTENSION_RISCV_A : boolean; -- implement atomic extension?
1333
      CPU_EXTENSION_RISCV_C : boolean; -- implement compressed extension?
1334 15 zero_gravi
      -- Physical memory protection (PMP) --
1335 62 zero_gravi
      PMP_NUM_REGIONS       : natural; -- number of regions (0..64)
1336
      PMP_MIN_GRANULARITY   : natural  -- minimal region granularity in bytes, has to be a power of 2, min 8 bytes
1337 2 zero_gravi
    );
1338
    port (
1339
      -- global control --
1340 12 zero_gravi
      clk_i          : in  std_ulogic; -- global clock, rising edge
1341 38 zero_gravi
      rstn_i      : in  std_ulogic := '0'; -- global reset, low-active, async
1342 12 zero_gravi
      ctrl_i         : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
1343
      -- cpu instruction fetch interface --
1344
      fetch_pc_i     : in  std_ulogic_vector(data_width_c-1 downto 0); -- PC for instruction fetch
1345
      instr_o        : out std_ulogic_vector(data_width_c-1 downto 0); -- instruction
1346
      i_wait_o       : out std_ulogic; -- wait for fetch to complete
1347
      --
1348
      ma_instr_o     : out std_ulogic; -- misaligned instruction address
1349
      be_instr_o     : out std_ulogic; -- bus error on instruction access
1350
      -- cpu data access interface --
1351
      addr_i         : in  std_ulogic_vector(data_width_c-1 downto 0); -- ALU result -> access address
1352
      wdata_i        : in  std_ulogic_vector(data_width_c-1 downto 0); -- write data
1353
      rdata_o        : out std_ulogic_vector(data_width_c-1 downto 0); -- read data
1354
      mar_o          : out std_ulogic_vector(data_width_c-1 downto 0); -- current memory address register
1355
      d_wait_o       : out std_ulogic; -- wait for access to complete
1356
      --
1357 57 zero_gravi
      excl_state_o   : out std_ulogic; -- atomic/exclusive access status
1358 12 zero_gravi
      ma_load_o      : out std_ulogic; -- misaligned load data address
1359
      ma_store_o     : out std_ulogic; -- misaligned store data address
1360
      be_load_o      : out std_ulogic; -- bus error on load data access
1361
      be_store_o     : out std_ulogic; -- bus error on store data access
1362 15 zero_gravi
      -- physical memory protection --
1363
      pmp_addr_i     : in  pmp_addr_if_t; -- addresses
1364
      pmp_ctrl_i     : in  pmp_ctrl_if_t; -- configs
1365 12 zero_gravi
      -- instruction bus --
1366
      i_bus_addr_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
1367
      i_bus_rdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
1368
      i_bus_wdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
1369
      i_bus_ben_o    : out std_ulogic_vector(03 downto 0); -- byte enable
1370
      i_bus_we_o     : out std_ulogic; -- write enable
1371
      i_bus_re_o     : out std_ulogic; -- read enable
1372 57 zero_gravi
      i_bus_lock_o   : out std_ulogic; -- exclusive access request
1373 12 zero_gravi
      i_bus_ack_i    : in  std_ulogic; -- bus transfer acknowledge
1374
      i_bus_err_i    : in  std_ulogic; -- bus transfer error
1375
      i_bus_fence_o  : out std_ulogic; -- fence operation
1376
      -- data bus --
1377
      d_bus_addr_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
1378
      d_bus_rdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
1379
      d_bus_wdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
1380
      d_bus_ben_o    : out std_ulogic_vector(03 downto 0); -- byte enable
1381
      d_bus_we_o     : out std_ulogic; -- write enable
1382
      d_bus_re_o     : out std_ulogic; -- read enable
1383 57 zero_gravi
      d_bus_lock_o   : out std_ulogic; -- exclusive access request
1384 12 zero_gravi
      d_bus_ack_i    : in  std_ulogic; -- bus transfer acknowledge
1385
      d_bus_err_i    : in  std_ulogic; -- bus transfer error
1386 57 zero_gravi
      d_bus_fence_o  : out std_ulogic  -- fence operation
1387 2 zero_gravi
    );
1388
  end component;
1389
 
1390 57 zero_gravi
  -- Component: Bus Keeper ------------------------------------------------------------------
1391
  -- -------------------------------------------------------------------------------------------
1392
  component neorv32_bus_keeper is
1393
    generic (
1394 59 zero_gravi
       -- External memory interface --
1395 62 zero_gravi
      MEM_EXT_EN        : boolean;  -- implement external memory bus interface?
1396 57 zero_gravi
      -- Internal instruction memory --
1397 62 zero_gravi
      MEM_INT_IMEM_EN   : boolean; -- implement processor-internal instruction memory
1398
      MEM_INT_IMEM_SIZE : natural; -- size of processor-internal instruction memory in bytes
1399 57 zero_gravi
      -- Internal data memory --
1400 62 zero_gravi
      MEM_INT_DMEM_EN   : boolean; -- implement processor-internal data memory
1401
      MEM_INT_DMEM_SIZE : natural  -- size of processor-internal data memory in bytes
1402 57 zero_gravi
    );
1403
    port (
1404
      -- host access --
1405
      clk_i  : in  std_ulogic; -- global clock line
1406
      rstn_i : in  std_ulogic; -- global reset line, low-active
1407
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
1408
      rden_i : in  std_ulogic; -- read enable
1409
      wren_i : in  std_ulogic; -- write enable
1410
      ack_i  : in  std_ulogic; -- transfer acknowledge from bus system
1411
      err_i  : in  std_ulogic; -- transfer error from bus system
1412
      err_o  : out std_ulogic  -- bus error
1413
    );
1414
  end component;
1415
 
1416 45 zero_gravi
  -- Component: CPU Instruction Cache -------------------------------------------------------
1417 41 zero_gravi
  -- -------------------------------------------------------------------------------------------
1418 45 zero_gravi
  component neorv32_icache
1419 41 zero_gravi
    generic (
1420 62 zero_gravi
      ICACHE_NUM_BLOCKS : natural; -- number of blocks (min 1), has to be a power of 2
1421
      ICACHE_BLOCK_SIZE : natural; -- block size in bytes (min 4), has to be a power of 2
1422
      ICACHE_NUM_SETS   : natural  -- associativity / number of sets (1=direct_mapped), has to be a power of 2
1423 41 zero_gravi
    );
1424
    port (
1425
      -- global control --
1426
      clk_i         : in  std_ulogic; -- global clock, rising edge
1427
      rstn_i        : in  std_ulogic; -- global reset, low-active, async
1428
      clear_i       : in  std_ulogic; -- cache clear
1429
      -- host controller interface --
1430
      host_addr_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
1431
      host_rdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
1432
      host_wdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
1433
      host_ben_i    : in  std_ulogic_vector(03 downto 0); -- byte enable
1434
      host_we_i     : in  std_ulogic; -- write enable
1435
      host_re_i     : in  std_ulogic; -- read enable
1436
      host_ack_o    : out std_ulogic; -- bus transfer acknowledge
1437
      host_err_o    : out std_ulogic; -- bus transfer error
1438
      -- peripheral bus interface --
1439
      bus_addr_o    : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
1440
      bus_rdata_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
1441
      bus_wdata_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
1442
      bus_ben_o     : out std_ulogic_vector(03 downto 0); -- byte enable
1443
      bus_we_o      : out std_ulogic; -- write enable
1444
      bus_re_o      : out std_ulogic; -- read enable
1445
      bus_ack_i     : in  std_ulogic; -- bus transfer acknowledge
1446
      bus_err_i     : in  std_ulogic  -- bus transfer error
1447
    );
1448
  end component;
1449
 
1450 12 zero_gravi
  -- Component: CPU Bus Switch --------------------------------------------------------------
1451
  -- -------------------------------------------------------------------------------------------
1452
  component neorv32_busswitch
1453
    generic (
1454 62 zero_gravi
      PORT_CA_READ_ONLY : boolean; -- set if controller port A is read-only
1455
      PORT_CB_READ_ONLY : boolean  -- set if controller port B is read-only
1456 12 zero_gravi
    );
1457
    port (
1458
      -- global control --
1459
      clk_i           : in  std_ulogic; -- global clock, rising edge
1460
      rstn_i          : in  std_ulogic; -- global reset, low-active, async
1461
      -- controller interface a --
1462
      ca_bus_addr_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
1463
      ca_bus_rdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
1464
      ca_bus_wdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
1465
      ca_bus_ben_i    : in  std_ulogic_vector(03 downto 0); -- byte enable
1466
      ca_bus_we_i     : in  std_ulogic; -- write enable
1467
      ca_bus_re_i     : in  std_ulogic; -- read enable
1468 57 zero_gravi
      ca_bus_lock_i   : in  std_ulogic; -- exclusive access request
1469 12 zero_gravi
      ca_bus_ack_o    : out std_ulogic; -- bus transfer acknowledge
1470
      ca_bus_err_o    : out std_ulogic; -- bus transfer error
1471
      -- controller interface b --
1472
      cb_bus_addr_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
1473
      cb_bus_rdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
1474
      cb_bus_wdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
1475
      cb_bus_ben_i    : in  std_ulogic_vector(03 downto 0); -- byte enable
1476
      cb_bus_we_i     : in  std_ulogic; -- write enable
1477
      cb_bus_re_i     : in  std_ulogic; -- read enable
1478 57 zero_gravi
      cb_bus_lock_i   : in  std_ulogic; -- exclusive access request
1479 12 zero_gravi
      cb_bus_ack_o    : out std_ulogic; -- bus transfer acknowledge
1480
      cb_bus_err_o    : out std_ulogic; -- bus transfer error
1481
      -- peripheral bus --
1482 36 zero_gravi
      p_bus_src_o     : out std_ulogic; -- access source: 0 = A, 1 = B
1483 12 zero_gravi
      p_bus_addr_o    : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
1484
      p_bus_rdata_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
1485
      p_bus_wdata_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
1486
      p_bus_ben_o     : out std_ulogic_vector(03 downto 0); -- byte enable
1487
      p_bus_we_o      : out std_ulogic; -- write enable
1488
      p_bus_re_o      : out std_ulogic; -- read enable
1489 57 zero_gravi
      p_bus_lock_o    : out std_ulogic; -- exclusive access request
1490 12 zero_gravi
      p_bus_ack_i     : in  std_ulogic; -- bus transfer acknowledge
1491
      p_bus_err_i     : in  std_ulogic  -- bus transfer error
1492
    );
1493
  end component;
1494
 
1495 2 zero_gravi
  -- Component: CPU Compressed Instructions Decompressor ------------------------------------
1496
  -- -------------------------------------------------------------------------------------------
1497
  component neorv32_cpu_decompressor
1498
    port (
1499
      -- instruction input --
1500
      ci_instr16_i : in  std_ulogic_vector(15 downto 0); -- compressed instruction input
1501
      -- instruction output --
1502
      ci_illegal_o : out std_ulogic; -- is an illegal compressed instruction
1503
      ci_instr32_o : out std_ulogic_vector(31 downto 0)  -- 32-bit decompressed instruction
1504
    );
1505
  end component;
1506
 
1507
  -- Component: Processor-internal instruction memory (IMEM) --------------------------------
1508
  -- -------------------------------------------------------------------------------------------
1509
  component neorv32_imem
1510
    generic (
1511 62 zero_gravi
      IMEM_BASE    : std_ulogic_vector(31 downto 0); -- memory base address
1512
      IMEM_SIZE    : natural; -- processor-internal instruction memory size in bytes
1513
      IMEM_AS_IROM : boolean  -- implement IMEM as pre-initialized read-only memory?
1514 2 zero_gravi
    );
1515
    port (
1516
      clk_i  : in  std_ulogic; -- global clock line
1517
      rden_i : in  std_ulogic; -- read enable
1518
      wren_i : in  std_ulogic; -- write enable
1519
      ben_i  : in  std_ulogic_vector(03 downto 0); -- byte write enable
1520
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
1521
      data_i : in  std_ulogic_vector(31 downto 0); -- data in
1522
      data_o : out std_ulogic_vector(31 downto 0); -- data out
1523
      ack_o  : out std_ulogic -- transfer acknowledge
1524
    );
1525
  end component;
1526
 
1527
  -- Component: Processor-internal data memory (DMEM) ---------------------------------------
1528
  -- -------------------------------------------------------------------------------------------
1529
  component neorv32_dmem
1530
    generic (
1531 62 zero_gravi
      DMEM_BASE : std_ulogic_vector(31 downto 0); -- memory base address
1532
      DMEM_SIZE : natural -- processor-internal instruction memory size in bytes
1533 2 zero_gravi
    );
1534
    port (
1535
      clk_i  : in  std_ulogic; -- global clock line
1536
      rden_i : in  std_ulogic; -- read enable
1537
      wren_i : in  std_ulogic; -- write enable
1538
      ben_i  : in  std_ulogic_vector(03 downto 0); -- byte write enable
1539
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
1540
      data_i : in  std_ulogic_vector(31 downto 0); -- data in
1541
      data_o : out std_ulogic_vector(31 downto 0); -- data out
1542
      ack_o  : out std_ulogic -- transfer acknowledge
1543
    );
1544
  end component;
1545
 
1546
  -- Component: Processor-internal bootloader ROM (BOOTROM) ---------------------------------
1547
  -- -------------------------------------------------------------------------------------------
1548
  component neorv32_boot_rom
1549 23 zero_gravi
    generic (
1550 62 zero_gravi
      BOOTROM_BASE : std_ulogic_vector(31 downto 0) -- boot ROM base address
1551 23 zero_gravi
    );
1552 2 zero_gravi
    port (
1553
      clk_i  : in  std_ulogic; -- global clock line
1554
      rden_i : in  std_ulogic; -- read enable
1555
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
1556
      data_o : out std_ulogic_vector(31 downto 0); -- data out
1557
      ack_o  : out std_ulogic -- transfer acknowledge
1558
    );
1559
  end component;
1560
 
1561
  -- Component: Machine System Timer (mtime) ------------------------------------------------
1562
  -- -------------------------------------------------------------------------------------------
1563
  component neorv32_mtime
1564
    port (
1565
      -- host access --
1566 60 zero_gravi
      clk_i  : in  std_ulogic; -- global clock line
1567
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
1568
      rden_i : in  std_ulogic; -- read enable
1569
      wren_i : in  std_ulogic; -- write enable
1570
      data_i : in  std_ulogic_vector(31 downto 0); -- data in
1571
      data_o : out std_ulogic_vector(31 downto 0); -- data out
1572
      ack_o  : out std_ulogic; -- transfer acknowledge
1573 11 zero_gravi
      -- time output for CPU --
1574 60 zero_gravi
      time_o : out std_ulogic_vector(63 downto 0); -- current system time
1575 2 zero_gravi
      -- interrupt --
1576 60 zero_gravi
      irq_o  : out std_ulogic  -- interrupt request
1577 2 zero_gravi
    );
1578
  end component;
1579
 
1580
  -- Component: General Purpose Input/Output Port (GPIO) ------------------------------------
1581
  -- -------------------------------------------------------------------------------------------
1582
  component neorv32_gpio
1583
    port (
1584
      -- host access --
1585
      clk_i  : in  std_ulogic; -- global clock line
1586
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
1587
      rden_i : in  std_ulogic; -- read enable
1588
      wren_i : in  std_ulogic; -- write enable
1589
      data_i : in  std_ulogic_vector(31 downto 0); -- data in
1590
      data_o : out std_ulogic_vector(31 downto 0); -- data out
1591
      ack_o  : out std_ulogic; -- transfer acknowledge
1592
      -- parallel io --
1593 61 zero_gravi
      gpio_o : out std_ulogic_vector(63 downto 0);
1594
      gpio_i : in  std_ulogic_vector(63 downto 0)
1595 2 zero_gravi
    );
1596
  end component;
1597
 
1598
  -- Component: Watchdog Timer (WDT) --------------------------------------------------------
1599
  -- -------------------------------------------------------------------------------------------
1600
  component neorv32_wdt
1601
    port (
1602
      -- host access --
1603
      clk_i       : in  std_ulogic; -- global clock line
1604
      rstn_i      : in  std_ulogic; -- global reset line, low-active
1605
      rden_i      : in  std_ulogic; -- read enable
1606
      wren_i      : in  std_ulogic; -- write enable
1607
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
1608
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
1609
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
1610
      ack_o       : out std_ulogic; -- transfer acknowledge
1611
      -- clock generator --
1612
      clkgen_en_o : out std_ulogic; -- enable clock generator
1613
      clkgen_i    : in  std_ulogic_vector(07 downto 0);
1614
      -- timeout event --
1615
      irq_o       : out std_ulogic; -- timeout IRQ
1616
      rstn_o      : out std_ulogic  -- timeout reset, low_active, use it as async!
1617
    );
1618
  end component;
1619
 
1620
  -- Component: Universal Asynchronous Receiver and Transmitter (UART) ----------------------
1621
  -- -------------------------------------------------------------------------------------------
1622
  component neorv32_uart
1623 50 zero_gravi
    generic (
1624 62 zero_gravi
      UART_PRIMARY : boolean -- true = primary UART (UART0), false = secondary UART (UART1)
1625 50 zero_gravi
    );
1626 2 zero_gravi
    port (
1627
      -- host access --
1628
      clk_i       : in  std_ulogic; -- global clock line
1629
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
1630
      rden_i      : in  std_ulogic; -- read enable
1631
      wren_i      : in  std_ulogic; -- write enable
1632
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
1633
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
1634
      ack_o       : out std_ulogic; -- transfer acknowledge
1635
      -- clock generator --
1636
      clkgen_en_o : out std_ulogic; -- enable clock generator
1637
      clkgen_i    : in  std_ulogic_vector(07 downto 0);
1638
      -- com lines --
1639
      uart_txd_o  : out std_ulogic;
1640
      uart_rxd_i  : in  std_ulogic;
1641 51 zero_gravi
      -- hardware flow control --
1642
      uart_rts_o  : out std_ulogic; -- UART.RX ready to receive ("RTR"), low-active, optional
1643
      uart_cts_i  : in  std_ulogic; -- UART.TX allowed to transmit, low-active, optional
1644 2 zero_gravi
      -- interrupts --
1645 48 zero_gravi
      irq_rxd_o   : out std_ulogic; -- uart data received interrupt
1646
      irq_txd_o   : out std_ulogic  -- uart transmission done interrupt
1647 2 zero_gravi
    );
1648
  end component;
1649
 
1650
  -- Component: Serial Peripheral Interface (SPI) -------------------------------------------
1651
  -- -------------------------------------------------------------------------------------------
1652
  component neorv32_spi
1653
    port (
1654
      -- host access --
1655
      clk_i       : in  std_ulogic; -- global clock line
1656
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
1657
      rden_i      : in  std_ulogic; -- read enable
1658
      wren_i      : in  std_ulogic; -- write enable
1659
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
1660
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
1661
      ack_o       : out std_ulogic; -- transfer acknowledge
1662
      -- clock generator --
1663
      clkgen_en_o : out std_ulogic; -- enable clock generator
1664
      clkgen_i    : in  std_ulogic_vector(07 downto 0);
1665
      -- com lines --
1666 6 zero_gravi
      spi_sck_o   : out std_ulogic; -- SPI serial clock
1667
      spi_sdo_o   : out std_ulogic; -- controller data out, peripheral data in
1668
      spi_sdi_i   : in  std_ulogic; -- controller data in, peripheral data out
1669 2 zero_gravi
      spi_csn_o   : out std_ulogic_vector(07 downto 0); -- SPI CS
1670
      -- interrupt --
1671 48 zero_gravi
      irq_o       : out std_ulogic -- transmission done interrupt
1672 2 zero_gravi
    );
1673
  end component;
1674
 
1675
  -- Component: Two-Wire Interface (TWI) ----------------------------------------------------
1676
  -- -------------------------------------------------------------------------------------------
1677
  component neorv32_twi
1678
    port (
1679
      -- host access --
1680
      clk_i       : in  std_ulogic; -- global clock line
1681
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
1682
      rden_i      : in  std_ulogic; -- read enable
1683
      wren_i      : in  std_ulogic; -- write enable
1684
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
1685
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
1686
      ack_o       : out std_ulogic; -- transfer acknowledge
1687
      -- clock generator --
1688
      clkgen_en_o : out std_ulogic; -- enable clock generator
1689
      clkgen_i    : in  std_ulogic_vector(07 downto 0);
1690
      -- com lines --
1691
      twi_sda_io  : inout std_logic; -- serial data line
1692
      twi_scl_io  : inout std_logic; -- serial clock line
1693
      -- interrupt --
1694 48 zero_gravi
      irq_o       : out std_ulogic -- transfer done IRQ
1695 2 zero_gravi
    );
1696
  end component;
1697
 
1698
  -- Component: Pulse-Width Modulation Controller (PWM) -------------------------------------
1699
  -- -------------------------------------------------------------------------------------------
1700
  component neorv32_pwm
1701 60 zero_gravi
    generic (
1702 62 zero_gravi
      NUM_CHANNELS : natural -- number of PWM channels (0..60)
1703 60 zero_gravi
    );
1704 2 zero_gravi
    port (
1705
      -- host access --
1706
      clk_i       : in  std_ulogic; -- global clock line
1707
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
1708
      rden_i      : in  std_ulogic; -- read enable
1709
      wren_i      : in  std_ulogic; -- write enable
1710
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
1711
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
1712
      ack_o       : out std_ulogic; -- transfer acknowledge
1713
      -- clock generator --
1714
      clkgen_en_o : out std_ulogic; -- enable clock generator
1715
      clkgen_i    : in  std_ulogic_vector(07 downto 0);
1716
      -- pwm output channels --
1717 60 zero_gravi
      pwm_o       : out std_ulogic_vector(NUM_CHANNELS-1 downto 0)
1718 2 zero_gravi
    );
1719
  end component;
1720
 
1721
  -- Component: True Random Number Generator (TRNG) -----------------------------------------
1722
  -- -------------------------------------------------------------------------------------------
1723
  component neorv32_trng
1724
    port (
1725
      -- host access --
1726
      clk_i  : in  std_ulogic; -- global clock line
1727
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
1728
      rden_i : in  std_ulogic; -- read enable
1729
      wren_i : in  std_ulogic; -- write enable
1730
      data_i : in  std_ulogic_vector(31 downto 0); -- data in
1731
      data_o : out std_ulogic_vector(31 downto 0); -- data out
1732
      ack_o  : out std_ulogic  -- transfer acknowledge
1733
    );
1734
  end component;
1735
 
1736
  -- Component: Wishbone Bus Gateway (WISHBONE) ---------------------------------------------
1737
  -- -------------------------------------------------------------------------------------------
1738
  component neorv32_wishbone
1739
    generic (
1740 23 zero_gravi
      -- Internal instruction memory --
1741 62 zero_gravi
      MEM_INT_IMEM_EN   : boolean; -- implement processor-internal instruction memory
1742
      MEM_INT_IMEM_SIZE : natural; -- size of processor-internal instruction memory in bytes
1743 23 zero_gravi
      -- Internal data memory --
1744 62 zero_gravi
      MEM_INT_DMEM_EN   : boolean; -- implement processor-internal data memory
1745
      MEM_INT_DMEM_SIZE : natural; -- size of processor-internal data memory in bytes
1746
      -- Interface Configuration --
1747
      BUS_TIMEOUT       : natural; -- cycles after an UNACKNOWLEDGED bus access triggers a bus fault exception
1748
      PIPE_MODE         : boolean; -- protocol: false=classic/standard wishbone mode, true=pipelined wishbone mode
1749
      BIG_ENDIAN        : boolean; -- byte order: true=big-endian, false=little-endian
1750
      ASYNC_RX          : boolean  -- use register buffer for RX data when false
1751 2 zero_gravi
    );
1752
    port (
1753
      -- global control --
1754 57 zero_gravi
      clk_i     : in  std_ulogic; -- global clock line
1755
      rstn_i    : in  std_ulogic; -- global reset line, low-active
1756 2 zero_gravi
      -- host access --
1757 57 zero_gravi
      src_i     : in  std_ulogic; -- access type (0: data, 1:instruction)
1758
      addr_i    : in  std_ulogic_vector(31 downto 0); -- address
1759
      rden_i    : in  std_ulogic; -- read enable
1760
      wren_i    : in  std_ulogic; -- write enable
1761
      ben_i     : in  std_ulogic_vector(03 downto 0); -- byte write enable
1762
      data_i    : in  std_ulogic_vector(31 downto 0); -- data in
1763
      data_o    : out std_ulogic_vector(31 downto 0); -- data out
1764
      lock_i    : in  std_ulogic; -- exclusive access request
1765
      ack_o     : out std_ulogic; -- transfer acknowledge
1766
      err_o     : out std_ulogic; -- transfer error
1767
      priv_i    : in  std_ulogic_vector(01 downto 0); -- current CPU privilege level
1768 2 zero_gravi
      -- wishbone interface --
1769 57 zero_gravi
      wb_tag_o  : out std_ulogic_vector(02 downto 0); -- request tag
1770
      wb_adr_o  : out std_ulogic_vector(31 downto 0); -- address
1771
      wb_dat_i  : in  std_ulogic_vector(31 downto 0); -- read data
1772
      wb_dat_o  : out std_ulogic_vector(31 downto 0); -- write data
1773
      wb_we_o   : out std_ulogic; -- read/write
1774
      wb_sel_o  : out std_ulogic_vector(03 downto 0); -- byte enable
1775
      wb_stb_o  : out std_ulogic; -- strobe
1776
      wb_cyc_o  : out std_ulogic; -- valid cycle
1777
      wb_lock_o : out std_ulogic; -- exclusive access request
1778
      wb_ack_i  : in  std_ulogic; -- transfer acknowledge
1779
      wb_err_i  : in  std_ulogic  -- transfer error
1780 2 zero_gravi
    );
1781
  end component;
1782
 
1783 47 zero_gravi
  -- Component: Custom Functions Subsystem (CFS) --------------------------------------------
1784 23 zero_gravi
  -- -------------------------------------------------------------------------------------------
1785 47 zero_gravi
  component neorv32_cfs
1786
    generic (
1787 52 zero_gravi
      CFS_CONFIG   : std_ulogic_vector(31 downto 0); -- custom CFS configuration generic
1788 62 zero_gravi
      CFS_IN_SIZE  : positive; -- size of CFS input conduit in bits
1789
      CFS_OUT_SIZE : positive  -- size of CFS output conduit in bits
1790 23 zero_gravi
    );
1791 34 zero_gravi
    port (
1792
      -- host access --
1793
      clk_i       : in  std_ulogic; -- global clock line
1794
      rstn_i      : in  std_ulogic; -- global reset line, low-active, use as async
1795
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
1796
      rden_i      : in  std_ulogic; -- read enable
1797 47 zero_gravi
      wren_i      : in  std_ulogic; -- word write enable
1798 34 zero_gravi
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
1799
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
1800
      ack_o       : out std_ulogic; -- transfer acknowledge
1801
      -- clock generator --
1802
      clkgen_en_o : out std_ulogic; -- enable clock generator
1803 47 zero_gravi
      clkgen_i    : in  std_ulogic_vector(07 downto 0); -- "clock" inputs
1804
      -- CPU state --
1805
      sleep_i     : in  std_ulogic; -- set if cpu is in sleep mode
1806
      -- interrupt --
1807
      irq_o       : out std_ulogic; -- interrupt request
1808
      -- custom io (conduit) --
1809 62 zero_gravi
      cfs_in_i    : in  std_ulogic_vector(CFS_IN_SIZE-1 downto 0); -- custom inputs
1810
      cfs_out_o   : out std_ulogic_vector(CFS_OUT_SIZE-1 downto 0) -- custom outputs
1811 34 zero_gravi
    );
1812
  end component;
1813
 
1814 61 zero_gravi
  -- Component: Smart LED (WS2811/WS2812) Interface (NEOLED) --------------------------------
1815 49 zero_gravi
  -- -------------------------------------------------------------------------------------------
1816 61 zero_gravi
  component neorv32_neoled
1817 62 zero_gravi
    generic (
1818
      FIFO_DEPTH : natural -- TX FIFO depth (1..32k, power of two)
1819
    );
1820 49 zero_gravi
    port (
1821
      -- host access --
1822
      clk_i       : in  std_ulogic; -- global clock line
1823
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
1824
      rden_i      : in  std_ulogic; -- read enable
1825
      wren_i      : in  std_ulogic; -- write enable
1826
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
1827
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
1828
      ack_o       : out std_ulogic; -- transfer acknowledge
1829
      -- clock generator --
1830
      clkgen_en_o : out std_ulogic; -- enable clock generator
1831
      clkgen_i    : in  std_ulogic_vector(07 downto 0);
1832 61 zero_gravi
      -- interrupt --
1833
      irq_o       : out std_ulogic; -- interrupt request
1834
      -- NEOLED output --
1835
      neoled_o    : out std_ulogic -- serial async data line
1836 49 zero_gravi
    );
1837
  end component;
1838
 
1839 61 zero_gravi
  -- Component: Stream Link Interface (SLINK) -----------------------------------------------
1840 52 zero_gravi
  -- -------------------------------------------------------------------------------------------
1841 61 zero_gravi
  component neorv32_slink
1842
    generic (
1843 62 zero_gravi
      SLINK_NUM_TX  : natural; -- number of TX links (0..8)
1844
      SLINK_NUM_RX  : natural; -- number of TX links (0..8)
1845
      SLINK_TX_FIFO : natural; -- TX fifo depth, has to be a power of two
1846
      SLINK_RX_FIFO : natural  -- RX fifo depth, has to be a power of two
1847 61 zero_gravi
    );
1848 52 zero_gravi
    port (
1849
      -- host access --
1850 61 zero_gravi
      clk_i          : in  std_ulogic; -- global clock line
1851
      addr_i         : in  std_ulogic_vector(31 downto 0); -- address
1852
      rden_i         : in  std_ulogic; -- read enable
1853
      wren_i         : in  std_ulogic; -- write enable
1854
      data_i         : in  std_ulogic_vector(31 downto 0); -- data in
1855
      data_o         : out std_ulogic_vector(31 downto 0); -- data out
1856
      ack_o          : out std_ulogic; -- transfer acknowledge
1857 52 zero_gravi
      -- interrupt --
1858 61 zero_gravi
      irq_tx_o       : out std_ulogic; -- transmission done
1859
      irq_rx_o       : out std_ulogic; -- data received
1860
      -- TX stream interfaces --
1861
      slink_tx_dat_o : out sdata_8x32_t; -- output data
1862
      slink_tx_val_o : out std_ulogic_vector(7 downto 0); -- valid output
1863
      slink_tx_rdy_i : in  std_ulogic_vector(7 downto 0); -- ready to send
1864
      -- RX stream interfaces --
1865
      slink_rx_dat_i : in  sdata_8x32_t; -- input data
1866
      slink_rx_val_i : in  std_ulogic_vector(7 downto 0); -- valid input
1867
      slink_rx_rdy_o : out std_ulogic_vector(7 downto 0)  -- ready to receive
1868 52 zero_gravi
    );
1869
  end component;
1870
 
1871 61 zero_gravi
  -- Component: External Interrupt Controller (XIRQ) ----------------------------------------
1872
  -- -------------------------------------------------------------------------------------------
1873
  component neorv32_xirq
1874
    generic (
1875 62 zero_gravi
      XIRQ_NUM_CH           : natural; -- number of external IRQ channels (0..32)
1876
      XIRQ_TRIGGER_TYPE     : std_ulogic_vector(31 downto 0); -- trigger type: 0=level, 1=edge
1877
      XIRQ_TRIGGER_POLARITY : std_ulogic_vector(31 downto 0)  -- trigger polarity: 0=low-level/falling-edge, 1=high-level/rising-edge
1878 61 zero_gravi
    );
1879
    port (
1880
      -- host access --
1881
      clk_i     : in  std_ulogic; -- global clock line
1882
      addr_i    : in  std_ulogic_vector(31 downto 0); -- address
1883
      rden_i    : in  std_ulogic; -- read enable
1884
      wren_i    : in  std_ulogic; -- write enable
1885
      data_i    : in  std_ulogic_vector(31 downto 0); -- data in
1886
      data_o    : out std_ulogic_vector(31 downto 0); -- data out
1887
      ack_o     : out std_ulogic; -- transfer acknowledge
1888
      -- external interrupt lines --
1889
      xirq_i    : in  std_ulogic_vector(XIRQ_NUM_CH-1 downto 0);
1890
      -- CPU interrupt --
1891
      cpu_irq_o : out std_ulogic
1892
    );
1893
  end component;
1894
 
1895 23 zero_gravi
  -- Component: System Configuration Information Memory (SYSINFO) ---------------------------
1896
  -- -------------------------------------------------------------------------------------------
1897 12 zero_gravi
  component neorv32_sysinfo
1898
    generic (
1899
      -- General --
1900 63 zero_gravi
      CLOCK_FREQUENCY              : natural; -- clock frequency of clk_i in Hz
1901
      INT_BOOTLOADER_EN            : boolean; -- boot configuration: true = boot explicit bootloader; false = boot from int/ext (I)MEM
1902
      -- RISC-V CPU Extensions --
1903
      CPU_EXTENSION_RISCV_Zbb      : boolean; -- implement basic bit-manipulation sub-extension?
1904
      CPU_EXTENSION_RISCV_Zfinx    : boolean; -- implement 32-bit floating-point extension (using INT reg!)
1905
      CPU_EXTENSION_RISCV_Zicsr    : boolean; -- implement CSR system?
1906
      CPU_EXTENSION_RISCV_Zifencei : boolean; -- implement instruction stream sync.?
1907
      CPU_EXTENSION_RISCV_Zmmul    : boolean; -- implement multiply-only M sub-extension?
1908
      CPU_EXTENSION_RISCV_DEBUG    : boolean; -- implement CPU debug mode?
1909
      -- Extension Options --
1910
      FAST_MUL_EN                  : boolean; -- use DSPs for M extension's multiplier
1911
      FAST_SHIFT_EN                : boolean; -- use barrel shifter for shift operations
1912
      CPU_CNT_WIDTH                : natural; -- total width of CPU cycle and instret counters (0..64)
1913
      -- Physical memory protection (PMP) --
1914
      PMP_NUM_REGIONS              : natural; -- number of regions (0..64)
1915
      -- Hardware Performance Monitors (HPM) --
1916
      HPM_NUM_CNTS                 : natural; -- number of implemented HPM counters (0..29)
1917 23 zero_gravi
      -- Internal Instruction memory --
1918 63 zero_gravi
      MEM_INT_IMEM_EN              : boolean; -- implement processor-internal instruction memory
1919
      MEM_INT_IMEM_SIZE            : natural; -- size of processor-internal instruction memory in bytes
1920 23 zero_gravi
      -- Internal Data memory --
1921 63 zero_gravi
      MEM_INT_DMEM_EN              : boolean; -- implement processor-internal data memory
1922
      MEM_INT_DMEM_SIZE            : natural; -- size of processor-internal data memory in bytes
1923 41 zero_gravi
      -- Internal Cache memory --
1924 63 zero_gravi
      ICACHE_EN                    : boolean; -- implement instruction cache
1925
      ICACHE_NUM_BLOCKS            : natural; -- i-cache: number of blocks (min 2), has to be a power of 2
1926
      ICACHE_BLOCK_SIZE            : natural; -- i-cache: block size in bytes (min 4), has to be a power of 2
1927
      ICACHE_ASSOCIATIVITY         : natural; -- i-cache: associativity (min 1), has to be a power 2
1928 23 zero_gravi
      -- External memory interface --
1929 63 zero_gravi
      MEM_EXT_EN                   : boolean; -- implement external memory bus interface?
1930
      MEM_EXT_BIG_ENDIAN           : boolean; -- byte order: true=big-endian, false=little-endian
1931 59 zero_gravi
      -- On-Chip Debugger --
1932 63 zero_gravi
      ON_CHIP_DEBUGGER_EN          : boolean; -- implement OCD?
1933 12 zero_gravi
      -- Processor peripherals --
1934 63 zero_gravi
      IO_GPIO_EN                   : boolean; -- implement general purpose input/output port unit (GPIO)?
1935
      IO_MTIME_EN                  : boolean; -- implement machine system timer (MTIME)?
1936
      IO_UART0_EN                  : boolean; -- implement primary universal asynchronous receiver/transmitter (UART0)?
1937
      IO_UART1_EN                  : boolean; -- implement secondary universal asynchronous receiver/transmitter (UART1)?
1938
      IO_SPI_EN                    : boolean; -- implement serial peripheral interface (SPI)?
1939
      IO_TWI_EN                    : boolean; -- implement two-wire interface (TWI)?
1940
      IO_PWM_NUM_CH                : natural; -- number of PWM channels to implement
1941
      IO_WDT_EN                    : boolean; -- implement watch dog timer (WDT)?
1942
      IO_TRNG_EN                   : boolean; -- implement true random number generator (TRNG)?
1943
      IO_CFS_EN                    : boolean; -- implement custom functions subsystem (CFS)?
1944
      IO_SLINK_EN                  : boolean; -- implement stream link interface?
1945
      IO_NEOLED_EN                 : boolean; -- implement NeoPixel-compatible smart LED interface (NEOLED)?
1946
      IO_XIRQ_NUM_CH               : natural  -- number of external interrupt (XIRQ) channels to implement
1947 12 zero_gravi
    );
1948
    port (
1949
      -- host access --
1950
      clk_i  : in  std_ulogic; -- global clock line
1951
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
1952
      rden_i : in  std_ulogic; -- read enable
1953
      data_o : out std_ulogic_vector(31 downto 0); -- data out
1954
      ack_o  : out std_ulogic  -- transfer acknowledge
1955
    );
1956
  end component;
1957
 
1958 62 zero_gravi
  -- Component: General Purpose FIFO --------------------------------------------------------
1959 61 zero_gravi
  -- -------------------------------------------------------------------------------------------
1960
  component neorv32_fifo
1961
    generic (
1962 62 zero_gravi
      FIFO_DEPTH : natural; -- number of fifo entries; has to be a power of two; min 1
1963
      FIFO_WIDTH : natural; -- size of data elements in fifo
1964
      FIFO_RSYNC : boolean; -- false = async read; true = sync read
1965
      FIFO_SAFE  : boolean  -- true = allow read/write only if entry available
1966 61 zero_gravi
    );
1967
    port (
1968
      -- control --
1969
      clk_i   : in  std_ulogic; -- clock, rising edge
1970
      rstn_i  : in  std_ulogic; -- async reset, low-active
1971
      clear_i : in  std_ulogic; -- sync reset, high-active
1972 62 zero_gravi
      level_o : out std_ulogic_vector(index_size_f(FIFO_DEPTH) downto 0); -- fill level
1973 61 zero_gravi
      -- write port --
1974
      wdata_i : in  std_ulogic_vector(FIFO_WIDTH-1 downto 0); -- write data
1975
      we_i    : in  std_ulogic; -- write enable
1976
      free_o  : out std_ulogic; -- at least one entry is free when set
1977
      -- read port --
1978
      re_i    : in  std_ulogic; -- read enable
1979
      rdata_o : out std_ulogic_vector(FIFO_WIDTH-1 downto 0); -- read data
1980
      avail_o : out std_ulogic  -- data available when set
1981
    );
1982
  end component;
1983
 
1984 59 zero_gravi
  -- Component: On-Chip Debugger - Debug Module (DM) ----------------------------------------
1985
  -- -------------------------------------------------------------------------------------------
1986
  component neorv32_debug_dm
1987
    port (
1988
      -- global control --
1989
      clk_i            : in  std_ulogic; -- global clock line
1990
      rstn_i           : in  std_ulogic; -- global reset line, low-active
1991
      -- debug module interface (DMI) --
1992
      dmi_rstn_i       : in  std_ulogic;
1993
      dmi_req_valid_i  : in  std_ulogic;
1994
      dmi_req_ready_o  : out std_ulogic; -- DMI is allowed to make new requests when set
1995
      dmi_req_addr_i   : in  std_ulogic_vector(06 downto 0);
1996
      dmi_req_op_i     : in  std_ulogic; -- 0=read, 1=write
1997
      dmi_req_data_i   : in  std_ulogic_vector(31 downto 0);
1998
      dmi_resp_valid_o : out std_ulogic; -- response valid when set
1999
      dmi_resp_ready_i : in  std_ulogic; -- ready to receive respond
2000
      dmi_resp_data_o  : out std_ulogic_vector(31 downto 0);
2001
      dmi_resp_err_o   : out std_ulogic; -- 0=ok, 1=error
2002
      -- CPU bus access --
2003
      cpu_addr_i       : in  std_ulogic_vector(31 downto 0); -- address
2004
      cpu_rden_i       : in  std_ulogic; -- read enable
2005
      cpu_wren_i       : in  std_ulogic; -- write enable
2006
      cpu_data_i       : in  std_ulogic_vector(31 downto 0); -- data in
2007
      cpu_data_o       : out std_ulogic_vector(31 downto 0); -- data out
2008
      cpu_ack_o        : out std_ulogic; -- transfer acknowledge
2009
      -- CPU control --
2010
      cpu_ndmrstn_o    : out std_ulogic; -- soc reset
2011
      cpu_halt_req_o   : out std_ulogic  -- request hart to halt (enter debug mode)
2012
    );
2013
  end component;
2014
 
2015
  -- Component: On-Chip Debugger - Debug Transport Module (DTM) -----------------------------
2016
  -- -------------------------------------------------------------------------------------------
2017
  component neorv32_debug_dtm
2018
    generic (
2019 62 zero_gravi
      IDCODE_VERSION : std_ulogic_vector(03 downto 0); -- version
2020
      IDCODE_PARTID  : std_ulogic_vector(15 downto 0); -- part number
2021
      IDCODE_MANID   : std_ulogic_vector(10 downto 0)  -- manufacturer id
2022 59 zero_gravi
    );
2023
    port (
2024
      -- global control --
2025
      clk_i            : in  std_ulogic; -- global clock line
2026
      rstn_i           : in  std_ulogic; -- global reset line, low-active
2027
      -- jtag connection --
2028
      jtag_trst_i      : in  std_ulogic;
2029
      jtag_tck_i       : in  std_ulogic;
2030
      jtag_tdi_i       : in  std_ulogic;
2031
      jtag_tdo_o       : out std_ulogic;
2032
      jtag_tms_i       : in  std_ulogic;
2033
      -- debug module interface (DMI) --
2034
      dmi_rstn_o       : out std_ulogic;
2035
      dmi_req_valid_o  : out std_ulogic;
2036
      dmi_req_ready_i  : in  std_ulogic; -- DMI is allowed to make new requests when set
2037
      dmi_req_addr_o   : out std_ulogic_vector(06 downto 0);
2038
      dmi_req_op_o     : out std_ulogic; -- 0=read, 1=write
2039
      dmi_req_data_o   : out std_ulogic_vector(31 downto 0);
2040
      dmi_resp_valid_i : in  std_ulogic; -- response valid when set
2041
      dmi_resp_ready_o : out std_ulogic; -- ready to receive respond
2042
      dmi_resp_data_i  : in  std_ulogic_vector(31 downto 0);
2043
      dmi_resp_err_i   : in  std_ulogic -- 0=ok, 1=error
2044
    );
2045
  end component;
2046
 
2047 2 zero_gravi
end neorv32_package;
2048
 
2049
package body neorv32_package is
2050
 
2051 41 zero_gravi
  -- Function: Minimal required number of bits to represent input number --------------------
2052 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
2053
  function index_size_f(input : natural) return natural is
2054
  begin
2055
    for i in 0 to natural'high loop
2056
      if (2**i >= input) then
2057
        return i;
2058
      end if;
2059
    end loop; -- i
2060
    return 0;
2061
  end function index_size_f;
2062
 
2063
  -- Function: Conditional select natural ---------------------------------------------------
2064
  -- -------------------------------------------------------------------------------------------
2065
  function cond_sel_natural_f(cond : boolean; val_t : natural; val_f : natural) return natural is
2066
  begin
2067
    if (cond = true) then
2068
      return val_t;
2069
    else
2070
      return val_f;
2071
    end if;
2072
  end function cond_sel_natural_f;
2073
 
2074 56 zero_gravi
  -- Function: Conditional select integer ---------------------------------------------------
2075
  -- -------------------------------------------------------------------------------------------
2076
  function cond_sel_int_f(cond : boolean; val_t : integer; val_f : integer) return integer is
2077
  begin
2078
    if (cond = true) then
2079
      return val_t;
2080
    else
2081
      return val_f;
2082
    end if;
2083
  end function cond_sel_int_f;
2084
 
2085 2 zero_gravi
  -- Function: Conditional select std_ulogic_vector -----------------------------------------
2086
  -- -------------------------------------------------------------------------------------------
2087
  function cond_sel_stdulogicvector_f(cond : boolean; val_t : std_ulogic_vector; val_f : std_ulogic_vector) return std_ulogic_vector is
2088
  begin
2089
    if (cond = true) then
2090
      return val_t;
2091
    else
2092
      return val_f;
2093
    end if;
2094
  end function cond_sel_stdulogicvector_f;
2095
 
2096 56 zero_gravi
  -- Function: Conditional select std_ulogic ------------------------------------------------
2097
  -- -------------------------------------------------------------------------------------------
2098
  function cond_sel_stdulogic_f(cond : boolean; val_t : std_ulogic; val_f : std_ulogic) return std_ulogic is
2099
  begin
2100
    if (cond = true) then
2101
      return val_t;
2102
    else
2103
      return val_f;
2104
    end if;
2105
  end function cond_sel_stdulogic_f;
2106
 
2107 50 zero_gravi
  -- Function: Conditional select string ----------------------------------------------------
2108 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
2109 50 zero_gravi
  function cond_sel_string_f(cond : boolean; val_t : string; val_f : string) return string is
2110
  begin
2111
    if (cond = true) then
2112
      return val_t;
2113
    else
2114
      return val_f;
2115
    end if;
2116
  end function cond_sel_string_f;
2117
 
2118
  -- Function: Convert bool to std_ulogic ---------------------------------------------------
2119
  -- -------------------------------------------------------------------------------------------
2120 2 zero_gravi
  function bool_to_ulogic_f(cond : boolean) return std_ulogic is
2121
  begin
2122
    if (cond = true) then
2123
      return '1';
2124
    else
2125
      return '0';
2126
    end if;
2127
  end function bool_to_ulogic_f;
2128
 
2129 60 zero_gravi
  -- Function: OR-reduce all bits -----------------------------------------------------------
2130 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
2131 60 zero_gravi
  function or_reduce_f(a : std_ulogic_vector) return std_ulogic is
2132 2 zero_gravi
    variable tmp_v : std_ulogic;
2133
  begin
2134 56 zero_gravi
    tmp_v := '0';
2135 15 zero_gravi
    if (a'low < a'high) then -- not null range?
2136 56 zero_gravi
      for i in a'low to a'high loop
2137 15 zero_gravi
        tmp_v := tmp_v or a(i);
2138
      end loop; -- i
2139
    end if;
2140 2 zero_gravi
    return tmp_v;
2141 60 zero_gravi
  end function or_reduce_f;
2142 2 zero_gravi
 
2143 60 zero_gravi
  -- Function: AND-reduce all bits ----------------------------------------------------------
2144 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
2145 60 zero_gravi
  function and_reduce_f(a : std_ulogic_vector) return std_ulogic is
2146 2 zero_gravi
    variable tmp_v : std_ulogic;
2147
  begin
2148 56 zero_gravi
    tmp_v := '1';
2149 15 zero_gravi
    if (a'low < a'high) then -- not null range?
2150 56 zero_gravi
      for i in a'low to a'high loop
2151 15 zero_gravi
        tmp_v := tmp_v and a(i);
2152
      end loop; -- i
2153
    end if;
2154 2 zero_gravi
    return tmp_v;
2155 60 zero_gravi
  end function and_reduce_f;
2156 2 zero_gravi
 
2157 60 zero_gravi
  -- Function: XOR-reduce all bits ----------------------------------------------------------
2158 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
2159 60 zero_gravi
  function xor_reduce_f(a : std_ulogic_vector) return std_ulogic is
2160 2 zero_gravi
    variable tmp_v : std_ulogic;
2161
  begin
2162 56 zero_gravi
    tmp_v := '0';
2163 15 zero_gravi
    if (a'low < a'high) then -- not null range?
2164 56 zero_gravi
      for i in a'low to a'high loop
2165 15 zero_gravi
        tmp_v := tmp_v xor a(i);
2166
      end loop; -- i
2167
    end if;
2168 2 zero_gravi
    return tmp_v;
2169 60 zero_gravi
  end function xor_reduce_f;
2170 2 zero_gravi
 
2171 40 zero_gravi
  -- Function: Convert std_ulogic_vector to hex char ----------------------------------------
2172 6 zero_gravi
  -- -------------------------------------------------------------------------------------------
2173
  function to_hexchar_f(input : std_ulogic_vector(3 downto 0)) return character is
2174
    variable output_v : character;
2175
  begin
2176
    case input is
2177 7 zero_gravi
      when x"0"   => output_v := '0';
2178
      when x"1"   => output_v := '1';
2179
      when x"2"   => output_v := '2';
2180
      when x"3"   => output_v := '3';
2181
      when x"4"   => output_v := '4';
2182
      when x"5"   => output_v := '5';
2183
      when x"6"   => output_v := '6';
2184
      when x"7"   => output_v := '7';
2185
      when x"8"   => output_v := '8';
2186
      when x"9"   => output_v := '9';
2187
      when x"a"   => output_v := 'a';
2188
      when x"b"   => output_v := 'b';
2189
      when x"c"   => output_v := 'c';
2190
      when x"d"   => output_v := 'd';
2191
      when x"e"   => output_v := 'e';
2192
      when x"f"   => output_v := 'f';
2193 6 zero_gravi
      when others => output_v := '?';
2194
    end case;
2195
    return output_v;
2196
  end function to_hexchar_f;
2197
 
2198 40 zero_gravi
  -- Function: Convert hex char to std_ulogic_vector ----------------------------------------
2199
  -- -------------------------------------------------------------------------------------------
2200
  function hexchar_to_stdulogicvector_f(input : character) return std_ulogic_vector is
2201
    variable hex_value_v : std_ulogic_vector(3 downto 0);
2202
  begin
2203
    case input is
2204
      when '0'       => hex_value_v := x"0";
2205
      when '1'       => hex_value_v := x"1";
2206
      when '2'       => hex_value_v := x"2";
2207
      when '3'       => hex_value_v := x"3";
2208
      when '4'       => hex_value_v := x"4";
2209
      when '5'       => hex_value_v := x"5";
2210
      when '6'       => hex_value_v := x"6";
2211
      when '7'       => hex_value_v := x"7";
2212
      when '8'       => hex_value_v := x"8";
2213
      when '9'       => hex_value_v := x"9";
2214
      when 'a' | 'A' => hex_value_v := x"a";
2215
      when 'b' | 'B' => hex_value_v := x"b";
2216
      when 'c' | 'C' => hex_value_v := x"c";
2217
      when 'd' | 'D' => hex_value_v := x"d";
2218
      when 'e' | 'E' => hex_value_v := x"e";
2219
      when 'f' | 'F' => hex_value_v := x"f";
2220
      when others    => hex_value_v := (others => 'X');
2221
    end case;
2222
    return hex_value_v;
2223
  end function hexchar_to_stdulogicvector_f;
2224
 
2225 32 zero_gravi
  -- Function: Bit reversal -----------------------------------------------------------------
2226
  -- -------------------------------------------------------------------------------------------
2227
  function bit_rev_f(input : std_ulogic_vector) return std_ulogic_vector is
2228
    variable output_v : std_ulogic_vector(input'range);
2229
  begin
2230
    for i in 0 to input'length-1 loop
2231
      output_v(input'length-i-1) := input(i);
2232
    end loop; -- i
2233
    return output_v;
2234
  end function bit_rev_f;
2235
 
2236 36 zero_gravi
  -- Function: Test if input number is a power of two ---------------------------------------
2237
  -- -------------------------------------------------------------------------------------------
2238
  function is_power_of_two_f(input : natural) return boolean is
2239
  begin
2240 38 zero_gravi
    if (input = 1) then -- 2^0
2241 36 zero_gravi
      return true;
2242 38 zero_gravi
    elsif ((input / 2) /= 0) and ((input mod 2) = 0) then
2243
      return true;
2244 36 zero_gravi
    else
2245
      return false;
2246
    end if;
2247
  end function is_power_of_two_f;
2248
 
2249 40 zero_gravi
  -- Function: Swap all bytes of a 32-bit word (endianness conversion) ----------------------
2250
  -- -------------------------------------------------------------------------------------------
2251
  function bswap32_f(input : std_ulogic_vector) return std_ulogic_vector is
2252
    variable output_v : std_ulogic_vector(input'range);
2253
  begin
2254
    output_v(07 downto 00) := input(31 downto 24);
2255
    output_v(15 downto 08) := input(23 downto 16);
2256
    output_v(23 downto 16) := input(15 downto 08);
2257
    output_v(31 downto 24) := input(07 downto 00);
2258
    return output_v;
2259
  end function bswap32_f;
2260
 
2261 61 zero_gravi
  -- Function: Convert char to lowercase ----------------------------------------------------
2262
  -- -------------------------------------------------------------------------------------------
2263 62 zero_gravi
  function char_to_lower_f(ch : character) return character is
2264 61 zero_gravi
    variable res: character;
2265
   begin
2266
     case ch is
2267
       when 'A'    => res := 'a';
2268
       when 'B'    => res := 'b';
2269
       when 'C'    => res := 'c';
2270
       when 'D'    => res := 'd';
2271
       when 'E'    => res := 'e';
2272
       when 'F'    => res := 'f';
2273
       when 'G'    => res := 'g';
2274
       when 'H'    => res := 'h';
2275
       when 'I'    => res := 'i';
2276
       when 'J'    => res := 'j';
2277
       when 'K'    => res := 'k';
2278
       when 'L'    => res := 'l';
2279
       when 'M'    => res := 'm';
2280
       when 'N'    => res := 'n';
2281
       when 'O'    => res := 'o';
2282
       when 'P'    => res := 'p';
2283
       when 'Q'    => res := 'q';
2284
       when 'R'    => res := 'r';
2285
       when 'S'    => res := 's';
2286
       when 'T'    => res := 't';
2287
       when 'U'    => res := 'u';
2288
       when 'V'    => res := 'v';
2289
       when 'W'    => res := 'w';
2290
       when 'X'    => res := 'x';
2291
       when 'Y'    => res := 'y';
2292
       when 'Z'    => res := 'z';
2293
       when others => res := ch;
2294
      end case;
2295
    return res;
2296 62 zero_gravi
  end function char_to_lower_f;
2297 61 zero_gravi
 
2298
  -- Function: Compare strings (convert to lower case, check lengths) -----------------------
2299
  -- -------------------------------------------------------------------------------------------
2300
  function str_equal_f(str0 : string; str1 : string) return boolean is
2301
    variable tmp0_v : string(str0'range);
2302
    variable tmp1_v : string(str1'range);
2303
  begin
2304
    if (str0'length /= str1'length) then -- equal length?
2305
      return false;
2306
    else
2307
      -- convert to lower case --
2308
      for i in str0'range loop
2309 62 zero_gravi
        tmp0_v(i) := char_to_lower_f(str0(i));
2310 61 zero_gravi
      end loop;
2311
      for i in str1'range loop
2312 62 zero_gravi
        tmp1_v(i) := char_to_lower_f(str1(i));
2313 61 zero_gravi
      end loop;
2314
      -- compare lowercase strings --
2315
      if (tmp0_v = tmp1_v) then
2316
        return true;
2317
      else
2318
        return false;
2319
      end if;
2320
    end if;
2321
  end function str_equal_f;
2322
 
2323 63 zero_gravi
  -- Function: Population count (number of set bits) ----------------------------------------
2324
  -- -------------------------------------------------------------------------------------------
2325
  function popcount_f(input : std_ulogic_vector) return natural is
2326
    variable cnt_v : natural range 0 to input'length;
2327
  begin
2328
    cnt_v := 0;
2329
    for i in input'length-1 downto 0 loop
2330
      if (input(i) = '1') then
2331
        cnt_v := cnt_v + 1;
2332
      end if;
2333
    end loop; -- i
2334
    return cnt_v;
2335
  end function popcount_f;
2336
 
2337
  -- Function: Count leading zeros ----------------------------------------------------------
2338
  -- -------------------------------------------------------------------------------------------
2339
  function leading_zeros_f(input : std_ulogic_vector) return natural is
2340
    variable cnt_v : natural range 0 to input'length;
2341
  begin
2342
    cnt_v := 0;
2343
    for i in input'length-1 downto 0 loop
2344
      if (input(i) = '0') then
2345
        cnt_v := cnt_v + 1;
2346
      else
2347
        exit;
2348
      end if;
2349
    end loop; -- i
2350
    return cnt_v;
2351
  end function leading_zeros_f;
2352
 
2353 61 zero_gravi
  -- Function: Initialize mem32_t array from another mem32_t array --------------------------
2354
  -- -------------------------------------------------------------------------------------------
2355
  -- impure function: returns NOT the same result every time it is evaluated with the same arguments since the source file might have changed
2356
  impure function mem32_init_f(init : mem32_t; depth : natural) return mem32_t is
2357
    variable mem_v : mem32_t(0 to depth-1);
2358
  begin
2359 62 zero_gravi
    mem_v := (others => (others => '0')); -- make sure remaining memory entries are set to zero
2360
    if (init'length > depth) then
2361
      return mem_v;
2362
    end if;
2363
    for idx_v in 0 to init'length-1 loop -- init only in range of source data array
2364
      mem_v(idx_v) := init(idx_v);
2365
    end loop; -- idx_v
2366 61 zero_gravi
    return mem_v;
2367
  end function mem32_init_f;
2368
 
2369 62 zero_gravi
 
2370 2 zero_gravi
end neorv32_package;

powered by: WebSVN 2.1.0

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