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

Subversion Repositories neorv32

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

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
    CPU_EXTENSION_RISCV_M : boolean := true -- implement muld/div extension?
48
  );
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
    -- co-processor interface --
63 19 zero_gravi
    cp0_start_o : out std_ulogic; -- trigger co-processor 0
64 2 zero_gravi
    cp0_data_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- co-processor 0 result
65
    cp0_valid_i : in  std_ulogic; -- co-processor 0 result valid
66 19 zero_gravi
    cp1_start_o : out std_ulogic; -- trigger co-processor 1
67 2 zero_gravi
    cp1_data_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- co-processor 1 result
68
    cp1_valid_i : in  std_ulogic; -- co-processor 1 result valid
69
    -- status --
70
    wait_o      : out std_ulogic -- busy due to iterative processing units
71
  );
72
end neorv32_cpu_alu;
73
 
74
architecture neorv32_cpu_cpu_rtl of neorv32_cpu_alu is
75
 
76
  -- operands --
77 29 zero_gravi
  signal opa, opb : std_ulogic_vector(data_width_c-1 downto 0);
78 2 zero_gravi
 
79
  -- results --
80 29 zero_gravi
  signal addsub_res : std_ulogic_vector(data_width_c-1 downto 0);
81
  signal cp_res     : std_ulogic_vector(data_width_c-1 downto 0);
82 2 zero_gravi
 
83
  -- comparator --
84
  signal cmp_opx   : std_ulogic_vector(data_width_c downto 0);
85
  signal cmp_opy   : std_ulogic_vector(data_width_c downto 0);
86
  signal cmp_sub   : std_ulogic_vector(data_width_c downto 0);
87
  signal cmp_less  : std_ulogic;
88
 
89
  -- shifter --
90 12 zero_gravi
  type shifter_t is record
91
    cmd    : std_ulogic;
92
    cmd_ff : std_ulogic;
93
    start  : std_ulogic;
94
    run    : std_ulogic;
95 19 zero_gravi
    halt   : std_ulogic;
96 12 zero_gravi
    cnt    : std_ulogic_vector(4 downto 0);
97
    sreg   : std_ulogic_vector(data_width_c-1 downto 0);
98
  end record;
99
  signal shifter : shifter_t;
100 2 zero_gravi
 
101 19 zero_gravi
  -- co-processor arbiter and interface --
102
  type cp_ctrl_t is record
103 29 zero_gravi
    cmd    : std_ulogic;
104 19 zero_gravi
    cmd_ff : std_ulogic;
105
    busy   : std_ulogic;
106
    start  : std_ulogic;
107
    halt   : std_ulogic;
108
  end record;
109
  signal cp_ctrl : cp_ctrl_t;
110 2 zero_gravi
 
111
begin
112
 
113
  -- Operand Mux ----------------------------------------------------------------------------
114
  -- -------------------------------------------------------------------------------------------
115 29 zero_gravi
  opa <= pc2_i when (ctrl_i(ctrl_alu_opa_mux_c) = '1') else rs1_i; -- operand a (first ALU input operand)
116
  opb <= imm_i when (ctrl_i(ctrl_alu_opb_mux_c) = '1') else rs2_i; -- operand b (second ALU input operand)
117 2 zero_gravi
 
118
 
119
  -- Comparator Unit ------------------------------------------------------------------------
120
  -- -------------------------------------------------------------------------------------------
121
  cmp_opx  <= (rs1_i(rs1_i'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & rs1_i;
122 29 zero_gravi
  cmp_opy  <= (rs2_i(rs2_i'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & rs2_i;
123
  cmp_sub  <= std_ulogic_vector(signed(cmp_opx) - signed(cmp_opy)); -- less than (x < y)
124 2 zero_gravi
 
125 29 zero_gravi
  cmp_o(alu_cmp_equal_c) <= '1' when (rs1_i = rs2_i) else '0';
126
  cmp_o(alu_cmp_less_c)  <= cmp_sub(cmp_sub'left); -- less = carry (borrow)
127 2 zero_gravi
 
128
 
129 29 zero_gravi
  -- Binary Adder/Subtractor ----------------------------------------------------------------
130 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
131 29 zero_gravi
  binary_arithmetic_core: process(ctrl_i, opa, opb)
132
    variable cin_v  : std_ulogic_vector(0 downto 0);
133
    variable op_a_v : std_ulogic_vector(data_width_c downto 0);
134
    variable op_b_v : std_ulogic_vector(data_width_c downto 0);
135
    variable op_y_v : std_ulogic_vector(data_width_c downto 0);
136
    variable res_v  : std_ulogic_vector(data_width_c downto 0);
137
  begin
138
    -- operand sign-extension --
139
    op_a_v := (opa(opa'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & opa;
140
    op_b_v := (opb(opb'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & opb;
141 2 zero_gravi
 
142 29 zero_gravi
    -- add/sub(slt) select --
143
    if (ctrl_i(ctrl_alu_addsub_c) = '1') then -- subtraction
144
      op_y_v   := not op_b_v;
145
      cin_v(0) := '1';
146
    else-- addition
147
      op_y_v   := op_b_v;
148
      cin_v(0) := '0';
149
    end if;
150 2 zero_gravi
 
151 29 zero_gravi
    -- adder core --
152
    res_v := std_ulogic_vector(unsigned(op_a_v) + unsigned(op_y_v) + unsigned(cin_v(0 downto 0)));
153
 
154
    -- output --
155
    cmp_less    <= res_v(32);
156
    addsub_res  <= res_v(31 downto 0);
157
    addsub_res  <= res_v(31 downto 0);
158
  end process binary_arithmetic_core;
159
 
160
 
161 2 zero_gravi
  -- Iterative Shifter Unit -----------------------------------------------------------------
162
  -- -------------------------------------------------------------------------------------------
163
  shifter_unit: process(rstn_i, clk_i)
164
  begin
165
    if (rstn_i = '0') then
166 12 zero_gravi
      shifter.sreg   <= (others => '0');
167
      shifter.cnt    <= (others => '0');
168
      shifter.cmd_ff <= '0';
169 2 zero_gravi
    elsif rising_edge(clk_i) then
170 12 zero_gravi
      shifter.cmd_ff <= shifter.cmd;
171
      if (shifter.start = '1') then -- trigger new shift
172
        shifter.sreg <= opa; -- shift operand
173
        shifter.cnt  <= opb(index_size_f(data_width_c)-1 downto 0); -- shift amount
174
      elsif (shifter.run = '1') then -- running shift
175 23 zero_gravi
        -- coarse shift: multiples of 4 --
176 12 zero_gravi
        if (or_all_f(shifter.cnt(shifter.cnt'left downto 2)) = '1') then -- shift amount >= 4
177
          shifter.cnt <= std_ulogic_vector(unsigned(shifter.cnt) - 4);
178
          if (ctrl_i(ctrl_alu_shift_dir_c) = '0') then -- SLL: shift left logical
179
            shifter.sreg <= shifter.sreg(shifter.sreg'left-4 downto 0) & "0000";
180
          else -- SRL: shift right logical / SRA: shift right arithmetical
181
            shifter.sreg <= (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) &
182
                            (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) &
183
                            (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)) & shifter.sreg(shifter.sreg'left downto 4);
185
          end if;
186 23 zero_gravi
        -- fine shift: single shifts, 0..3 times --
187 12 zero_gravi
        else
188
          shifter.cnt <= std_ulogic_vector(unsigned(shifter.cnt) - 1);
189
          if (ctrl_i(ctrl_alu_shift_dir_c) = '0') then -- SLL: shift left logical
190
            shifter.sreg <= shifter.sreg(shifter.sreg'left-1 downto 0) & '0';
191
          else -- SRL: shift right logical / SRA: shift right arithmetical
192
            shifter.sreg <= (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) & shifter.sreg(shifter.sreg'left downto 1);
193
          end if;
194 2 zero_gravi
        end if;
195
      end if;
196
    end if;
197
  end process shifter_unit;
198
 
199
  -- is shift operation? --
200 29 zero_gravi
  shifter.cmd   <= '1' when (ctrl_i(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) = alu_cmd_shift_c) else '0';
201 12 zero_gravi
  shifter.start <= '1' when (shifter.cmd = '1') and (shifter.cmd_ff = '0') else '0';
202 2 zero_gravi
 
203
  -- shift operation running? --
204 19 zero_gravi
  shifter.run  <= '1' when (or_all_f(shifter.cnt) = '1') or (shifter.start = '1') else '0';
205
  shifter.halt <= '1' when (or_all_f(shifter.cnt(shifter.cnt'left downto 1)) = '1') or (shifter.start = '1') else '0';
206 2 zero_gravi
 
207
 
208 19 zero_gravi
  -- Coprocessor Arbiter --------------------------------------------------------------------
209 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
210 19 zero_gravi
  cp_arbiter: process(rstn_i, clk_i)
211 2 zero_gravi
  begin
212
    if (rstn_i = '0') then
213 19 zero_gravi
      cp_ctrl.cmd_ff <= '0';
214
      cp_ctrl.busy   <= '0';
215 2 zero_gravi
    elsif rising_edge(clk_i) then
216 19 zero_gravi
      if (CPU_EXTENSION_RISCV_M = true) then
217 29 zero_gravi
        cp_ctrl.cmd_ff <= cp_ctrl.cmd;
218 19 zero_gravi
        if (cp_ctrl.start = '1') then
219
          cp_ctrl.busy <= '1';
220
        elsif ((cp0_valid_i or cp1_valid_i) = '1') then -- cp computation done?
221 24 zero_gravi
          cp_ctrl.busy <= '0';
222 2 zero_gravi
        end if;
223 23 zero_gravi
      else -- no co-processor(s) implemented
224 19 zero_gravi
        cp_ctrl.cmd_ff <= '0';
225
        cp_ctrl.busy   <= '0';
226 2 zero_gravi
      end if;
227
    end if;
228 19 zero_gravi
  end process cp_arbiter;
229 2 zero_gravi
 
230
  -- is co-processor operation? --
231 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';
232
  cp_ctrl.start <= '1' when (cp_ctrl.cmd = '1') and (cp_ctrl.cmd_ff = '0') else '0';
233 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
234
  cp1_start_o   <= '0'; -- not yet implemented
235 2 zero_gravi
 
236
  -- co-processor operation running? --
237 19 zero_gravi
  cp_ctrl.halt <= cp_ctrl.busy or cp_ctrl.start;
238 2 zero_gravi
 
239 24 zero_gravi
  -- co-processor result --
240 29 zero_gravi
  cp_res <= cp0_data_i or cp1_data_i; -- only the **actaully selected** co-processor should output data != 0
241 24 zero_gravi
 
242
 
243 2 zero_gravi
  -- ALU Function Select --------------------------------------------------------------------
244
  -- -------------------------------------------------------------------------------------------
245 29 zero_gravi
  alu_function_mux: process(ctrl_i, opa, opb, addsub_res, cp_res, cmp_less, shifter.sreg)
246 2 zero_gravi
  begin
247
    case ctrl_i(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) is
248 29 zero_gravi
      when alu_cmd_xor_c    => res_o <= opa xor opb;
249
      when alu_cmd_or_c     => res_o <= opa or  opb;
250
      when alu_cmd_and_c    => res_o <= opa and opb;
251
      when alu_cmd_movb_c   => res_o <= opb;
252
      when alu_cmd_addsub_c => res_o <= addsub_res;
253
      when alu_cmd_cp_c     => res_o <= cp_res;
254
      when alu_cmd_shift_c  => res_o <= shifter.sreg;
255
      when alu_cmd_slt_c    => res_o <= (others => '0'); res_o(0) <= cmp_less;
256
      when others           => res_o <= opb; -- undefined
257 2 zero_gravi
    end case;
258
  end process alu_function_mux;
259
 
260
 
261 29 zero_gravi
  -- ALU Busy -------------------------------------------------------------------------------
262 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
263 19 zero_gravi
  wait_o <= shifter.halt or cp_ctrl.halt; -- wait until iterative units have completed
264 2 zero_gravi
 
265
 
266
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.