library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity design1 is
end entity;
architecture beh of design1 is
type slu_2dim is array (natural range <>, natural range <>)
of std_logic;
CONSTANT myconst : slu_2dim(0 TO 13, 9 DOWNTO 0) := (
1 => std_logic_vector(to_unsigned(1, 10)),
2 => std_logic_vector(to_unsigned(2, 10)),
3 => std_logic_vector(to_unsigned(3, 10)),
4 => std_logic_vector(to_unsigned(4, 10)),
OTHERS => std_logic_vector(to_unsigned(0, 10))
);
begin
end architecture;
Vivado 给我一个错误,指出 std_logic_vector 与 slu_2dim 类型不匹配。 我试图在不设置初始化字符串中的各个位的情况下执行此操作...
但是,似乎它应该可以工作,因为 std_logic_vector 定义为:
TYPE std_logic_vector IS ARRAY (NATURAL RANGE <>) OF std_logic;
TYPE std_ulogic IS ('U', -- Uninitialized
'X', -- Forcing Unknown
'0', -- Forcing 0
'1', -- Forcing 1
'Z', -- High Impedance
'W', -- Weak Unknown
'L', -- Weak 0
'H', -- Weak 1
'-'); -- Don't care
我知道这个会起作用:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity design1 is
end entity;
architecture beh of design1 is
type slu_2dim is array (natural range <>, natural range <>) of std_logic;
CONSTANT myconst : slu_2dim(0 TO 13, 3 DOWNTO 0) := (
1 => ('0', '0', '0', '1'),
2 => ('0', '0', '1', '0'),
3 => ('0', '0', '1', '1'),
4 => ('0', '1', '0', '0'),
OTHERS => ('0', '0', '0', '0')
);
begin
end architecture;
但是,用位设置向量而不是将整数值转换为向量有点烦人。 寻找一种更好的方法来设置数组的索引。
还使用更明显和常见的类型:
type slu_2dim is array (natural range <>) of std_logic_vector;
这不是一个选项,因为我无法控制源代码并且需要保留原始类型。
您的类型是二维位数组:
type slu_2dim is array (natural range <>, natural range <>) of std_logic;
初始化是一个std_logic_vector数组。 这是行不通的。
让我们为 std_logic_vector 数组创建一个唯一的类型:
type slvv is array (natural range <>) of std_logic_vector;
让我们编写一个在两者之间进行转换的函数:
function slvv_to_2dim (a : slvv) return slu_2dim is
constant OUTER_ARRAY_LEN : integer := a'length ;
constant INNER_ARRAY_LEN : integer := a(a'left)'length ;
alias aA : slvv(0 to OUTER_ARRAY_LEN - 1)(INNER_ARRAY_LEN - 1 downto 0) is a ;
variable result : slu_2dim(0 to OUTER_ARRAY_LEN - 1, INNER_ARRAY_LEN - 1 downto 0) ;
begin
for i in 0 to OUTER_ARRAY_LEN - 1 loop
for j in INNER_ARRAY_LEN -1 downto 0 loop
result(i, j) := aA(i)(j) ;
end loop ;
end loop ;
return result ;
end function slvv_to_2dim ;
现在使用常量中的转换:
CONSTANT myconst : slu_2dim(0 TO 13, 9 DOWNTO 0) :=
slvv_to_2dim (
1 => std_logic_vector(to_unsigned(1, 10)),
2 => std_logic_vector(to_unsigned(2, 10)),
3 => std_logic_vector(to_unsigned(3, 10)),
4 => std_logic_vector(to_unsigned(4, 10)),
0 | 5 to 13 => std_logic_vector(to_unsigned(0, 10))
);
可能存在一些语法错误,因为我没有对此进行测试,但它应该会让您接近。