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

Subversion Repositories neorv32

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

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

powered by: WebSVN 2.1.0

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