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

Subversion Repositories neorv32

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

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

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