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

Subversion Repositories neorv32

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

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 34 zero_gravi
    CPU_EXTENSION_RISCV_M : boolean := true; -- implement muld/div extension?
47
    FAST_SHIFT_EN         : boolean := false -- use barrel shifter for shift operations
48 11 zero_gravi
  );
49 2 zero_gravi
  port (
50
    -- global control --
51
    clk_i       : in  std_ulogic; -- global clock, rising edge
52
    rstn_i      : in  std_ulogic; -- global reset, low-active, async
53
    ctrl_i      : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
54
    -- data input --
55
    rs1_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
56
    rs2_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
57
    pc2_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- delayed PC
58
    imm_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- immediate
59
    -- data output --
60
    res_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
61 36 zero_gravi
    add_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- address computation result
62 2 zero_gravi
    -- co-processor interface --
63 19 zero_gravi
    cp0_start_o : out std_ulogic; -- trigger co-processor 0
64 2 zero_gravi
    cp0_data_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- co-processor 0 result
65
    cp0_valid_i : in  std_ulogic; -- co-processor 0 result valid
66 19 zero_gravi
    cp1_start_o : out std_ulogic; -- trigger co-processor 1
67 2 zero_gravi
    cp1_data_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- co-processor 1 result
68
    cp1_valid_i : in  std_ulogic; -- co-processor 1 result valid
69 36 zero_gravi
    cp2_start_o : out std_ulogic; -- trigger co-processor 2
70
    cp2_data_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- co-processor 2 result
71
    cp2_valid_i : in  std_ulogic; -- co-processor 2 result valid
72
    cp3_start_o : out std_ulogic; -- trigger co-processor 3
73
    cp3_data_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- co-processor 3 result
74
    cp3_valid_i : in  std_ulogic; -- co-processor 3 result valid
75 2 zero_gravi
    -- status --
76
    wait_o      : out std_ulogic -- busy due to iterative processing units
77
  );
78
end neorv32_cpu_alu;
79
 
80
architecture neorv32_cpu_cpu_rtl of neorv32_cpu_alu is
81
 
82
  -- operands --
83 29 zero_gravi
  signal opa, opb : std_ulogic_vector(data_width_c-1 downto 0);
84 2 zero_gravi
 
85
  -- results --
86 36 zero_gravi
  signal addsub_res : std_ulogic_vector(data_width_c downto 0);
87 39 zero_gravi
  --
88 29 zero_gravi
  signal cp_res     : std_ulogic_vector(data_width_c-1 downto 0);
89 39 zero_gravi
  signal arith_res  : std_ulogic_vector(data_width_c-1 downto 0);
90
  signal logic_res  : std_ulogic_vector(data_width_c-1 downto 0);
91 2 zero_gravi
 
92
  -- shifter --
93 12 zero_gravi
  type shifter_t is record
94 34 zero_gravi
    cmd     : std_ulogic;
95
    cmd_ff  : std_ulogic;
96
    start   : std_ulogic;
97
    run     : std_ulogic;
98
    halt    : std_ulogic;
99
    cnt     : std_ulogic_vector(4 downto 0);
100
    sreg    : std_ulogic_vector(data_width_c-1 downto 0);
101
    -- for barrel shifter only --
102
    bs_a_in : std_ulogic_vector(4 downto 0);
103
    bs_d_in : std_ulogic_vector(data_width_c-1 downto 0);
104 12 zero_gravi
  end record;
105
  signal shifter : shifter_t;
106 2 zero_gravi
 
107 19 zero_gravi
  -- co-processor arbiter and interface --
108
  type cp_ctrl_t is record
109 29 zero_gravi
    cmd    : std_ulogic;
110 19 zero_gravi
    cmd_ff : std_ulogic;
111
    busy   : std_ulogic;
112
    start  : std_ulogic;
113
    halt   : std_ulogic;
114
  end record;
115
  signal cp_ctrl : cp_ctrl_t;
116 2 zero_gravi
 
117
begin
118
 
119
  -- Operand Mux ----------------------------------------------------------------------------
120
  -- -------------------------------------------------------------------------------------------
121 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
122 29 zero_gravi
  opb <= imm_i when (ctrl_i(ctrl_alu_opb_mux_c) = '1') else rs2_i; -- operand b (second ALU input operand)
123 2 zero_gravi
 
124
 
125 29 zero_gravi
  -- Binary Adder/Subtractor ----------------------------------------------------------------
126 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
127 29 zero_gravi
  binary_arithmetic_core: process(ctrl_i, opa, opb)
128
    variable cin_v  : std_ulogic_vector(0 downto 0);
129
    variable op_a_v : std_ulogic_vector(data_width_c downto 0);
130
    variable op_b_v : std_ulogic_vector(data_width_c downto 0);
131
    variable op_y_v : std_ulogic_vector(data_width_c downto 0);
132
    variable res_v  : std_ulogic_vector(data_width_c downto 0);
133
  begin
134
    -- operand sign-extension --
135
    op_a_v := (opa(opa'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & opa;
136
    op_b_v := (opb(opb'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & opb;
137
    -- add/sub(slt) select --
138
    if (ctrl_i(ctrl_alu_addsub_c) = '1') then -- subtraction
139
      op_y_v   := not op_b_v;
140
      cin_v(0) := '1';
141 36 zero_gravi
    else -- addition
142 29 zero_gravi
      op_y_v   := op_b_v;
143
      cin_v(0) := '0';
144
    end if;
145 36 zero_gravi
    -- adder core (result + carry/borrow) --
146
    addsub_res <= std_ulogic_vector(unsigned(op_a_v) + unsigned(op_y_v) + unsigned(cin_v(0 downto 0)));
147 29 zero_gravi
  end process binary_arithmetic_core;
148
 
149 36 zero_gravi
  -- direct output of address result --
150
  add_o <= addsub_res(data_width_c-1 downto 0);
151 29 zero_gravi
 
152 39 zero_gravi
  -- ALU arithmetic logic core --
153
  arithmetic_core: process(ctrl_i, addsub_res)
154
  begin
155
    if (ctrl_i(ctrl_alu_arith_c) = alu_arith_cmd_addsub_c) then -- ADD/SUB
156
      arith_res <= addsub_res(data_width_c-1 downto 0);
157
    else -- SLT
158
      arith_res <= (others => '0');
159
      arith_res(0) <= addsub_res(addsub_res'left); -- => carry/borrow
160
    end if;
161
  end process arithmetic_core;
162 36 zero_gravi
 
163 39 zero_gravi
 
164 34 zero_gravi
  -- Shifter Unit ---------------------------------------------------------------------------
165 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
166 36 zero_gravi
  shifter_unit: process(clk_i)
167 34 zero_gravi
    variable bs_input_v   : std_ulogic_vector(data_width_c-1 downto 0);
168
    variable bs_level_4_v : std_ulogic_vector(data_width_c-1 downto 0);
169
    variable bs_level_3_v : std_ulogic_vector(data_width_c-1 downto 0);
170
    variable bs_level_2_v : std_ulogic_vector(data_width_c-1 downto 0);
171
    variable bs_level_1_v : std_ulogic_vector(data_width_c-1 downto 0);
172
    variable bs_level_0_v : std_ulogic_vector(data_width_c-1 downto 0);
173 2 zero_gravi
  begin
174 36 zero_gravi
    if rising_edge(clk_i) then
175 12 zero_gravi
      shifter.cmd_ff <= shifter.cmd;
176 34 zero_gravi
 
177
      -- --------------------------------------------------------------------------------
178
      -- Iterative shifter (small but slow) (default)
179
      -- --------------------------------------------------------------------------------
180
      if (FAST_SHIFT_EN = false) then
181
 
182
        if (shifter.start = '1') then -- trigger new shift
183 36 zero_gravi
          shifter.sreg <= rs1_i; -- shift operand (can only be rs1; opa would also contain pc)
184 34 zero_gravi
          shifter.cnt  <= opb(index_size_f(data_width_c)-1 downto 0); -- shift amount
185
        elsif (shifter.run = '1') then -- running shift
186
          -- coarse shift: multiples of 4 --
187
          if (or_all_f(shifter.cnt(shifter.cnt'left downto 2)) = '1') then -- shift amount >= 4
188
            shifter.cnt <= std_ulogic_vector(unsigned(shifter.cnt) - 4);
189
            if (ctrl_i(ctrl_alu_shift_dir_c) = '0') then -- SLL: shift left logical
190
              shifter.sreg <= shifter.sreg(shifter.sreg'left-4 downto 0) & "0000";
191
            else -- SRL: shift right logical / SRA: shift right arithmetical
192
              shifter.sreg <= (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) &
193
                              (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) &
194
                              (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) &
195
                              (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) & shifter.sreg(shifter.sreg'left downto 4);
196
            end if;
197
          -- fine shift: single shifts, 0..3 times --
198
          else
199
            shifter.cnt <= std_ulogic_vector(unsigned(shifter.cnt) - 1);
200
            if (ctrl_i(ctrl_alu_shift_dir_c) = '0') then -- SLL: shift left logical
201
              shifter.sreg <= shifter.sreg(shifter.sreg'left-1 downto 0) & '0';
202
            else -- SRL: shift right logical / SRA: shift right arithmetical
203
              shifter.sreg <= (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) & shifter.sreg(shifter.sreg'left downto 1);
204
            end if;
205 12 zero_gravi
          end if;
206 34 zero_gravi
        end if;
207
 
208
      -- --------------------------------------------------------------------------------
209
      -- Barrel shifter (huge but fast)
210
      -- --------------------------------------------------------------------------------
211
      else
212
        -- operands and cycle control --
213
        if (shifter.start = '1') then -- trigger new shift
214 36 zero_gravi
          shifter.bs_d_in <= rs1_i; -- shift operand (can only be rs1; opa would also contain pc)
215 34 zero_gravi
          shifter.bs_a_in <= opb(index_size_f(data_width_c)-1 downto 0); -- shift amount
216
          shifter.cnt     <= (others => '0');
217
        end if;
218
 
219
        -- convert left shifts to right shifts --
220
        if (ctrl_i(ctrl_alu_shift_dir_c) = '0') then -- is left shift?
221
          bs_input_v := bit_rev_f(shifter.bs_d_in); -- reverse bit order of input operand
222 12 zero_gravi
        else
223 34 zero_gravi
          bs_input_v := shifter.bs_d_in;
224 2 zero_gravi
        end if;
225 34 zero_gravi
        -- shift >> 16 --
226
        if (shifter.bs_a_in(4) = '1') then
227
          bs_level_4_v(31 downto 16) := (others => (bs_input_v(bs_input_v'left) and ctrl_i(ctrl_alu_shift_ar_c)));
228
          bs_level_4_v(15 downto 00) := (bs_input_v(31 downto 16));
229
        else
230
          bs_level_4_v := bs_input_v;
231
        end if;
232
        -- shift >> 8 --
233
        if (shifter.bs_a_in(3) = '1') then
234
          bs_level_3_v(31 downto 24) := (others => (bs_input_v(bs_input_v'left) and ctrl_i(ctrl_alu_shift_ar_c)));
235
          bs_level_3_v(23 downto 00) := (bs_level_4_v(31 downto 8));
236
        else
237
          bs_level_3_v := bs_level_4_v;
238
        end if;
239
        -- shift >> 4 --
240
        if (shifter.bs_a_in(2) = '1') then
241
          bs_level_2_v(31 downto 28) := (others => (bs_input_v(bs_input_v'left) and ctrl_i(ctrl_alu_shift_ar_c)));
242
          bs_level_2_v(27 downto 00) := (bs_level_3_v(31 downto 4));
243
        else
244
          bs_level_2_v := bs_level_3_v;
245
        end if;
246
        -- shift >> 2 --
247
        if (shifter.bs_a_in(1) = '1') then
248
          bs_level_1_v(31 downto 30) := (others => (bs_input_v(bs_input_v'left) and ctrl_i(ctrl_alu_shift_ar_c)));
249
          bs_level_1_v(29 downto 00) := (bs_level_2_v(31 downto 2));
250
        else
251
          bs_level_1_v := bs_level_2_v;
252
        end if;
253
        -- shift >> 1 --
254
        if (shifter.bs_a_in(0) = '1') then
255
          bs_level_0_v(31 downto 31) := (others => (bs_input_v(bs_input_v'left) and ctrl_i(ctrl_alu_shift_ar_c)));
256
          bs_level_0_v(30 downto 00) := (bs_level_1_v(31 downto 1));
257
        else
258
          bs_level_0_v := bs_level_1_v;
259
        end if;
260
        -- re-convert original left shifts --
261
        if (ctrl_i(ctrl_alu_shift_dir_c) = '0') then
262
          shifter.sreg <= bit_rev_f(bs_level_0_v);
263
        else
264
          shifter.sreg <= bs_level_0_v;
265
        end if;
266 2 zero_gravi
      end if;
267
    end if;
268
  end process shifter_unit;
269
 
270
  -- is shift operation? --
271 39 zero_gravi
  shifter.cmd   <= '1' when (ctrl_i(ctrl_alu_func1_c downto ctrl_alu_func0_c) = alu_func_cmd_shift_c) else '0';
272 12 zero_gravi
  shifter.start <= '1' when (shifter.cmd = '1') and (shifter.cmd_ff = '0') else '0';
273 2 zero_gravi
 
274
  -- shift operation running? --
275 19 zero_gravi
  shifter.run  <= '1' when (or_all_f(shifter.cnt) = '1') or (shifter.start = '1') else '0';
276
  shifter.halt <= '1' when (or_all_f(shifter.cnt(shifter.cnt'left downto 1)) = '1') or (shifter.start = '1') else '0';
277 2 zero_gravi
 
278
 
279 47 zero_gravi
  -- Co-Processor Arbiter -------------------------------------------------------------------
280 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
281 47 zero_gravi
  -- Interface:
282
  -- Co-processor "valid" signal has to be asserted (for one cycle) one cycle before asserting output data
283
  -- Co-processor "output data" has to be always zero unless co-processor was explicitly triggered
284 19 zero_gravi
  cp_arbiter: process(rstn_i, clk_i)
285 2 zero_gravi
  begin
286
    if (rstn_i = '0') then
287 19 zero_gravi
      cp_ctrl.cmd_ff <= '0';
288
      cp_ctrl.busy   <= '0';
289 2 zero_gravi
    elsif rising_edge(clk_i) then
290 40 zero_gravi
      cp_ctrl.cmd_ff <= cp_ctrl.cmd;
291
      if ((cp0_valid_i or cp1_valid_i or cp2_valid_i or cp3_valid_i) = '1') then -- cp computation done?
292
        cp_ctrl.busy <= '0';
293
      elsif (cp_ctrl.start = '1') then
294
        cp_ctrl.busy <= '1';
295 2 zero_gravi
      end if;
296
    end if;
297 19 zero_gravi
  end process cp_arbiter;
298 2 zero_gravi
 
299
  -- is co-processor operation? --
300 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';
301 29 zero_gravi
  cp_ctrl.start <= '1' when (cp_ctrl.cmd = '1') and (cp_ctrl.cmd_ff = '0') else '0';
302 2 zero_gravi
 
303 39 zero_gravi
  -- co-processor select --
304
  cp0_start_o <= '1' when (cp_ctrl.start = '1') and (ctrl_i(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) = "00") else '0';
305
  cp1_start_o <= '1' when (cp_ctrl.start = '1') and (ctrl_i(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) = "01") else '0';
306
  cp2_start_o <= '1' when (cp_ctrl.start = '1') and (ctrl_i(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) = "10") else '0';
307
  cp3_start_o <= '1' when (cp_ctrl.start = '1') and (ctrl_i(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) = "11") else '0';
308 2 zero_gravi
 
309 39 zero_gravi
  -- co-processor operation (still) running? --
310
  cp_ctrl.halt <= (cp_ctrl.busy and (not (cp0_valid_i or cp1_valid_i or cp2_valid_i or cp3_valid_i))) or cp_ctrl.start;
311
 
312 24 zero_gravi
  -- co-processor result --
313 47 zero_gravi
  cp_res <= cp0_data_i or cp1_data_i or cp2_data_i or cp3_data_i; -- only the *actually selected* co-processor may output data != 0
314 24 zero_gravi
 
315
 
316 39 zero_gravi
  -- ALU Logic Core -------------------------------------------------------------------------
317
  -- -------------------------------------------------------------------------------------------
318
  alu_logic_core: process(ctrl_i, rs1_i, opb)
319
  begin
320
    case ctrl_i(ctrl_alu_logic1_c downto ctrl_alu_logic0_c) is
321
      when alu_logic_cmd_movb_c => logic_res <= opb; -- (default)
322
      when alu_logic_cmd_xor_c  => logic_res <= rs1_i xor opb; -- only rs1 required for logic ops (opa would also contain pc)
323
      when alu_logic_cmd_or_c   => logic_res <= rs1_i or  opb;
324
      when alu_logic_cmd_and_c  => logic_res <= rs1_i and opb;
325
      when others               => logic_res <= opb; -- undefined
326
    end case;
327
  end process alu_logic_core;
328
 
329
 
330 2 zero_gravi
  -- ALU Function Select --------------------------------------------------------------------
331
  -- -------------------------------------------------------------------------------------------
332 39 zero_gravi
  alu_function_mux: process(ctrl_i, arith_res, logic_res, shifter.sreg, cp_res)
333 2 zero_gravi
  begin
334 39 zero_gravi
    case ctrl_i(ctrl_alu_func1_c downto ctrl_alu_func0_c) is
335
      when alu_func_cmd_arith_c => res_o <= arith_res; -- (default)
336
      when alu_func_cmd_logic_c => res_o <= logic_res;
337
      when alu_func_cmd_shift_c => res_o <= shifter.sreg;
338
      when alu_func_cmd_copro_c => res_o <= cp_res;
339
      when others               => res_o <= arith_res; -- undefined
340 2 zero_gravi
    end case;
341
  end process alu_function_mux;
342
 
343
 
344 29 zero_gravi
  -- ALU Busy -------------------------------------------------------------------------------
345 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
346 19 zero_gravi
  wait_o <= shifter.halt or cp_ctrl.halt; -- wait until iterative units have completed
347 2 zero_gravi
 
348
 
349
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.