Code_ The Hidden Language of Computer Hardware and Software - Charles Petzold [59]
Inputs
Outputs
D
Clk
Q
Q-bar
0
1
0
1
1
1
1
0
X
0
Q
Q-bar
This circuit is also known as a level-triggered D-type latch, and that term simply means that the circuit latches onto one bit of data and keeps it around for further use. The circuit can also be referred to as a 1-bit memory. I'll demonstrate in Chapter 16 how very many of these flip-flops can be wired together to provide many bits of memory.
Saving a multibit value in latches is often useful. Suppose you want to use the adding machine in Chapter 12 to add three 8-bit numbers together. You'd key in the first number on the first set of switches and the second number on the second set of switches as usual, but then you'd have to write down the result. You'd then have to key in that result on one set of switches and key in the third number on the other set of switches. You really shouldn't have to key in an intermediate result. You should be able to use it directly from the first calculation.
Let's solve this problem using latches. Let's assemble eight latches in a box. Each of the eight latches uses two NOR gates and two AND gates and one inverter, as shown previously. The Clock inputs are all connected. Here's the resultant package:
This latch is capable of saving 8 bits at once. The eight inputs on the top are labeled D0 through D7, and the eight outputs on the bottom are labeled Q0 through Q7. The input at the left is the Clock. The Clock signal is normally 0. When the Clock signal is 1, the 8-bit value on the D inputs is transferred to the Q outputs. When the Clock signal goes back to 0, that 8-bit value stays there until the next time the Clock signal is 1.
The 8-Bit Latch can also be drawn with the eight Data inputs and eight Q outputs grouped together as you see on the following page.
Here's the 8-Bit Adder:
Normally (ignoring what we did with subtraction in the last chapter), the eight A inputs and eight B inputs are connected to switches, the CI (Carry In) input is connected to ground, and the eight S (Sum) outputs and CO (Carry Out) are connected to lightbulbs.
In this revised version, the eight S outputs of the 8-Bit Adder can be connected to both the lightbulbs and the D inputs of the 8-Bit Latch. A switch labeled Save can be the Clock input of the latches to save a result from the adder:
The box labeled 2-Line-to-1-Line Selector lets you choose with a switch whether you want the B inputs to the adder to come from the second row of switches or from the Q outputs of the latches. You close the switch to select the outputs from the 8-Bit Latch. The 2-Line-to-1-Line Selector uses eight of the following circuits:
If the Select input is 1, the output of the OR gate is the same as the B input. That's because the output of the top AND gate is the same as the B input, and the output of the bottom AND gate is 0. Similarly, if the Select input is 0, the output is the same as the A input. This is summed up in the following function table:
Inputs
Outputs
Select
A
B
Q
0
0
X
0
0
1
X
1
1
x
0
0
1
x
1
1
The box shown in the revised adding machine comprises eight of these 1-bit selectors. All the Select inputs are wired together.
This revised adding machine isn't handling the Carry Out signal very well. If the addition of two numbers causes the Carry Out signal to be 1, the signal is ignored when the next number is added in. One possible solution is to make the Adder, the Latch, and the Selector all 16 bits wide, or at least wider than the largest sum you'll encounter. I won't really be solving this problem until Chapter 17.
A more interesting approach to the adding machine eliminates an entire row of eight switches. But first we need to modify the D-type flip-flop slightly by adding an OR gate and an input signal called Clear. The Clear input is normally 0. But when it's 1, the Q output becomes 0, as shown here:
This signal forces Q to be 0 regardless of the other input signals, in effect clearing the flip-flop.
Why do we need this, you might ask? Why can't we clear the flip-flop