至少有两种选择可以根据需要初始化记录类型。一个使用初始化函数,另一个使用聚合中的N值。
函数是初始化自定义数据类型的好方法。在你的情况,你可以创建一个功能default_entry_from_width(n)
,返回一个entry_type
值:
type entry_type is record
state: std_logic_vector;
end record;
function default_entry_from_width(width: natural) return entry_type is
variable return_vector: std_logic_vector(width-1 downto 0);
begin
for i in return_vector'range loop
return_vector(i) := '1' when i <= width/2 else '0';
end loop;
return (state => return_vector);
end;
constant ENTRY_1: entry_type := default_entry_from_width(3); -- return 011
constant ENTRY_2: entry_type := default_entry_from_width(4); -- return 0011
另一种方法是初始化常量与集料,使用N的previsouly定义的值:
constant N: natural := 4;
constant ENTRY_3: entry_type := (
state => (
N-1 downto N/2 => '1',
N/2-1 downto 0 => '0'
)
);
如果您为记录类型Entry_Type添加其他元素,则需要将这些元素的任何默认值添加到entry_default,其缺省值为聚合。 – user1155120