Computer Science Lab#4
© Dmitriy Shironosov/ShutterStock, Inc.
LABORATORY Logic Circuits
6
■■ Experiment■with■digital■logic■circuits■using■a■simple■logic■gate■simulator.
■Software■needed: ■ 1)■ LogicGates■app■from■the■Lab■Manual■website■(Logic■Gates.jar)■
OBJECTIVE
REFERENCES
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
Logic Circuits | 43
BACKGROUND Review these topics from your textbook, lecture notes, or online resources:
■ Logic gates: AND, OR, NOT, XOR, NAND, NOR ■ Truth tables, combinational circuits, gate processing, 1-bit full adders, multiplexers
ACTIVITY Digital logic circuits are the bedrock of computer design. CPU chips from companies like Intel and AMD are silicon wafers in which millions of circuits are embedded. Everything your computer does depends upon them.
In this lab, we will learn how to use the LogicGates app to draw and test digital logic circuits. Chip manufacturers, such as Intel and AMD, use sophisticated (and extremely expensive) software to help them design and test their chips. But the basic idea of what we will be doing is the same.
First, start the LogicGates app. You can load a number of examples by pulling down the example choice pull-down menu. Load the example labeled 1 simple, as shown here:
Each logic gate has a distinctive shape. The wire coming out of the gate shows its current value. Gates are oriented so that their inputs come in from the left side and their outputs go out from the right side. This design cannot be changed; nor can the gates be rotated in this app.
Two special boxes exist for input and output. The switch box has a lever in it along with 1 or 0. To flip the lever and change the value, click on the number right in the center. The other box has only 1 or 0 in it. This is the output box, which accepts a wire from a gate and displays its value.
To propagate new values through the circuit, click on the Run button. It changes to say Stop, so that when you click on it again, the simulator stops running. When the circuit is running, you can click on the switches and watch values change throughout the circuit.
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
44 | Laboratory 6
To practice using the simulator, we’ll build the logic circuit encoding the Boolean expression (AB) OR (AC), which will require three total switches and three total gates connected sequentially. First, if your simple example circuit is still running, click the Stop button. Now, clear the screen by clicking the Clear button. To build the circuit, you’ll need to add three switches, two AND gates, one OR gate, and one output box. To create them, click on the New button, pull down the gate menu, and select the gate or other item you want. Then click once where you want the switch, gate, or output box to sit. You can reposition gates, switches, and outputs by dragging them to a new location.
Notice that when you are adding gates, the New button changes to say *New. Most of the buttons in this app work like this: They show they are still active by displaying an asterisk in front of the label on the button. Because the button stays active, you can click on different gates without having to reclick the New button over and over.
Click on the New button again to stop adding components. The asterisk will go away. Let’s also attach labels to the boxes. However, in the logic gate simulator, labels
attach to boxes, not wires, as you may see in textbooks or other diagrams. First, click on the Label button, which allows us to label gates and switches. Then
click on the top switch. A text field opens up right above the switch. Type the letter “A” into it and press Return.
Here’s what you should see:
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
Logic Circuits | 45
Label the middle switch as “B” and the bottom switch as “C.” Also label the output “X.” If you want to be thorough, label the top AND gate as “D” and the bottom AND gate as “E.” When done labeling, click on the Label button so that the asterisk doesn’t appear.
Now it’s time to connect the gates, so click on the Connect button. Click once on switch A. A red line flashes into view, anchored in the center of the switch and following the mouse pointer around the screen. Click on the top AND gate. Repeat this process and connect all the boxes so they match the screenshot below. If you make a mistake, click on the Disconnect button, then click on a gate. All lines between that gate and all other components will be removed.
A gate’s output may have any number of wires coming out of it so that it connects to more than one other gate. Merely select the same gate as the left end of a wire more than once.
Automatic elbows break lines up into horizontal and vertical segments. To see a version that uses diagonal lines, click on the Stepped button. However, in most circuit fabrication technologies, all wires must lie on a grid, so diagonal wires are either impossible or expensive. To see the elbowed lines again, click on the bottom button, which has changed from Stepped to Diagonal.
Sometimes the elbows make it hard to see overlapped wires, and that plagues this circuit; move the bottom AND gate a little to the left until the wires show more clearly.
Here’s the final picture:
Now run the circuit by clicking the Run button. Click on the switches to change their values. Record the results in a truth table.
Imagine what a terrible thing it would be if a major chip manufacturer’s simulation software didn’t give the right values and they sent a chip into production with flaws in it. Well, actually, don’t bother imagining: It’s already happened! In the early 1990s, Intel recalled its new Pentium processor when scientists and engineers pointed out that in certain calculations the chip gave the wrong answer! Analysts have estimated that this bug in the chip’s division algorithm cost the company about $500 million. (Too bad they weren’t using our simulator to check their circuit design!)
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
46 | Laboratory 6
Our simulator provides you with six types of gates: AND, OR, NOT, NAND, NOR, and XOR. All of these are defined in most textbooks and many online references. Another type of “gate” available in our circuit simulation is the truth table, which allows you to avoid building really complicated circuits with lots of gates. Instead, you fill in the truth table values and the simulator treats it as a gate. A truth table “gate” can have two, three, or more inputs.
Let’s build a circuit using truth tables. Clear the screen and add three new switches and one output.
The trickiest part is putting the correctly-sized truth table into the circuit. To do this, make sure the New button is activated (has an asterisk next to *New). Then pull down on the Gates menu and bring Truthtable into view. In the yellow message box, LogicGates will ask you how many inputs your truth table should have. The default is 2, but since we have 3 switches, replace the 2 with 3. Then click once in the middle of the area to create the truth table. Click off the New button and connect these gates as shown below:
Important note! Make sure that when you connect the switches, you connect the top one first, then the middle one, and finally the bottom one. Using the proper sequence is necessary because the order of the incoming wires determines which value is associated with which column in the truth table. The first wire connected will correspond to the first column in the table, the second wire will correspond to the second column, and so on. If you connect them in a different order, the wires going into the truth table will not correspond to the columns of your truth table.
New truth tables list all the input values in canonical order, but since the output is shown as always 0, we need to edit the truth table so it implements our desired output. To edit a truth table, click once on it (after making sure that all the buttons on the left are off [i.e., do not have asterisks in front of their names]). This makes the truth table the top gate, and its border will turn red. Now click on EditTT. A green window pops up, allowing you to edit this truth table.
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
Logic Circuits | 47
The input values are separated from the output value by a vertical bar symbol, (|). Go through the truth table and change some of the output values, as shown in the picture below.
Once you are done, click OK to dismiss the truth table editing window, and then click on Refresh or just drag the truth table around so that the simulator will repaint the screen.
Run the simulator and play with the switches, comparing the value in the output box to what you see in the truth table. Do they match?
Since the truth table “gate” bypasses digital logic gates altogether and implements the given truth table, it might seem like using it is cheating! Actually, the truth table is similar to something called an SSI chip. Sometimes we are more interested in the function that the circuit computes (the pairing up of inputs and outputs) than in exactly which gates are needed to implement that function.
By the way, if there are only two inputs to the truth table, there will be two columns and four rows. If there are three inputs, there will be three columns and eight rows, as shown in the example. Four inputs would require four columns and 16 rows, and five inputs would require five columns and 32 rows. Do you see a pattern?
The input values in all the truth tables you see in this lab are shown in canonical (standard) order, which is the professional convention. This isn’t necessary for the simulator to work properly, but canonical order helps us fallible, distracted humans keep track of our inputs and outputs.
TIP LogicGates can load and save your programs. It saves the data in a plain text file format.
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION
60 | Laboratory 6
DEEPER INVESTIGATION The LogicGates app can create some very complicated circuits. Its main limitation is due to the finite amount of space available—if you have very many gates, you’ll eventually run out of room. However, judicious use of the truth table box can cut down on screen clutter.
Example 5 of the sample circuits in the app is a model of a circuit called an S-R latch, also known as a “memory latch.” Try running it. When it starts, the output boxes will cycle between 0 and 1. To stop that, click on one of the switches so that it reads 1. Now the circuit is stable. Experiment with it by setting one of the inputs to 1, then changing it back to 0, and watch the outputs change.
S-R latches are 1-bit memories built out of logic gates. Real computer chips use something like them in the main CPU for registers and other small, high-speed storage. Because the S-R latch has some serious timing problems, variants of it such as the clocked D-latch are used instead, as are even more complicated circuits called “flip-flops.”
S-R latches are circuits that have a peculiar characteristic about their wiring. Can you tell what it is? If not, pretend that the top switch pours a blue-colored liquid through the wires and the bottom switch pours a red-colored liquid. Trace the liquids with colored pens. What do you notice? What would you call this characteristic of S-R latch circuits? Your teacher might be able to give you a hint.
Because of this characteristic, circuits like the S-R latch are called “sequential circuits.” The output of sequential circuits depends not just on the current inputs, but also on the previous values on all the wires. They give a different sequence of results over time. Straight-through circuits like the others in this lab are called “combinational circuits” because their outputs depend only on the combination of their input values.
Try to find out more about logic circuits. Look in some hardware books for an example of a flip-flop circuit. Try tracing its workings.
© Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION