Tuesday, June 29, 2010

JK Flip-Flop

The JK flip-flop augments the behavior of the SR flip-flop (J=Set, K=Reset) by interpreting the S = R = 1 condition as a "flip" or toggle command. Specifically, the combination J = 1, K = 0 is a command to set the flip-flop; the combination J = 0, K = 1 is a command to reset the flip-flop; and the combination J = K = 1 is a command to toggle the flip-flop, i.e., change its output to the logical complement of its current value. Setting J = K = 0 does NOT result in a D flip-flop, but rather, will hold the current state. To synthesize a D flip-flop, simply set K equal to the complement of J. The JK flip-flop is therefore a universal flip-flop, because it can be configured to work as an SR flip-flop, a D flip-flop, or a T flip-flop.

module jkff (j, k, clk, reset, q);
input j;
input k;
input clk;
input reset;
output q;
reg q;
always@(posedge clk or negedge reset)
    begin
    if(~reset)
        begin
            q=1'b0;
        end
    else if(j==1'b0 && k==1'b1)
        begin
            q=1'b0;
        end
     if(j==1'b1 & k==1'b0)
        begin
            q=1'b1;
        end
     if(j==1'b1 & k==1'b1)
        begin
            q=~q;
        end
     if(j==1'b0 & k==1'b0)
        begin
            q=q;
            end
        end
endmodule 
You could download file jkff.v and testbench.tb here.

1 comment: