# SR-Latch

## Introduction

Latches are the fundamental bi-stable memory circuit in digital systems to store data and indicate the state of the system. In this project, we are going to implement and simulate the basic NAND cell of an SR-Latch and see how it functions.

##### Before you begin, you should:
• Have the Xilinx® ISE WebPACK™ installed.
• Have your FPGA board set up.
• Be able to describe digital circuit using logic operators.
• Be able to write test bench and simulate circuit using ISim.
• Be able to model and simulate circuit delay.
##### After you're done, you should:
• Understand the operations of an SR-Latch.
• Understand metastability of bi-stable circuit.
• Understand the difference between NAND cell and NOR cell.

## Inventory:

Qty Description
1 Digilent® Nexys™4, Nexys™3, Nexys™2, or Basys™2 FPGA Board
1 Xilinx ISE Design Suite: WebPACK (14.6 Recommended)

## SR-Latch NAND Cell

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:

				timescale 1ns / 1ps
module sr_latch(
input S,
input R,
output Q,
output Qn
);

wire Q_int, Qn_int;

assign #1 Q_int = ~(S & Qn_int);
assign #1 Qn_int = ~(R & Q_int);
assign Q = Q_int;
assign Qn = 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:

				initial begin
// Initialize Inputs
S = 1;
R = 1;

#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).

Now that you've completed this project, try these modifications:

1. Implement an SR-Latch using NOR Cell and simulate the NOR cell and see if you get a similar waveform as in Step 2.
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:
1. set operation
2. reset operation
3. confounded outputs (both outputs at the same voltage)
4. storing a value in memory
5. 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
3. 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!