Verilog中的FSM状态机

时间:2018-09-06 20:47:45

标签: initialization 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: ........

1 个答案:

答案 0 :(得分:1)

不能,因为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块和上面的组合状态转换逻辑有助于创建无锁存设计。我知道这远远超出了您的要求,但是这种编码风格有助于创建良好的综合设计。