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

Subversion Repositories neorv32

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

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 3 zero_gravi
  constant hw_version_c : std_ulogic_vector(31 downto 0) := x"00000204"; -- 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
  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
  function xnor_all_f(a : std_ulogic_vector) return std_ulogic;
56
 
57
  -- Processor-internal Address Space Layout ------------------------------------------------
58
  -- -------------------------------------------------------------------------------------------
59
  -- Instruction Memory & Data Memory --
60
  -- => configured via top's generics
61
 
62
  -- Bootloader ROM --
63
  constant boot_base_c          : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFF0000"; -- bootloader base address, fixed!
64
  constant boot_size_c          : natural := 4*1024; -- bytes
65
  constant boot_max_size_c      : natural := 32*1024; -- bytes, fixed!
66
 
67
  -- IO: Peripheral Devices ("IO") Area --
68
  -- Control register(s) (including the device-enable) should be located at the base address of each device
69
  constant io_base_c            : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFF80";
70
  constant io_size_c            : natural := 32*4; -- bytes, fixed!
71
 
72
  -- General Purpose Input/Output Unit (GPIO) --
73
  constant gpio_base_c          : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFF80"; -- base address, fixed!
74
  constant gpio_size_c          : natural := 2*4; -- bytes, fixed!
75
  constant gpio_in_addr_c       : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(gpio_base_c) + x"00000000");
76
  constant gpio_out_addr_c      : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(gpio_base_c) + x"00000004");
77
 
78
  -- Core-Local Interrupt Controller (CLIC) --
79
  constant clic_base_c          : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFF88"; -- base address, fixed!
80
  constant clic_size_c          : natural := 1*4; -- bytes, fixed!
81
  constant clic_ctrl_addr_c     : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(clic_base_c) + x"00000000");
82
 
83
  -- Watch Dog Timer (WDT) --
84
  constant wdt_base_c           : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFF8C"; -- base address, fixed!
85
  constant wdt_size_c           : natural := 1*4; -- bytes, fixed!
86
  constant wdt_ctrl_addr_c      : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(wdt_base_c) + x"00000000");
87
 
88
  -- Machine System Timer (MTIME) --
89
  constant mtime_base_c         : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFF90"; -- base address, fixed!
90
  constant mtime_size_c         : natural := 4*4; -- bytes, fixed!
91
  constant mtime_time_lo_addr_c : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(mtime_base_c) + x"00000000");
92
  constant mtime_time_hi_addr_c : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(mtime_base_c) + x"00000004");
93
  constant mtime_cmp_lo_addr_c  : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(mtime_base_c) + x"00000008");
94
  constant mtime_cmp_hi_addr_c  : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(mtime_base_c) + x"0000000C");
95
 
96
  -- Universal Asynchronous Receiver/Transmitter (UART) --
97
  constant uart_base_c          : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFFA0"; -- base address, fixed!
98
  constant uart_size_c          : natural := 2*4; -- bytes, fixed!
99
  constant uart_ctrl_addr_c     : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(uart_base_c) + x"00000000");
100
  constant uart_rtx_addr_c      : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(uart_base_c) + x"00000004");
101
 
102
  -- Serial Peripheral Interface (SPI) --
103
  constant spi_base_c           : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFFA8"; -- base address, fixed!
104
  constant spi_size_c           : natural := 2*4; -- bytes, fixed!
105
  constant spi_ctrl_addr_c      : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(spi_base_c) + x"00000000");
106
  constant spi_rtx_addr_c       : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(spi_base_c) + x"00000004");
107
 
108
  -- Two Wire Interface (TWI) --
109
  constant twi_base_c           : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFFB0"; -- base address, fixed!
110
  constant twi_size_c           : natural := 2*4; -- bytes, fixed!
111
  constant twi_ctrl_addr_c      : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(twi_base_c) + x"00000000");
112
  constant twi_rtx_addr_c       : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(twi_base_c) + x"00000004");
113
 
114
  -- Pulse-Width Modulation Controller (PWM) --
115
  constant pwm_base_c           : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFFB8"; -- base address, fixed!
116
  constant pwm_size_c           : natural := 2*4; -- bytes, fixed!
117
  constant pwm_ctrl_addr_c      : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(pwm_base_c) + x"00000000");
118
  constant pwm_duty_addr_c      : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(pwm_base_c) + x"00000004");
119
 
120
  -- True Random Number generator (TRNG) --
121
  constant trng_base_c          : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFFC0"; -- base address, fixed!
122
  constant trng_size_c          : natural := 2*4; -- bytes, fixed!
123
  constant trng_ctrl_addr_c     : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(trng_base_c) + x"00000000");
124
  constant trng_data_addr_c     : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(trng_base_c) + x"00000004");
125
 
126
  -- RESERVED --
127
--constant ???_base_c           : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFFC8"; -- base address, fixed!
128 3 zero_gravi
--constant ???_size_c           : natural := 13*4; -- bytes, fixed!
129 2 zero_gravi
 
130 3 zero_gravi
  -- Dummy Device (with SIM output) (DEVNULL) --
131
  constant devnull_base_c       : std_ulogic_vector(data_width_c-1 downto 0) := x"FFFFFFFC"; -- base address, fixed!
132
  constant devnull_size_c       : natural := 1*4; -- bytes, fixed!
133
  constant devnull_data_addr_c  : std_ulogic_vector(31 downto 0) := std_ulogic_vector(unsigned(devnull_base_c) + x"00000000");
134
 
135 2 zero_gravi
  -- Main Control Bus -----------------------------------------------------------------------
136
  -- -------------------------------------------------------------------------------------------
137
  -- register file --
138
  constant ctrl_rf_in_mux_lsb_c   : natural :=  0; -- input source select lsb (00=ALU, 01=MEM)
139
  constant ctrl_rf_in_mux_msb_c   : natural :=  1; -- input source select msb (10=PC,  11=CSR)
140
  constant ctrl_rf_rs1_adr0_c     : natural :=  2; -- source register 1 address bit 0
141
  constant ctrl_rf_rs1_adr1_c     : natural :=  3; -- source register 1 address bit 1
142
  constant ctrl_rf_rs1_adr2_c     : natural :=  4; -- source register 1 address bit 2
143
  constant ctrl_rf_rs1_adr3_c     : natural :=  5; -- source register 1 address bit 3
144
  constant ctrl_rf_rs1_adr4_c     : natural :=  6; -- source register 1 address bit 4
145
  constant ctrl_rf_rs2_adr0_c     : natural :=  7; -- source register 2 address bit 0
146
  constant ctrl_rf_rs2_adr1_c     : natural :=  8; -- source register 2 address bit 1
147
  constant ctrl_rf_rs2_adr2_c     : natural :=  9; -- source register 2 address bit 2
148
  constant ctrl_rf_rs2_adr3_c     : natural := 10; -- source register 2 address bit 3
149
  constant ctrl_rf_rs2_adr4_c     : natural := 11; -- source register 2 address bit 4
150
  constant ctrl_rf_rd_adr0_c      : natural := 12; -- destiantion register address bit 0
151
  constant ctrl_rf_rd_adr1_c      : natural := 13; -- destiantion register address bit 1
152
  constant ctrl_rf_rd_adr2_c      : natural := 14; -- destiantion register address bit 2
153
  constant ctrl_rf_rd_adr3_c      : natural := 15; -- destiantion register address bit 3
154
  constant ctrl_rf_rd_adr4_c      : natural := 16; -- destiantion register address bit 4
155
  constant ctrl_rf_wb_en_c        : natural := 17; -- write back enable
156
  constant ctrl_rf_clear_rs1_c    : natural := 18; -- force rs1=r0
157
  constant ctrl_rf_clear_rs2_c    : natural := 19; -- force rs2=r0
158
  -- alu --
159
  constant ctrl_alu_cmd0_c        : natural := 20; -- ALU command bit 0
160
  constant ctrl_alu_cmd1_c        : natural := 21; -- ALU command bit 1
161
  constant ctrl_alu_cmd2_c        : natural := 22; -- ALU command bit 2
162
  constant ctrl_alu_opa_mux_lsb_c : natural := 23; -- operand A select lsb (00=rs1, 01=PC)
163
  constant ctrl_alu_opa_mux_msb_c : natural := 24; -- operand A select msb (10=CSR, 11=CSR)
164
  constant ctrl_alu_opb_mux_lsb_c : natural := 25; -- operand B select lsb (00=rs2, 01=PC)
165
  constant ctrl_alu_opb_mux_msb_c : natural := 26; -- operand B select msb (10=rs1, 11=PC_increment(2/4))
166
  constant ctrl_alu_opc_mux_c     : natural := 27; -- operand C select (0=IMM, 1=rs2)
167
  constant ctrl_alu_unsigned_c    : natural := 28; -- is unsigned ALU operation
168
  constant ctrl_alu_shift_dir_c   : natural := 29; -- shift direction (0=left, 1=right)
169
  constant ctrl_alu_shift_ar_c    : natural := 30; -- is arithmetic shift
170
  -- bus interface --
171
  constant ctrl_bus_size_lsb_c    : natural := 31; -- transfer size lsb (00=byte, 01=half-word)
172
  constant ctrl_bus_size_msb_c    : natural := 32; -- transfer size msb (10=word, 11=?)
173
  constant ctrl_bus_rd_c          : natural := 33; -- read data request
174
  constant ctrl_bus_wr_c          : natural := 34; -- write data request
175
  constant ctrl_bus_if_c          : natural := 35; -- instruction fetch request (output PC, otherwise output MAR)
176
  constant ctrl_bus_mar_we_c      : natural := 36; -- memory address register write enable
177
  constant ctrl_bus_mdo_we_c      : natural := 37; -- memory data out register write enable
178
  constant ctrl_bus_mdi_we_c      : natural := 38; -- memory data in register write enable
179
  constant ctrl_bus_unsigned_c    : natural := 39; -- is unsigned load
180
  -- csr/system --
181
  constant ctrl_csr_pc_we_c       : natural := 40; -- PC write enable
182
  constant ctrl_csr_re_c          : natural := 41; -- valid CSR read
183
  constant ctrl_csr_we_c          : natural := 42; -- valid CSR write
184
  -- co-processor --
185
  constant ctrl_cp_use_c          : natural := 43; -- is cp operation
186
  constant ctrl_cp_id_lsb_c       : natural := 44; -- cp select lsb
187
  constant ctrl_cp_id_msb_c       : natural := 45; -- cp select msb
188
  constant ctrl_cp_cmd0_c         : natural := 46; -- cp command bit 0
189
  constant ctrl_cp_cmd1_c         : natural := 47; -- cp command bit 1
190
  constant ctrl_cp_cmd2_c         : natural := 48; -- cp command bit 2
191
  -- control bus size --
192
  constant ctrl_width_c           : natural := 49; -- control bus size
193
 
194
  -- ALU Comparator Bus ---------------------------------------------------------------------
195
  -- -------------------------------------------------------------------------------------------
196
  constant alu_cmp_equal_c : natural := 0;
197
  constant alu_cmp_less_c  : natural := 1; -- for signed and unsigned
198
 
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
  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
 
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
      -- RISC-V CPU Extensions --
341
      CPU_EXTENSION_RISCV_C     : boolean := false;  -- implement compressed extension?
342
      CPU_EXTENSION_RISCV_E     : boolean := false;  -- implement embedded RF extension?
343
      CPU_EXTENSION_RISCV_M     : boolean := false;  -- implement muld/div extension?
344
      CPU_EXTENSION_RISCV_Zicsr : boolean := true;   -- implement CSR system?
345
      -- Memory configuration: Instruction memory --
346
      MEM_ISPACE_BASE           : std_ulogic_vector(31 downto 0) := x"00000000"; -- base address of instruction memory space
347
      MEM_ISPACE_SIZE           : natural := 16*1024; -- total size of instruction memory space in byte
348
      MEM_INT_IMEM_USE          : boolean := true;    -- implement processor-internal instruction memory
349
      MEM_INT_IMEM_SIZE         : natural := 16*1024; -- size of processor-internal instruction memory in bytes
350
      MEM_INT_IMEM_ROM          : boolean := false;   -- implement processor-internal instruction memory as ROM
351
      -- Memory configuration: Data memory --
352
      MEM_DSPACE_BASE           : std_ulogic_vector(31 downto 0) := x"80000000"; -- base address of data memory space
353
      MEM_DSPACE_SIZE           : natural := 8*1024; -- total size of data memory space in byte
354
      MEM_INT_DMEM_USE          : boolean := true;   -- implement processor-internal data memory
355
      MEM_INT_DMEM_SIZE         : natural := 8*1024; -- size of processor-internal data memory in bytes
356
      -- Memory configuration: External memory interface --
357
      MEM_EXT_USE               : boolean := false;  -- implement external memory bus interface?
358
      MEM_EXT_REG_STAGES        : natural := 2;      -- number of interface register stages (0,1,2)
359
      MEM_EXT_TIMEOUT           : natural := 15;     -- cycles after which a valid bus access will timeout (>=1)
360
      -- Processor peripherals --
361
      IO_GPIO_USE               : boolean := true;   -- implement general purpose input/output port unit (GPIO)?
362
      IO_MTIME_USE              : boolean := true;   -- implement machine system timer (MTIME)?
363
      IO_UART_USE               : boolean := true;   -- implement universal asynchronous receiver/transmitter (UART)?
364
      IO_SPI_USE                : boolean := true;   -- implement serial peripheral interface (SPI)?
365
      IO_TWI_USE                : boolean := true;   -- implement two-wire interface (TWI)?
366
      IO_PWM_USE                : boolean := true;   -- implement pulse-width modulation unit (PWM)?
367
      IO_WDT_USE                : boolean := true;   -- implement watch dog timer (WDT)?
368
      IO_CLIC_USE               : boolean := true;   -- implement core local interrupt controller (CLIC)?
369 3 zero_gravi
      IO_TRNG_USE               : boolean := false;  -- implement true random number generator (TRNG)?
370
      IO_DEVNULL_USE            : boolean := true    -- implement dummy device (DEVNULL)?
371 2 zero_gravi
    );
372
    port (
373
      -- Global control --
374
      clk_i      : in  std_ulogic := '0'; -- global clock, rising edge
375
      rstn_i     : in  std_ulogic := '0'; -- global reset, low-active, async
376
      -- Wishbone bus interface --
377
      wb_adr_o   : out std_ulogic_vector(31 downto 0); -- address
378
      wb_dat_i   : in  std_ulogic_vector(31 downto 0) := (others => '0'); -- read data
379
      wb_dat_o   : out std_ulogic_vector(31 downto 0); -- write data
380
      wb_we_o    : out std_ulogic; -- read/write
381
      wb_sel_o   : out std_ulogic_vector(03 downto 0); -- byte enable
382
      wb_stb_o   : out std_ulogic; -- strobe
383
      wb_cyc_o   : out std_ulogic; -- valid cycle
384
      wb_ack_i   : in  std_ulogic := '0'; -- transfer acknowledge
385
      wb_err_i   : in  std_ulogic := '0'; -- transfer error
386
      -- GPIO --
387
      gpio_o     : out std_ulogic_vector(15 downto 0); -- parallel output
388
      gpio_i     : in  std_ulogic_vector(15 downto 0) := (others => '0'); -- parallel input
389
      -- UART --
390
      uart_txd_o : out std_ulogic; -- UART send data
391
      uart_rxd_i : in  std_ulogic := '0'; -- UART receive data
392
      -- SPI --
393
      spi_sclk_o : out std_ulogic; -- serial clock line
394
      spi_mosi_o : out std_ulogic; -- serial data line out
395
      spi_miso_i : in  std_ulogic := '0'; -- serial data line in
396
      spi_csn_o  : out std_ulogic_vector(07 downto 0); -- SPI CS
397
      -- TWI --
398
      twi_sda_io : inout std_logic := 'H'; -- twi serial data line
399
      twi_scl_io : inout std_logic := 'H'; -- twi serial clock line
400
      -- PWM --
401
      pwm_o      : out std_ulogic_vector(03 downto 0);  -- pwm channels
402
      -- Interrupts --
403
      ext_irq_i  : in  std_ulogic_vector(01 downto 0) := (others => '0'); -- external interrupt request
404
      ext_ack_o  : out std_ulogic_vector(01 downto 0)  -- external interrupt request acknowledge
405
    );
406
  end component;
407
 
408
  -- Component: CPU Control -----------------------------------------------------------------
409
  -- -------------------------------------------------------------------------------------------
410
  component neorv32_cpu_control
411
    generic (
412
      -- General --
413
      CLOCK_FREQUENCY           : natural := 0; -- clock frequency of clk_i in Hz
414
      HART_ID                   : std_ulogic_vector(31 downto 0) := x"00000000"; -- custom hardware thread ID
415
      BOOTLOADER_USE            : boolean := true;   -- implement processor-internal bootloader?
416
      -- RISC-V CPU Extensions --
417
      CPU_EXTENSION_RISCV_C     : boolean := false;  -- implement compressed extension?
418
      CPU_EXTENSION_RISCV_E     : boolean := false;  -- implement embedded RF extension?
419
      CPU_EXTENSION_RISCV_M     : boolean := false;  -- implement muld/div extension?
420
      CPU_EXTENSION_RISCV_Zicsr : boolean := true;   -- implement CSR system?
421
      -- Memory configuration: Instruction memory --
422
      MEM_ISPACE_BASE           : std_ulogic_vector(31 downto 0) := x"00000000"; -- base address of instruction memory space
423
      MEM_ISPACE_SIZE           : natural := 16*1024; -- total size of instruction memory space in byte
424
      MEM_INT_IMEM_USE          : boolean := true;   -- implement processor-internal instruction memory
425
      MEM_INT_IMEM_SIZE         : natural := 16*1024; -- size of processor-internal instruction memory in bytes
426
      MEM_INT_IMEM_ROM          : boolean := false;  -- implement processor-internal instruction memory as ROM
427
      -- Memory configuration: Data memory --
428
      MEM_DSPACE_BASE           : std_ulogic_vector(31 downto 0) := x"80000000"; -- base address of data memory space
429
      MEM_DSPACE_SIZE           : natural := 8*1024; -- total size of data memory space in byte
430
      MEM_INT_DMEM_USE          : boolean := true;   -- implement processor-internal data memory
431
      MEM_INT_DMEM_SIZE         : natural := 8*1024; -- size of processor-internal data memory in bytes
432
      -- Memory configuration: External memory interface --
433
      MEM_EXT_USE               : boolean := false;  -- implement external memory bus interface?
434
      -- Processor peripherals --
435
      IO_GPIO_USE               : boolean := true;   -- implement general purpose input/output port unit (GPIO)?
436
      IO_MTIME_USE              : boolean := true;   -- implement machine system timer (MTIME)?
437
      IO_UART_USE               : boolean := true;   -- implement universal asynchronous receiver/transmitter (UART)?
438
      IO_SPI_USE                : boolean := true;   -- implement serial peripheral interface (SPI)?
439
      IO_TWI_USE                : boolean := true;   -- implement two-wire interface (TWI)?
440
      IO_PWM_USE                : boolean := true;   -- implement pulse-width modulation unit (PWM)?
441
      IO_WDT_USE                : boolean := true;   -- implement watch dog timer (WDT)?
442
      IO_CLIC_USE               : boolean := true;   -- implement core local interrupt controller (CLIC)?
443 3 zero_gravi
      IO_TRNG_USE               : boolean := true;   -- implement true random number generator (TRNG)?
444
      IO_DEVNULL_USE            : boolean := true    -- implement dummy device (DEVNULL)?
445 2 zero_gravi
    );
446
    port (
447
      -- global control --
448
      clk_i         : in  std_ulogic; -- global clock, rising edge
449
      rstn_i        : in  std_ulogic; -- global reset, low-active, async
450
      ctrl_o        : out std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
451
      -- status input --
452
      alu_wait_i    : in  std_ulogic; -- wait for ALU
453
      bus_wait_i    : in  std_ulogic; -- wait for bus
454
      -- data input --
455
      instr_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- instruction
456
      cmp_i         : in  std_ulogic_vector(1 downto 0); -- comparator status
457
      alu_add_i     : in  std_ulogic_vector(data_width_c-1 downto 0); -- ALU.add result
458
      -- data output --
459
      imm_o         : out std_ulogic_vector(data_width_c-1 downto 0); -- immediate
460
      pc_o          : out std_ulogic_vector(data_width_c-1 downto 0); -- current PC
461
      alu_pc_o      : out std_ulogic_vector(data_width_c-1 downto 0); -- delayed PC for ALU
462
      -- csr interface --
463
      csr_wdata_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- CSR write data
464
      csr_rdata_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- CSR read data
465
      -- external interrupt --
466
      clic_irq_i    : in  std_ulogic; -- CLIC interrupt request
467
      mtime_irq_i   : in  std_ulogic; -- machine timer interrupt
468
      -- bus access exceptions --
469
      mar_i         : in  std_ulogic_vector(data_width_c-1 downto 0); -- memory address register
470
      ma_instr_i    : in  std_ulogic; -- misaligned instruction address
471
      ma_load_i     : in  std_ulogic; -- misaligned load data address
472
      ma_store_i    : in  std_ulogic; -- misaligned store data address
473
      be_instr_i    : in  std_ulogic; -- bus error on instruction access
474
      be_load_i     : in  std_ulogic; -- bus error on load data access
475
      be_store_i    : in  std_ulogic; -- bus error on store data access
476
      bus_exc_ack_o : out std_ulogic  -- bus exception error acknowledge
477
    );
478
  end component;
479
 
480
  -- Component: CPU Register File -----------------------------------------------------------
481
  -- -------------------------------------------------------------------------------------------
482
  component neorv32_cpu_regfile
483
    generic (
484
      CPU_EXTENSION_RISCV_E : boolean := false -- implement embedded RF extension?
485
    );
486
    port (
487
      -- global control --
488
      clk_i  : in  std_ulogic; -- global clock, rising edge
489
      ctrl_i : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
490
      -- data input --
491
      mem_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- memory read data
492
      alu_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
493
      csr_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- CSR read data
494
      pc_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- current pc
495
      -- data output --
496
      rs1_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- operand 1
497
      rs2_o  : out std_ulogic_vector(data_width_c-1 downto 0)  -- operand 2
498
    );
499
  end component;
500
 
501
  -- Component: CPU ALU ---------------------------------------------------------------------
502
  -- -------------------------------------------------------------------------------------------
503
  component neorv32_cpu_alu
504
    generic (
505
      CPU_EXTENSION_RISCV_C : boolean := false; -- implement compressed extension?
506
      CPU_EXTENSION_RISCV_M : boolean := false  -- implement mul/div extension?
507
    );
508
    port (
509
      -- global control --
510
      clk_i       : in  std_ulogic; -- global clock, rising edge
511
      rstn_i      : in  std_ulogic; -- global reset, low-active, async
512
      ctrl_i      : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
513
      -- data input --
514
      rs1_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
515
      rs2_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
516
      pc_i        : in  std_ulogic_vector(data_width_c-1 downto 0); -- current PC
517
      pc2_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- delayed PC
518
      imm_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- immediate
519
      csr_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- csr read data
520
      -- data output --
521
      cmp_o       : out std_ulogic_vector(1 downto 0); -- comparator status
522
      add_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- OPA + OPB
523
      res_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
524
      -- co-processor interface --
525
      cp0_data_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- co-processor 0 result
526
      cp0_valid_i : in  std_ulogic; -- co-processor 0 result valid
527
      cp1_data_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- co-processor 1 result
528
      cp1_valid_i : in  std_ulogic; -- co-processor 1 result valid
529
      -- status --
530
      wait_o      : out std_ulogic -- busy due to iterative processing units
531
    );
532
  end component;
533
 
534
  -- Component: CPU Co-Processor MULDIV -----------------------------------------------------
535
  -- -------------------------------------------------------------------------------------------
536
  component neorv32_cpu_cp_muldiv
537
    port (
538
      -- global control --
539
      clk_i   : in  std_ulogic; -- global clock, rising edge
540
      rstn_i  : in  std_ulogic; -- global reset, low-active, async
541
      ctrl_i  : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
542
      -- data input --
543
      rs1_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
544
      rs2_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
545
      -- result and status --
546
      res_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- operation result
547
      valid_o : out std_ulogic -- data output valid
548
    );
549
  end component;
550
 
551
  -- Component: CPU Bus Interface -----------------------------------------------------------
552
  -- -------------------------------------------------------------------------------------------
553
  component neorv32_cpu_bus
554
    generic (
555
      CPU_EXTENSION_RISCV_C : boolean := false; -- implement compressed extension?
556
      MEM_EXT_TIMEOUT       : natural := 15     -- cycles after which a valid bus access will timeout
557
    );
558
    port (
559
      -- global control --
560
      clk_i       : in  std_ulogic; -- global clock, rising edge
561
      rstn_i      : in  std_ulogic; -- global reset, low-active, async
562
      ctrl_i      : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
563
      -- data input --
564
      wdata_i     : in  std_ulogic_vector(data_width_c-1 downto 0); -- write data
565
      pc_i        : in  std_ulogic_vector(data_width_c-1 downto 0); -- current PC
566
      alu_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
567
      -- data output --
568
      instr_o     : out std_ulogic_vector(data_width_c-1 downto 0); -- instruction
569
      rdata_o     : out std_ulogic_vector(data_width_c-1 downto 0); -- read data
570
      -- status --
571
      mar_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- current memory address register
572
      ma_instr_o  : out std_ulogic; -- misaligned instruction address
573
      ma_load_o   : out std_ulogic; -- misaligned load data address
574
      ma_store_o  : out std_ulogic; -- misaligned store data address
575
      be_instr_o  : out std_ulogic; -- bus error on instruction access
576
      be_load_o   : out std_ulogic; -- bus error on load data access
577
      be_store_o  : out std_ulogic; -- bus error on store data 
578
      bus_wait_o  : out std_ulogic; -- wait for bus operation to finish
579
      exc_ack_i   : in  std_ulogic; -- exception controller ACK
580
      -- bus system --
581
      bus_addr_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
582
      bus_rdata_i : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
583
      bus_wdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
584
      bus_ben_o   : out std_ulogic_vector(03 downto 0); -- byte enable
585
      bus_we_o    : out std_ulogic; -- write enable
586
      bus_re_o    : out std_ulogic; -- read enable
587
      bus_ack_i   : in  std_ulogic; -- bus transfer acknowledge
588
      bus_err_i   : in  std_ulogic  -- bus transfer error
589
    );
590
  end component;
591
 
592
  -- Component: CPU Compressed Instructions Decompressor ------------------------------------
593
  -- -------------------------------------------------------------------------------------------
594
  component neorv32_cpu_decompressor
595
    port (
596
      -- instruction input --
597
      ci_instr16_i : in  std_ulogic_vector(15 downto 0); -- compressed instruction input
598
      -- instruction output --
599
      ci_valid_o   : out std_ulogic; -- is a compressed instruction
600
      ci_illegal_o : out std_ulogic; -- is an illegal compressed instruction
601
      ci_instr32_o : out std_ulogic_vector(31 downto 0)  -- 32-bit decompressed instruction
602
    );
603
  end component;
604
 
605
  -- Component: CPU Top Entity --------------------------------------------------------------
606
  -- -------------------------------------------------------------------------------------------
607
  component neorv32_cpu
608
    generic (
609
      -- General --
610
      CLOCK_FREQUENCY           : natural := 0; -- clock frequency of clk_i in Hz
611
      HART_ID                   : std_ulogic_vector(31 downto 0) := x"00000000"; -- custom hardware thread ID
612
      BOOTLOADER_USE            : boolean := true;   -- implement processor-internal bootloader?
613
      -- RISC-V CPU Extensions --
614
      CPU_EXTENSION_RISCV_C     : boolean := false;  -- implement compressed extension?
615
      CPU_EXTENSION_RISCV_E     : boolean := false;  -- implement embedded RF extension?
616
      CPU_EXTENSION_RISCV_M     : boolean := false;  -- implement muld/div extension?
617
      CPU_EXTENSION_RISCV_Zicsr : boolean := true;   -- implement CSR system?
618
      -- Memory configuration: Instruction memory --
619
      MEM_ISPACE_BASE           : std_ulogic_vector(31 downto 0) := x"00000000"; -- base address of instruction memory space
620
      MEM_ISPACE_SIZE           : natural := 8*1024; -- total size of instruction memory space in byte
621
      MEM_INT_IMEM_USE          : boolean := true;   -- implement processor-internal instruction memory
622
      MEM_INT_IMEM_SIZE         : natural := 8*1024; -- size of processor-internal instruction memory in bytes
623
      MEM_INT_IMEM_ROM          : boolean := false;  -- implement processor-internal instruction memory as ROM
624
      -- Memory configuration: Data memory --
625
      MEM_DSPACE_BASE           : std_ulogic_vector(31 downto 0) := x"80000000"; -- base address of data memory space
626
      MEM_DSPACE_SIZE           : natural := 4*1024; -- total size of data memory space in byte
627
      MEM_INT_DMEM_USE          : boolean := true;   -- implement processor-internal data memory
628
      MEM_INT_DMEM_SIZE         : natural := 4*1024; -- size of processor-internal data memory in bytes
629
      -- Memory configuration: External memory interface --
630
      MEM_EXT_USE               : boolean := false;  -- implement external memory bus interface?
631
      MEM_EXT_TIMEOUT           : natural := 15;     -- cycles after which a valid bus access will timeout
632
      -- Processor peripherals --
633
      IO_GPIO_USE               : boolean := true;   -- implement general purpose input/output port unit (GPIO)?
634
      IO_MTIME_USE              : boolean := true;   -- implement machine system timer (MTIME)?
635
      IO_UART_USE               : boolean := true;   -- implement universal asynchronous receiver/transmitter (UART)?
636
      IO_SPI_USE                : boolean := true;   -- implement serial peripheral interface (SPI)?
637
      IO_TWI_USE                : boolean := true;   -- implement two-wire interface (TWI)?
638
      IO_PWM_USE                : boolean := true;   -- implement pulse-width modulation unit (PWM)?
639
      IO_WDT_USE                : boolean := true;   -- implement watch dog timer (WDT)?
640
      IO_CLIC_USE               : boolean := true;   -- implement core local interrupt controller (CLIC)?
641 3 zero_gravi
      IO_TRNG_USE               : boolean := true;   -- implement true random number generator (TRNG)?
642
      IO_DEVNULL_USE            : boolean := true    -- implement dummy device (DEVNULL)?
643 2 zero_gravi
    );
644
    port (
645
      -- global control --
646
      clk_i       : in  std_ulogic; -- global clock, rising edge
647
      rstn_i      : in  std_ulogic; -- global reset, low-active, async
648
      -- bus interface --
649
      bus_addr_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
650
      bus_rdata_i : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
651
      bus_wdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
652
      bus_ben_o   : out std_ulogic_vector(03 downto 0); -- byte enable
653
      bus_we_o    : out std_ulogic; -- write enable
654
      bus_re_o    : out std_ulogic; -- read enable
655
      bus_ack_i   : in  std_ulogic; -- bus transfer acknowledge
656
      bus_err_i   : in  std_ulogic; -- bus transfer error
657
      -- external interrupts --
658
      clic_irq_i  : in  std_ulogic; -- CLIC interrupt request
659
      mtime_irq_i : in  std_ulogic  -- machine timer interrupt
660
    );
661
  end component;
662
 
663
  -- Component: Processor-internal instruction memory (IMEM) --------------------------------
664
  -- -------------------------------------------------------------------------------------------
665
  component neorv32_imem
666
    generic (
667
      IMEM_BASE      : std_ulogic_vector(31 downto 0) := x"00000000"; -- memory base address
668
      IMEM_SIZE      : natural := 4*1024; -- processor-internal instruction memory size in bytes
669
      IMEM_AS_ROM    : boolean := false;  -- implement IMEM as read-only memory?
670
      BOOTLOADER_USE : boolean := true    -- implement and use bootloader?
671
    );
672
    port (
673
      clk_i  : in  std_ulogic; -- global clock line
674
      rden_i : in  std_ulogic; -- read enable
675
      wren_i : in  std_ulogic; -- write enable
676
      ben_i  : in  std_ulogic_vector(03 downto 0); -- byte write enable
677
      upen_i : in  std_ulogic; -- update enable
678
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
679
      data_i : in  std_ulogic_vector(31 downto 0); -- data in
680
      data_o : out std_ulogic_vector(31 downto 0); -- data out
681
      ack_o  : out std_ulogic -- transfer acknowledge
682
    );
683
  end component;
684
 
685
  -- Component: Processor-internal data memory (DMEM) ---------------------------------------
686
  -- -------------------------------------------------------------------------------------------
687
  component neorv32_dmem
688
    generic (
689
      DMEM_BASE : std_ulogic_vector(31 downto 0) := x"80000000"; -- memory base address
690
      DMEM_SIZE : natural := 4*1024  -- processor-internal instruction memory size in bytes
691
    );
692
    port (
693
      clk_i  : in  std_ulogic; -- global clock line
694
      rden_i : in  std_ulogic; -- read enable
695
      wren_i : in  std_ulogic; -- write enable
696
      ben_i  : in  std_ulogic_vector(03 downto 0); -- byte write enable
697
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
698
      data_i : in  std_ulogic_vector(31 downto 0); -- data in
699
      data_o : out std_ulogic_vector(31 downto 0); -- data out
700
      ack_o  : out std_ulogic -- transfer acknowledge
701
    );
702
  end component;
703
 
704
  -- Component: Processor-internal bootloader ROM (BOOTROM) ---------------------------------
705
  -- -------------------------------------------------------------------------------------------
706
  component neorv32_boot_rom
707
    port (
708
      clk_i  : in  std_ulogic; -- global clock line
709
      rden_i : in  std_ulogic; -- read enable
710
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
711
      data_o : out std_ulogic_vector(31 downto 0); -- data out
712
      ack_o  : out std_ulogic -- transfer acknowledge
713
    );
714
  end component;
715
 
716
  -- Component: Machine System Timer (mtime) ------------------------------------------------
717
  -- -------------------------------------------------------------------------------------------
718
  component neorv32_mtime
719
    port (
720
      -- host access --
721
      clk_i     : in  std_ulogic; -- global clock line
722
      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
      spi_sclk_o  : out std_ulogic; -- SPI serial clock
841
      spi_mosi_o  : out std_ulogic; -- SPI master out, slave in
842
      spi_miso_i  : in  std_ulogic; -- SPI master in, slave out
843
      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 3 zero_gravi
  -- Component: Dummy Device with SIM Output (DEVNULL) -------------------------------------
953
  -- -------------------------------------------------------------------------------------------
954
  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
end neorv32_package;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.