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

Subversion Repositories neorv32

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

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 47 zero_gravi
-- # Main data and address ALU and co-processor interface/arbiter.                                 #
5 2 zero_gravi
-- # ********************************************************************************************* #
6
-- # BSD 3-Clause License                                                                          #
7
-- #                                                                                               #
8 44 zero_gravi
-- # Copyright (c) 2021, 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 62 zero_gravi
    CPU_EXTENSION_RISCV_M     : boolean; -- implement mul/div extension?
48
    CPU_EXTENSION_RISCV_Zmmul : boolean; -- implement multiply-only M sub-extension?
49
    CPU_EXTENSION_RISCV_Zfinx : boolean; -- implement 32-bit floating-point extension (using INT reg!)
50 61 zero_gravi
    -- Extension Options --
51 62 zero_gravi
    FAST_MUL_EN               : boolean; -- use DSPs for M extension's multiplier
52
    FAST_SHIFT_EN             : boolean  -- use barrel shifter for shift operations
53 11 zero_gravi
  );
54 2 zero_gravi
  port (
55
    -- global control --
56
    clk_i       : in  std_ulogic; -- global clock, rising edge
57
    rstn_i      : in  std_ulogic; -- global reset, low-active, async
58
    ctrl_i      : in  std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
59
    -- data input --
60
    rs1_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
61
    rs2_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
62
    pc2_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- delayed PC
63
    imm_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- immediate
64 61 zero_gravi
    csr_i       : in  std_ulogic_vector(data_width_c-1 downto 0); -- CSR read data
65
    cmp_i       : in  std_ulogic_vector(1 downto 0); -- comparator status
66 2 zero_gravi
    -- data output --
67
    res_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
68 36 zero_gravi
    add_o       : out std_ulogic_vector(data_width_c-1 downto 0); -- address computation result
69 61 zero_gravi
    fpu_flags_o : out std_ulogic_vector(4 downto 0); -- FPU exception flags
70 2 zero_gravi
    -- status --
71 61 zero_gravi
    idone_o     : out std_ulogic -- iterative processing units done?
72 2 zero_gravi
  );
73
end neorv32_cpu_alu;
74
 
75
architecture neorv32_cpu_cpu_rtl of neorv32_cpu_alu is
76
 
77
  -- operands --
78 29 zero_gravi
  signal opa, opb : std_ulogic_vector(data_width_c-1 downto 0);
79 2 zero_gravi
 
80
  -- results --
81 36 zero_gravi
  signal addsub_res : std_ulogic_vector(data_width_c downto 0);
82 39 zero_gravi
  --
83 29 zero_gravi
  signal cp_res     : std_ulogic_vector(data_width_c-1 downto 0);
84 39 zero_gravi
  signal arith_res  : std_ulogic_vector(data_width_c-1 downto 0);
85
  signal logic_res  : std_ulogic_vector(data_width_c-1 downto 0);
86 2 zero_gravi
 
87 19 zero_gravi
  -- co-processor arbiter and interface --
88
  type cp_ctrl_t is record
89 55 zero_gravi
    cmd     : std_ulogic;
90
    cmd_ff  : std_ulogic;
91 61 zero_gravi
    start   : std_ulogic;
92 55 zero_gravi
    busy    : std_ulogic;
93
    timeout : std_ulogic_vector(9 downto 0);
94 19 zero_gravi
  end record;
95
  signal cp_ctrl : cp_ctrl_t;
96 2 zero_gravi
 
97 61 zero_gravi
  -- co-processor interface --
98
  signal cp_start  : std_ulogic_vector(3 downto 0); -- trigger co-processor i
99
  signal cp_valid  : std_ulogic_vector(3 downto 0); -- co-processor i done
100
  signal cp_result : cp_data_if_t; -- co-processor result
101
 
102 2 zero_gravi
begin
103
 
104
  -- Operand Mux ----------------------------------------------------------------------------
105
  -- -------------------------------------------------------------------------------------------
106 36 zero_gravi
  opa <= pc2_i when (ctrl_i(ctrl_alu_opa_mux_c) = '1') else rs1_i; -- operand a (first ALU input operand), only required for arithmetic ops
107 29 zero_gravi
  opb <= imm_i when (ctrl_i(ctrl_alu_opb_mux_c) = '1') else rs2_i; -- operand b (second ALU input operand)
108 2 zero_gravi
 
109
 
110 61 zero_gravi
  -- Binary Adder/Subtracter ----------------------------------------------------------------
111 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
112 29 zero_gravi
  binary_arithmetic_core: process(ctrl_i, opa, opb)
113
    variable cin_v  : std_ulogic_vector(0 downto 0);
114
    variable op_a_v : std_ulogic_vector(data_width_c downto 0);
115
    variable op_b_v : std_ulogic_vector(data_width_c downto 0);
116
    variable op_y_v : std_ulogic_vector(data_width_c downto 0);
117
    variable res_v  : std_ulogic_vector(data_width_c downto 0);
118
  begin
119
    -- operand sign-extension --
120
    op_a_v := (opa(opa'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & opa;
121
    op_b_v := (opb(opb'left) and (not ctrl_i(ctrl_alu_unsigned_c))) & opb;
122
    -- add/sub(slt) select --
123
    if (ctrl_i(ctrl_alu_addsub_c) = '1') then -- subtraction
124
      op_y_v   := not op_b_v;
125
      cin_v(0) := '1';
126 36 zero_gravi
    else -- addition
127 29 zero_gravi
      op_y_v   := op_b_v;
128
      cin_v(0) := '0';
129
    end if;
130 36 zero_gravi
    -- adder core (result + carry/borrow) --
131
    addsub_res <= std_ulogic_vector(unsigned(op_a_v) + unsigned(op_y_v) + unsigned(cin_v(0 downto 0)));
132 29 zero_gravi
  end process binary_arithmetic_core;
133
 
134 36 zero_gravi
  -- direct output of address result --
135
  add_o <= addsub_res(data_width_c-1 downto 0);
136 29 zero_gravi
 
137 39 zero_gravi
  -- ALU arithmetic logic core --
138
  arithmetic_core: process(ctrl_i, addsub_res)
139
  begin
140
    if (ctrl_i(ctrl_alu_arith_c) = alu_arith_cmd_addsub_c) then -- ADD/SUB
141
      arith_res <= addsub_res(data_width_c-1 downto 0);
142
    else -- SLT
143
      arith_res <= (others => '0');
144
      arith_res(0) <= addsub_res(addsub_res'left); -- => carry/borrow
145
    end if;
146
  end process arithmetic_core;
147 36 zero_gravi
 
148 39 zero_gravi
 
149 47 zero_gravi
  -- Co-Processor Arbiter -------------------------------------------------------------------
150 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
151 47 zero_gravi
  -- Interface:
152
  -- Co-processor "valid" signal has to be asserted (for one cycle) one cycle before asserting output data
153
  -- Co-processor "output data" has to be always zero unless co-processor was explicitly triggered
154 19 zero_gravi
  cp_arbiter: process(rstn_i, clk_i)
155 2 zero_gravi
  begin
156
    if (rstn_i = '0') then
157 55 zero_gravi
      cp_ctrl.cmd_ff  <= '0';
158
      cp_ctrl.busy    <= '0';
159
      cp_ctrl.timeout <= (others => '0');
160 2 zero_gravi
    elsif rising_edge(clk_i) then
161 40 zero_gravi
      cp_ctrl.cmd_ff <= cp_ctrl.cmd;
162 61 zero_gravi
      -- timeout counter --
163
      if (cp_ctrl.start = '1') then
164
        cp_ctrl.busy <= '1';
165
      elsif (or_reduce_f(cp_valid) = '1') then
166 40 zero_gravi
        cp_ctrl.busy <= '0';
167 2 zero_gravi
      end if;
168 55 zero_gravi
      if (cp_ctrl.busy = '1') and (cp_timeout_en_c = true) then
169
        cp_ctrl.timeout <= std_ulogic_vector(unsigned(cp_ctrl.timeout) + 1);
170
      else
171
        cp_ctrl.timeout <= (others => '0');
172
      end if;
173 61 zero_gravi
      if (cp_ctrl.timeout(cp_ctrl.timeout'left) = '1') and (cp_timeout_en_c = true) then -- timeout
174
        assert false report "NEORV32 CPU CO-PROCESSOR TIMEOUT ERROR!" severity warning;
175
      end if;
176 2 zero_gravi
    end if;
177 19 zero_gravi
  end process cp_arbiter;
178 2 zero_gravi
 
179
  -- is co-processor operation? --
180 39 zero_gravi
  cp_ctrl.cmd   <= '1' when (ctrl_i(ctrl_alu_func1_c downto ctrl_alu_func0_c) = alu_func_cmd_copro_c) else '0';
181 29 zero_gravi
  cp_ctrl.start <= '1' when (cp_ctrl.cmd = '1') and (cp_ctrl.cmd_ff = '0') else '0';
182 2 zero_gravi
 
183 61 zero_gravi
  -- co-processor select / star trigger --
184
  cp_start(0) <= '1' when (cp_ctrl.start = '1') and (ctrl_i(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) = "00") else '0';
185
  cp_start(1) <= '1' when (cp_ctrl.start = '1') and (ctrl_i(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) = "01") else '0';
186
  cp_start(2) <= '1' when (cp_ctrl.start = '1') and (ctrl_i(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) = "10") else '0';
187
  cp_start(3) <= '1' when (cp_ctrl.start = '1') and (ctrl_i(ctrl_cp_id_msb_c downto ctrl_cp_id_lsb_c) = "11") else '0';
188 2 zero_gravi
 
189 61 zero_gravi
  -- co-processor operation done? --
190
  idone_o <= or_reduce_f(cp_valid);
191 39 zero_gravi
 
192 49 zero_gravi
  -- co-processor result - only the *actually selected* co-processor may output data != 0 --
193 61 zero_gravi
  cp_res <= cp_result(0) or cp_result(1) or cp_result(2) or cp_result(3);
194 24 zero_gravi
 
195
 
196 39 zero_gravi
  -- ALU Logic Core -------------------------------------------------------------------------
197
  -- -------------------------------------------------------------------------------------------
198
  alu_logic_core: process(ctrl_i, rs1_i, opb)
199
  begin
200
    case ctrl_i(ctrl_alu_logic1_c downto ctrl_alu_logic0_c) is
201
      when alu_logic_cmd_movb_c => logic_res <= opb; -- (default)
202
      when alu_logic_cmd_xor_c  => logic_res <= rs1_i xor opb; -- only rs1 required for logic ops (opa would also contain pc)
203
      when alu_logic_cmd_or_c   => logic_res <= rs1_i or  opb;
204
      when alu_logic_cmd_and_c  => logic_res <= rs1_i and opb;
205
      when others               => logic_res <= opb; -- undefined
206
    end case;
207
  end process alu_logic_core;
208
 
209
 
210 2 zero_gravi
  -- ALU Function Select --------------------------------------------------------------------
211
  -- -------------------------------------------------------------------------------------------
212 61 zero_gravi
  alu_function_mux: process(ctrl_i, arith_res, logic_res, csr_i, cp_res)
213 2 zero_gravi
  begin
214 39 zero_gravi
    case ctrl_i(ctrl_alu_func1_c downto ctrl_alu_func0_c) is
215
      when alu_func_cmd_arith_c => res_o <= arith_res; -- (default)
216
      when alu_func_cmd_logic_c => res_o <= logic_res;
217 61 zero_gravi
      when alu_func_cmd_csrr_c  => res_o <= csr_i;
218 39 zero_gravi
      when alu_func_cmd_copro_c => res_o <= cp_res;
219
      when others               => res_o <= arith_res; -- undefined
220 2 zero_gravi
    end case;
221
  end process alu_function_mux;
222
 
223
 
224 61 zero_gravi
  -- **************************************************************************************************************************
225
  -- Co-Processors
226
  -- **************************************************************************************************************************
227
 
228
  -- Co-Processor 0: Shifter (CPU Core ISA) --------------------------------------------------
229 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
230 61 zero_gravi
    neorv32_cpu_cp_shifter_inst: neorv32_cpu_cp_shifter
231
    generic map (
232
      FAST_SHIFT_EN => FAST_SHIFT_EN -- use barrel shifter for shift operations
233
    )
234
    port map (
235
      -- global control --
236
      clk_i   => clk_i,           -- global clock, rising edge
237
      rstn_i  => rstn_i,          -- global reset, low-active, async
238
      ctrl_i  => ctrl_i,          -- main control bus
239
      start_i => cp_start(0),     -- trigger operation
240
      -- data input --
241
      rs1_i   => rs1_i,           -- rf source 1
242
      rs2_i   => rs2_i,           -- rf source 2
243
      imm_i   => imm_i,           -- immediate
244
      -- result and status --
245
      res_o   => cp_result(0),    -- operation result
246
      valid_o => cp_valid(0)      -- data output valid
247
    );
248 2 zero_gravi
 
249
 
250 61 zero_gravi
  -- Co-Processor 1: Integer Multiplication/Division ('M' Extension) ------------------------
251
  -- -------------------------------------------------------------------------------------------
252
  neorv32_cpu_cp_muldiv_inst_true:
253
  if (CPU_EXTENSION_RISCV_M = true) or (CPU_EXTENSION_RISCV_Zmmul = true) generate
254
    neorv32_cpu_cp_muldiv_inst: neorv32_cpu_cp_muldiv
255
    generic map (
256
      FAST_MUL_EN => FAST_MUL_EN,          -- use DSPs for faster multiplication
257
      DIVISION_EN => CPU_EXTENSION_RISCV_M -- implement divider hardware
258
    )
259
    port map (
260
      -- global control --
261
      clk_i   => clk_i,           -- global clock, rising edge
262
      rstn_i  => rstn_i,          -- global reset, low-active, async
263
      ctrl_i  => ctrl_i,          -- main control bus
264
      start_i => cp_start(1),     -- trigger operation
265
      -- data input --
266
      rs1_i   => rs1_i,           -- rf source 1
267
      rs2_i   => rs2_i,           -- rf source 2
268
      -- result and status --
269
      res_o   => cp_result(1),    -- operation result
270
      valid_o => cp_valid(1)      -- data output valid
271
    );
272
  end generate;
273
 
274
  neorv32_cpu_cp_muldiv_inst_false:
275
  if (CPU_EXTENSION_RISCV_M = false) and (CPU_EXTENSION_RISCV_Zmmul = false) generate
276
    cp_result(1) <= (others => '0');
277
    cp_valid(1)  <= cp_start(1); -- to make sure CPU does not get stalled if there is an accidental access
278
  end generate;
279
 
280
 
281
  -- Co-Processor 2: reserved ---------------------------------------------------------------
282
  -- -------------------------------------------------------------------------------------------
283
  cp_result(2) <= (others => '0');
284
  cp_valid(2)  <= cp_start(2); -- to make sure CPU does not get stalled if there is an accidental access
285
 
286
 
287
  -- Co-Processor 3: Single-Precision Floating-Point Unit ('Zfinx' Extension) ---------------
288
  -- -------------------------------------------------------------------------------------------
289
  neorv32_cpu_cp_fpu_inst_true:
290
  if (CPU_EXTENSION_RISCV_Zfinx = true) generate
291
    neorv32_cpu_cp_fpu_inst: neorv32_cpu_cp_fpu
292
    port map (
293
      -- global control --
294
      clk_i    => clk_i,        -- global clock, rising edge
295
      rstn_i   => rstn_i,       -- global reset, low-active, async
296
      ctrl_i   => ctrl_i,       -- main control bus
297
      start_i  => cp_start(3),  -- trigger operation
298
      -- data input --
299
      cmp_i    => cmp_i,        -- comparator status
300
      rs1_i    => rs1_i,        -- rf source 1
301
      rs2_i    => rs2_i,        -- rf source 2
302
      -- result and status --
303
      res_o    => cp_result(3), -- operation result
304
      fflags_o => fpu_flags_o,  -- exception flags
305
      valid_o  => cp_valid(3)   -- data output valid
306
    );
307
  end generate;
308
 
309
  neorv32_cpu_cp_fpu_inst_false:
310
  if (CPU_EXTENSION_RISCV_Zfinx = false) generate
311
    cp_result(3) <= (others => '0');
312
    fpu_flags_o  <= (others => '0');
313
    cp_valid(3)  <= cp_start(3); -- to make sure CPU does not get stalled if there is an accidental access
314
  end generate;
315
 
316
 
317 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.