vhdl - 为什么不是 VHDL 中的两进程状态机?

标签 vhdl idioms fsm

当我学习如何在 VHDL 中表达有限状态机时,它采用的是两进程架构。一个进程处理时钟/复位信号,另一个进程处理更新状态和输出的组合逻辑。下面是一个例子。

我见过这种风格受到批评(例如,请参阅 this question 的评论和答案),但从未详细说明过。我想知道这背后是否有客观(ish)原因。

是否有技术原因可以避免这种风格? Xilinx 的合成器似乎将其检测为状态机(您可以在输出中看到它,并验证转换),但其他人是否对此感到困惑,或者生成质量较差的实现?

它不是惯用的VHDL吗?记住避免基于意见的答案;如果它不是惯用的,是否有使用不同风格的广泛使用的教学资源或引用资料?惯用风格也可以存在,因为,例如。有几类错误很容易用正确的风格捕捉到,或者因为代码结构可以更好地表达问题域,或者出于其他原因。

(请注意,我不是要求对不同风格进行定义或演示,我想知道是否有客观原因专门避免两进程实现。)

例子

一些示例可以在 Free Range VHDL 中找到(第 89 页)。这是一个 super 简单的例子:

library ieee;
use ieee.std_logic_1164.all;

-- Moore state machine that transitions from IDLE to WAITING, WAITING
-- to READY, and then READY back to WAITING each time the input is
-- detected as on.
entity fsm is
    port(
        clk    : in std_logic;
        rst    : in std_logic;
        input  : in std_logic;
        output : out std_logic
    );
end entity fsm;

architecture fsm_arc of fsm is
    type state is (idle, waiting, ready);
    signal prev_state, next_state : state;
begin
    -- Synchronous/reset process: update state on clock edge and handle
    -- reset action.
    sync_proc: process(clk, rst)
    begin
        if (rst = '1') then
            prev_state <= idle;
        elsif (rising_edge(clk)) then
            prev_state <= next_state;
        end if;
    end process sync_proc;

    -- Combinatorial process: compute next state and output.
    comb_proc: process(prev_state, input)
    begin
        case prev_state is
            when idle =>
                output <= '0';
                if input = '1' then
                    next_state <= waiting;
                else
                    next_state <= idle;
                end if; 
            when waiting =>
                output <= '1';
                if input = '1' then
                    next_state <= ready;
                else
                    next_state <= waiting;
                end if;
            when ready =>
                output <= '0';
                if input = '1' then
                    next_state <= waiting;
                else
                    next_state <= ready;
                end if;
        end case;
    end process comb_proc;
end fsm_arc;

(请注意,我现在无法访问合成器,因此其中可能存在一些错误。)

最佳答案

我总是推荐单进程状态机,因为它避免了初学者非常常见的两类基本错误:

  • 组合过程的敏感性列表中缺少项目会导致模拟行为异常。它甚至可以在实验室中使用,因为大多数合成器并不关心灵敏度列表。
  • 使用组合结果之一作为输入而不是注册版本,导致非时钟循环或只是长路径/跳过状态。

  • 不太重要的是,组合过程会降低仿真效率。

    不太客观,我发现它们更容易阅读和维护;他们需要更少的样板,我不必使敏感度列表与逻辑同步。

    关于vhdl - 为什么不是 VHDL 中的两进程状态机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33718668/

    相关文章:

    vhdl - VHDL 中数字的数据类型是什么

    enums - VHDL:为什么没有为枚举定义长度?

    math - 如何摆脱 CORDIC 中的比例因子

    arrays - 如何: multidimensional arrays in vhdl

    haskell - 寻求对 monad 实现的建设性批评

    scala - 检测 Akka FSM

    c# - 不重复的说法 : access this object's member unless the object is null

    java - 根据o2对应的getter设置o1的所有setter

    另一种方式的python FSM Fysom回调

    verilog - FSM verilog代码语法错误