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

Subversion Repositories neorv32

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

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 6 zero_gravi
  constant hw_version_c : std_ulogic_vector(31 downto 0) := x"01000000"; -- no touchy!
45 2 zero_gravi
 
46
  -- Internal Functions ---------------------------------------------------------------------
47
  -- -------------------------------------------------------------------------------------------
48
  function index_size_f(input : natural) return natural;
49
  function cond_sel_natural_f(cond : boolean; val_t : natural; val_f : natural) return natural;
50
  function cond_sel_stdulogicvector_f(cond : boolean; val_t : std_ulogic_vector; val_f : std_ulogic_vector) return std_ulogic_vector;
51
  function bool_to_ulogic_f(cond : boolean) return std_ulogic;
52 4 zero_gravi
  function or_all_f(  a : std_ulogic_vector) return std_ulogic;
53
  function and_all_f( a : std_ulogic_vector) return std_ulogic;
54
  function xor_all_f( a : std_ulogic_vector) return std_ulogic;
55 2 zero_gravi
  function xnor_all_f(a : std_ulogic_vector) return std_ulogic;
56 6 zero_gravi
  function to_hexchar_f(input : std_ulogic_vector(3 downto 0)) return character;
57 2 zero_gravi
 
58
  -- Processor-internal Address Space Layout ------------------------------------------------
59
  -- -------------------------------------------------------------------------------------------
60
  -- Instruction Memory & Data Memory --
61
  -- => configured via top's generics
62
 
63
  -- Bootloader ROM --
64
  constant boot_base_c          : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFF0000"; -- bootloader base address, fixed!
65
  constant boot_size_c          : natural := 4*1024; -- bytes
66
  constant boot_max_size_c      : natural := 32*1024; -- bytes, fixed!
67
 
68
  -- IO: Peripheral Devices ("IO") Area --
69
  -- Control register(s) (including the device-enable) should be located at the base address of each device
70
  constant io_base_c            : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFF80";
71
  constant io_size_c            : natural := 32*4; -- bytes, fixed!
72
 
73
  -- General Purpose Input/Output Unit (GPIO) --
74
  constant gpio_base_c          : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFF80"; -- base address, fixed!
75
  constant gpio_size_c          : natural := 2*4; -- bytes, fixed!
76
  constant gpio_in_addr_c       : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(gpio_base_c) + x"00000000");
77
  constant gpio_out_addr_c      : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(gpio_base_c) + x"00000004");
78
 
79
  -- Core-Local Interrupt Controller (CLIC) --
80
  constant clic_base_c          : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFF88"; -- base address, fixed!
81
  constant clic_size_c          : natural := 1*4; -- bytes, fixed!
82
  constant clic_ctrl_addr_c     : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(clic_base_c) + x"00000000");
83
 
84
  -- Watch Dog Timer (WDT) --
85
  constant wdt_base_c           : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFF8C"; -- base address, fixed!
86
  constant wdt_size_c           : natural := 1*4; -- bytes, fixed!
87
  constant wdt_ctrl_addr_c      : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(wdt_base_c) + x"00000000");
88
 
89
  -- Machine System Timer (MTIME) --
90
  constant mtime_base_c         : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFF90"; -- base address, fixed!
91
  constant mtime_size_c         : natural := 4*4; -- bytes, fixed!
92
  constant mtime_time_lo_addr_c : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(mtime_base_c) + x"00000000");
93
  constant mtime_time_hi_addr_c : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(mtime_base_c) + x"00000004");
94
  constant mtime_cmp_lo_addr_c  : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(mtime_base_c) + x"00000008");
95
  constant mtime_cmp_hi_addr_c  : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(mtime_base_c) + x"0000000C");
96
 
97
  -- Universal Asynchronous Receiver/Transmitter (UART) --
98
  constant uart_base_c          : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFFA0"; -- base address, fixed!
99
  constant uart_size_c          : natural := 2*4; -- bytes, fixed!
100
  constant uart_ctrl_addr_c     : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(uart_base_c) + x"00000000");
101
  constant uart_rtx_addr_c      : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(uart_base_c) + x"00000004");
102
 
103
  -- Serial Peripheral Interface (SPI) --
104
  constant spi_base_c           : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFFA8"; -- base address, fixed!
105
  constant spi_size_c           : natural := 2*4; -- bytes, fixed!
106
  constant spi_ctrl_addr_c      : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(spi_base_c) + x"00000000");
107
  constant spi_rtx_addr_c       : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(spi_base_c) + x"00000004");
108
 
109
  -- Two Wire Interface (TWI) --
110
  constant twi_base_c           : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFFB0"; -- base address, fixed!
111
  constant twi_size_c           : natural := 2*4; -- bytes, fixed!
112
  constant twi_ctrl_addr_c      : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(twi_base_c) + x"00000000");
113
  constant twi_rtx_addr_c       : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(twi_base_c) + x"00000004");
114
 
115
  -- Pulse-Width Modulation Controller (PWM) --
116
  constant pwm_base_c           : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFFB8"; -- base address, fixed!
117
  constant pwm_size_c           : natural := 2*4; -- bytes, fixed!
118
  constant pwm_ctrl_addr_c      : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(pwm_base_c) + x"00000000");
119
  constant pwm_duty_addr_c      : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(pwm_base_c) + x"00000004");
120
 
121
  -- True Random Number generator (TRNG) --
122
  constant trng_base_c          : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFFC0"; -- base address, fixed!
123
  constant trng_size_c          : natural := 2*4; -- bytes, fixed!
124
  constant trng_ctrl_addr_c     : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(trng_base_c) + x"00000000");
125
  constant trng_data_addr_c     : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(trng_base_c) + x"00000004");
126
 
127
  -- RESERVED --
128
--constant ???_base_c           : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFFC8"; -- base address, fixed!
129 3 zero_gravi
--constant ???_size_c           : natural := 13*4; -- bytes, fixed!
130 2 zero_gravi
 
131 3 zero_gravi
  -- Dummy Device (with SIM output) (DEVNULL) --
132
  constant devnull_base_c       : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFFFC"; -- base address, fixed!
133
  constant devnull_size_c       : natural := 1*4; -- bytes, fixed!
134
  constant devnull_data_addr_c  : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(devnull_base_c) + x"00000000");
135
 
136 2 zero_gravi
  -- Main Control Bus -----------------------------------------------------------------------
137
  -- -------------------------------------------------------------------------------------------
138
  -- register file --
139
  constant ctrl_rf_in_mux_lsb_c   : natural :=  0; -- input source select lsb (00=ALU, 01=MEM)
140
  constant ctrl_rf_in_mux_msb_c   : natural :=  1; -- input source select msb (10=PC,  11=CSR)
141
  constant ctrl_rf_rs1_adr0_c     : natural :=  2; -- source register 1 address bit 0
142
  constant ctrl_rf_rs1_adr1_c     : natural :=  3; -- source register 1 address bit 1
143
  constant ctrl_rf_rs1_adr2_c     : natural :=  4; -- source register 1 address bit 2
144
  constant ctrl_rf_rs1_adr3_c     : natural :=  5; -- source register 1 address bit 3
145
  constant ctrl_rf_rs1_adr4_c     : natural :=  6; -- source register 1 address bit 4
146
  constant ctrl_rf_rs2_adr0_c     : natural :=  7; -- source register 2 address bit 0
147
  constant ctrl_rf_rs2_adr1_c     : natural :=  8; -- source register 2 address bit 1
148
  constant ctrl_rf_rs2_adr2_c     : natural :=  9; -- source register 2 address bit 2
149
  constant ctrl_rf_rs2_adr3_c     : natural := 10; -- source register 2 address bit 3
150
  constant ctrl_rf_rs2_adr4_c     : natural := 11; -- source register 2 address bit 4
151
  constant ctrl_rf_rd_adr0_c      : natural := 12; -- destiantion register address bit 0
152
  constant ctrl_rf_rd_adr1_c      : natural := 13; -- destiantion register address bit 1
153
  constant ctrl_rf_rd_adr2_c      : natural := 14; -- destiantion register address bit 2
154
  constant ctrl_rf_rd_adr3_c      : natural := 15; -- destiantion register address bit 3
155
  constant ctrl_rf_rd_adr4_c      : natural := 16; -- destiantion register address bit 4
156
  constant ctrl_rf_wb_en_c        : natural := 17; -- write back enable
157
  constant ctrl_rf_clear_rs1_c    : natural := 18; -- force rs1=r0
158
  constant ctrl_rf_clear_rs2_c    : natural := 19; -- force rs2=r0
159
  -- alu --
160
  constant ctrl_alu_cmd0_c        : natural := 20; -- ALU command bit 0
161
  constant ctrl_alu_cmd1_c        : natural := 21; -- ALU command bit 1
162
  constant ctrl_alu_cmd2_c        : natural := 22; -- ALU command bit 2
163
  constant ctrl_alu_opa_mux_lsb_c : natural := 23; -- operand A select lsb (00=rs1, 01=PC)
164 6 zero_gravi
  constant ctrl_alu_opa_mux_msb_c : natural := 24; -- operand A select msb (10=CSR, 11=?)
165
  constant ctrl_alu_opb_mux_lsb_c : natural := 25; -- operand B select lsb (00=rs2, 01=IMM)
166
  constant ctrl_alu_opb_mux_msb_c : natural := 26; -- operand B select msb (10=rs1, 11=?)
167 2 zero_gravi
  constant ctrl_alu_opc_mux_c     : natural := 27; -- operand C select (0=IMM, 1=rs2)
168
  constant ctrl_alu_unsigned_c    : natural := 28; -- is unsigned ALU operation
169
  constant ctrl_alu_shift_dir_c   : natural := 29; -- shift direction (0=left, 1=right)
170
  constant ctrl_alu_shift_ar_c    : natural := 30; -- is arithmetic shift
171
  -- bus interface --
172
  constant ctrl_bus_size_lsb_c    : natural := 31; -- transfer size lsb (00=byte, 01=half-word)
173
  constant ctrl_bus_size_msb_c    : natural := 32; -- transfer size msb (10=word, 11=?)
174
  constant ctrl_bus_rd_c          : natural := 33; -- read data request
175
  constant ctrl_bus_wr_c          : natural := 34; -- write data request
176 4 zero_gravi
  constant ctrl_bus_if_c          : natural := 35; -- instruction fetch request (1: output PC, 0: output MAR)
177 2 zero_gravi
  constant ctrl_bus_mar_we_c      : natural := 36; -- memory address register write enable
178
  constant ctrl_bus_mdo_we_c      : natural := 37; -- memory data out register write enable
179
  constant ctrl_bus_mdi_we_c      : natural := 38; -- memory data in register write enable
180
  constant ctrl_bus_unsigned_c    : natural := 39; -- is unsigned load
181
  -- co-processor --
182 6 zero_gravi
  constant ctrl_cp_use_c          : natural := 40; -- is cp operation
183
  constant ctrl_cp_id_lsb_c       : natural := 41; -- cp select lsb
184
  constant ctrl_cp_id_msb_c       : natural := 42; -- cp select msb
185
  constant ctrl_cp_cmd0_c         : natural := 43; -- cp command bit 0
186
  constant ctrl_cp_cmd1_c         : natural := 44; -- cp command bit 1
187
  constant ctrl_cp_cmd2_c         : natural := 45; -- cp command bit 2
188
  -- system --
189
  constant ctrl_sys_c_ext_en_c    : natural := 46; -- CPU C extension enabled
190
  constant ctrl_sys_m_ext_en_c    : natural := 47; -- CPU M extension enabled
191 2 zero_gravi
  -- control bus size --
192 6 zero_gravi
  constant ctrl_width_c           : natural := 48; -- control bus size
193 2 zero_gravi
 
194
  -- ALU Comparator Bus ---------------------------------------------------------------------
195
  -- -------------------------------------------------------------------------------------------
196
  constant alu_cmp_equal_c : natural := 0;
197 6 zero_gravi
  constant alu_cmp_less_c  : natural := 1; -- for signed and unsigned comparisons
198 2 zero_gravi
 
199
  -- RISC-V Opcode Layout -------------------------------------------------------------------
200
  -- -------------------------------------------------------------------------------------------
201
  constant instr_opcode_lsb_c  : natural :=  0; -- opcode bit 0
202
  constant instr_opcode_msb_c  : natural :=  6; -- opcode bit 6
203
  constant instr_rd_lsb_c      : natural :=  7; -- destination register address bit 0
204
  constant instr_rd_msb_c      : natural := 11; -- destination register address bit 4
205
  constant instr_funct3_lsb_c  : natural := 12; -- funct3 bit 0
206
  constant instr_funct3_msb_c  : natural := 14; -- funct3 bit 2
207
  constant instr_rs1_lsb_c     : natural := 15; -- source register 1 address bit 0
208
  constant instr_rs1_msb_c     : natural := 19; -- source register 1 address bit 4
209
  constant instr_rs2_lsb_c     : natural := 20; -- source register 2 address bit 0
210
  constant instr_rs2_msb_c     : natural := 24; -- source register 2 address bit 4
211
  constant instr_funct7_lsb_c  : natural := 25; -- funct7 bit 0
212
  constant instr_funct7_msb_c  : natural := 31; -- funct7 bit 6
213
  constant instr_funct12_lsb_c : natural := 20; -- funct12 bit 0
214
  constant instr_funct12_msb_c : natural := 31; -- funct12 bit 11
215
  constant instr_imm12_lsb_c   : natural := 20; -- immediate12 bit 0
216
  constant instr_imm12_msb_c   : natural := 31; -- immediate12 bit 11
217
  constant instr_imm20_lsb_c   : natural := 12; -- immediate20 bit 0
218
  constant instr_imm20_msb_c   : natural := 31; -- immediate20 bit 21
219
  constant instr_csr_id_lsb_c  : natural := 20; -- csr select bit 0
220
  constant instr_csr_id_msb_c  : natural := 31; -- csr select bit 11
221
 
222
  -- RISC-V Opcodes -------------------------------------------------------------------------
223
  -- -------------------------------------------------------------------------------------------
224
  -- alu --
225
  constant opcode_lui_c    : std_ulogic_vector(6 downto 0) := "0110111"; -- load upper immediate
226
  constant opcode_auipc_c  : std_ulogic_vector(6 downto 0) := "0010111"; -- add upper immediate to PC
227
  constant opcode_alui_c   : std_ulogic_vector(6 downto 0) := "0010011"; -- ALU operation with immediate (operation via funct3 and funct7)
228
  constant opcode_alu_c    : std_ulogic_vector(6 downto 0) := "0110011"; -- ALU operation (operation via funct3 and funct7)
229
  -- control flow --
230
  constant opcode_jal_c    : std_ulogic_vector(6 downto 0) := "1101111"; -- jump and link
231
  constant opcode_jalr_c   : std_ulogic_vector(6 downto 0) := "1100111"; -- jump and register
232
  constant opcode_branch_c : std_ulogic_vector(6 downto 0) := "1100011"; -- branch (condition set via funct3)
233
  -- memory access --
234
  constant opcode_load_c   : std_ulogic_vector(6 downto 0) := "0000011"; -- load (data type via funct3)
235
  constant opcode_store_c  : std_ulogic_vector(6 downto 0) := "0100011"; -- store (data type via funct3)
236
  -- system/csr --
237
  constant opcode_fence_c  : std_ulogic_vector(6 downto 0) := "0001111"; -- fence
238
  constant opcode_syscsr_c : std_ulogic_vector(6 downto 0) := "1110011"; -- system/csr access (type via funct3)
239
 
240
  -- RISC-V Funct3 --------------------------------------------------------------------------
241
  -- -------------------------------------------------------------------------------------------
242
  -- control flow --
243
  constant funct3_beq_c    : std_ulogic_vector(2 downto 0) := "000"; -- branch if equal
244
  constant funct3_bne_c    : std_ulogic_vector(2 downto 0) := "001"; -- branch if not equal
245
  constant funct3_blt_c    : std_ulogic_vector(2 downto 0) := "100"; -- branch if less than
246
  constant funct3_bge_c    : std_ulogic_vector(2 downto 0) := "101"; -- branch if greater than or equal
247
  constant funct3_bltu_c   : std_ulogic_vector(2 downto 0) := "110"; -- branch if less than (unsigned)
248
  constant funct3_bgeu_c   : std_ulogic_vector(2 downto 0) := "111"; -- branch if greater than or equal (unsigned)
249
  -- memory access --
250
  constant funct3_lb_c     : std_ulogic_vector(2 downto 0) := "000"; -- load byte
251
  constant funct3_lh_c     : std_ulogic_vector(2 downto 0) := "001"; -- load half word
252
  constant funct3_lw_c     : std_ulogic_vector(2 downto 0) := "010"; -- load word
253
  constant funct3_lbu_c    : std_ulogic_vector(2 downto 0) := "100"; -- load byte (unsigned)
254
  constant funct3_lhu_c    : std_ulogic_vector(2 downto 0) := "101"; -- load half word (unsigned)
255
  constant funct3_sb_c     : std_ulogic_vector(2 downto 0) := "000"; -- store byte
256
  constant funct3_sh_c     : std_ulogic_vector(2 downto 0) := "001"; -- store half word
257
  constant funct3_sw_c     : std_ulogic_vector(2 downto 0) := "010"; -- store word
258
  -- alu --
259
  constant funct3_subadd_c : std_ulogic_vector(2 downto 0) := "000"; -- sub/add via funct7
260
  constant funct3_sll_c    : std_ulogic_vector(2 downto 0) := "001"; -- shift logical left
261
  constant funct3_slt_c    : std_ulogic_vector(2 downto 0) := "010"; -- set on less
262
  constant funct3_sltu_c   : std_ulogic_vector(2 downto 0) := "011"; -- set on less unsigned
263
  constant funct3_xor_c    : std_ulogic_vector(2 downto 0) := "100"; -- xor
264
  constant funct3_sr_c     : std_ulogic_vector(2 downto 0) := "101"; -- shift right via funct7
265
  constant funct3_or_c     : std_ulogic_vector(2 downto 0) := "110"; -- or
266
  constant funct3_and_c    : std_ulogic_vector(2 downto 0) := "111"; -- and
267
  -- system/csr --
268
  constant funct3_env_c    : std_ulogic_vector(2 downto 0) := "000"; -- ecall, ebreak, mret, wfi
269
  constant funct3_csrrw_c  : std_ulogic_vector(2 downto 0) := "001"; -- atomic r/w
270
  constant funct3_csrrs_c  : std_ulogic_vector(2 downto 0) := "010"; -- atomic read & set bit
271
  constant funct3_csrrc_c  : std_ulogic_vector(2 downto 0) := "011"; -- atomic read & clear bit
272
  --
273
  constant funct3_csrrwi_c : std_ulogic_vector(2 downto 0) := "101"; -- atomic r/w immediate
274
  constant funct3_csrrsi_c : std_ulogic_vector(2 downto 0) := "110"; -- atomic read & set bit immediate
275
  constant funct3_csrrci_c : std_ulogic_vector(2 downto 0) := "111"; -- atomic read & clear bit immediate
276
 
277
  -- Co-Processor Operations ----------------------------------------------------------------
278
  -- -------------------------------------------------------------------------------------------
279
  -- cp ids --
280
  constant cp_sel_muldiv_c : std_ulogic_vector(1 downto 0) := "00"; -- MULDIV CP
281
  -- muldiv cp --
282 6 zero_gravi
  constant cp_op_mul_c     : std_ulogic_vector(2 downto 0) := "000"; -- mul
283
  constant cp_op_mulh_c    : std_ulogic_vector(2 downto 0) := "001"; -- mulh
284
  constant cp_op_mulhsu_c  : std_ulogic_vector(2 downto 0) := "010"; -- mulhsu
285
  constant cp_op_mulhu_c   : std_ulogic_vector(2 downto 0) := "011"; -- mulhu
286
  constant cp_op_div_c     : std_ulogic_vector(2 downto 0) := "100"; -- div
287
  constant cp_op_divu_c    : std_ulogic_vector(2 downto 0) := "101"; -- divu
288
  constant cp_op_rem_c     : std_ulogic_vector(2 downto 0) := "110"; -- rem
289
  constant cp_op_remu_c    : std_ulogic_vector(2 downto 0) := "111"; -- remu
290 2 zero_gravi
 
291
  -- ALU Function Codes ---------------------------------------------------------------------
292
  -- -------------------------------------------------------------------------------------------
293
  constant alu_cmd_add_c   : std_ulogic_vector(2 downto 0) := "000"; -- r <= A + B
294
  constant alu_cmd_sub_c   : std_ulogic_vector(2 downto 0) := "001"; -- r <= A - B
295
  constant alu_cmd_slt_c   : std_ulogic_vector(2 downto 0) := "010"; -- r <= A < B
296
  constant alu_cmd_shift_c : std_ulogic_vector(2 downto 0) := "011"; -- r <= A <</>> B
297
  constant alu_cmd_xor_c   : std_ulogic_vector(2 downto 0) := "100"; -- r <= A xor B
298
  constant alu_cmd_or_c    : std_ulogic_vector(2 downto 0) := "101"; -- r <= A or B
299
  constant alu_cmd_and_c   : std_ulogic_vector(2 downto 0) := "110"; -- r <= A and B
300
  constant alu_cmd_bitc_c  : std_ulogic_vector(2 downto 0) := "111"; -- r <= A and (not B)
301
 
302
  -- CPU Control Exception System -----------------------------------------------------------
303
  -- -------------------------------------------------------------------------------------------
304
  -- exception source bits --
305
  constant exception_iaccess_c   : natural := 0; -- instrution access fault
306
  constant exception_iillegal_c  : natural := 1; -- illegal instrution
307
  constant exception_ialign_c    : natural := 2; -- instrution address misaligned
308
  constant exception_m_envcall_c : natural := 3; -- ENV call from m-mode
309
  constant exception_break_c     : natural := 4; -- breakpoint
310
  constant exception_salign_c    : natural := 5; -- store address misaligned
311
  constant exception_lalign_c    : natural := 6; -- load address misaligned
312
  constant exception_saccess_c   : natural := 7; -- store access fault
313
  constant exception_laccess_c   : natural := 8; -- load access fault
314
  constant exception_width_c     : natural := 9; -- length of this list in bits
315
  -- interrupt source bits --
316
  constant interrupt_mtime_irq_c : natural := 0; -- machine timer interrupt
317
  constant interrupt_msw_irq_c   : natural := 1; -- machine sw interrupt
318
  constant interrupt_mext_irq_c  : natural := 2; -- machine external interrupt
319
  constant interrupt_width_c     : natural := 3; -- length of this list in bits
320
 
321
  -- Clock Generator -------------------------------------------------------------------------
322
  -- -------------------------------------------------------------------------------------------
323
  constant clk_div2_c    : natural := 0;
324
  constant clk_div4_c    : natural := 1;
325
  constant clk_div8_c    : natural := 2;
326
  constant clk_div64_c   : natural := 3;
327
  constant clk_div128_c  : natural := 4;
328
  constant clk_div1024_c : natural := 5;
329
  constant clk_div2048_c : natural := 6;
330
  constant clk_div4096_c : natural := 7;
331
 
332
  -- Component: NEORV32 Processor Top Entity ------------------------------------------------
333
  -- -------------------------------------------------------------------------------------------
334
  component neorv32_top
335
    generic (
336
      -- General --
337
      CLOCK_FREQUENCY           : natural := 0; -- clock frequency of clk_i in Hz
338
      HART_ID                   : std_ulogic_vector(31 downto 0) := x"00000000"; -- custom hardware thread ID
339
      BOOTLOADER_USE            : boolean := true;   -- implement processor-internal bootloader?
340 6 zero_gravi
      CSR_COUNTERS_USE          : boolean := true;   -- implement RISC-V perf. counters ([m]instret[h], [m]cycle[h], time[h])?
341 2 zero_gravi
      -- RISC-V CPU Extensions --
342
      CPU_EXTENSION_RISCV_C     : boolean := false;  -- implement compressed extension?
343
      CPU_EXTENSION_RISCV_E     : boolean := false;  -- implement embedded RF extension?
344
      CPU_EXTENSION_RISCV_M     : boolean := false;  -- implement muld/div extension?
345
      CPU_EXTENSION_RISCV_Zicsr : boolean := true;   -- implement CSR system?
346
      -- Memory configuration: Instruction memory --
347
      MEM_ISPACE_BASE           : std_ulogic_vector(31 downto 0) := x"00000000"; -- base address of instruction memory space
348
      MEM_ISPACE_SIZE           : natural := 16*1024; -- total size of instruction memory space in byte
349
      MEM_INT_IMEM_USE          : boolean := true;    -- implement processor-internal instruction memory
350
      MEM_INT_IMEM_SIZE         : natural := 16*1024; -- size of processor-internal instruction memory in bytes
351
      MEM_INT_IMEM_ROM          : boolean := false;   -- implement processor-internal instruction memory as ROM
352
      -- Memory configuration: Data memory --
353
      MEM_DSPACE_BASE           : std_ulogic_vector(31 downto 0) := x"80000000"; -- base address of data memory space
354
      MEM_DSPACE_SIZE           : natural := 8*1024; -- total size of data memory space in byte
355
      MEM_INT_DMEM_USE          : boolean := true;   -- implement processor-internal data memory
356
      MEM_INT_DMEM_SIZE         : natural := 8*1024; -- size of processor-internal data memory in bytes
357
      -- Memory configuration: External memory interface --
358
      MEM_EXT_USE               : boolean := false;  -- implement external memory bus interface?
359
      MEM_EXT_REG_STAGES        : natural := 2;      -- number of interface register stages (0,1,2)
360
      MEM_EXT_TIMEOUT           : natural := 15;     -- cycles after which a valid bus access will timeout (>=1)
361
      -- Processor peripherals --
362
      IO_GPIO_USE               : boolean := true;   -- implement general purpose input/output port unit (GPIO)?
363
      IO_MTIME_USE              : boolean := true;   -- implement machine system timer (MTIME)?
364
      IO_UART_USE               : boolean := true;   -- implement universal asynchronous receiver/transmitter (UART)?
365
      IO_SPI_USE                : boolean := true;   -- implement serial peripheral interface (SPI)?
366
      IO_TWI_USE                : boolean := true;   -- implement two-wire interface (TWI)?
367
      IO_PWM_USE                : boolean := true;   -- implement pulse-width modulation unit (PWM)?
368
      IO_WDT_USE                : boolean := true;   -- implement watch dog timer (WDT)?
369
      IO_CLIC_USE               : boolean := true;   -- implement core local interrupt controller (CLIC)?
370 3 zero_gravi
      IO_TRNG_USE               : boolean := false;  -- implement true random number generator (TRNG)?
371
      IO_DEVNULL_USE            : boolean := true    -- implement dummy device (DEVNULL)?
372 2 zero_gravi
    );
373
    port (
374
      -- Global control --
375
      clk_i      : in  std_ulogic := '0'; -- global clock, rising edge
376
      rstn_i     : in  std_ulogic := '0'; -- global reset, low-active, async
377
      -- Wishbone bus interface --
378
      wb_adr_o   : out std_ulogic_vector(31 downto 0); -- address
379
      wb_dat_i   : in  std_ulogic_vector(31 downto 0) := (others => '0'); -- read data
380
      wb_dat_o   : out std_ulogic_vector(31 downto 0); -- write data
381
      wb_we_o    : out std_ulogic; -- read/write
382
      wb_sel_o   : out std_ulogic_vector(03 downto 0); -- byte enable
383
      wb_stb_o   : out std_ulogic; -- strobe
384
      wb_cyc_o   : out std_ulogic; -- valid cycle
385
      wb_ack_i   : in  std_ulogic := '0'; -- transfer acknowledge
386
      wb_err_i   : in  std_ulogic := '0'; -- transfer error
387
      -- GPIO --
388
      gpio_o     : out std_ulogic_vector(15 downto 0); -- parallel output
389
      gpio_i     : in  std_ulogic_vector(15 downto 0) := (others => '0'); -- parallel input
390
      -- UART --
391
      uart_txd_o : out std_ulogic; -- UART send data
392
      uart_rxd_i : in  std_ulogic := '0'; -- UART receive data
393
      -- SPI --
394 6 zero_gravi
      spi_sck_o  : out std_ulogic; -- SPI serial clock
395
      spi_sdo_o  : out std_ulogic; -- controller data out, peripheral data in
396
      spi_sdi_i  : in  std_ulogic; -- controller data in, peripheral data out
397 2 zero_gravi
      spi_csn_o  : out std_ulogic_vector(07 downto 0); -- SPI CS
398
      -- TWI --
399
      twi_sda_io : inout std_logic := 'H'; -- twi serial data line
400
      twi_scl_io : inout std_logic := 'H'; -- twi serial clock line
401
      -- PWM --
402
      pwm_o      : out std_ulogic_vector(03 downto 0);  -- pwm channels
403
      -- Interrupts --
404
      ext_irq_i  : in  std_ulogic_vector(01 downto 0) := (others => '0'); -- external interrupt request
405
      ext_ack_o  : out std_ulogic_vector(01 downto 0)  -- external interrupt request acknowledge
406
    );
407
  end component;
408
 
409 4 zero_gravi
  -- Component: CPU Top Entity --------------------------------------------------------------
410
  -- -------------------------------------------------------------------------------------------
411
  component neorv32_cpu
412
    generic (
413
      -- General --
414
      CLOCK_FREQUENCY           : natural := 0; -- clock frequency of clk_i in Hz
415
      HART_ID                   : std_ulogic_vector(31 downto 0) := x"00000000"; -- custom hardware thread ID
416
      BOOTLOADER_USE            : boolean := true;   -- implement processor-internal bootloader?
417 6 zero_gravi
      CSR_COUNTERS_USE          : boolean := true;   -- implement RISC-V perf. counters ([m]instret[h], [m]cycle[h], time[h])?
418 4 zero_gravi
      -- RISC-V CPU Extensions --
419
      CPU_EXTENSION_RISCV_C     : boolean := false;  -- implement compressed extension?
420
      CPU_EXTENSION_RISCV_E     : boolean := false;  -- implement embedded RF extension?
421
      CPU_EXTENSION_RISCV_M     : boolean := false;  -- implement muld/div extension?
422
      CPU_EXTENSION_RISCV_Zicsr : boolean := true;   -- implement CSR system?
423
      -- Memory configuration: Instruction memory --
424
      MEM_ISPACE_BASE           : std_ulogic_vector(31 downto 0) := x"00000000"; -- base address of instruction memory space
425
      MEM_ISPACE_SIZE           : natural := 8*1024; -- total size of instruction memory space in byte
426
      MEM_INT_IMEM_USE          : boolean := true;   -- implement processor-internal instruction memory
427
      MEM_INT_IMEM_SIZE         : natural := 8*1024; -- size of processor-internal instruction memory in bytes
428
      MEM_INT_IMEM_ROM          : boolean := false;  -- implement processor-internal instruction memory as ROM
429
      -- Memory configuration: Data memory --
430
      MEM_DSPACE_BASE           : std_ulogic_vector(31 downto 0) := x"80000000"; -- base address of data memory space
431
      MEM_DSPACE_SIZE           : natural := 4*1024; -- total size of data memory space in byte
432
      MEM_INT_DMEM_USE          : boolean := true;   -- implement processor-internal data memory
433
      MEM_INT_DMEM_SIZE         : natural := 4*1024; -- size of processor-internal data memory in bytes
434
      -- Memory configuration: External memory interface --
435
      MEM_EXT_USE               : boolean := false;  -- implement external memory bus interface?
436
      MEM_EXT_TIMEOUT           : natural := 15;     -- cycles after which a valid bus access will timeout
437
      -- Processor peripherals --
438
      IO_GPIO_USE               : boolean := true;   -- implement general purpose input/output port unit (GPIO)?
439
      IO_MTIME_USE              : boolean := true;   -- implement machine system timer (MTIME)?
440
      IO_UART_USE               : boolean := true;   -- implement universal asynchronous receiver/transmitter (UART)?
441
      IO_SPI_USE                : boolean := true;   -- implement serial peripheral interface (SPI)?
442
      IO_TWI_USE                : boolean := true;   -- implement two-wire interface (TWI)?
443
      IO_PWM_USE                : boolean := true;   -- implement pulse-width modulation unit (PWM)?
444
      IO_WDT_USE                : boolean := true;   -- implement watch dog timer (WDT)?
445
      IO_CLIC_USE               : boolean := true;   -- implement core local interrupt controller (CLIC)?
446
      IO_TRNG_USE               : boolean := true;   -- implement true random number generator (TRNG)?
447
      IO_DEVNULL_USE            : boolean := true    -- implement dummy device (DEVNULL)?
448
    );
449
    port (
450
      -- global control --
451
      clk_i       : in  std_ulogic; -- global clock, rising edge
452
      rstn_i      : in  std_ulogic; -- global reset, low-active, async
453
      -- bus interface --
454
      bus_addr_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
455
      bus_rdata_i : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
456
      bus_wdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
457
      bus_ben_o   : out std_ulogic_vector(03 downto 0); -- byte enable
458
      bus_we_o    : out std_ulogic; -- write enable
459
      bus_re_o    : out std_ulogic; -- read enable
460
      bus_ack_i   : in  std_ulogic; -- bus transfer acknowledge
461
      bus_err_i   : in  std_ulogic; -- bus transfer error
462
      -- external interrupts --
463
      clic_irq_i  : in  std_ulogic; -- CLIC interrupt request
464
      mtime_irq_i : in  std_ulogic  -- machine timer interrupt
465
    );
466
  end component;
467
 
468 2 zero_gravi
  -- Component: CPU Control -----------------------------------------------------------------
469
  -- -------------------------------------------------------------------------------------------
470
  component neorv32_cpu_control
471
    generic (
472
      -- General --
473
      CLOCK_FREQUENCY           : natural := 0; -- clock frequency of clk_i in Hz
474
      HART_ID                   : std_ulogic_vector(31 downto 0) := x"00000000"; -- custom hardware thread ID
475
      BOOTLOADER_USE            : boolean := true;   -- implement processor-internal bootloader?
476 6 zero_gravi
      CSR_COUNTERS_USE          : boolean := true;   -- implement RISC-V perf. counters ([m]instret[h], [m]cycle[h], time[h])?
477 2 zero_gravi
      -- RISC-V CPU Extensions --
478
      CPU_EXTENSION_RISCV_C     : boolean := false;  -- implement compressed extension?
479
      CPU_EXTENSION_RISCV_E     : boolean := false;  -- implement embedded RF extension?
480
      CPU_EXTENSION_RISCV_M     : boolean := false;  -- implement muld/div extension?
481
      CPU_EXTENSION_RISCV_Zicsr : boolean := true;   -- implement CSR system?
482
      -- Memory configuration: Instruction memory --
483
      MEM_ISPACE_BASE           : std_ulogic_vector(31 downto 0) := x"00000000"; -- base address of instruction memory space
484
      MEM_ISPACE_SIZE           : natural := 16*1024; -- total size of instruction memory space in byte
485
      MEM_INT_IMEM_USE          : boolean := true;   -- implement processor-internal instruction memory
486
      MEM_INT_IMEM_SIZE         : natural := 16*1024; -- size of processor-internal instruction memory in bytes
487
      MEM_INT_IMEM_ROM          : boolean := false;  -- implement processor-internal instruction memory as ROM
488
      -- Memory configuration: Data memory --
489
      MEM_DSPACE_BASE           : std_ulogic_vector(31 downto 0) := x"80000000"; -- base address of data memory space
490
      MEM_DSPACE_SIZE           : natural := 8*1024; -- total size of data memory space in byte
491
      MEM_INT_DMEM_USE          : boolean := true;   -- implement processor-internal data memory
492
      MEM_INT_DMEM_SIZE         : natural := 8*1024; -- size of processor-internal data memory in bytes
493
      -- Memory configuration: External memory interface --
494
      MEM_EXT_USE               : boolean := false;  -- implement external memory bus interface?
495
      -- Processor peripherals --
496
      IO_GPIO_USE               : boolean := true;   -- implement general purpose input/output port unit (GPIO)?
497
      IO_MTIME_USE              : boolean := true;   -- implement machine system timer (MTIME)?
498
      IO_UART_USE               : boolean := true;   -- implement universal asynchronous receiver/transmitter (UART)?
499
      IO_SPI_USE                : boolean := true;   -- implement serial peripheral interface (SPI)?
500
      IO_TWI_USE                : boolean := true;   -- implement two-wire interface (TWI)?
501
      IO_PWM_USE                : boolean := true;   -- implement pulse-width modulation unit (PWM)?
502
      IO_WDT_USE                : boolean := true;   -- implement watch dog timer (WDT)?
503
      IO_CLIC_USE               : boolean := true;   -- implement core local interrupt controller (CLIC)?
504 3 zero_gravi
      IO_TRNG_USE               : boolean := true;   -- implement true random number generator (TRNG)?
505
      IO_DEVNULL_USE            : boolean := true    -- implement dummy device (DEVNULL)?
506 2 zero_gravi
    );
507
    port (
508
      -- global control --
509
      clk_i         : in  std_ulogic; -- global clock, rising edge
510
      rstn_i        : in  std_ulogic; -- global reset, low-active, async
511
      ctrl_o        : out std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
512
      -- status input --
513
      alu_wait_i    : in  std_ulogic; -- wait for ALU
514
      bus_wait_i    : in  std_ulogic; -- wait for bus
515
      -- data input --
516
      instr_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- instruction
517
      cmp_i         : in  std_ulogic_vector(1 downto 0); -- comparator status
518
      alu_add_i     : in  std_ulogic_vector(data_width_c-1 downto 0); -- ALU.add result
519
      -- data output --
520
      imm_o         : out std_ulogic_vector(data_width_c-1 downto 0); -- immediate
521 6 zero_gravi
      fetch_pc_o    : out std_ulogic_vector(data_width_c-1 downto 0); -- PC for instruction fetch
522
      curr_pc_o     : out std_ulogic_vector(data_width_c-1 downto 0); -- current PC (corresponding to current instruction)
523
      next_pc_o     : out std_ulogic_vector(data_width_c-1 downto 0); -- next PC (corresponding to current instruction)
524 2 zero_gravi
      -- csr interface --
525
      csr_wdata_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- CSR write data
526
      csr_rdata_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- CSR read data
527
      -- external interrupt --
528
      clic_irq_i    : in  std_ulogic; -- CLIC interrupt request
529
      mtime_irq_i   : in  std_ulogic; -- machine timer interrupt
530
      -- bus access exceptions --
531
      mar_i         : in  std_ulogic_vector(data_width_c-1 downto 0); -- memory address register
532
      ma_instr_i    : in  std_ulogic; -- misaligned instruction address
533
      ma_load_i     : in  std_ulogic; -- misaligned load data address
534
      ma_store_i    : in  std_ulogic; -- misaligned store data address
535
      be_instr_i    : in  std_ulogic; -- bus error on instruction access
536
      be_load_i     : in  std_ulogic; -- bus error on load data access
537
      be_store_i    : in  std_ulogic; -- bus error on store data access
538 6 zero_gravi
      bus_exc_ack_o : out std_ulogic; -- bus exception error acknowledge
539
      bus_busy_i    : in  std_ulogic  -- bus unit is busy
540 2 zero_gravi
    );
541
  end component;
542
 
543
  -- Component: CPU Register File -----------------------------------------------------------
544
  -- -------------------------------------------------------------------------------------------
545
  component neorv32_cpu_regfile
546
    generic (
547
      CPU_EXTENSION_RISCV_E : boolean := false -- implement embedded RF extension?
548
    );
549
    port (
550
      -- global control --
551
      clk_i  : in  std_ulogic; -- global clock, rising edge
552
      ctrl_i : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
553
      -- data input --
554
      mem_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- memory read data
555
      alu_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
556
      csr_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- CSR read data
557
      pc_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- current pc
558
      -- data output --
559
      rs1_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- operand 1
560
      rs2_o  : out std_ulogic_vector(data_width_c-1 downto 0)  -- operand 2
561
    );
562
  end component;
563
 
564
  -- Component: CPU ALU ---------------------------------------------------------------------
565
  -- -------------------------------------------------------------------------------------------
566
  component neorv32_cpu_alu
567
    port (
568
      -- global control --
569
      clk_i       : in  std_ulogic; -- global clock, rising edge
570
      rstn_i      : in  std_ulogic; -- global reset, low-active, async
571
      ctrl_i      : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
572
      -- data input --
573
      rs1_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
574
      rs2_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
575
      pc2_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- delayed PC
576
      imm_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- immediate
577
      csr_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- csr read data
578
      -- data output --
579
      cmp_o       : out std_ulogic_vector(1 downto 0); -- comparator status
580
      add_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- OPA + OPB
581
      res_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
582
      -- co-processor interface --
583
      cp0_data_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- co-processor 0 result
584
      cp0_valid_i : in  std_ulogic; -- co-processor 0 result valid
585
      cp1_data_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- co-processor 1 result
586
      cp1_valid_i : in  std_ulogic; -- co-processor 1 result valid
587
      -- status --
588
      wait_o      : out std_ulogic -- busy due to iterative processing units
589
    );
590
  end component;
591
 
592
  -- Component: CPU Co-Processor MULDIV -----------------------------------------------------
593
  -- -------------------------------------------------------------------------------------------
594
  component neorv32_cpu_cp_muldiv
595
    port (
596
      -- global control --
597
      clk_i   : in  std_ulogic; -- global clock, rising edge
598
      rstn_i  : in  std_ulogic; -- global reset, low-active, async
599
      ctrl_i  : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
600
      -- data input --
601
      rs1_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
602
      rs2_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
603
      -- result and status --
604
      res_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- operation result
605
      valid_o : out std_ulogic -- data output valid
606
    );
607
  end component;
608
 
609
  -- Component: CPU Bus Interface -----------------------------------------------------------
610
  -- -------------------------------------------------------------------------------------------
611
  component neorv32_cpu_bus
612
    generic (
613 6 zero_gravi
      MEM_EXT_TIMEOUT : natural := 15 -- cycles after which a valid bus access will timeout
614 2 zero_gravi
    );
615
    port (
616
      -- global control --
617
      clk_i       : in  std_ulogic; -- global clock, rising edge
618
      rstn_i      : in  std_ulogic; -- global reset, low-active, async
619
      ctrl_i      : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
620
      -- data input --
621
      wdata_i     : in  std_ulogic_vector(data_width_c-1 downto 0); -- write data
622
      pc_i        : in  std_ulogic_vector(data_width_c-1 downto 0); -- current PC
623
      alu_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
624
      -- data output --
625
      instr_o     : out std_ulogic_vector(data_width_c-1 downto 0); -- instruction
626
      rdata_o     : out std_ulogic_vector(data_width_c-1 downto 0); -- read data
627
      -- status --
628
      mar_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- current memory address register
629
      ma_instr_o  : out std_ulogic; -- misaligned instruction address
630
      ma_load_o   : out std_ulogic; -- misaligned load data address
631
      ma_store_o  : out std_ulogic; -- misaligned store data address
632
      be_instr_o  : out std_ulogic; -- bus error on instruction access
633
      be_load_o   : out std_ulogic; -- bus error on load data access
634
      be_store_o  : out std_ulogic; -- bus error on store data 
635
      bus_wait_o  : out std_ulogic; -- wait for bus operation to finish
636 6 zero_gravi
      bus_busy_o  : out std_ulogic; -- bus unit is busy
637 2 zero_gravi
      exc_ack_i   : in  std_ulogic; -- exception controller ACK
638
      -- bus system --
639
      bus_addr_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
640
      bus_rdata_i : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
641
      bus_wdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
642
      bus_ben_o   : out std_ulogic_vector(03 downto 0); -- byte enable
643
      bus_we_o    : out std_ulogic; -- write enable
644
      bus_re_o    : out std_ulogic; -- read enable
645
      bus_ack_i   : in  std_ulogic; -- bus transfer acknowledge
646
      bus_err_i   : in  std_ulogic  -- bus transfer error
647
    );
648
  end component;
649
 
650
  -- Component: CPU Compressed Instructions Decompressor ------------------------------------
651
  -- -------------------------------------------------------------------------------------------
652
  component neorv32_cpu_decompressor
653
    port (
654
      -- instruction input --
655
      ci_instr16_i : in  std_ulogic_vector(15 downto 0); -- compressed instruction input
656
      -- instruction output --
657
      ci_illegal_o : out std_ulogic; -- is an illegal compressed instruction
658
      ci_instr32_o : out std_ulogic_vector(31 downto 0)  -- 32-bit decompressed instruction
659
    );
660
  end component;
661
 
662
  -- Component: Processor-internal instruction memory (IMEM) --------------------------------
663
  -- -------------------------------------------------------------------------------------------
664
  component neorv32_imem
665
    generic (
666
      IMEM_BASE      : std_ulogic_vector(31 downto 0) := x"00000000"; -- memory base address
667
      IMEM_SIZE      : natural := 4*1024; -- processor-internal instruction memory size in bytes
668
      IMEM_AS_ROM    : boolean := false;  -- implement IMEM as read-only memory?
669
      BOOTLOADER_USE : boolean := true    -- implement and use bootloader?
670
    );
671
    port (
672
      clk_i  : in  std_ulogic; -- global clock line
673
      rden_i : in  std_ulogic; -- read enable
674
      wren_i : in  std_ulogic; -- write enable
675
      ben_i  : in  std_ulogic_vector(03 downto 0); -- byte write enable
676
      upen_i : in  std_ulogic; -- update enable
677
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
678
      data_i : in  std_ulogic_vector(31 downto 0); -- data in
679
      data_o : out std_ulogic_vector(31 downto 0); -- data out
680
      ack_o  : out std_ulogic -- transfer acknowledge
681
    );
682
  end component;
683
 
684
  -- Component: Processor-internal data memory (DMEM) ---------------------------------------
685
  -- -------------------------------------------------------------------------------------------
686
  component neorv32_dmem
687
    generic (
688
      DMEM_BASE : std_ulogic_vector(31 downto 0) := x"80000000"; -- memory base address
689
      DMEM_SIZE : natural := 4*1024  -- processor-internal instruction memory size in bytes
690
    );
691
    port (
692
      clk_i  : in  std_ulogic; -- global clock line
693
      rden_i : in  std_ulogic; -- read enable
694
      wren_i : in  std_ulogic; -- write enable
695
      ben_i  : in  std_ulogic_vector(03 downto 0); -- byte write enable
696
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
697
      data_i : in  std_ulogic_vector(31 downto 0); -- data in
698
      data_o : out std_ulogic_vector(31 downto 0); -- data out
699
      ack_o  : out std_ulogic -- transfer acknowledge
700
    );
701
  end component;
702
 
703
  -- Component: Processor-internal bootloader ROM (BOOTROM) ---------------------------------
704
  -- -------------------------------------------------------------------------------------------
705
  component neorv32_boot_rom
706
    port (
707
      clk_i  : in  std_ulogic; -- global clock line
708
      rden_i : in  std_ulogic; -- read enable
709
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
710
      data_o : out std_ulogic_vector(31 downto 0); -- data out
711
      ack_o  : out std_ulogic -- transfer acknowledge
712
    );
713
  end component;
714
 
715
  -- Component: Machine System Timer (mtime) ------------------------------------------------
716
  -- -------------------------------------------------------------------------------------------
717
  component neorv32_mtime
718
    port (
719
      -- host access --
720
      clk_i     : in  std_ulogic; -- global clock line
721 4 zero_gravi
      rstn_i    : in  std_ulogic := '0'; -- global reset, low-active, async
722 2 zero_gravi
      addr_i    : in  std_ulogic_vector(31 downto 0); -- address
723
      rden_i    : in  std_ulogic; -- read enable
724
      wren_i    : in  std_ulogic; -- write enable
725
      ben_i     : in  std_ulogic_vector(03 downto 0); -- byte write enable
726
      data_i    : in  std_ulogic_vector(31 downto 0); -- data in
727
      data_o    : out std_ulogic_vector(31 downto 0); -- data out
728
      ack_o     : out std_ulogic; -- transfer acknowledge
729
      -- interrupt --
730
      irq_o     : out std_ulogic  -- interrupt request
731
    );
732
  end component;
733
 
734
  -- Component: General Purpose Input/Output Port (GPIO) ------------------------------------
735
  -- -------------------------------------------------------------------------------------------
736
  component neorv32_gpio
737
    port (
738
      -- host access --
739
      clk_i  : in  std_ulogic; -- global clock line
740
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
741
      rden_i : in  std_ulogic; -- read enable
742
      wren_i : in  std_ulogic; -- write enable
743
      ben_i  : in  std_ulogic_vector(03 downto 0); -- byte write enable
744
      data_i : in  std_ulogic_vector(31 downto 0); -- data in
745
      data_o : out std_ulogic_vector(31 downto 0); -- data out
746
      ack_o  : out std_ulogic; -- transfer acknowledge
747
      -- parallel io --
748
      gpio_o : out std_ulogic_vector(15 downto 0);
749
      gpio_i : in  std_ulogic_vector(15 downto 0);
750
      -- interrupt --
751
      irq_o  : out std_ulogic
752
    );
753
  end component;
754
 
755
  -- Component: Core Local Interrupt Controller (CLIC) --------------------------------------
756
  -- -------------------------------------------------------------------------------------------
757
  component neorv32_clic
758
    port (
759
      -- host access --
760
      clk_i     : in  std_ulogic; -- global clock line
761
      addr_i    : in  std_ulogic_vector(31 downto 0); -- address
762
      rden_i    : in  std_ulogic; -- read enable
763
      wren_i    : in  std_ulogic; -- write enable
764
      ben_i     : in  std_ulogic_vector(03 downto 0); -- byte write enable
765
      data_i    : in  std_ulogic_vector(31 downto 0); -- data in
766
      data_o    : out std_ulogic_vector(31 downto 0); -- data out
767
      ack_o     : out std_ulogic; -- transfer acknowledge
768
      -- cpu interrupt --
769
      cpu_irq_o : out std_ulogic; -- trigger CPU's external IRQ
770
      -- external interrupt lines --
771
      ext_irq_i : in  std_ulogic_vector(07 downto 0); -- IRQ, triggering on HIGH level
772
      ext_ack_o : out std_ulogic_vector(07 downto 0)  -- acknowledge
773
    );
774
  end component;
775
 
776
  -- Component: Watchdog Timer (WDT) --------------------------------------------------------
777
  -- -------------------------------------------------------------------------------------------
778
  component neorv32_wdt
779
    port (
780
      -- host access --
781
      clk_i       : in  std_ulogic; -- global clock line
782
      rstn_i      : in  std_ulogic; -- global reset line, low-active
783
      rden_i      : in  std_ulogic; -- read enable
784
      wren_i      : in  std_ulogic; -- write enable
785
      ben_i       : in  std_ulogic_vector(03 downto 0); -- byte write enable
786
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
787
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
788
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
789
      ack_o       : out std_ulogic; -- transfer acknowledge
790
      -- clock generator --
791
      clkgen_en_o : out std_ulogic; -- enable clock generator
792
      clkgen_i    : in  std_ulogic_vector(07 downto 0);
793
      -- timeout event --
794
      irq_o       : out std_ulogic; -- timeout IRQ
795
      rstn_o      : out std_ulogic  -- timeout reset, low_active, use it as async!
796
    );
797
  end component;
798
 
799
  -- Component: Universal Asynchronous Receiver and Transmitter (UART) ----------------------
800
  -- -------------------------------------------------------------------------------------------
801
  component neorv32_uart
802
    port (
803
      -- host access --
804
      clk_i       : in  std_ulogic; -- global clock line
805
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
806
      rden_i      : in  std_ulogic; -- read enable
807
      wren_i      : in  std_ulogic; -- write enable
808
      ben_i       : in  std_ulogic_vector(03 downto 0); -- byte write enable
809
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
810
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
811
      ack_o       : out std_ulogic; -- transfer acknowledge
812
      -- clock generator --
813
      clkgen_en_o : out std_ulogic; -- enable clock generator
814
      clkgen_i    : in  std_ulogic_vector(07 downto 0);
815
      -- com lines --
816
      uart_txd_o  : out std_ulogic;
817
      uart_rxd_i  : in  std_ulogic;
818
      -- interrupts --
819
      uart_irq_o  : out std_ulogic  -- uart rx/tx interrupt
820
    );
821
  end component;
822
 
823
  -- Component: Serial Peripheral Interface (SPI) -------------------------------------------
824
  -- -------------------------------------------------------------------------------------------
825
  component neorv32_spi
826
    port (
827
      -- host access --
828
      clk_i       : in  std_ulogic; -- global clock line
829
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
830
      rden_i      : in  std_ulogic; -- read enable
831
      wren_i      : in  std_ulogic; -- write enable
832
      ben_i       : in  std_ulogic_vector(03 downto 0); -- byte write enable
833
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
834
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
835
      ack_o       : out std_ulogic; -- transfer acknowledge
836
      -- clock generator --
837
      clkgen_en_o : out std_ulogic; -- enable clock generator
838
      clkgen_i    : in  std_ulogic_vector(07 downto 0);
839
      -- com lines --
840 6 zero_gravi
      spi_sck_o   : out std_ulogic; -- SPI serial clock
841
      spi_sdo_o   : out std_ulogic; -- controller data out, peripheral data in
842
      spi_sdi_i   : in  std_ulogic; -- controller data in, peripheral data out
843 2 zero_gravi
      spi_csn_o   : out std_ulogic_vector(07 downto 0); -- SPI CS
844
      -- interrupt --
845
      spi_irq_o   : out std_ulogic -- transmission done interrupt
846
    );
847
  end component;
848
 
849
  -- Component: Two-Wire Interface (TWI) ----------------------------------------------------
850
  -- -------------------------------------------------------------------------------------------
851
  component neorv32_twi
852
    port (
853
      -- host access --
854
      clk_i       : in  std_ulogic; -- global clock line
855
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
856
      rden_i      : in  std_ulogic; -- read enable
857
      wren_i      : in  std_ulogic; -- write enable
858
      ben_i       : in  std_ulogic_vector(03 downto 0); -- byte write enable
859
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
860
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
861
      ack_o       : out std_ulogic; -- transfer acknowledge
862
      -- clock generator --
863
      clkgen_en_o : out std_ulogic; -- enable clock generator
864
      clkgen_i    : in  std_ulogic_vector(07 downto 0);
865
      -- com lines --
866
      twi_sda_io  : inout std_logic; -- serial data line
867
      twi_scl_io  : inout std_logic; -- serial clock line
868
      -- interrupt --
869
      twi_irq_o   : out std_ulogic -- transfer done IRQ
870
    );
871
  end component;
872
 
873
  -- Component: Pulse-Width Modulation Controller (PWM) -------------------------------------
874
  -- -------------------------------------------------------------------------------------------
875
  component neorv32_pwm
876
    port (
877
      -- host access --
878
      clk_i       : in  std_ulogic; -- global clock line
879
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
880
      rden_i      : in  std_ulogic; -- read enable
881
      wren_i      : in  std_ulogic; -- write enable
882
      ben_i       : in  std_ulogic_vector(03 downto 0); -- byte write enable
883
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
884
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
885
      ack_o       : out std_ulogic; -- transfer acknowledge
886
      -- clock generator --
887
      clkgen_en_o : out std_ulogic; -- enable clock generator
888
      clkgen_i    : in  std_ulogic_vector(07 downto 0);
889
      -- pwm output channels --
890
      pwm_o       : out std_ulogic_vector(03 downto 0)
891
    );
892
  end component;
893
 
894
  -- Component: True Random Number Generator (TRNG) -----------------------------------------
895
  -- -------------------------------------------------------------------------------------------
896
  component neorv32_trng
897
    port (
898
      -- host access --
899
      clk_i  : in  std_ulogic; -- global clock line
900
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
901
      rden_i : in  std_ulogic; -- read enable
902
      wren_i : in  std_ulogic; -- write enable
903
      ben_i  : in  std_ulogic_vector(03 downto 0); -- byte write enable
904
      data_i : in  std_ulogic_vector(31 downto 0); -- data in
905
      data_o : out std_ulogic_vector(31 downto 0); -- data out
906
      ack_o  : out std_ulogic  -- transfer acknowledge
907
    );
908
  end component;
909
 
910
  -- Component: Wishbone Bus Gateway (WISHBONE) ---------------------------------------------
911
  -- -------------------------------------------------------------------------------------------
912
  component neorv32_wishbone
913
    generic (
914
      INTERFACE_REG_STAGES : natural := 2; -- number of interface register stages (0,1,2)
915
      -- Memory configuration: Instruction memory --
916
      MEM_ISPACE_BASE      : std_ulogic_vector(31 downto 0) := x"00000000"; -- base address of instruction memory space
917
      MEM_ISPACE_SIZE      : natural := 8*1024; -- total size of instruction memory space in byte
918
      MEM_INT_IMEM_USE     : boolean := true;   -- implement processor-internal instruction memory
919
      MEM_INT_IMEM_SIZE    : natural := 8*1024; -- size of processor-internal instruction memory in bytes
920
      -- Memory configuration: Data memory --
921
      MEM_DSPACE_BASE      : std_ulogic_vector(31 downto 0) := x"80000000"; -- base address of data memory space
922
      MEM_DSPACE_SIZE      : natural := 4*1024; -- total size of data memory space in byte
923
      MEM_INT_DMEM_USE     : boolean := true;   -- implement processor-internal data memory
924
      MEM_INT_DMEM_SIZE    : natural := 4*1024  -- size of processor-internal data memory in bytes
925
    );
926
    port (
927
      -- global control --
928
      clk_i    : in  std_ulogic; -- global clock line
929
      rstn_i   : in  std_ulogic; -- global reset line, low-active
930
      -- host access --
931
      addr_i   : in  std_ulogic_vector(31 downto 0); -- address
932
      rden_i   : in  std_ulogic; -- read enable
933
      wren_i   : in  std_ulogic; -- write enable
934
      ben_i    : in  std_ulogic_vector(03 downto 0); -- byte write enable
935
      data_i   : in  std_ulogic_vector(31 downto 0); -- data in
936
      data_o   : out std_ulogic_vector(31 downto 0); -- data out
937
      ack_o    : out std_ulogic; -- transfer acknowledge
938
      err_o    : out std_ulogic; -- transfer error
939
      -- wishbone interface --
940
      wb_adr_o : out std_ulogic_vector(31 downto 0); -- address
941
      wb_dat_i : in  std_ulogic_vector(31 downto 0); -- read data
942
      wb_dat_o : out std_ulogic_vector(31 downto 0); -- write data
943
      wb_we_o  : out std_ulogic; -- read/write
944
      wb_sel_o : out std_ulogic_vector(03 downto 0); -- byte enable
945
      wb_stb_o : out std_ulogic; -- strobe
946
      wb_cyc_o : out std_ulogic; -- valid cycle
947
      wb_ack_i : in  std_ulogic; -- transfer acknowledge
948
      wb_err_i : in  std_ulogic  -- transfer error
949
    );
950
  end component;
951
 
952 4 zero_gravi
  ---- Component: Dummy Device with SIM Output (DEVNULL) -------------------------------------
953
  ---- -------------------------------------------------------------------------------------------
954 3 zero_gravi
  component neorv32_devnull
955
    port (
956
      -- host access --
957
      clk_i  : in  std_ulogic; -- global clock line
958
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
959
      rden_i : in  std_ulogic; -- read enable
960
      wren_i : in  std_ulogic; -- write enable
961
      ben_i  : in  std_ulogic_vector(03 downto 0); -- byte write enable
962
      data_i : in  std_ulogic_vector(31 downto 0); -- data in
963
      data_o : out std_ulogic_vector(31 downto 0); -- data out
964
      ack_o  : out std_ulogic  -- transfer acknowledge
965
    );
966
  end component;
967
 
968 2 zero_gravi
end neorv32_package;
969
 
970
package body neorv32_package is
971
 
972
  -- Function: Minimal required bit width ---------------------------------------------------
973
  -- -------------------------------------------------------------------------------------------
974
  function index_size_f(input : natural) return natural is
975
  begin
976
    for i in 0 to natural'high loop
977
      if (2**i >= input) then
978
        return i;
979
      end if;
980
    end loop; -- i
981
    return 0;
982
  end function index_size_f;
983
 
984
  -- Function: Conditional select natural ---------------------------------------------------
985
  -- -------------------------------------------------------------------------------------------
986
  function cond_sel_natural_f(cond : boolean; val_t : natural; val_f : natural) return natural is
987
  begin
988
    if (cond = true) then
989
      return val_t;
990
    else
991
      return val_f;
992
    end if;
993
  end function cond_sel_natural_f;
994
 
995
  -- Function: Conditional select std_ulogic_vector -----------------------------------------
996
  -- -------------------------------------------------------------------------------------------
997
  function cond_sel_stdulogicvector_f(cond : boolean; val_t : std_ulogic_vector; val_f : std_ulogic_vector) return std_ulogic_vector is
998
  begin
999
    if (cond = true) then
1000
      return val_t;
1001
    else
1002
      return val_f;
1003
    end if;
1004
  end function cond_sel_stdulogicvector_f;
1005
 
1006
  -- Function: Convert BOOL to STD_ULOGIC ---------------------------------------------------
1007
  -- -------------------------------------------------------------------------------------------
1008
  function bool_to_ulogic_f(cond : boolean) return std_ulogic is
1009
  begin
1010
    if (cond = true) then
1011
      return '1';
1012
    else
1013
      return '0';
1014
    end if;
1015
  end function bool_to_ulogic_f;
1016
 
1017
  -- Function: OR all bits ------------------------------------------------------------------
1018
  -- -------------------------------------------------------------------------------------------
1019
  function or_all_f(a : std_ulogic_vector) return std_ulogic is
1020
    variable tmp_v : std_ulogic;
1021
  begin
1022
    tmp_v := a(a'low);
1023
    for i in a'low+1 to a'high loop
1024
      tmp_v := tmp_v or a(i);
1025
    end loop; -- i
1026
    return tmp_v;
1027
  end function or_all_f;
1028
 
1029
  -- Function: AND all bits -----------------------------------------------------------------
1030
  -- -------------------------------------------------------------------------------------------
1031
  function and_all_f(a : std_ulogic_vector) return std_ulogic is
1032
    variable tmp_v : std_ulogic;
1033
  begin
1034
    tmp_v := a(a'low);
1035
    for i in a'low+1 to a'high loop
1036
      tmp_v := tmp_v and a(i);
1037
    end loop; -- i
1038
    return tmp_v;
1039
  end function and_all_f;
1040
 
1041
  -- Function: XOR all bits -----------------------------------------------------------------
1042
  -- -------------------------------------------------------------------------------------------
1043
  function xor_all_f(a : std_ulogic_vector) return std_ulogic is
1044
    variable tmp_v : std_ulogic;
1045
  begin
1046
    tmp_v := a(a'low);
1047
    for i in a'low+1 to a'high loop
1048
      tmp_v := tmp_v xor a(i);
1049
    end loop; -- i
1050
    return tmp_v;
1051
  end function xor_all_f;
1052
 
1053
  -- Function: XNOR all bits ----------------------------------------------------------------
1054
  -- -------------------------------------------------------------------------------------------
1055
  function xnor_all_f(a : std_ulogic_vector) return std_ulogic is
1056
    variable tmp_v : std_ulogic;
1057
  begin
1058
    tmp_v := a(a'low);
1059
    for i in a'low+1 to a'high loop
1060
      tmp_v := tmp_v xnor a(i);
1061
    end loop; -- i
1062
    return tmp_v;
1063
  end function xnor_all_f;
1064
 
1065 6 zero_gravi
  -- Function: Convert to hex char ----------------------------------------------------------
1066
  -- -------------------------------------------------------------------------------------------
1067
  function to_hexchar_f(input : std_ulogic_vector(3 downto 0)) return character is
1068
    variable output_v : character;
1069
  begin
1070
    case input is
1071
      when x"0" => output_v := '0';
1072
      when x"1" => output_v := '1';
1073
      when x"2" => output_v := '2';
1074
      when x"3" => output_v := '3';
1075
      when x"4" => output_v := '4';
1076
      when x"5" => output_v := '5';
1077
      when x"6" => output_v := '6';
1078
      when x"7" => output_v := '7';
1079
      when x"8" => output_v := '8';
1080
      when x"9" => output_v := '9';
1081
      when x"a" => output_v := 'a';
1082
      when x"b" => output_v := 'b';
1083
      when x"c" => output_v := 'c';
1084
      when x"d" => output_v := 'd';
1085
      when x"e" => output_v := 'e';
1086
      when x"f" => output_v := 'f';
1087
      when others => output_v := '?';
1088
    end case;
1089
    return output_v;
1090
  end function to_hexchar_f;
1091
 
1092 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.