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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_cpu_cp_muldiv.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 61 zero_gravi
-- # << NEORV32 - CPU Co-Processor: Integer Multiplier/Divider Unit (RISC-V "M" Extension) >>      #
3 2 zero_gravi
-- # ********************************************************************************************* #
4 56 zero_gravi
-- # Multiplier and Divider unit. Implements the RISC-V M CPU extension.                           #
5
-- #                                                                                               #
6 61 zero_gravi
-- # Multiplier core (signed/unsigned) uses classical serial algorithm. Unit latency: 31+3 cycles  #
7 56 zero_gravi
-- # Divider core (unsigned) uses classical serial algorithm. Unit latency: 32+4 cycles            #
8
-- #                                                                                               #
9
-- # Multiplications can be mapped to DSP blocks (faster!) when FAST_MUL_EN = true.                #
10 2 zero_gravi
-- # ********************************************************************************************* #
11
-- # BSD 3-Clause License                                                                          #
12
-- #                                                                                               #
13 44 zero_gravi
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
14 2 zero_gravi
-- #                                                                                               #
15
-- # Redistribution and use in source and binary forms, with or without modification, are          #
16
-- # permitted provided that the following conditions are met:                                     #
17
-- #                                                                                               #
18
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
19
-- #    conditions and the following disclaimer.                                                   #
20
-- #                                                                                               #
21
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
22
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
23
-- #    provided with the distribution.                                                            #
24
-- #                                                                                               #
25
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
26
-- #    endorse or promote products derived from this software without specific prior written      #
27
-- #    permission.                                                                                #
28
-- #                                                                                               #
29
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
30
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
31
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
32
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
33
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
34
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
35
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
36
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
37
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
38
-- # ********************************************************************************************* #
39
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
40
-- #################################################################################################
41
 
42
library ieee;
43
use ieee.std_logic_1164.all;
44
use ieee.numeric_std.all;
45
 
46
library neorv32;
47
use neorv32.neorv32_package.all;
48
 
49
entity neorv32_cpu_cp_muldiv is
50 19 zero_gravi
  generic (
51 62 zero_gravi
    FAST_MUL_EN : boolean; -- use DSPs for faster multiplication
52
    DIVISION_EN : boolean  -- implement divider hardware
53 19 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 36 zero_gravi
    start_i : in  std_ulogic; -- trigger operation
60 2 zero_gravi
    -- data input --
61
    rs1_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 1
62
    rs2_i   : in  std_ulogic_vector(data_width_c-1 downto 0); -- rf source 2
63
    -- result and status --
64
    res_o   : out std_ulogic_vector(data_width_c-1 downto 0); -- operation result
65
    valid_o : out std_ulogic -- data output valid
66
  );
67
end neorv32_cpu_cp_muldiv;
68
 
69
architecture neorv32_cpu_cp_muldiv_rtl of neorv32_cpu_cp_muldiv is
70
 
71 44 zero_gravi
  -- operations --
72
  constant cp_op_mul_c    : std_ulogic_vector(2 downto 0) := "000"; -- mul
73
  constant cp_op_mulh_c   : std_ulogic_vector(2 downto 0) := "001"; -- mulh
74
  constant cp_op_mulhsu_c : std_ulogic_vector(2 downto 0) := "010"; -- mulhsu
75
  constant cp_op_mulhu_c  : std_ulogic_vector(2 downto 0) := "011"; -- mulhu
76
  constant cp_op_div_c    : std_ulogic_vector(2 downto 0) := "100"; -- div
77
  constant cp_op_divu_c   : std_ulogic_vector(2 downto 0) := "101"; -- divu
78
  constant cp_op_rem_c    : std_ulogic_vector(2 downto 0) := "110"; -- rem
79
  constant cp_op_remu_c   : std_ulogic_vector(2 downto 0) := "111"; -- remu
80
 
81 2 zero_gravi
  -- controller --
82 56 zero_gravi
  type state_t is (IDLE, DIV_PREPROCESS, PROCESSING, FINALIZE, COMPLETED);
83 2 zero_gravi
  signal state         : state_t;
84
  signal cnt           : std_ulogic_vector(4 downto 0);
85
  signal cp_op         : std_ulogic_vector(2 downto 0); -- operation to execute
86 39 zero_gravi
  signal cp_op_ff      : std_ulogic_vector(2 downto 0); -- operation that was executed
87 56 zero_gravi
  signal start_div     : std_ulogic;
88
  signal start_mul     : std_ulogic;
89 2 zero_gravi
  signal operation     : std_ulogic;
90 56 zero_gravi
  signal div_opx       : std_ulogic_vector(data_width_c-1 downto 0);
91
  signal div_opy       : std_ulogic_vector(data_width_c-1 downto 0);
92
  signal rs1_is_signed : std_ulogic;
93
  signal rs2_is_signed : std_ulogic;
94 6 zero_gravi
  signal opy_is_zero   : std_ulogic;
95 2 zero_gravi
  signal div_res_corr  : std_ulogic;
96 39 zero_gravi
  signal valid         : std_ulogic;
97 2 zero_gravi
 
98
  -- divider core --
99
  signal remainder        : std_ulogic_vector(data_width_c-1 downto 0);
100
  signal quotient         : std_ulogic_vector(data_width_c-1 downto 0);
101
  signal div_sub          : std_ulogic_vector(data_width_c   downto 0);
102
  signal div_sign_comp_in : std_ulogic_vector(data_width_c-1 downto 0);
103
  signal div_sign_comp    : std_ulogic_vector(data_width_c-1 downto 0);
104
  signal div_res          : std_ulogic_vector(data_width_c-1 downto 0);
105
 
106
  -- multiplier core --
107
  signal mul_product    : std_ulogic_vector(63 downto 0);
108 12 zero_gravi
  signal mul_do_add     : std_ulogic_vector(data_width_c downto 0);
109 2 zero_gravi
  signal mul_sign_cycle : std_ulogic;
110
  signal mul_p_sext     : std_ulogic;
111 19 zero_gravi
  signal mul_op_x       : signed(32 downto 0); -- for using DSPs
112
  signal mul_op_y       : signed(32 downto 0); -- for using DSPs
113
  signal mul_buf_ff     : signed(65 downto 0); -- for using DSPs
114 2 zero_gravi
 
115
begin
116
 
117
  -- Co-Processor Controller ----------------------------------------------------------------
118
  -- -------------------------------------------------------------------------------------------
119
  coprocessor_ctrl: process(rstn_i, clk_i)
120
  begin
121
    if (rstn_i = '0') then
122
      state        <= IDLE;
123 56 zero_gravi
      div_opx      <= (others => def_rst_val_c);
124
      div_opy      <= (others => def_rst_val_c);
125
      cnt          <= (others => def_rst_val_c);
126
      cp_op_ff     <= (others => def_rst_val_c);
127
      start_div    <= '0';
128 39 zero_gravi
      valid        <= '0';
129 56 zero_gravi
      div_res_corr <= def_rst_val_c;
130
      opy_is_zero  <= def_rst_val_c;
131 2 zero_gravi
    elsif rising_edge(clk_i) then
132
      -- defaults --
133 56 zero_gravi
      start_div <= '0';
134
      valid     <= '0';
135 2 zero_gravi
 
136
      -- FSM --
137
      case state is
138
        when IDLE =>
139 56 zero_gravi
          cp_op_ff <= cp_op;
140 19 zero_gravi
          if (start_i = '1') then
141 61 zero_gravi
            if (operation = '1') and (DIVISION_EN = true) then -- division
142 56 zero_gravi
              cnt <= "11111";
143
              state <= DIV_PREPROCESS;
144
            else
145
              cnt <= "11110";
146
              if (FAST_MUL_EN = true) then
147
                state <= FINALIZE;
148
              else
149
                state <= PROCESSING;
150
              end if;
151
            end if;
152 2 zero_gravi
          end if;
153
 
154 56 zero_gravi
        when DIV_PREPROCESS =>
155 61 zero_gravi
          if (DIVISION_EN = true) then
156
            -- check rlevatn input signs --
157
            if (cp_op = cp_op_div_c) then -- result sign compensation for div?
158
              div_res_corr <= rs1_i(rs1_i'left) xor rs2_i(rs2_i'left);
159
            elsif (cp_op = cp_op_rem_c) then -- result sign compensation for rem?
160
              div_res_corr <= rs1_i(rs1_i'left);
161
            else
162
              div_res_corr <= '0';
163
            end if;
164
            -- divide by zero? --
165
            opy_is_zero <= not or_reduce_f(rs2_i); -- set if rs2 = 0
166
            -- abs(rs1) --
167
            if ((rs1_i(rs1_i'left) and rs1_is_signed) = '1') then -- signed division?
168
              div_opx <= std_ulogic_vector(0 - unsigned(rs1_i)); -- make positive
169
            else
170
              div_opx <= rs1_i;
171
            end if;
172
            -- abs(rs2) --
173
            if ((rs2_i(rs2_i'left) and rs2_is_signed) = '1') then -- signed division?
174
              div_opy <= std_ulogic_vector(0 - unsigned(rs2_i)); -- make positive
175
            else
176
              div_opy <= rs2_i;
177
            end if;
178
            --
179
            start_div <= '1';
180
            state     <= PROCESSING;
181 2 zero_gravi
          else
182 61 zero_gravi
            state <= IDLE;
183 2 zero_gravi
          end if;
184
 
185
        when PROCESSING =>
186
          cnt <= std_ulogic_vector(unsigned(cnt) - 1);
187
          if (cnt = "00000") then
188
            state <= FINALIZE;
189
          end if;
190
 
191
        when FINALIZE =>
192
          state <= COMPLETED;
193
 
194
        when COMPLETED =>
195 39 zero_gravi
          valid <= '1';
196
          state <= IDLE;
197 2 zero_gravi
      end case;
198
    end if;
199
  end process coprocessor_ctrl;
200
 
201 36 zero_gravi
  -- co-processor command --
202
  cp_op <= ctrl_i(ctrl_ir_funct3_2_c downto ctrl_ir_funct3_0_c);
203
 
204 56 zero_gravi
  -- operation: 0=mul, 1=div --
205
  operation <= '1' when (cp_op(2) = '1') else '0';
206 2 zero_gravi
 
207
  -- opx (rs1) signed? --
208 56 zero_gravi
  rs1_is_signed <= '1' when (cp_op = cp_op_mulh_c) or (cp_op = cp_op_mulhsu_c) or (cp_op = cp_op_div_c) or (cp_op = cp_op_rem_c) else '0';
209 2 zero_gravi
 
210
  -- opy (rs2) signed? --
211 56 zero_gravi
  rs2_is_signed <= '1' when (cp_op = cp_op_mulh_c) or (cp_op = cp_op_div_c) or (cp_op = cp_op_rem_c) else '0';
212 2 zero_gravi
 
213 56 zero_gravi
  -- start MUL operation (do it fast!) --
214
  start_mul <= '1' when (state = IDLE) and (start_i = '1') and (operation = '0') else '0';
215 2 zero_gravi
 
216 56 zero_gravi
 
217 36 zero_gravi
  -- Multiplier Core (signed/unsigned) ------------------------------------------------------
218 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
219 61 zero_gravi
  -- iterative multiplication (bit-serial) --
220
  multiplier_core_serial:
221
  if (FAST_MUL_EN = false) generate
222
    multiplier_core: process(rstn_i, clk_i)
223
    begin
224
      if (rstn_i = '0') then
225
        mul_product <= (others => def_rst_val_c);
226
      elsif rising_edge(clk_i) then
227 56 zero_gravi
        if (start_mul = '1') then -- start new multiplication
228 61 zero_gravi
          mul_product(63 downto 32) <= (others => '0');
229
          mul_product(31 downto 00) <= rs2_i;
230 56 zero_gravi
        elsif (state = PROCESSING) or (state = FINALIZE) then -- processing step or sign-finalization step
231 61 zero_gravi
          mul_product(63 downto 31) <= mul_do_add(32 downto 0);
232
          mul_product(30 downto 00) <= mul_product(31 downto 1);
233 12 zero_gravi
        end if;
234 56 zero_gravi
      end if;
235 61 zero_gravi
    end process multiplier_core;
236
  end generate;
237 56 zero_gravi
 
238 61 zero_gravi
  -- parallel multiplication --
239
  multiplier_core_dsp:
240
  if (FAST_MUL_EN = true) generate
241
    multiplier_core: process(clk_i)
242
    begin
243
      if rising_edge(clk_i) then
244 56 zero_gravi
        if (start_mul = '1') then
245
          mul_op_x <= signed((rs1_i(rs1_i'left) and rs1_is_signed) & rs1_i);
246
          mul_op_y <= signed((rs2_i(rs2_i'left) and rs2_is_signed) & rs2_i);
247 12 zero_gravi
        end if;
248 61 zero_gravi
        mul_buf_ff  <= mul_op_x * mul_op_y;
249
        mul_product <= std_ulogic_vector(mul_buf_ff(63 downto 0)); -- let the register balancing do the magic here
250 2 zero_gravi
      end if;
251 61 zero_gravi
    end process multiplier_core;
252
  end generate;
253 2 zero_gravi
 
254 61 zero_gravi
  -- do another addition (bit-serial) --
255 56 zero_gravi
  mul_update: process(mul_product, mul_sign_cycle, mul_p_sext, rs1_is_signed, rs1_i)
256 2 zero_gravi
  begin
257 56 zero_gravi
    -- current bit of rs2_i to take care of --
258 12 zero_gravi
    if (mul_product(0) = '1') then -- multiply with 1
259
      if (mul_sign_cycle = '1') then -- for signed operations only: take care of negative weighted MSB -> multiply with -1
260 56 zero_gravi
        mul_do_add <= std_ulogic_vector(unsigned(mul_p_sext & mul_product(63 downto 32)) - unsigned((rs1_i(rs1_i'left) and rs1_is_signed) & rs1_i));
261 12 zero_gravi
      else -- multiply with +1
262 56 zero_gravi
        mul_do_add <= std_ulogic_vector(unsigned(mul_p_sext & mul_product(63 downto 32)) + unsigned((rs1_i(rs1_i'left) and rs1_is_signed) & rs1_i));
263 2 zero_gravi
      end if;
264 12 zero_gravi
    else -- multiply with 0
265 2 zero_gravi
      mul_do_add <= mul_p_sext & mul_product(63 downto 32);
266
    end if;
267
  end process mul_update;
268
 
269
  -- sign control --
270 56 zero_gravi
  mul_sign_cycle <= rs2_is_signed when (state = FINALIZE) else '0';
271
  mul_p_sext     <= mul_product(mul_product'left) and rs1_is_signed;
272 2 zero_gravi
 
273
 
274 12 zero_gravi
  -- Divider Core (unsigned) ----------------------------------------------------------------
275 2 zero_gravi
  -- -------------------------------------------------------------------------------------------
276 61 zero_gravi
  divider_core_serial:
277
  if (DIVISION_EN = true) generate
278
    divider_core: process(rstn_i, clk_i)
279
    begin
280
      if (rstn_i = '0') then
281
        quotient  <= (others => def_rst_val_c);
282
        remainder <= (others => def_rst_val_c);
283
      elsif rising_edge(clk_i) then
284
        if (start_div = '1') then -- start new division
285
          quotient  <= div_opx;
286
          remainder <= (others => '0');
287
        elsif (state = PROCESSING) or (state = FINALIZE) then -- running?
288
          quotient <= quotient(30 downto 0) & (not div_sub(32));
289
          if (div_sub(32) = '0') then -- still overflowing
290
            remainder <= div_sub(31 downto 0);
291
          else -- underflow
292
            remainder <= remainder(30 downto 0) & quotient(31);
293
          end if;
294 2 zero_gravi
        end if;
295
      end if;
296 61 zero_gravi
    end process divider_core;
297 2 zero_gravi
 
298 61 zero_gravi
    -- try another subtraction --
299
    div_sub <= std_ulogic_vector(unsigned('0' & remainder(30 downto 0) & quotient(31)) - unsigned('0' & div_opy));
300 2 zero_gravi
 
301 61 zero_gravi
    -- result sign compensation --
302
    div_sign_comp_in <= quotient when (cp_op = cp_op_div_c) else remainder;
303
    div_sign_comp    <= std_ulogic_vector(0 - unsigned(div_sign_comp_in));
304
    div_res          <= div_sign_comp when (div_res_corr = '1') and (opy_is_zero = '0') else div_sign_comp_in;
305
  end generate;
306 2 zero_gravi
 
307 61 zero_gravi
  -- no divider --
308
  divider_core_serial_none:
309
  if (DIVISION_EN = false) generate
310
    remainder <= (others => '-');
311
    quotient  <= (others => '-');
312
    div_res   <= (others => '-');
313
  end generate;
314 2 zero_gravi
 
315 61 zero_gravi
 
316 2 zero_gravi
  -- Data Output ----------------------------------------------------------------------------
317
  -- -------------------------------------------------------------------------------------------
318 56 zero_gravi
  operation_result: process(rstn_i, clk_i)
319 2 zero_gravi
  begin
320 56 zero_gravi
    if (rstn_i = '0') then
321
      res_o <= (others => def_rst_val_c);
322
    elsif rising_edge(clk_i) then
323 47 zero_gravi
      res_o <= (others => '0');
324
      if (valid = '1') then
325
        case cp_op_ff is
326
          when cp_op_mul_c =>
327
            res_o <= mul_product(31 downto 00);
328
          when cp_op_mulh_c | cp_op_mulhsu_c | cp_op_mulhu_c =>
329
            res_o <= mul_product(63 downto 32);
330
          when cp_op_div_c =>
331 61 zero_gravi
            if (DIVISION_EN = true) then res_o <= div_res; else NULL; end if;
332 47 zero_gravi
          when cp_op_divu_c =>
333 61 zero_gravi
            if (DIVISION_EN = true) then res_o <= quotient; else NULL; end if;
334 47 zero_gravi
          when cp_op_rem_c =>
335 61 zero_gravi
            if (DIVISION_EN = true) then
336
              if (opy_is_zero = '0') then
337
                res_o <= div_res;
338
              else
339
                res_o <= rs1_i;
340
              end if;
341 47 zero_gravi
            else
342 61 zero_gravi
              NULL;
343 47 zero_gravi
            end if;
344
          when others => -- cp_op_remu_c
345 61 zero_gravi
            if (DIVISION_EN = true) then res_o <= remainder; else NULL; end if;
346 47 zero_gravi
        end case;
347
      end if;
348 2 zero_gravi
    end if;
349
  end process operation_result;
350
 
351 40 zero_gravi
  -- status output --
352
  valid_o <= valid;
353 2 zero_gravi
 
354 40 zero_gravi
 
355 2 zero_gravi
end neorv32_cpu_cp_muldiv_rtl;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.