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

Subversion Repositories neorv32

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