从 VHDL 记录类型中获取字段的属性
Get attribute of a field from a VHDL record type
考虑以下 VHDL 记录:
type big_record_t is record
field_a : unsigned(15 downto 0);
field_b : unsigned(23 downto 0);
end record;
是否可以在不实例化记录本身的情况下获取记录字段的属性?例如
signal ex : unsigned(big_record_t.field_a'range);
modelsim 报如下错误:
(vcom-1260) Type mark (big_record_t) cannot be prefix of selected name.
我知道获取实例化信号的属性是可能的,但对于这种特定情况,我想从类型本身获取类型属性。
您不能在 type † 上使用 'range
属性代码。如果你要这样做:
signal big_record_instance : big_record_t;
signal ex : unsigned(big_record_instance.field_a'range);
它应该可以工作,因为您现在正在尝试获取实例的范围,而不是类型。
如果您没有实例,另一种选择可能是让您的宽度基于定义记录类型的同一个包中的常量,如下所示:
constant field_a_width : integer := 16;
type big_record_t is record
field_a : std_logic_vector(field_a_width-1 downto 0);
field_b : std_logic_vector(23 downto 0);
end record;
signal ex : std_logic_vector(field_a_width-1 downto 0);
或者
constant field_a_width : integer := 16;
subtype field_a_type is std_logic_vector(field_a_width-1 downto 0);
type big_record_t is record
field_a : field_a_type;
field_b : std_logic_vector(23 downto 0);
end record;
signal ex : field_a_type;
† 请参阅评论中的例外情况
另一个建议是做类似的事情:
subtype field_a_range is range 15 downto 0;
subtype field_b_range is range 31 downto 0:
type big_record_t is record
field_a : unsigned(field_a_range);
field_b : unsigned(field_b_range);
end record;
然后您可以执行以下操作:
signal ex : unsigned(field_a_range);
另一个修复方法可能是使用一个有点傻的函数——使用一个函数。虽然这有效地创建了一个实例(虽然隐藏了一点)。
function field_a_length return natural is
variable tmp : big_record_t;
begin
return tmp.field_a'length;
end function field_a_length;
然后将其用作:
signal ex : unsigned(field_a_length-1 downto 0);
考虑以下 VHDL 记录:
type big_record_t is record
field_a : unsigned(15 downto 0);
field_b : unsigned(23 downto 0);
end record;
是否可以在不实例化记录本身的情况下获取记录字段的属性?例如
signal ex : unsigned(big_record_t.field_a'range);
modelsim 报如下错误:
(vcom-1260) Type mark (big_record_t) cannot be prefix of selected name.
我知道获取实例化信号的属性是可能的,但对于这种特定情况,我想从类型本身获取类型属性。
您不能在 type † 上使用 'range
属性代码。如果你要这样做:
signal big_record_instance : big_record_t;
signal ex : unsigned(big_record_instance.field_a'range);
它应该可以工作,因为您现在正在尝试获取实例的范围,而不是类型。
如果您没有实例,另一种选择可能是让您的宽度基于定义记录类型的同一个包中的常量,如下所示:
constant field_a_width : integer := 16;
type big_record_t is record
field_a : std_logic_vector(field_a_width-1 downto 0);
field_b : std_logic_vector(23 downto 0);
end record;
signal ex : std_logic_vector(field_a_width-1 downto 0);
或者
constant field_a_width : integer := 16;
subtype field_a_type is std_logic_vector(field_a_width-1 downto 0);
type big_record_t is record
field_a : field_a_type;
field_b : std_logic_vector(23 downto 0);
end record;
signal ex : field_a_type;
† 请参阅评论中的例外情况
另一个建议是做类似的事情:
subtype field_a_range is range 15 downto 0;
subtype field_b_range is range 31 downto 0:
type big_record_t is record
field_a : unsigned(field_a_range);
field_b : unsigned(field_b_range);
end record;
然后您可以执行以下操作:
signal ex : unsigned(field_a_range);
另一个修复方法可能是使用一个有点傻的函数——使用一个函数。虽然这有效地创建了一个实例(虽然隐藏了一点)。
function field_a_length return natural is
variable tmp : big_record_t;
begin
return tmp.field_a'length;
end function field_a_length;
然后将其用作:
signal ex : unsigned(field_a_length-1 downto 0);