Laboratory: Digital Circuit Design
CSC 105 - The Digital Age

Summary: In this laboratory, you will use digital circuit design software to explore some fundamental components of digital circuits.

# Preparation

The software we will use for this lab is called LogiSim, or more formally, the "Logic Circuit Simulator." It is a nice program for beginners.

1. Open a terminal window, and move to your directory for 105 labs:
cd 105
2. Copy LogiSim to your directory with the following command:
cp ~weinman/courses/CSC105/labs/logisim.jar .
3. You can now run LogiSim with the command:
java -jar logisim.jar &
The larger portion of the window that appears with a grid of dots is the circuit drawing area.

# Part I: Getting Started with LogiSim

Take a look at the top of the window beneath the menus. You should see some icons: a hand, an arrow, a line, the letter A, and digital logic gates. This row of icons is also a sort of menu: you can click on one of them to activate it and then place an item onto the drawing area of the LogiSim window to add them to a circuit you are building.

## Exercise 1: Input switches

1. Let's start with the center icon, which is a green circle inside a square. Click on this icon. Now you may click somewhere on the drawing area. You should find that this creates a copy of the icon. This icon represents a switch, and it is used to specify an input value to a circuit.
2. Click the "hand" from the row of icons to change into "simulation" mode. (This also tells LogiSim that we do not want to draw any more input switches.)
3. Click in the inner square of the switch (on your drawing area) to see how it works. Each time you click the switch, this should toggle a green "light" on and off inside the switch. When the light is on, this means the input voltage is high (true or 1); when it is off, the voltage is low (false or 0).

## Exercise 2: Wires

1. Next let's experiment with adding wires to your circuit. To do this, click the arrow icon.
2. Move the mouse to the right side of your input switch where there is a green dot along the side of the square. When you move your mouse there, you should see a green circle appear. This tells you that when you click, it will begin connecting a wire from that switch at that location.
3. Click and drag the mouse to the right to add a wire. Wherever you release the mouse button will be the endpoint of the wire.
4. Click at the right endpoint of your wire and drag the mouse downward to add another wire segment, releasing it where you like. Note that the green circle follows your mouse, telling you where a wire would be atttached if you were to click.
5. Continue clicking to add a few more wire segments.

## Exercise 3: Moving Circuit Elements

1. Clicking the arrow from the icon menu (which you have already done) also allows you to reposition elements in your circuit drawing.
2. You can now move your switch to a new location in the drawing area by simply clicking and dragging it. Go ahead and give this a try.
3. Unfortunately, you cannot move wires that way. Instead, you must click and drag your mouse on the drawing window to draw a rectangle around the wires you want to move, then lift the mouse button. Notice that the wires inside the box are now selected (white boxes appear at their endpoints). You can move move all of the selected wires by dragging one to a new location. (You probably won't need to move wires often, but go ahead and give it a try.)
4. To unselect these wires, click anywhere on the drawing area (except on another circuit element).
5. Like many applications you may be used to, you can also select multiple circuit elements (including wires), by holding down the Shift key as you click on them. Give that a try.

## Exercise 4: Deleting circuit elements

1. Now lets delete some or all of the wires you just drew. To do this, begin by selecting some of the wires using either strategy given above. (Don't forget to make sure the arrow from the icon menu is selected first!)
2. To delete the selected elements, simply type the "Delete" key. You may also use the usual cut/copy/paste clipboard operations found under the edit menu.

## Exercise 5: Output Pins (LEDs)

Another icon in the icon menu consists of two concentric circles (the inside being green). This icon represents an LED (a light) that can be used to show the logical output of a circuit you have drawn.
1. Place an output pin (LED) somewhere in your drawing area.
2. Draw a wire from your input switch to the LED.
3. Now click the input switch to turn it on and off. (Don't forget to switch to simulation mode.) If your circuit is connected correctly, you should see the LED turn on and off as well.
Similar to the light inside the switch, when the LED light is on this indicates that it is receiving a logical one, and when it is off this indicates a logical zero.

## Exercise 6: Logic gates

1. Find the icon for an AND-Gate among the icons at the bottom of the window, and click to drop a gate into your drawing area. If you have look carefully, you should see tiny blue dots at each of the input and output points of the gate. These are points where you can connect the gate to other elements of your circuit.
2. Connect two input switches to two input points of your AND-gate, and an LED to its output point. You can do this by placing the various elements in direct contact with each other, or by drawing wires between the points you want to connect.
3. Test all the possible input combinations (i.e., both switches off, switch one on and switch two off, etc) to convince yourself that your circuit and the AND-gate are working correctly.
4. Finally, do the same with both the OR-gate and the NOT-gate icons. (I.e., connect each of these icons to input switches and LEDs, and then convince yourself that they generate the correct output for each input combination).

# Part II: Your First Digital Circuits

In this part, you will start drawing real circuits. You should begin by creating a new drawing: select "New" from the File menu.

## Exercise 1

1. Draw a circuit diagram that uses two AND-gates to create a "three-input AND-gate." While the default AND-gates in LogiSim have several inputs, you should treat them as if they only had two-inputs.

Recall that we discussed this circuit in class: its output should be (x1 AND x2 AND x3), where x1, x2, and x3 are the circuit's three input values.

2. Test your circuit by trying all eight input combinations (i.e., all inputs off, only x1 on, etc). Your output LED should only light up when all of the input switches are on.
3. Save your circuit by selecting "Save" from the File menu. Please give it the name AND-3.circ.
4. Now, in the Linux terminal window, look at a listing of the files in your 105 directory. You should see that a file named AND-3.circ has appeared.

## Exercise 2

Recall from class the concept of the "XOR" operator. As shown in the logic table below, (x1 XOR x2) should be true when exactly one of the inputs x1 and x2 are true.
 x1 x2 x1 XOR x2 0 0 0 0 1 1 1 0 1 1 1 0
1. Please begin by creating a new drawing (select "New" from the File menu).
2. Refer to your notes from class to remember what the circuit diagram that implements the XOR operator (with AND, OR, and NOT-gates) looks like. Then draw the circuit diagram, noting that is ok for wires in your diagram to cross one another; the circuit will behave as if the wires are not touching one another. However, if one wire starts from somewhere along another wire, the two will be "soldered," as indicated by a dot covering the intersection.
3. Test your circuit to make sure that it produces the correct output value for each of the input combinations.
4. Save your drawing in a file called XOR.circ

Recall the "half-adder" circuit we drew in class. This circuit has two inputs and two outputs. The inputs are two bits that we want to add together, and the outputs are the "carry-out" and "sum" bits that result. The logic table for the circuit is given below.
 x1 x2 carry-out sum 0 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0

Please review your notes to remember what the circuit diagram for a half-adder looks like. You should see that the circuit uses an XOR-gate. Since you have just created an XOR-gate it would be possible to cut and paste your XOR circuit to use here. However, there is also another way: LogiSim provides an XOR gate, but you have to know where to look for it. We'll do that next.

## Exercise 1

1. Please create a new drawing. (You may close any of the others after you create a new, blank drawing.)
2. On the left side of your window is a tree with at least two folders displayed, including one entitled "Base" and the other entitled "Gates". Click the triangle to the left of the "Gates" folder to expand it.
3. Now you should be able to scroll down and see a whole host of gates, some familiar and some new.
4. One of the gates that appears should be the XOR gate. You may click it to select, just as you did the items from the icon menu. Now you can drop XOR gates into your drawing.
If the steps above did not work for you, please ask for help.

## Exercise 2

1. Once you have an XOR gate, implement a half-adder circuit in LogiSim. You may find it helpful to arrange your two outputs such that "carry-out" is to the left of "sum". This way, the two output bits can be read as a normal two-digit binary number, making it easy to test your circuit.

Remember that you can connect wires by starting one wire where it touches another.

2. Click the "A" in the menu to activate the text tool. Use this to label the inputs and outputs of your half-adder (e.g., with the same names that appear in the truth table above.)
3. Test your circuit. It should display the sum of the two input bits, which will always be 0, 1, or 2 (displayed in binary).

# Part IV: Flip-flops

Please review your notes to recall what the circuit diagram for a flip-flop looks like. (Actually, it is worth noting that there are several different ways to implement flip-flops, but we will work with the version drawn in class.)

Here is the logic table for this circuit. Remember that the first three columns are input values, and the last column is the sole output. (Notice that I have left out the last two lines of the logic table, where both x1 and x2 are 1, because these input combinations are not used in practice.)

 x1 x2 out(before) out(after) 0 0 0 0 0 0 1 1 0 1 0 0 0 1 1 0 1 0 0 1 1 0 1 1

## Exercise 1

Before you implement the flip-flop circuit, it may be helpful to notice the following.
1. Select the AND-gate icon from the icon menu of icons. Note that a table appears in the bottom-left portion of the LogiSim window.
2. One of the entries in the table is "Facing", and it has the value "East." Click on "East" to activate a pull down menu.
3. You may select "West" to create AND gates that face the reverse direction, as the circuit we drew in class does.

## Exercise 2

Now you are ready to build the flip-flop circuit. Please do so. Be sure to label your inputs and outputs!

## Exercise 3

1. Set x1 = 1 and x2 = 0. This should "set" the output to 1.
2. Now "lower" the x1 input value, so that both inputs are off (0). You should see that the output value is unaffected by this. It should continue to hold it's previous value (1).
3. Now "raise" the x2 input to 1. At this point, you should have x1 = 0 and x2 = 1. The result should be that the output is "reset" to 0.
4. Finally, "lower" x2 again, and you should find that your circuit once again holds its previous value (0).
Congratulations! You have created a one-bit memory element!

## Exercise 4

Save your work in a file called flip-flip.circ

You will be called upon to print some of your circuits for turn-in as homework. You may follow these steps to do so:
1. Start LogiSim, using steps (a) and (c) from Preparation (if you haven't already).
3. Select "Print..." from the File menu.
4. A dialog box titled "Print Parameters" will appear. Leave everything as it is and click "OK."
5. Another dialog box titled "Print" should appear. From the pull down menu called "Name:" be sure that the entry "duerer" is chosen.
6. Click "Print"
7. Repeat these steps, probably from (b) on, for any other files you wish to print.
8. The printer duerer is located in SCI 3810, the small room adjacent to our classroom.

# For Those With Extra Time

## Exploration 1

There is an old puzzle in which a farmer (F) must transport a wolf (W), a goose (G), and a sack of corn (C) across a river, subject to the conditions that the farmer can only transport one of W, G, or C across the river at a time, and if unattended, the wolf will eat the goose and the goose will eat the corn. Let F=0 indicate the presence of the farmer on the west bank of the river and F=1 indicate presence on the east bank. Make similar assumptions for W, G, and C.

### Exercise 1

Derive a truth table defining a function which gives 1 if the farmer is in danger of losing the goose or the corn. Assume that if an item (or farmer) is not on one side of the river it must be on the other side. In other words, fill in the following table with the appropriate values:

F W G C Danger
0 0 0 0 ?
0 0 0 1 ?
0 0 1 0 ?
...
1 1 1 0 ?
1 1 1 1 ?

### Exercise 2

How did you derive the truth table? You probably thought about which settings of the variables F,W,C and G indicated an unsafe state. For instance, (W AND G AND NOT F) would be one indicator of an unsafe state.

Write a logical sentence using AND, OR, and NOT that expresses the truth table above.

### Exercise 3

Implement your logic expression as a circuit in LogiSim. Verify that it is correct by testing the possible states with input and output pins.

## Exploration 2

A half-adder basically stands in isolation because it can't take the carry from a previous column as input. The full-adder therefore incorporates not only the two bits of a number to sum, but also the carry-out bit from the previous column as a "carry-in" input bit.

The truth table for a full-adder would look like the following:
 carry-in x1 x2 carry-out sum

### Exercise 1

Complete the truth table for a full-adder. Essentially, you are calculating the sum of the three bits for each row.

### Exercise 2

Using the truth table, draw a circuit for which the carry-out value is true under the appropriate conditions. Note that if you think carefully about the cases when carry-in is 0 and carry-in is 1 separately, you may be able to produce a simpler circuit.

### Exercise 3

Using the truth table, draw a circuit for which the sum value is true under the appropriate conditions. Note that if you think carefully about the cases when carry-in is 0 and carry-in is 1 separately, you may be able to produce a simpler circuit.

### Exercise 4

Combine your two circuit designs, implementing them in LogiSim.

### Exercise 5

Test the input values to make sure they produce the right output value.
Written: Marge M. Coahran, April 2008
Revised: Jerod Weinman, 17 March 2009
Revised: Marge Coahran, 14 April 2010
Revised: Jerod Weinman, 22 March 2011 & 15 April 2011