// // Oct 14, 2003 // // Mealy machine for 0110 recognizer // Not fully tested // module mealy (Ck, Reset, In, Out); input Ck, Reset, In; output Out; reg Out; parameter [1:0] ST_Wait0 = 0, ST_Seen0 = 1, ST_Seen01 = 2, ST_Seen011 = 3; reg [1:0] CurrentState, NextState; // Combinational block for NextState and output logic // Note that combinational logic for output will change when state // changes or input changes, so this is a Mealy machine always @(CurrentState or In) begin : COMB case (CurrentState) ST_Wait0: if(In) begin Out = 0; NextState = ST_Wait0; end else begin Out = 0; NextState = ST_Seen0; end ST_Seen0: if(In) begin Out = 0; NextState = ST_Seen01; end else begin Out = 0; NextState = ST_Seen0; end ST_Seen01: if(In) begin Out = 0; NextState = ST_Seen011; end else begin Out = 0; NextState = ST_Seen0; end ST_Seen011: if(In) begin Out = 0; NextState = ST_Wait0; end else begin Out = 1; 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