是否有找到第一位'1'的捷径?

问题描述 投票:0回答:1

我想在函数中向您显示标题中提到的问题。

finding_first_one(signal a : std_logic_vector(...)) { return bit_number }

意思是说,我们有一个信号'10010100',那么返回值bit_number应该为2。在一个周期内是否有找到它的短途方法?我不想扫描每个时钟的所有位。

bit-manipulation vhdl fpga
1个回答
0
投票

您可以在函数中执行for循环。请注意,for循环不能总是在硬件上实现,它可以使用A LOT逻辑元素。尝试这样的事情(未经测试)。 index应在一个时钟周期内等于2。

architecture behav of test is

  signal sig    : std_logic_vector(15 downto 0) := x"2224";
  signal index  : integer;

  function finding_first_one (signal a : std_logic_vector()) return integer is
  begin    
    for i in a'low to a'high loop
      if a(i) = '1' then
        return i;
      end if;
    end loop;    
    -- all zero
    return -1;
  end function;

begin

  process (CLK)
  begin
    if rising_edge(clk) then
      index <= finding_first_one(sig);
    end if;
  end process;

end architecture;
© www.soinside.com 2019 - 2024. All rights reserved.