I'm trying to create a constant or variable of type integer_vector
with a single element, where the value comes from a parameter of a function. The following example does not work with GHDL but shows what I'm trying to make. In this example I use the RNG of OSVVM just for demonstration. The third parameter to RandInt
is of type integer_vector
and indicates which numbers to exclude from the given range.
impure function test(i : integer)
return integer is
constant excl : integer_vector := (i);
begin
return rv.RandInt(0, 4, excl);
end function test;
When compiling with GHDL, it shows the error message can't match "i" with type array type "integer_vector"
. In the case of RandInt
I can solve the problem by changing the constant to
constant excl : integer_vector := (i, i);
which is functionally the same but feels like a hack and won't work with all functions that expect an integer_vector
parameter. I also tried
constant excl : integer_vector := integer_vector(i);
constant excl : integer_vector := integer_vector((i));
constant excl : integer_vector := integer_vector'(i);
constant excl : integer_vector := integer_vector'((i));
but none worked.
2 Answers 2
Simply:
constant excl : integer_vector := (0 => i);
constant excl : integer_vector(1 to 1) := (others => i);
Single element values of an array aggregate require named association.
Also note that for newer versions of OSVVM (2024.11 and newer) most randomization functions that use an exclude vector are also overloaded to accept an integer value.
If you're referring to type integer_vector defined in in package standard of IEEE Std 1076-2008, it is a single dimensional array type with an index type of natural. Without knowing the declaration of rv a minimal, complete and verifiable example can't be constructed from your snippet containing function test. A verifiable example showing ways of constructing a value of an integer_vector with a single element can be demonstrated.
An array object value is the value of it's components. This collective value can be expressed as an aggregate.
An aggregate with a single element association has to be named.
The others choice in an association stands for all other choices, possible none and when used as a single choice requires a static subtype be provided by usage.
A null vector is an array with no elements. A null vector of type integer_vector would have no elements. There is no equivalent to a null string for a string with no elements expressed as a string literal. There is no literal for a null integer_vector.
However concatenation is predefined for every single dimensional array type.
The following example shows an aggregate association with a single element as well as concatenating a null array of type integer_vector with an integer value to form an expression of an integer array value.
package test_pkg is -- -2008 integer_vector tyee declaration
function test (i: integer) return integer_vector;
end package test_pkg;
package body test_pkg is
function test (i: integer) return integer_vector is
begin
return (0 => i); -- an aggregate, named association (single element)
end function;
end package body;
entity mcve is
end entity;
architecture foo of mcve is
use work.test_pkg.all;
constant ZERO: integer_vector := test(0);
-- a null array of integer_vector type, having no elements:
constant NULL_INTEGER_VECTOR: integer_vector (1 to 0) := (others => 0);
-- concatenate a null vector and an element value in an agregate:
constant STILL_ZERO: integer_vector := (NULL_INTEGER_VECTOR & 0);
begin
process
begin
report "ZERO(0) = " & integer'image(ZERO(0));
report "STILL_ZERO(0) = " & integer'image(STILL_ZERO(0));
wait;
end process;
end architecture;
When run:
%: ghdl -a --std=08 test_pkg.vhdl
%: ghdl -e --std=08 mcve
%: ghdl -r --std=08 mcve
test_pkg.vhdl:25:9:@0ms:(report note): ZERO(0) = 0
test_pkg.vhdl:27:9:@0ms:(report note): STILL_ZERO(0) = 0
%:
The leftmost element (or single element) value of an integer_vector object can use the value expression's index range which starts from the 'left value of the index type.