Search code examples
type-conversionvhdl

Illegal type conversion VHDL


I was trying to return type std_logic_vector by type conversion in vhdl. Here is my code:

 function mul(num1,num2 : in std_logic_vector(7 DOWNTO 0)) return std_logic_vector is
    variable v_TEST_VARIABLE1 : integer;
    variable v_TEST_VARIABLE2 : integer;
    variable n_times: integer:=1;
    variable product: integer:=0;
    begin 
      for n_times in 1 to v_TEST_VARIABLE2 loop
        product:=product + v_TEST_VARIABLE1;
      end loop;
    return std_logic_vector(product);
  end mul;

It gives "Illegal type conversion from std.standard.integer to ieee.std_logic_1164.std_logic_vector (numeric to array)." on compilation. How do I return std_logic_vector in such a code?


Solution

  • You must first convert it to an unsigned. I hope you're using numeric_std? If so, your code looks like this. Note that to use to_unsigned you must know the length of your output std_logic_vector. So this either needs to be an input to your function or you can bound the return vector:

    return std_logic_vector(to_unsigned(product, length));
    

    More information about how to convert std_logic_vector to integer.