我正在尝试制作一个在 vhdl 中使用整数矩阵(二维数组)的程序,但我以前从未这样做过。
首先,是否可以在实体的信号定义中定义一个二维数组?我的意思是这样的;
entity Matrix is
Port ( CLK : in STD_LOGIC;
RESET : in STD_LOGIc;
Output : out array (integer range <> , integer range <> ) of integer);
end Matrix;
还有。实际初始化矩阵的最佳方法是什么?我想做这样的事情;
type 2d_array is array(2 downto 0, 2 downto 0) of integer;
constant A2d : 2d_array :=((1,2,3),
(1,2,3),
(1,2,3));
不过,我不太确定这是否正确。
最后但并非最不重要的一点是,如果我尝试将其中一个输出矩阵重组为一维数组,会发生什么?这会解决我的第一个问题,还是会产生一个新问题?
最佳答案
在VHDL中,所有类型在使用前都必须声明。所以你不能简单地将一个对象声明为“数组”,因为你还没有声明类型。要在实体端口定义中使用类型,通常需要在包中声明该类型,并将该包包含在实体中。
package my_types_pkg is
type my_array_t is array(integer range <>, integer range <>) of integer;
end package;
use work.my_types_pkg;
entity Matrix is
Port ( CLK : in STD_LOGIC;
RESET : in STD_LOGIc;
output : out my_array_t -- note this is not yet constrained - the object mapped to this port will constrain the port
);
初始化:您对二维数组做了正确的事情。
“ reshape ”:VHDL 是一种强类型语言。所以数组不一定可以直接转换。二维整数数组与一维整数数组的类型不同,因此需要类型转换函数。
关于vhdl - 在 VHDL 中定义和初始化矩阵的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72246811/