Vhdl:无约束数组和大小设置



我试图将数组大小(整数ArraySize(从顶级文件传递到组件,但得到错误:

[Synth 8-561]范围表达式无法解析为常量[/UncArray.vhd":39]

我想知道是否有办法做到这一点。另外,如果一个不受约束的数组必须定义为一个常数,那么这有什么意义?

无限制测试.vhd

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity UnconstrainedTest is
Port ( clk  : in std_logic;
reset: in std_logic;
LED0 : out std_logic := '0';
LED1 : out std_logic := '0'
);
end UnconstrainedTest;
architecture Behavioral of UnconstrainedTest is
component UnconArray is
port( clk_1 : in std_logic; 
ArraySize : in integer;
LED_0 : out std_logic
); 
end component;
begin
A1: UnconArray port map (
clk_1 => clk,
ArraySize => 12,
LED_0 => LED0
);
A2: UnconArray port map (
clk_1 => clk,
ArraySize => 8,
LED_0 => LED1
);

end Behavioral;

组件UnArray.vhd

begin
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity UnconArray is
--  generic (depth : integer := 2);
Port ( clk_1     : in std_logic;
ArraySize : in integer;
LED_0     : out std_logic
);
end UnconArray;
architecture Behavioral of UnconArray is
type Array_type is array (integer range <>) of integer;
signal MyUnconArray : Array_type (0 to ArraySize);
-- type Array_type is array (0 to ArraySize) of integer;
-- signal MyUnconArray : Array_type;
begin
MyUnconArray <= (1, 2, 3); 
process (clk_1)
begin
if rising_edge(clk_1) then   
if ( MyUnconArray(0) = 1 )then
LED_0 <= '1';
else
LED_0 <= '0';
end if;
end if;
end process;

end Behavioral;

在用VHDL编写实际硬件模型时,必须为数组使用恒定大小。信号就像电路板上的一根电线——你不能动态地添加或删除它们。

如果你想使用这个代码,你必须使用一个泛型(这是一个局部常数(来声明向量的大小。实际上,你已经在代码中对此进行了注释,但它应该是这样的:

entity UnconArray is
generic (
depth : integer := 2
);
port ( 
clk_1 : in std_logic;
LED_0 : out std_logic
);
end UnconArray;
architecture Behavioral of UnconArray is
type Array_type is array (integer range <>) of integer;
signal MyUnconArray : Array_type (0 to depth);
...

然后当您实例化组件时:

A1: UnconArray 
generic map (
depth => 2    
)
port map (
clk_1 => clk,
LED_0 => LED0
);

然而,如果不将深度泛型设置为3,则在分配数据时仍会出现错误,因为您将其视为固定大小。您也只使用元素0,因此阵列的其余部分将在合成最小化过程中删除。

无约束数组的意义在于,您可以在声明信号时设置它们的大小,这可能会减少需要声明的类型数量。

例如,假设您希望在代码中使用两个不同的整数数组,其中包含不同数量的元素。可以声明两个不同大小的受约束数组类型,也可以声明一个不受约束的数组。

最新更新