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

Subversion Repositories neorv32

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

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
    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 24 zero_gravi
  signal cp_res  : std_ulogic_vector(data_width_c-1 downto 0);
84 2 zero_gravi
 
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
  end record;
112
  signal cp_ctrl : cp_ctrl_t;
113 2 zero_gravi
 
114
begin
115
 
116
  -- Operand Mux ----------------------------------------------------------------------------
117
  -- -------------------------------------------------------------------------------------------
118 27 zero_gravi
  opa <= rs1_i when (ctrl_i(ctrl_alu_opa_mux_c) = '0') else pc2_i; -- operand a (first ALU input operand)
119
  opb <= rs2_i when (ctrl_i(ctrl_alu_opb_mux_c) = '0') else imm_i; -- operand b (second ALU input operand)
120
  opc <= rs2_i when (ctrl_i(ctrl_alu_opc_mux_c) = '0') else imm_i; -- operand c (third ALU input operand for comparison and SUB)
121 2 zero_gravi
 
122
 
123
  -- Comparator Unit ------------------------------------------------------------------------
124
  -- -------------------------------------------------------------------------------------------
125
  -- less than (x < y) --
126
  cmp_opx  <= (rs1_i(rs1_i'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & rs1_i;
127
  cmp_opy  <= (opc(opc'left)     and (not ctrl_i(ctrl_alu_unsigned_c))) & opc;
128
  cmp_sub  <= std_ulogic_vector(signed(cmp_opx) - signed(cmp_opy));
129 9 zero_gravi
  cmp_less <= cmp_sub(cmp_sub'left); -- carry (borrow) indicates a "less"
130 2 zero_gravi
  sub_res  <= cmp_sub(data_width_c-1 downto 0); -- use the less-comparator also for SUB operations
131
 
132 26 zero_gravi
  -- equal (for branch check only) --
133
  cmp_equal <= '1' when (rs1_i = rs2_i) else '0';
134 2 zero_gravi
 
135 26 zero_gravi
  -- output for branch condition evaluation --
136 2 zero_gravi
  cmp_o(alu_cmp_equal_c) <= cmp_equal;
137
  cmp_o(alu_cmp_less_c)  <= cmp_less;
138
 
139
 
140
  -- Binary Adder ---------------------------------------------------------------------------
141
  -- -------------------------------------------------------------------------------------------
142
  add_res <= std_ulogic_vector(unsigned(opa) + unsigned(opb));
143 26 zero_gravi
  add_o   <= add_res; -- direct output (for PC modification)
144 2 zero_gravi
 
145
 
146
  -- Iterative Shifter Unit -----------------------------------------------------------------
147
  -- -------------------------------------------------------------------------------------------
148
  shifter_unit: process(rstn_i, clk_i)
149
  begin
150
    if (rstn_i = '0') then
151 12 zero_gravi
      shifter.sreg   <= (others => '0');
152
      shifter.cnt    <= (others => '0');
153
      shifter.cmd_ff <= '0';
154 2 zero_gravi
    elsif rising_edge(clk_i) then
155 12 zero_gravi
      shifter.cmd_ff <= shifter.cmd;
156
      if (shifter.start = '1') then -- trigger new shift
157
        shifter.sreg <= opa; -- shift operand
158
        shifter.cnt  <= opb(index_size_f(data_width_c)-1 downto 0); -- shift amount
159
      elsif (shifter.run = '1') then -- running shift
160 23 zero_gravi
        -- coarse shift: multiples of 4 --
161 12 zero_gravi
        if (or_all_f(shifter.cnt(shifter.cnt'left downto 2)) = '1') then -- shift amount >= 4
162
          shifter.cnt <= std_ulogic_vector(unsigned(shifter.cnt) - 4);
163
          if (ctrl_i(ctrl_alu_shift_dir_c) = '0') then -- SLL: shift left logical
164
            shifter.sreg <= shifter.sreg(shifter.sreg'left-4 downto 0) & "0000";
165
          else -- SRL: shift right logical / SRA: shift right arithmetical
166
            shifter.sreg <= (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) &
167
                            (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) &
168
                            (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) &
169
                            (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) & shifter.sreg(shifter.sreg'left downto 4);
170
          end if;
171 23 zero_gravi
        -- fine shift: single shifts, 0..3 times --
172 12 zero_gravi
        else
173
          shifter.cnt <= std_ulogic_vector(unsigned(shifter.cnt) - 1);
174
          if (ctrl_i(ctrl_alu_shift_dir_c) = '0') then -- SLL: shift left logical
175
            shifter.sreg <= shifter.sreg(shifter.sreg'left-1 downto 0) & '0';
176
          else -- SRL: shift right logical / SRA: shift right arithmetical
177
            shifter.sreg <= (shifter.sreg(shifter.sreg'left) and ctrl_i(ctrl_alu_shift_ar_c)) & shifter.sreg(shifter.sreg'left downto 1);
178
          end if;
179 2 zero_gravi
        end if;
180
      end if;
181
    end if;
182
  end process shifter_unit;
183
 
184
  -- is shift operation? --
185 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';
186 12 zero_gravi
  shifter.start <= '1' when (shifter.cmd = '1') and (shifter.cmd_ff = '0') else '0';
187 2 zero_gravi
 
188
  -- shift operation running? --
189 19 zero_gravi
  shifter.run  <= '1' when (or_all_f(shifter.cnt) = '1') or (shifter.start = '1') else '0';
190
  shifter.halt <= '1' when (or_all_f(shifter.cnt(shifter.cnt'left downto 1)) = '1') or (shifter.start = '1') else '0';
191 2 zero_gravi
 
192
 
193 19 zero_gravi
  -- Coprocessor Arbiter --------------------------------------------------------------------
194 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
195 19 zero_gravi
  cp_arbiter: process(rstn_i, clk_i)
196 2 zero_gravi
  begin
197
    if (rstn_i = '0') then
198 19 zero_gravi
      cp_ctrl.cmd_ff <= '0';
199
      cp_ctrl.busy   <= '0';
200 2 zero_gravi
    elsif rising_edge(clk_i) then
201 19 zero_gravi
      if (CPU_EXTENSION_RISCV_M = true) then
202
        cp_ctrl.cmd_ff <= ctrl_i(ctrl_cp_use_c);
203
        if (cp_ctrl.start = '1') then
204
          cp_ctrl.busy <= '1';
205
        elsif ((cp0_valid_i or cp1_valid_i) = '1') then -- cp computation done?
206 24 zero_gravi
          cp_ctrl.busy <= '0';
207 2 zero_gravi
        end if;
208 23 zero_gravi
      else -- no co-processor(s) implemented
209 19 zero_gravi
        cp_ctrl.cmd_ff <= '0';
210
        cp_ctrl.busy   <= '0';
211 2 zero_gravi
      end if;
212
    end if;
213 19 zero_gravi
  end process cp_arbiter;
214 2 zero_gravi
 
215
  -- is co-processor operation? --
216 19 zero_gravi
  cp_ctrl.start <= '1' when (ctrl_i(ctrl_cp_use_c) = '1') and (cp_ctrl.cmd_ff = '0') else '0';
217 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
218
  cp1_start_o   <= '0'; -- not yet implemented
219 2 zero_gravi
 
220
  -- co-processor operation running? --
221 19 zero_gravi
  cp_ctrl.halt <= cp_ctrl.busy or cp_ctrl.start;
222 2 zero_gravi
 
223 24 zero_gravi
  -- co-processor result --
224 27 zero_gravi
  cp_res <= cp0_data_i or cp1_data_i; -- only the selected cp may output data != 0
225 24 zero_gravi
 
226
 
227 2 zero_gravi
  -- ALU Function Select --------------------------------------------------------------------
228
  -- -------------------------------------------------------------------------------------------
229 25 zero_gravi
  alu_function_mux: process(ctrl_i, opa, opb, add_res, sub_res, cmp_less, shifter.sreg)
230 2 zero_gravi
  begin
231
    case ctrl_i(ctrl_alu_cmd2_c downto ctrl_alu_cmd0_c) is
232
      when alu_cmd_xor_c   => alu_res <= opa xor opb;
233 23 zero_gravi
      when alu_cmd_or_c    => alu_res <= opa or  opb;
234 2 zero_gravi
      when alu_cmd_and_c   => alu_res <= opa and opb;
235 27 zero_gravi
      when alu_cmd_movb_c  => alu_res <= opb;
236 9 zero_gravi
      when alu_cmd_sub_c   => alu_res <= sub_res;
237
      when alu_cmd_add_c   => alu_res <= add_res;
238 12 zero_gravi
      when alu_cmd_shift_c => alu_res <= shifter.sreg;
239 2 zero_gravi
      when alu_cmd_slt_c   => alu_res <= (others => '0'); alu_res(0) <= cmp_less;
240 3 zero_gravi
      when others          => alu_res <= (others => '0'); -- undefined
241 2 zero_gravi
    end case;
242
  end process alu_function_mux;
243
 
244
 
245
  -- ALU Result -----------------------------------------------------------------------------
246
  -- -------------------------------------------------------------------------------------------
247 19 zero_gravi
  wait_o <= shifter.halt or cp_ctrl.halt; -- wait until iterative units have completed
248 24 zero_gravi
  res_o  <= cp_res when (ctrl_i(ctrl_cp_use_c) = '1') else alu_res; -- FIXME?
249 2 zero_gravi
 
250
 
251
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.