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

Subversion Repositories neorv32

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

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 41 zero_gravi
    BUS_TIMEOUT                  : natural := 63;    -- cycles after an UNACKNOWLEDGED bus access triggers a bus fault exception
62 2 zero_gravi
    -- RISC-V CPU Extensions --
63 39 zero_gravi
    CPU_EXTENSION_RISCV_A        : boolean := false; -- implement atomic extension?
64 44 zero_gravi
    CPU_EXTENSION_RISCV_B        : boolean := false; -- implement bit manipulation extensions?
65 12 zero_gravi
    CPU_EXTENSION_RISCV_C        : boolean := false; -- implement compressed extension?
66
    CPU_EXTENSION_RISCV_E        : boolean := false; -- implement embedded RF extension?
67
    CPU_EXTENSION_RISCV_M        : boolean := false; -- implement muld/div extension?
68 15 zero_gravi
    CPU_EXTENSION_RISCV_U        : boolean := false; -- implement user mode extension?
69 53 zero_gravi
    CPU_EXTENSION_RISCV_Zfinx    : boolean := false; -- implement 32-bit floating-point extension (using INT reg!)
70 12 zero_gravi
    CPU_EXTENSION_RISCV_Zicsr    : boolean := true;  -- implement CSR system?
71 52 zero_gravi
    CPU_EXTENSION_RISCV_Zifencei : boolean := false; -- implement instruction stream sync.?
72 19 zero_gravi
    -- Extension Options --
73
    FAST_MUL_EN                  : boolean := false; -- use DSPs for M extension's multiplier
74 34 zero_gravi
    FAST_SHIFT_EN                : boolean := false; -- use barrel shifter for shift operations
75 56 zero_gravi
    TINY_SHIFT_EN                : boolean := false; -- use tiny (single-bit) shifter for shift operations
76
    CPU_CNT_WIDTH                : natural := 64;    -- total width of CPU cycle and instret counters (0..64)
77 15 zero_gravi
    -- Physical Memory Protection (PMP) --
78 55 zero_gravi
    PMP_NUM_REGIONS              : natural := 0;     -- number of regions (0..64)
79 42 zero_gravi
    PMP_MIN_GRANULARITY          : natural := 64*1024; -- minimal region granularity in bytes, has to be a power of 2, min 8 bytes
80
    -- Hardware Performance Monitors (HPM) --
81 56 zero_gravi
    HPM_NUM_CNTS                 : natural := 0;     -- number of implemented HPM counters (0..29)
82
    HPM_CNT_WIDTH                : natural := 40     -- total size of HPM counters (1..64)
83 2 zero_gravi
  );
84
  port (
85
    -- global control --
86 14 zero_gravi
    clk_i          : in  std_ulogic := '0'; -- global clock, rising edge
87
    rstn_i         : in  std_ulogic := '0'; -- global reset, low-active, async
88 47 zero_gravi
    sleep_o        : out std_ulogic; -- cpu is in sleep mode when set
89 12 zero_gravi
    -- instruction bus interface --
90
    i_bus_addr_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
91 14 zero_gravi
    i_bus_rdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0) := (others => '0'); -- bus read data
92 12 zero_gravi
    i_bus_wdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
93
    i_bus_ben_o    : out std_ulogic_vector(03 downto 0); -- byte enable
94
    i_bus_we_o     : out std_ulogic; -- write enable
95
    i_bus_re_o     : out std_ulogic; -- read enable
96
    i_bus_cancel_o : out std_ulogic; -- cancel current bus transaction
97 14 zero_gravi
    i_bus_ack_i    : in  std_ulogic := '0'; -- bus transfer acknowledge
98
    i_bus_err_i    : in  std_ulogic := '0'; -- bus transfer error
99 12 zero_gravi
    i_bus_fence_o  : out std_ulogic; -- executed FENCEI operation
100 35 zero_gravi
    i_bus_priv_o   : out std_ulogic_vector(1 downto 0); -- privilege level
101 12 zero_gravi
    -- data bus interface --
102
    d_bus_addr_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
103 14 zero_gravi
    d_bus_rdata_i  : in  std_ulogic_vector(data_width_c-1 downto 0) := (others => '0'); -- bus read data
104 12 zero_gravi
    d_bus_wdata_o  : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
105
    d_bus_ben_o    : out std_ulogic_vector(03 downto 0); -- byte enable
106
    d_bus_we_o     : out std_ulogic; -- write enable
107
    d_bus_re_o     : out std_ulogic; -- read enable
108
    d_bus_cancel_o : out std_ulogic; -- cancel current bus transaction
109 14 zero_gravi
    d_bus_ack_i    : in  std_ulogic := '0'; -- bus transfer acknowledge
110
    d_bus_err_i    : in  std_ulogic := '0'; -- bus transfer error
111 12 zero_gravi
    d_bus_fence_o  : out std_ulogic; -- executed FENCE operation
112 35 zero_gravi
    d_bus_priv_o   : out std_ulogic_vector(1 downto 0); -- privilege level
113 53 zero_gravi
    d_bus_excl_o   : out std_ulogic; -- exclusive access request
114
    d_bus_excl_i   : in  std_ulogic; -- state of exclusiv access (set if success)
115 11 zero_gravi
    -- system time input from MTIME --
116 14 zero_gravi
    time_i         : in  std_ulogic_vector(63 downto 0) := (others => '0'); -- current system time
117
    -- interrupts (risc-v compliant) --
118
    msw_irq_i      : in  std_ulogic := '0'; -- machine software interrupt
119
    mext_irq_i     : in  std_ulogic := '0'; -- machine external interrupt
120
    mtime_irq_i    : in  std_ulogic := '0'; -- machine timer interrupt
121
    -- fast interrupts (custom) --
122 48 zero_gravi
    firq_i         : in  std_ulogic_vector(15 downto 0) := (others => '0');
123
    firq_ack_o     : out std_ulogic_vector(15 downto 0)
124 2 zero_gravi
  );
125
end neorv32_cpu;
126
 
127
architecture neorv32_cpu_rtl of neorv32_cpu is
128
 
129
  -- local signals --
130 53 zero_gravi
  signal ctrl        : std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
131
  signal comparator  : std_ulogic_vector(1 downto 0); -- comparator result
132
  signal imm         : std_ulogic_vector(data_width_c-1 downto 0); -- immediate
133
  signal instr       : std_ulogic_vector(data_width_c-1 downto 0); -- new instruction
134
  signal rs1, rs2    : std_ulogic_vector(data_width_c-1 downto 0); -- source registers
135
  signal alu_res     : std_ulogic_vector(data_width_c-1 downto 0); -- alu result
136
  signal alu_add     : std_ulogic_vector(data_width_c-1 downto 0); -- alu address result
137
  signal mem_rdata   : std_ulogic_vector(data_width_c-1 downto 0); -- memory read data
138
  signal alu_wait    : std_ulogic; -- alu is busy due to iterative unit
139
  signal bus_i_wait  : std_ulogic; -- wait for current bus instruction fetch
140
  signal bus_d_wait  : std_ulogic; -- wait for current bus data access
141
  signal csr_rdata   : std_ulogic_vector(data_width_c-1 downto 0); -- csr read data
142
  signal mar         : std_ulogic_vector(data_width_c-1 downto 0); -- current memory address register
143
  signal ma_instr    : std_ulogic; -- misaligned instruction address
144
  signal ma_load     : std_ulogic; -- misaligned load data address
145
  signal ma_store    : std_ulogic; -- misaligned store data address
146
  signal bus_excl_ok : std_ulogic; -- atomic memory access successful
147
  signal be_instr    : std_ulogic; -- bus error on instruction access
148
  signal be_load     : std_ulogic; -- bus error on load data access
149
  signal be_store    : std_ulogic; -- bus error on store data access
150
  signal fetch_pc    : std_ulogic_vector(data_width_c-1 downto 0); -- pc for instruction fetch
151
  signal curr_pc     : std_ulogic_vector(data_width_c-1 downto 0); -- current pc (for current executed instruction)
152
  signal fpu_rm      : std_ulogic_vector(2 downto 0); -- FPU rounding mode
153
  signal fpu_flags   : std_ulogic_vector(4 downto 0); -- FPU exception flags
154 2 zero_gravi
 
155
  -- co-processor interface --
156 49 zero_gravi
  signal cp_start  : std_ulogic_vector(7 downto 0); -- trigger co-processor i
157
  signal cp_valid  : std_ulogic_vector(7 downto 0); -- co-processor i done
158
  signal cp_result : cp_data_if_t; -- co-processor result
159 2 zero_gravi
 
160 15 zero_gravi
  -- pmp interface --
161
  signal pmp_addr  : pmp_addr_if_t;
162
  signal pmp_ctrl  : pmp_ctrl_if_t;
163
 
164 47 zero_gravi
  -- atomic memory access - success? --
165 53 zero_gravi
  signal atomic_sc_res    : std_ulogic;
166
  signal atomic_sc_res_ff : std_ulogic;
167
  signal atomic_sc_val    : std_ulogic;
168 47 zero_gravi
 
169 2 zero_gravi
begin
170
 
171 15 zero_gravi
  -- Sanity Checks --------------------------------------------------------------------------
172
  -- -------------------------------------------------------------------------------------------
173 56 zero_gravi
  -- hardware reset notifier --
174
  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;
175
  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;
176
  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;
177
 
178 23 zero_gravi
  -- CSR system --
179 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;
180
 
181
  -- CPU counters (cycle and instret) --
182
  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;
183
  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;
184
 
185 23 zero_gravi
  -- U-extension requires Zicsr extension --
186 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;
187 40 zero_gravi
 
188 41 zero_gravi
  -- Bus timeout --
189
  assert not (BUS_TIMEOUT < 2) report "NEORV32 CPU CONFIG ERROR! Invalid bus access timeout value <BUS_TIMEOUT>. Has to be >= 2." severity error;
190
 
191 38 zero_gravi
  -- Instruction prefetch buffer size --
192
  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;
193 45 zero_gravi
  -- A extension - only lr.w and sc.w are supported yet --
194
  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;
195 15 zero_gravi
 
196 52 zero_gravi
  -- FIXME: Bit manipulation warning --
197 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;
198 44 zero_gravi
 
199 55 zero_gravi
  -- Co-processor timeout counter (for debugging only) --
200
  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;
201 52 zero_gravi
 
202 40 zero_gravi
  -- PMP regions check --
203 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;
204 40 zero_gravi
  -- PMP granulartiy --
205 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;
206
  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;
207 40 zero_gravi
  -- PMP notifier --
208 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;
209 56 zero_gravi
  -- PMP requires Zicsr extension --
210
  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;
211 40 zero_gravi
 
212 42 zero_gravi
  -- HPM counters check --
213
  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;
214 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;
215 42 zero_gravi
  -- HPM counters notifier --
216 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;
217 44 zero_gravi
  -- HPM CNT requires Zicsr extension --
218 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;
219 41 zero_gravi
 
220 42 zero_gravi
 
221 2 zero_gravi
  -- Control Unit ---------------------------------------------------------------------------
222
  -- -------------------------------------------------------------------------------------------
223
  neorv32_cpu_control_inst: neorv32_cpu_control
224
  generic map (
225
    -- General --
226 40 zero_gravi
    HW_THREAD_ID                 => HW_THREAD_ID,  -- hardware thread id
227
    CPU_BOOT_ADDR                => CPU_BOOT_ADDR, -- cpu boot address
228 2 zero_gravi
    -- RISC-V CPU Extensions --
229 39 zero_gravi
    CPU_EXTENSION_RISCV_A        => CPU_EXTENSION_RISCV_A,        -- implement atomic extension?
230 44 zero_gravi
    CPU_EXTENSION_RISCV_B        => CPU_EXTENSION_RISCV_B,        -- implement bit manipulation extensions?
231 15 zero_gravi
    CPU_EXTENSION_RISCV_C        => CPU_EXTENSION_RISCV_C,        -- implement compressed extension?
232
    CPU_EXTENSION_RISCV_M        => CPU_EXTENSION_RISCV_M,        -- implement muld/div extension?
233
    CPU_EXTENSION_RISCV_U        => CPU_EXTENSION_RISCV_U,        -- implement user mode extension?
234 53 zero_gravi
    CPU_EXTENSION_RISCV_Zfinx    => CPU_EXTENSION_RISCV_Zfinx,    -- implement 32-bit floating-point extension (using INT reg!)
235 15 zero_gravi
    CPU_EXTENSION_RISCV_Zicsr    => CPU_EXTENSION_RISCV_Zicsr,    -- implement CSR system?
236
    CPU_EXTENSION_RISCV_Zifencei => CPU_EXTENSION_RISCV_Zifencei, -- implement instruction stream sync.?
237 56 zero_gravi
    -- Extension Options --
238
    CPU_CNT_WIDTH                => CPU_CNT_WIDTH,                -- total width of CPU cycle and instret counters (0..64)
239 15 zero_gravi
    -- Physical memory protection (PMP) --
240 42 zero_gravi
    PMP_NUM_REGIONS              => PMP_NUM_REGIONS,              -- number of regions (0..64)
241
    PMP_MIN_GRANULARITY          => PMP_MIN_GRANULARITY,          -- minimal region granularity in bytes, has to be a power of 2, min 8 bytes
242
    -- Hardware Performance Monitors (HPM) --
243 56 zero_gravi
    HPM_NUM_CNTS                 => HPM_NUM_CNTS,                 -- number of implemented HPM counters (0..29)
244
    HPM_CNT_WIDTH                => HPM_CNT_WIDTH                 -- total size of HPM counters
245 2 zero_gravi
  )
246
  port map (
247
    -- global control --
248
    clk_i         => clk_i,       -- global clock, rising edge
249
    rstn_i        => rstn_i,      -- global reset, low-active, async
250
    ctrl_o        => ctrl,        -- main control bus
251
    -- status input --
252
    alu_wait_i    => alu_wait,    -- wait for ALU
253 12 zero_gravi
    bus_i_wait_i  => bus_i_wait,  -- wait for bus
254
    bus_d_wait_i  => bus_d_wait,  -- wait for bus
255 2 zero_gravi
    -- data input --
256
    instr_i       => instr,       -- instruction
257 47 zero_gravi
    cmp_i         => comparator,  -- comparator status
258 36 zero_gravi
    alu_add_i     => alu_add,     -- ALU address result
259
    rs1_i         => rs1,         -- rf source 1
260 2 zero_gravi
    -- data output --
261
    imm_o         => imm,         -- immediate
262 6 zero_gravi
    fetch_pc_o    => fetch_pc,    -- PC for instruction fetch
263
    curr_pc_o     => curr_pc,     -- current PC (corresponding to current instruction)
264 2 zero_gravi
    csr_rdata_o   => csr_rdata,   -- CSR read data
265 52 zero_gravi
    -- FPU interface --
266
    fpu_rm_o      => fpu_rm,      -- rounding mode
267
    fpu_flags_i   => fpu_flags,   -- exception flags
268 14 zero_gravi
    -- interrupts (risc-v compliant) --
269
    msw_irq_i     => msw_irq_i,   -- machine software interrupt
270
    mext_irq_i    => mext_irq_i,  -- machine external interrupt
271 2 zero_gravi
    mtime_irq_i   => mtime_irq_i, -- machine timer interrupt
272 14 zero_gravi
    -- fast interrupts (custom) --
273 47 zero_gravi
    firq_i        => firq_i,      -- fast interrupt trigger
274
    firq_ack_o    => firq_ack_o,  -- fast interrupt acknowledge mask
275 11 zero_gravi
    -- system time input from MTIME --
276
    time_i        => time_i,      -- current system time
277 15 zero_gravi
    -- physical memory protection --
278
    pmp_addr_o    => pmp_addr,    -- addresses
279
    pmp_ctrl_o    => pmp_ctrl,    -- configs
280 2 zero_gravi
    -- bus access exceptions --
281
    mar_i         => mar,         -- memory address register
282
    ma_instr_i    => ma_instr,    -- misaligned instruction address
283
    ma_load_i     => ma_load,     -- misaligned load data address
284
    ma_store_i    => ma_store,    -- misaligned store data address
285
    be_instr_i    => be_instr,    -- bus error on instruction access
286
    be_load_i     => be_load,     -- bus error on load data access
287 12 zero_gravi
    be_store_i    => be_store     -- bus error on store data access
288 2 zero_gravi
  );
289
 
290 47 zero_gravi
  -- CPU is sleeping? --
291
  sleep_o <= ctrl(ctrl_sleep_c); -- set when CPU is sleeping (after WFI)
292 2 zero_gravi
 
293 47 zero_gravi
 
294 2 zero_gravi
  -- Register File --------------------------------------------------------------------------
295
  -- -------------------------------------------------------------------------------------------
296 45 zero_gravi
  neorv32_cpu_regfile_inst: neorv32_cpu_regfile
297 2 zero_gravi
  generic map (
298
    CPU_EXTENSION_RISCV_E => CPU_EXTENSION_RISCV_E -- implement embedded RF extension?
299
  )
300
  port map (
301
    -- global control --
302
    clk_i  => clk_i,              -- global clock, rising edge
303
    ctrl_i => ctrl,               -- main control bus
304
    -- data input --
305 52 zero_gravi
    mem_i  => mem_rdata,          -- memory read data
306 2 zero_gravi
    alu_i  => alu_res,            -- ALU result
307
    -- data output --
308
    rs1_o  => rs1,                -- operand 1
309 47 zero_gravi
    rs2_o  => rs2,                -- operand 2
310
    cmp_o  => comparator          -- comparator status
311 2 zero_gravi
  );
312
 
313
 
314
  -- ALU ------------------------------------------------------------------------------------
315
  -- -------------------------------------------------------------------------------------------
316
  neorv32_cpu_alu_inst: neorv32_cpu_alu
317 11 zero_gravi
  generic map (
318 34 zero_gravi
    CPU_EXTENSION_RISCV_M => CPU_EXTENSION_RISCV_M, -- implement muld/div extension?
319 56 zero_gravi
    FAST_SHIFT_EN         => FAST_SHIFT_EN,         -- use barrel shifter for shift operations
320
    TINY_SHIFT_EN         => TINY_SHIFT_EN          -- use tiny (single-bit) shifter for shift operations
321 11 zero_gravi
  )
322 2 zero_gravi
  port map (
323
    -- global control --
324
    clk_i       => clk_i,         -- global clock, rising edge
325
    rstn_i      => rstn_i,        -- global reset, low-active, async
326
    ctrl_i      => ctrl,          -- main control bus
327
    -- data input --
328
    rs1_i       => rs1,           -- rf source 1
329
    rs2_i       => rs2,           -- rf source 2
330 6 zero_gravi
    pc2_i       => curr_pc,       -- delayed PC
331 2 zero_gravi
    imm_i       => imm,           -- immediate
332
    -- data output --
333
    res_o       => alu_res,       -- ALU result
334 36 zero_gravi
    add_o       => alu_add,       -- address computation result
335 2 zero_gravi
    -- co-processor interface --
336 49 zero_gravi
    cp_start_o  => cp_start,      -- trigger co-processor i
337
    cp_valid_i  => cp_valid,      -- co-processor i done
338
    cp_result_i => cp_result,     -- co-processor result
339 2 zero_gravi
    -- status --
340
    wait_o      => alu_wait       -- busy due to iterative processing units
341
  );
342
 
343
 
344 47 zero_gravi
  -- Co-Processor 0: Integer Multiplication/Division ('M' Extension) ------------------------
345 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
346
  neorv32_cpu_cp_muldiv_inst_true:
347
  if (CPU_EXTENSION_RISCV_M = true) generate
348
    neorv32_cpu_cp_muldiv_inst: neorv32_cpu_cp_muldiv
349 19 zero_gravi
    generic map (
350 38 zero_gravi
      FAST_MUL_EN => FAST_MUL_EN  -- use DSPs for faster multiplication
351 19 zero_gravi
    )
352 2 zero_gravi
    port map (
353
      -- global control --
354
      clk_i   => clk_i,           -- global clock, rising edge
355
      rstn_i  => rstn_i,          -- global reset, low-active, async
356
      ctrl_i  => ctrl,            -- main control bus
357 49 zero_gravi
      start_i => cp_start(0),     -- trigger operation
358 2 zero_gravi
      -- data input --
359 27 zero_gravi
      rs1_i   => rs1,             -- rf source 1
360
      rs2_i   => rs2,             -- rf source 2
361 2 zero_gravi
      -- result and status --
362 49 zero_gravi
      res_o   => cp_result(0),    -- operation result
363
      valid_o => cp_valid(0)      -- data output valid
364 2 zero_gravi
    );
365
  end generate;
366
 
367
  neorv32_cpu_cp_muldiv_inst_false:
368
  if (CPU_EXTENSION_RISCV_M = false) generate
369 49 zero_gravi
    cp_result(0) <= (others => '0');
370
    cp_valid(0)  <= cp_start(0); -- to make sure CPU does not get stalled if there is an accidental access
371 2 zero_gravi
  end generate;
372
 
373
 
374 47 zero_gravi
  -- Co-Processor 1: Atomic Memory Access ('A' Extension) -----------------------------------
375 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
376 47 zero_gravi
  -- "pseudo" co-processor for atomic operations
377 49 zero_gravi
  -- required to get the result of a store-conditional operation into the data path
378 56 zero_gravi
  atomic_op_cp: process(rstn_i, clk_i)
379 39 zero_gravi
  begin
380 56 zero_gravi
    if (rstn_i = '0') then
381
      atomic_sc_val    <= def_rst_val_c;
382
      atomic_sc_res    <= def_rst_val_c;
383
      atomic_sc_res_ff <= def_rst_val_c;
384
    elsif rising_edge(clk_i) then
385 53 zero_gravi
      atomic_sc_val <= cp_start(1);
386
      atomic_sc_res <= bus_excl_ok;
387
      if (atomic_sc_val = '1') then
388
        atomic_sc_res_ff <= not atomic_sc_res;
389 40 zero_gravi
      else
390 53 zero_gravi
        atomic_sc_res_ff <= '0';
391 40 zero_gravi
      end if;
392 39 zero_gravi
    end if;
393 40 zero_gravi
  end process atomic_op_cp;
394 2 zero_gravi
 
395 47 zero_gravi
  -- CP result --
396 49 zero_gravi
  cp_result(1)(data_width_c-1 downto 1) <= (others => '0');
397 53 zero_gravi
  cp_result(1)(0) <= atomic_sc_res_ff when (CPU_EXTENSION_RISCV_A = true) else '0';
398
  cp_valid(1)     <= atomic_sc_val    when (CPU_EXTENSION_RISCV_A = true) else cp_start(1); -- assigned even if A extension is disabled so CPU does not get stalled on accidental access
399 2 zero_gravi
 
400 47 zero_gravi
 
401
  -- Co-Processor 2: Bit Manipulation ('B' Extension) ---------------------------------------
402 36 zero_gravi
  -- -------------------------------------------------------------------------------------------
403 44 zero_gravi
  neorv32_cpu_cp_bitmanip_inst_true:
404
  if (CPU_EXTENSION_RISCV_B = true) generate
405
    neorv32_cpu_cp_bitmanip_inst: neorv32_cpu_cp_bitmanip
406
    port map (
407
      -- global control --
408
      clk_i   => clk_i,           -- global clock, rising edge
409
      rstn_i  => rstn_i,          -- global reset, low-active, async
410
      ctrl_i  => ctrl,            -- main control bus
411 49 zero_gravi
      start_i => cp_start(2),     -- trigger operation
412 44 zero_gravi
      -- data input --
413 47 zero_gravi
      cmp_i   => comparator,      -- comparator status
414 44 zero_gravi
      rs1_i   => rs1,             -- rf source 1
415
      rs2_i   => rs2,             -- rf source 2
416
      -- result and status --
417 49 zero_gravi
      res_o   => cp_result(2),    -- operation result
418
      valid_o => cp_valid(2)      -- data output valid
419 44 zero_gravi
    );
420
  end generate;
421 36 zero_gravi
 
422 44 zero_gravi
  neorv32_cpu_cp_bitmanip_inst_false:
423
  if (CPU_EXTENSION_RISCV_B = false) generate
424 49 zero_gravi
    cp_result(2) <= (others => '0');
425
    cp_valid(2)  <= cp_start(2); -- to make sure CPU does not get stalled if there is an accidental access
426 44 zero_gravi
  end generate;
427 36 zero_gravi
 
428 44 zero_gravi
 
429 49 zero_gravi
  -- Co-Processor 3: CSR (Read) Access ('Zicsr' Extension) ----------------------------------
430 36 zero_gravi
  -- -------------------------------------------------------------------------------------------
431 49 zero_gravi
  -- "pseudo" co-processor for CSR *read* access operations
432 56 zero_gravi
  -- required to get CSR read data into the data path
433 49 zero_gravi
  cp_result(3) <= csr_rdata when (CPU_EXTENSION_RISCV_Zicsr = true) else (others => '0');
434
  cp_valid(3)  <= cp_start(3); -- always assigned even if Zicsr extension is disabled to make sure CPU does not get stalled if there is an accidental access
435 36 zero_gravi
 
436
 
437 53 zero_gravi
  -- Co-Processor 4: Single-Precision Floating-Point Unit ('Zfinx' Extension) ---------------
438 49 zero_gravi
  -- -------------------------------------------------------------------------------------------
439 52 zero_gravi
  neorv32_cpu_cp_fpu_inst_true:
440 53 zero_gravi
  if (CPU_EXTENSION_RISCV_Zfinx = true) generate
441 52 zero_gravi
    neorv32_cpu_cp_fpu_inst: neorv32_cpu_cp_fpu
442
    port map (
443
      -- global control --
444 53 zero_gravi
      clk_i    => clk_i,        -- global clock, rising edge
445
      rstn_i   => rstn_i,       -- global reset, low-active, async
446
      ctrl_i   => ctrl,         -- main control bus
447
      start_i  => cp_start(4),  -- trigger operation
448 52 zero_gravi
      -- data input --
449 53 zero_gravi
      frm_i    => fpu_rm,       -- rounding mode
450 56 zero_gravi
      cmp_i    => comparator,   -- comparator status
451 53 zero_gravi
      rs1_i    => rs1,          -- rf source 1
452
      rs2_i    => rs2,          -- rf source 2
453 52 zero_gravi
      -- result and status --
454 53 zero_gravi
      res_o    => cp_result(4), -- operation result
455
      fflags_o => fpu_flags,    -- exception flags
456
      valid_o  => cp_valid(4)   -- data output valid
457 52 zero_gravi
    );
458
  end generate;
459
 
460
  neorv32_cpu_cp_fpu_inst_false:
461 53 zero_gravi
  if (CPU_EXTENSION_RISCV_Zfinx = false) generate
462
    cp_result(4) <= (others => '0');
463
    fpu_flags    <= (others => '0');
464
    cp_valid(4)  <= cp_start(4); -- to make sure CPU does not get stalled if there is an accidental access
465 52 zero_gravi
  end generate;
466
 
467
 
468 56 zero_gravi
  -- Co-Processor 5,6,7: Not Implemented Yet ------------------------------------------------
469 52 zero_gravi
  -- -------------------------------------------------------------------------------------------
470 49 zero_gravi
  cp_result(5) <= (others => '0');
471
  cp_valid(5)  <= '0';
472
  --
473
  cp_result(6) <= (others => '0');
474
  cp_valid(6)  <= '0';
475
  --
476
  cp_result(7) <= (others => '0');
477
  cp_valid(7)  <= '0';
478
 
479
 
480 12 zero_gravi
  -- Bus Interface Unit ---------------------------------------------------------------------
481 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
482
  neorv32_cpu_bus_inst: neorv32_cpu_bus
483
  generic map (
484 53 zero_gravi
    CPU_EXTENSION_RISCV_A => CPU_EXTENSION_RISCV_A, -- implement atomic extension?
485 11 zero_gravi
    CPU_EXTENSION_RISCV_C => CPU_EXTENSION_RISCV_C, -- implement compressed extension?
486 15 zero_gravi
    -- Physical memory protection (PMP) --
487 42 zero_gravi
    PMP_NUM_REGIONS       => PMP_NUM_REGIONS,       -- number of regions (0..64)
488
    PMP_MIN_GRANULARITY   => PMP_MIN_GRANULARITY,   -- minimal region granularity in bytes, has to be a power of 2, min 8 bytes
489 41 zero_gravi
    -- Bus Timeout --
490
    BUS_TIMEOUT           => BUS_TIMEOUT            -- cycles after an UNACKNOWLEDGED bus access triggers a bus fault exception
491 2 zero_gravi
  )
492
  port map (
493
    -- global control --
494 12 zero_gravi
    clk_i          => clk_i,          -- global clock, rising edge
495 38 zero_gravi
    rstn_i         => rstn_i,         -- global reset, low-active, async
496 12 zero_gravi
    ctrl_i         => ctrl,           -- main control bus
497
    -- cpu instruction fetch interface --
498
    fetch_pc_i     => fetch_pc,       -- PC for instruction fetch
499
    instr_o        => instr,          -- instruction
500
    i_wait_o       => bus_i_wait,     -- wait for fetch to complete
501
    --
502
    ma_instr_o     => ma_instr,       -- misaligned instruction address
503
    be_instr_o     => be_instr,       -- bus error on instruction access
504
    -- cpu data access interface --
505 39 zero_gravi
    addr_i         => alu_add,        -- ALU.add result -> access address
506 53 zero_gravi
    wdata_i        => rs2,            -- write data
507 52 zero_gravi
    rdata_o        => mem_rdata,      -- read data
508 12 zero_gravi
    mar_o          => mar,            -- current memory address register
509
    d_wait_o       => bus_d_wait,     -- wait for access to complete
510
    --
511 53 zero_gravi
    bus_excl_ok_o  => bus_excl_ok,    -- bus exclusive access successful
512 12 zero_gravi
    ma_load_o      => ma_load,        -- misaligned load data address
513
    ma_store_o     => ma_store,       -- misaligned store data address
514
    be_load_o      => be_load,        -- bus error on load data access
515
    be_store_o     => be_store,       -- bus error on store data access
516 15 zero_gravi
    -- physical memory protection --
517
    pmp_addr_i     => pmp_addr,       -- addresses
518
    pmp_ctrl_i     => pmp_ctrl,       -- configs
519 12 zero_gravi
    -- instruction bus --
520
    i_bus_addr_o   => i_bus_addr_o,   -- bus access address
521
    i_bus_rdata_i  => i_bus_rdata_i,  -- bus read data
522
    i_bus_wdata_o  => i_bus_wdata_o,  -- bus write data
523
    i_bus_ben_o    => i_bus_ben_o,    -- byte enable
524
    i_bus_we_o     => i_bus_we_o,     -- write enable
525
    i_bus_re_o     => i_bus_re_o,     -- read enable
526
    i_bus_cancel_o => i_bus_cancel_o, -- cancel current bus transaction
527
    i_bus_ack_i    => i_bus_ack_i,    -- bus transfer acknowledge
528
    i_bus_err_i    => i_bus_err_i,    -- bus transfer error
529
    i_bus_fence_o  => i_bus_fence_o,  -- fence operation
530
    -- data bus --
531
    d_bus_addr_o   => d_bus_addr_o,   -- bus access address
532
    d_bus_rdata_i  => d_bus_rdata_i,  -- bus read data
533
    d_bus_wdata_o  => d_bus_wdata_o,  -- bus write data
534
    d_bus_ben_o    => d_bus_ben_o,    -- byte enable
535
    d_bus_we_o     => d_bus_we_o,     -- write enable
536
    d_bus_re_o     => d_bus_re_o,     -- read enable
537
    d_bus_cancel_o => d_bus_cancel_o, -- cancel current bus transaction
538
    d_bus_ack_i    => d_bus_ack_i,    -- bus transfer acknowledge
539
    d_bus_err_i    => d_bus_err_i,    -- bus transfer error
540 39 zero_gravi
    d_bus_fence_o  => d_bus_fence_o,  -- fence operation
541 53 zero_gravi
    d_bus_excl_o   => d_bus_excl_o,   -- exclusive access request
542
    d_bus_excl_i   => d_bus_excl_i    -- state of exclusiv access (set if success)
543 2 zero_gravi
  );
544
 
545 35 zero_gravi
  -- current privilege level --
546 36 zero_gravi
  i_bus_priv_o <= ctrl(ctrl_priv_lvl_msb_c downto ctrl_priv_lvl_lsb_c);
547
  d_bus_priv_o <= ctrl(ctrl_priv_lvl_msb_c downto ctrl_priv_lvl_lsb_c);
548 2 zero_gravi
 
549 35 zero_gravi
 
550 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.