module x4enc2( input A, input B, input clk, output reg p, output reg dir ); reg [1:0] Ar, Br; wire A_posedge = (Ar==2'b01); // now we can detect A rising edges wire A_negedge = (Ar==2'b10); // and falling edges wire B_posedge = (Br==2'b01); // now we can detect B rising edges wire B_negedge = (Br==2'b10); // and falling edges always @ (posedge clk) begin Ar <= {Ar[0], A}; Br <= {Br[0], B}; if (A_posedge) begin p <= 1; if (~Br[1]) dir<=1; // A leads B else dir<=0; end else if (A_negedge) begin p <= 1; if (Br[1]) dir<=1; else dir<=0; end else if (B_posedge) begin p <= 1; if (Ar[1]) dir<=1; else dir<=0; end else if (B_negedge) begin p <= 1; if (~Ar[1]) dir<=1; else dir<=0; end else p<=0; end endmodule