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

Subversion Repositories neorv32

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 zero_gravi
-- #################################################################################################
2
-- # << NEORV32 - Arithmetical/Logical Unit >>                                                     #
3
-- # ********************************************************************************************* #
4 72 zero_gravi
-- # Main data/address ALU and ALU co-processor (= multi-cycle function units).                    #
5 2 zero_gravi
-- # ********************************************************************************************* #
6
-- # BSD 3-Clause License                                                                          #
7
-- #                                                                                               #
8 70 zero_gravi
-- # Copyright (c) 2022, Stephan Nolting. All rights reserved.                                     #
9 2 zero_gravi
-- #                                                                                               #
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 61 zero_gravi
    -- RISC-V CPU Extensions --
47 66 zero_gravi
    CPU_EXTENSION_RISCV_B     : boolean; -- implement bit-manipulation extension?
48 62 zero_gravi
    CPU_EXTENSION_RISCV_M     : boolean; -- implement mul/div extension?
49
    CPU_EXTENSION_RISCV_Zmmul : boolean; -- implement multiply-only M sub-extension?
50
    CPU_EXTENSION_RISCV_Zfinx : boolean; -- implement 32-bit floating-point extension (using INT reg!)
51 72 zero_gravi
    CPU_EXTENSION_RISCV_Zxcfu : boolean; -- implement custom (instr.) functions unit?
52 61 zero_gravi
    -- Extension Options --
53 62 zero_gravi
    FAST_MUL_EN               : boolean; -- use DSPs for M extension's multiplier
54
    FAST_SHIFT_EN             : boolean  -- use barrel shifter for shift operations
55 11 zero_gravi
  );
56 2 zero_gravi
  port (
57
    -- global control --
58
    clk_i       : in  std_ulogic; -- global clock, rising edge
59
    rstn_i      : in  std_ulogic; -- global reset, low-active, async
60
    ctrl_i      : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
61
    -- data input --
62
    rs1_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
63
    rs2_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
64 68 zero_gravi
    pc_i        : in  std_ulogic_vector(data_width_c-1 downto 0); -- current PC
65 2 zero_gravi
    imm_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- immediate
66
    -- data output --
67 65 zero_gravi
    cmp_o       : out std_ulogic_vector(1 downto 0); -- comparator status
68 2 zero_gravi
    res_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
69 36 zero_gravi
    add_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- address computation result
70 61 zero_gravi
    fpu_flags_o : out std_ulogic_vector(4 downto 0); -- FPU exception flags
71 2 zero_gravi
    -- status --
72 61 zero_gravi
    idone_o     : out std_ulogic -- iterative processing units done?
73 2 zero_gravi
  );
74
end neorv32_cpu_alu;
75
 
76
architecture neorv32_cpu_cpu_rtl of neorv32_cpu_alu is
77
 
78 65 zero_gravi
  -- comparator --
79
  signal cmp_opx : std_ulogic_vector(data_width_c downto 0);
80
  signal cmp_opy : std_ulogic_vector(data_width_c downto 0);
81
  signal cmp     : std_ulogic_vector(1 downto 0); -- comparator status
82
 
83 2 zero_gravi
  -- operands --
84 29 zero_gravi
  signal opa, opb : std_ulogic_vector(data_width_c-1 downto 0);
85 2 zero_gravi
 
86
  -- results --
87 36 zero_gravi
  signal addsub_res : std_ulogic_vector(data_width_c downto 0);
88 29 zero_gravi
  signal cp_res     : std_ulogic_vector(data_width_c-1 downto 0);
89 2 zero_gravi
 
90 61 zero_gravi
  -- co-processor interface --
91 71 zero_gravi
  type cp_data_if_t  is array (0 to 7)  of std_ulogic_vector(data_width_c-1 downto 0);
92 73 zero_gravi
  signal cp_result : cp_data_if_t; -- co-processor i result
93 71 zero_gravi
  signal cp_start  : std_ulogic_vector(7 downto 0); -- trigger co-processor i
94
  signal cp_valid  : std_ulogic_vector(7 downto 0); -- co-processor i done
95 61 zero_gravi
 
96 2 zero_gravi
begin
97
 
98 65 zero_gravi
  -- Comparator Unit (for conditional branches) ---------------------------------------------
99 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
100 65 zero_gravi
  cmp_opx <= (rs1_i(rs1_i'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & rs1_i;
101
  cmp_opy <= (rs2_i(rs2_i'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & rs2_i;
102
 
103
  cmp(cmp_equal_c) <= '1' when (rs1_i = rs2_i) else '0';
104
  cmp(cmp_less_c)  <= '1' when (signed(cmp_opx) < signed(cmp_opy)) else '0';
105
  cmp_o            <= cmp;
106
 
107
 
108
  -- ALU Input Operand Mux ------------------------------------------------------------------
109
  -- -------------------------------------------------------------------------------------------
110 68 zero_gravi
  opa <= pc_i  when (ctrl_i(ctrl_alu_opa_mux_c) = '1') else rs1_i; -- operand a (first ALU input operand), only required for arithmetic ops
111 29 zero_gravi
  opb <= imm_i when (ctrl_i(ctrl_alu_opb_mux_c) = '1') else rs2_i; -- operand b (second ALU input operand)
112 2 zero_gravi
 
113
 
114 61 zero_gravi
  -- Binary Adder/Subtracter ----------------------------------------------------------------
115 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
116 29 zero_gravi
  binary_arithmetic_core: process(ctrl_i, opa, opb)
117
    variable cin_v  : std_ulogic_vector(0 downto 0);
118
    variable op_a_v : std_ulogic_vector(data_width_c downto 0);
119
    variable op_b_v : std_ulogic_vector(data_width_c downto 0);
120
    variable op_y_v : std_ulogic_vector(data_width_c downto 0);
121
    variable res_v  : std_ulogic_vector(data_width_c downto 0);
122
  begin
123
    -- operand sign-extension --
124
    op_a_v := (opa(opa'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & opa;
125
    op_b_v := (opb(opb'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & opb;
126
    -- add/sub(slt) select --
127 68 zero_gravi
    if (ctrl_i(ctrl_alu_op0_c) = '1') then -- subtraction
128 29 zero_gravi
      op_y_v   := not op_b_v;
129
      cin_v(0) := '1';
130 36 zero_gravi
    else -- addition
131 29 zero_gravi
      op_y_v   := op_b_v;
132
      cin_v(0) := '0';
133
    end if;
134 68 zero_gravi
    -- adder core --
135 36 zero_gravi
    addsub_res <= std_ulogic_vector(unsigned(op_a_v) + unsigned(op_y_v) + unsigned(cin_v(0 downto 0)));
136 29 zero_gravi
  end process binary_arithmetic_core;
137
 
138 68 zero_gravi
  -- direct output of adder result --
139 36 zero_gravi
  add_o <= addsub_res(data_width_c-1 downto 0);
140 29 zero_gravi
 
141 68 zero_gravi
 
142
  -- ALU Operation Select -------------------------------------------------------------------
143
  -- -------------------------------------------------------------------------------------------
144 73 zero_gravi
  alu_core: process(ctrl_i, addsub_res, cp_res, rs1_i, opb)
145 39 zero_gravi
  begin
146 68 zero_gravi
    case ctrl_i(ctrl_alu_op2_c downto ctrl_alu_op0_c) is
147 73 zero_gravi
      when alu_op_add_c  => res_o <= addsub_res(data_width_c-1 downto 0); -- default
148
      when alu_op_sub_c  => res_o <= addsub_res(data_width_c-1 downto 0);
149
      when alu_op_cp_c   => res_o <= cp_res;
150
      when alu_op_slt_c  => res_o <= (others => '0'); res_o(0) <= addsub_res(addsub_res'left); -- carry/borrow
151
      when alu_op_movb_c => res_o <= opb;
152
      when alu_op_xor_c  => res_o <= rs1_i xor opb; -- only rs1 required for logic ops (opa would also contain pc)
153
      when alu_op_or_c   => res_o <= rs1_i or  opb;
154
      when alu_op_and_c  => res_o <= rs1_i and opb;
155
      when others        => res_o <= addsub_res(data_width_c-1 downto 0);
156 68 zero_gravi
    end case;
157
  end process alu_core;
158 36 zero_gravi
 
159 39 zero_gravi
 
160 68 zero_gravi
  -- **************************************************************************************************************************
161 73 zero_gravi
  -- ALU Co-Processors
162 68 zero_gravi
  -- **************************************************************************************************************************
163
 
164 71 zero_gravi
  -- co-processor select / start trigger --
165 73 zero_gravi
  -- > "cp_start" is high for one cycle to trigger operation of the according co-processor
166
  cp_start(7 downto 0) <= ctrl_i(ctrl_cp_trig7_c downto ctrl_cp_trig0_c);
167 2 zero_gravi
 
168 61 zero_gravi
  -- co-processor operation done? --
169 73 zero_gravi
  -- > "cp_valid" signal has to be set (for one cycle) one cycle before output data (cp_result) is valid
170
  idone_o <= cp_valid(0) or cp_valid(1) or cp_valid(2) or cp_valid(3) or
171
             cp_valid(4) or cp_valid(5) or cp_valid(6) or cp_valid(7);
172 39 zero_gravi
 
173 73 zero_gravi
  -- co-processor result --
174
  -- > "cp_result" data has to be always zero unless co-processor was actually triggered
175
  cp_res <= cp_result(0) or cp_result(1) or cp_result(2) or cp_result(3) or
176
            cp_result(4) or cp_result(5) or cp_result(6) or cp_result(7);
177 24 zero_gravi
 
178
 
179 73 zero_gravi
  -- Co-Processor 0: Shifter Unit (CPU Base ISA) --------------------------------------------
180 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
181 65 zero_gravi
  neorv32_cpu_cp_shifter_inst: neorv32_cpu_cp_shifter
182
  generic map (
183
    FAST_SHIFT_EN => FAST_SHIFT_EN -- use barrel shifter for shift operations
184
  )
185
  port map (
186
    -- global control --
187 70 zero_gravi
    clk_i   => clk_i,        -- global clock, rising edge
188
    rstn_i  => rstn_i,       -- global reset, low-active, async
189
    ctrl_i  => ctrl_i,       -- main control bus
190
    start_i => cp_start(0),  -- trigger operation
191 65 zero_gravi
    -- data input --
192 70 zero_gravi
    rs1_i   => rs1_i,        -- rf source 1
193 66 zero_gravi
    shamt_i => opb(index_size_f(data_width_c)-1 downto 0), -- shift amount
194 65 zero_gravi
    -- result and status --
195 70 zero_gravi
    res_o   => cp_result(0), -- operation result
196
    valid_o => cp_valid(0)   -- data output valid
197 65 zero_gravi
  );
198 2 zero_gravi
 
199
 
200 73 zero_gravi
  -- Co-Processor 1: Integer Multiplication/Division Unit ('M' Extension) -------------------
201 61 zero_gravi
  -- -------------------------------------------------------------------------------------------
202
  neorv32_cpu_cp_muldiv_inst_true:
203
  if (CPU_EXTENSION_RISCV_M = true) or (CPU_EXTENSION_RISCV_Zmmul = true) generate
204
    neorv32_cpu_cp_muldiv_inst: neorv32_cpu_cp_muldiv
205
    generic map (
206
      FAST_MUL_EN => FAST_MUL_EN,          -- use DSPs for faster multiplication
207
      DIVISION_EN => CPU_EXTENSION_RISCV_M -- implement divider hardware
208
    )
209
    port map (
210
      -- global control --
211 70 zero_gravi
      clk_i   => clk_i,        -- global clock, rising edge
212
      rstn_i  => rstn_i,       -- global reset, low-active, async
213
      ctrl_i  => ctrl_i,       -- main control bus
214
      start_i => cp_start(1),  -- trigger operation
215 61 zero_gravi
      -- data input --
216 70 zero_gravi
      rs1_i   => rs1_i,        -- rf source 1
217
      rs2_i   => rs2_i,        -- rf source 2
218 61 zero_gravi
      -- result and status --
219 70 zero_gravi
      res_o   => cp_result(1), -- operation result
220
      valid_o => cp_valid(1)   -- data output valid
221 61 zero_gravi
    );
222
  end generate;
223
 
224
  neorv32_cpu_cp_muldiv_inst_false:
225
  if (CPU_EXTENSION_RISCV_M = false) and (CPU_EXTENSION_RISCV_Zmmul = false) generate
226
    cp_result(1) <= (others => '0');
227 71 zero_gravi
    cp_valid(1)  <= '0';
228 61 zero_gravi
  end generate;
229
 
230
 
231 66 zero_gravi
  -- Co-Processor 2: Bit-Manipulation Unit ('B' Extension) ----------------------------------
232 61 zero_gravi
  -- -------------------------------------------------------------------------------------------
233 63 zero_gravi
  neorv32_cpu_cp_bitmanip_inst_true:
234 66 zero_gravi
  if (CPU_EXTENSION_RISCV_B = true) generate
235 63 zero_gravi
    neorv32_cpu_cp_bitmanip_inst: neorv32_cpu_cp_bitmanip
236
    generic map (
237
      FAST_SHIFT_EN => FAST_SHIFT_EN -- use barrel shifter for shift operations
238
    )
239
    port map (
240
      -- global control --
241 66 zero_gravi
      clk_i   => clk_i,        -- global clock, rising edge
242
      rstn_i  => rstn_i,       -- global reset, low-active, async
243
      ctrl_i  => ctrl_i,       -- main control bus
244
      start_i => cp_start(2),  -- trigger operation
245 63 zero_gravi
      -- data input --
246 66 zero_gravi
      cmp_i   => cmp,          -- comparator status
247
      rs1_i   => rs1_i,        -- rf source 1
248
      rs2_i   => rs2_i,        -- rf source 2
249
      shamt_i => opb(index_size_f(data_width_c)-1 downto 0), -- shift amount
250 63 zero_gravi
      -- result and status --
251 66 zero_gravi
      res_o   => cp_result(2), -- operation result
252
      valid_o => cp_valid(2)   -- data output valid
253 63 zero_gravi
    );
254
  end generate;
255 61 zero_gravi
 
256 63 zero_gravi
  neorv32_cpu_cp_bitmanip_inst_false:
257 66 zero_gravi
  if (CPU_EXTENSION_RISCV_B = false) generate
258 63 zero_gravi
    cp_result(2) <= (others => '0');
259 71 zero_gravi
    cp_valid(2)  <= '0';
260 63 zero_gravi
  end generate;
261 61 zero_gravi
 
262 63 zero_gravi
 
263 61 zero_gravi
  -- Co-Processor 3: Single-Precision Floating-Point Unit ('Zfinx' Extension) ---------------
264
  -- -------------------------------------------------------------------------------------------
265
  neorv32_cpu_cp_fpu_inst_true:
266
  if (CPU_EXTENSION_RISCV_Zfinx = true) generate
267
    neorv32_cpu_cp_fpu_inst: neorv32_cpu_cp_fpu
268
    port map (
269
      -- global control --
270 66 zero_gravi
      clk_i    => clk_i,        -- global clock, rising edge  
271 61 zero_gravi
      rstn_i   => rstn_i,       -- global reset, low-active, async
272
      ctrl_i   => ctrl_i,       -- main control bus
273
      start_i  => cp_start(3),  -- trigger operation
274
      -- data input --
275 65 zero_gravi
      cmp_i    => cmp,          -- comparator status
276 61 zero_gravi
      rs1_i    => rs1_i,        -- rf source 1
277
      rs2_i    => rs2_i,        -- rf source 2
278
      -- result and status --
279
      res_o    => cp_result(3), -- operation result
280
      fflags_o => fpu_flags_o,  -- exception flags
281
      valid_o  => cp_valid(3)   -- data output valid
282
    );
283
  end generate;
284
 
285
  neorv32_cpu_cp_fpu_inst_false:
286
  if (CPU_EXTENSION_RISCV_Zfinx = false) generate
287
    cp_result(3) <= (others => '0');
288
    fpu_flags_o  <= (others => '0');
289 71 zero_gravi
    cp_valid(3)  <= '0';
290 61 zero_gravi
  end generate;
291
 
292
 
293 72 zero_gravi
  -- Co-Processor 4: Custom (Instructions) Functions Unit ('Zxcfu' Extension) ---------------
294 71 zero_gravi
  -- -------------------------------------------------------------------------------------------
295 72 zero_gravi
  neorv32_cpu_cp_cfu_inst_true:
296
  if (CPU_EXTENSION_RISCV_Zxcfu = true) generate
297
    neorv32_cpu_cp_cfu_inst: neorv32_cpu_cp_cfu
298
    port map (
299
      -- global control --
300
      clk_i   => clk_i,        -- global clock, rising edge
301
      rstn_i  => rstn_i,       -- global reset, low-active, async
302
      ctrl_i  => ctrl_i,       -- main control bus
303
      start_i => cp_start(4),  -- trigger operation
304
      -- data input --
305
      rs1_i   => rs1_i,        -- rf source 1
306
      rs2_i   => rs2_i,        -- rf source 2
307
      -- result and status --
308
      res_o   => cp_result(4), -- operation result
309
      valid_o => cp_valid(4)   -- data output valid
310
    );
311
  end generate;
312 71 zero_gravi
 
313 72 zero_gravi
  neorv32_cpu_cp_cfu_inst_false:
314
  if (CPU_EXTENSION_RISCV_Zxcfu = false) generate
315
    cp_result(4) <= (others => '0');
316
    cp_valid(4)  <= '0';
317
  end generate;
318 71 zero_gravi
 
319 72 zero_gravi
 
320 71 zero_gravi
  -- Co-Processor 5: Reserved ---------------------------------------------------------------
321
  -- -------------------------------------------------------------------------------------------
322
  cp_result(5) <= (others => '0');
323
  cp_valid(5)  <= '0';
324
 
325
 
326
  -- Co-Processor 6: Reserved ---------------------------------------------------------------
327
  -- -------------------------------------------------------------------------------------------
328
  cp_result(6) <= (others => '0');
329
  cp_valid(6)  <= '0';
330
 
331
 
332
  -- Co-Processor 7: Reserved ---------------------------------------------------------------
333
  -- -------------------------------------------------------------------------------------------
334
  cp_result(7) <= (others => '0');
335
  cp_valid(7)  <= '0';
336
 
337
 
338 2 zero_gravi
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.