有人知道这在VHDL中是否合法吗?
type foo is (ONE, TWO, THREE);
signal fooArray is array (foo'left to foo'right) of int;
signal intArray is array (0 to 2) of int;
fooArray <= intArray; -- is this statement legal?
据我所知,阅读VHDL LRM,数组赋值是按位置完成的,而不是按索引完成的。所以我知道分配数组的界限不相等但其索引长度相等是合法的。但没有什么可以讨论将由整数索引的数组分配给由枚举类型索引的数组。只是想知道是否有人知道这是否合法或者主流VHDL模拟器是否允许这样做?
好吧,我想我应该已经表明代码是“伪代码”(而且我的vhdl编码很糟糕!)。我的主要目的是找出VHDL是否允许将由枚举索引的数组赋值给由整数索引的相同大小的数组。所以让我们试试这个例子:entity eiv is
end entity;
architecture fooArch of eiv is
type foo is (ONE, TWO, THREE);
type fooArrayType is array(foo) of integer;
type intArrayType is array(0 to 2) of integer;
signal fooArray : fooArrayType;
signal intArray : intArrayType;
begin
Statement:
fooArray <= intArray; -- is this statement legal?
end architecture;
答案 0 :(得分:2)
问题是您的代码段不代表合法的VHDL。
这三个尝试构成了一个叙述:
entity eiv is
end entity;
architecture foo of eiv is
type foo is (ONE, TWO, THREE);
signal fooArray is array (foo'left to foo'right) of int;
signal intArray is array (0 to 2) of int;
begin
Statement:
fooArray <= intArray; -- is this statement legal?
end architecture;
%% ghdl -a eiv.vhdl
eiv.vhdl:8:21:在信号声明中的标识符之后,预期','或':' ghdl:编译错误
%%
所以我们摆脱了明显的语法错误:
entity eiv is
end entity;
architecture foo of eiv is
type foo is (ONE, TWO, THREE);
type int_array is array (natural range <>) of integer;
signal fooArray: int_array (foo'left to foo'right);
signal intArray: int_array (0 to 2);
begin
Statement:
fooArray <= intArray; -- is this statement legal?
end architecture;
%% ghdl -a eiv.vhdl
eiv.vhdl:9:49:无法将'right属性与类型整数相匹配
eiv.vhdl:9:49 :(右属性的位置)
eiv.vhdl:9:37:无法将'left属性与类型整数相匹配
eiv.vhdl:9:37 :(左属性的位置)
eiv.vhdl:15:17:值的长度与目标的长度不匹配
ghdl:编译错误
%%
关闭以在LRM中查找预定义属性:
entity eiv is
end entity;
architecture foo of eiv is
type foo is (ONE, TWO, THREE);
type int_array is array (natural range <>) of integer;
signal fooArray: int_array (foo'POS(ONE) to foo'POS(THREE));
signal intArray: int_array (0 to 2);
begin
Statement:
fooArray <= intArray; -- is this statement legal?
end architecture;
%% ghdl -a eiv.vhdl#analyze
%% ghdl -e eiv#elaborate
%% ghdl -r eiv#run
%%
我们必须更改属性的原因是foo'RIGHT and foo'LEFT
返回的值是ONE
和THREE
而不是位置索引。
获取我们熟悉A
,B
,C
的值的字符类型,......这些字符的位置索引为65,66,67,... 。(而不是编写VHDL设计描述来确定这一点,我查看了ascii的手册页,你会在包标准character
中注意到ASCII到位置127)。
那么你可以从类型枚举数组中提取位置索引吗?是。
您可以将枚举值用作直接绑定的数组吗?否。
位置和价值是分开的。您可以使用'POS
属性恢复该位置。
或者int_array (foo'POS(foo'LEFT) to foo'POS(foo'RIGHT))
而不是直接指定值。
好吧,我想我应该表明代码是“伪代码” (而我的vhdl编码很糟糕!)。我的主要目的是找出VHDL 允许将由枚举索引的数组赋值给一个数组 相同大小的整数索引。所以让我们试试这个例子:
entity eiv is
end entity;
architecture fooArch of eiv is
type foo is (ONE, TWO, THREE);
type fooArrayType is array(foo) of integer;
type intArrayType is array(0 to 2) of integer;
signal fooArray : fooArrayType;
signal intArray : intArrayType;
begin
Statement:
fooArray <= intArray; -- is this statement legal?
end architecture;
简短的回答是否,声明不合法。类型foo
不是整数子类型,因此FooArrayType
与IntArrayType
不兼容。
注意
fooArray <= fooArrayType(intArray);
也不起作用。它们不是密切相关的类型。您需要一个函数来将intArray
转换为fooArrayType
:
entity eiv1 is
end entity;
architecture fooArch of eiv1 is
type foo is (ONE, TWO, THREE);
type fooArrayType is array(foo) of integer;
type intArrayType is array(0 to 2) of integer;
signal fooArray : fooArrayType;
signal intArray : intArrayType;
function conv_fooarray (v: intArrayType) return fooArrayType is
variable temp: fooArrayType;
begin
for i in v'range loop
temp(foo'VAL(i)) := v(i);
end loop;
return temp;
end function;
begin
Statement:
fooArray <= conv_fooarray(intArray); -- this statement is legal
end architecture;