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

Subversion Repositories neorv32

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

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