// // Oct 14, 2003 // // Moore machine for 0110 recognizer // Not fully tested // module moore (Ck, Reset, In, Out); input Ck, Reset, In; output Out; reg Out; parameter [2:0] ST_Wait0 = 0, ST_Seen0 = 1, ST_Seen01 = 2, ST_Seen011 = 3, ST_Seen0110 = 4; reg [2:0] CurrentState, NextState; // Combinational block for NextState and output logic // Note that with this logic the output is // is independent of the input. This defines a Moore machine. always @(CurrentState or In) begin : COMB case (CurrentState) ST_Wait0: begin Out = 0; if(In) NextState = ST_Wait0; else NextState = ST_Seen0; end ST_Seen0: begin Out = 0; if(In) NextState = ST_Seen01; else NextState = ST_Seen0; end ST_Seen01: begin Out = 0; if(In) NextState = ST_Seen011; else NextState = ST_Seen0; end ST_Seen011: begin Out = 0; if(In) NextState = ST_Wait0; else NextState = ST_Seen0110; end ST_Seen0110: begin Out = 1; if(In) NextState = ST_Seen01; else NextState = ST_Seen0; end default: // Just to make sure we don't get latches begin Out = 0; NextState = ST_Wait0; end endcase end // COMB always // Sequential block for state FFs // It is good to keep this separate for clarity. Clearly // defines where state FFs are and the combinational logic // In general, it is good to separate the combinational logic from // the sequential logic. It avoids reader confusion and you know // that the tool should not get confused. It may take more lines to // type, but it is better to get working logic first! always @(posedge Ck) begin : SEQ if (Reset) CurrentState <= ST_Wait0; else CurrentState <= NextState; end endmodule