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

Subversion Repositories neorv32

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

Go to most recent revision | Details | Compare with Previous | View Log

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