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

TRo*_*Roa 5 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");
...
Run Code Online (Sandbox Code Playgroud)

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

怎么了?我不知道。有什么建议么?

小智 2

使用下面的小型测试台,我能够使用您提到的方法 -> 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;
Run Code Online (Sandbox Code Playgroud)

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

test_sig <= ins_dummy(to_integer(unsigned(the_input))) when (the_input < 3) else
            others => '0';
Run Code Online (Sandbox Code Playgroud)