1
\$\begingroup\$

Here is my gate level description of an S-R latch:

module SR_Latch_Nand(input S, R, C, output Q, QB);
wire s1, r1;
nand #8 n1(r1, R, C);
nand #8 n2(s1, S, C);
nand #8 n3(QB, R, Q);
nand #8 n4(Q, S, QB);
endmodule

and here is testbench for this S-R latch:

module SR_Latch_Nand_TB();
logic s, r, clk;
wire q, qb;
SR_Latch_Nand sr(s, r, clk, q, qb);
initial begin
 s = 0; r = 0; clk = 0;
 #100 s = 1;
 #100 clk = 1;
 #100 clk = 0;
 #100 clk = 1;
 #100 s = 0;
 #100;
end
endmodule 

When I check the waveform, the value of Q is x at most of the times. Other times, it's mostly incorrect. I've tried to preset values of q and qb, but it still doesn't seem to work.

Can you tell what's the problem with this code?

toolic
10.8k11 gold badges31 silver badges35 bronze badges
asked May 2, 2017 at 20:47
\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

The problem is with your testbench and the way you set r and s. If they are both active low, then make sure your testbench tests only one active low.

answered May 2, 2017 at 21:18
\$\endgroup\$
-1
\$\begingroup\$

The code of SR_Latch_Nand is wrong. You missed to use the s1 and r1 for the output flops. Corrected SR latch module should be.

module SR_Latch_Nand(input S, R, C, output Q, QB);
 wire s1, r1;
 nand #8 n1(r1, R, C);
 nand #8 n2(s1, S, C);
 nand #8 n3(QB, s1, Q);
 nand #8 n4(Q, r1, QB);
endmodule
answered May 4, 2017 at 1:23
\$\endgroup\$
1
  • \$\begingroup\$ This does not improve this simulation results. The n1 andn2 instances (and the C input) in the question are unnecessary. \$\endgroup\$ Commented Jan 19, 2024 at 19:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.