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

Subversion Repositories neorv32

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

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
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
7
-- #                                                                                               #
8
-- # Redistribution and use in source and binary forms, with or without modification, are          #
9
-- # permitted provided that the following conditions are met:                                     #
10
-- #                                                                                               #
11
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
12
-- #    conditions and the following disclaimer.                                                   #
13
-- #                                                                                               #
14
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
15
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
16
-- #    provided with the distribution.                                                            #
17
-- #                                                                                               #
18
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
19
-- #    endorse or promote products derived from this software without specific prior written      #
20
-- #    permission.                                                                                #
21
-- #                                                                                               #
22
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
23
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
24
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
25
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
26
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
27
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
28
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
29
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
30
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
31
-- # ********************************************************************************************* #
32
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
33
-- #################################################################################################
34
 
35
library ieee;
36
use ieee.std_logic_1164.all;
37
use ieee.numeric_std.all;
38
 
39
package neorv32_package is
40
 
41
  -- Architecture Constants -----------------------------------------------------------------
42
  -- -------------------------------------------------------------------------------------------
43 20 zero_gravi
  constant data_width_c  : natural := 32; -- data width - FIXED!
44 22 zero_gravi
  constant hw_version_c  : std_ulogic_vector(31 downto 0) := x"01040000"; -- no touchy!
45 20 zero_gravi
  constant pmp_max_r_c   : natural := 8; -- max PMP regions
46 22 zero_gravi
  constant ipb_entries_c : natural := 2; -- entries in instruction prefetch buffer, must be a power of 2, default=2
47 2 zero_gravi
 
48 12 zero_gravi
  -- Helper Functions -----------------------------------------------------------------------
49 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
50
  function index_size_f(input : natural) return natural;
51
  function cond_sel_natural_f(cond : boolean; val_t : natural; val_f : natural) return natural;
52
  function cond_sel_stdulogicvector_f(cond : boolean; val_t : std_ulogic_vector; val_f : std_ulogic_vector) return std_ulogic_vector;
53
  function bool_to_ulogic_f(cond : boolean) return std_ulogic;
54 15 zero_gravi
  function or_all_f(a : std_ulogic_vector) return std_ulogic;
55
  function and_all_f(a : std_ulogic_vector) return std_ulogic;
56
  function xor_all_f(a : std_ulogic_vector) return std_ulogic;
57 2 zero_gravi
  function xnor_all_f(a : std_ulogic_vector) return std_ulogic;
58 6 zero_gravi
  function to_hexchar_f(input : std_ulogic_vector(3 downto 0)) return character;
59 2 zero_gravi
 
60 15 zero_gravi
  -- Internal Types -------------------------------------------------------------------------
61
  -- -------------------------------------------------------------------------------------------
62
  type pmp_ctrl_if_t is array (0 to pmp_max_r_c-1) of std_ulogic_vector(7 downto 0);
63
  type pmp_addr_if_t is array (0 to pmp_max_r_c-1) of std_ulogic_vector(33 downto 0);
64
 
65 2 zero_gravi
  -- Processor-internal Address Space Layout ------------------------------------------------
66
  -- -------------------------------------------------------------------------------------------
67
  -- Instruction Memory & Data Memory --
68
  -- => configured via top's generics
69
 
70
  -- Bootloader ROM --
71
  constant boot_base_c          : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFF0000"; -- bootloader base address, fixed!
72
  constant boot_size_c          : natural := 4*1024; -- bytes
73
  constant boot_max_size_c      : natural := 32*1024; -- bytes, fixed!
74
 
75
  -- IO: Peripheral Devices ("IO") Area --
76
  -- Control register(s) (including the device-enable) should be located at the base address of each device
77
  constant io_base_c            : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFF80";
78
  constant io_size_c            : natural := 32*4; -- bytes, fixed!
79
 
80
  -- General Purpose Input/Output Unit (GPIO) --
81
  constant gpio_base_c          : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFF80"; -- base address, fixed!
82
  constant gpio_size_c          : natural := 2*4; -- bytes, fixed!
83
  constant gpio_in_addr_c       : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(gpio_base_c) + x"00000000");
84
  constant gpio_out_addr_c      : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(gpio_base_c) + x"00000004");
85
 
86 18 zero_gravi
  -- Dummy Device (with SIMULATION output) (DEVNULL) --
87
  constant devnull_base_c       : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFF88"; -- base address, fixed!
88
  constant devnull_size_c       : natural := 1*4; -- bytes, fixed!
89
  constant devnull_data_addr_c  : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(devnull_base_c) + x"00000000");
90 2 zero_gravi
 
91
  -- Watch Dog Timer (WDT) --
92
  constant wdt_base_c           : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFF8C"; -- base address, fixed!
93
  constant wdt_size_c           : natural := 1*4; -- bytes, fixed!
94
  constant wdt_ctrl_addr_c      : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(wdt_base_c) + x"00000000");
95
 
96
  -- Machine System Timer (MTIME) --
97
  constant mtime_base_c         : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFF90"; -- base address, fixed!
98
  constant mtime_size_c         : natural := 4*4; -- bytes, fixed!
99
  constant mtime_time_lo_addr_c : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(mtime_base_c) + x"00000000");
100
  constant mtime_time_hi_addr_c : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(mtime_base_c) + x"00000004");
101
  constant mtime_cmp_lo_addr_c  : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(mtime_base_c) + x"00000008");
102
  constant mtime_cmp_hi_addr_c  : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(mtime_base_c) + x"0000000C");
103
 
104
  -- Universal Asynchronous Receiver/Transmitter (UART) --
105
  constant uart_base_c          : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFFA0"; -- base address, fixed!
106
  constant uart_size_c          : natural := 2*4; -- bytes, fixed!
107
  constant uart_ctrl_addr_c     : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(uart_base_c) + x"00000000");
108
  constant uart_rtx_addr_c      : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(uart_base_c) + x"00000004");
109
 
110
  -- Serial Peripheral Interface (SPI) --
111
  constant spi_base_c           : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFFA8"; -- base address, fixed!
112
  constant spi_size_c           : natural := 2*4; -- bytes, fixed!
113
  constant spi_ctrl_addr_c      : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(spi_base_c) + x"00000000");
114
  constant spi_rtx_addr_c       : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(spi_base_c) + x"00000004");
115
 
116
  -- Two Wire Interface (TWI) --
117
  constant twi_base_c           : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFFB0"; -- base address, fixed!
118
  constant twi_size_c           : natural := 2*4; -- bytes, fixed!
119
  constant twi_ctrl_addr_c      : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(twi_base_c) + x"00000000");
120
  constant twi_rtx_addr_c       : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(twi_base_c) + x"00000004");
121
 
122
  -- Pulse-Width Modulation Controller (PWM) --
123
  constant pwm_base_c           : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFFB8"; -- base address, fixed!
124
  constant pwm_size_c           : natural := 2*4; -- bytes, fixed!
125
  constant pwm_ctrl_addr_c      : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(pwm_base_c) + x"00000000");
126
  constant pwm_duty_addr_c      : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(pwm_base_c) + x"00000004");
127
 
128
  -- True Random Number generator (TRNG) --
129
  constant trng_base_c          : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFFC0"; -- base address, fixed!
130
  constant trng_size_c          : natural := 2*4; -- bytes, fixed!
131
  constant trng_ctrl_addr_c     : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(trng_base_c) + x"00000000");
132
  constant trng_data_addr_c     : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(trng_base_c) + x"00000004");
133
 
134 12 zero_gravi
  -- RESERVED --
135 18 zero_gravi
--constant ???_base_c           : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFFC8"; -- base address, fixed!
136
--constant ???_size_c           : natural := 6*4; -- bytes, fixed!
137 12 zero_gravi
 
138
  -- System Information Memory (with SIMULATION output) (SYSINFO) --
139
  constant sysinfo_base_c       : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFFE0"; -- base address, fixed!
140
  constant sysinfo_size_c       : natural := 8*4; -- bytes, fixed!
141
 
142 2 zero_gravi
  -- Main Control Bus -----------------------------------------------------------------------
143
  -- -------------------------------------------------------------------------------------------
144
  -- register file --
145
  constant ctrl_rf_in_mux_lsb_c   : natural :=  0; -- input source select lsb (00=ALU, 01=MEM)
146
  constant ctrl_rf_in_mux_msb_c   : natural :=  1; -- input source select msb (10=PC,  11=CSR)
147
  constant ctrl_rf_rs1_adr0_c     : natural :=  2; -- source register 1 address bit 0
148
  constant ctrl_rf_rs1_adr1_c     : natural :=  3; -- source register 1 address bit 1
149
  constant ctrl_rf_rs1_adr2_c     : natural :=  4; -- source register 1 address bit 2
150
  constant ctrl_rf_rs1_adr3_c     : natural :=  5; -- source register 1 address bit 3
151
  constant ctrl_rf_rs1_adr4_c     : natural :=  6; -- source register 1 address bit 4
152
  constant ctrl_rf_rs2_adr0_c     : natural :=  7; -- source register 2 address bit 0
153
  constant ctrl_rf_rs2_adr1_c     : natural :=  8; -- source register 2 address bit 1
154
  constant ctrl_rf_rs2_adr2_c     : natural :=  9; -- source register 2 address bit 2
155
  constant ctrl_rf_rs2_adr3_c     : natural := 10; -- source register 2 address bit 3
156
  constant ctrl_rf_rs2_adr4_c     : natural := 11; -- source register 2 address bit 4
157
  constant ctrl_rf_rd_adr0_c      : natural := 12; -- destiantion register address bit 0
158
  constant ctrl_rf_rd_adr1_c      : natural := 13; -- destiantion register address bit 1
159
  constant ctrl_rf_rd_adr2_c      : natural := 14; -- destiantion register address bit 2
160
  constant ctrl_rf_rd_adr3_c      : natural := 15; -- destiantion register address bit 3
161
  constant ctrl_rf_rd_adr4_c      : natural := 16; -- destiantion register address bit 4
162
  constant ctrl_rf_wb_en_c        : natural := 17; -- write back enable
163
  constant ctrl_rf_clear_rs1_c    : natural := 18; -- force rs1=r0
164
  constant ctrl_rf_clear_rs2_c    : natural := 19; -- force rs2=r0
165
  -- alu --
166
  constant ctrl_alu_cmd0_c        : natural := 20; -- ALU command bit 0
167
  constant ctrl_alu_cmd1_c        : natural := 21; -- ALU command bit 1
168
  constant ctrl_alu_cmd2_c        : natural := 22; -- ALU command bit 2
169
  constant ctrl_alu_opa_mux_lsb_c : natural := 23; -- operand A select lsb (00=rs1, 01=PC)
170 12 zero_gravi
  constant ctrl_alu_opa_mux_msb_c : natural := 24; -- operand A select msb (1-=CSR)
171 6 zero_gravi
  constant ctrl_alu_opb_mux_lsb_c : natural := 25; -- operand B select lsb (00=rs2, 01=IMM)
172 12 zero_gravi
  constant ctrl_alu_opb_mux_msb_c : natural := 26; -- operand B select msb (1-=rs1)
173 2 zero_gravi
  constant ctrl_alu_opc_mux_c     : natural := 27; -- operand C select (0=IMM, 1=rs2)
174
  constant ctrl_alu_unsigned_c    : natural := 28; -- is unsigned ALU operation
175
  constant ctrl_alu_shift_dir_c   : natural := 29; -- shift direction (0=left, 1=right)
176
  constant ctrl_alu_shift_ar_c    : natural := 30; -- is arithmetic shift
177
  -- bus interface --
178
  constant ctrl_bus_size_lsb_c    : natural := 31; -- transfer size lsb (00=byte, 01=half-word)
179
  constant ctrl_bus_size_msb_c    : natural := 32; -- transfer size msb (10=word, 11=?)
180
  constant ctrl_bus_rd_c          : natural := 33; -- read data request
181
  constant ctrl_bus_wr_c          : natural := 34; -- write data request
182 12 zero_gravi
  constant ctrl_bus_if_c          : natural := 35; -- instruction fetch request
183 2 zero_gravi
  constant ctrl_bus_mar_we_c      : natural := 36; -- memory address register write enable
184
  constant ctrl_bus_mdo_we_c      : natural := 37; -- memory data out register write enable
185
  constant ctrl_bus_mdi_we_c      : natural := 38; -- memory data in register write enable
186
  constant ctrl_bus_unsigned_c    : natural := 39; -- is unsigned load
187 12 zero_gravi
  constant ctrl_bus_ierr_ack_c    : natural := 40; -- acknowledge instruction fetch bus exception
188
  constant ctrl_bus_derr_ack_c    : natural := 41; -- acknowledge data access bus exception
189
  constant ctrl_bus_fence_c       : natural := 42; -- executed fence operation
190
  constant ctrl_bus_fencei_c      : natural := 43; -- executed fencei operation
191 2 zero_gravi
  -- co-processor --
192 12 zero_gravi
  constant ctrl_cp_use_c          : natural := 44; -- is cp operation
193
  constant ctrl_cp_id_lsb_c       : natural := 45; -- cp select lsb
194
  constant ctrl_cp_id_msb_c       : natural := 46; -- cp select msb
195
  constant ctrl_cp_cmd0_c         : natural := 47; -- cp command bit 0
196
  constant ctrl_cp_cmd1_c         : natural := 48; -- cp command bit 1
197
  constant ctrl_cp_cmd2_c         : natural := 49; -- cp command bit 2
198 2 zero_gravi
  -- control bus size --
199 12 zero_gravi
  constant ctrl_width_c           : natural := 50; -- control bus size
200 2 zero_gravi
 
201
  -- ALU Comparator Bus ---------------------------------------------------------------------
202
  -- -------------------------------------------------------------------------------------------
203
  constant alu_cmp_equal_c : natural := 0;
204 6 zero_gravi
  constant alu_cmp_less_c  : natural := 1; -- for signed and unsigned comparisons
205 2 zero_gravi
 
206
  -- RISC-V Opcode Layout -------------------------------------------------------------------
207
  -- -------------------------------------------------------------------------------------------
208
  constant instr_opcode_lsb_c  : natural :=  0; -- opcode bit 0
209
  constant instr_opcode_msb_c  : natural :=  6; -- opcode bit 6
210
  constant instr_rd_lsb_c      : natural :=  7; -- destination register address bit 0
211
  constant instr_rd_msb_c      : natural := 11; -- destination register address bit 4
212
  constant instr_funct3_lsb_c  : natural := 12; -- funct3 bit 0
213
  constant instr_funct3_msb_c  : natural := 14; -- funct3 bit 2
214
  constant instr_rs1_lsb_c     : natural := 15; -- source register 1 address bit 0
215
  constant instr_rs1_msb_c     : natural := 19; -- source register 1 address bit 4
216
  constant instr_rs2_lsb_c     : natural := 20; -- source register 2 address bit 0
217
  constant instr_rs2_msb_c     : natural := 24; -- source register 2 address bit 4
218
  constant instr_funct7_lsb_c  : natural := 25; -- funct7 bit 0
219
  constant instr_funct7_msb_c  : natural := 31; -- funct7 bit 6
220
  constant instr_funct12_lsb_c : natural := 20; -- funct12 bit 0
221
  constant instr_funct12_msb_c : natural := 31; -- funct12 bit 11
222
  constant instr_imm12_lsb_c   : natural := 20; -- immediate12 bit 0
223
  constant instr_imm12_msb_c   : natural := 31; -- immediate12 bit 11
224
  constant instr_imm20_lsb_c   : natural := 12; -- immediate20 bit 0
225
  constant instr_imm20_msb_c   : natural := 31; -- immediate20 bit 21
226
  constant instr_csr_id_lsb_c  : natural := 20; -- csr select bit 0
227
  constant instr_csr_id_msb_c  : natural := 31; -- csr select bit 11
228
 
229
  -- RISC-V Opcodes -------------------------------------------------------------------------
230
  -- -------------------------------------------------------------------------------------------
231
  -- alu --
232
  constant opcode_lui_c    : std_ulogic_vector(6 downto 0) := "0110111"; -- load upper immediate
233
  constant opcode_auipc_c  : std_ulogic_vector(6 downto 0) := "0010111"; -- add upper immediate to PC
234
  constant opcode_alui_c   : std_ulogic_vector(6 downto 0) := "0010011"; -- ALU operation with immediate (operation via funct3 and funct7)
235
  constant opcode_alu_c    : std_ulogic_vector(6 downto 0) := "0110011"; -- ALU operation (operation via funct3 and funct7)
236
  -- control flow --
237
  constant opcode_jal_c    : std_ulogic_vector(6 downto 0) := "1101111"; -- jump and link
238
  constant opcode_jalr_c   : std_ulogic_vector(6 downto 0) := "1100111"; -- jump and register
239
  constant opcode_branch_c : std_ulogic_vector(6 downto 0) := "1100011"; -- branch (condition set via funct3)
240
  -- memory access --
241
  constant opcode_load_c   : std_ulogic_vector(6 downto 0) := "0000011"; -- load (data type via funct3)
242
  constant opcode_store_c  : std_ulogic_vector(6 downto 0) := "0100011"; -- store (data type via funct3)
243
  -- system/csr --
244 8 zero_gravi
  constant opcode_fence_c  : std_ulogic_vector(6 downto 0) := "0001111"; -- fence / fence.i
245 2 zero_gravi
  constant opcode_syscsr_c : std_ulogic_vector(6 downto 0) := "1110011"; -- system/csr access (type via funct3)
246
 
247
  -- RISC-V Funct3 --------------------------------------------------------------------------
248
  -- -------------------------------------------------------------------------------------------
249
  -- control flow --
250
  constant funct3_beq_c    : std_ulogic_vector(2 downto 0) := "000"; -- branch if equal
251
  constant funct3_bne_c    : std_ulogic_vector(2 downto 0) := "001"; -- branch if not equal
252
  constant funct3_blt_c    : std_ulogic_vector(2 downto 0) := "100"; -- branch if less than
253
  constant funct3_bge_c    : std_ulogic_vector(2 downto 0) := "101"; -- branch if greater than or equal
254
  constant funct3_bltu_c   : std_ulogic_vector(2 downto 0) := "110"; -- branch if less than (unsigned)
255
  constant funct3_bgeu_c   : std_ulogic_vector(2 downto 0) := "111"; -- branch if greater than or equal (unsigned)
256
  -- memory access --
257
  constant funct3_lb_c     : std_ulogic_vector(2 downto 0) := "000"; -- load byte
258
  constant funct3_lh_c     : std_ulogic_vector(2 downto 0) := "001"; -- load half word
259
  constant funct3_lw_c     : std_ulogic_vector(2 downto 0) := "010"; -- load word
260
  constant funct3_lbu_c    : std_ulogic_vector(2 downto 0) := "100"; -- load byte (unsigned)
261
  constant funct3_lhu_c    : std_ulogic_vector(2 downto 0) := "101"; -- load half word (unsigned)
262
  constant funct3_sb_c     : std_ulogic_vector(2 downto 0) := "000"; -- store byte
263
  constant funct3_sh_c     : std_ulogic_vector(2 downto 0) := "001"; -- store half word
264
  constant funct3_sw_c     : std_ulogic_vector(2 downto 0) := "010"; -- store word
265
  -- alu --
266
  constant funct3_subadd_c : std_ulogic_vector(2 downto 0) := "000"; -- sub/add via funct7
267
  constant funct3_sll_c    : std_ulogic_vector(2 downto 0) := "001"; -- shift logical left
268
  constant funct3_slt_c    : std_ulogic_vector(2 downto 0) := "010"; -- set on less
269
  constant funct3_sltu_c   : std_ulogic_vector(2 downto 0) := "011"; -- set on less unsigned
270
  constant funct3_xor_c    : std_ulogic_vector(2 downto 0) := "100"; -- xor
271
  constant funct3_sr_c     : std_ulogic_vector(2 downto 0) := "101"; -- shift right via funct7
272
  constant funct3_or_c     : std_ulogic_vector(2 downto 0) := "110"; -- or
273
  constant funct3_and_c    : std_ulogic_vector(2 downto 0) := "111"; -- and
274
  -- system/csr --
275
  constant funct3_env_c    : std_ulogic_vector(2 downto 0) := "000"; -- ecall, ebreak, mret, wfi
276
  constant funct3_csrrw_c  : std_ulogic_vector(2 downto 0) := "001"; -- atomic r/w
277
  constant funct3_csrrs_c  : std_ulogic_vector(2 downto 0) := "010"; -- atomic read & set bit
278
  constant funct3_csrrc_c  : std_ulogic_vector(2 downto 0) := "011"; -- atomic read & clear bit
279
  --
280
  constant funct3_csrrwi_c : std_ulogic_vector(2 downto 0) := "101"; -- atomic r/w immediate
281
  constant funct3_csrrsi_c : std_ulogic_vector(2 downto 0) := "110"; -- atomic read & set bit immediate
282
  constant funct3_csrrci_c : std_ulogic_vector(2 downto 0) := "111"; -- atomic read & clear bit immediate
283 8 zero_gravi
  -- fence --
284
  constant funct3_fence_c  : std_ulogic_vector(2 downto 0) := "000"; -- fence - order IO/memory access (->NOP)
285
  constant funct3_fencei_c : std_ulogic_vector(2 downto 0) := "001"; -- fencei - instructon stream sync
286 2 zero_gravi
 
287 11 zero_gravi
  -- RISC-V Funct12 --------------------------------------------------------------------------
288
  -- -------------------------------------------------------------------------------------------
289
  -- system --
290
  constant funct12_ecall_c  : std_ulogic_vector(11 downto 0) := x"000"; -- ECALL
291
  constant funct12_ebreak_c : std_ulogic_vector(11 downto 0) := x"001"; -- EBREAK
292
  constant funct12_mret_c   : std_ulogic_vector(11 downto 0) := x"302"; -- MRET
293
  constant funct12_wfi_c    : std_ulogic_vector(11 downto 0) := x"105"; -- WFI
294
 
295 2 zero_gravi
  -- Co-Processor Operations ----------------------------------------------------------------
296
  -- -------------------------------------------------------------------------------------------
297
  -- cp ids --
298
  constant cp_sel_muldiv_c : std_ulogic_vector(1 downto 0) := "00"; -- MULDIV CP
299
  -- muldiv cp --
300 6 zero_gravi
  constant cp_op_mul_c     : std_ulogic_vector(2 downto 0) := "000"; -- mul
301
  constant cp_op_mulh_c    : std_ulogic_vector(2 downto 0) := "001"; -- mulh
302
  constant cp_op_mulhsu_c  : std_ulogic_vector(2 downto 0) := "010"; -- mulhsu
303
  constant cp_op_mulhu_c   : std_ulogic_vector(2 downto 0) := "011"; -- mulhu
304
  constant cp_op_div_c     : std_ulogic_vector(2 downto 0) := "100"; -- div
305
  constant cp_op_divu_c    : std_ulogic_vector(2 downto 0) := "101"; -- divu
306
  constant cp_op_rem_c     : std_ulogic_vector(2 downto 0) := "110"; -- rem
307
  constant cp_op_remu_c    : std_ulogic_vector(2 downto 0) := "111"; -- remu
308 2 zero_gravi
 
309
  -- ALU Function Codes ---------------------------------------------------------------------
310
  -- -------------------------------------------------------------------------------------------
311
  constant alu_cmd_add_c   : std_ulogic_vector(2 downto 0) := "000"; -- r <= A + B
312
  constant alu_cmd_sub_c   : std_ulogic_vector(2 downto 0) := "001"; -- r <= A - B
313
  constant alu_cmd_slt_c   : std_ulogic_vector(2 downto 0) := "010"; -- r <= A < B
314
  constant alu_cmd_shift_c : std_ulogic_vector(2 downto 0) := "011"; -- r <= A <</>> B
315
  constant alu_cmd_xor_c   : std_ulogic_vector(2 downto 0) := "100"; -- r <= A xor B
316
  constant alu_cmd_or_c    : std_ulogic_vector(2 downto 0) := "101"; -- r <= A or B
317
  constant alu_cmd_and_c   : std_ulogic_vector(2 downto 0) := "110"; -- r <= A and B
318
  constant alu_cmd_bitc_c  : std_ulogic_vector(2 downto 0) := "111"; -- r <= A and (not B)
319
 
320 12 zero_gravi
  -- Trap ID Codes --------------------------------------------------------------------------
321
  -- -------------------------------------------------------------------------------------------
322 14 zero_gravi
  -- risc-v compliant --
323
  constant trap_ima_c   : std_ulogic_vector(5 downto 0) := "000000"; -- 0.0:  instruction misaligned
324
  constant trap_iba_c   : std_ulogic_vector(5 downto 0) := "000001"; -- 0.1:  instruction access fault
325
  constant trap_iil_c   : std_ulogic_vector(5 downto 0) := "000010"; -- 0.2:  illegal instruction
326
  constant trap_brk_c   : std_ulogic_vector(5 downto 0) := "000011"; -- 0.3:  breakpoint
327
  constant trap_lma_c   : std_ulogic_vector(5 downto 0) := "000100"; -- 0.4:  load address misaligned
328
  constant trap_lbe_c   : std_ulogic_vector(5 downto 0) := "000101"; -- 0.5:  load access fault
329
  constant trap_sma_c   : std_ulogic_vector(5 downto 0) := "000110"; -- 0.6:  store address misaligned
330
  constant trap_sbe_c   : std_ulogic_vector(5 downto 0) := "000111"; -- 0.7:  store access fault
331
  constant trap_menv_c  : std_ulogic_vector(5 downto 0) := "001011"; -- 0.11: environment call from m-mode
332
  --
333
  constant trap_msi_c   : std_ulogic_vector(5 downto 0) := "100011"; -- 1.3:  machine software interrupt
334
  constant trap_mti_c   : std_ulogic_vector(5 downto 0) := "100111"; -- 1.7:  machine timer interrupt
335
  constant trap_mei_c   : std_ulogic_vector(5 downto 0) := "101011"; -- 1.11: machine external interrupt
336
  -- custom --
337
  constant trap_firq0_c : std_ulogic_vector(5 downto 0) := "110000"; -- 1.16: fast interrupt 0
338
  constant trap_firq1_c : std_ulogic_vector(5 downto 0) := "110001"; -- 1.17: fast interrupt 1
339
  constant trap_firq2_c : std_ulogic_vector(5 downto 0) := "110010"; -- 1.18: fast interrupt 2
340
  constant trap_firq3_c : std_ulogic_vector(5 downto 0) := "110011"; -- 1.19: fast interrupt 3
341 12 zero_gravi
 
342 2 zero_gravi
  -- CPU Control Exception System -----------------------------------------------------------
343
  -- -------------------------------------------------------------------------------------------
344
  -- exception source bits --
345
  constant exception_iaccess_c   : natural := 0; -- instrution access fault
346
  constant exception_iillegal_c  : natural := 1; -- illegal instrution
347
  constant exception_ialign_c    : natural := 2; -- instrution address misaligned
348
  constant exception_m_envcall_c : natural := 3; -- ENV call from m-mode
349
  constant exception_break_c     : natural := 4; -- breakpoint
350
  constant exception_salign_c    : natural := 5; -- store address misaligned
351
  constant exception_lalign_c    : natural := 6; -- load address misaligned
352
  constant exception_saccess_c   : natural := 7; -- store access fault
353
  constant exception_laccess_c   : natural := 8; -- load access fault
354 14 zero_gravi
  --
355 2 zero_gravi
  constant exception_width_c     : natural := 9; -- length of this list in bits
356
  -- interrupt source bits --
357 12 zero_gravi
  constant interrupt_msw_irq_c   : natural := 0; -- machine software interrupt
358
  constant interrupt_mtime_irq_c : natural := 1; -- machine timer interrupt
359 2 zero_gravi
  constant interrupt_mext_irq_c  : natural := 2; -- machine external interrupt
360 14 zero_gravi
  constant interrupt_firq_0_c    : natural := 3; -- fast interrupt channel 0
361
  constant interrupt_firq_1_c    : natural := 4; -- fast interrupt channel 1
362
  constant interrupt_firq_2_c    : natural := 5; -- fast interrupt channel 2
363
  constant interrupt_firq_3_c    : natural := 6; -- fast interrupt channel 3
364
  --
365
  constant interrupt_width_c     : natural := 7; -- length of this list in bits
366 2 zero_gravi
 
367 15 zero_gravi
  -- CPU Privilege Modes --------------------------------------------------------------------
368
  -- -------------------------------------------------------------------------------------------
369
  constant m_priv_mode_c : std_ulogic_vector(1 downto 0) := "11"; -- machine mode
370
  constant u_priv_mode_c : std_ulogic_vector(1 downto 0) := "00"; -- user mode
371
 
372 2 zero_gravi
  -- Clock Generator -------------------------------------------------------------------------
373
  -- -------------------------------------------------------------------------------------------
374
  constant clk_div2_c    : natural := 0;
375
  constant clk_div4_c    : natural := 1;
376
  constant clk_div8_c    : natural := 2;
377
  constant clk_div64_c   : natural := 3;
378
  constant clk_div128_c  : natural := 4;
379
  constant clk_div1024_c : natural := 5;
380
  constant clk_div2048_c : natural := 6;
381
  constant clk_div4096_c : natural := 7;
382
 
383
  -- Component: NEORV32 Processor Top Entity ------------------------------------------------
384
  -- -------------------------------------------------------------------------------------------
385
  component neorv32_top
386
    generic (
387
      -- General --
388 12 zero_gravi
      CLOCK_FREQUENCY              : natural := 0;      -- clock frequency of clk_i in Hz
389 8 zero_gravi
      BOOTLOADER_USE               : boolean := true;   -- implement processor-internal bootloader?
390 12 zero_gravi
      USER_CODE                    : std_ulogic_vector(31 downto 0) := x"00000000"; -- custom user code
391 2 zero_gravi
      -- RISC-V CPU Extensions --
392 18 zero_gravi
      CPU_EXTENSION_RISCV_C        : boolean := false;  -- implement compressed extension?
393 8 zero_gravi
      CPU_EXTENSION_RISCV_E        : boolean := false;  -- implement embedded RF extension?
394 18 zero_gravi
      CPU_EXTENSION_RISCV_M        : boolean := false;  -- implement muld/div extension?
395
      CPU_EXTENSION_RISCV_U        : boolean := false;  -- implement user mode extension?
396 8 zero_gravi
      CPU_EXTENSION_RISCV_Zicsr    : boolean := true;   -- implement CSR system?
397
      CPU_EXTENSION_RISCV_Zifencei : boolean := true;   -- implement instruction stream sync.?
398 19 zero_gravi
      -- Extension Options --
399
      CSR_COUNTERS_USE             : boolean := true;  -- implement RISC-V perf. counters ([m]instret[h], [m]cycle[h], time[h])?
400
      FAST_MUL_EN                  : boolean := false; -- use DSPs for M extension's multiplier
401 15 zero_gravi
      -- Physical Memory Protection (PMP) --
402
      PMP_USE                      : boolean := false; -- implement PMP?
403 16 zero_gravi
      PMP_NUM_REGIONS              : natural := 4;     -- number of regions (max 8)
404
      PMP_GRANULARITY              : natural := 14;    -- minimal region granularity (1=8B, 2=16B, 3=32B, ...) default is 64k
405 2 zero_gravi
      -- Memory configuration: Instruction memory --
406 8 zero_gravi
      MEM_ISPACE_BASE              : std_ulogic_vector(31 downto 0) := x"00000000"; -- base address of instruction memory space
407
      MEM_ISPACE_SIZE              : natural := 16*1024; -- total size of instruction memory space in byte
408
      MEM_INT_IMEM_USE             : boolean := true;    -- implement processor-internal instruction memory
409
      MEM_INT_IMEM_SIZE            : natural := 16*1024; -- size of processor-internal instruction memory in bytes
410
      MEM_INT_IMEM_ROM             : boolean := false;   -- implement processor-internal instruction memory as ROM
411 2 zero_gravi
      -- Memory configuration: Data memory --
412 8 zero_gravi
      MEM_DSPACE_BASE              : std_ulogic_vector(31 downto 0) := x"80000000"; -- base address of data memory space
413
      MEM_DSPACE_SIZE              : natural := 8*1024; -- total size of data memory space in byte
414
      MEM_INT_DMEM_USE             : boolean := true;   -- implement processor-internal data memory
415
      MEM_INT_DMEM_SIZE            : natural := 8*1024; -- size of processor-internal data memory in bytes
416 2 zero_gravi
      -- Memory configuration: External memory interface --
417 8 zero_gravi
      MEM_EXT_USE                  : boolean := false;  -- implement external memory bus interface?
418
      MEM_EXT_REG_STAGES           : natural := 2;      -- number of interface register stages (0,1,2)
419
      MEM_EXT_TIMEOUT              : natural := 15;     -- cycles after which a valid bus access will timeout (>=1)
420 2 zero_gravi
      -- Processor peripherals --
421 8 zero_gravi
      IO_GPIO_USE                  : boolean := true;   -- implement general purpose input/output port unit (GPIO)?
422
      IO_MTIME_USE                 : boolean := true;   -- implement machine system timer (MTIME)?
423
      IO_UART_USE                  : boolean := true;   -- implement universal asynchronous receiver/transmitter (UART)?
424
      IO_SPI_USE                   : boolean := true;   -- implement serial peripheral interface (SPI)?
425
      IO_TWI_USE                   : boolean := true;   -- implement two-wire interface (TWI)?
426
      IO_PWM_USE                   : boolean := true;   -- implement pulse-width modulation unit (PWM)?
427
      IO_WDT_USE                   : boolean := true;   -- implement watch dog timer (WDT)?
428
      IO_TRNG_USE                  : boolean := false;  -- implement true random number generator (TRNG)?
429
      IO_DEVNULL_USE               : boolean := true    -- implement dummy device (DEVNULL)?
430 2 zero_gravi
    );
431
    port (
432
      -- Global control --
433
      clk_i      : in  std_ulogic := '0'; -- global clock, rising edge
434
      rstn_i     : in  std_ulogic := '0'; -- global reset, low-active, async
435
      -- Wishbone bus interface --
436
      wb_adr_o   : out std_ulogic_vector(31 downto 0); -- address
437
      wb_dat_i   : in  std_ulogic_vector(31 downto 0) := (others => '0'); -- read data
438
      wb_dat_o   : out std_ulogic_vector(31 downto 0); -- write data
439
      wb_we_o    : out std_ulogic; -- read/write
440
      wb_sel_o   : out std_ulogic_vector(03 downto 0); -- byte enable
441
      wb_stb_o   : out std_ulogic; -- strobe
442
      wb_cyc_o   : out std_ulogic; -- valid cycle
443
      wb_ack_i   : in  std_ulogic := '0'; -- transfer acknowledge
444
      wb_err_i   : in  std_ulogic := '0'; -- transfer error
445 12 zero_gravi
      -- Advanced memory control signals (available if MEM_EXT_USE = true) --
446
      fence_o    : out std_ulogic; -- indicates an executed FENCE operation
447
      fencei_o   : out std_ulogic; -- indicates an executed FENCEI operation
448 2 zero_gravi
      -- GPIO --
449 22 zero_gravi
      gpio_o     : out std_ulogic_vector(31 downto 0); -- parallel output
450
      gpio_i     : in  std_ulogic_vector(31 downto 0) := (others => '0'); -- parallel input
451 2 zero_gravi
      -- UART --
452
      uart_txd_o : out std_ulogic; -- UART send data
453
      uart_rxd_i : in  std_ulogic := '0'; -- UART receive data
454
      -- SPI --
455 6 zero_gravi
      spi_sck_o  : out std_ulogic; -- SPI serial clock
456
      spi_sdo_o  : out std_ulogic; -- controller data out, peripheral data in
457 14 zero_gravi
      spi_sdi_i  : in  std_ulogic := '0'; -- controller data in, peripheral data out
458 2 zero_gravi
      spi_csn_o  : out std_ulogic_vector(07 downto 0); -- SPI CS
459
      -- TWI --
460
      twi_sda_io : inout std_logic := 'H'; -- twi serial data line
461
      twi_scl_io : inout std_logic := 'H'; -- twi serial clock line
462
      -- PWM --
463
      pwm_o      : out std_ulogic_vector(03 downto 0);  -- pwm channels
464
      -- Interrupts --
465 14 zero_gravi
      msw_irq_i  : in  std_ulogic := '0'; -- machine software interrupt
466
      mext_irq_i : in  std_ulogic := '0'  -- machine external interrupt
467 2 zero_gravi
    );
468
  end component;
469
 
470 4 zero_gravi
  -- Component: CPU Top Entity --------------------------------------------------------------
471
  -- -------------------------------------------------------------------------------------------
472
  component neorv32_cpu
473
    generic (
474
      -- General --
475 14 zero_gravi
      HW_THREAD_ID                 : std_ulogic_vector(31 downto 0):= (others => '0'); -- hardware thread id
476
      CPU_BOOT_ADDR                : std_ulogic_vector(31 downto 0):= (others => '0'); -- cpu boot address
477 4 zero_gravi
      -- RISC-V CPU Extensions --
478 12 zero_gravi
      CPU_EXTENSION_RISCV_C        : boolean := false; -- implement compressed extension?
479
      CPU_EXTENSION_RISCV_E        : boolean := false; -- implement embedded RF extension?
480
      CPU_EXTENSION_RISCV_M        : boolean := false; -- implement muld/div extension?
481 15 zero_gravi
      CPU_EXTENSION_RISCV_U        : boolean := false; -- implement user mode extension?
482 12 zero_gravi
      CPU_EXTENSION_RISCV_Zicsr    : boolean := true;  -- implement CSR system?
483
      CPU_EXTENSION_RISCV_Zifencei : boolean := true;  -- implement instruction stream sync.?
484 19 zero_gravi
      -- Extension Options --
485
      CSR_COUNTERS_USE             : boolean := true;  -- implement RISC-V perf. counters ([m]instret[h], [m]cycle[h], time[h])?
486
      FAST_MUL_EN                  : boolean := false; -- use DSPs for M extension's multiplier
487 15 zero_gravi
      -- Physical Memory Protection (PMP) --
488
      PMP_USE                      : boolean := false; -- implement PMP?
489 16 zero_gravi
      PMP_NUM_REGIONS              : natural := 4;     -- number of regions (max 8)
490
      PMP_GRANULARITY              : natural := 14;    -- minimal region granularity (1=8B, 2=16B, 3=32B, ...) default is 64k
491 14 zero_gravi
      -- Bus Interface --
492
      BUS_TIMEOUT                  : natural := 15     -- cycles after which a valid bus access will timeout
493 4 zero_gravi
    );
494
    port (
495
      -- global control --
496 14 zero_gravi
      clk_i          : in  std_ulogic := '0'; -- global clock, rising edge
497
      rstn_i         : in  std_ulogic := '0'; -- global reset, low-active, async
498 12 zero_gravi
      -- instruction bus interface --
499
      i_bus_addr_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
500 14 zero_gravi
      i_bus_rdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0) := (others => '0'); -- bus read data
501 12 zero_gravi
      i_bus_wdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
502
      i_bus_ben_o    : out std_ulogic_vector(03 downto 0); -- byte enable
503
      i_bus_we_o     : out std_ulogic; -- write enable
504
      i_bus_re_o     : out std_ulogic; -- read enable
505
      i_bus_cancel_o : out std_ulogic; -- cancel current bus transaction
506 14 zero_gravi
      i_bus_ack_i    : in  std_ulogic := '0'; -- bus transfer acknowledge
507
      i_bus_err_i    : in  std_ulogic := '0'; -- bus transfer error
508 12 zero_gravi
      i_bus_fence_o  : out std_ulogic; -- executed FENCEI operation
509
      -- data bus interface --
510
      d_bus_addr_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
511 14 zero_gravi
      d_bus_rdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0) := (others => '0'); -- bus read data
512 12 zero_gravi
      d_bus_wdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
513
      d_bus_ben_o    : out std_ulogic_vector(03 downto 0); -- byte enable
514
      d_bus_we_o     : out std_ulogic; -- write enable
515
      d_bus_re_o     : out std_ulogic; -- read enable
516
      d_bus_cancel_o : out std_ulogic; -- cancel current bus transaction
517 14 zero_gravi
      d_bus_ack_i    : in  std_ulogic := '0'; -- bus transfer acknowledge
518
      d_bus_err_i    : in  std_ulogic := '0'; -- bus transfer error
519 12 zero_gravi
      d_bus_fence_o  : out std_ulogic; -- executed FENCE operation
520 11 zero_gravi
      -- system time input from MTIME --
521 14 zero_gravi
      time_i         : in  std_ulogic_vector(63 downto 0) := (others => '0'); -- current system time
522
      -- interrupts (risc-v compliant) --
523
      msw_irq_i      : in  std_ulogic := '0'; -- machine software interrupt
524
      mext_irq_i     : in  std_ulogic := '0'; -- machine external interrupt
525
      mtime_irq_i    : in  std_ulogic := '0'; -- machine timer interrupt
526
      -- fast interrupts (custom) --
527
      firq_i         : in  std_ulogic_vector(3 downto 0) := (others => '0')
528 4 zero_gravi
    );
529
  end component;
530
 
531 2 zero_gravi
  -- Component: CPU Control -----------------------------------------------------------------
532
  -- -------------------------------------------------------------------------------------------
533
  component neorv32_cpu_control
534
    generic (
535
      -- General --
536 12 zero_gravi
      CSR_COUNTERS_USE             : boolean := true;  -- implement RISC-V perf. counters ([m]instret[h], [m]cycle[h], time[h])?
537
      HW_THREAD_ID                 : std_ulogic_vector(31 downto 0):= x"00000000"; -- hardware thread id
538
      CPU_BOOT_ADDR                : std_ulogic_vector(31 downto 0):= x"00000000"; -- cpu boot address
539 2 zero_gravi
      -- RISC-V CPU Extensions --
540 12 zero_gravi
      CPU_EXTENSION_RISCV_C        : boolean := false; -- implement compressed extension?
541
      CPU_EXTENSION_RISCV_E        : boolean := false; -- implement embedded RF extension?
542
      CPU_EXTENSION_RISCV_M        : boolean := false; -- implement muld/div extension?
543 15 zero_gravi
      CPU_EXTENSION_RISCV_U        : boolean := false; -- implement user mode extension?
544 12 zero_gravi
      CPU_EXTENSION_RISCV_Zicsr    : boolean := true;  -- implement CSR system?
545 15 zero_gravi
      CPU_EXTENSION_RISCV_Zifencei : boolean := true;  -- implement instruction stream sync.?
546
      -- Physical memory protection (PMP) --
547
      PMP_USE                      : boolean := false; -- implement physical memory protection?
548
      PMP_NUM_REGIONS              : natural := 4; -- number of regions (1..4)
549
      PMP_GRANULARITY              : natural := 0  -- granularity (0=none, 1=8B, 2=16B, 3=32B, ...)
550 2 zero_gravi
    );
551
    port (
552
      -- global control --
553
      clk_i         : in  std_ulogic; -- global clock, rising edge
554
      rstn_i        : in  std_ulogic; -- global reset, low-active, async
555
      ctrl_o        : out std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
556
      -- status input --
557
      alu_wait_i    : in  std_ulogic; -- wait for ALU
558 12 zero_gravi
      bus_i_wait_i  : in  std_ulogic; -- wait for bus
559
      bus_d_wait_i  : in  std_ulogic; -- wait for bus
560 2 zero_gravi
      -- data input --
561
      instr_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- instruction
562
      cmp_i         : in  std_ulogic_vector(1 downto 0); -- comparator status
563
      alu_add_i     : in  std_ulogic_vector(data_width_c-1 downto 0); -- ALU.add result
564
      -- data output --
565
      imm_o         : out std_ulogic_vector(data_width_c-1 downto 0); -- immediate
566 6 zero_gravi
      fetch_pc_o    : out std_ulogic_vector(data_width_c-1 downto 0); -- PC for instruction fetch
567
      curr_pc_o     : out std_ulogic_vector(data_width_c-1 downto 0); -- current PC (corresponding to current instruction)
568
      next_pc_o     : out std_ulogic_vector(data_width_c-1 downto 0); -- next PC (corresponding to current instruction)
569 2 zero_gravi
      -- csr interface --
570
      csr_wdata_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- CSR write data
571
      csr_rdata_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- CSR read data
572 14 zero_gravi
      -- interrupts (risc-v compliant) --
573
      msw_irq_i     : in  std_ulogic; -- machine software interrupt
574
      mext_irq_i    : in  std_ulogic; -- machine external interrupt
575 2 zero_gravi
      mtime_irq_i   : in  std_ulogic; -- machine timer interrupt
576 14 zero_gravi
      -- fast interrupts (custom) --
577
      firq_i        : in  std_ulogic_vector(3 downto 0);
578 11 zero_gravi
      -- system time input from MTIME --
579
      time_i        : in  std_ulogic_vector(63 downto 0); -- current system time
580 15 zero_gravi
      -- physical memory protection --
581
      pmp_addr_o    : out pmp_addr_if_t; -- addresses
582
      pmp_ctrl_o    : out pmp_ctrl_if_t; -- configs
583
      priv_mode_o   : out std_ulogic_vector(1 downto 0); -- current CPU privilege level
584 2 zero_gravi
      -- bus access exceptions --
585
      mar_i         : in  std_ulogic_vector(data_width_c-1 downto 0); -- memory address register
586
      ma_instr_i    : in  std_ulogic; -- misaligned instruction address
587
      ma_load_i     : in  std_ulogic; -- misaligned load data address
588
      ma_store_i    : in  std_ulogic; -- misaligned store data address
589
      be_instr_i    : in  std_ulogic; -- bus error on instruction access
590
      be_load_i     : in  std_ulogic; -- bus error on load data access
591 12 zero_gravi
      be_store_i    : in  std_ulogic  -- bus error on store data access
592 2 zero_gravi
    );
593
  end component;
594
 
595
  -- Component: CPU Register File -----------------------------------------------------------
596
  -- -------------------------------------------------------------------------------------------
597
  component neorv32_cpu_regfile
598
    generic (
599
      CPU_EXTENSION_RISCV_E : boolean := false -- implement embedded RF extension?
600
    );
601
    port (
602
      -- global control --
603
      clk_i  : in  std_ulogic; -- global clock, rising edge
604
      ctrl_i : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
605
      -- data input --
606
      mem_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- memory read data
607
      alu_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
608
      csr_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- CSR read data
609
      pc_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- current pc
610
      -- data output --
611
      rs1_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- operand 1
612
      rs2_o  : out std_ulogic_vector(data_width_c-1 downto 0)  -- operand 2
613
    );
614
  end component;
615
 
616
  -- Component: CPU ALU ---------------------------------------------------------------------
617
  -- -------------------------------------------------------------------------------------------
618
  component neorv32_cpu_alu
619 11 zero_gravi
    generic (
620
      CPU_EXTENSION_RISCV_M : boolean := true -- implement muld/div extension?
621
    );
622 2 zero_gravi
    port (
623
      -- global control --
624
      clk_i       : in  std_ulogic; -- global clock, rising edge
625
      rstn_i      : in  std_ulogic; -- global reset, low-active, async
626
      ctrl_i      : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
627
      -- data input --
628
      rs1_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
629
      rs2_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
630
      pc2_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- delayed PC
631
      imm_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- immediate
632
      csr_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- csr read data
633
      -- data output --
634
      cmp_o       : out std_ulogic_vector(1 downto 0); -- comparator status
635
      add_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- OPA + OPB
636
      res_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
637
      -- co-processor interface --
638 19 zero_gravi
      cp0_start_o : out std_ulogic; -- trigger co-processor 0
639 2 zero_gravi
      cp0_data_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- co-processor 0 result
640
      cp0_valid_i : in  std_ulogic; -- co-processor 0 result valid
641 19 zero_gravi
      cp1_start_o : out std_ulogic; -- trigger co-processor 1
642 2 zero_gravi
      cp1_data_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- co-processor 1 result
643
      cp1_valid_i : in  std_ulogic; -- co-processor 1 result valid
644
      -- status --
645
      wait_o      : out std_ulogic -- busy due to iterative processing units
646
    );
647
  end component;
648
 
649
  -- Component: CPU Co-Processor MULDIV -----------------------------------------------------
650
  -- -------------------------------------------------------------------------------------------
651
  component neorv32_cpu_cp_muldiv
652 19 zero_gravi
    generic (
653
      FAST_MUL_EN : boolean := false -- use DSPs for faster multiplication
654
    );
655 2 zero_gravi
    port (
656
      -- global control --
657
      clk_i   : in  std_ulogic; -- global clock, rising edge
658
      rstn_i  : in  std_ulogic; -- global reset, low-active, async
659
      ctrl_i  : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
660
      -- data input --
661 19 zero_gravi
      start_i : in  std_ulogic; -- trigger operation
662 2 zero_gravi
      rs1_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
663
      rs2_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
664
      -- result and status --
665
      res_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- operation result
666
      valid_o : out std_ulogic -- data output valid
667
    );
668
  end component;
669
 
670
  -- Component: CPU Bus Interface -----------------------------------------------------------
671
  -- -------------------------------------------------------------------------------------------
672
  component neorv32_cpu_bus
673
    generic (
674 11 zero_gravi
      CPU_EXTENSION_RISCV_C : boolean := true; -- implement compressed extension?
675 15 zero_gravi
      BUS_TIMEOUT           : natural := 15;   -- cycles after which a valid bus access will timeout
676
      -- Physical memory protection (PMP) --
677
      PMP_USE               : boolean := false; -- implement physical memory protection?
678
      PMP_NUM_REGIONS       : natural := 4; -- number of regions (1..4)
679 18 zero_gravi
      PMP_GRANULARITY       : natural := 0  -- granularity (1=8B, 2=16B, 3=32B, ...)
680 2 zero_gravi
    );
681
    port (
682
      -- global control --
683 12 zero_gravi
      clk_i          : in  std_ulogic; -- global clock, rising edge
684
      rstn_i         : in  std_ulogic; -- global reset, low-active, async
685
      ctrl_i         : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
686
      -- cpu instruction fetch interface --
687
      fetch_pc_i     : in  std_ulogic_vector(data_width_c-1 downto 0); -- PC for instruction fetch
688
      instr_o        : out std_ulogic_vector(data_width_c-1 downto 0); -- instruction
689
      i_wait_o       : out std_ulogic; -- wait for fetch to complete
690
      --
691
      ma_instr_o     : out std_ulogic; -- misaligned instruction address
692
      be_instr_o     : out std_ulogic; -- bus error on instruction access
693
      -- cpu data access interface --
694
      addr_i         : in  std_ulogic_vector(data_width_c-1 downto 0); -- ALU result -> access address
695
      wdata_i        : in  std_ulogic_vector(data_width_c-1 downto 0); -- write data
696
      rdata_o        : out std_ulogic_vector(data_width_c-1 downto 0); -- read data
697
      mar_o          : out std_ulogic_vector(data_width_c-1 downto 0); -- current memory address register
698
      d_wait_o       : out std_ulogic; -- wait for access to complete
699
      --
700
      ma_load_o      : out std_ulogic; -- misaligned load data address
701
      ma_store_o     : out std_ulogic; -- misaligned store data address
702
      be_load_o      : out std_ulogic; -- bus error on load data access
703
      be_store_o     : out std_ulogic; -- bus error on store data access
704 15 zero_gravi
      -- physical memory protection --
705
      pmp_addr_i     : in  pmp_addr_if_t; -- addresses
706
      pmp_ctrl_i     : in  pmp_ctrl_if_t; -- configs
707
      priv_mode_i    : in  std_ulogic_vector(1 downto 0); -- current CPU privilege level
708 12 zero_gravi
      -- instruction bus --
709
      i_bus_addr_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
710
      i_bus_rdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
711
      i_bus_wdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
712
      i_bus_ben_o    : out std_ulogic_vector(03 downto 0); -- byte enable
713
      i_bus_we_o     : out std_ulogic; -- write enable
714
      i_bus_re_o     : out std_ulogic; -- read enable
715
      i_bus_cancel_o : out std_ulogic; -- cancel current bus transaction
716
      i_bus_ack_i    : in  std_ulogic; -- bus transfer acknowledge
717
      i_bus_err_i    : in  std_ulogic; -- bus transfer error
718
      i_bus_fence_o  : out std_ulogic; -- fence operation
719
      -- data bus --
720
      d_bus_addr_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
721
      d_bus_rdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
722
      d_bus_wdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
723
      d_bus_ben_o    : out std_ulogic_vector(03 downto 0); -- byte enable
724
      d_bus_we_o     : out std_ulogic; -- write enable
725
      d_bus_re_o     : out std_ulogic; -- read enable
726
      d_bus_cancel_o : out std_ulogic; -- cancel current bus transaction
727
      d_bus_ack_i    : in  std_ulogic; -- bus transfer acknowledge
728
      d_bus_err_i    : in  std_ulogic; -- bus transfer error
729
      d_bus_fence_o  : out std_ulogic  -- fence operation
730 2 zero_gravi
    );
731
  end component;
732
 
733 12 zero_gravi
  -- Component: CPU Bus Switch --------------------------------------------------------------
734
  -- -------------------------------------------------------------------------------------------
735
  component neorv32_busswitch
736
    generic (
737
      PORT_CA_READ_ONLY : boolean := false; -- set if controller port A is read-only
738
      PORT_CB_READ_ONLY : boolean := false  -- set if controller port B is read-only
739
    );
740
    port (
741
      -- global control --
742
      clk_i           : in  std_ulogic; -- global clock, rising edge
743
      rstn_i          : in  std_ulogic; -- global reset, low-active, async
744
      -- controller interface a --
745
      ca_bus_addr_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
746
      ca_bus_rdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
747
      ca_bus_wdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
748
      ca_bus_ben_i    : in  std_ulogic_vector(03 downto 0); -- byte enable
749
      ca_bus_we_i     : in  std_ulogic; -- write enable
750
      ca_bus_re_i     : in  std_ulogic; -- read enable
751
      ca_bus_cancel_i : in  std_ulogic; -- cancel current bus transaction
752
      ca_bus_ack_o    : out std_ulogic; -- bus transfer acknowledge
753
      ca_bus_err_o    : out std_ulogic; -- bus transfer error
754
      -- controller interface b --
755
      cb_bus_addr_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
756
      cb_bus_rdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
757
      cb_bus_wdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
758
      cb_bus_ben_i    : in  std_ulogic_vector(03 downto 0); -- byte enable
759
      cb_bus_we_i     : in  std_ulogic; -- write enable
760
      cb_bus_re_i     : in  std_ulogic; -- read enable
761
      cb_bus_cancel_i : in  std_ulogic; -- cancel current bus transaction
762
      cb_bus_ack_o    : out std_ulogic; -- bus transfer acknowledge
763
      cb_bus_err_o    : out std_ulogic; -- bus transfer error
764
      -- peripheral bus --
765
      p_bus_addr_o    : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
766
      p_bus_rdata_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
767
      p_bus_wdata_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
768
      p_bus_ben_o     : out std_ulogic_vector(03 downto 0); -- byte enable
769
      p_bus_we_o      : out std_ulogic; -- write enable
770
      p_bus_re_o      : out std_ulogic; -- read enable
771
      p_bus_cancel_o  : out std_ulogic; -- cancel current bus transaction
772
      p_bus_ack_i     : in  std_ulogic; -- bus transfer acknowledge
773
      p_bus_err_i     : in  std_ulogic  -- bus transfer error
774
    );
775
  end component;
776
 
777 2 zero_gravi
  -- Component: CPU Compressed Instructions Decompressor ------------------------------------
778
  -- -------------------------------------------------------------------------------------------
779
  component neorv32_cpu_decompressor
780
    port (
781
      -- instruction input --
782
      ci_instr16_i : in  std_ulogic_vector(15 downto 0); -- compressed instruction input
783
      -- instruction output --
784
      ci_illegal_o : out std_ulogic; -- is an illegal compressed instruction
785
      ci_instr32_o : out std_ulogic_vector(31 downto 0)  -- 32-bit decompressed instruction
786
    );
787
  end component;
788
 
789
  -- Component: Processor-internal instruction memory (IMEM) --------------------------------
790
  -- -------------------------------------------------------------------------------------------
791
  component neorv32_imem
792
    generic (
793
      IMEM_BASE      : std_ulogic_vector(31 downto 0) := x"00000000"; -- memory base address
794
      IMEM_SIZE      : natural := 4*1024; -- processor-internal instruction memory size in bytes
795
      IMEM_AS_ROM    : boolean := false;  -- implement IMEM as read-only memory?
796
      BOOTLOADER_USE : boolean := true    -- implement and use bootloader?
797
    );
798
    port (
799
      clk_i  : in  std_ulogic; -- global clock line
800
      rden_i : in  std_ulogic; -- read enable
801
      wren_i : in  std_ulogic; -- write enable
802
      ben_i  : in  std_ulogic_vector(03 downto 0); -- byte write enable
803
      upen_i : in  std_ulogic; -- update enable
804
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
805
      data_i : in  std_ulogic_vector(31 downto 0); -- data in
806
      data_o : out std_ulogic_vector(31 downto 0); -- data out
807
      ack_o  : out std_ulogic -- transfer acknowledge
808
    );
809
  end component;
810
 
811
  -- Component: Processor-internal data memory (DMEM) ---------------------------------------
812
  -- -------------------------------------------------------------------------------------------
813
  component neorv32_dmem
814
    generic (
815
      DMEM_BASE : std_ulogic_vector(31 downto 0) := x"80000000"; -- memory base address
816
      DMEM_SIZE : natural := 4*1024  -- processor-internal instruction memory size in bytes
817
    );
818
    port (
819
      clk_i  : in  std_ulogic; -- global clock line
820
      rden_i : in  std_ulogic; -- read enable
821
      wren_i : in  std_ulogic; -- write enable
822
      ben_i  : in  std_ulogic_vector(03 downto 0); -- byte write enable
823
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
824
      data_i : in  std_ulogic_vector(31 downto 0); -- data in
825
      data_o : out std_ulogic_vector(31 downto 0); -- data out
826
      ack_o  : out std_ulogic -- transfer acknowledge
827
    );
828
  end component;
829
 
830
  -- Component: Processor-internal bootloader ROM (BOOTROM) ---------------------------------
831
  -- -------------------------------------------------------------------------------------------
832
  component neorv32_boot_rom
833
    port (
834
      clk_i  : in  std_ulogic; -- global clock line
835
      rden_i : in  std_ulogic; -- read enable
836
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
837
      data_o : out std_ulogic_vector(31 downto 0); -- data out
838
      ack_o  : out std_ulogic -- transfer acknowledge
839
    );
840
  end component;
841
 
842
  -- Component: Machine System Timer (mtime) ------------------------------------------------
843
  -- -------------------------------------------------------------------------------------------
844
  component neorv32_mtime
845
    port (
846
      -- host access --
847
      clk_i     : in  std_ulogic; -- global clock line
848 4 zero_gravi
      rstn_i    : in  std_ulogic := '0'; -- global reset, low-active, async
849 2 zero_gravi
      addr_i    : in  std_ulogic_vector(31 downto 0); -- address
850
      rden_i    : in  std_ulogic; -- read enable
851
      wren_i    : in  std_ulogic; -- write enable
852
      data_i    : in  std_ulogic_vector(31 downto 0); -- data in
853
      data_o    : out std_ulogic_vector(31 downto 0); -- data out
854
      ack_o     : out std_ulogic; -- transfer acknowledge
855 11 zero_gravi
      -- time output for CPU --
856
      time_o    : out std_ulogic_vector(63 downto 0); -- current system time
857 2 zero_gravi
      -- interrupt --
858
      irq_o     : out std_ulogic  -- interrupt request
859
    );
860
  end component;
861
 
862
  -- Component: General Purpose Input/Output Port (GPIO) ------------------------------------
863
  -- -------------------------------------------------------------------------------------------
864
  component neorv32_gpio
865
    port (
866
      -- host access --
867
      clk_i  : in  std_ulogic; -- global clock line
868
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
869
      rden_i : in  std_ulogic; -- read enable
870
      wren_i : in  std_ulogic; -- write enable
871
      data_i : in  std_ulogic_vector(31 downto 0); -- data in
872
      data_o : out std_ulogic_vector(31 downto 0); -- data out
873
      ack_o  : out std_ulogic; -- transfer acknowledge
874
      -- parallel io --
875 22 zero_gravi
      gpio_o : out std_ulogic_vector(31 downto 0);
876
      gpio_i : in  std_ulogic_vector(31 downto 0);
877 2 zero_gravi
      -- interrupt --
878
      irq_o  : out std_ulogic
879
    );
880
  end component;
881
 
882
  -- Component: Watchdog Timer (WDT) --------------------------------------------------------
883
  -- -------------------------------------------------------------------------------------------
884
  component neorv32_wdt
885
    port (
886
      -- host access --
887
      clk_i       : in  std_ulogic; -- global clock line
888
      rstn_i      : in  std_ulogic; -- global reset line, low-active
889
      rden_i      : in  std_ulogic; -- read enable
890
      wren_i      : in  std_ulogic; -- write enable
891
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
892
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
893
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
894
      ack_o       : out std_ulogic; -- transfer acknowledge
895
      -- clock generator --
896
      clkgen_en_o : out std_ulogic; -- enable clock generator
897
      clkgen_i    : in  std_ulogic_vector(07 downto 0);
898
      -- timeout event --
899
      irq_o       : out std_ulogic; -- timeout IRQ
900
      rstn_o      : out std_ulogic  -- timeout reset, low_active, use it as async!
901
    );
902
  end component;
903
 
904
  -- Component: Universal Asynchronous Receiver and Transmitter (UART) ----------------------
905
  -- -------------------------------------------------------------------------------------------
906
  component neorv32_uart
907
    port (
908
      -- host access --
909
      clk_i       : in  std_ulogic; -- global clock line
910
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
911
      rden_i      : in  std_ulogic; -- read enable
912
      wren_i      : in  std_ulogic; -- write enable
913
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
914
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
915
      ack_o       : out std_ulogic; -- transfer acknowledge
916
      -- clock generator --
917
      clkgen_en_o : out std_ulogic; -- enable clock generator
918
      clkgen_i    : in  std_ulogic_vector(07 downto 0);
919
      -- com lines --
920
      uart_txd_o  : out std_ulogic;
921
      uart_rxd_i  : in  std_ulogic;
922
      -- interrupts --
923
      uart_irq_o  : out std_ulogic  -- uart rx/tx interrupt
924
    );
925
  end component;
926
 
927
  -- Component: Serial Peripheral Interface (SPI) -------------------------------------------
928
  -- -------------------------------------------------------------------------------------------
929
  component neorv32_spi
930
    port (
931
      -- host access --
932
      clk_i       : in  std_ulogic; -- global clock line
933
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
934
      rden_i      : in  std_ulogic; -- read enable
935
      wren_i      : in  std_ulogic; -- write enable
936
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
937
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
938
      ack_o       : out std_ulogic; -- transfer acknowledge
939
      -- clock generator --
940
      clkgen_en_o : out std_ulogic; -- enable clock generator
941
      clkgen_i    : in  std_ulogic_vector(07 downto 0);
942
      -- com lines --
943 6 zero_gravi
      spi_sck_o   : out std_ulogic; -- SPI serial clock
944
      spi_sdo_o   : out std_ulogic; -- controller data out, peripheral data in
945
      spi_sdi_i   : in  std_ulogic; -- controller data in, peripheral data out
946 2 zero_gravi
      spi_csn_o   : out std_ulogic_vector(07 downto 0); -- SPI CS
947
      -- interrupt --
948
      spi_irq_o   : out std_ulogic -- transmission done interrupt
949
    );
950
  end component;
951
 
952
  -- Component: Two-Wire Interface (TWI) ----------------------------------------------------
953
  -- -------------------------------------------------------------------------------------------
954
  component neorv32_twi
955
    port (
956
      -- host access --
957
      clk_i       : in  std_ulogic; -- global clock line
958
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
959
      rden_i      : in  std_ulogic; -- read enable
960
      wren_i      : in  std_ulogic; -- write enable
961
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
962
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
963
      ack_o       : out std_ulogic; -- transfer acknowledge
964
      -- clock generator --
965
      clkgen_en_o : out std_ulogic; -- enable clock generator
966
      clkgen_i    : in  std_ulogic_vector(07 downto 0);
967
      -- com lines --
968
      twi_sda_io  : inout std_logic; -- serial data line
969
      twi_scl_io  : inout std_logic; -- serial clock line
970
      -- interrupt --
971
      twi_irq_o   : out std_ulogic -- transfer done IRQ
972
    );
973
  end component;
974
 
975
  -- Component: Pulse-Width Modulation Controller (PWM) -------------------------------------
976
  -- -------------------------------------------------------------------------------------------
977
  component neorv32_pwm
978
    port (
979
      -- host access --
980
      clk_i       : in  std_ulogic; -- global clock line
981
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
982
      rden_i      : in  std_ulogic; -- read enable
983
      wren_i      : in  std_ulogic; -- write enable
984
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
985
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
986
      ack_o       : out std_ulogic; -- transfer acknowledge
987
      -- clock generator --
988
      clkgen_en_o : out std_ulogic; -- enable clock generator
989
      clkgen_i    : in  std_ulogic_vector(07 downto 0);
990
      -- pwm output channels --
991
      pwm_o       : out std_ulogic_vector(03 downto 0)
992
    );
993
  end component;
994
 
995
  -- Component: True Random Number Generator (TRNG) -----------------------------------------
996
  -- -------------------------------------------------------------------------------------------
997
  component neorv32_trng
998
    port (
999
      -- host access --
1000
      clk_i  : in  std_ulogic; -- global clock line
1001
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
1002
      rden_i : in  std_ulogic; -- read enable
1003
      wren_i : in  std_ulogic; -- write enable
1004
      data_i : in  std_ulogic_vector(31 downto 0); -- data in
1005
      data_o : out std_ulogic_vector(31 downto 0); -- data out
1006
      ack_o  : out std_ulogic  -- transfer acknowledge
1007
    );
1008
  end component;
1009
 
1010
  -- Component: Wishbone Bus Gateway (WISHBONE) ---------------------------------------------
1011
  -- -------------------------------------------------------------------------------------------
1012
  component neorv32_wishbone
1013
    generic (
1014
      INTERFACE_REG_STAGES : natural := 2; -- number of interface register stages (0,1,2)
1015
      -- Memory configuration: Instruction memory --
1016
      MEM_ISPACE_BASE      : std_ulogic_vector(31 downto 0) := x"00000000"; -- base address of instruction memory space
1017
      MEM_ISPACE_SIZE      : natural := 8*1024; -- total size of instruction memory space in byte
1018
      MEM_INT_IMEM_USE     : boolean := true;   -- implement processor-internal instruction memory
1019
      MEM_INT_IMEM_SIZE    : natural := 8*1024; -- size of processor-internal instruction memory in bytes
1020
      -- Memory configuration: Data memory --
1021
      MEM_DSPACE_BASE      : std_ulogic_vector(31 downto 0) := x"80000000"; -- base address of data memory space
1022
      MEM_DSPACE_SIZE      : natural := 4*1024; -- total size of data memory space in byte
1023
      MEM_INT_DMEM_USE     : boolean := true;   -- implement processor-internal data memory
1024
      MEM_INT_DMEM_SIZE    : natural := 4*1024  -- size of processor-internal data memory in bytes
1025
    );
1026
    port (
1027
      -- global control --
1028
      clk_i    : in  std_ulogic; -- global clock line
1029
      rstn_i   : in  std_ulogic; -- global reset line, low-active
1030
      -- host access --
1031
      addr_i   : in  std_ulogic_vector(31 downto 0); -- address
1032
      rden_i   : in  std_ulogic; -- read enable
1033
      wren_i   : in  std_ulogic; -- write enable
1034
      ben_i    : in  std_ulogic_vector(03 downto 0); -- byte write enable
1035
      data_i   : in  std_ulogic_vector(31 downto 0); -- data in
1036
      data_o   : out std_ulogic_vector(31 downto 0); -- data out
1037 11 zero_gravi
      cancel_i : in  std_ulogic; -- cancel current bus transaction
1038 2 zero_gravi
      ack_o    : out std_ulogic; -- transfer acknowledge
1039
      err_o    : out std_ulogic; -- transfer error
1040
      -- wishbone interface --
1041
      wb_adr_o : out std_ulogic_vector(31 downto 0); -- address
1042
      wb_dat_i : in  std_ulogic_vector(31 downto 0); -- read data
1043
      wb_dat_o : out std_ulogic_vector(31 downto 0); -- write data
1044
      wb_we_o  : out std_ulogic; -- read/write
1045
      wb_sel_o : out std_ulogic_vector(03 downto 0); -- byte enable
1046
      wb_stb_o : out std_ulogic; -- strobe
1047
      wb_cyc_o : out std_ulogic; -- valid cycle
1048
      wb_ack_i : in  std_ulogic; -- transfer acknowledge
1049
      wb_err_i : in  std_ulogic  -- transfer error
1050
    );
1051
  end component;
1052
 
1053 4 zero_gravi
  ---- Component: Dummy Device with SIM Output (DEVNULL) -------------------------------------
1054
  ---- -------------------------------------------------------------------------------------------
1055 3 zero_gravi
  component neorv32_devnull
1056
    port (
1057
      -- host access --
1058
      clk_i  : in  std_ulogic; -- global clock line
1059
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
1060
      rden_i : in  std_ulogic; -- read enable
1061
      wren_i : in  std_ulogic; -- write enable
1062
      data_i : in  std_ulogic_vector(31 downto 0); -- data in
1063
      data_o : out std_ulogic_vector(31 downto 0); -- data out
1064
      ack_o  : out std_ulogic  -- transfer acknowledge
1065
    );
1066
  end component;
1067
 
1068 12 zero_gravi
  ---- Component: System Configuration Information Memory (SYSINFO) ---------------------------
1069
  ---- -------------------------------------------------------------------------------------------
1070
  component neorv32_sysinfo
1071
    generic (
1072
      -- General --
1073
      CLOCK_FREQUENCY   : natural := 0;      -- clock frequency of clk_i in Hz
1074
      BOOTLOADER_USE    : boolean := true;   -- implement processor-internal bootloader?
1075
      USER_CODE         : std_ulogic_vector(31 downto 0) := x"00000000"; -- custom user code
1076
      -- Memory configuration: Instruction memory --
1077
      MEM_ISPACE_BASE   : std_ulogic_vector(31 downto 0) := x"00000000"; -- base address of instruction memory space
1078
      MEM_ISPACE_SIZE   : natural := 8*1024; -- total size of instruction memory space in byte
1079
      MEM_INT_IMEM_USE  : boolean := true;   -- implement processor-internal instruction memory
1080
      MEM_INT_IMEM_SIZE : natural := 8*1024; -- size of processor-internal instruction memory in bytes
1081
      MEM_INT_IMEM_ROM  : boolean := false;  -- implement processor-internal instruction memory as ROM
1082
      -- Memory configuration: Data memory --
1083
      MEM_DSPACE_BASE   : std_ulogic_vector(31 downto 0) := x"80000000"; -- base address of data memory space
1084
      MEM_DSPACE_SIZE   : natural := 4*1024; -- total size of data memory space in byte
1085
      MEM_INT_DMEM_USE  : boolean := true;   -- implement processor-internal data memory
1086
      MEM_INT_DMEM_SIZE : natural := 4*1024; -- size of processor-internal data memory in bytes
1087
      -- Memory configuration: External memory interface --
1088
      MEM_EXT_USE       : boolean := false;  -- implement external memory bus interface?
1089
      -- Processor peripherals --
1090
      IO_GPIO_USE       : boolean := true;   -- implement general purpose input/output port unit (GPIO)?
1091
      IO_MTIME_USE      : boolean := true;   -- implement machine system timer (MTIME)?
1092
      IO_UART_USE       : boolean := true;   -- implement universal asynchronous receiver/transmitter (UART)?
1093
      IO_SPI_USE        : boolean := true;   -- implement serial peripheral interface (SPI)?
1094
      IO_TWI_USE        : boolean := true;   -- implement two-wire interface (TWI)?
1095
      IO_PWM_USE        : boolean := true;   -- implement pulse-width modulation unit (PWM)?
1096
      IO_WDT_USE        : boolean := true;   -- implement watch dog timer (WDT)?
1097
      IO_TRNG_USE       : boolean := true;   -- implement true random number generator (TRNG)?
1098
      IO_DEVNULL_USE    : boolean := true    -- implement dummy device (DEVNULL)?
1099
    );
1100
    port (
1101
      -- host access --
1102
      clk_i  : in  std_ulogic; -- global clock line
1103
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
1104
      rden_i : in  std_ulogic; -- read enable
1105
      data_o : out std_ulogic_vector(31 downto 0); -- data out
1106
      ack_o  : out std_ulogic  -- transfer acknowledge
1107
    );
1108
  end component;
1109
 
1110 2 zero_gravi
end neorv32_package;
1111
 
1112
package body neorv32_package is
1113
 
1114
  -- Function: Minimal required bit width ---------------------------------------------------
1115
  -- -------------------------------------------------------------------------------------------
1116
  function index_size_f(input : natural) return natural is
1117
  begin
1118
    for i in 0 to natural'high loop
1119
      if (2**i >= input) then
1120
        return i;
1121
      end if;
1122
    end loop; -- i
1123
    return 0;
1124
  end function index_size_f;
1125
 
1126
  -- Function: Conditional select natural ---------------------------------------------------
1127
  -- -------------------------------------------------------------------------------------------
1128
  function cond_sel_natural_f(cond : boolean; val_t : natural; val_f : natural) return natural is
1129
  begin
1130
    if (cond = true) then
1131
      return val_t;
1132
    else
1133
      return val_f;
1134
    end if;
1135
  end function cond_sel_natural_f;
1136
 
1137
  -- Function: Conditional select std_ulogic_vector -----------------------------------------
1138
  -- -------------------------------------------------------------------------------------------
1139
  function cond_sel_stdulogicvector_f(cond : boolean; val_t : std_ulogic_vector; val_f : std_ulogic_vector) return std_ulogic_vector is
1140
  begin
1141
    if (cond = true) then
1142
      return val_t;
1143
    else
1144
      return val_f;
1145
    end if;
1146
  end function cond_sel_stdulogicvector_f;
1147
 
1148
  -- Function: Convert BOOL to STD_ULOGIC ---------------------------------------------------
1149
  -- -------------------------------------------------------------------------------------------
1150
  function bool_to_ulogic_f(cond : boolean) return std_ulogic is
1151
  begin
1152
    if (cond = true) then
1153
      return '1';
1154
    else
1155
      return '0';
1156
    end if;
1157
  end function bool_to_ulogic_f;
1158
 
1159
  -- Function: OR all bits ------------------------------------------------------------------
1160
  -- -------------------------------------------------------------------------------------------
1161
  function or_all_f(a : std_ulogic_vector) return std_ulogic is
1162
    variable tmp_v : std_ulogic;
1163
  begin
1164
    tmp_v := a(a'low);
1165 15 zero_gravi
    if (a'low < a'high) then -- not null range?
1166
      for i in a'low+1 to a'high loop
1167
        tmp_v := tmp_v or a(i);
1168
      end loop; -- i
1169
    end if;
1170 2 zero_gravi
    return tmp_v;
1171
  end function or_all_f;
1172
 
1173
  -- Function: AND all bits -----------------------------------------------------------------
1174
  -- -------------------------------------------------------------------------------------------
1175
  function and_all_f(a : std_ulogic_vector) return std_ulogic is
1176
    variable tmp_v : std_ulogic;
1177
  begin
1178
    tmp_v := a(a'low);
1179 15 zero_gravi
    if (a'low < a'high) then -- not null range?
1180
      for i in a'low+1 to a'high loop
1181
        tmp_v := tmp_v and a(i);
1182
      end loop; -- i
1183
    end if;
1184 2 zero_gravi
    return tmp_v;
1185
  end function and_all_f;
1186
 
1187
  -- Function: XOR all bits -----------------------------------------------------------------
1188
  -- -------------------------------------------------------------------------------------------
1189
  function xor_all_f(a : std_ulogic_vector) return std_ulogic is
1190
    variable tmp_v : std_ulogic;
1191
  begin
1192
    tmp_v := a(a'low);
1193 15 zero_gravi
    if (a'low < a'high) then -- not null range?
1194
      for i in a'low+1 to a'high loop
1195
        tmp_v := tmp_v xor a(i);
1196
      end loop; -- i
1197
    end if;
1198 2 zero_gravi
    return tmp_v;
1199
  end function xor_all_f;
1200
 
1201
  -- Function: XNOR all bits ----------------------------------------------------------------
1202
  -- -------------------------------------------------------------------------------------------
1203
  function xnor_all_f(a : std_ulogic_vector) return std_ulogic is
1204
    variable tmp_v : std_ulogic;
1205
  begin
1206
    tmp_v := a(a'low);
1207 15 zero_gravi
    if (a'low < a'high) then -- not null range?
1208
      for i in a'low+1 to a'high loop
1209
        tmp_v := tmp_v xnor a(i);
1210
      end loop; -- i
1211
    end if;
1212 2 zero_gravi
    return tmp_v;
1213
  end function xnor_all_f;
1214
 
1215 6 zero_gravi
  -- Function: Convert to hex char ----------------------------------------------------------
1216
  -- -------------------------------------------------------------------------------------------
1217
  function to_hexchar_f(input : std_ulogic_vector(3 downto 0)) return character is
1218
    variable output_v : character;
1219
  begin
1220
    case input is
1221 7 zero_gravi
      when x"0"   => output_v := '0';
1222
      when x"1"   => output_v := '1';
1223
      when x"2"   => output_v := '2';
1224
      when x"3"   => output_v := '3';
1225
      when x"4"   => output_v := '4';
1226
      when x"5"   => output_v := '5';
1227
      when x"6"   => output_v := '6';
1228
      when x"7"   => output_v := '7';
1229
      when x"8"   => output_v := '8';
1230
      when x"9"   => output_v := '9';
1231
      when x"a"   => output_v := 'a';
1232
      when x"b"   => output_v := 'b';
1233
      when x"c"   => output_v := 'c';
1234
      when x"d"   => output_v := 'd';
1235
      when x"e"   => output_v := 'e';
1236
      when x"f"   => output_v := 'f';
1237 6 zero_gravi
      when others => output_v := '?';
1238
    end case;
1239
    return output_v;
1240
  end function to_hexchar_f;
1241
 
1242 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.