Question:
How does one declare, if possible, a function intended for a parameter of any type T
where the only constraint on T
is that it was defined as a 1D array
as in
type T is array ( integer range <> ) of a_random_type;
where a_random_type
can be any type
.
The following syntactically incorrect function is an example of what is desired
function measure_size_of_any_array ( a : array ( integer range <> ) ) return natural is
variable r : natural := 0;
begin
for i in a'range loop
r := r + 1;
end loop;
return r;
end function;
which could then be used on any array
type natural_array is array ( integer range <> ) of natural;
type stdlogv_array is array ( integer range <> ) of std_logic_vector;
[...]
variable some_natural_array : natural_array;
variable some_stdlogv_array : stdlogv_array;
[...]
constant size_1 : natural := measure_size_of_any_array(some_natural_array);
constant size_2 : natural := measure_size_of_any_array(some_stdlogv_array);
Obviously, this question is about the way of defining the function and not about the function itself: I am not looking for a'length
.
Possible Solution:
From Ashenden's VHDL-2008: Just the New Stuff
Generic types can be specified for subprograms.
We can declare a formal generic type in a generic list in the following way:
type indentifier
A function with a generic list takes the form:
function indentifier generic ( ... ) parameter ( ... ) return result_type is ... -- declarations begin ... -- statements end function identifier
which would allow the following definition
function measure_size_of_any_array
generic ( type arr_type )
parameter ( arr : arr_type );
and the following use
function measure_size_of_natural_array is new measure_size_of_any_array
generic ( arr_type => natural_array );
function measure_size_of_stdlogv_array is new measure_size_of_any_array
generic ( arr_type => stdlogv_array );
constant size_1 : natural := measure_size_of_natural_array(some_natural_array);
constant size_2 : natural := measure_size_of_stdlogv_array(some_stdlogv_array);
This provides the desired behavior of sharing the body of the function among different calls, regardless of the type of the elements of the array
but still requires an instantiated function (which can be as local as desired so it isn't that bad).
Because of how little support is being provided for VHDL-2008 by the main vendors (the previous solution wasn't understood by the compilers I tried), a VHDL-87, -93 or -2002 solution will be preferred.
Comment after receiving the first answers:
The previous information is my attempt to find a way of writing a VHDL subprogram accepting any argument as long as it is an array
(i.e. to answer the initial question). The expected answer isn't necessarily supposed to use the same approach (namely using VHDL-2008 generic subprograms)!