二维无约束 Nx1 阵列

2D Unconstrained Nx1 Array

我正在尝试创建一个灵活的常量数组。我想使用二维数组,有时可能是 2x1、2x2、3x2 数组等。例如:

type int_2d_array is array (integer range<>, integer range<>) of integer;
constant M : positive := 2;
constant nMax : positive := 1;
constant n : int_2d_array(M - 1 downto 0, nMax - 1 downto 0) := ( (1) , (2) ); -- wrong

error: type int_2d_array does not match with the integer literal

如果我这样做,它不会抱怨:

type int_2d_array is array (integer range<>, integer range<>) of integer;
constant M : positive := 2;
constant nMax : positive := 2;
constant n : int_2d_array(M - 1 downto 0, nMax - 1 downto 0) := ( ( 0,1 ) , ( 2,2 )); -- accepted

第一个示例是否可以使用二维数组?

我设法用下面丑陋的方式编译了第一个例子:

type int_2d_array is array (integer range<>, integer range<>) of integer;
  constant M : positive := 2;
  constant nMax : positive := 1;
  constant n : int_2d_array(M - 1 downto 0, nMax - 1 downto 0) := ( (others => 1) , (others => 2) ); 

确实是奇怪的行为。

LRM(第 9.3.3 节聚合)指出:

Aggregates containing a single element association shall always be specified using named association in order to distinguish them from parenthesized expressions.

所以,这没问题:

constant n : int_1d_array(0 downto 0) := ( 0 => 1 );

这不是:

constant n : int_1d_array(0 downto 0) := ( 1 );

http://www.edaplayground.com/x/6a4