初始化动态 VHDL 数组

Initialize dynamic VHDL array

--in the package
type t_array is array (natural range <>) of std_logic_vector (7 downto 0);
type p_array is access t_array;

--in my testbench
variable my_array : p_array := null;
begin
my_array := new t_array(0 to 19);
my_array := ( X"00",X"00",X"00",X"00",X"FF",
              X"FF",X"FF",X"FF",X"00",X"00",
              X"FF",X"FF",X"FF",X"FF",X"FF",
              X"FF",X"FF",X"FF",X"FF",X"FF" );

Error: Target type util_lib.tb_pkg.p_array in variable assignment is different from expression type util_lib.tb_pkg.t_array.

如何紧凑地分配数组的所有元素?

(1)。取消引用你的 pointcough 访问类型。

my_array.all := (...);

(2) 从函数初始化它

begin
    my_array := new_array(20);

初始化它的血腥细节可以隐藏在函数中,它可以通过算法计算值,从常量数组​​中复制它们,甚至从文件中读取内容。

constant initialiser : t_array := (...);

function new_array(length : natural range initialiser'range) return t_array is
   variable temp : p_array := new t_array(0 to length - 1);
begin
   -- either this
   for i in temp'range loop
      temp(i) := initialiser(i);
   end loop;
   -- or simply this
   temp.all := initialiser(temp'range);
   return temp;
end new_array;

(注意 new_array 参数的约束:确保它不会创建大于初始化程序的数组。)

您的错误信息:

Error: Target type util_lib.tb_pkg.p_array in variable assignment is different from expression type util_lib.tb_pkg.t_array.

告诉我们目标的子类型与右边的表达式不匹配。

可以通过多种方式治愈:

library ieee;
use ieee.std_logic_1164.all;

package initialize is 
--in the package
type t_array is array (natural range <>) of std_logic_vector (7 downto 0);
type p_array is access t_array;
end package;

library ieee;
use ieee.std_logic_1164.all;
use work.initialize.all;

entity testbench is
end entity;

architecture fum of testbench is

begin
    process
    --in my testbench
    variable my_array : p_array := null;
    begin
        my_array := new t_array(0 to 19);
        my_array (my_array'range) := ( 
                      X"00",X"00",X"00",X"00",X"FF",
                      X"FF",X"FF",X"FF",X"00",X"00",
                      X"FF",X"FF",X"FF",X"FF",X"FF",
                      X"FF",X"FF",X"FF",X"FF",X"FF" );
        wait;
    end process;
end architecture;

此处使用的切片名称的范围由 my_array'range 提供。

没有范围,目标名称将被解释为访问类型名称。作为具有离散范围的切片名称,目标名称表示对象类型的值表示:

IEEE Std 1076-2008 8. 名称 8.1 总则第 3 - 4 段:

Certain forms of name (indexed and selected names, slice names, and attribute names) include a prefix that is a name or a function call. If the prefix of a name is a function call, then the name denotes an element, a slice, or an attribute, either of the result of the function call, or (if the result is an access value) of the object designated by the result. Function calls are defined in 9.3.4.

A prefix is said to be appropriate for a type in either of the following cases:
— The type of the prefix is the type considered.
— The type of the prefix is an access type whose designated type is the type considered.

这里有助于理解 designatedenote 的同义词,用于描述访问类型的值与访问类型之间的关系它引用的对象。

第 5 段:

The evaluation of a name determines the named entity denoted by the name. The evaluation of a name that has a prefix includes the evaluation of the prefix, that is, of the corresponding name or function call. If the type of the prefix is an access type, the evaluation of the prefix includes the determination of the object designated by the corresponding access value. In such a case, it is an error if the value of the prefix is a null access value. It is an error if, after all type analysis (including overload resolution), the name is ambiguous.

在这种情况下,您可以使用包含整个数组的切片名称。

您可以为指定的访问类型对象使用选定的名称:

architecture fie of testbench is

begin
    process
        variable my_array : p_array := null;
    begin
    my_array := new t_array(0 to 19);
    my_array.all := ( X"00",X"00",X"00",X"00",X"FF",
                      X"FF",X"FF",X"FF",X"00",X"00",
                      X"FF",X"FF",X"FF",X"FF",X"FF",
                      X"FF",X"FF",X"FF",X"FF",X"FF" );
        wait;
    end process;
end architecture;

8.3 第 5 段的选定名称:

For a selected name that is used to denote the object designated by an access value, the suffix shall be the reserved word all. The prefix shall belong to an access type.

使用这些方法可以区分对访问类型(不是右手表达式中的复合类型)的对象的分配和访问类型的对象指定的分配对象。

如果你喜欢一步,你也可以这样做:

--in my testbench
variable my_array : p_array := null;
begin
my_array := new t_array'( X"00",X"00",X"00",X"00",X"FF",
              X"FF",X"FF",X"FF",X"00",X"00",
              X"FF",X"FF",X"FF",X"FF",X"FF",
              X"FF",X"FF",X"FF",X"FF",X"FF" );

您也可以在初始化时这样做:

--in my testbench
variable my_array : p_array := new t_array'(
              X"00",X"00",X"00",X"00",X"FF",
              X"FF",X"FF",X"FF",X"00",X"00",
              X"FF",X"FF",X"FF",X"FF",X"FF",
              X"FF",X"FF",X"FF",X"FF",X"FF" );

begin