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

Subversion Repositories neorv32

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

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 4 zero_gravi
  constant hw_version_c : std_ulogic_vector(31 downto 0) := x"00000206"; -- 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
 
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 4 zero_gravi
  constant ctrl_bus_if_c          : natural := 35; -- instruction fetch request (1: output PC, 0: output MAR)
176 2 zero_gravi
  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 4 zero_gravi
  -- Component: CPU Top Entity --------------------------------------------------------------
409
  -- -------------------------------------------------------------------------------------------
410
  component neorv32_cpu
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 := 8*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 := 8*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 := 4*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 := 4*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
      MEM_EXT_TIMEOUT           : natural := 15;     -- cycles after which a valid bus access will timeout
435
      -- Processor peripherals --
436
      IO_GPIO_USE               : boolean := true;   -- implement general purpose input/output port unit (GPIO)?
437
      IO_MTIME_USE              : boolean := true;   -- implement machine system timer (MTIME)?
438
      IO_UART_USE               : boolean := true;   -- implement universal asynchronous receiver/transmitter (UART)?
439
      IO_SPI_USE                : boolean := true;   -- implement serial peripheral interface (SPI)?
440
      IO_TWI_USE                : boolean := true;   -- implement two-wire interface (TWI)?
441
      IO_PWM_USE                : boolean := true;   -- implement pulse-width modulation unit (PWM)?
442
      IO_WDT_USE                : boolean := true;   -- implement watch dog timer (WDT)?
443
      IO_CLIC_USE               : boolean := true;   -- implement core local interrupt controller (CLIC)?
444
      IO_TRNG_USE               : boolean := true;   -- implement true random number generator (TRNG)?
445
      IO_DEVNULL_USE            : boolean := true    -- implement dummy device (DEVNULL)?
446
    );
447
    port (
448
      -- global control --
449
      clk_i       : in  std_ulogic; -- global clock, rising edge
450
      rstn_i      : in  std_ulogic; -- global reset, low-active, async
451
      -- bus interface --
452
      bus_addr_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
453
      bus_rdata_i : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
454
      bus_wdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
455
      bus_ben_o   : out std_ulogic_vector(03 downto 0); -- byte enable
456
      bus_we_o    : out std_ulogic; -- write enable
457
      bus_re_o    : out std_ulogic; -- read enable
458
      bus_ack_i   : in  std_ulogic; -- bus transfer acknowledge
459
      bus_err_i   : in  std_ulogic; -- bus transfer error
460
      -- external interrupts --
461
      clic_irq_i  : in  std_ulogic; -- CLIC interrupt request
462
      mtime_irq_i : in  std_ulogic  -- machine timer interrupt
463
    );
464
  end component;
465
 
466 2 zero_gravi
  -- Component: CPU Control -----------------------------------------------------------------
467
  -- -------------------------------------------------------------------------------------------
468
  component neorv32_cpu_control
469
    generic (
470
      -- General --
471
      CLOCK_FREQUENCY           : natural := 0; -- clock frequency of clk_i in Hz
472
      HART_ID                   : std_ulogic_vector(31 downto 0) := x"00000000"; -- custom hardware thread ID
473
      BOOTLOADER_USE            : boolean := true;   -- implement processor-internal bootloader?
474
      -- RISC-V CPU Extensions --
475
      CPU_EXTENSION_RISCV_C     : boolean := false;  -- implement compressed extension?
476
      CPU_EXTENSION_RISCV_E     : boolean := false;  -- implement embedded RF extension?
477
      CPU_EXTENSION_RISCV_M     : boolean := false;  -- implement muld/div extension?
478
      CPU_EXTENSION_RISCV_Zicsr : boolean := true;   -- implement CSR system?
479
      -- Memory configuration: Instruction memory --
480
      MEM_ISPACE_BASE           : std_ulogic_vector(31 downto 0) := x"00000000"; -- base address of instruction memory space
481
      MEM_ISPACE_SIZE           : natural := 16*1024; -- total size of instruction memory space in byte
482
      MEM_INT_IMEM_USE          : boolean := true;   -- implement processor-internal instruction memory
483
      MEM_INT_IMEM_SIZE         : natural := 16*1024; -- size of processor-internal instruction memory in bytes
484
      MEM_INT_IMEM_ROM          : boolean := false;  -- implement processor-internal instruction memory as ROM
485
      -- Memory configuration: Data memory --
486
      MEM_DSPACE_BASE           : std_ulogic_vector(31 downto 0) := x"80000000"; -- base address of data memory space
487
      MEM_DSPACE_SIZE           : natural := 8*1024; -- total size of data memory space in byte
488
      MEM_INT_DMEM_USE          : boolean := true;   -- implement processor-internal data memory
489
      MEM_INT_DMEM_SIZE         : natural := 8*1024; -- size of processor-internal data memory in bytes
490
      -- Memory configuration: External memory interface --
491
      MEM_EXT_USE               : boolean := false;  -- implement external memory bus interface?
492
      -- Processor peripherals --
493
      IO_GPIO_USE               : boolean := true;   -- implement general purpose input/output port unit (GPIO)?
494
      IO_MTIME_USE              : boolean := true;   -- implement machine system timer (MTIME)?
495
      IO_UART_USE               : boolean := true;   -- implement universal asynchronous receiver/transmitter (UART)?
496
      IO_SPI_USE                : boolean := true;   -- implement serial peripheral interface (SPI)?
497
      IO_TWI_USE                : boolean := true;   -- implement two-wire interface (TWI)?
498
      IO_PWM_USE                : boolean := true;   -- implement pulse-width modulation unit (PWM)?
499
      IO_WDT_USE                : boolean := true;   -- implement watch dog timer (WDT)?
500
      IO_CLIC_USE               : boolean := true;   -- implement core local interrupt controller (CLIC)?
501 3 zero_gravi
      IO_TRNG_USE               : boolean := true;   -- implement true random number generator (TRNG)?
502
      IO_DEVNULL_USE            : boolean := true    -- implement dummy device (DEVNULL)?
503 2 zero_gravi
    );
504
    port (
505
      -- global control --
506
      clk_i         : in  std_ulogic; -- global clock, rising edge
507
      rstn_i        : in  std_ulogic; -- global reset, low-active, async
508
      ctrl_o        : out std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
509
      -- status input --
510
      alu_wait_i    : in  std_ulogic; -- wait for ALU
511
      bus_wait_i    : in  std_ulogic; -- wait for bus
512
      -- data input --
513
      instr_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- instruction
514
      cmp_i         : in  std_ulogic_vector(1 downto 0); -- comparator status
515
      alu_add_i     : in  std_ulogic_vector(data_width_c-1 downto 0); -- ALU.add result
516
      -- data output --
517
      imm_o         : out std_ulogic_vector(data_width_c-1 downto 0); -- immediate
518
      pc_o          : out std_ulogic_vector(data_width_c-1 downto 0); -- current PC
519
      alu_pc_o      : out std_ulogic_vector(data_width_c-1 downto 0); -- delayed PC for ALU
520
      -- csr interface --
521
      csr_wdata_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- CSR write data
522
      csr_rdata_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- CSR read data
523
      -- external interrupt --
524
      clic_irq_i    : in  std_ulogic; -- CLIC interrupt request
525
      mtime_irq_i   : in  std_ulogic; -- machine timer interrupt
526
      -- bus access exceptions --
527
      mar_i         : in  std_ulogic_vector(data_width_c-1 downto 0); -- memory address register
528
      ma_instr_i    : in  std_ulogic; -- misaligned instruction address
529
      ma_load_i     : in  std_ulogic; -- misaligned load data address
530
      ma_store_i    : in  std_ulogic; -- misaligned store data address
531
      be_instr_i    : in  std_ulogic; -- bus error on instruction access
532
      be_load_i     : in  std_ulogic; -- bus error on load data access
533
      be_store_i    : in  std_ulogic; -- bus error on store data access
534
      bus_exc_ack_o : out std_ulogic  -- bus exception error acknowledge
535
    );
536
  end component;
537
 
538
  -- Component: CPU Register File -----------------------------------------------------------
539
  -- -------------------------------------------------------------------------------------------
540
  component neorv32_cpu_regfile
541
    generic (
542
      CPU_EXTENSION_RISCV_E : boolean := false -- implement embedded RF extension?
543
    );
544
    port (
545
      -- global control --
546
      clk_i  : in  std_ulogic; -- global clock, rising edge
547
      ctrl_i : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
548
      -- data input --
549
      mem_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- memory read data
550
      alu_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
551
      csr_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- CSR read data
552
      pc_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- current pc
553
      -- data output --
554
      rs1_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- operand 1
555
      rs2_o  : out std_ulogic_vector(data_width_c-1 downto 0)  -- operand 2
556
    );
557
  end component;
558
 
559
  -- Component: CPU ALU ---------------------------------------------------------------------
560
  -- -------------------------------------------------------------------------------------------
561
  component neorv32_cpu_alu
562
    generic (
563
      CPU_EXTENSION_RISCV_C : boolean := false; -- implement compressed extension?
564
      CPU_EXTENSION_RISCV_M : boolean := false  -- implement mul/div extension?
565
    );
566
    port (
567
      -- global control --
568
      clk_i       : in  std_ulogic; -- global clock, rising edge
569
      rstn_i      : in  std_ulogic; -- global reset, low-active, async
570
      ctrl_i      : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
571
      -- data input --
572
      rs1_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
573
      rs2_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
574
      pc_i        : in  std_ulogic_vector(data_width_c-1 downto 0); -- current PC
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
      CPU_EXTENSION_RISCV_C : boolean := false; -- implement compressed extension?
614
      MEM_EXT_TIMEOUT       : natural := 15     -- cycles after which a valid bus access will timeout
615
    );
616
    port (
617
      -- global control --
618
      clk_i       : in  std_ulogic; -- global clock, rising edge
619
      rstn_i      : in  std_ulogic; -- global reset, low-active, async
620
      ctrl_i      : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
621
      -- data input --
622
      wdata_i     : in  std_ulogic_vector(data_width_c-1 downto 0); -- write data
623
      pc_i        : in  std_ulogic_vector(data_width_c-1 downto 0); -- current PC
624
      alu_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
625
      -- data output --
626
      instr_o     : out std_ulogic_vector(data_width_c-1 downto 0); -- instruction
627
      rdata_o     : out std_ulogic_vector(data_width_c-1 downto 0); -- read data
628
      -- status --
629
      mar_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- current memory address register
630
      ma_instr_o  : out std_ulogic; -- misaligned instruction address
631
      ma_load_o   : out std_ulogic; -- misaligned load data address
632
      ma_store_o  : out std_ulogic; -- misaligned store data address
633
      be_instr_o  : out std_ulogic; -- bus error on instruction access
634
      be_load_o   : out std_ulogic; -- bus error on load data access
635
      be_store_o  : out std_ulogic; -- bus error on store data 
636
      bus_wait_o  : out std_ulogic; -- wait for bus operation to finish
637
      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_valid_o   : out std_ulogic; -- is a compressed instruction
658
      ci_illegal_o : out std_ulogic; -- is an illegal compressed instruction
659
      ci_instr32_o : out std_ulogic_vector(31 downto 0)  -- 32-bit decompressed instruction
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 4 zero_gravi
      rstn_i    : in  std_ulogic := '0'; -- global reset, low-active, async
723 2 zero_gravi
      addr_i    : in  std_ulogic_vector(31 downto 0); -- address
724
      rden_i    : in  std_ulogic; -- read enable
725
      wren_i    : in  std_ulogic; -- write enable
726
      ben_i     : in  std_ulogic_vector(03 downto 0); -- byte write enable
727
      data_i    : in  std_ulogic_vector(31 downto 0); -- data in
728
      data_o    : out std_ulogic_vector(31 downto 0); -- data out
729
      ack_o     : out std_ulogic; -- transfer acknowledge
730
      -- interrupt --
731
      irq_o     : out std_ulogic  -- interrupt request
732
    );
733
  end component;
734
 
735
  -- Component: General Purpose Input/Output Port (GPIO) ------------------------------------
736
  -- -------------------------------------------------------------------------------------------
737
  component neorv32_gpio
738
    port (
739
      -- host access --
740
      clk_i  : in  std_ulogic; -- global clock line
741
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
742
      rden_i : in  std_ulogic; -- read enable
743
      wren_i : in  std_ulogic; -- write enable
744
      ben_i  : in  std_ulogic_vector(03 downto 0); -- byte write enable
745
      data_i : in  std_ulogic_vector(31 downto 0); -- data in
746
      data_o : out std_ulogic_vector(31 downto 0); -- data out
747
      ack_o  : out std_ulogic; -- transfer acknowledge
748
      -- parallel io --
749
      gpio_o : out std_ulogic_vector(15 downto 0);
750
      gpio_i : in  std_ulogic_vector(15 downto 0);
751
      -- interrupt --
752
      irq_o  : out std_ulogic
753
    );
754
  end component;
755
 
756
  -- Component: Core Local Interrupt Controller (CLIC) --------------------------------------
757
  -- -------------------------------------------------------------------------------------------
758
  component neorv32_clic
759
    port (
760
      -- host access --
761
      clk_i     : in  std_ulogic; -- global clock line
762
      addr_i    : in  std_ulogic_vector(31 downto 0); -- address
763
      rden_i    : in  std_ulogic; -- read enable
764
      wren_i    : in  std_ulogic; -- write enable
765
      ben_i     : in  std_ulogic_vector(03 downto 0); -- byte write enable
766
      data_i    : in  std_ulogic_vector(31 downto 0); -- data in
767
      data_o    : out std_ulogic_vector(31 downto 0); -- data out
768
      ack_o     : out std_ulogic; -- transfer acknowledge
769
      -- cpu interrupt --
770
      cpu_irq_o : out std_ulogic; -- trigger CPU's external IRQ
771
      -- external interrupt lines --
772
      ext_irq_i : in  std_ulogic_vector(07 downto 0); -- IRQ, triggering on HIGH level
773
      ext_ack_o : out std_ulogic_vector(07 downto 0)  -- acknowledge
774
    );
775
  end component;
776
 
777
  -- Component: Watchdog Timer (WDT) --------------------------------------------------------
778
  -- -------------------------------------------------------------------------------------------
779
  component neorv32_wdt
780
    port (
781
      -- host access --
782
      clk_i       : in  std_ulogic; -- global clock line
783
      rstn_i      : in  std_ulogic; -- global reset line, low-active
784
      rden_i      : in  std_ulogic; -- read enable
785
      wren_i      : in  std_ulogic; -- write enable
786
      ben_i       : in  std_ulogic_vector(03 downto 0); -- byte write enable
787
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
788
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
789
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
790
      ack_o       : out std_ulogic; -- transfer acknowledge
791
      -- clock generator --
792
      clkgen_en_o : out std_ulogic; -- enable clock generator
793
      clkgen_i    : in  std_ulogic_vector(07 downto 0);
794
      -- timeout event --
795
      irq_o       : out std_ulogic; -- timeout IRQ
796
      rstn_o      : out std_ulogic  -- timeout reset, low_active, use it as async!
797
    );
798
  end component;
799
 
800
  -- Component: Universal Asynchronous Receiver and Transmitter (UART) ----------------------
801
  -- -------------------------------------------------------------------------------------------
802
  component neorv32_uart
803
    port (
804
      -- host access --
805
      clk_i       : in  std_ulogic; -- global clock line
806
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
807
      rden_i      : in  std_ulogic; -- read enable
808
      wren_i      : in  std_ulogic; -- write enable
809
      ben_i       : in  std_ulogic_vector(03 downto 0); -- byte write enable
810
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
811
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
812
      ack_o       : out std_ulogic; -- transfer acknowledge
813
      -- clock generator --
814
      clkgen_en_o : out std_ulogic; -- enable clock generator
815
      clkgen_i    : in  std_ulogic_vector(07 downto 0);
816
      -- com lines --
817
      uart_txd_o  : out std_ulogic;
818
      uart_rxd_i  : in  std_ulogic;
819
      -- interrupts --
820
      uart_irq_o  : out std_ulogic  -- uart rx/tx interrupt
821
    );
822
  end component;
823
 
824
  -- Component: Serial Peripheral Interface (SPI) -------------------------------------------
825
  -- -------------------------------------------------------------------------------------------
826
  component neorv32_spi
827
    port (
828
      -- host access --
829
      clk_i       : in  std_ulogic; -- global clock line
830
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
831
      rden_i      : in  std_ulogic; -- read enable
832
      wren_i      : in  std_ulogic; -- write enable
833
      ben_i       : in  std_ulogic_vector(03 downto 0); -- byte write enable
834
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
835
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
836
      ack_o       : out std_ulogic; -- transfer acknowledge
837
      -- clock generator --
838
      clkgen_en_o : out std_ulogic; -- enable clock generator
839
      clkgen_i    : in  std_ulogic_vector(07 downto 0);
840
      -- com lines --
841
      spi_sclk_o  : out std_ulogic; -- SPI serial clock
842
      spi_mosi_o  : out std_ulogic; -- SPI master out, slave in
843
      spi_miso_i  : in  std_ulogic; -- SPI master in, slave out
844
      spi_csn_o   : out std_ulogic_vector(07 downto 0); -- SPI CS
845
      -- interrupt --
846
      spi_irq_o   : out std_ulogic -- transmission done interrupt
847
    );
848
  end component;
849
 
850
  -- Component: Two-Wire Interface (TWI) ----------------------------------------------------
851
  -- -------------------------------------------------------------------------------------------
852
  component neorv32_twi
853
    port (
854
      -- host access --
855
      clk_i       : in  std_ulogic; -- global clock line
856
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
857
      rden_i      : in  std_ulogic; -- read enable
858
      wren_i      : in  std_ulogic; -- write enable
859
      ben_i       : in  std_ulogic_vector(03 downto 0); -- byte write enable
860
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
861
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
862
      ack_o       : out std_ulogic; -- transfer acknowledge
863
      -- clock generator --
864
      clkgen_en_o : out std_ulogic; -- enable clock generator
865
      clkgen_i    : in  std_ulogic_vector(07 downto 0);
866
      -- com lines --
867
      twi_sda_io  : inout std_logic; -- serial data line
868
      twi_scl_io  : inout std_logic; -- serial clock line
869
      -- interrupt --
870
      twi_irq_o   : out std_ulogic -- transfer done IRQ
871
    );
872
  end component;
873
 
874
  -- Component: Pulse-Width Modulation Controller (PWM) -------------------------------------
875
  -- -------------------------------------------------------------------------------------------
876
  component neorv32_pwm
877
    port (
878
      -- host access --
879
      clk_i       : in  std_ulogic; -- global clock line
880
      addr_i      : in  std_ulogic_vector(31 downto 0); -- address
881
      rden_i      : in  std_ulogic; -- read enable
882
      wren_i      : in  std_ulogic; -- write enable
883
      ben_i       : in  std_ulogic_vector(03 downto 0); -- byte write enable
884
      data_i      : in  std_ulogic_vector(31 downto 0); -- data in
885
      data_o      : out std_ulogic_vector(31 downto 0); -- data out
886
      ack_o       : out std_ulogic; -- transfer acknowledge
887
      -- clock generator --
888
      clkgen_en_o : out std_ulogic; -- enable clock generator
889
      clkgen_i    : in  std_ulogic_vector(07 downto 0);
890
      -- pwm output channels --
891
      pwm_o       : out std_ulogic_vector(03 downto 0)
892
    );
893
  end component;
894
 
895
  -- Component: True Random Number Generator (TRNG) -----------------------------------------
896
  -- -------------------------------------------------------------------------------------------
897
  component neorv32_trng
898
    port (
899
      -- host access --
900
      clk_i  : in  std_ulogic; -- global clock line
901
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
902
      rden_i : in  std_ulogic; -- read enable
903
      wren_i : in  std_ulogic; -- write enable
904
      ben_i  : in  std_ulogic_vector(03 downto 0); -- byte write enable
905
      data_i : in  std_ulogic_vector(31 downto 0); -- data in
906
      data_o : out std_ulogic_vector(31 downto 0); -- data out
907
      ack_o  : out std_ulogic  -- transfer acknowledge
908
    );
909
  end component;
910
 
911
  -- Component: Wishbone Bus Gateway (WISHBONE) ---------------------------------------------
912
  -- -------------------------------------------------------------------------------------------
913
  component neorv32_wishbone
914
    generic (
915
      INTERFACE_REG_STAGES : natural := 2; -- number of interface register stages (0,1,2)
916
      -- Memory configuration: Instruction memory --
917
      MEM_ISPACE_BASE      : std_ulogic_vector(31 downto 0) := x"00000000"; -- base address of instruction memory space
918
      MEM_ISPACE_SIZE      : natural := 8*1024; -- total size of instruction memory space in byte
919
      MEM_INT_IMEM_USE     : boolean := true;   -- implement processor-internal instruction memory
920
      MEM_INT_IMEM_SIZE    : natural := 8*1024; -- size of processor-internal instruction memory in bytes
921
      -- Memory configuration: Data memory --
922
      MEM_DSPACE_BASE      : std_ulogic_vector(31 downto 0) := x"80000000"; -- base address of data memory space
923
      MEM_DSPACE_SIZE      : natural := 4*1024; -- total size of data memory space in byte
924
      MEM_INT_DMEM_USE     : boolean := true;   -- implement processor-internal data memory
925
      MEM_INT_DMEM_SIZE    : natural := 4*1024  -- size of processor-internal data memory in bytes
926
    );
927
    port (
928
      -- global control --
929
      clk_i    : in  std_ulogic; -- global clock line
930
      rstn_i   : in  std_ulogic; -- global reset line, low-active
931
      -- host access --
932
      addr_i   : in  std_ulogic_vector(31 downto 0); -- address
933
      rden_i   : in  std_ulogic; -- read enable
934
      wren_i   : in  std_ulogic; -- write enable
935
      ben_i    : in  std_ulogic_vector(03 downto 0); -- byte write enable
936
      data_i   : in  std_ulogic_vector(31 downto 0); -- data in
937
      data_o   : out std_ulogic_vector(31 downto 0); -- data out
938
      ack_o    : out std_ulogic; -- transfer acknowledge
939
      err_o    : out std_ulogic; -- transfer error
940
      -- wishbone interface --
941
      wb_adr_o : out std_ulogic_vector(31 downto 0); -- address
942
      wb_dat_i : in  std_ulogic_vector(31 downto 0); -- read data
943
      wb_dat_o : out std_ulogic_vector(31 downto 0); -- write data
944
      wb_we_o  : out std_ulogic; -- read/write
945
      wb_sel_o : out std_ulogic_vector(03 downto 0); -- byte enable
946
      wb_stb_o : out std_ulogic; -- strobe
947
      wb_cyc_o : out std_ulogic; -- valid cycle
948
      wb_ack_i : in  std_ulogic; -- transfer acknowledge
949
      wb_err_i : in  std_ulogic  -- transfer error
950
    );
951
  end component;
952
 
953 4 zero_gravi
  ---- Component: Dummy Device with SIM Output (DEVNULL) -------------------------------------
954
  ---- -------------------------------------------------------------------------------------------
955 3 zero_gravi
  component neorv32_devnull
956
    port (
957
      -- host access --
958
      clk_i  : in  std_ulogic; -- global clock line
959
      addr_i : in  std_ulogic_vector(31 downto 0); -- address
960
      rden_i : in  std_ulogic; -- read enable
961
      wren_i : in  std_ulogic; -- write enable
962
      ben_i  : in  std_ulogic_vector(03 downto 0); -- byte write enable
963
      data_i : in  std_ulogic_vector(31 downto 0); -- data in
964
      data_o : out std_ulogic_vector(31 downto 0); -- data out
965
      ack_o  : out std_ulogic  -- transfer acknowledge
966
    );
967
  end component;
968
 
969 2 zero_gravi
end neorv32_package;
970
 
971
package body neorv32_package is
972
 
973
  -- Function: Minimal required bit width ---------------------------------------------------
974
  -- -------------------------------------------------------------------------------------------
975
  function index_size_f(input : natural) return natural is
976
  begin
977
    for i in 0 to natural'high loop
978
      if (2**i >= input) then
979
        return i;
980
      end if;
981
    end loop; -- i
982
    return 0;
983
  end function index_size_f;
984
 
985
  -- Function: Conditional select natural ---------------------------------------------------
986
  -- -------------------------------------------------------------------------------------------
987
  function cond_sel_natural_f(cond : boolean; val_t : natural; val_f : natural) return natural is
988
  begin
989
    if (cond = true) then
990
      return val_t;
991
    else
992
      return val_f;
993
    end if;
994
  end function cond_sel_natural_f;
995
 
996
  -- Function: Conditional select std_ulogic_vector -----------------------------------------
997
  -- -------------------------------------------------------------------------------------------
998
  function cond_sel_stdulogicvector_f(cond : boolean; val_t : std_ulogic_vector; val_f : std_ulogic_vector) return std_ulogic_vector is
999
  begin
1000
    if (cond = true) then
1001
      return val_t;
1002
    else
1003
      return val_f;
1004
    end if;
1005
  end function cond_sel_stdulogicvector_f;
1006
 
1007
  -- Function: Convert BOOL to STD_ULOGIC ---------------------------------------------------
1008
  -- -------------------------------------------------------------------------------------------
1009
  function bool_to_ulogic_f(cond : boolean) return std_ulogic is
1010
  begin
1011
    if (cond = true) then
1012
      return '1';
1013
    else
1014
      return '0';
1015
    end if;
1016
  end function bool_to_ulogic_f;
1017
 
1018
  -- Function: OR all bits ------------------------------------------------------------------
1019
  -- -------------------------------------------------------------------------------------------
1020
  function or_all_f(a : std_ulogic_vector) return std_ulogic is
1021
    variable tmp_v : std_ulogic;
1022
  begin
1023
    tmp_v := a(a'low);
1024
    for i in a'low+1 to a'high loop
1025
      tmp_v := tmp_v or a(i);
1026
    end loop; -- i
1027
    return tmp_v;
1028
  end function or_all_f;
1029
 
1030
  -- Function: AND all bits -----------------------------------------------------------------
1031
  -- -------------------------------------------------------------------------------------------
1032
  function and_all_f(a : std_ulogic_vector) return std_ulogic is
1033
    variable tmp_v : std_ulogic;
1034
  begin
1035
    tmp_v := a(a'low);
1036
    for i in a'low+1 to a'high loop
1037
      tmp_v := tmp_v and a(i);
1038
    end loop; -- i
1039
    return tmp_v;
1040
  end function and_all_f;
1041
 
1042
  -- Function: XOR all bits -----------------------------------------------------------------
1043
  -- -------------------------------------------------------------------------------------------
1044
  function xor_all_f(a : std_ulogic_vector) return std_ulogic is
1045
    variable tmp_v : std_ulogic;
1046
  begin
1047
    tmp_v := a(a'low);
1048
    for i in a'low+1 to a'high loop
1049
      tmp_v := tmp_v xor a(i);
1050
    end loop; -- i
1051
    return tmp_v;
1052
  end function xor_all_f;
1053
 
1054
  -- Function: XNOR all bits ----------------------------------------------------------------
1055
  -- -------------------------------------------------------------------------------------------
1056
  function xnor_all_f(a : std_ulogic_vector) return std_ulogic is
1057
    variable tmp_v : std_ulogic;
1058
  begin
1059
    tmp_v := a(a'low);
1060
    for i in a'low+1 to a'high loop
1061
      tmp_v := tmp_v xnor a(i);
1062
    end loop; -- i
1063
    return tmp_v;
1064
  end function xnor_all_f;
1065
 
1066
end neorv32_package;

powered by: WebSVN 2.1.0

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