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

Subversion Repositories neorv32

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

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 14 zero_gravi
    HW_THREAD_ID                 : std_ulogic_vector(31 downto 0):= (others => '0'); -- hardware thread id
56
    CPU_BOOT_ADDR                : std_ulogic_vector(31 downto 0):= (others => '0'); -- 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 14 zero_gravi
    -- Bus Interface --
64
    BUS_TIMEOUT                  : natural := 15     -- cycles after which a valid bus access will timeout
65 2 zero_gravi
  );
66
  port (
67
    -- global control --
68 14 zero_gravi
    clk_i          : in  std_ulogic := '0'; -- global clock, rising edge
69
    rstn_i         : in  std_ulogic := '0'; -- global reset, low-active, async
70 12 zero_gravi
    -- instruction bus interface --
71
    i_bus_addr_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
72 14 zero_gravi
    i_bus_rdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0) := (others => '0'); -- bus read data
73 12 zero_gravi
    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 14 zero_gravi
    i_bus_ack_i    : in  std_ulogic := '0'; -- bus transfer acknowledge
79
    i_bus_err_i    : in  std_ulogic := '0'; -- bus transfer error
80 12 zero_gravi
    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 14 zero_gravi
    d_bus_rdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0) := (others => '0'); -- bus read data
84 12 zero_gravi
    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 14 zero_gravi
    d_bus_ack_i    : in  std_ulogic := '0'; -- bus transfer acknowledge
90
    d_bus_err_i    : in  std_ulogic := '0'; -- bus transfer error
91 12 zero_gravi
    d_bus_fence_o  : out std_ulogic; -- executed FENCE operation
92 11 zero_gravi
    -- system time input from MTIME --
93 14 zero_gravi
    time_i         : in  std_ulogic_vector(63 downto 0) := (others => '0'); -- current system time
94
    -- interrupts (risc-v compliant) --
95
    msw_irq_i      : in  std_ulogic := '0'; -- machine software interrupt
96
    mext_irq_i     : in  std_ulogic := '0'; -- machine external interrupt
97
    mtime_irq_i    : in  std_ulogic := '0'; -- machine timer interrupt
98
    -- fast interrupts (custom) --
99
    firq_i         : in  std_ulogic_vector(3 downto 0) := (others => '0')
100 2 zero_gravi
  );
101
end neorv32_cpu;
102
 
103
architecture neorv32_cpu_rtl of neorv32_cpu is
104
 
105
  -- local signals --
106 12 zero_gravi
  signal ctrl       : std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
107
  signal alu_cmp    : std_ulogic_vector(1 downto 0); -- alu comparator result
108
  signal imm        : std_ulogic_vector(data_width_c-1 downto 0); -- immediate
109
  signal instr      : std_ulogic_vector(data_width_c-1 downto 0); -- new instruction
110
  signal rs1, rs2   : std_ulogic_vector(data_width_c-1 downto 0); -- source registers
111
  signal alu_res    : std_ulogic_vector(data_width_c-1 downto 0); -- alu result
112
  signal alu_add    : std_ulogic_vector(data_width_c-1 downto 0); -- alu adder result
113
  signal rdata      : std_ulogic_vector(data_width_c-1 downto 0); -- memory read data
114
  signal alu_wait   : std_ulogic; -- alu is busy due to iterative unit
115
  signal bus_i_wait : std_ulogic; -- wait for current bus instruction fetch
116
  signal bus_d_wait : std_ulogic; -- wait for current bus data access
117
  signal csr_rdata  : std_ulogic_vector(data_width_c-1 downto 0); -- csr read data
118
  signal mar        : std_ulogic_vector(data_width_c-1 downto 0); -- current memory address register
119
  signal ma_instr   : std_ulogic; -- misaligned instruction address
120
  signal ma_load    : std_ulogic; -- misaligned load data address
121
  signal ma_store   : std_ulogic; -- misaligned store data address
122
  signal be_instr   : std_ulogic; -- bus error on instruction access
123
  signal be_load    : std_ulogic; -- bus error on load data access
124
  signal be_store   : std_ulogic; -- bus error on store data access
125
  signal fetch_pc   : std_ulogic_vector(data_width_c-1 downto 0); -- pc for instruction fetch
126
  signal curr_pc    : std_ulogic_vector(data_width_c-1 downto 0); -- current pc (for current executed instruction)
127
  signal next_pc    : std_ulogic_vector(data_width_c-1 downto 0); -- next pc (for current executed instruction)
128 2 zero_gravi
 
129
  -- co-processor interface --
130
  signal cp0_data,  cp1_data  : std_ulogic_vector(data_width_c-1 downto 0);
131
  signal cp0_valid, cp1_valid : std_ulogic;
132
 
133
begin
134
 
135
  -- Control Unit ---------------------------------------------------------------------------
136
  -- -------------------------------------------------------------------------------------------
137
  neorv32_cpu_control_inst: neorv32_cpu_control
138
  generic map (
139
    -- General --
140 8 zero_gravi
    CSR_COUNTERS_USE             => CSR_COUNTERS_USE, -- implement RISC-V perf. counters ([m]instret[h], [m]cycle[h], time[h])?
141 12 zero_gravi
    HW_THREAD_ID                 => HW_THREAD_ID,     -- hardware thread id
142
    CPU_BOOT_ADDR                => CPU_BOOT_ADDR,    -- cpu boot address
143 2 zero_gravi
    -- RISC-V CPU Extensions --
144 12 zero_gravi
    CPU_EXTENSION_RISCV_C        => CPU_EXTENSION_RISCV_C,       -- implement compressed extension?
145
    CPU_EXTENSION_RISCV_E        => CPU_EXTENSION_RISCV_E,       -- implement embedded RF extension?
146
    CPU_EXTENSION_RISCV_M        => CPU_EXTENSION_RISCV_M,       -- implement muld/div extension?
147
    CPU_EXTENSION_RISCV_Zicsr    => CPU_EXTENSION_RISCV_Zicsr,   -- implement CSR system?
148
    CPU_EXTENSION_RISCV_Zifencei => CPU_EXTENSION_RISCV_Zifencei -- implement instruction stream sync.?
149 2 zero_gravi
  )
150
  port map (
151
    -- global control --
152
    clk_i         => clk_i,       -- global clock, rising edge
153
    rstn_i        => rstn_i,      -- global reset, low-active, async
154
    ctrl_o        => ctrl,        -- main control bus
155
    -- status input --
156
    alu_wait_i    => alu_wait,    -- wait for ALU
157 12 zero_gravi
    bus_i_wait_i  => bus_i_wait,  -- wait for bus
158
    bus_d_wait_i  => bus_d_wait,  -- wait for bus
159 2 zero_gravi
    -- data input --
160
    instr_i       => instr,       -- instruction
161
    cmp_i         => alu_cmp,     -- comparator status
162
    alu_add_i     => alu_add,     -- ALU.add result
163
    -- data output --
164
    imm_o         => imm,         -- immediate
165 6 zero_gravi
    fetch_pc_o    => fetch_pc,    -- PC for instruction fetch
166
    curr_pc_o     => curr_pc,     -- current PC (corresponding to current instruction)
167
    next_pc_o     => next_pc,     -- next PC (corresponding to current instruction)
168 2 zero_gravi
    -- csr interface --
169
    csr_wdata_i   => alu_res,     -- CSR write data
170
    csr_rdata_o   => csr_rdata,   -- CSR read data
171 14 zero_gravi
    -- interrupts (risc-v compliant) --
172
    msw_irq_i     => msw_irq_i,   -- machine software interrupt
173
    mext_irq_i    => mext_irq_i,  -- machine external interrupt
174 2 zero_gravi
    mtime_irq_i   => mtime_irq_i, -- machine timer interrupt
175 14 zero_gravi
    -- fast interrupts (custom) --
176
    firq_i        => firq_i,
177 11 zero_gravi
    -- system time input from MTIME --
178
    time_i        => time_i,      -- current system time
179 2 zero_gravi
    -- bus access exceptions --
180
    mar_i         => mar,         -- memory address register
181
    ma_instr_i    => ma_instr,    -- misaligned instruction address
182
    ma_load_i     => ma_load,     -- misaligned load data address
183
    ma_store_i    => ma_store,    -- misaligned store data address
184
    be_instr_i    => be_instr,    -- bus error on instruction access
185
    be_load_i     => be_load,     -- bus error on load data access
186 12 zero_gravi
    be_store_i    => be_store     -- bus error on store data access
187 2 zero_gravi
  );
188
 
189
 
190
  -- Register File --------------------------------------------------------------------------
191
  -- -------------------------------------------------------------------------------------------
192
  neorv32_regfile_inst: neorv32_cpu_regfile
193
  generic map (
194
    CPU_EXTENSION_RISCV_E => CPU_EXTENSION_RISCV_E -- implement embedded RF extension?
195
  )
196
  port map (
197
    -- global control --
198
    clk_i  => clk_i,              -- global clock, rising edge
199
    ctrl_i => ctrl,               -- main control bus
200
    -- data input --
201
    mem_i  => rdata,              -- memory read data
202
    alu_i  => alu_res,            -- ALU result
203
    csr_i  => csr_rdata,          -- CSR read data
204 13 zero_gravi
    pc_i   => next_pc,            -- next pc (for linking)
205 2 zero_gravi
    -- data output --
206
    rs1_o  => rs1,                -- operand 1
207
    rs2_o  => rs2                 -- operand 2
208
  );
209
 
210
 
211
  -- ALU ------------------------------------------------------------------------------------
212
  -- -------------------------------------------------------------------------------------------
213
  neorv32_cpu_alu_inst: neorv32_cpu_alu
214 11 zero_gravi
  generic map (
215
    CPU_EXTENSION_RISCV_M => CPU_EXTENSION_RISCV_M -- implement muld/div extension?
216
  )
217 2 zero_gravi
  port map (
218
    -- global control --
219
    clk_i       => clk_i,         -- global clock, rising edge
220
    rstn_i      => rstn_i,        -- global reset, low-active, async
221
    ctrl_i      => ctrl,          -- main control bus
222
    -- data input --
223
    rs1_i       => rs1,           -- rf source 1
224
    rs2_i       => rs2,           -- rf source 2
225 6 zero_gravi
    pc2_i       => curr_pc,       -- delayed PC
226 2 zero_gravi
    imm_i       => imm,           -- immediate
227
    csr_i       => csr_rdata,     -- csr read data
228
    -- data output --
229
    cmp_o       => alu_cmp,       -- comparator status
230
    add_o       => alu_add,       -- OPA + OPB
231
    res_o       => alu_res,       -- ALU result
232
    -- co-processor interface --
233
    cp0_data_i  => cp0_data,      -- co-processor 0 result
234
    cp0_valid_i => cp0_valid,     -- co-processor 0 result valid
235
    cp1_data_i  => cp1_data,      -- co-processor 1 result
236
    cp1_valid_i => cp1_valid,     -- co-processor 1 result valid
237
    -- status --
238
    wait_o      => alu_wait       -- busy due to iterative processing units
239
  );
240
 
241
 
242
  -- Co-Processor 0: MULDIV Unit ------------------------------------------------------------
243
  -- -------------------------------------------------------------------------------------------
244
  neorv32_cpu_cp_muldiv_inst_true:
245
  if (CPU_EXTENSION_RISCV_M = true) generate
246
    neorv32_cpu_cp_muldiv_inst: neorv32_cpu_cp_muldiv
247
    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
      -- result and status --
256
      res_o   => cp0_data,        -- operation result
257
      valid_o => cp0_valid        -- data output valid
258
    );
259
  end generate;
260
 
261
  neorv32_cpu_cp_muldiv_inst_false:
262
  if (CPU_EXTENSION_RISCV_M = false) generate
263
    cp0_data  <= (others => '0');
264
    cp0_valid <= '0';
265
  end generate;
266
 
267
 
268
  -- Co-Processor 1: Not Implemented Yet ----------------------------------------------------
269
  -- -------------------------------------------------------------------------------------------
270
  cp1_data  <= (others => '0');
271
  cp1_valid <= '0';
272
 
273
 
274 12 zero_gravi
  -- Bus Interface Unit ---------------------------------------------------------------------
275 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
276
  neorv32_cpu_bus_inst: neorv32_cpu_bus
277
  generic map (
278 11 zero_gravi
    CPU_EXTENSION_RISCV_C => CPU_EXTENSION_RISCV_C, -- implement compressed extension?
279 14 zero_gravi
    BUS_TIMEOUT           => BUS_TIMEOUT            -- cycles after which a valid bus access will timeout
280 2 zero_gravi
  )
281
  port map (
282
    -- global control --
283 12 zero_gravi
    clk_i          => clk_i,          -- global clock, rising edge
284
    rstn_i         => rstn_i,         -- global reset, low-active, async
285
    ctrl_i         => ctrl,           -- main control bus
286
    -- cpu instruction fetch interface --
287
    fetch_pc_i     => fetch_pc,       -- PC for instruction fetch
288
    instr_o        => instr,          -- instruction
289
    i_wait_o       => bus_i_wait,     -- wait for fetch to complete
290
    --
291
    ma_instr_o     => ma_instr,       -- misaligned instruction address
292
    be_instr_o     => be_instr,       -- bus error on instruction access
293
    -- cpu data access interface --
294
    addr_i         => alu_add,        -- ALU.add result -> access address
295
    wdata_i        => rs2,            -- write data
296
    rdata_o        => rdata,          -- read data
297
    mar_o          => mar,            -- current memory address register
298
    d_wait_o       => bus_d_wait,     -- wait for access to complete
299
    --
300
    ma_load_o      => ma_load,        -- misaligned load data address
301
    ma_store_o     => ma_store,       -- misaligned store data address
302
    be_load_o      => be_load,        -- bus error on load data access
303
    be_store_o     => be_store,       -- bus error on store data access
304
    -- instruction bus --
305
    i_bus_addr_o   => i_bus_addr_o,   -- bus access address
306
    i_bus_rdata_i  => i_bus_rdata_i,  -- bus read data
307
    i_bus_wdata_o  => i_bus_wdata_o,  -- bus write data
308
    i_bus_ben_o    => i_bus_ben_o,    -- byte enable
309
    i_bus_we_o     => i_bus_we_o,     -- write enable
310
    i_bus_re_o     => i_bus_re_o,     -- read enable
311
    i_bus_cancel_o => i_bus_cancel_o, -- cancel current bus transaction
312
    i_bus_ack_i    => i_bus_ack_i,    -- bus transfer acknowledge
313
    i_bus_err_i    => i_bus_err_i,    -- bus transfer error
314
    i_bus_fence_o  => i_bus_fence_o,  -- fence operation
315
    -- data bus --
316
    d_bus_addr_o   => d_bus_addr_o,   -- bus access address
317
    d_bus_rdata_i  => d_bus_rdata_i,  -- bus read data
318
    d_bus_wdata_o  => d_bus_wdata_o,  -- bus write data
319
    d_bus_ben_o    => d_bus_ben_o,    -- byte enable
320
    d_bus_we_o     => d_bus_we_o,     -- write enable
321
    d_bus_re_o     => d_bus_re_o,     -- read enable
322
    d_bus_cancel_o => d_bus_cancel_o, -- cancel current bus transaction
323
    d_bus_ack_i    => d_bus_ack_i,    -- bus transfer acknowledge
324
    d_bus_err_i    => d_bus_err_i,    -- bus transfer error
325
    d_bus_fence_o  => d_bus_fence_o   -- fence operation
326 2 zero_gravi
  );
327
 
328
 
329
end neorv32_cpu_rtl;

powered by: WebSVN 2.1.0

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