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

Subversion Repositories neorv32

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

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