最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论