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

Subversion Repositories neorv32

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

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