Search code examples
genericspackagevhdlmodelsimquestasim

How to pass an array type as generic type parameter to a VHDL package?


I'm working on a generic package (list) in VHDL-2008. This package has a type generic for the element type. If I declare an array type of this element type within the package, it's a new type. So for e.g. integer, my new integer_array would be incompatible with integer_vector from library ieee.

So I need also to pass in the array type (e.g. integer_vector). When an array instance of that array type is used with a 'range attribute, it gives me a warning in QuestaSim:

Prefix of attribute "range" must be appropriate for an array object or must denote an array subtype.

How do a denote that a generic type parameter is an array?

Generic Package:

package SortListGenericPkg is
  generic (
    type ElementType;  -- e.g. integer
    type ArrayofElementType;  -- e.g. integer_vector
    function LessThan(L : ElementType; R : ElementType) return boolean;     -- e.g. "<"
    function LessEqual(L : ElementType; R : ElementType) return boolean     -- e.g. "<="
  );

  function inside (constant E : ElementType; constant A : in ArrayofElementType) return boolean;
end package;

package body SortListGenericPkg is
  function inside (constant E : ElementType; constant A : in ArrayofElementType) return boolean is
  begin
    for i in A'range loop  -- this line causes the error
      if E = A(i) then
        return TRUE ;
      end if ;
    end loop ;
    return FALSE ;
  end function inside ;
end package body;

Instantiation:

package SortListPkg is
  package SortListPkg_int is new work.SortListGenericPkg
    generic map (
      ElementType        => integer,
      ArrayofElementType => integer_vector,
      LessThan           => "<",
      LessEqual          => "<="
    );
  alias Integer_SortList is SortListPkg_int.SortListPType;
end package SortListPkg ;

Solution

  • ModelSim makes a similar error/warning, so it's maybe a VHDL standard issues.

    A workaround is to declare ArrayofElementType as part of the package, like:

    package SortListGenericPkg is
      generic (
        type ElementType  -- e.g. integer
      );
      type ArrayofElementType is array (integer range <>) of ElementType;
      function inside(constant E : ElementType; constant A : in ArrayofElementType) return boolean;
    end package;
    

    and then convert the argument when inside is called, like:

    ... inside(int, ArrayofElementType(int_vec));
    

    or simple use ArrayofElementType as type when declaring the argument if possible/feasible.