Verilog中的FSM状态机

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Verilog中的FSM状态机相关的知识,希望对你有一定的参考价值。

如果输入中没有复位,如何将初始状态设置为state_0?

reg[2:0]    state;


localparam  s0 = 3'b000, s1 = 3'b001, s2 = 3'b010, s3 = 3'b011, s4 = 
3'b100, s5 = 3'b101;    
assign  state = s0; /* NOT SURE IF THIS IS RIGHT!*/


localparam  i=0, j=64, k=0, h=0; 


always @ ( posedge clk ) begin

case( state )

        s0: ........
答案

不,这是行不通的,因为assign声明会一直强迫state = s0。编译器还会抱怨设置state的多个驱动程序。如果没有复位信号,一个选项是:

initial begin
  // set any initial values
  state = s0;
end

这将取代你有assign声明的地方。这在模拟中很有效,但更好的做法是修改状态逻辑:

localparam  s0 = 3'b000, s1 = 3'b001, s2 = 3'b010, s3 = 3'b011, s4 = 3'b100, s5 = 3'b101;
reg [2:0] state, next_state;

always @(posedge clk) begin
  state <= next_state;
end

always @(state) begin
  case (state)
  // modify this state logic to reflect your FSM
  s0: next_state <= s1;
  s1: next_state <= s2;
  s2: next_state <= s3;
  s3: next_state <= s4;
  s4: next_state <= s5;
  s5: next_state <= s0;
  // this controls the behavior at bringup w/o a reset
  // you should include a default case even with a reset
  default: next_state <= s0;
  endcase
end

always @(state) begin
  case (state)
  // modify this output logic to reflect your FSM
  s0: // set your output signals accordingly
  s1: // set your output signals accordingly
  s2: // set your output signals accordingly
  s3: // set your output signals accordingly
  s4: // set your output signals accordingly
  s5: // set your output signals accordingly
  // this controls the behavior at bringup w/o a reset
  // you should include a default case even with a reset
  default: // set all outputs to 0
  endcase
end

将逻辑分离到时钟的always块和上面的组合状态转换逻辑有助于创建无锁存器设计。我知道它比你提出的要多,但这种编码风格有助于创造出良好的综合设计。

以上是关于Verilog中的FSM状态机的主要内容,如果未能解决你的问题,请参考以下文章

Verilog三段式状态机描述

数字IC设计——用Verilog实现序列检测器(有限状态机FSM)

Verilog 我有两个always,模拟一个有6个状态(S0-S5)的状态机(FSM)。

三段式有限状态机Verilog代码

基于Verilog HDL的状态机描述方法

有限状态机FSM详解及其实现