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

Subversion Repositories neorv32

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

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