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

Subversion Repositories neorv32

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

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