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

Subversion Repositories neorv32

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

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