CA Lab5 - RS and JK Flip Flop
CA Lab5 - RS and JK Flip Flop
The SR flip flop has two inputs SET ‘S’ and RESET ‘R’. As the name suggests, when S = 1,
output Q becomes 1, and when R = 1, output Q becomes 0. The output Q’ is the
complement of Q.
For S = 1 and R = 1, output Q = x i.e. 1 or 0. Hence, S=1 and R=1 input combination
is invalid and must be avoided in the SR flip flop.
module tb;
reg clk, rst_n;
reg s, r;
wire q, q_bar;
SR_flipflop dff(clk, rst_n, s, r, q, q_bar);
always #2 clk = ~clk;
initial begin
clk = 0; rst_n = 0;
$display("Reset=%b --> q=%b, q_bar=%b", rst_n, q, q_bar);
#3 rst_n = 1;
$display("Reset=%b --> q=%b, q_bar=%b", rst_n, q, q_bar);
drive(2'b00);
drive(2'b01);
drive(2'b10);
drive(2'b11);
#5;
$finish;
end
task drive;
input [1:0] ip;
begin
#2; {s, r} = ip; #2;
$display("s=%b, r=%b --> q=%b, q_bar=%b", s, r, q, q_bar);
end
endtask
endmodule
Output:
Reset=0 --> q=x, q_bar=x
Reset=1 --> q=0, q_bar=1
s=0, r=0 --> q=0, q_bar=1
s=0, r=1 --> q=0, q_bar=1
s=1, r=0 --> q=1, q_bar=0
s=1, r=1 --> q=x, q_bar=x
Design JK Flip Flop Circuit
The JK flip flop has two inputs ‘J’ and ‘K’. It behaves the same as SR flip flop except that it
eliminates undefined output state (Q = x for S=1, R=1).
For J=1, K=1, output Q toggles from its previous output state.
module tb;
reg clk, rst_n;
reg j, k;
wire q, q_bar;
JK_flipflop dff(clk, rst_n, j, k, q, q_bar);
always #2 clk = ~clk;
initial begin
clk = 0; rst_n = 0;
$display("Reset=%b --> q=%b, q_bar=%b", rst_n, q, q_bar);
#3 rst_n = 1;
$display("Reset=%b --> q=%b, q_bar=%b", rst_n, q, q_bar);
drive(2'b00);
drive(2'b01);
drive(2'b10);
drive(2'b11); // Toggles previous output
drive(2'b11); // Toggles previous output
#5;
$finish;
end
task drive;
input [1:0] ip;
begin
#2; {j,k} = ip; #2;
$display("j=%b, k=%b --> q=%b, q_bar=%b",j, k, q, q_bar);
end
endtask
endmodule
Output:
Reset=0 --> q=x, q_bar=x
Reset=1 --> q=0, q_bar=1
j=0, k=0 --> q=0, q_bar=1
j=0, k=1 --> q=0, q_bar=1
j=1, k=0 --> q=1, q_bar=0
j=1, k=1 --> q=0, q_bar=1
j=1, k=1 --> q=1, q_bar=0