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

Subversion Repositories neorv32

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

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