当我们在verilog中编写FSM时,有两种编写FSM的方法首先是使用3个总是块(1个用于下一个状态组合逻辑+ 1个用于presene->下一个状态顺序逻辑+ 1个用于输出逻辑),第二种方法是仅使用一个总是阻塞所有 3 种操作,但两种情况下的输出波形不同..为什么会这样?
例如,我以两种方式编写了简单的 fsm,并且我的输出偏移了 20 个时间单位
第一种方式:
//using one alwys block
module moore_20_1(x_in,y_out,clk,reset);
input wire clk,reset,x_in;
output reg y_out;
reg [1:0] state;
parameter start=2'b00,s0=2'b01,s1=2'b10,s2=2'b11;
always @ (posedge clk)//next state logic + ps->ns logic + output logic
begin
if(reset==1'b1) begin
state<=start;
y_out<=0;
end
else begin
case(state)
start: begin if(x_in) state<=s0;
else state<=s0;
y_out<=0;
end
s0: begin if(x_in) state<=s1;
else state<=s0;
y_out<=0;
end
s1: begin if(x_in) state<=s2;
else state<=s1 ;
y_out<=0;
end
s2: begin if(x_in) state<=s0;
else state<=s2;
y_out<=1;
end
endcase
end
end
endmodule
第二种方式
//moore machine using 3 always block(ps->ns+output logic+next-sate logic)
module moore_5_20_2(x_in,y_out,clk,reset);
input wire clk,reset,x_in;
output reg y_out;
reg [1:0] state;
reg [1:0] next_state;
parameter start=2'b00,s0=2'b01,s1=2'b10,s2=2'b11;
//ps->ns logic
always@ (posedge(clk))
if(reset==1'b1)
next_state<= #1 start;
else
next_state<= #1 state;
//next-stae logic
always @(next_state,x_in)
case(next_state)
start: begin if(x_in) state<=s0;
else state=s0;
end
s0: begin if(x_in) state<=s1;
else state=s0;
end
s1: begin if(x_in) state<=s2;
else state=s1 ;
end
s2: begin if(x_in) state<=s0;
else state=s2;
end
endcase
//OUTPUT LOGIc
always@(next_state)
if (reset==1'b1) y_out<= 0;
else begin
case(next_state)
start:y_out<= 0;
s0: y_out<= 0;
s1: y_out<=0;
s2: y_out<=#1 1;
endcase
end
endmodule
为什么输出移动了 20 个时间单位..?