-1

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.

3
  • 1
    Provide a minimal reproducible example with a specific problem. Have you considered declaring a subtype of t_ints to use as a type mark for the qualified expression? An array aggregate with a single element requires named association. Commented Nov 21, 2024 at 15:07
  • @user16145658, I don't know how a subtype could help here. What I missed here is the named association for the single-element array… Commented Nov 21, 2024 at 15:34
  • There are cases when context supplies type and subtype, e.g. here variable v_ints: t_ints (0 to 0) := (0 => 1);. There can also be cases where the type and subtype are supplied by a qualified expression. Commented Nov 21, 2024 at 19:29

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

4 Comments

Can anyone explain why the line variable v_ints2: t_ints(0 to 0) := c_ints_null & 1; does not give a compile error?
@MatthiasSchweikart, this appends an integer to an empty array of integers (note the null-range 1 to 0 in the declaration of c_ints_null)…
Well, it would have been so easy, just using the named choices notation of the array aggregate (v_ints0) – thanks for that! Moreover, my problem is not linked to Qualified Expressions as I thought. Anyway, what is really interesting is that even (-1 => 1) or t_ints'(-1 => 1) work, even though the index range of t_ints is defined to be of type natural; seems only the base type (integer) is relevant for the indexing…
@MatthiasSchweikart All single dimensional arrays in VHDL support &. Concatenating a null array with an scalar value produces an array value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.