使用 std_logic_vector (VHDL) 访问数组元素



请参阅下面的代码:

....
port(
the_input: in std_logic_vector(0 to 3));
...
type dummy_array is array (0 to 2) of std_logic_vector (0 to 7);
signal ins_dummy: dummy_array := ( 8x"1",  8x"2", 8x"3");
...

现在我想使用位访问这个数组的元素the_input(0 to 1).我该怎么做?据我所知,数组接受整数作为参数,但此输入std_logic。我尝试了不同论坛上可用的许多解决方案,但似乎没有任何效果。例如,当我应用这个时:to_integer(unsigned(the_input(0 to 1))),结果为零。

发生了什么事情?我不知道。有什么建议吗?

使用下面的小型测试平台,我能够使用您提到的方法 -> some_array(to_integer(unsigned(some_signal(((访问数组的元素。

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;   
use std.textio.all;
use ieee.std_logic_textio.all;
entity test is
end entity test;
architecture behav of test is
signal the_input : std_logic_vector(0 to 3);
signal test_sig  : std_logic_vector(7 downto 0);
type dummy_array is array(0 to 2) of std_logic_vector(7 downto 0);
signal ins_dummy : dummy_array := (x"01", x"02", x"03");
begin
test_sig <= ins_dummy(to_integer(unsigned(the_input)));
process
begin
wait for 1 ns;
the_input <= "0000";
wait for 1 ns;
the_input <= "0001";
wait for 1 ns;
the_input <= "0010";
end process;
end architecture behav;

但是,这是一个模拟,合成器可能会抱怨,因为端口the_input的范围大于可能的数组选项的数量。您可能必须添加逻辑以确保无法访问"越界"的数组索引。希望有帮助。可以尝试:

test_sig <= ins_dummy(to_integer(unsigned(the_input))) when (the_input < 3) else
others => '0';

最新更新