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

Subversion Repositories neorv32

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

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 20 zero_gravi
-- # Main data and address ALU. Includes comparator unit and co-processor interface/arbiter.       #
5
-- # The shifter sub-unit uses an iterative approach.                                              #
6 2 zero_gravi
-- # ********************************************************************************************* #
7
-- # BSD 3-Clause License                                                                          #
8
-- #                                                                                               #
9
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
10
-- #                                                                                               #
11
-- # Redistribution and use in source and binary forms, with or without modification, are          #
12
-- # permitted provided that the following conditions are met:                                     #
13
-- #                                                                                               #
14
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
15
-- #    conditions and the following disclaimer.                                                   #
16
-- #                                                                                               #
17
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
18
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
19
-- #    provided with the distribution.                                                            #
20
-- #                                                                                               #
21
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
22
-- #    endorse or promote products derived from this software without specific prior written      #
23
-- #    permission.                                                                                #
24
-- #                                                                                               #
25
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
26
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
27
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
28
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
29
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
30
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
31
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
32
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
33
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
34
-- # ********************************************************************************************* #
35
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
36
-- #################################################################################################
37
 
38
library ieee;
39
use ieee.std_logic_1164.all;
40
use ieee.numeric_std.all;
41
 
42
library neorv32;
43
use neorv32.neorv32_package.all;
44
 
45
entity neorv32_cpu_alu is
46 11 zero_gravi
  generic (
47 34 zero_gravi
    CPU_EXTENSION_RISCV_M : boolean := true; -- implement muld/div extension?
48
    FAST_SHIFT_EN         : boolean := false -- use barrel shifter for shift operations
49 11 zero_gravi
  );
50 2 zero_gravi
  port (
51
    -- global control --
52
    clk_i       : in  std_ulogic; -- global clock, rising edge
53
    rstn_i      : in  std_ulogic; -- global reset, low-active, async
54
    ctrl_i      : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
55
    -- data input --
56
    rs1_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
57
    rs2_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
58
    pc2_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- delayed PC
59
    imm_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- immediate
60
    -- data output --
61
    cmp_o       : out std_ulogic_vector(1 downto 0); -- comparator status
62
    res_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
63
    -- co-processor interface --
64 19 zero_gravi
    cp0_start_o : out std_ulogic; -- trigger co-processor 0
65 2 zero_gravi
    cp0_data_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- co-processor 0 result
66
    cp0_valid_i : in  std_ulogic; -- co-processor 0 result valid
67 19 zero_gravi
    cp1_start_o : out std_ulogic; -- trigger co-processor 1
68 2 zero_gravi
    cp1_data_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- co-processor 1 result
69
    cp1_valid_i : in  std_ulogic; -- co-processor 1 result valid
70
    -- status --
71
    wait_o      : out std_ulogic -- busy due to iterative processing units
72
  );
73
end neorv32_cpu_alu;
74
 
75
architecture neorv32_cpu_cpu_rtl of neorv32_cpu_alu is
76
 
77
  -- operands --
78 29 zero_gravi
  signal opa, opb : std_ulogic_vector(data_width_c-1 downto 0);
79 2 zero_gravi
 
80
  -- results --
81 29 zero_gravi
  signal addsub_res : std_ulogic_vector(data_width_c-1 downto 0);
82
  signal cp_res     : std_ulogic_vector(data_width_c-1 downto 0);
83 2 zero_gravi
 
84
  -- comparator --
85
  signal cmp_opx   : std_ulogic_vector(data_width_c downto 0);
86
  signal cmp_opy   : std_ulogic_vector(data_width_c downto 0);
87
  signal cmp_sub   : std_ulogic_vector(data_width_c downto 0);
88
  signal cmp_less  : std_ulogic;
89
 
90
  -- shifter --
91 12 zero_gravi
  type shifter_t is record
92 34 zero_gravi
    cmd     : std_ulogic;
93
    cmd_ff  : std_ulogic;
94
    start   : std_ulogic;
95
    run     : std_ulogic;
96
    halt    : std_ulogic;
97
    cnt     : std_ulogic_vector(4 downto 0);
98
    sreg    : std_ulogic_vector(data_width_c-1 downto 0);
99
    -- for barrel shifter only --
100
    bs_a_in : std_ulogic_vector(4 downto 0);
101
    bs_d_in : std_ulogic_vector(data_width_c-1 downto 0);
102 12 zero_gravi
  end record;
103
  signal shifter : shifter_t;
104 2 zero_gravi
 
105 19 zero_gravi
  -- co-processor arbiter and interface --
106
  type cp_ctrl_t is record
107 29 zero_gravi
    cmd    : std_ulogic;
108 19 zero_gravi
    cmd_ff : std_ulogic;
109
    busy   : std_ulogic;
110
    start  : std_ulogic;
111
    halt   : std_ulogic;
112
  end record;
113
  signal cp_ctrl : cp_ctrl_t;
114 2 zero_gravi
 
115
begin
116
 
117
  -- Operand Mux ----------------------------------------------------------------------------
118
  -- -------------------------------------------------------------------------------------------
119 29 zero_gravi
  opa <= pc2_i when (ctrl_i(ctrl_alu_opa_mux_c) = '1') else rs1_i; -- operand a (first ALU input operand)
120
  opb <= imm_i when (ctrl_i(ctrl_alu_opb_mux_c) = '1') else rs2_i; -- operand b (second ALU input operand)
121 2 zero_gravi
 
122
 
123
  -- Comparator Unit ------------------------------------------------------------------------
124
  -- -------------------------------------------------------------------------------------------
125
  cmp_opx  <= (rs1_i(rs1_i'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & rs1_i;
126 29 zero_gravi
  cmp_opy  <= (rs2_i(rs2_i'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & rs2_i;
127
  cmp_sub  <= std_ulogic_vector(signed(cmp_opx) - signed(cmp_opy)); -- less than (x < y)
128 2 zero_gravi
 
129 29 zero_gravi
  cmp_o(alu_cmp_equal_c) <= '1' when (rs1_i = rs2_i) else '0';
130
  cmp_o(alu_cmp_less_c)  <= cmp_sub(cmp_sub'left); -- less = carry (borrow)
131 2 zero_gravi
 
132
 
133 29 zero_gravi
  -- Binary Adder/Subtractor ----------------------------------------------------------------
134 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
135 29 zero_gravi
  binary_arithmetic_core: process(ctrl_i, opa, opb)
136
    variable cin_v  : std_ulogic_vector(0 downto 0);
137
    variable op_a_v : std_ulogic_vector(data_width_c downto 0);
138
    variable op_b_v : std_ulogic_vector(data_width_c downto 0);
139
    variable op_y_v : std_ulogic_vector(data_width_c downto 0);
140
    variable res_v  : std_ulogic_vector(data_width_c downto 0);
141
  begin
142
    -- operand sign-extension --
143
    op_a_v := (opa(opa'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & opa;
144
    op_b_v := (opb(opb'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & opb;
145 2 zero_gravi
 
146 29 zero_gravi
    -- add/sub(slt) select --
147
    if (ctrl_i(ctrl_alu_addsub_c) = '1') then -- subtraction
148
      op_y_v   := not op_b_v;
149
      cin_v(0) := '1';
150
    else-- addition
151
      op_y_v   := op_b_v;
152
      cin_v(0) := '0';
153
    end if;
154 2 zero_gravi
 
155 29 zero_gravi
    -- adder core --
156
    res_v := std_ulogic_vector(unsigned(op_a_v) + unsigned(op_y_v) + unsigned(cin_v(0 downto 0)));
157
 
158
    -- output --
159
    cmp_less    <= res_v(32);
160
    addsub_res  <= res_v(31 downto 0);
161
    addsub_res  <= res_v(31 downto 0);
162
  end process binary_arithmetic_core;
163
 
164
 
165 34 zero_gravi
  -- Shifter Unit ---------------------------------------------------------------------------
166 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
167
  shifter_unit: process(rstn_i, clk_i)
168 34 zero_gravi
    variable bs_input_v   : std_ulogic_vector(data_width_c-1 downto 0);
169
    variable bs_level_4_v : std_ulogic_vector(data_width_c-1 downto 0);
170
    variable bs_level_3_v : std_ulogic_vector(data_width_c-1 downto 0);
171
    variable bs_level_2_v : std_ulogic_vector(data_width_c-1 downto 0);
172
    variable bs_level_1_v : std_ulogic_vector(data_width_c-1 downto 0);
173
    variable bs_level_0_v : std_ulogic_vector(data_width_c-1 downto 0);
174 2 zero_gravi
  begin
175
    if (rstn_i = '0') then
176 12 zero_gravi
      shifter.sreg   <= (others => '0');
177
      shifter.cnt    <= (others => '0');
178
      shifter.cmd_ff <= '0';
179 34 zero_gravi
      if (FAST_SHIFT_EN = true) then
180
        shifter.bs_d_in <= (others => '0');
181
        shifter.bs_a_in <= (others => '0');
182
      end if;
183 2 zero_gravi
    elsif rising_edge(clk_i) then
184 12 zero_gravi
      shifter.cmd_ff <= shifter.cmd;
185 34 zero_gravi
 
186
      -- --------------------------------------------------------------------------------
187
      -- Iterative shifter (small but slow) (default)
188
      -- --------------------------------------------------------------------------------
189
      if (FAST_SHIFT_EN = false) then
190
 
191
        if (shifter.start = '1') then -- trigger new shift
192
          shifter.sreg <= opa; -- shift operand
193
          shifter.cnt  <= opb(index_size_f(data_width_c)-1 downto 0); -- shift amount
194
        elsif (shifter.run = '1') then -- running shift
195
          -- coarse shift: multiples of 4 --
196
          if (or_all_f(shifter.cnt(shifter.cnt'left downto 2)) = '1') then -- shift amount >= 4
197
            shifter.cnt <= std_ulogic_vector(unsigned(shifter.cnt) - 4);
198
            if (ctrl_i(ctrl_alu_shift_dir_c) = '0') then -- SLL: shift left logical
199
              shifter.sreg <= shifter.sreg(shifter.sreg'left-4 downto 0) & "0000";
200
            else -- SRL: shift right logical / SRA: shift right arithmetical
201
              shifter.sreg <= (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) &
202
                              (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) &
203
                              (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) &
204
                              (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) & shifter.sreg(shifter.sreg'left downto 4);
205
            end if;
206
          -- fine shift: single shifts, 0..3 times --
207
          else
208
            shifter.cnt <= std_ulogic_vector(unsigned(shifter.cnt) - 1);
209
            if (ctrl_i(ctrl_alu_shift_dir_c) = '0') then -- SLL: shift left logical
210
              shifter.sreg <= shifter.sreg(shifter.sreg'left-1 downto 0) & '0';
211
            else -- SRL: shift right logical / SRA: shift right arithmetical
212
              shifter.sreg <= (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) & shifter.sreg(shifter.sreg'left downto 1);
213
            end if;
214 12 zero_gravi
          end if;
215 34 zero_gravi
        end if;
216
 
217
      -- --------------------------------------------------------------------------------
218
      -- Barrel shifter (huge but fast)
219
      -- --------------------------------------------------------------------------------
220
      else
221
 
222
        -- operands and cycle control --
223
        if (shifter.start = '1') then -- trigger new shift
224
          shifter.bs_d_in <= opa; -- shift data
225
          shifter.bs_a_in <= opb(index_size_f(data_width_c)-1 downto 0); -- shift amount
226
          shifter.cnt     <= (others => '0');
227
        end if;
228
 
229
        -- convert left shifts to right shifts --
230
        if (ctrl_i(ctrl_alu_shift_dir_c) = '0') then -- is left shift?
231
          bs_input_v := bit_rev_f(shifter.bs_d_in); -- reverse bit order of input operand
232 12 zero_gravi
        else
233 34 zero_gravi
          bs_input_v := shifter.bs_d_in;
234 2 zero_gravi
        end if;
235 34 zero_gravi
        -- shift >> 16 --
236
        if (shifter.bs_a_in(4) = '1') then
237
          bs_level_4_v(31 downto 16) := (others => (bs_input_v(bs_input_v'left) and ctrl_i(ctrl_alu_shift_ar_c)));
238
          bs_level_4_v(15 downto 00) := (bs_input_v(31 downto 16));
239
        else
240
          bs_level_4_v := bs_input_v;
241
        end if;
242
        -- shift >> 8 --
243
        if (shifter.bs_a_in(3) = '1') then
244
          bs_level_3_v(31 downto 24) := (others => (bs_input_v(bs_input_v'left) and ctrl_i(ctrl_alu_shift_ar_c)));
245
          bs_level_3_v(23 downto 00) := (bs_level_4_v(31 downto 8));
246
        else
247
          bs_level_3_v := bs_level_4_v;
248
        end if;
249
        -- shift >> 4 --
250
        if (shifter.bs_a_in(2) = '1') then
251
          bs_level_2_v(31 downto 28) := (others => (bs_input_v(bs_input_v'left) and ctrl_i(ctrl_alu_shift_ar_c)));
252
          bs_level_2_v(27 downto 00) := (bs_level_3_v(31 downto 4));
253
        else
254
          bs_level_2_v := bs_level_3_v;
255
        end if;
256
        -- shift >> 2 --
257
        if (shifter.bs_a_in(1) = '1') then
258
          bs_level_1_v(31 downto 30) := (others => (bs_input_v(bs_input_v'left) and ctrl_i(ctrl_alu_shift_ar_c)));
259
          bs_level_1_v(29 downto 00) := (bs_level_2_v(31 downto 2));
260
        else
261
          bs_level_1_v := bs_level_2_v;
262
        end if;
263
        -- shift >> 1 --
264
        if (shifter.bs_a_in(0) = '1') then
265
          bs_level_0_v(31 downto 31) := (others => (bs_input_v(bs_input_v'left) and ctrl_i(ctrl_alu_shift_ar_c)));
266
          bs_level_0_v(30 downto 00) := (bs_level_1_v(31 downto 1));
267
        else
268
          bs_level_0_v := bs_level_1_v;
269
        end if;
270
        -- re-convert original left shifts --
271
        if (ctrl_i(ctrl_alu_shift_dir_c) = '0') then
272
          shifter.sreg <= bit_rev_f(bs_level_0_v);
273
        else
274
          shifter.sreg <= bs_level_0_v;
275
        end if;
276 2 zero_gravi
      end if;
277
    end if;
278
  end process shifter_unit;
279
 
280
  -- is shift operation? --
281 29 zero_gravi
  shifter.cmd   <= '1' when (ctrl_i(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) = alu_cmd_shift_c) else '0';
282 12 zero_gravi
  shifter.start <= '1' when (shifter.cmd = '1') and (shifter.cmd_ff = '0') else '0';
283 2 zero_gravi
 
284
  -- shift operation running? --
285 19 zero_gravi
  shifter.run  <= '1' when (or_all_f(shifter.cnt) = '1') or (shifter.start = '1') else '0';
286
  shifter.halt <= '1' when (or_all_f(shifter.cnt(shifter.cnt'left downto 1)) = '1') or (shifter.start = '1') else '0';
287 2 zero_gravi
 
288
 
289 19 zero_gravi
  -- Coprocessor Arbiter --------------------------------------------------------------------
290 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
291 19 zero_gravi
  cp_arbiter: process(rstn_i, clk_i)
292 2 zero_gravi
  begin
293
    if (rstn_i = '0') then
294 19 zero_gravi
      cp_ctrl.cmd_ff <= '0';
295
      cp_ctrl.busy   <= '0';
296 2 zero_gravi
    elsif rising_edge(clk_i) then
297 19 zero_gravi
      if (CPU_EXTENSION_RISCV_M = true) then
298 29 zero_gravi
        cp_ctrl.cmd_ff <= cp_ctrl.cmd;
299 19 zero_gravi
        if (cp_ctrl.start = '1') then
300
          cp_ctrl.busy <= '1';
301
        elsif ((cp0_valid_i or cp1_valid_i) = '1') then -- cp computation done?
302 24 zero_gravi
          cp_ctrl.busy <= '0';
303 2 zero_gravi
        end if;
304 23 zero_gravi
      else -- no co-processor(s) implemented
305 19 zero_gravi
        cp_ctrl.cmd_ff <= '0';
306
        cp_ctrl.busy   <= '0';
307 2 zero_gravi
      end if;
308
    end if;
309 19 zero_gravi
  end process cp_arbiter;
310 2 zero_gravi
 
311
  -- is co-processor operation? --
312 29 zero_gravi
  cp_ctrl.cmd   <= '1' when (ctrl_i(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) = alu_cmd_cp_c) else '0';
313
  cp_ctrl.start <= '1' when (cp_ctrl.cmd = '1') and (cp_ctrl.cmd_ff = '0') else '0';
314 23 zero_gravi
  cp0_start_o   <= '1' when (cp_ctrl.start = '1') and (ctrl_i(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) = cp_sel_muldiv_c) else '0'; -- MULDIV CP
315
  cp1_start_o   <= '0'; -- not yet implemented
316 2 zero_gravi
 
317
  -- co-processor operation running? --
318 19 zero_gravi
  cp_ctrl.halt <= cp_ctrl.busy or cp_ctrl.start;
319 2 zero_gravi
 
320 24 zero_gravi
  -- co-processor result --
321 29 zero_gravi
  cp_res <= cp0_data_i or cp1_data_i; -- only the **actaully selected** co-processor should output data != 0
322 24 zero_gravi
 
323
 
324 2 zero_gravi
  -- ALU Function Select --------------------------------------------------------------------
325
  -- -------------------------------------------------------------------------------------------
326 29 zero_gravi
  alu_function_mux: process(ctrl_i, opa, opb, addsub_res, cp_res, cmp_less, shifter.sreg)
327 2 zero_gravi
  begin
328
    case ctrl_i(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) is
329 29 zero_gravi
      when alu_cmd_xor_c    => res_o <= opa xor opb;
330
      when alu_cmd_or_c     => res_o <= opa or  opb;
331
      when alu_cmd_and_c    => res_o <= opa and opb;
332
      when alu_cmd_movb_c   => res_o <= opb;
333
      when alu_cmd_addsub_c => res_o <= addsub_res;
334
      when alu_cmd_cp_c     => res_o <= cp_res;
335
      when alu_cmd_shift_c  => res_o <= shifter.sreg;
336
      when alu_cmd_slt_c    => res_o <= (others => '0'); res_o(0) <= cmp_less;
337
      when others           => res_o <= opb; -- 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.