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

Subversion Repositories neorv32

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

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