vhdl - Why may a function return arrays of different sizes, but a constant may not be used the same way? - Stack Overflow

admin2025-04-19  1

In a previous question, I noted that this is not allowed in VHDL 2008:

type pipi is array (natural range <>) of bit;
type papa is array (natural range <>) of pipi;

constant foo : papa := (0 => ('1', '1'), 1 => ('1'));

The problem is that all elements of papa must have the same subtype, i.e. not merely the same type. Jim Lewis points out that this type of "jagged" array can be obtained using access types. In that case, foo must be a variable.

There are cases where this type of array is useful at elaboration time. For example, a list of variable-length instructions to some machine. To use that, foo would have to be a constant, surely?

I have one workaround, which is to use a function as a lookup table:

constant pipi_1 : pipi := ('1', '0');
constant pipi_2 : pipi := ('0', '1', '1');

function choose_a_pipi(a : natural) return pipi is
begin
    if a = 0 then
        return pipi_1;
    else
        return pipi_2;
    end if;
end;

Any subtype of pipi is a valid value to return. As this is a pure function, the result is effectively a constant.

This is interesting from a language design standpoint. Why does the language permit this but not the jagged constant? Is there a case during elaboration which would make such a constant difficult to reason bout?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745002324a279304.html

最新回复(0)