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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_cpu.vhd] - Blame information for rev 11

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

Line No. Rev Author Line
1 2 zero_gravi
-- #################################################################################################
2
-- # << NEORV32 - CPU Top Entity >>                                                                #
3
-- # ********************************************************************************************* #
4
-- # Top NEORV32 CPU:                                                                              #
5
-- # * neorv32_cpu_alu: Arithemtical/logical unit                                                  #
6
-- # * neorv32_cpu_ctrl: CPU control and CSR system                                                #
7
-- #   * neorv32_cpu_decompressor: Compressed instructions decoder                                 #
8
-- # * neorv32_cpu_bus: Memory/IO bus interface unit                                               #
9
-- # * neorv32_cpu_cp_muldiv: MULDIV co-processor                                                  #
10
-- # * neorv32_cpu_regfile: Data register file                                                     #
11
-- # ********************************************************************************************* #
12
-- # BSD 3-Clause License                                                                          #
13
-- #                                                                                               #
14
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
15
-- #                                                                                               #
16
-- # Redistribution and use in source and binary forms, with or without modification, are          #
17
-- # permitted provided that the following conditions are met:                                     #
18
-- #                                                                                               #
19
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
20
-- #    conditions and the following disclaimer.                                                   #
21
-- #                                                                                               #
22
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
23
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
24
-- #    provided with the distribution.                                                            #
25
-- #                                                                                               #
26
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
27
-- #    endorse or promote products derived from this software without specific prior written      #
28
-- #    permission.                                                                                #
29
-- #                                                                                               #
30
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
31
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
32
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
33
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
34
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
35
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
36
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
37
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
38
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
39
-- # ********************************************************************************************* #
40
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
41
-- #################################################################################################
42
 
43
library ieee;
44
use ieee.std_logic_1164.all;
45
use ieee.numeric_std.all;
46
 
47
library neorv32;
48
use neorv32.neorv32_package.all;
49
 
50
entity neorv32_cpu is
51
  generic (
52
    -- General --
53 8 zero_gravi
    CLOCK_FREQUENCY              : natural := 0; -- clock frequency of clk_i in Hz
54
    HART_ID                      : std_ulogic_vector(31 downto 0) := x"00000000"; -- custom hardware thread ID
55
    BOOTLOADER_USE               : boolean := true;   -- implement processor-internal bootloader?
56
    CSR_COUNTERS_USE             : boolean := true;   -- implement RISC-V perf. counters ([m]instret[h], [m]cycle[h], time[h])?
57 2 zero_gravi
    -- RISC-V CPU Extensions --
58 8 zero_gravi
    CPU_EXTENSION_RISCV_C        : boolean := false;  -- implement compressed extension?
59
    CPU_EXTENSION_RISCV_E        : boolean := false;  -- implement embedded RF extension?
60
    CPU_EXTENSION_RISCV_M        : boolean := false;  -- implement muld/div extension?
61
    CPU_EXTENSION_RISCV_Zicsr    : boolean := true;   -- implement CSR system?
62
    CPU_EXTENSION_RISCV_Zifencei : boolean := true;   -- implement instruction stream sync.?
63 2 zero_gravi
    -- Memory configuration: Instruction memory --
64 8 zero_gravi
    MEM_ISPACE_BASE              : std_ulogic_vector(31 downto 0) := x"00000000"; -- base address of instruction memory space
65
    MEM_ISPACE_SIZE              : natural := 8*1024; -- total size of instruction memory space in byte
66
    MEM_INT_IMEM_USE             : boolean := true;   -- implement processor-internal instruction memory
67
    MEM_INT_IMEM_SIZE            : natural := 8*1024; -- size of processor-internal instruction memory in bytes
68
    MEM_INT_IMEM_ROM             : boolean := false;  -- implement processor-internal instruction memory as ROM
69 2 zero_gravi
    -- Memory configuration: Data memory --
70 8 zero_gravi
    MEM_DSPACE_BASE              : std_ulogic_vector(31 downto 0) := x"80000000"; -- base address of data memory space
71
    MEM_DSPACE_SIZE              : natural := 4*1024; -- total size of data memory space in byte
72
    MEM_INT_DMEM_USE             : boolean := true;   -- implement processor-internal data memory
73
    MEM_INT_DMEM_SIZE            : natural := 4*1024; -- size of processor-internal data memory in bytes
74 2 zero_gravi
    -- Memory configuration: External memory interface --
75 8 zero_gravi
    MEM_EXT_USE                  : boolean := false;  -- implement external memory bus interface?
76
    MEM_EXT_TIMEOUT              : natural := 15;     -- cycles after which a valid bus access will timeout
77 2 zero_gravi
    -- Processor peripherals --
78 8 zero_gravi
    IO_GPIO_USE                  : boolean := true;   -- implement general purpose input/output port unit (GPIO)?
79
    IO_MTIME_USE                 : boolean := true;   -- implement machine system timer (MTIME)?
80
    IO_UART_USE                  : boolean := true;   -- implement universal asynchronous receiver/transmitter (UART)?
81
    IO_SPI_USE                   : boolean := true;   -- implement serial peripheral interface (SPI)?
82
    IO_TWI_USE                   : boolean := true;   -- implement two-wire interface (TWI)?
83
    IO_PWM_USE                   : boolean := true;   -- implement pulse-width modulation unit (PWM)?
84
    IO_WDT_USE                   : boolean := true;   -- implement watch dog timer (WDT)?
85
    IO_CLIC_USE                  : boolean := true;   -- implement core local interrupt controller (CLIC)?
86
    IO_TRNG_USE                  : boolean := true;   -- implement true random number generator (TRNG)?
87
    IO_DEVNULL_USE               : boolean := true    -- implement dummy device (DEVNULL)?
88 2 zero_gravi
  );
89
  port (
90
    -- global control --
91 11 zero_gravi
    clk_i        : in  std_ulogic; -- global clock, rising edge
92
    rstn_i       : in  std_ulogic; -- global reset, low-active, async
93 2 zero_gravi
    -- bus interface --
94 11 zero_gravi
    bus_addr_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
95
    bus_rdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
96
    bus_wdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
97
    bus_ben_o    : out std_ulogic_vector(03 downto 0); -- byte enable
98
    bus_we_o     : out std_ulogic; -- write enable
99
    bus_re_o     : out std_ulogic; -- read enable
100
    bus_cancel_o : out std_ulogic; -- cancel current bus transaction
101
    bus_ack_i    : in  std_ulogic; -- bus transfer acknowledge
102
    bus_err_i    : in  std_ulogic; -- bus transfer error
103
    -- system time input from MTIME --
104
    time_i       : in  std_ulogic_vector(63 downto 0); -- current system time
105 2 zero_gravi
    -- external interrupts --
106 11 zero_gravi
    clic_irq_i   : in  std_ulogic; -- CLIC interrupt request
107
    mtime_irq_i  : in  std_ulogic  -- machine timer interrupt
108 2 zero_gravi
  );
109
end neorv32_cpu;
110
 
111
architecture neorv32_cpu_rtl of neorv32_cpu is
112
 
113
  -- local signals --
114
  signal ctrl        : std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
115
  signal alu_cmp     : std_ulogic_vector(1 downto 0); -- alu comparator result
116
  signal imm         : std_ulogic_vector(data_width_c-1 downto 0); -- immediate
117
  signal instr       : std_ulogic_vector(data_width_c-1 downto 0); -- new instruction
118
  signal rs1, rs2    : std_ulogic_vector(data_width_c-1 downto 0); -- source registers
119
  signal alu_res     : std_ulogic_vector(data_width_c-1 downto 0); -- alu result
120
  signal alu_add     : std_ulogic_vector(data_width_c-1 downto 0); -- alu adder result
121
  signal rdata       : std_ulogic_vector(data_width_c-1 downto 0); -- memory read data
122
  signal alu_wait    : std_ulogic; -- alu is busy due to iterative unit
123
  signal bus_wait    : std_ulogic; -- wait for bus to finish operation
124
  signal csr_rdata   : std_ulogic_vector(data_width_c-1 downto 0); -- csr read data
125
  signal mar         : std_ulogic_vector(data_width_c-1 downto 0); -- current memory address register
126
  signal ma_instr    : std_ulogic; -- misaligned instruction address
127
  signal ma_load     : std_ulogic; -- misaligned load data address
128
  signal ma_store    : std_ulogic; -- misaligned store data address
129
  signal be_instr    : std_ulogic; -- bus error on instruction access
130
  signal be_load     : std_ulogic; -- bus error on load data access
131
  signal be_store    : std_ulogic; -- bus error on store data access
132 6 zero_gravi
  signal bus_busy    : std_ulogic; -- bus unit is busy
133
  signal fetch_pc    : std_ulogic_vector(data_width_c-1 downto 0); -- pc for instruction fetch
134
  signal curr_pc     : std_ulogic_vector(data_width_c-1 downto 0); -- current pc (for current executed instruction)
135
  signal next_pc     : std_ulogic_vector(data_width_c-1 downto 0); -- next pc (for current executed instruction)
136 2 zero_gravi
 
137
  -- co-processor interface --
138
  signal cp0_data,  cp1_data  : std_ulogic_vector(data_width_c-1 downto 0);
139
  signal cp0_valid, cp1_valid : std_ulogic;
140
 
141
begin
142
 
143
  -- Control Unit ---------------------------------------------------------------------------
144
  -- -------------------------------------------------------------------------------------------
145
  neorv32_cpu_control_inst: neorv32_cpu_control
146
  generic map (
147
    -- General --
148 8 zero_gravi
    CLOCK_FREQUENCY              => CLOCK_FREQUENCY,  -- clock frequency of clk_i in Hz
149
    HART_ID                      => HART_ID,          -- custom hardware thread ID
150
    BOOTLOADER_USE               => BOOTLOADER_USE,   -- implement processor-internal bootloader?
151
    CSR_COUNTERS_USE             => CSR_COUNTERS_USE, -- implement RISC-V perf. counters ([m]instret[h], [m]cycle[h], time[h])?
152 2 zero_gravi
    -- RISC-V CPU Extensions --
153 8 zero_gravi
    CPU_EXTENSION_RISCV_C        => CPU_EXTENSION_RISCV_C,     -- implement compressed extension?
154
    CPU_EXTENSION_RISCV_E        => CPU_EXTENSION_RISCV_E,     -- implement embedded RF extension?
155
    CPU_EXTENSION_RISCV_M        => CPU_EXTENSION_RISCV_M,     -- implement muld/div extension?
156
    CPU_EXTENSION_RISCV_Zicsr    => CPU_EXTENSION_RISCV_Zicsr, -- implement CSR system?
157
    CPU_EXTENSION_RISCV_Zifencei => CPU_EXTENSION_RISCV_Zifencei, -- implement instruction stream sync.?
158 2 zero_gravi
    -- Memory configuration: Instruction memory --
159 8 zero_gravi
    MEM_ISPACE_BASE              => MEM_ISPACE_BASE,   -- base address of instruction memory space
160
    MEM_ISPACE_SIZE              => MEM_ISPACE_SIZE,   -- total size of instruction memory space in byte
161
    MEM_INT_IMEM_USE             => MEM_INT_IMEM_USE,  -- implement processor-internal instruction memory
162
    MEM_INT_IMEM_SIZE            => MEM_INT_IMEM_SIZE, -- size of processor-internal instruction memory in bytes
163
    MEM_INT_IMEM_ROM             => MEM_INT_IMEM_ROM,  -- implement processor-internal instruction memory as ROM
164 2 zero_gravi
    -- Memory configuration: Data memory --
165 8 zero_gravi
    MEM_DSPACE_BASE              => MEM_DSPACE_BASE,   -- base address of data memory space
166
    MEM_DSPACE_SIZE              => MEM_DSPACE_SIZE,   -- total size of data memory space in byte
167
    MEM_INT_DMEM_USE             => MEM_INT_DMEM_USE,  -- implement processor-internal data memory
168
    MEM_INT_DMEM_SIZE            => MEM_INT_DMEM_SIZE, -- size of processor-internal data memory in bytes
169 2 zero_gravi
    -- Memory configuration: External memory interface --
170 8 zero_gravi
    MEM_EXT_USE                  => MEM_EXT_USE,       -- implement external memory bus interface?
171 2 zero_gravi
    -- Processor peripherals --
172 8 zero_gravi
    IO_GPIO_USE                  => IO_GPIO_USE,       -- implement general purpose input/output port unit (GPIO)?
173
    IO_MTIME_USE                 => IO_MTIME_USE,      -- implement machine system timer (MTIME)?
174
    IO_UART_USE                  => IO_UART_USE,       -- implement universal asynchronous receiver/transmitter (UART)?
175
    IO_SPI_USE                   => IO_SPI_USE,        -- implement serial peripheral interface (SPI)?
176
    IO_TWI_USE                   => IO_TWI_USE,        -- implement two-wire interface (TWI)?
177
    IO_PWM_USE                   => IO_PWM_USE,        -- implement pulse-width modulation unit (PWM)?
178
    IO_WDT_USE                   => IO_WDT_USE,        -- implement watch dog timer (WDT)?
179
    IO_CLIC_USE                  => IO_CLIC_USE,       -- implement core local interrupt controller (CLIC)?
180
    IO_TRNG_USE                  => IO_TRNG_USE,       -- implement true random number generator (TRNG)?
181
    IO_DEVNULL_USE               => IO_DEVNULL_USE     -- implement dummy device (DEVNULL)?
182 2 zero_gravi
  )
183
  port map (
184
    -- global control --
185
    clk_i         => clk_i,       -- global clock, rising edge
186
    rstn_i        => rstn_i,      -- global reset, low-active, async
187
    ctrl_o        => ctrl,        -- main control bus
188
    -- status input --
189
    alu_wait_i    => alu_wait,    -- wait for ALU
190
    bus_wait_i    => bus_wait,    -- wait for bus
191
    -- data input --
192
    instr_i       => instr,       -- instruction
193
    cmp_i         => alu_cmp,     -- comparator status
194
    alu_add_i     => alu_add,     -- ALU.add result
195
    -- data output --
196
    imm_o         => imm,         -- immediate
197 6 zero_gravi
    fetch_pc_o    => fetch_pc,    -- PC for instruction fetch
198
    curr_pc_o     => curr_pc,     -- current PC (corresponding to current instruction)
199
    next_pc_o     => next_pc,     -- next PC (corresponding to current instruction)
200 2 zero_gravi
    -- csr interface --
201
    csr_wdata_i   => alu_res,     -- CSR write data
202
    csr_rdata_o   => csr_rdata,   -- CSR read data
203
    -- external interrupt --
204
    clic_irq_i    => clic_irq_i,  -- CLIC interrupt request
205
    mtime_irq_i   => mtime_irq_i, -- machine timer interrupt
206 11 zero_gravi
    -- system time input from MTIME --
207
    time_i        => time_i,      -- current system time
208 2 zero_gravi
    -- bus access exceptions --
209
    mar_i         => mar,         -- memory address register
210
    ma_instr_i    => ma_instr,    -- misaligned instruction address
211
    ma_load_i     => ma_load,     -- misaligned load data address
212
    ma_store_i    => ma_store,    -- misaligned store data address
213
    be_instr_i    => be_instr,    -- bus error on instruction access
214
    be_load_i     => be_load,     -- bus error on load data access
215
    be_store_i    => be_store,    -- bus error on store data access
216 6 zero_gravi
    bus_busy_i    => bus_busy     -- bus unit is busy
217 2 zero_gravi
  );
218
 
219
 
220
  -- Register File --------------------------------------------------------------------------
221
  -- -------------------------------------------------------------------------------------------
222
  neorv32_regfile_inst: neorv32_cpu_regfile
223
  generic map (
224
    CPU_EXTENSION_RISCV_E => CPU_EXTENSION_RISCV_E -- implement embedded RF extension?
225
  )
226
  port map (
227
    -- global control --
228
    clk_i  => clk_i,              -- global clock, rising edge
229
    ctrl_i => ctrl,               -- main control bus
230
    -- data input --
231
    mem_i  => rdata,              -- memory read data
232
    alu_i  => alu_res,            -- ALU result
233
    csr_i  => csr_rdata,          -- CSR read data
234 6 zero_gravi
    pc_i   => next_pc,            -- next pc
235 2 zero_gravi
    -- data output --
236
    rs1_o  => rs1,                -- operand 1
237
    rs2_o  => rs2                 -- operand 2
238
  );
239
 
240
 
241
  -- ALU ------------------------------------------------------------------------------------
242
  -- -------------------------------------------------------------------------------------------
243
  neorv32_cpu_alu_inst: neorv32_cpu_alu
244 11 zero_gravi
  generic map (
245
    CPU_EXTENSION_RISCV_M => CPU_EXTENSION_RISCV_M -- implement muld/div extension?
246
  )
247 2 zero_gravi
  port map (
248
    -- global control --
249
    clk_i       => clk_i,         -- global clock, rising edge
250
    rstn_i      => rstn_i,        -- global reset, low-active, async
251
    ctrl_i      => ctrl,          -- main control bus
252
    -- data input --
253
    rs1_i       => rs1,           -- rf source 1
254
    rs2_i       => rs2,           -- rf source 2
255 6 zero_gravi
    pc2_i       => curr_pc,       -- delayed PC
256 2 zero_gravi
    imm_i       => imm,           -- immediate
257
    csr_i       => csr_rdata,     -- csr read data
258
    -- data output --
259
    cmp_o       => alu_cmp,       -- comparator status
260
    add_o       => alu_add,       -- OPA + OPB
261
    res_o       => alu_res,       -- ALU result
262
    -- co-processor interface --
263
    cp0_data_i  => cp0_data,      -- co-processor 0 result
264
    cp0_valid_i => cp0_valid,     -- co-processor 0 result valid
265
    cp1_data_i  => cp1_data,      -- co-processor 1 result
266
    cp1_valid_i => cp1_valid,     -- co-processor 1 result valid
267
    -- status --
268
    wait_o      => alu_wait       -- busy due to iterative processing units
269
  );
270
 
271
 
272
  -- Co-Processor 0: MULDIV Unit ------------------------------------------------------------
273
  -- -------------------------------------------------------------------------------------------
274
  neorv32_cpu_cp_muldiv_inst_true:
275
  if (CPU_EXTENSION_RISCV_M = true) generate
276
    neorv32_cpu_cp_muldiv_inst: neorv32_cpu_cp_muldiv
277
    port map (
278
      -- global control --
279
      clk_i   => clk_i,           -- global clock, rising edge
280
      rstn_i  => rstn_i,          -- global reset, low-active, async
281
      ctrl_i  => ctrl,            -- main control bus
282
      -- data input --
283
      rs1_i   => rs1,             -- rf source 1
284
      rs2_i   => rs2,             -- rf source 2
285
      -- result and status --
286
      res_o   => cp0_data,        -- operation result
287
      valid_o => cp0_valid        -- data output valid
288
    );
289
  end generate;
290
 
291
  neorv32_cpu_cp_muldiv_inst_false:
292
  if (CPU_EXTENSION_RISCV_M = false) generate
293
    cp0_data  <= (others => '0');
294
    cp0_valid <= '0';
295
  end generate;
296
 
297
 
298
  -- Co-Processor 1: Not Implemented Yet ----------------------------------------------------
299
  -- -------------------------------------------------------------------------------------------
300
  cp1_data  <= (others => '0');
301
  cp1_valid <= '0';
302
 
303
 
304
  -- Bus Unit -------------------------------------------------------------------------------
305
  -- -------------------------------------------------------------------------------------------
306
  neorv32_cpu_bus_inst: neorv32_cpu_bus
307
  generic map (
308 11 zero_gravi
    CPU_EXTENSION_RISCV_C => CPU_EXTENSION_RISCV_C, -- implement compressed extension?
309
    MEM_EXT_TIMEOUT       => MEM_EXT_TIMEOUT -- cycles after which a valid bus access will timeout
310 2 zero_gravi
  )
311
  port map (
312
    -- global control --
313 11 zero_gravi
    clk_i        => clk_i,         -- global clock, rising edge
314
    rstn_i       => rstn_i,        -- global reset, low-active, async
315
    ctrl_i       => ctrl,          -- main control bus
316 2 zero_gravi
    -- data input --
317 11 zero_gravi
    wdata_i      => rs2,           -- write data
318
    pc_i         => fetch_pc,      -- current PC for instruction fetch
319
    alu_i        => alu_res,       -- ALU result
320 2 zero_gravi
    -- data output --
321 11 zero_gravi
    instr_o      => instr,         -- instruction
322
    rdata_o      => rdata,         -- read data
323 2 zero_gravi
    -- status --
324 11 zero_gravi
    mar_o        => mar,           -- current memory address register
325
    ma_instr_o   => ma_instr,      -- misaligned instruction address
326
    ma_load_o    => ma_load,       -- misaligned load data address
327
    ma_store_o   => ma_store,      -- misaligned store data address
328
    be_instr_o   => be_instr,      -- bus error on instruction access
329
    be_load_o    => be_load,       -- bus error on load data access
330
    be_store_o   => be_store,      -- bus error on store data access
331
    bus_wait_o   => bus_wait,      -- wait for bus operation to finish
332
    bus_busy_o   => bus_busy,      -- bus unit is busy
333 2 zero_gravi
    -- bus system --
334 11 zero_gravi
    bus_addr_o   => bus_addr_o,    -- bus access address
335
    bus_rdata_i  => bus_rdata_i,   -- bus read data
336
    bus_wdata_o  => bus_wdata_o,   -- bus write data
337
    bus_ben_o    => bus_ben_o,     -- byte enable
338
    bus_we_o     => bus_we_o,      -- write enable
339
    bus_re_o     => bus_re_o,      -- read enable
340
    bus_cancel_o => bus_cancel_o,  -- cancel current bus transaction
341
    bus_ack_i    => bus_ack_i,     -- bus transfer acknowledge
342
    bus_err_i    => bus_err_i      -- bus transfer error
343 2 zero_gravi
  );
344
 
345
 
346
end neorv32_cpu_rtl;

powered by: WebSVN 2.1.0

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