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

Subversion Repositories neorv32

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

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