$monitor(S,R,En,Q,Qbar);
// Initialize Inputs
S = 0;
R = 1; En = 1;
// Add stimulus here
#100 S = 0; R = 1;
#100 S = 1; R = 0;
#100 S = 1; R = 1;
#100 R = 1; R = 0;
#100 S = 0; R = 0;
#100 S = 1; R = 1;
#100 S = 0; R = 0;
En = 0;
#100 S = 0; R = 1;
#100 S = 1; R = 0;
#100 S = 1; R = 1;
#100 R = 1; R = 0;
#100 S = 0; R = 0;
#100 S = 1; R = 1;
#100 S = 0; R = 0;
$monitor(S,R,G,Q,Qn);
// Initialize Inputs
S = 0;
R = 1;
G = 1;
// Add stimulus here
#100 S = 0; R = 1;
#100 S = 1; R = 0;
#100 S = 1; R = 1;
#100 R = 1; R = 0;
#100 S = 0; R = 0;
#100 S = 1; R = 1;
#100 S = 0; R = 0;
G = 0;
#100 S = 0; R = 1;
#100 S = 1; R = 0;
#100 S = 1; R = 1;
#100 R = 1; R = 0;
#100 S = 0; R = 0;
#100 S = 1; R = 1;
#100 S = 0; R = 0;
Figure 1 below shows an implementation for SR-Latch with NAND implementation. According to the truth table on the right, S and R are active low. When only S is asserted (S is '0'), the output Q is SET to '1'. When only R is asserted (R is '0'), the output Q is RESET to '0'. When neither S and R are asserted, the output holds its previous value.
SR-Latch is a kind of bi-stable circuit. However, due to propagation delay of NAND gate, it is possible to drive the circuit into metastable state, where the output is oscillating between 0 and 1. The metastable state will be triggered when neither the set operation nor the reset operation propagates through the whole cell before the cell changes to the hold state.
Step 1: Implement the Circuit in Verilog®
Assume the NAND gates in SR-Latch have a delay of 1 ns. The Verilog file for the SR-Latch looks like follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
`timescale 1ns / 1ps
modulesr_latch(
inputS,
inputR,
outputQ,
outputQn
);
wireQ_int, Qn_int;
assign#1 Q_int = ~(S & Qn_int);
assign#1 Qn_int = ~(R & Q_int);
assignQ = Q_int;
assignQn = Qn_int;
endmodule
Step 2: Create the Test Bench and Simulate the Circuit
For the purpose of demonstrating the functionality of SR-Latch, we consider the following input simulus:
De-assert both inputs at the start of the simulation.
At 100 ns, asset S.
At 200 ns, de-assert S.
At 300 ns, assert R.
At 400 ns, de-assert R.
At 500 ns, assert both inputs.
At 600 ns, de-assert both inputs.
At 700 ns, assert both inputs.
The codes in the test bench looks as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
initialbegin
// Initialize Inputs
S = 1;
R = 1;
// Add stimulus here
#100 S = 0;
#100 S = 1;
#100 R = 0;
#100 R = 1;
#100 S = 0;
R = 0;
#100 S = 1;
R = 1;
#100 S = 0;
R = 0;
#100 ;
end
Figure 2 below shows the simulated waveform of the circuit; at 300 ns, Reset signal is asserted. From the zoomed-in graph on the left, it takes 2 ns for Reset operation to propagate through the NAND cell. At 600 ns, both S and R toggles from 0 to 1, resulting in a change between an unfinished SET/RESET operation directly to hold state, which finally triggers an astable state (usually referred as meta-stability).
Test Your Knowledge!
Now that you've completed this project, try these modifications:
Implement an SR-Latch using NOR Cell and simulate the NOR cell and see if you get a similar waveform as in Step 2.
Simulate the following input sequence on both a NAND cell and a NOR cell. Complete the following table by placing the correct letter in the output column:
set operation
reset operation
confounded outputs (both outputs at the same voltage)
storing a value in memory
a metastable state
Time
SET
RESET
NAND Cell
NOR Cell
100 ns
1→0
1→1
200 ns
0→1
1→0
300 ns
1→1
0→1
400 ns
1→0
1→0
500 ns
0→1
0→1
600 ns
1→0
1→0
700 ns
0→0
0→1
800 ns
0→1
1→0
Implement a D-Latch displayed in Fig. 3 below. Simulate it and try to drive it into a metastable state.
HINT: An SR-Latch is driven into metastability by issuing a store operation before a set or reset operation is finished. Similarly, the D-Latch can be driven into stability by issuing a latching operation before the new data is updated to the D-Latch.
Challenge Problem
If you are confident in your ability to implement and simulate the basic NAND cell of an SR-Latch, go ahead and try out the design challenge problem below for some extra practice!