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

Subversion Repositories neorv32

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

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