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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_cpu_cp_muldiv.vhd] - Diff between revs 60 and 61

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 60 Rev 61
Line 1... Line 1...
-- #################################################################################################
-- #################################################################################################
-- # << NEORV32 - CPU Co-Processor: Integer Multiplier/Divider Unit (RISC-V "M" Extension)>>       #
-- # << NEORV32 - CPU Co-Processor: Integer Multiplier/Divider Unit (RISC-V "M" Extension)>>       #
-- # ********************************************************************************************* #
-- # ********************************************************************************************* #
-- # Multiplier and Divider unit. Implements the RISC-V M CPU extension.                           #
-- # Multiplier and Divider unit. Implements the RISC-V M CPU extension.                           #
-- #                                                                                               #
-- #                                                                                               #
-- # Multiplier core (signed/unsigned) uses classical serial algorithm. Unit atency: 31+3 cycles   #
-- # Multiplier core (signed/unsigned) uses classical serial algorithm. Unit latency: 31+3 cycles  #
-- # Divider core (unsigned) uses classical serial algorithm. Unit latency: 32+4 cycles            #
-- # Divider core (unsigned) uses classical serial algorithm. Unit latency: 32+4 cycles            #
-- #                                                                                               #
-- #                                                                                               #
-- # Multiplications can be mapped to DSP blocks (faster!) when FAST_MUL_EN = true.                #
-- # Multiplications can be mapped to DSP blocks (faster!) when FAST_MUL_EN = true.                #
-- # Unit latency: 3 cycles                                                                        #
 
-- # ********************************************************************************************* #
-- # ********************************************************************************************* #
-- # BSD 3-Clause License                                                                          #
-- # BSD 3-Clause License                                                                          #
-- #                                                                                               #
-- #                                                                                               #
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
-- #                                                                                               #
-- #                                                                                               #
Line 47... Line 46...
library neorv32;
library neorv32;
use neorv32.neorv32_package.all;
use neorv32.neorv32_package.all;
 
 
entity neorv32_cpu_cp_muldiv is
entity neorv32_cpu_cp_muldiv is
  generic (
  generic (
    FAST_MUL_EN : boolean := false -- use DSPs for faster multiplication
    FAST_MUL_EN : boolean := false; -- use DSPs for faster multiplication
 
    DIVISION_EN : boolean := true   -- implement divider hardware
  );
  );
  port (
  port (
    -- global control --
    -- global control --
    clk_i   : in  std_ulogic; -- global clock, rising edge
    clk_i   : in  std_ulogic; -- global clock, rising edge
    rstn_i  : in  std_ulogic; -- global reset, low-active, async
    rstn_i  : in  std_ulogic; -- global reset, low-active, async
Line 102... Line 102...
  signal div_sign_comp_in : std_ulogic_vector(data_width_c-1 downto 0);
  signal div_sign_comp_in : std_ulogic_vector(data_width_c-1 downto 0);
  signal div_sign_comp    : std_ulogic_vector(data_width_c-1 downto 0);
  signal div_sign_comp    : std_ulogic_vector(data_width_c-1 downto 0);
  signal div_res          : std_ulogic_vector(data_width_c-1 downto 0);
  signal div_res          : std_ulogic_vector(data_width_c-1 downto 0);
 
 
  -- multiplier core --
  -- multiplier core --
  signal mul_product_p  : std_ulogic_vector(63 downto 0);
 
  signal mul_product_s  : std_ulogic_vector(63 downto 0);
 
  signal mul_product    : std_ulogic_vector(63 downto 0);
  signal mul_product    : std_ulogic_vector(63 downto 0);
  signal mul_do_add     : std_ulogic_vector(data_width_c downto 0);
  signal mul_do_add     : std_ulogic_vector(data_width_c downto 0);
  signal mul_sign_cycle : std_ulogic;
  signal mul_sign_cycle : std_ulogic;
  signal mul_p_sext     : std_ulogic;
  signal mul_p_sext     : std_ulogic;
  signal mul_op_x       : signed(32 downto 0); -- for using DSPs
  signal mul_op_x       : signed(32 downto 0); -- for using DSPs
Line 138... Line 136...
      -- FSM --
      -- FSM --
      case state is
      case state is
        when IDLE =>
        when IDLE =>
          cp_op_ff <= cp_op;
          cp_op_ff <= cp_op;
          if (start_i = '1') then
          if (start_i = '1') then
            if (operation = '1') then -- division
            if (operation = '1') and (DIVISION_EN = true) then -- division
              cnt <= "11111";
              cnt <= "11111";
              state <= DIV_PREPROCESS;
              state <= DIV_PREPROCESS;
            else
            else
              cnt <= "11110";
              cnt <= "11110";
              if (FAST_MUL_EN = true) then
              if (FAST_MUL_EN = true) then
Line 152... Line 150...
              end if;
              end if;
            end if;
            end if;
          end if;
          end if;
 
 
        when DIV_PREPROCESS =>
        when DIV_PREPROCESS =>
 
          if (DIVISION_EN = true) then
          -- check rlevatn input signs --
          -- check rlevatn input signs --
          if (cp_op = cp_op_div_c) then -- result sign compensation for div?
          if (cp_op = cp_op_div_c) then -- result sign compensation for div?
            div_res_corr <= rs1_i(rs1_i'left) xor rs2_i(rs2_i'left);
            div_res_corr <= rs1_i(rs1_i'left) xor rs2_i(rs2_i'left);
          elsif (cp_op = cp_op_rem_c) then -- result sign compensation for rem?
          elsif (cp_op = cp_op_rem_c) then -- result sign compensation for rem?
            div_res_corr <= rs1_i(rs1_i'left);
            div_res_corr <= rs1_i(rs1_i'left);
Line 177... Line 176...
            div_opy <= rs2_i;
            div_opy <= rs2_i;
          end if;
          end if;
          --
          --
          start_div <= '1';
          start_div <= '1';
          state     <= PROCESSING;
          state     <= PROCESSING;
 
          else
 
            state <= IDLE;
 
          end if;
 
 
        when PROCESSING =>
        when PROCESSING =>
          cnt <= std_ulogic_vector(unsigned(cnt) - 1);
          cnt <= std_ulogic_vector(unsigned(cnt) - 1);
          if (cnt = "00000") then
          if (cnt = "00000") then
            state <= FINALIZE;
            state <= FINALIZE;
Line 212... Line 214...
  start_mul <= '1' when (state = IDLE) and (start_i = '1') and (operation = '0') else '0';
  start_mul <= '1' when (state = IDLE) and (start_i = '1') and (operation = '0') else '0';
 
 
 
 
  -- Multiplier Core (signed/unsigned) ------------------------------------------------------
  -- Multiplier Core (signed/unsigned) ------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  multiplier_core_serial: process(rstn_i, clk_i)
  -- iterative multiplication (bit-serial) --
 
  multiplier_core_serial:
 
  if (FAST_MUL_EN = false) generate
 
    multiplier_core: process(rstn_i, clk_i)
  begin
  begin
    if (rstn_i = '0') then
    if (rstn_i = '0') then
      mul_product_s <= (others => def_rst_val_c);
        mul_product <= (others => def_rst_val_c);
    elsif rising_edge(clk_i) then
    elsif rising_edge(clk_i) then
      if (FAST_MUL_EN = false) then -- use small iterative computation
 
        if (start_mul = '1') then -- start new multiplication
        if (start_mul = '1') then -- start new multiplication
          mul_product_s(63 downto 32) <= (others => '0');
          mul_product(63 downto 32) <= (others => '0');
          mul_product_s(31 downto 00) <= rs2_i;
          mul_product(31 downto 00) <= rs2_i;
        elsif (state = PROCESSING) or (state = FINALIZE) then -- processing step or sign-finalization step
        elsif (state = PROCESSING) or (state = FINALIZE) then -- processing step or sign-finalization step
          mul_product_s(63 downto 31) <= mul_do_add(32 downto 0);
          mul_product(63 downto 31) <= mul_do_add(32 downto 0);
          mul_product_s(30 downto 00) <= mul_product_s(31 downto 1);
          mul_product(30 downto 00) <= mul_product(31 downto 1);
        end if;
        end if;
      end if;
      end if;
    end if;
    end process multiplier_core;
  end process multiplier_core_serial;
  end generate;
 
 
  multiplier_core_dsp: process(clk_i)
  -- parallel multiplication --
 
  multiplier_core_dsp:
 
  if (FAST_MUL_EN = true) generate
 
    multiplier_core: process(clk_i)
  begin
  begin
    if rising_edge(clk_i) then
    if rising_edge(clk_i) then
      if (FAST_MUL_EN = true) then -- use direct approach using DSP blocks
 
        if (start_mul = '1') then
        if (start_mul = '1') then
          mul_op_x <= signed((rs1_i(rs1_i'left) and rs1_is_signed) & rs1_i);
          mul_op_x <= signed((rs1_i(rs1_i'left) and rs1_is_signed) & rs1_i);
          mul_op_y <= signed((rs2_i(rs2_i'left) and rs2_is_signed) & rs2_i);
          mul_op_y <= signed((rs2_i(rs2_i'left) and rs2_is_signed) & rs2_i);
        end if;
        end if;
        mul_buf_ff    <= mul_op_x * mul_op_y;
        mul_buf_ff    <= mul_op_x * mul_op_y;
        mul_product_p <= std_ulogic_vector(mul_buf_ff(63 downto 0)); -- let the register balancing do the magic here
        mul_product <= std_ulogic_vector(mul_buf_ff(63 downto 0)); -- let the register balancing do the magic here
      end if;
 
    end if;
    end if;
  end process multiplier_core_dsp;
    end process multiplier_core;
 
  end generate;
 
 
  mul_product <= mul_product_p when (FAST_MUL_EN = true) else mul_product_s;
  -- do another addition (bit-serial) --
 
 
  -- do another addition --
 
  mul_update: process(mul_product, mul_sign_cycle, mul_p_sext, rs1_is_signed, rs1_i)
  mul_update: process(mul_product, mul_sign_cycle, mul_p_sext, rs1_is_signed, rs1_i)
  begin
  begin
    -- current bit of rs2_i to take care of --
    -- current bit of rs2_i to take care of --
    if (mul_product(0) = '1') then -- multiply with 1
    if (mul_product(0) = '1') then -- multiply with 1
      if (mul_sign_cycle = '1') then -- for signed operations only: take care of negative weighted MSB -> multiply with -1
      if (mul_sign_cycle = '1') then -- for signed operations only: take care of negative weighted MSB -> multiply with -1
Line 267... Line 271...
  mul_p_sext     <= mul_product(mul_product'left) and rs1_is_signed;
  mul_p_sext     <= mul_product(mul_product'left) and rs1_is_signed;
 
 
 
 
  -- Divider Core (unsigned) ----------------------------------------------------------------
  -- Divider Core (unsigned) ----------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
 
  divider_core_serial:
 
  if (DIVISION_EN = true) generate
  divider_core: process(rstn_i, clk_i)
  divider_core: process(rstn_i, clk_i)
  begin
  begin
    if (rstn_i = '0') then
    if (rstn_i = '0') then
      quotient  <= (others => def_rst_val_c);
      quotient  <= (others => def_rst_val_c);
      remainder <= (others => def_rst_val_c);
      remainder <= (others => def_rst_val_c);
Line 294... Line 300...
 
 
  -- result sign compensation --
  -- result sign compensation --
  div_sign_comp_in <= quotient when (cp_op = cp_op_div_c) else remainder;
  div_sign_comp_in <= quotient when (cp_op = cp_op_div_c) else remainder;
  div_sign_comp    <= std_ulogic_vector(0 - unsigned(div_sign_comp_in));
  div_sign_comp    <= std_ulogic_vector(0 - unsigned(div_sign_comp_in));
  div_res          <= div_sign_comp when (div_res_corr = '1') and (opy_is_zero = '0') else div_sign_comp_in;
  div_res          <= div_sign_comp when (div_res_corr = '1') and (opy_is_zero = '0') else div_sign_comp_in;
 
  end generate;
 
 
 
  -- no divider --
 
  divider_core_serial_none:
 
  if (DIVISION_EN = false) generate
 
    remainder <= (others => '-');
 
    quotient  <= (others => '-');
 
    div_res   <= (others => '-');
 
  end generate;
 
 
 
 
  -- Data Output ----------------------------------------------------------------------------
  -- Data Output ----------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  operation_result: process(rstn_i, clk_i)
  operation_result: process(rstn_i, clk_i)
Line 311... Line 326...
          when cp_op_mul_c =>
          when cp_op_mul_c =>
            res_o <= mul_product(31 downto 00);
            res_o <= mul_product(31 downto 00);
          when cp_op_mulh_c | cp_op_mulhsu_c | cp_op_mulhu_c =>
          when cp_op_mulh_c | cp_op_mulhsu_c | cp_op_mulhu_c =>
            res_o <= mul_product(63 downto 32);
            res_o <= mul_product(63 downto 32);
          when cp_op_div_c =>
          when cp_op_div_c =>
            res_o <= div_res;
            if (DIVISION_EN = true) then res_o <= div_res; else NULL; end if;
          when cp_op_divu_c =>
          when cp_op_divu_c =>
            res_o <= quotient;
            if (DIVISION_EN = true) then res_o <= quotient; else NULL; end if;
          when cp_op_rem_c =>
          when cp_op_rem_c =>
 
            if (DIVISION_EN = true) then
            if (opy_is_zero = '0') then
            if (opy_is_zero = '0') then
              res_o <= div_res;
              res_o <= div_res;
            else
            else
              res_o <= rs1_i;
              res_o <= rs1_i;
            end if;
            end if;
 
            else
 
              NULL;
 
            end if;
          when others => -- cp_op_remu_c
          when others => -- cp_op_remu_c
            res_o <= remainder;
            if (DIVISION_EN = true) then res_o <= remainder; else NULL; end if;
        end case;
        end case;
      end if;
      end if;
    end if;
    end if;
  end process operation_result;
  end process operation_result;
 
 

powered by: WebSVN 2.1.0

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