VHDL 中计数器内的 if 语句

if statement inside counter in VHDL

我有一个从“000”开始向上计数的 3 位计数器。每次达到“101”时,我都希望 o_en 信号为高电平。
我有以下 VHDL 文件。
不幸的是,行为有所不同。
当达到“110”(延迟一个时钟周期)时,o_en 信号为高电平。

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity special_counter is
    port(
        i_clk : in std_logic;
        i_en : in std_logic;
        i_rst : in std_logic;
        o_data : out std_logic_vector(2 downto 0);
        o_en : out std_logic
    );
end entity;

architecture behave of special_counter is
signal w_data : std_logic_vector(2 downto 0);
signal w_en : std_logic;

begin
    process(i_clk, i_rst) begin
        if i_rst = '1' then
            w_data <= "000";
        elsif rising_edge(i_clk) then
            if i_en = '1' then
                if w_data = "101" then
                    w_en <= '1';
                    w_data <= w_data + 1;
                else
                    w_en <= '0';
                    w_data <= w_data + 1;
                end if;
            end if;
        end if;
    end process;
    o_data <= w_data;
    o_en <= w_en;
end architecture;

如何更改我的程序以执行预期的行为?

完全正确,这就是您编写的代码。

换句话说: "At the rising edge of the clock, when the counter has the value 101 then the w_en will be set high."

因此 o_enw_en 信号将在 时钟上升沿后变为高电平
w_data 变化和 之后 上升时钟变为“110”的同时。

有两种解决方法:

  1. 测试“100”(因此更快一个周期)

  2. 使w_en(因此o_en)组合。

对于后者,您必须将作业移到 'clocked' 部分之外 并使用例如

w_en <= '1' when w_data = "101" else '0';