title: Lab 5: Digital circuits
***Because [Problem Set 5][ps5] is all due on Thursday, these lab
exercises are optional, as is attendance at the labs this week.***
During the labs, the course staff will be happy to go over these
problems with you, or to help you with [Problem Set 5][ps5]. You may
also want to use these problems for extra practice later in the
semester.
[TOC]
### Task 0: Become familiar with Logicly
In [Problem Set 5][ps5], you will be using a circuit-design tool
called Logicly. This tool is available on the lab machines, and the
problems set includes [instructions for installing and using it][prep]
on your own machine.
**Other resources:**
* We strongly encourage you to watch [a video on Logicly][video]
that was created by our illustrious former staff member, Alex Breen.
It provides a good overview of how to use the program.
* The Logicly documentation is available [here][logicly-doc] or
by selecting the Help->Contents menu option.
### Task 1: Practice minterm expansion
Consider the truth table below, which represents an unknown boolean
function:
| x | y | z | boolean function's value |
|---|---|---|:------------------------:|
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 0 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 0 |
| 1 | 0 | 1 | 1 |
| 1 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 |
On paper, create the *minterm expansion* formula for this truth table.
Here is a reminder of the steps:
1. Delete all rows from the truth table where the value of the
function is 0.
2. For each remaining row, create a *minterm* as follows:
* For each variable that has a 1 in that row, write the name of the
variable. If the input variable is 0 in that row, write the variable
with a negation symbol to NOT it.
* AND all of these variables together (using multiplication).
3. Combine all of the minterms for the rows using OR (i.e., addition).
### Task 2: Practice building a circuit
Once you have completed the formula for Task 1, use Logicly to build
the corresponding circuit.
To begin, download the following file:
[lab5task2.logicly](../files/labs/lab5/lab5task2.logicly)
and open it in Logicly.
Note that we’ve provided you with:
* switches for the inputs `x`, `y`, and `z`
* a `NOT` gate for the negation of each input; for example, the
leftmost switch is for `x` itself, and the `NOT` gate immediately to
its right is for `NOT x`
* a light bulb for the output.
You may need to use the *Pan Tool* (the hand icon available at the top
of the screen) and the magnification slider available in the lower
right-hand corner to adjust how much of your circuit is
visible. **After using the *Pan Tool*, don’t forget to switch back to
the *Selection Tool* (the arrow icon).**
When constructing the circuit, remember that:
* each minterm in the minterm expansion should get its own AND gate
* the outputs of the AND gates should be fed into an OR gate.
Once you have completed the circuit, test it by clicking the input
switches to turn them on and off. Check to see if you obtain the
correct output for all possible inputs.
***After testing, make sure to save the final version of your circuit.***
### Task 3: More practice with circuits
1. Build a circuit that represents a `NAND` gate, which has the following
truth table:
| x | y | x NAND y |
|---|---|:--------:|
| 0 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Use the following file for your work:
[`lab5task3-1.logicly`](../files/labs/lab5/lab5task3-1.logicly)
***Once you are done, make sure to save the final version of your
circuit.***
2. To practice exporting and importing a circuit in Logicly, let's
export your NAND circuit and use it in another circuit.
**Exporting your NAND circuit:**
1. In your `lab5task3-1.logicly` file, use the *Edit->Select All*
menu option to select the entire circuit.
2. Click the building block icon (which looks like a Lego piece)
at the top of the screen. (Alternatively, you can select the
*Edit->Create Integrated Circuit* menu option.)
3. In the window that pops up, give your circuit a name (e.g., `My
NAND`) by entering it in the *Full Name* text box. You can also
add a *Symbol Label*, but doing so is not required.
4. Click the *Create IC* button to create the integrated circuit.
5. Use the *File->**Export** Integrated Circuit Library* menu option
to export your work. Give the file an appropriate name (e.g.,
`my_nand`) and save it in a place where you can easily find it.
**Using your NAND circuit in another circuit:**
6. Download the following file:
[`lab5task3-2.logicly`](../files/labs/lab5/lab5task3-2.logicly)
and open it in Logicly.
7. Use the *File->**Import** Integrated Circuit Library* menu option
to import the file in which you stored your exported NAND
circuit in step e.
8. After importing the file, scroll down to the bottom of the
left-hand pane of components. You should see a section labeled
`Custom` that contains a lego-like building block for your
NAND circuit. (Do **not** use the built-in NAND gate under
`Logic Gates`. Use your building block under `Custom`.)
9. You can insert a copy of your NAND building block into your circuit by
using the *Selection Tool* to drag the NAND building block
into the circuit. You should see a box that looks like this:

![nand box][nand_box]

10. Add wires to connect :
* the input switch for `x` to the pin `x` in your
NAND building block
* the input switch for `y` to the pin `y` in your
NAND building block
* the output of your NAND building block to the output light bulb
of the circuit.
11. Test different combinations of values for the inputs, and you should
see that your NAND building block behaves exactly the way that your
NAND circuit did!
12. ***Once you are done, make sure to save the final version of your
circuit.***
3. Build a partial *arithmetic logic unit* that accepts three inputs:
`x`, `y`, and `op`, where a value of 0 for `op` means that the
circuit should compute and output `x + y` (with carry) and a value of
1 for `op` means that the circuit should compute and output `x * y`.
Begin by filling in the following truth table:
| op | x | y | | r1 | r0 |
|----|---|---|-|:---|---:|
| 0 | 0 | 0 | | | |
| 0 | 0 | 1 | | | |
| 0 | 1 | 0 | | | |
| 0 | 1 | 1 | | | |
| 1 | 0 | 0 | | | |
| 1 | 0 | 1 | | | |
| 1 | 1 | 0 | | | |
| 1 | 1 | 1 | | | |
where `r1` and `r0` are the two bits of the result:
x
y
-----
r1 r0
In the case of addition, `r1` is the carry bit. In the case of
multiplication, `r1` will always be 0.
Next, use minterm expansion twice -- once for each result bit -- and
create a separate subcircuit for each of those bits.
Use the following file for your work:
[`lab5task3-3.logicly`](../files/labs/lab5/lab5task3-3.logicly)
***Once you are done, make sure to save the final version of your
circuit.***
[minterm]: http://www.cs.hmc.edu/csforall/ComputerOrganization/ComputerOrganization.html#making-other-boolean-functions
[ps5]: ../problem_sets/ps5.html
[prep]: ../problem_sets/ps5.html#preparing-for-the-remaining-problems_1
[video]: https://www.youtube.com/watch?v=CgK_3j7VIIg
[logicly-doc]: http://logic.ly/help/?utm_source=logicly-desktop&utm_content=menu-help
[nand_box]: ../files/labs/lab5/nand_box.gif