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

Subversion Repositories neorv32

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

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 18 zero_gravi
-- # NEORV32 CPU:                                                                                  #
5 47 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 52 zero_gravi
-- #   * neorv32_cpu_cp_bitmanip.vhd     - Bit-manipulation co-processor ('B')                     #
9 53 zero_gravi
-- #   * neorv32_cpu_cp_fpu.vhd          - Single-precision FPU co-processor ('Zfinx')             #
10 52 zero_gravi
-- #   * neorv32_cpu_cp_muldiv.vhd       - Integer multiplier/divider co-processor ('M')           #
11 47 zero_gravi
-- #   * neorv32_cpu_ctrl.vhd            - CPU control and CSR system                              #
12
-- #     * neorv32_cpu_decompressor.vhd  - Compressed instructions decoder                         #
13
-- #   * neorv32_cpu_regfile.vhd         - Data register file                                      #
14 56 zero_gravi
-- # * neorv32_package.vhd               - Main CPU & Processor package file                       #
15 38 zero_gravi
-- #                                                                                               #
16 29 zero_gravi
-- # Check out the processor's data sheet for more information: docs/NEORV32.pdf                   #
17 2 zero_gravi
-- # ********************************************************************************************* #
18
-- # BSD 3-Clause License                                                                          #
19
-- #                                                                                               #
20 42 zero_gravi
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
21 2 zero_gravi
-- #                                                                                               #
22
-- # Redistribution and use in source and binary forms, with or without modification, are          #
23
-- # permitted provided that the following conditions are met:                                     #
24
-- #                                                                                               #
25
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
26
-- #    conditions and the following disclaimer.                                                   #
27
-- #                                                                                               #
28
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
29
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
30
-- #    provided with the distribution.                                                            #
31
-- #                                                                                               #
32
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
33
-- #    endorse or promote products derived from this software without specific prior written      #
34
-- #    permission.                                                                                #
35
-- #                                                                                               #
36
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
37
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
38
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
39
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
40
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
41
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
42
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
43
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
44
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
45
-- # ********************************************************************************************* #
46
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
47
-- #################################################################################################
48
 
49
library ieee;
50
use ieee.std_logic_1164.all;
51
use ieee.numeric_std.all;
52
 
53
library neorv32;
54
use neorv32.neorv32_package.all;
55
 
56
entity neorv32_cpu is
57
  generic (
58
    -- General --
59 49 zero_gravi
    HW_THREAD_ID                 : natural := 0;     -- hardware thread id (32-bit)
60
    CPU_BOOT_ADDR                : std_ulogic_vector(31 downto 0):= x"00000000"; -- cpu boot address
61 2 zero_gravi
    -- RISC-V CPU Extensions --
62 39 zero_gravi
    CPU_EXTENSION_RISCV_A        : boolean := false; -- implement atomic extension?
63 44 zero_gravi
    CPU_EXTENSION_RISCV_B        : boolean := false; -- implement bit manipulation extensions?
64 12 zero_gravi
    CPU_EXTENSION_RISCV_C        : boolean := false; -- implement compressed extension?
65
    CPU_EXTENSION_RISCV_E        : boolean := false; -- implement embedded RF extension?
66
    CPU_EXTENSION_RISCV_M        : boolean := false; -- implement muld/div extension?
67 15 zero_gravi
    CPU_EXTENSION_RISCV_U        : boolean := false; -- implement user mode extension?
68 53 zero_gravi
    CPU_EXTENSION_RISCV_Zfinx    : boolean := false; -- implement 32-bit floating-point extension (using INT reg!)
69 12 zero_gravi
    CPU_EXTENSION_RISCV_Zicsr    : boolean := true;  -- implement CSR system?
70 52 zero_gravi
    CPU_EXTENSION_RISCV_Zifencei : boolean := false; -- implement instruction stream sync.?
71 19 zero_gravi
    -- Extension Options --
72
    FAST_MUL_EN                  : boolean := false; -- use DSPs for M extension's multiplier
73 34 zero_gravi
    FAST_SHIFT_EN                : boolean := false; -- use barrel shifter for shift operations
74 56 zero_gravi
    TINY_SHIFT_EN                : boolean := false; -- use tiny (single-bit) shifter for shift operations
75
    CPU_CNT_WIDTH                : natural := 64;    -- total width of CPU cycle and instret counters (0..64)
76 15 zero_gravi
    -- Physical Memory Protection (PMP) --
77 55 zero_gravi
    PMP_NUM_REGIONS              : natural := 0;     -- number of regions (0..64)
78 42 zero_gravi
    PMP_MIN_GRANULARITY          : natural := 64*1024; -- minimal region granularity in bytes, has to be a power of 2, min 8 bytes
79
    -- Hardware Performance Monitors (HPM) --
80 56 zero_gravi
    HPM_NUM_CNTS                 : natural := 0;     -- number of implemented HPM counters (0..29)
81
    HPM_CNT_WIDTH                : natural := 40     -- total size of HPM counters (1..64)
82 2 zero_gravi
  );
83
  port (
84
    -- global control --
85 14 zero_gravi
    clk_i          : in  std_ulogic := '0'; -- global clock, rising edge
86
    rstn_i         : in  std_ulogic := '0'; -- global reset, low-active, async
87 47 zero_gravi
    sleep_o        : out std_ulogic; -- cpu is in sleep mode when set
88 12 zero_gravi
    -- instruction bus interface --
89
    i_bus_addr_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
90 14 zero_gravi
    i_bus_rdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0) := (others => '0'); -- bus read data
91 12 zero_gravi
    i_bus_wdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
92
    i_bus_ben_o    : out std_ulogic_vector(03 downto 0); -- byte enable
93
    i_bus_we_o     : out std_ulogic; -- write enable
94
    i_bus_re_o     : out std_ulogic; -- read enable
95 57 zero_gravi
    i_bus_lock_o   : out std_ulogic; -- exclusive access request
96 14 zero_gravi
    i_bus_ack_i    : in  std_ulogic := '0'; -- bus transfer acknowledge
97
    i_bus_err_i    : in  std_ulogic := '0'; -- bus transfer error
98 12 zero_gravi
    i_bus_fence_o  : out std_ulogic; -- executed FENCEI operation
99 35 zero_gravi
    i_bus_priv_o   : out std_ulogic_vector(1 downto 0); -- privilege level
100 12 zero_gravi
    -- data bus interface --
101
    d_bus_addr_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
102 14 zero_gravi
    d_bus_rdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0) := (others => '0'); -- bus read data
103 12 zero_gravi
    d_bus_wdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
104
    d_bus_ben_o    : out std_ulogic_vector(03 downto 0); -- byte enable
105
    d_bus_we_o     : out std_ulogic; -- write enable
106
    d_bus_re_o     : out std_ulogic; -- read enable
107 57 zero_gravi
    d_bus_lock_o   : out std_ulogic; -- exclusive access request
108 14 zero_gravi
    d_bus_ack_i    : in  std_ulogic := '0'; -- bus transfer acknowledge
109
    d_bus_err_i    : in  std_ulogic := '0'; -- bus transfer error
110 12 zero_gravi
    d_bus_fence_o  : out std_ulogic; -- executed FENCE operation
111 35 zero_gravi
    d_bus_priv_o   : out std_ulogic_vector(1 downto 0); -- privilege level
112 11 zero_gravi
    -- system time input from MTIME --
113 14 zero_gravi
    time_i         : in  std_ulogic_vector(63 downto 0) := (others => '0'); -- current system time
114
    -- interrupts (risc-v compliant) --
115
    msw_irq_i      : in  std_ulogic := '0'; -- machine software interrupt
116
    mext_irq_i     : in  std_ulogic := '0'; -- machine external interrupt
117
    mtime_irq_i    : in  std_ulogic := '0'; -- machine timer interrupt
118
    -- fast interrupts (custom) --
119 48 zero_gravi
    firq_i         : in  std_ulogic_vector(15 downto 0) := (others => '0');
120
    firq_ack_o     : out std_ulogic_vector(15 downto 0)
121 2 zero_gravi
  );
122
end neorv32_cpu;
123
 
124
architecture neorv32_cpu_rtl of neorv32_cpu is
125
 
126
  -- local signals --
127 53 zero_gravi
  signal ctrl        : std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
128
  signal comparator  : std_ulogic_vector(1 downto 0); -- comparator result
129
  signal imm         : std_ulogic_vector(data_width_c-1 downto 0); -- immediate
130
  signal instr       : std_ulogic_vector(data_width_c-1 downto 0); -- new instruction
131
  signal rs1, rs2    : std_ulogic_vector(data_width_c-1 downto 0); -- source registers
132
  signal alu_res     : std_ulogic_vector(data_width_c-1 downto 0); -- alu result
133
  signal alu_add     : std_ulogic_vector(data_width_c-1 downto 0); -- alu address result
134
  signal mem_rdata   : std_ulogic_vector(data_width_c-1 downto 0); -- memory read data
135
  signal alu_wait    : std_ulogic; -- alu is busy due to iterative unit
136
  signal bus_i_wait  : std_ulogic; -- wait for current bus instruction fetch
137
  signal bus_d_wait  : std_ulogic; -- wait for current bus data access
138
  signal csr_rdata   : std_ulogic_vector(data_width_c-1 downto 0); -- csr read data
139
  signal mar         : std_ulogic_vector(data_width_c-1 downto 0); -- current memory address register
140
  signal ma_instr    : std_ulogic; -- misaligned instruction address
141
  signal ma_load     : std_ulogic; -- misaligned load data address
142
  signal ma_store    : std_ulogic; -- misaligned store data address
143 57 zero_gravi
  signal excl_state  : std_ulogic; -- atomic/exclusive access lock status
144 53 zero_gravi
  signal be_instr    : std_ulogic; -- bus error on instruction access
145
  signal be_load     : std_ulogic; -- bus error on load data access
146
  signal be_store    : std_ulogic; -- bus error on store data access
147
  signal fetch_pc    : std_ulogic_vector(data_width_c-1 downto 0); -- pc for instruction fetch
148
  signal curr_pc     : std_ulogic_vector(data_width_c-1 downto 0); -- current pc (for current executed instruction)
149
  signal fpu_rm      : std_ulogic_vector(2 downto 0); -- FPU rounding mode
150
  signal fpu_flags   : std_ulogic_vector(4 downto 0); -- FPU exception flags
151 2 zero_gravi
 
152
  -- co-processor interface --
153 49 zero_gravi
  signal cp_start  : std_ulogic_vector(7 downto 0); -- trigger co-processor i
154
  signal cp_valid  : std_ulogic_vector(7 downto 0); -- co-processor i done
155
  signal cp_result : cp_data_if_t; -- co-processor result
156 2 zero_gravi
 
157 15 zero_gravi
  -- pmp interface --
158
  signal pmp_addr  : pmp_addr_if_t;
159
  signal pmp_ctrl  : pmp_ctrl_if_t;
160
 
161 2 zero_gravi
begin
162
 
163 15 zero_gravi
  -- Sanity Checks --------------------------------------------------------------------------
164
  -- -------------------------------------------------------------------------------------------
165 56 zero_gravi
  -- hardware reset notifier --
166
  assert not ((dedicated_reset_c = false) and (def_rst_val_c = '-')) report "NEORV32 CPU CONFIG NOTE: Using NO dedicated hardware reset for uncritical registers (default, might reduce area footprint). Set the package constant <dedicated_reset_c> to TRUE if you need a defined reset value." severity note;
167
  assert not ((dedicated_reset_c = true)  and (def_rst_val_c = '0')) report "NEORV32 CPU CONFIG NOTE: Using defined hardware reset for uncritical registers (non-default, reset-to-zero, might increase area footprint)." severity note;
168
  assert not ((def_rst_val_c /= '-') and (def_rst_val_c /= '0')) report "NEORV32 CPU CONFIG ERROR! Invalid configuration of package <def_rst_val_c> constant (has to be '-' or '0')." severity error;
169
 
170 23 zero_gravi
  -- CSR system --
171 56 zero_gravi
  assert not (CPU_EXTENSION_RISCV_Zicsr = false) report "NEORV32 CPU CONFIG WARNING! No exception/interrupt/trap/privileged features available when <CPU_EXTENSION_RISCV_Zicsr> = false." severity warning;
172
 
173
  -- CPU counters (cycle and instret) --
174
  assert not ((CPU_CNT_WIDTH < 0) or (CPU_CNT_WIDTH > 64)) report "NEORV32 CPU CONFIG ERROR! Invalid <CPU_CNT_WIDTH> configuration. Has to be 0..64." severity error;
175
  assert not (CPU_CNT_WIDTH < 64) report "NEORV32 CPU CONFIG WARNING! Implementing CPU <cycle> and <instret> CSRs with reduced size (" & integer'image(CPU_CNT_WIDTH) & "-bit instead of 64-bit). This is not RISC-V compliant and might have unintended SW side effects." severity warning;
176
 
177 23 zero_gravi
  -- U-extension requires Zicsr extension --
178 56 zero_gravi
  assert not ((CPU_EXTENSION_RISCV_Zicsr = false) and (CPU_EXTENSION_RISCV_U = true)) report "NEORV32 CPU CONFIG ERROR! User mode requires <CPU_EXTENSION_RISCV_Zicsr> extension to be enabled." severity error;
179 40 zero_gravi
 
180 38 zero_gravi
  -- Instruction prefetch buffer size --
181
  assert not (is_power_of_two_f(ipb_entries_c) = false) report "NEORV32 CPU CONFIG ERROR! Number of entries in instruction prefetch buffer <ipb_entries_c> has to be a power of two." severity error;
182 45 zero_gravi
  -- A extension - only lr.w and sc.w are supported yet --
183
  assert not (CPU_EXTENSION_RISCV_A = true) report "NEORV32 CPU CONFIG WARNING! Atomic operations extension (A) only supports <lr.w> and <sc.w> instructions." severity warning;
184 15 zero_gravi
 
185 52 zero_gravi
  -- FIXME: Bit manipulation warning --
186 56 zero_gravi
  assert not (CPU_EXTENSION_RISCV_B = true) report "NEORV32 CPU CONFIG WARNING! Bit manipulation extension (B) is still EXPERIMENTAL (and spec. is not ratified yet)." severity warning;
187 44 zero_gravi
 
188 55 zero_gravi
  -- Co-processor timeout counter (for debugging only) --
189
  assert not (cp_timeout_en_c = true) report "NEORV32 CPU CONFIG WARNING! Co-processor timeout counter enabled. This should be used for debugging/simulation only." severity warning;
190 52 zero_gravi
 
191 40 zero_gravi
  -- PMP regions check --
192 53 zero_gravi
  assert not (PMP_NUM_REGIONS > 64) report "NEORV32 CPU CONFIG ERROR! Number of PMP regions <PMP_NUM_REGIONS> out xf valid range (0..64)." severity error;
193 40 zero_gravi
  -- PMP granulartiy --
194 56 zero_gravi
  assert not ((is_power_of_two_f(PMP_MIN_GRANULARITY) = false) and (PMP_NUM_REGIONS > 0)) report "NEORV32 CPU CONFIG ERROR! <PMP_MIN_GRANULARITY> has to be a power of two." severity error;
195
  assert not ((PMP_MIN_GRANULARITY < 8) and (PMP_NUM_REGIONS > 0)) report "NEORV32 CPU CONFIG ERROR! <PMP_MIN_GRANULARITY> has to be >= 8 bytes." severity error;
196 40 zero_gravi
  -- PMP notifier --
197 42 zero_gravi
  assert not (PMP_NUM_REGIONS > 0) report "NEORV32 CPU CONFIG NOTE: Implementing physical memory protection (PMP) with " & integer'image(PMP_NUM_REGIONS) & " regions and a minimal granularity of " & integer'image(PMP_MIN_GRANULARITY) & " bytes." severity note;
198 56 zero_gravi
  -- PMP requires Zicsr extension --
199
  assert not ((CPU_EXTENSION_RISCV_Zicsr = false) and (PMP_NUM_REGIONS > 0)) report "NEORV32 CPU CONFIG ERROR! Physical memory protection (PMP) requires <CPU_EXTENSION_RISCV_Zicsr> extension to be enabled." severity error;
200 40 zero_gravi
 
201 42 zero_gravi
  -- HPM counters check --
202
  assert not (HPM_NUM_CNTS > 29) report "NEORV32 CPU CONFIG ERROR! Number of HPM counters <HPM_NUM_CNTS> out of valid range (0..29)." severity error;
203 56 zero_gravi
  assert not ((HPM_CNT_WIDTH < 1) or (HPM_CNT_WIDTH > 64)) report "NEORV32 CPU CONFIG ERROR! HPM counter width <HPM_CNT_WIDTH> has to be 1..64 bit." severity error;
204 42 zero_gravi
  -- HPM counters notifier --
205 56 zero_gravi
  assert not (HPM_NUM_CNTS > 0) report "NEORV32 CPU CONFIG NOTE: Implementing " & integer'image(HPM_NUM_CNTS) & " HPM counters (each " & integer'image(HPM_CNT_WIDTH) & "-bit wide)." severity note;
206 44 zero_gravi
  -- HPM CNT requires Zicsr extension --
207 56 zero_gravi
  assert not ((CPU_EXTENSION_RISCV_Zicsr = false) and (HPM_NUM_CNTS > 0)) report "NEORV32 CPU CONFIG ERROR! Hardware performance monitors (HPM) require <CPU_EXTENSION_RISCV_Zicsr> extension to be enabled." severity error;
208 41 zero_gravi
 
209 42 zero_gravi
 
210 2 zero_gravi
  -- Control Unit ---------------------------------------------------------------------------
211
  -- -------------------------------------------------------------------------------------------
212
  neorv32_cpu_control_inst: neorv32_cpu_control
213
  generic map (
214
    -- General --
215 40 zero_gravi
    HW_THREAD_ID                 => HW_THREAD_ID,  -- hardware thread id
216
    CPU_BOOT_ADDR                => CPU_BOOT_ADDR, -- cpu boot address
217 2 zero_gravi
    -- RISC-V CPU Extensions --
218 39 zero_gravi
    CPU_EXTENSION_RISCV_A        => CPU_EXTENSION_RISCV_A,        -- implement atomic extension?
219 44 zero_gravi
    CPU_EXTENSION_RISCV_B        => CPU_EXTENSION_RISCV_B,        -- implement bit manipulation extensions?
220 15 zero_gravi
    CPU_EXTENSION_RISCV_C        => CPU_EXTENSION_RISCV_C,        -- implement compressed extension?
221
    CPU_EXTENSION_RISCV_M        => CPU_EXTENSION_RISCV_M,        -- implement muld/div extension?
222
    CPU_EXTENSION_RISCV_U        => CPU_EXTENSION_RISCV_U,        -- implement user mode extension?
223 53 zero_gravi
    CPU_EXTENSION_RISCV_Zfinx    => CPU_EXTENSION_RISCV_Zfinx,    -- implement 32-bit floating-point extension (using INT reg!)
224 15 zero_gravi
    CPU_EXTENSION_RISCV_Zicsr    => CPU_EXTENSION_RISCV_Zicsr,    -- implement CSR system?
225
    CPU_EXTENSION_RISCV_Zifencei => CPU_EXTENSION_RISCV_Zifencei, -- implement instruction stream sync.?
226 56 zero_gravi
    -- Extension Options --
227
    CPU_CNT_WIDTH                => CPU_CNT_WIDTH,                -- total width of CPU cycle and instret counters (0..64)
228 15 zero_gravi
    -- Physical memory protection (PMP) --
229 42 zero_gravi
    PMP_NUM_REGIONS              => PMP_NUM_REGIONS,              -- number of regions (0..64)
230
    PMP_MIN_GRANULARITY          => PMP_MIN_GRANULARITY,          -- minimal region granularity in bytes, has to be a power of 2, min 8 bytes
231
    -- Hardware Performance Monitors (HPM) --
232 56 zero_gravi
    HPM_NUM_CNTS                 => HPM_NUM_CNTS,                 -- number of implemented HPM counters (0..29)
233
    HPM_CNT_WIDTH                => HPM_CNT_WIDTH                 -- total size of HPM counters
234 2 zero_gravi
  )
235
  port map (
236
    -- global control --
237
    clk_i         => clk_i,       -- global clock, rising edge
238
    rstn_i        => rstn_i,      -- global reset, low-active, async
239
    ctrl_o        => ctrl,        -- main control bus
240
    -- status input --
241
    alu_wait_i    => alu_wait,    -- wait for ALU
242 12 zero_gravi
    bus_i_wait_i  => bus_i_wait,  -- wait for bus
243
    bus_d_wait_i  => bus_d_wait,  -- wait for bus
244 57 zero_gravi
    excl_state_i  => excl_state,  -- atomic/exclusive access lock status
245 2 zero_gravi
    -- data input --
246
    instr_i       => instr,       -- instruction
247 47 zero_gravi
    cmp_i         => comparator,  -- comparator status
248 36 zero_gravi
    alu_add_i     => alu_add,     -- ALU address result
249
    rs1_i         => rs1,         -- rf source 1
250 2 zero_gravi
    -- data output --
251
    imm_o         => imm,         -- immediate
252 6 zero_gravi
    fetch_pc_o    => fetch_pc,    -- PC for instruction fetch
253
    curr_pc_o     => curr_pc,     -- current PC (corresponding to current instruction)
254 2 zero_gravi
    csr_rdata_o   => csr_rdata,   -- CSR read data
255 52 zero_gravi
    -- FPU interface --
256
    fpu_rm_o      => fpu_rm,      -- rounding mode
257
    fpu_flags_i   => fpu_flags,   -- exception flags
258 14 zero_gravi
    -- interrupts (risc-v compliant) --
259
    msw_irq_i     => msw_irq_i,   -- machine software interrupt
260
    mext_irq_i    => mext_irq_i,  -- machine external interrupt
261 2 zero_gravi
    mtime_irq_i   => mtime_irq_i, -- machine timer interrupt
262 14 zero_gravi
    -- fast interrupts (custom) --
263 47 zero_gravi
    firq_i        => firq_i,      -- fast interrupt trigger
264
    firq_ack_o    => firq_ack_o,  -- fast interrupt acknowledge mask
265 11 zero_gravi
    -- system time input from MTIME --
266
    time_i        => time_i,      -- current system time
267 15 zero_gravi
    -- physical memory protection --
268
    pmp_addr_o    => pmp_addr,    -- addresses
269
    pmp_ctrl_o    => pmp_ctrl,    -- configs
270 2 zero_gravi
    -- bus access exceptions --
271
    mar_i         => mar,         -- memory address register
272
    ma_instr_i    => ma_instr,    -- misaligned instruction address
273
    ma_load_i     => ma_load,     -- misaligned load data address
274
    ma_store_i    => ma_store,    -- misaligned store data address
275
    be_instr_i    => be_instr,    -- bus error on instruction access
276
    be_load_i     => be_load,     -- bus error on load data access
277 12 zero_gravi
    be_store_i    => be_store     -- bus error on store data access
278 2 zero_gravi
  );
279
 
280 47 zero_gravi
  -- CPU is sleeping? --
281
  sleep_o <= ctrl(ctrl_sleep_c); -- set when CPU is sleeping (after WFI)
282 2 zero_gravi
 
283 47 zero_gravi
 
284 2 zero_gravi
  -- Register File --------------------------------------------------------------------------
285
  -- -------------------------------------------------------------------------------------------
286 45 zero_gravi
  neorv32_cpu_regfile_inst: neorv32_cpu_regfile
287 2 zero_gravi
  generic map (
288
    CPU_EXTENSION_RISCV_E => CPU_EXTENSION_RISCV_E -- implement embedded RF extension?
289
  )
290
  port map (
291
    -- global control --
292
    clk_i  => clk_i,              -- global clock, rising edge
293
    ctrl_i => ctrl,               -- main control bus
294
    -- data input --
295 52 zero_gravi
    mem_i  => mem_rdata,          -- memory read data
296 2 zero_gravi
    alu_i  => alu_res,            -- ALU result
297
    -- data output --
298
    rs1_o  => rs1,                -- operand 1
299 47 zero_gravi
    rs2_o  => rs2,                -- operand 2
300
    cmp_o  => comparator          -- comparator status
301 2 zero_gravi
  );
302
 
303
 
304
  -- ALU ------------------------------------------------------------------------------------
305
  -- -------------------------------------------------------------------------------------------
306
  neorv32_cpu_alu_inst: neorv32_cpu_alu
307 11 zero_gravi
  generic map (
308 34 zero_gravi
    CPU_EXTENSION_RISCV_M => CPU_EXTENSION_RISCV_M, -- implement muld/div extension?
309 56 zero_gravi
    FAST_SHIFT_EN         => FAST_SHIFT_EN,         -- use barrel shifter for shift operations
310
    TINY_SHIFT_EN         => TINY_SHIFT_EN          -- use tiny (single-bit) shifter for shift operations
311 11 zero_gravi
  )
312 2 zero_gravi
  port map (
313
    -- global control --
314
    clk_i       => clk_i,         -- global clock, rising edge
315
    rstn_i      => rstn_i,        -- global reset, low-active, async
316
    ctrl_i      => ctrl,          -- main control bus
317
    -- data input --
318
    rs1_i       => rs1,           -- rf source 1
319
    rs2_i       => rs2,           -- rf source 2
320 6 zero_gravi
    pc2_i       => curr_pc,       -- delayed PC
321 2 zero_gravi
    imm_i       => imm,           -- immediate
322
    -- data output --
323
    res_o       => alu_res,       -- ALU result
324 36 zero_gravi
    add_o       => alu_add,       -- address computation result
325 2 zero_gravi
    -- co-processor interface --
326 49 zero_gravi
    cp_start_o  => cp_start,      -- trigger co-processor i
327
    cp_valid_i  => cp_valid,      -- co-processor i done
328
    cp_result_i => cp_result,     -- co-processor result
329 2 zero_gravi
    -- status --
330
    wait_o      => alu_wait       -- busy due to iterative processing units
331
  );
332
 
333
 
334 57 zero_gravi
  -- Co-Processor 0: CSR (Read) Access ('Zicsr' Extension) ----------------------------------
335 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
336 57 zero_gravi
  -- "pseudo" co-processor for CSR *read* access operations
337
  -- required to get CSR read data into the data path
338
  cp_result(0) <= csr_rdata when (CPU_EXTENSION_RISCV_Zicsr = true) else (others => '0');
339
  cp_valid(0)  <= cp_start(0); -- always assigned even if Zicsr extension is disabled to make sure CPU does not get stalled if there is an accidental access
340
 
341
 
342
  -- Co-Processor 1: Integer Multiplication/Division ('M' Extension) ------------------------
343
  -- -------------------------------------------------------------------------------------------
344 2 zero_gravi
  neorv32_cpu_cp_muldiv_inst_true:
345
  if (CPU_EXTENSION_RISCV_M = true) generate
346
    neorv32_cpu_cp_muldiv_inst: neorv32_cpu_cp_muldiv
347 19 zero_gravi
    generic map (
348 38 zero_gravi
      FAST_MUL_EN => FAST_MUL_EN  -- use DSPs for faster multiplication
349 19 zero_gravi
    )
350 2 zero_gravi
    port map (
351
      -- global control --
352
      clk_i   => clk_i,           -- global clock, rising edge
353
      rstn_i  => rstn_i,          -- global reset, low-active, async
354
      ctrl_i  => ctrl,            -- main control bus
355 57 zero_gravi
      start_i => cp_start(1),     -- trigger operation
356 2 zero_gravi
      -- data input --
357 27 zero_gravi
      rs1_i   => rs1,             -- rf source 1
358
      rs2_i   => rs2,             -- rf source 2
359 2 zero_gravi
      -- result and status --
360 57 zero_gravi
      res_o   => cp_result(1),    -- operation result
361
      valid_o => cp_valid(1)      -- data output valid
362 2 zero_gravi
    );
363
  end generate;
364
 
365
  neorv32_cpu_cp_muldiv_inst_false:
366
  if (CPU_EXTENSION_RISCV_M = false) generate
367 57 zero_gravi
    cp_result(1) <= (others => '0');
368
    cp_valid(1)  <= cp_start(1); -- to make sure CPU does not get stalled if there is an accidental access
369 2 zero_gravi
  end generate;
370
 
371
 
372 47 zero_gravi
  -- Co-Processor 2: Bit Manipulation ('B' Extension) ---------------------------------------
373 36 zero_gravi
  -- -------------------------------------------------------------------------------------------
374 44 zero_gravi
  neorv32_cpu_cp_bitmanip_inst_true:
375
  if (CPU_EXTENSION_RISCV_B = true) generate
376
    neorv32_cpu_cp_bitmanip_inst: neorv32_cpu_cp_bitmanip
377
    port map (
378
      -- global control --
379
      clk_i   => clk_i,           -- global clock, rising edge
380
      rstn_i  => rstn_i,          -- global reset, low-active, async
381
      ctrl_i  => ctrl,            -- main control bus
382 49 zero_gravi
      start_i => cp_start(2),     -- trigger operation
383 44 zero_gravi
      -- data input --
384 47 zero_gravi
      cmp_i   => comparator,      -- comparator status
385 44 zero_gravi
      rs1_i   => rs1,             -- rf source 1
386
      rs2_i   => rs2,             -- rf source 2
387
      -- result and status --
388 49 zero_gravi
      res_o   => cp_result(2),    -- operation result
389
      valid_o => cp_valid(2)      -- data output valid
390 44 zero_gravi
    );
391
  end generate;
392 36 zero_gravi
 
393 44 zero_gravi
  neorv32_cpu_cp_bitmanip_inst_false:
394
  if (CPU_EXTENSION_RISCV_B = false) generate
395 49 zero_gravi
    cp_result(2) <= (others => '0');
396
    cp_valid(2)  <= cp_start(2); -- to make sure CPU does not get stalled if there is an accidental access
397 44 zero_gravi
  end generate;
398 36 zero_gravi
 
399 44 zero_gravi
 
400 57 zero_gravi
  -- Co-Processor 3: Single-Precision Floating-Point Unit ('Zfinx' Extension) ---------------
401 36 zero_gravi
  -- -------------------------------------------------------------------------------------------
402 52 zero_gravi
  neorv32_cpu_cp_fpu_inst_true:
403 53 zero_gravi
  if (CPU_EXTENSION_RISCV_Zfinx = true) generate
404 52 zero_gravi
    neorv32_cpu_cp_fpu_inst: neorv32_cpu_cp_fpu
405
    port map (
406
      -- global control --
407 53 zero_gravi
      clk_i    => clk_i,        -- global clock, rising edge
408
      rstn_i   => rstn_i,       -- global reset, low-active, async
409
      ctrl_i   => ctrl,         -- main control bus
410 57 zero_gravi
      start_i  => cp_start(3),  -- trigger operation
411 52 zero_gravi
      -- data input --
412 53 zero_gravi
      frm_i    => fpu_rm,       -- rounding mode
413 56 zero_gravi
      cmp_i    => comparator,   -- comparator status
414 53 zero_gravi
      rs1_i    => rs1,          -- rf source 1
415
      rs2_i    => rs2,          -- rf source 2
416 52 zero_gravi
      -- result and status --
417 57 zero_gravi
      res_o    => cp_result(3), -- operation result
418 53 zero_gravi
      fflags_o => fpu_flags,    -- exception flags
419 57 zero_gravi
      valid_o  => cp_valid(3)   -- data output valid
420 52 zero_gravi
    );
421
  end generate;
422
 
423
  neorv32_cpu_cp_fpu_inst_false:
424 53 zero_gravi
  if (CPU_EXTENSION_RISCV_Zfinx = false) generate
425 57 zero_gravi
    cp_result(3) <= (others => '0');
426 53 zero_gravi
    fpu_flags    <= (others => '0');
427 57 zero_gravi
    cp_valid(3)  <= cp_start(3); -- to make sure CPU does not get stalled if there is an accidental access
428 52 zero_gravi
  end generate;
429
 
430
 
431 57 zero_gravi
  -- Co-Processor 4,5,6,7: Not Implemented --------------------------------------------------
432 52 zero_gravi
  -- -------------------------------------------------------------------------------------------
433 57 zero_gravi
  cp_result(4) <= (others => '0');
434
  cp_valid(4)  <= '0';
435
  --
436 49 zero_gravi
  cp_result(5) <= (others => '0');
437
  cp_valid(5)  <= '0';
438
  --
439
  cp_result(6) <= (others => '0');
440
  cp_valid(6)  <= '0';
441
  --
442
  cp_result(7) <= (others => '0');
443
  cp_valid(7)  <= '0';
444
 
445
 
446 12 zero_gravi
  -- Bus Interface Unit ---------------------------------------------------------------------
447 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
448
  neorv32_cpu_bus_inst: neorv32_cpu_bus
449
  generic map (
450 53 zero_gravi
    CPU_EXTENSION_RISCV_A => CPU_EXTENSION_RISCV_A, -- implement atomic extension?
451 11 zero_gravi
    CPU_EXTENSION_RISCV_C => CPU_EXTENSION_RISCV_C, -- implement compressed extension?
452 15 zero_gravi
    -- Physical memory protection (PMP) --
453 42 zero_gravi
    PMP_NUM_REGIONS       => PMP_NUM_REGIONS,       -- number of regions (0..64)
454 57 zero_gravi
    PMP_MIN_GRANULARITY   => PMP_MIN_GRANULARITY    -- minimal region granularity in bytes, has to be a power of 2, min 8 bytes
455 2 zero_gravi
  )
456
  port map (
457
    -- global control --
458 12 zero_gravi
    clk_i          => clk_i,          -- global clock, rising edge
459 38 zero_gravi
    rstn_i         => rstn_i,         -- global reset, low-active, async
460 12 zero_gravi
    ctrl_i         => ctrl,           -- main control bus
461
    -- cpu instruction fetch interface --
462
    fetch_pc_i     => fetch_pc,       -- PC for instruction fetch
463
    instr_o        => instr,          -- instruction
464
    i_wait_o       => bus_i_wait,     -- wait for fetch to complete
465
    --
466
    ma_instr_o     => ma_instr,       -- misaligned instruction address
467
    be_instr_o     => be_instr,       -- bus error on instruction access
468
    -- cpu data access interface --
469 39 zero_gravi
    addr_i         => alu_add,        -- ALU.add result -> access address
470 53 zero_gravi
    wdata_i        => rs2,            -- write data
471 52 zero_gravi
    rdata_o        => mem_rdata,      -- read data
472 12 zero_gravi
    mar_o          => mar,            -- current memory address register
473
    d_wait_o       => bus_d_wait,     -- wait for access to complete
474
    --
475 57 zero_gravi
    excl_state_o   => excl_state,     -- atomic/exclusive access status
476 12 zero_gravi
    ma_load_o      => ma_load,        -- misaligned load data address
477
    ma_store_o     => ma_store,       -- misaligned store data address
478
    be_load_o      => be_load,        -- bus error on load data access
479
    be_store_o     => be_store,       -- bus error on store data access
480 15 zero_gravi
    -- physical memory protection --
481
    pmp_addr_i     => pmp_addr,       -- addresses
482
    pmp_ctrl_i     => pmp_ctrl,       -- configs
483 12 zero_gravi
    -- instruction bus --
484
    i_bus_addr_o   => i_bus_addr_o,   -- bus access address
485
    i_bus_rdata_i  => i_bus_rdata_i,  -- bus read data
486
    i_bus_wdata_o  => i_bus_wdata_o,  -- bus write data
487
    i_bus_ben_o    => i_bus_ben_o,    -- byte enable
488
    i_bus_we_o     => i_bus_we_o,     -- write enable
489
    i_bus_re_o     => i_bus_re_o,     -- read enable
490 57 zero_gravi
    i_bus_lock_o   => i_bus_lock_o,   -- exclusive access request
491 12 zero_gravi
    i_bus_ack_i    => i_bus_ack_i,    -- bus transfer acknowledge
492
    i_bus_err_i    => i_bus_err_i,    -- bus transfer error
493
    i_bus_fence_o  => i_bus_fence_o,  -- fence operation
494
    -- data bus --
495
    d_bus_addr_o   => d_bus_addr_o,   -- bus access address
496
    d_bus_rdata_i  => d_bus_rdata_i,  -- bus read data
497
    d_bus_wdata_o  => d_bus_wdata_o,  -- bus write data
498
    d_bus_ben_o    => d_bus_ben_o,    -- byte enable
499
    d_bus_we_o     => d_bus_we_o,     -- write enable
500
    d_bus_re_o     => d_bus_re_o,     -- read enable
501 57 zero_gravi
    d_bus_lock_o   => d_bus_lock_o,   -- exclusive access request
502 12 zero_gravi
    d_bus_ack_i    => d_bus_ack_i,    -- bus transfer acknowledge
503
    d_bus_err_i    => d_bus_err_i,    -- bus transfer error
504 57 zero_gravi
    d_bus_fence_o  => d_bus_fence_o   -- fence operation
505 2 zero_gravi
  );
506
 
507 35 zero_gravi
  -- current privilege level --
508 36 zero_gravi
  i_bus_priv_o <= ctrl(ctrl_priv_lvl_msb_c downto ctrl_priv_lvl_lsb_c);
509
  d_bus_priv_o <= ctrl(ctrl_priv_lvl_msb_c downto ctrl_priv_lvl_lsb_c);
510 2 zero_gravi
 
511 35 zero_gravi
 
512 2 zero_gravi
end neorv32_cpu_rtl;

powered by: WebSVN 2.1.0

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