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

Subversion Repositories neorv32

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

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