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

Subversion Repositories neorv32

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

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

powered by: WebSVN 2.1.0

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