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

Subversion Repositories neorv32

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

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