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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_cpu_alu.vhd] - Blame information for rev 63

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 zero_gravi
-- #################################################################################################
2
-- # << NEORV32 - Arithmetical/Logical Unit >>                                                     #
3
-- # ********************************************************************************************* #
4 47 zero_gravi
-- # Main data and address ALU and co-processor interface/arbiter.                                 #
5 2 zero_gravi
-- # ********************************************************************************************* #
6
-- # BSD 3-Clause License                                                                          #
7
-- #                                                                                               #
8 44 zero_gravi
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
9 2 zero_gravi
-- #                                                                                               #
10
-- # Redistribution and use in source and binary forms, with or without modification, are          #
11
-- # permitted provided that the following conditions are met:                                     #
12
-- #                                                                                               #
13
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
14
-- #    conditions and the following disclaimer.                                                   #
15
-- #                                                                                               #
16
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
17
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
18
-- #    provided with the distribution.                                                            #
19
-- #                                                                                               #
20
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
21
-- #    endorse or promote products derived from this software without specific prior written      #
22
-- #    permission.                                                                                #
23
-- #                                                                                               #
24
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
25
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
26
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
27
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
28
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
29
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
30
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
31
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
32
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
33
-- # ********************************************************************************************* #
34
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
35
-- #################################################################################################
36
 
37
library ieee;
38
use ieee.std_logic_1164.all;
39
use ieee.numeric_std.all;
40
 
41
library neorv32;
42
use neorv32.neorv32_package.all;
43
 
44
entity neorv32_cpu_alu is
45 11 zero_gravi
  generic (
46 61 zero_gravi
    -- RISC-V CPU Extensions --
47 62 zero_gravi
    CPU_EXTENSION_RISCV_M     : boolean; -- implement mul/div extension?
48 63 zero_gravi
    CPU_EXTENSION_RISCV_Zbb   : boolean; -- implement basic bit-manipulation sub-extension?
49 62 zero_gravi
    CPU_EXTENSION_RISCV_Zmmul : boolean; -- implement multiply-only M sub-extension?
50
    CPU_EXTENSION_RISCV_Zfinx : boolean; -- implement 32-bit floating-point extension (using INT reg!)
51 61 zero_gravi
    -- Extension Options --
52 62 zero_gravi
    FAST_MUL_EN               : boolean; -- use DSPs for M extension's multiplier
53
    FAST_SHIFT_EN             : boolean  -- use barrel shifter for shift operations
54 11 zero_gravi
  );
55 2 zero_gravi
  port (
56
    -- global control --
57
    clk_i       : in  std_ulogic; -- global clock, rising edge
58
    rstn_i      : in  std_ulogic; -- global reset, low-active, async
59
    ctrl_i      : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
60
    -- data input --
61
    rs1_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
62
    rs2_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
63
    pc2_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- delayed PC
64
    imm_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- immediate
65 61 zero_gravi
    csr_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- CSR read data
66
    cmp_i       : in  std_ulogic_vector(1 downto 0); -- comparator status
67 2 zero_gravi
    -- data output --
68
    res_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
69 36 zero_gravi
    add_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- address computation result
70 61 zero_gravi
    fpu_flags_o : out std_ulogic_vector(4 downto 0); -- FPU exception flags
71 2 zero_gravi
    -- status --
72 61 zero_gravi
    idone_o     : out std_ulogic -- iterative processing units done?
73 2 zero_gravi
  );
74
end neorv32_cpu_alu;
75
 
76
architecture neorv32_cpu_cpu_rtl of neorv32_cpu_alu is
77
 
78
  -- operands --
79 29 zero_gravi
  signal opa, opb : std_ulogic_vector(data_width_c-1 downto 0);
80 2 zero_gravi
 
81
  -- results --
82 36 zero_gravi
  signal addsub_res : std_ulogic_vector(data_width_c downto 0);
83 39 zero_gravi
  --
84 29 zero_gravi
  signal cp_res     : std_ulogic_vector(data_width_c-1 downto 0);
85 39 zero_gravi
  signal arith_res  : std_ulogic_vector(data_width_c-1 downto 0);
86
  signal logic_res  : std_ulogic_vector(data_width_c-1 downto 0);
87 2 zero_gravi
 
88 19 zero_gravi
  -- co-processor arbiter and interface --
89
  type cp_ctrl_t is record
90 55 zero_gravi
    cmd     : std_ulogic;
91
    cmd_ff  : std_ulogic;
92 61 zero_gravi
    start   : std_ulogic;
93 55 zero_gravi
    busy    : std_ulogic;
94
    timeout : std_ulogic_vector(9 downto 0);
95 19 zero_gravi
  end record;
96
  signal cp_ctrl : cp_ctrl_t;
97 2 zero_gravi
 
98 61 zero_gravi
  -- co-processor interface --
99
  signal cp_start  : std_ulogic_vector(3 downto 0); -- trigger co-processor i
100
  signal cp_valid  : std_ulogic_vector(3 downto 0); -- co-processor i done
101
  signal cp_result : cp_data_if_t; -- co-processor result
102
 
103 2 zero_gravi
begin
104
 
105
  -- Operand Mux ----------------------------------------------------------------------------
106
  -- -------------------------------------------------------------------------------------------
107 36 zero_gravi
  opa <= pc2_i when (ctrl_i(ctrl_alu_opa_mux_c) = '1') else rs1_i; -- operand a (first ALU input operand), only required for arithmetic ops
108 29 zero_gravi
  opb <= imm_i when (ctrl_i(ctrl_alu_opb_mux_c) = '1') else rs2_i; -- operand b (second ALU input operand)
109 2 zero_gravi
 
110
 
111 61 zero_gravi
  -- Binary Adder/Subtracter ----------------------------------------------------------------
112 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
113 29 zero_gravi
  binary_arithmetic_core: process(ctrl_i, opa, opb)
114
    variable cin_v  : std_ulogic_vector(0 downto 0);
115
    variable op_a_v : std_ulogic_vector(data_width_c downto 0);
116
    variable op_b_v : std_ulogic_vector(data_width_c downto 0);
117
    variable op_y_v : std_ulogic_vector(data_width_c downto 0);
118
    variable res_v  : std_ulogic_vector(data_width_c downto 0);
119
  begin
120
    -- operand sign-extension --
121
    op_a_v := (opa(opa'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & opa;
122
    op_b_v := (opb(opb'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & opb;
123
    -- add/sub(slt) select --
124
    if (ctrl_i(ctrl_alu_addsub_c) = '1') then -- subtraction
125
      op_y_v   := not op_b_v;
126
      cin_v(0) := '1';
127 36 zero_gravi
    else -- addition
128 29 zero_gravi
      op_y_v   := op_b_v;
129
      cin_v(0) := '0';
130
    end if;
131 36 zero_gravi
    -- adder core (result + carry/borrow) --
132
    addsub_res <= std_ulogic_vector(unsigned(op_a_v) + unsigned(op_y_v) + unsigned(cin_v(0 downto 0)));
133 29 zero_gravi
  end process binary_arithmetic_core;
134
 
135 36 zero_gravi
  -- direct output of address result --
136
  add_o <= addsub_res(data_width_c-1 downto 0);
137 29 zero_gravi
 
138 39 zero_gravi
  -- ALU arithmetic logic core --
139
  arithmetic_core: process(ctrl_i, addsub_res)
140
  begin
141
    if (ctrl_i(ctrl_alu_arith_c) = alu_arith_cmd_addsub_c) then -- ADD/SUB
142
      arith_res <= addsub_res(data_width_c-1 downto 0);
143
    else -- SLT
144
      arith_res <= (others => '0');
145
      arith_res(0) <= addsub_res(addsub_res'left); -- => carry/borrow
146
    end if;
147
  end process arithmetic_core;
148 36 zero_gravi
 
149 39 zero_gravi
 
150 47 zero_gravi
  -- Co-Processor Arbiter -------------------------------------------------------------------
151 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
152 47 zero_gravi
  -- Interface:
153
  -- Co-processor "valid" signal has to be asserted (for one cycle) one cycle before asserting output data
154
  -- Co-processor "output data" has to be always zero unless co-processor was explicitly triggered
155 19 zero_gravi
  cp_arbiter: process(rstn_i, clk_i)
156 2 zero_gravi
  begin
157
    if (rstn_i = '0') then
158 55 zero_gravi
      cp_ctrl.cmd_ff  <= '0';
159
      cp_ctrl.busy    <= '0';
160
      cp_ctrl.timeout <= (others => '0');
161 2 zero_gravi
    elsif rising_edge(clk_i) then
162 40 zero_gravi
      cp_ctrl.cmd_ff <= cp_ctrl.cmd;
163 61 zero_gravi
      -- timeout counter --
164
      if (cp_ctrl.start = '1') then
165
        cp_ctrl.busy <= '1';
166
      elsif (or_reduce_f(cp_valid) = '1') then
167 40 zero_gravi
        cp_ctrl.busy <= '0';
168 2 zero_gravi
      end if;
169 55 zero_gravi
      if (cp_ctrl.busy = '1') and (cp_timeout_en_c = true) then
170
        cp_ctrl.timeout <= std_ulogic_vector(unsigned(cp_ctrl.timeout) + 1);
171
      else
172
        cp_ctrl.timeout <= (others => '0');
173
      end if;
174 61 zero_gravi
      if (cp_ctrl.timeout(cp_ctrl.timeout'left) = '1') and (cp_timeout_en_c = true) then -- timeout
175
        assert false report "NEORV32 CPU CO-PROCESSOR TIMEOUT ERROR!" severity warning;
176
      end if;
177 2 zero_gravi
    end if;
178 19 zero_gravi
  end process cp_arbiter;
179 2 zero_gravi
 
180
  -- is co-processor operation? --
181 39 zero_gravi
  cp_ctrl.cmd   <= '1' when (ctrl_i(ctrl_alu_func1_c downto ctrl_alu_func0_c) = alu_func_cmd_copro_c) else '0';
182 29 zero_gravi
  cp_ctrl.start <= '1' when (cp_ctrl.cmd = '1') and (cp_ctrl.cmd_ff = '0') else '0';
183 2 zero_gravi
 
184 61 zero_gravi
  -- co-processor select / star trigger --
185
  cp_start(0) <= '1' when (cp_ctrl.start = '1') and (ctrl_i(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) = "00") else '0';
186
  cp_start(1) <= '1' when (cp_ctrl.start = '1') and (ctrl_i(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) = "01") else '0';
187
  cp_start(2) <= '1' when (cp_ctrl.start = '1') and (ctrl_i(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) = "10") else '0';
188
  cp_start(3) <= '1' when (cp_ctrl.start = '1') and (ctrl_i(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) = "11") else '0';
189 2 zero_gravi
 
190 61 zero_gravi
  -- co-processor operation done? --
191
  idone_o <= or_reduce_f(cp_valid);
192 39 zero_gravi
 
193 49 zero_gravi
  -- co-processor result - only the *actually selected* co-processor may output data != 0 --
194 61 zero_gravi
  cp_res <= cp_result(0) or cp_result(1) or cp_result(2) or cp_result(3);
195 24 zero_gravi
 
196
 
197 39 zero_gravi
  -- ALU Logic Core -------------------------------------------------------------------------
198
  -- -------------------------------------------------------------------------------------------
199
  alu_logic_core: process(ctrl_i, rs1_i, opb)
200
  begin
201
    case ctrl_i(ctrl_alu_logic1_c downto ctrl_alu_logic0_c) is
202
      when alu_logic_cmd_movb_c => logic_res <= opb; -- (default)
203
      when alu_logic_cmd_xor_c  => logic_res <= rs1_i xor opb; -- only rs1 required for logic ops (opa would also contain pc)
204
      when alu_logic_cmd_or_c   => logic_res <= rs1_i or  opb;
205
      when alu_logic_cmd_and_c  => logic_res <= rs1_i and opb;
206
      when others               => logic_res <= opb; -- undefined
207
    end case;
208
  end process alu_logic_core;
209
 
210
 
211 2 zero_gravi
  -- ALU Function Select --------------------------------------------------------------------
212
  -- -------------------------------------------------------------------------------------------
213 61 zero_gravi
  alu_function_mux: process(ctrl_i, arith_res, logic_res, csr_i, cp_res)
214 2 zero_gravi
  begin
215 39 zero_gravi
    case ctrl_i(ctrl_alu_func1_c downto ctrl_alu_func0_c) is
216
      when alu_func_cmd_arith_c => res_o <= arith_res; -- (default)
217
      when alu_func_cmd_logic_c => res_o <= logic_res;
218 61 zero_gravi
      when alu_func_cmd_csrr_c  => res_o <= csr_i;
219 39 zero_gravi
      when alu_func_cmd_copro_c => res_o <= cp_res;
220
      when others               => res_o <= arith_res; -- undefined
221 2 zero_gravi
    end case;
222
  end process alu_function_mux;
223
 
224
 
225 61 zero_gravi
  -- **************************************************************************************************************************
226
  -- Co-Processors
227
  -- **************************************************************************************************************************
228
 
229
  -- Co-Processor 0: Shifter (CPU Core ISA) --------------------------------------------------
230 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
231 61 zero_gravi
    neorv32_cpu_cp_shifter_inst: neorv32_cpu_cp_shifter
232
    generic map (
233
      FAST_SHIFT_EN => FAST_SHIFT_EN -- use barrel shifter for shift operations
234
    )
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_i  => ctrl_i,          -- main control bus
240
      start_i => cp_start(0),     -- trigger operation
241
      -- data input --
242
      rs1_i   => rs1_i,           -- rf source 1
243
      rs2_i   => rs2_i,           -- rf source 2
244
      imm_i   => imm_i,           -- immediate
245
      -- result and status --
246
      res_o   => cp_result(0),    -- operation result
247
      valid_o => cp_valid(0)      -- data output valid
248
    );
249 2 zero_gravi
 
250
 
251 61 zero_gravi
  -- Co-Processor 1: Integer Multiplication/Division ('M' Extension) ------------------------
252
  -- -------------------------------------------------------------------------------------------
253
  neorv32_cpu_cp_muldiv_inst_true:
254
  if (CPU_EXTENSION_RISCV_M = true) or (CPU_EXTENSION_RISCV_Zmmul = true) generate
255
    neorv32_cpu_cp_muldiv_inst: neorv32_cpu_cp_muldiv
256
    generic map (
257
      FAST_MUL_EN => FAST_MUL_EN,          -- use DSPs for faster multiplication
258
      DIVISION_EN => CPU_EXTENSION_RISCV_M -- implement divider hardware
259
    )
260
    port map (
261
      -- global control --
262
      clk_i   => clk_i,           -- global clock, rising edge
263
      rstn_i  => rstn_i,          -- global reset, low-active, async
264
      ctrl_i  => ctrl_i,          -- main control bus
265
      start_i => cp_start(1),     -- trigger operation
266
      -- data input --
267
      rs1_i   => rs1_i,           -- rf source 1
268
      rs2_i   => rs2_i,           -- rf source 2
269
      -- result and status --
270
      res_o   => cp_result(1),    -- operation result
271
      valid_o => cp_valid(1)      -- data output valid
272
    );
273
  end generate;
274
 
275
  neorv32_cpu_cp_muldiv_inst_false:
276
  if (CPU_EXTENSION_RISCV_M = false) and (CPU_EXTENSION_RISCV_Zmmul = false) generate
277
    cp_result(1) <= (others => '0');
278
    cp_valid(1)  <= cp_start(1); -- to make sure CPU does not get stalled if there is an accidental access
279
  end generate;
280
 
281
 
282 63 zero_gravi
  -- Co-Processor 2: Bit-Manipulation Unit ('Zbb' Extension) --------------------------------
283 61 zero_gravi
  -- -------------------------------------------------------------------------------------------
284 63 zero_gravi
  neorv32_cpu_cp_bitmanip_inst_true:
285
  if (CPU_EXTENSION_RISCV_Zbb = true) generate
286
    neorv32_cpu_cp_bitmanip_inst: neorv32_cpu_cp_bitmanip
287
    generic map (
288
      FAST_SHIFT_EN => FAST_SHIFT_EN -- use barrel shifter for shift operations
289
    )
290
    port map (
291
      -- global control --
292
      clk_i    => clk_i,        -- global clock, rising edge
293
      rstn_i   => rstn_i,       -- global reset, low-active, async
294
      ctrl_i   => ctrl_i,       -- main control bus
295
      start_i  => cp_start(2),  -- trigger operation
296
      -- data input --
297
      cmp_i    => cmp_i,        -- comparator status
298
      rs1_i    => rs1_i,        -- rf source 1
299
      rs2_i    => rs2_i,        -- rf source 2
300
      -- result and status --
301
      res_o    => cp_result(2), -- operation result
302
      valid_o  => cp_valid(2)   -- data output valid
303
    );
304
  end generate;
305 61 zero_gravi
 
306 63 zero_gravi
  neorv32_cpu_cp_bitmanip_inst_false:
307
  if (CPU_EXTENSION_RISCV_Zbb = false) generate
308
    cp_result(2) <= (others => '0');
309
    cp_valid(2)  <= cp_start(2); -- to make sure CPU does not get stalled if there is an accidental access
310
  end generate;
311 61 zero_gravi
 
312 63 zero_gravi
 
313 61 zero_gravi
  -- Co-Processor 3: Single-Precision Floating-Point Unit ('Zfinx' Extension) ---------------
314
  -- -------------------------------------------------------------------------------------------
315
  neorv32_cpu_cp_fpu_inst_true:
316
  if (CPU_EXTENSION_RISCV_Zfinx = true) generate
317
    neorv32_cpu_cp_fpu_inst: neorv32_cpu_cp_fpu
318
    port map (
319
      -- global control --
320
      clk_i    => clk_i,        -- global clock, rising edge
321
      rstn_i   => rstn_i,       -- global reset, low-active, async
322
      ctrl_i   => ctrl_i,       -- main control bus
323
      start_i  => cp_start(3),  -- trigger operation
324
      -- data input --
325
      cmp_i    => cmp_i,        -- comparator status
326
      rs1_i    => rs1_i,        -- rf source 1
327
      rs2_i    => rs2_i,        -- rf source 2
328
      -- result and status --
329
      res_o    => cp_result(3), -- operation result
330
      fflags_o => fpu_flags_o,  -- exception flags
331
      valid_o  => cp_valid(3)   -- data output valid
332
    );
333
  end generate;
334
 
335
  neorv32_cpu_cp_fpu_inst_false:
336
  if (CPU_EXTENSION_RISCV_Zfinx = false) generate
337
    cp_result(3) <= (others => '0');
338
    fpu_flags_o  <= (others => '0');
339
    cp_valid(3)  <= cp_start(3); -- to make sure CPU does not get stalled if there is an accidental access
340
  end generate;
341
 
342
 
343 2 zero_gravi
end neorv32_cpu_cpu_rtl;

powered by: WebSVN 2.1.0

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