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

Subversion Repositories neorv32

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

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 2 zero_gravi
-- # ********************************************************************************************* #
6
-- # BSD 3-Clause License                                                                          #
7
-- #                                                                                               #
8
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
9
-- #                                                                                               #
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
    cmp_o       : out std_ulogic_vector(1 downto 0); -- comparator status
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
    opb_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- ALU operand B
64 2 zero_gravi
    -- co-processor interface --
65 19 zero_gravi
    cp0_start_o : out std_ulogic; -- trigger co-processor 0
66 2 zero_gravi
    cp0_data_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- co-processor 0 result
67
    cp0_valid_i : in  std_ulogic; -- co-processor 0 result valid
68 19 zero_gravi
    cp1_start_o : out std_ulogic; -- trigger co-processor 1
69 2 zero_gravi
    cp1_data_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- co-processor 1 result
70
    cp1_valid_i : in  std_ulogic; -- co-processor 1 result valid
71 36 zero_gravi
    cp2_start_o : out std_ulogic; -- trigger co-processor 2
72
    cp2_data_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- co-processor 2 result
73
    cp2_valid_i : in  std_ulogic; -- co-processor 2 result valid
74
    cp3_start_o : out std_ulogic; -- trigger co-processor 3
75
    cp3_data_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- co-processor 3 result
76
    cp3_valid_i : in  std_ulogic; -- co-processor 3 result valid
77 2 zero_gravi
    -- status --
78
    wait_o      : out std_ulogic -- busy due to iterative processing units
79
  );
80
end neorv32_cpu_alu;
81
 
82
architecture neorv32_cpu_cpu_rtl of neorv32_cpu_alu is
83
 
84
  -- operands --
85 29 zero_gravi
  signal opa, opb : std_ulogic_vector(data_width_c-1 downto 0);
86 2 zero_gravi
 
87
  -- results --
88 36 zero_gravi
  signal addsub_res : std_ulogic_vector(data_width_c downto 0);
89 29 zero_gravi
  signal cp_res     : std_ulogic_vector(data_width_c-1 downto 0);
90 2 zero_gravi
 
91
  -- comparator --
92 36 zero_gravi
  signal cmp_opx : std_ulogic_vector(data_width_c downto 0);
93
  signal cmp_opy : std_ulogic_vector(data_width_c downto 0);
94
  signal cmp_sub : std_ulogic_vector(data_width_c downto 0);
95 2 zero_gravi
 
96
  -- shifter --
97 12 zero_gravi
  type shifter_t is record
98 34 zero_gravi
    cmd     : std_ulogic;
99
    cmd_ff  : std_ulogic;
100
    start   : std_ulogic;
101
    run     : std_ulogic;
102
    halt    : std_ulogic;
103
    cnt     : std_ulogic_vector(4 downto 0);
104
    sreg    : std_ulogic_vector(data_width_c-1 downto 0);
105
    -- for barrel shifter only --
106
    bs_a_in : std_ulogic_vector(4 downto 0);
107
    bs_d_in : std_ulogic_vector(data_width_c-1 downto 0);
108 12 zero_gravi
  end record;
109
  signal shifter : shifter_t;
110 2 zero_gravi
 
111 19 zero_gravi
  -- co-processor arbiter and interface --
112
  type cp_ctrl_t is record
113 29 zero_gravi
    cmd    : std_ulogic;
114 19 zero_gravi
    cmd_ff : std_ulogic;
115
    busy   : std_ulogic;
116
    start  : std_ulogic;
117
    halt   : std_ulogic;
118
  end record;
119
  signal cp_ctrl : cp_ctrl_t;
120 2 zero_gravi
 
121
begin
122
 
123
  -- Operand Mux ----------------------------------------------------------------------------
124
  -- -------------------------------------------------------------------------------------------
125 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
126 29 zero_gravi
  opb <= imm_i when (ctrl_i(ctrl_alu_opb_mux_c) = '1') else rs2_i; -- operand b (second ALU input operand)
127 36 zero_gravi
  --
128
  opb_o <= opb;
129 2 zero_gravi
 
130
 
131
  -- Comparator Unit ------------------------------------------------------------------------
132
  -- -------------------------------------------------------------------------------------------
133
  cmp_opx  <= (rs1_i(rs1_i'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & rs1_i;
134 29 zero_gravi
  cmp_opy  <= (rs2_i(rs2_i'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & rs2_i;
135
  cmp_sub  <= std_ulogic_vector(signed(cmp_opx) - signed(cmp_opy)); -- less than (x < y)
136 2 zero_gravi
 
137 29 zero_gravi
  cmp_o(alu_cmp_equal_c) <= '1' when (rs1_i = rs2_i) else '0';
138
  cmp_o(alu_cmp_less_c)  <= cmp_sub(cmp_sub'left); -- less = carry (borrow)
139 2 zero_gravi
 
140
 
141 29 zero_gravi
  -- Binary Adder/Subtractor ----------------------------------------------------------------
142 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
143 29 zero_gravi
  binary_arithmetic_core: process(ctrl_i, opa, opb)
144
    variable cin_v  : std_ulogic_vector(0 downto 0);
145
    variable op_a_v : std_ulogic_vector(data_width_c downto 0);
146
    variable op_b_v : std_ulogic_vector(data_width_c downto 0);
147
    variable op_y_v : std_ulogic_vector(data_width_c downto 0);
148
    variable res_v  : std_ulogic_vector(data_width_c downto 0);
149
  begin
150
    -- operand sign-extension --
151
    op_a_v := (opa(opa'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & opa;
152
    op_b_v := (opb(opb'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & opb;
153 2 zero_gravi
 
154 29 zero_gravi
    -- add/sub(slt) select --
155
    if (ctrl_i(ctrl_alu_addsub_c) = '1') then -- subtraction
156
      op_y_v   := not op_b_v;
157
      cin_v(0) := '1';
158 36 zero_gravi
    else -- addition
159 29 zero_gravi
      op_y_v   := op_b_v;
160
      cin_v(0) := '0';
161
    end if;
162 2 zero_gravi
 
163 36 zero_gravi
    -- adder core (result + carry/borrow) --
164
    addsub_res <= std_ulogic_vector(unsigned(op_a_v) + unsigned(op_y_v) + unsigned(cin_v(0 downto 0)));
165 29 zero_gravi
  end process binary_arithmetic_core;
166
 
167 36 zero_gravi
  -- direct output of address result --
168
  add_o <= addsub_res(data_width_c-1 downto 0);
169 29 zero_gravi
 
170 36 zero_gravi
 
171 34 zero_gravi
  -- Shifter Unit ---------------------------------------------------------------------------
172 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
173 36 zero_gravi
  shifter_unit: process(clk_i)
174 34 zero_gravi
    variable bs_input_v   : std_ulogic_vector(data_width_c-1 downto 0);
175
    variable bs_level_4_v : std_ulogic_vector(data_width_c-1 downto 0);
176
    variable bs_level_3_v : std_ulogic_vector(data_width_c-1 downto 0);
177
    variable bs_level_2_v : std_ulogic_vector(data_width_c-1 downto 0);
178
    variable bs_level_1_v : std_ulogic_vector(data_width_c-1 downto 0);
179
    variable bs_level_0_v : std_ulogic_vector(data_width_c-1 downto 0);
180 2 zero_gravi
  begin
181 36 zero_gravi
    if rising_edge(clk_i) then
182 12 zero_gravi
      shifter.cmd_ff <= shifter.cmd;
183 34 zero_gravi
 
184
      -- --------------------------------------------------------------------------------
185
      -- Iterative shifter (small but slow) (default)
186
      -- --------------------------------------------------------------------------------
187
      if (FAST_SHIFT_EN = false) then
188
 
189
        if (shifter.start = '1') then -- trigger new shift
190 36 zero_gravi
          shifter.sreg <= rs1_i; -- shift operand (can only be rs1; opa would also contain pc)
191 34 zero_gravi
          shifter.cnt  <= opb(index_size_f(data_width_c)-1 downto 0); -- shift amount
192
        elsif (shifter.run = '1') then -- running shift
193
          -- coarse shift: multiples of 4 --
194
          if (or_all_f(shifter.cnt(shifter.cnt'left downto 2)) = '1') then -- shift amount >= 4
195
            shifter.cnt <= std_ulogic_vector(unsigned(shifter.cnt) - 4);
196
            if (ctrl_i(ctrl_alu_shift_dir_c) = '0') then -- SLL: shift left logical
197
              shifter.sreg <= shifter.sreg(shifter.sreg'left-4 downto 0) & "0000";
198
            else -- SRL: shift right logical / SRA: shift right arithmetical
199
              shifter.sreg <= (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) &
200
                              (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) &
201
                              (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)) & shifter.sreg(shifter.sreg'left downto 4);
203
            end if;
204
          -- fine shift: single shifts, 0..3 times --
205
          else
206
            shifter.cnt <= std_ulogic_vector(unsigned(shifter.cnt) - 1);
207
            if (ctrl_i(ctrl_alu_shift_dir_c) = '0') then -- SLL: shift left logical
208
              shifter.sreg <= shifter.sreg(shifter.sreg'left-1 downto 0) & '0';
209
            else -- SRL: shift right logical / SRA: shift right arithmetical
210
              shifter.sreg <= (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) & shifter.sreg(shifter.sreg'left downto 1);
211
            end if;
212 12 zero_gravi
          end if;
213 34 zero_gravi
        end if;
214
 
215
      -- --------------------------------------------------------------------------------
216
      -- Barrel shifter (huge but fast)
217
      -- --------------------------------------------------------------------------------
218
      else
219
 
220
        -- operands and cycle control --
221
        if (shifter.start = '1') then -- trigger new shift
222 36 zero_gravi
          shifter.bs_d_in <= rs1_i; -- shift operand (can only be rs1; opa would also contain pc)
223 34 zero_gravi
          shifter.bs_a_in <= opb(index_size_f(data_width_c)-1 downto 0); -- shift amount
224
          shifter.cnt     <= (others => '0');
225
        end if;
226
 
227
        -- convert left shifts to right shifts --
228
        if (ctrl_i(ctrl_alu_shift_dir_c) = '0') then -- is left shift?
229
          bs_input_v := bit_rev_f(shifter.bs_d_in); -- reverse bit order of input operand
230 12 zero_gravi
        else
231 34 zero_gravi
          bs_input_v := shifter.bs_d_in;
232 2 zero_gravi
        end if;
233 34 zero_gravi
        -- shift >> 16 --
234
        if (shifter.bs_a_in(4) = '1') then
235
          bs_level_4_v(31 downto 16) := (others => (bs_input_v(bs_input_v'left) and ctrl_i(ctrl_alu_shift_ar_c)));
236
          bs_level_4_v(15 downto 00) := (bs_input_v(31 downto 16));
237
        else
238
          bs_level_4_v := bs_input_v;
239
        end if;
240
        -- shift >> 8 --
241
        if (shifter.bs_a_in(3) = '1') then
242
          bs_level_3_v(31 downto 24) := (others => (bs_input_v(bs_input_v'left) and ctrl_i(ctrl_alu_shift_ar_c)));
243
          bs_level_3_v(23 downto 00) := (bs_level_4_v(31 downto 8));
244
        else
245
          bs_level_3_v := bs_level_4_v;
246
        end if;
247
        -- shift >> 4 --
248
        if (shifter.bs_a_in(2) = '1') then
249
          bs_level_2_v(31 downto 28) := (others => (bs_input_v(bs_input_v'left) and ctrl_i(ctrl_alu_shift_ar_c)));
250
          bs_level_2_v(27 downto 00) := (bs_level_3_v(31 downto 4));
251
        else
252
          bs_level_2_v := bs_level_3_v;
253
        end if;
254
        -- shift >> 2 --
255
        if (shifter.bs_a_in(1) = '1') then
256
          bs_level_1_v(31 downto 30) := (others => (bs_input_v(bs_input_v'left) and ctrl_i(ctrl_alu_shift_ar_c)));
257
          bs_level_1_v(29 downto 00) := (bs_level_2_v(31 downto 2));
258
        else
259
          bs_level_1_v := bs_level_2_v;
260
        end if;
261
        -- shift >> 1 --
262
        if (shifter.bs_a_in(0) = '1') then
263
          bs_level_0_v(31 downto 31) := (others => (bs_input_v(bs_input_v'left) and ctrl_i(ctrl_alu_shift_ar_c)));
264
          bs_level_0_v(30 downto 00) := (bs_level_1_v(31 downto 1));
265
        else
266
          bs_level_0_v := bs_level_1_v;
267
        end if;
268
        -- re-convert original left shifts --
269
        if (ctrl_i(ctrl_alu_shift_dir_c) = '0') then
270
          shifter.sreg <= bit_rev_f(bs_level_0_v);
271
        else
272
          shifter.sreg <= bs_level_0_v;
273
        end if;
274 2 zero_gravi
      end if;
275
    end if;
276
  end process shifter_unit;
277
 
278
  -- is shift operation? --
279 29 zero_gravi
  shifter.cmd   <= '1' when (ctrl_i(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) = alu_cmd_shift_c) else '0';
280 12 zero_gravi
  shifter.start <= '1' when (shifter.cmd = '1') and (shifter.cmd_ff = '0') else '0';
281 2 zero_gravi
 
282
  -- shift operation running? --
283 19 zero_gravi
  shifter.run  <= '1' when (or_all_f(shifter.cnt) = '1') or (shifter.start = '1') else '0';
284
  shifter.halt <= '1' when (or_all_f(shifter.cnt(shifter.cnt'left downto 1)) = '1') or (shifter.start = '1') else '0';
285 2 zero_gravi
 
286
 
287 19 zero_gravi
  -- Coprocessor Arbiter --------------------------------------------------------------------
288 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
289 19 zero_gravi
  cp_arbiter: process(rstn_i, clk_i)
290 2 zero_gravi
  begin
291
    if (rstn_i = '0') then
292 19 zero_gravi
      cp_ctrl.cmd_ff <= '0';
293
      cp_ctrl.busy   <= '0';
294 2 zero_gravi
    elsif rising_edge(clk_i) then
295 19 zero_gravi
      if (CPU_EXTENSION_RISCV_M = true) then
296 29 zero_gravi
        cp_ctrl.cmd_ff <= cp_ctrl.cmd;
297 19 zero_gravi
        if (cp_ctrl.start = '1') then
298
          cp_ctrl.busy <= '1';
299 36 zero_gravi
        elsif ((cp0_valid_i or cp1_valid_i or cp2_valid_i or cp3_valid_i) = '1') then -- cp computation done?
300 24 zero_gravi
          cp_ctrl.busy <= '0';
301 2 zero_gravi
        end if;
302 23 zero_gravi
      else -- no co-processor(s) implemented
303 19 zero_gravi
        cp_ctrl.cmd_ff <= '0';
304
        cp_ctrl.busy   <= '0';
305 2 zero_gravi
      end if;
306
    end if;
307 19 zero_gravi
  end process cp_arbiter;
308 2 zero_gravi
 
309
  -- is co-processor operation? --
310 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';
311
  cp_ctrl.start <= '1' when (cp_ctrl.cmd = '1') and (cp_ctrl.cmd_ff = '0') else '0';
312 36 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) = "00") else '0'; -- CP0: MULDIV CP
313
  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'; -- CP1: not implemented yet
314
  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'; -- CP2: not implemented yet
315
  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'; -- CP3: not implemented yet
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 36 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
322 24 zero_gravi
 
323
 
324 2 zero_gravi
  -- ALU Function Select --------------------------------------------------------------------
325
  -- -------------------------------------------------------------------------------------------
326 36 zero_gravi
  alu_function_mux: process(ctrl_i, rs1_i, opb, addsub_res, cp_res, shifter.sreg)
327 2 zero_gravi
  begin
328
    case ctrl_i(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) is
329 36 zero_gravi
      when alu_cmd_xor_c    => res_o <= rs1_i xor opb; -- only rs1 required for logic ops (opa would also contain pc)
330
      when alu_cmd_or_c     => res_o <= rs1_i or  opb;
331
      when alu_cmd_and_c    => res_o <= rs1_i and opb;
332 29 zero_gravi
      when alu_cmd_movb_c   => res_o <= opb;
333 36 zero_gravi
      when alu_cmd_addsub_c => res_o <= addsub_res(data_width_c-1 downto 0);
334 29 zero_gravi
      when alu_cmd_cp_c     => res_o <= cp_res;
335
      when alu_cmd_shift_c  => res_o <= shifter.sreg;
336 36 zero_gravi
      when alu_cmd_slt_c    => res_o <= (others => '0'); res_o(0) <= addsub_res(addsub_res'left); -- => carry/borrow
337 29 zero_gravi
      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.