when we write FSM in verilog there are two ways to write FSM first is using 3 always block(1 for next-state combinational logic + 1 for presene->next state sequential logic + 1 for output logic) and second way is to use only one always block for all 3 operation but my output wave for both cases is different..why is it so?
for example i have coded simple fsm in both ways and my out is shifted by 20 timeunit
first way :
//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
second way
//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
why the output is shifted by 20 timeunit..?