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

Subversion Repositories neorv32

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

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