vhdl

How to form a Qualified Expression of an Array Type with a Single Element?


Consider a situation where you need to initialise an (1D) array with a length of one, hence containing a single element, using a qualified expression, like this1:

-- Declaration of type of an integer array:
type t_ints is array (natural range <>) of integer;
-- Assignment of a single-element array:
variable v_ints: t_ints(0 to 0) := t_ints'((1));

Unfortunately, this fails, because (1) is not interpreted as an array with one element, but as an over-parenthesised expression of a scalar integer.

However, the following code snippet, applying two elements, works:

-- Declaration of type of an integer array:
type t_ints is array (natural range <>) of integer;
-- Assignment of a dual-element array:
variable v_ints: t_ints(0 to 1) := t_ints'((1, 2));

This is obviously, because (1, 2) is correctly seen as an array of integers.

So how to correctly provide a qualified expression with an array of just a single element, or how to force (1) to be considered as an array?


The only work-around I found so far is to use an interim constant that holds more than an array element, and then slicing out one element for variable assignment, but this appears quite clumsy:

-- Declaration of type of an integer array:
type t_ints is array (natural range <>) of integer;
-- Declaration of interim auxiliary constant:
constant c_ints: t_ints(0 to 1) := (1, 2);
-- Assignment of a single-element array by slicing:
variable v_ints: t_ints(0 to 0) := c_ints(0 to 0);

1) This code could appear in the declaration section of a process, for example.


Solution

  • I can see at least three methods for doing this. Using index, using others, or through concatenation o another array:

    type t_ints is array (natural range <>) of integer;
    constant c_ints_null: t_ints(1 to 0) := (others => 1);
    variable v_ints0: t_ints(0 to 0) := (0 => 1);
    variable v_ints1: t_ints(0 to 0) := (others => 1);
    variable v_ints2: t_ints(0 to 0) := c_ints_null & 1;