有没有 short-way 找到第一个 '1' 位?

Is there any short-way to find first '1' bit?

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

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

意思是说,我们有一个信号“10010100”,那么 return 值,bit_number 应该是 2。有没有什么捷径可以在一个周期内找到它。我不想每个时钟扫描所有位。

你可以在你的函数中做一个for循环。
请注意,for 循环并不总是在硬件上实现,它可以使用 大量 逻辑元素。
尝试这样的事情(未测试)。 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;