我试图在VHDL中实现加密算法,并且我创建了一个带有通用参数的反馈移位寄存器组件,以提高可重用性。这是我第一次使用泛型和数组,所以请耐心等待。
该组件将反馈位作为输入,并将其一些位(抽头)连接到输出端口,但可以使用通用参数更改此连接。 FSR组件的代码:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.fsr_taps_type.all;
entity FSR is
generic (
r_WIDTH : integer; -- Register width
r_STEP : integer; -- Update step
r_FWIDTH : integer; -- Feedback output width
r_HWIDTH : integer; -- h-function output width
r_TAPS : TAPS; -- Change the size according to the number of taps
r_STATE : TAPS
);
port (
clk : in std_logic;
rst : in std_logic;
fb_in : in std_logic_vector ((r_STEP-1) downto 0);
init : in std_logic;
ini_data : in std_logic_vector ((r_WIDTH-1) downto 0);
out_data : out std_logic_vector ((r_STEP-1) downto 0);
fb_out : out std_logic_vector ((r_FWIDTH-1) downto 0);
h_out : out std_logic_vector ((r_HWIDTH-1) downto 0)
);
end entity;
architecture behavioural of FSR is
signal shifted,shifted_next : std_logic_vector((r_WIDTH-1) downto 0);
begin
process(clk,rst)
begin
if rst = '1' then
shifted <= (others => '0');
elsif clk'event and clk = '1' then
shifted <= shifted_next;
end if;
end process;
process (fb_in,init,ini_data,shifted)
begin
if init = '1' then
shifted_next <= ini_data;
else
shifted_next <= shifted((r_WIDTH-r_STEP-1) downto 0) & fb_in;
end if;
end process;
out_data <= shifted ((r_WIDTH-1) downto (r_WIDTH-r_STEP));
-- The bits defined in the r_TAPS and r_STATE arrays are connected to the outputs in the same order as they are written (left to right)
-- Example: r_TAPS := (10,6) will create fb_out (1 downto 0) = bit10 & bit 6, in that order
-- Connect taps in the order of r_TAPS
gen_feedback: for I in (r_FWIDTH-1) downto 0 generate
fb_out(I) <= shifted(r_STATE(r_FWIDTH-I-1));
end generate gen_feedback;
-- Connect output bits for h function
gen_h: for I in (r_HWIDTH-1) downto 0 generate
h_out(I) <= shifted(r_STATE(r_HWIDTH-I-1));
end generate gen_h;
end architecture;
使用不同的通用值在同一文件中两次实例化此组件时出现问题。这是两个实例的通用映射:
LFSR : FSR
generic map (
r_WIDTH => 128,
r_STEP => STEP,
r_FWIDTH => 6,
r_HWIDTH => 7,
r_TAPS (0 to 5) => (128,121,90,58,47,32),
r_STATE (0 to 6) => (34,49,68,86,108,115,120)
)
NFSR : FSR
generic map (
r_WIDTH => 128,
r_STEP => STEP,
r_FWIDTH => 29,
r_HWIDTH => 2,
r_TAPS (0 to 28) => (40,36,35,33,106,104,103,58,50,46,117,115,111,110,88,80,101,69,67,63,125,61,60,44,128,102,72,37,32),
r_STATE => (33,116)
)
当我只创建第一个实例时,详细说明按预期工作,Vivado没有任何错误。但是,当我添加第二个实例时,我得到一个超出范围的错误:
ERROR: [Synth 8-97] array index 0 out of range (FSR.vhdl:54)
第54行是第一个用于生成循环的行:
fb_out(I) <= shifted(r_STATE(r_FWIDTH-I-1));
只有第二个实例会出错。我已经尝试将参数从第一个实例复制到第二个实例,我仍然得到相同的错误。
我做错了什么?
编辑:我添加了FSR组件的整个代码。
编辑2:我更改了TAPS的类型声明,以便限制数组:
type TAPS is array (0 to 31) of integer;
这似乎有效,我只需要添加其他语句来填充未使用的数组,所以这个:
r_TAPS (0 to 5) => (128,121,90,58,47,32)
成为这个:
r_TAPS (0 to 5) => (128,121,90,58,47,32,others =>0)
正如我之前所说的,我是VHDL数组的新手,所以我想知道是否有办法使用无约束数组类型对任意长数组执行此操作。