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

Subversion Repositories neorv32

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

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
    csr_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- csr read data
60
    -- data output --
61
    cmp_o       : out std_ulogic_vector(1 downto 0); -- comparator status
62
    add_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- OPA + OPB
63
    res_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
64
    -- co-processor interface --
65 24 zero_gravi
    cp_opa_o    : out std_ulogic_vector(data_width_c-1 downto 0); -- co-processor operand a
66
    cp_opb_o    : out std_ulogic_vector(data_width_c-1 downto 0); -- co-processor operand b
67 19 zero_gravi
    cp0_start_o : out std_ulogic; -- trigger co-processor 0
68 2 zero_gravi
    cp0_data_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- co-processor 0 result
69
    cp0_valid_i : in  std_ulogic; -- co-processor 0 result valid
70 19 zero_gravi
    cp1_start_o : out std_ulogic; -- trigger co-processor 1
71 2 zero_gravi
    cp1_data_i  : in  std_ulogic_vector(data_width_c-1 downto 0); -- co-processor 1 result
72
    cp1_valid_i : in  std_ulogic; -- co-processor 1 result valid
73
    -- status --
74
    wait_o      : out std_ulogic -- busy due to iterative processing units
75
  );
76
end neorv32_cpu_alu;
77
 
78
architecture neorv32_cpu_cpu_rtl of neorv32_cpu_alu is
79
 
80
  -- operands --
81 6 zero_gravi
  signal opa, opb, opc : std_ulogic_vector(data_width_c-1 downto 0);
82 2 zero_gravi
 
83
  -- results --
84
  signal add_res : std_ulogic_vector(data_width_c-1 downto 0);
85
  signal alu_res : std_ulogic_vector(data_width_c-1 downto 0);
86 24 zero_gravi
  signal cp_res  : std_ulogic_vector(data_width_c-1 downto 0);
87 2 zero_gravi
 
88
  -- comparator --
89
  signal cmp_opx   : std_ulogic_vector(data_width_c downto 0);
90
  signal cmp_opy   : std_ulogic_vector(data_width_c downto 0);
91
  signal cmp_sub   : std_ulogic_vector(data_width_c downto 0);
92
  signal sub_res   : std_ulogic_vector(data_width_c-1 downto 0);
93
  signal cmp_equal : std_ulogic;
94
  signal cmp_less  : std_ulogic;
95
 
96
  -- shifter --
97 12 zero_gravi
  type shifter_t is record
98
    cmd    : std_ulogic;
99
    cmd_ff : std_ulogic;
100
    start  : std_ulogic;
101
    run    : std_ulogic;
102 19 zero_gravi
    halt   : std_ulogic;
103 12 zero_gravi
    cnt    : std_ulogic_vector(4 downto 0);
104
    sreg   : std_ulogic_vector(data_width_c-1 downto 0);
105
  end record;
106
  signal shifter : shifter_t;
107 2 zero_gravi
 
108 19 zero_gravi
  -- co-processor arbiter and interface --
109
  type cp_ctrl_t is record
110
    cmd_ff : std_ulogic;
111
    busy   : std_ulogic;
112
    start  : std_ulogic;
113
    halt   : std_ulogic;
114
  end record;
115
  signal cp_ctrl : cp_ctrl_t;
116 2 zero_gravi
 
117
begin
118
 
119
  -- Operand Mux ----------------------------------------------------------------------------
120
  -- -------------------------------------------------------------------------------------------
121 6 zero_gravi
  input_op_mux: process(ctrl_i, csr_i, pc2_i, rs1_i, rs2_i, imm_i)
122 2 zero_gravi
  begin
123 26 zero_gravi
    -- operand a (first ALU input operand) --
124 12 zero_gravi
    case ctrl_i(ctrl_alu_opa_mux_msb_c downto ctrl_alu_opa_mux_lsb_c) is
125
      when "00"   => opa <= rs1_i;
126
      when "01"   => opa <= pc2_i;
127
      when others => opa <= csr_i;
128
    end case;
129 26 zero_gravi
    -- operand b (second ALU input operand) --
130 25 zero_gravi
    if (ctrl_i(ctrl_alu_opb_mux_c) = '0') then
131
      opb <= rs2_i;
132
    else
133
      opb <= imm_i;
134
    end if;
135 26 zero_gravi
    -- operand c (third ALU input operand for comparison and SUB) --
136 2 zero_gravi
    if (ctrl_i(ctrl_alu_opc_mux_c) = '0') then
137 26 zero_gravi
      opc <= rs2_i;
138
    else
139 2 zero_gravi
      opc <= imm_i;
140
    end if;
141
  end process input_op_mux;
142
 
143
 
144
  -- Comparator Unit ------------------------------------------------------------------------
145
  -- -------------------------------------------------------------------------------------------
146
  -- less than (x < y) --
147
  cmp_opx  <= (rs1_i(rs1_i'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & rs1_i;
148
  cmp_opy  <= (opc(opc'left)     and (not ctrl_i(ctrl_alu_unsigned_c))) & opc;
149
  cmp_sub  <= std_ulogic_vector(signed(cmp_opx) - signed(cmp_opy));
150 9 zero_gravi
  cmp_less <= cmp_sub(cmp_sub'left); -- carry (borrow) indicates a "less"
151 2 zero_gravi
  sub_res  <= cmp_sub(data_width_c-1 downto 0); -- use the less-comparator also for SUB operations
152
 
153 26 zero_gravi
  -- equal (for branch check only) --
154
  cmp_equal <= '1' when (rs1_i = rs2_i) else '0';
155 2 zero_gravi
 
156 26 zero_gravi
  -- output for branch condition evaluation --
157 2 zero_gravi
  cmp_o(alu_cmp_equal_c) <= cmp_equal;
158
  cmp_o(alu_cmp_less_c)  <= cmp_less;
159
 
160
 
161
  -- Binary Adder ---------------------------------------------------------------------------
162
  -- -------------------------------------------------------------------------------------------
163
  add_res <= std_ulogic_vector(unsigned(opa) + unsigned(opb));
164 26 zero_gravi
  add_o   <= add_res; -- direct output (for PC modification)
165 2 zero_gravi
 
166
 
167
  -- Iterative Shifter Unit -----------------------------------------------------------------
168
  -- -------------------------------------------------------------------------------------------
169
  shifter_unit: process(rstn_i, clk_i)
170
  begin
171
    if (rstn_i = '0') then
172 12 zero_gravi
      shifter.sreg   <= (others => '0');
173
      shifter.cnt    <= (others => '0');
174
      shifter.cmd_ff <= '0';
175 2 zero_gravi
    elsif rising_edge(clk_i) then
176 12 zero_gravi
      shifter.cmd_ff <= shifter.cmd;
177
      if (shifter.start = '1') then -- trigger new shift
178
        shifter.sreg <= opa; -- shift operand
179
        shifter.cnt  <= opb(index_size_f(data_width_c)-1 downto 0); -- shift amount
180
      elsif (shifter.run = '1') then -- running shift
181 23 zero_gravi
        -- coarse shift: multiples of 4 --
182 12 zero_gravi
        if (or_all_f(shifter.cnt(shifter.cnt'left downto 2)) = '1') then -- shift amount >= 4
183
          shifter.cnt <= std_ulogic_vector(unsigned(shifter.cnt) - 4);
184
          if (ctrl_i(ctrl_alu_shift_dir_c) = '0') then -- SLL: shift left logical
185
            shifter.sreg <= shifter.sreg(shifter.sreg'left-4 downto 0) & "0000";
186
          else -- SRL: shift right logical / SRA: shift right arithmetical
187
            shifter.sreg <= (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)) &
190
                            (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) & shifter.sreg(shifter.sreg'left downto 4);
191
          end if;
192 23 zero_gravi
        -- fine shift: single shifts, 0..3 times --
193 12 zero_gravi
        else
194
          shifter.cnt <= std_ulogic_vector(unsigned(shifter.cnt) - 1);
195
          if (ctrl_i(ctrl_alu_shift_dir_c) = '0') then -- SLL: shift left logical
196
            shifter.sreg <= shifter.sreg(shifter.sreg'left-1 downto 0) & '0';
197
          else -- SRL: shift right logical / SRA: shift right arithmetical
198
            shifter.sreg <= (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) & shifter.sreg(shifter.sreg'left downto 1);
199
          end if;
200 2 zero_gravi
        end if;
201
      end if;
202
    end if;
203
  end process shifter_unit;
204
 
205
  -- is shift operation? --
206 19 zero_gravi
  shifter.cmd   <= '1' when (ctrl_i(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) = alu_cmd_shift_c) and (ctrl_i(ctrl_cp_use_c) = '0') else '0';
207 12 zero_gravi
  shifter.start <= '1' when (shifter.cmd = '1') and (shifter.cmd_ff = '0') else '0';
208 2 zero_gravi
 
209
  -- shift operation running? --
210 19 zero_gravi
  shifter.run  <= '1' when (or_all_f(shifter.cnt) = '1') or (shifter.start = '1') else '0';
211
  shifter.halt <= '1' when (or_all_f(shifter.cnt(shifter.cnt'left downto 1)) = '1') or (shifter.start = '1') else '0';
212 2 zero_gravi
 
213
 
214 19 zero_gravi
  -- Coprocessor Arbiter --------------------------------------------------------------------
215 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
216 19 zero_gravi
  cp_arbiter: process(rstn_i, clk_i)
217 2 zero_gravi
  begin
218
    if (rstn_i = '0') then
219 19 zero_gravi
      cp_ctrl.cmd_ff <= '0';
220
      cp_ctrl.busy   <= '0';
221 2 zero_gravi
    elsif rising_edge(clk_i) then
222 19 zero_gravi
      if (CPU_EXTENSION_RISCV_M = true) then
223
        cp_ctrl.cmd_ff <= ctrl_i(ctrl_cp_use_c);
224
        if (cp_ctrl.start = '1') then
225
          cp_ctrl.busy <= '1';
226
        elsif ((cp0_valid_i or cp1_valid_i) = '1') then -- cp computation done?
227 24 zero_gravi
          cp_ctrl.busy <= '0';
228 2 zero_gravi
        end if;
229 23 zero_gravi
      else -- no co-processor(s) implemented
230 19 zero_gravi
        cp_ctrl.cmd_ff <= '0';
231
        cp_ctrl.busy   <= '0';
232 2 zero_gravi
      end if;
233
    end if;
234 19 zero_gravi
  end process cp_arbiter;
235 2 zero_gravi
 
236
  -- is co-processor operation? --
237 19 zero_gravi
  cp_ctrl.start <= '1' when (ctrl_i(ctrl_cp_use_c) = '1') and (cp_ctrl.cmd_ff = '0') else '0';
238 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
239
  cp1_start_o   <= '0'; -- not yet implemented
240 2 zero_gravi
 
241
  -- co-processor operation running? --
242 19 zero_gravi
  cp_ctrl.halt <= cp_ctrl.busy or cp_ctrl.start;
243 2 zero_gravi
 
244 24 zero_gravi
  -- co-processor operands --
245
  cp_opa_o <= opa;
246
  cp_opb_o <= opb;
247 2 zero_gravi
 
248 24 zero_gravi
  -- co-processor result --
249
  cp_res <= cp0_data_i or cp1_data_i; -- only the selcted cp may output data != 0
250
 
251
 
252 2 zero_gravi
  -- ALU Function Select --------------------------------------------------------------------
253
  -- -------------------------------------------------------------------------------------------
254 25 zero_gravi
  alu_function_mux: process(ctrl_i, opa, opb, add_res, sub_res, cmp_less, shifter.sreg)
255 2 zero_gravi
  begin
256
    case ctrl_i(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) is
257
      when alu_cmd_xor_c   => alu_res <= opa xor opb;
258 23 zero_gravi
      when alu_cmd_or_c    => alu_res <= opa or  opb;
259 2 zero_gravi
      when alu_cmd_and_c   => alu_res <= opa and opb;
260 24 zero_gravi
      when alu_cmd_bclr_c  => alu_res <= opa and (not opb);
261 9 zero_gravi
      when alu_cmd_sub_c   => alu_res <= sub_res;
262
      when alu_cmd_add_c   => alu_res <= add_res;
263 12 zero_gravi
      when alu_cmd_shift_c => alu_res <= shifter.sreg;
264 2 zero_gravi
      when alu_cmd_slt_c   => alu_res <= (others => '0'); alu_res(0) <= cmp_less;
265 3 zero_gravi
      when others          => alu_res <= (others => '0'); -- undefined
266 2 zero_gravi
    end case;
267
  end process alu_function_mux;
268
 
269
 
270
  -- ALU Result -----------------------------------------------------------------------------
271
  -- -------------------------------------------------------------------------------------------
272 19 zero_gravi
  wait_o <= shifter.halt or cp_ctrl.halt; -- wait until iterative units have completed
273 24 zero_gravi
  res_o  <= cp_res when (ctrl_i(ctrl_cp_use_c) = '1') else alu_res; -- FIXME?
274 2 zero_gravi
 
275
 
276
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.