我想在函数中向您显示标题中提到的问题。
finding_first_one(signal a : std_logic_vector(...)) { return bit_number }
意思是说,我们有一个信号'10010100',那么返回值bit_number应该为2。在一个周期内是否有找到它的短途方法?我不想扫描每个时钟的所有位。
您可以在函数中执行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;