【基本知识】FMS有限状态机设计
阅读原文时间:2023年07月11日阅读:1

  有限状态机是Verilog中十分基本也是十分重要的知识。本文对有限状态机做了一个简单介绍。

1.状态机三要素

  有限状态机具有三个要素:状态跳转、跳转判断、状态操作;

  1)状态跳转:现态跳转到次态;

  2)跳转判断:状态跳转的判断条件;

  3)状态操作:状态对应的操作;

2.状态机的实现方式

  1)一段式:状态机三要素集成于一个 always 块中。

    always@(posedge clk or negedge rst_n)
    begin
      if(!rst_n)begin
        state <= 'b00;
        Qout <= 'b0;
      end
      else case(state)
        'b00: begin
          if(A)begin
            state <= 'b01;
            Qout <= 'b1;
          end
          else begin
            state <= 'b00;
            Qout <= 'b0;
          end
        end
        'b01: begin
          if(!A)begin
            state <= 'b00;
            Qout <= 'b0;
          end
          else begin
            state <= 'b01;
            Qout <= 'b1;
          end
        end
        default:;
      endcase
    end

FMS_ONE

  2)二段式:状态机三要素分别设计于两个 always 块。

    always@(posedge clk or negedge rst_n)
    begin
      if(!rst_n)
        state <= 'b00;
      else case(state)
        'b00: begin
          if(A)
            state <= 'b01;
          else
            state <= 'b00;
        end
        'b01: begin
          if(!A)
            state <= 'b00;
          else
            state <= 'b01;
        end
        default:;
      endcase
    end

       always@(posedge clk or negedge rst\_n)  

    begin
      if(!rst_n)
        Qout <= 'b0;
      else case(state)
        'b00: Qout <= 1'b0;
        'b01: Qout <= 1'b1;
        default:;
      endcase
    end

FMS_TWO

  3)三段式:状态机三要素分别于三个 always 块。

FMS_THREE

    always@(posedge clk or negedge rst_n)//状态跳转
    begin
      if(!rst_n)
current_state <= 'b00; //复位
      else
current_state <= next_state//在时钟上升沿刷新现状态
    end

     always@(current\_state)          //跳转判断  

    begin
case(current_state)
'b00:begin
if(A)
next_state = 'b01;
else
next_state = 'b00;
end
'b01:begin
if(!A)
next_state = 'b00;
else
next_state = 'b01;
end
default:;
    end

     always @ (\*)                     //状态操作  
     begin  
         case(current\_state)  
             'b00:Qout <= 1'b0;  
             'b01:Qout <= 1'b1;  
             default:;  
         endcase  
     end

3.状态机设计要求

1)根据设计需求选择合适的风格;

2)case语句中都应加入default语句;

3)巧加DFF中继,提高可靠性;

4)课采用独热编码、格雷码设计状态;

5)记得采用全局复位;