Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
14 years ago

function error declaration in vhdl

hello guys ..i'm kind of new to this ..

i have a problem in vhdl function declaration .. i want to define function that calculate the avg of two std logic vectors .. but it keeps telling me that i have an error in the declaration .."Design unit declaration expected"

here is my code :

LIBRARY ieee;

LIBRARY ieee_proposed;

USE ieee.std_logic_1164.ALL;

USE ieee.std_logic_signed.ALL;

USE ieee_proposed.std_logic_1164_additions.ALL;

function avg (value1,value2 :in std_logic_vector(31 downto 0)) return std_logic_vector(31 downto 0) is

variable result : std_logic_vector (31 downto 0);

begin

result= (value1+value2) /2;

return result;

end function avg;

i hope anyone can help ..

thnxx :)

4 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    a function has to be declared inside something. So it has to be decalred inside a package, or architecture or a process. Functions do not exist on their own.

    Secondly - as you're new to VHDL, may I suggest you ditch std_logic_signed - it is a non-standard package. std_logic_vectors are not meant to represent numbers. VHDL is strongly typed, and has unsigned and signed types to deal with numeric vectors inside the numeric_std package (which is part of the VHDL standard).
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    yes that really helped thank you .. actually i used these libraries because i needed them .. the declaration of the function was a small part of my project .. and it kept telling me that there was a problem in the declaration .. but now it doesn't ..

    so thanks alot :)

    package Average is

    function avg (value1,value2 : std_logic_vector) return std_logic_vector ;

    end;

    package body Average is

    function avg (value1,value2 : std_logic_vector) return std_logic_vector is

    variable result : std_logic_vector (31 downto 0);

    variable res: integer;

    begin

    res:= (CONV_INTEGER(value1)+CONV_INTEGER(value2))/2;

    result:=CONV_STD_LOGIC_VECTOR(res,32);

    return result;

    end package body ;
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    You're still using the non-standard package as evidenced by the call to 'conv_integer'. You should switch to using ieee.numeric_std package and use the 'to_integer' and 'to_signed', 'to_unsigned' functions in those packages instead.

    Kevin Jennings