# Lab 4: Latches and Flip-Flops

In this lab, you will build your understanding of memory elements by building an S–R latch, a D latch, and a D-type flip-flop. In the last step, you can implement either a simple register file or a “neat” light display using latches.

## Groups

• Sara and Kamal
• Sophie and Hattie
• Hamza and Ryan
• Linda and Blake
• Jerry and Bea
• Theo and Tanner
• Devin and Ana
• Charlie and Eli

## Step 1: Build an S–R latch

Build an S–R latch on yor protoboard using the following circuit diagram:

Image from Wikipedia

Test your S–R latch. How do you know it is storing a value?

## Step 2: Build a D latch

Add the necessary gates to turn your S–R latch into a D latch. Recall that a D latch has two inputs and two outputs:

input $D$
The data value that should be stored in the latch
input $C$
The clock input. When this pin is high, the value on $D$ is stored in the latch. If $C$ is low, then the stored value should not change. Note that this input is sometimes called $E$, for “enable.”
output $Q$
The stored value in the latch
output $\bar{Q}$
The inverse of the stored value

Test your D latch to verify that it stores the correct value, and only updates the stored value when the clock input is high.

## Step 3: Drive your D latch with a clock

Connect the $C$ input of your D latch to the clock signal on the left side of your protoboard. You should hook a logic indicator light up to the clock input so you can monitor it. Set your clock on the TTL option, square wave, and a low frequency (Hz, not KHz, and 1, not 10 or 100).

With the clock set slow enough, you should be able to watch the value on $D$ update the latch state only when the clock is high.

What happens when you change $D$ while $C$ is still high?

## Step 4: Build a D flip-flop

Build a second D latch and connect it to your first D latch to build an edge-triggered flip-flop. The following figure shows a D flip-flop that stores an input value on the clock’s falling edge.

Once you have completed this step, please have Charlie or a mentor sign off on your circuit.

Image from Patterson & Hennessy

For the final step of this lab, you may choose one of the two exercises. You must complete at least one, but you do not need to complete both to earn full credit on the lab.

Once you have completed one of the options, please have Charlie or a mentor sign off on your circuit.

### Option A: Flip-Flop-Flip-Flop

Chaining two D latches together does something interesting with your data input. Every time the clock “ticks,” the value moves from the first D latch to the second. You can use this behavior to drive LEDs in an interesting pattern.

1. Build two more D latches and chain them onto the first two in your flip-flop. Your D latches should alternate between the $C$ and $\bar{C}$ inputs so a value moves one step on each clock tick.
2. Hook the $Q$ output of each D latch to a logic indicator.
3. With your circuit connected to the clock on your protoboard, turn the $D$ input to your first latch on and off. What happens? Try changing the clock frequency using the slider on the protoboard.

Extra Challenge: Can you hook the latches up in a loop so a single light moves through the latches over and over again? You will need to disconnect your latches from each other to initialize the stored state in each latch, then hook them together in a loop and start the clock without turning off the board.

### Option B: Build a Simple Register File

Using the two D latches you built for your flip-flop, implement a simple two-bit register file. Your circuit should have the following inputs and outputs:

input $A_R$
The address of the register that should be read. This will either be $0$ for the first D latch or $1$ for the second D latch.
input $A_W$
The address of the register that should be written to.
input $W$
If true, writing is enabled
input $D$
The value that should be written to the specified register, if writing is enabled.
output $Q$
The value of the register specified by $A_R$.

This circuit will require a simple decoder and multiplexor. The one bit versions of these components can be built with very few gates, so try to think up a simple implementation before building your circuit.

### Citations

Acknowledgment: This lab is adapted from a lab introduced to Grinnell College by Marge Coahran and updated by Janet Davis.