如何遍历多维数组的元素并寻找匹配项?

时间:2020-11-01 10:47:26

标签: vhdl

如何遍历数组(recarr1)的元素? 我的目标是要找到匹配的f1值,然后输出相关的f2值。阵列必须存储为ROM。

问题出在书上,与现实应用无关。

library ieee;
use ieee.std_logic_1164.all;

entity recordtest is
port(
address: in integer range 0 to 15;
data_out : out std_logic_vector(7 downto 0));
end entity;

architecture recorder of recordtest is

type t_rec1 is record                  -- Declare a record with two fields
   f1 : std_logic_vector(15 downto 0);
   f2 : std_logic_vector(15 downto 0);
end record t_rec1;

type t_rec1_array is array (natural range 0 to 255) of t_rec1;

constant recarr1 : t_rec1_array := (
   1      => (f1 => x"0111", f2 => x"0111"),
   2      => (f1 => x"0011", f2 => x"0111"),
   others => (f1 => x"1111", f2 => x"0111"));

begin


end architecture;

1 个答案:

答案 0 :(得分:2)

您应该只能够遍历数组并搜索第一个匹配项:

function get_f2 (f1 : std_logic_vector(15 downto 0)) return std_logic_vector is
  variable result : std_logic_vector(15 downto 0);
begin
  result := (others => 'X');

  for i in recarr1'range loop
    if (recarr1(i).f1 = f1) then
      result := recarr1(i).f2;
      exit;
    end if;
  end loop;

  return result;
end function get_f2;

请注意,这是一种“强力”方法。它将在一个周期内运行。但是,如果您的阵列/ ROM太大,那当然就行不通了。在这种情况下,您将需要一个设计,该设计在每个循环中尝试一个/几个条目并在完成搜索时发出信号。

recarr1数组可以存储在ROM中,因为它是只读的。不受此功能的任何影响。

我想您的示例中还是缺少一些东西,因为我不清楚您在何处获得此功能的f1输入。