vhdl - 计算输入信号中的“01”序列
问题描述
我的目标是计算iaI
数组中“01”序列的数量。我尝试了以下代码,但它不像我想象的那样工作。
entity prob35 is
port (
iaI : in std_logic_vector (11 downto 0);
oaO : out std_logic_vector (2 downto 0)
);
end prob35;
architecture Behavioral of prob35 is
signal counter : integer := 0;
begin
process(iaI)
begin
for i in 1 to 11 loop
if (iaI(i-1)='1' and iaI(i)='0') then
counter<=counter+1;
end if;
end loop;
oaO<=conv_std_logic_vector(counter,oaO'length);
end process;
end Behavioral;
解决方案
vhdl 中的信号和变量之间存在显着差异。虽然变量立即获取赋值的值,但顺序代码(如进程)中的信号用于创建触发器,触发器本质上不会立即获取其赋值的值。您应该在这里使用变量来实现所需的功能。
entity prob35 is
port (
iaI : in std_logic_vector (11 downto 0);
oaO : out std_logic_vector (2 downto 0)
);
end prob35;
architecture Behavioral of prob35 is
begin
process(iaI)
variable counter : unsigned(2 downto 0) := "000";
begin
counter := "000";
for i in 1 to 11 loop
if (iaI(i-1)='1' and iaI(i)='0') then
counter := counter + 1;
end if;
end loop;
oaO <= std_logic_vector(counter);
end process;
end Behavioral;