<<

EC312 Lesson 9: Digital

Objectives: a) Identify the logic circuit gates and reproduce the tables for NOT, AND, NAND, OR, XOR, and NOR gates. b) Given a schematic of a logic circuit, determine the corresponding Boolean expression and for the circuit output.

I. Digital Logic and Fundamental Logic Gates1 Computers work with the binary number system; i.e. everything comes down to 1’s and 0’s. The two digits of the binary number system can be represented by the state or condition of electrical or electronic devices. For example, a binary 1 can be represented by a switch that is closed or a lamp that is lit, and a binary 0 would be represented by the same devices in the opposite state: the switch open or the lamp is off. Of course, a computer doesn’t just store data; it manipulates the data and produces desired outcome based on certain programmed logic statements. (Think about the if-else and for statements in C.) Another way to say this is that computers use the TRUE and FALSE logic conditions of a logical to make a programmed decision. Kind of convenient, since TRUE and FALSE can be represented as 1 and 0! But here’s the main question: how do you build a circuit (after all, a CPU is just a really, really complex electrical circuit) that can evaluate logical statements? That’s what we want to explore. 1. Digital concepts of logic The conditions of a statement can be represented by symbols (variables); for instance, the statement “The Naval Academy is in Annapolis” might be represented by the symbol P. Such a statement can have two conditions, either TRUE or FALSE. In computers, these two conditions are represented by electronic circuits operating in two LOGIC STATES. These logic states are 0 (zero) and 1 (one). Respectively, 0 and 1 represent the FALSE and TRUE conditions of a statement. If the statement “The Naval Academy is in Annapolis” is FALSE, then the statement “The Naval Academy is NOT in Annapolis” must be TRUE. This is called the COMPLEMENT of the original statement. In the case of computer math, complement is defined as the opposite or negative form of the original statement or variable. The complement is shown by placing a bar over the statement symbol (in this case, P̅). This variable is spoken as NOT P. The following table illustrates this concept and the relationship with logic states and logic levels.

Example: The Naval Academy is in Annapolis STATEMENT SYMBOL CONDITION LOGIC STATE LOGIC LEVEL Original: The Naval Academy is in P TRUE 1 HIGH Annapolis Complement: The Naval Academy is NOT in 푃̅ FALSE 0 LOW Annapolis

1 Portions of these lesson notes were adapted from the Navy Electricity and Electronics Training Series (NEETS) course notes, Module 13, September 1998. 1 2. The logic gates This binary logic can be extended to circuits. A switch for example has two different states: open (0) and shut (1). An output of the circuit could be a light (LED) which has two states: off (0) or on (1). We’ll examine a few different circuits and see how these switches can be linked together to perform various functions and even do computations. a. The AND gate Let’s hearken back to EE331 (yes, a long long time ago, back to last semester…) and consider the circuit shown below. If we think of the state of switch A and switch B as our device inputs (0=open, 1=closed) and the state of the light as our device output, we can apply our finely tuned analysis tools (you still have them, right?) to determine that the LED will only turn on if both A and B are closed. All other cases will result in the LED remaining off. This can be expressed in a truth table as shown below. The corresponding logic gate is known as the AND gate.

Truth Table for AND gate A B X X 0 0 0

0 1 0

X 1 0 0 1 1 1

The AND gate is represented by the symbol:

And can be written as: 푋 = 퐴 ∙ 퐵

Note that an AND gate can have more than two inputs. In this situation, all combinations result in a 0 output except for the case where every input is 1. (i.e., an AND gate requires a “unanimous vote”.)

2 b. The OR gate Now let’s consider the circuit shown below, again viewing our two switches, this time in parallel, as inputs and the LED as the output. In this case, the LED will light (i.e. X=1) if either the first switch or the second switch is closed (or if both are closed). In fact, the only case in which the LED won’t light is if both switches are open. The corresponding logic gate is known as the OR gate. The truth table is shown below.

A Truth Table for OR gate A B X B X 0 0 0

0 1 1 1 0 1 1 1 1 The OR gate is represented by the symbol:

A 푿 B

And can be written as: 푋 = 퐴 + 퐵 Note that an OR gate can have more than two inputs. In this situation, all combinations result in a 1 output except for the case where every input is 0. c. The NOT gate This time the switch is in parallel with the output (LED). In this case, the LED will light (i.e. X=1) only if the switch is open. If the switch is closed the LED will be shorted and not light. The corresponding logic gate is known as the NOT gate. The truth table is shown below.

Resistor Truth Table for

NOT gate

A X X A X 0 1

1 0

A NOT gate (also known as an ), is a logic device that has an output opposite of the input and is represented by the symbol:

And can be written as: 푋 = 퐴̅

3 d. The NAND gate The next gate is simply the combination of the NOT gate with the AND gate. This is known as a NAND gate, which is the exact opposite of the AND gate, i.e., the output is always 1 except for the case where both inputs are 1. The logic gate and truth table are below.

Truth Table for NAND gate

푨 A B X 푿 푩 0 0 1 0 1 1 The expression for a NAND is: 1 0 1 푋 = 퐴̅̅̅∙̅̅퐵̅ 1 1 0 e. The NOR gate By now you’re probably getting the picture of how this works. The NOR gate is equivalent to an OR gate connected in series with a NOT gate. It outputs a 1 only when both inputs are 0. The truth table and logic symbol are shown below.

Truth Table for NOR gate

A 푨 A B X 푿 B 푩 0 0 1 0 1 0 It can be written as: 1 0 0 푋 = 퐴̅̅̅+̅̅̅̅퐵̅ 1 1 0

f. The XOR gate The last gate we’ll discuss is the “”, or XOR, gate. The XOR gate outputs a 1 only if the two inputs differ from each other (i.e. one input is 0 and the other is 1). The truth table and logic symbol are shown below. Truth Table for XOR gate A B X 0 0 0 0 1 1 It can be written as: 1 0 1 푋 = 퐴 ⊕ 퐵 1 1 0 Practice Problem Fill in the truth table for the logic circuit shown below and complete the A B X expression.

푨 푿

X=

4 3. Logic gates in combination Most logic circuit diagrams are made up of many combinations of gates, with the outputs of some gates serving as inputs for others. At first it may seem confusing and complex, but if you interpret one gate at a time, you can work your way through any network.

Typically, our goal when analyzing a logic circuit is to determine the Boolean expression of the output and its corresponding truth table. This is best taught by example, so let’s consider the logic circuit shown below.

푩 푿

Here the outputs of the AND gate and the NOT gate become the inputs for the OR gate, so that the final output X will go High if either C is Low or both A AND B are High. The best way to tackle analyzing such a circuit is to take it one gate at a time from left to right, writing the Boolean expression at each output and working our way to the final output X. In this case, we know that the output of the AND gate is AB, and the output of the NOT gate is C̅, and therefore the final output X is given by X = AB + C̅, as shown below.

It’s important to write out the intermediate outputs at each gate, even if you think you can just “eye it up”. Many logic circuits are much more complex than this one, and it’s easy to make a mistake if you don’t approach this methodically.

To generate the truth table, it’s best to simply include additional columns for A B C AB 퐂̅ X the intermediate gate outputs, to help you determine the final gate output. For 0 0 0 0 1 1 the example shown here, we’ll include a column for the quantity AB as well as the NOT gate output C̅, and then we can use our OR rules to determine the 0 0 1 0 0 0 expression for X. The final truth table is shown to the right. 0 1 0 0 1 1

How did we know how many rows to put in our truth table? If we have a total 0 1 1 0 0 0 of N inputs to the system (in this case N=3 because we have inputs A, B, C), 1 0 0 0 1 1 then we need 2푁rows. You’ll note that we filled in the inputs for those rows by simply counting up from 000 to 111 in binary. Then we can determine the 1 0 1 0 0 0 ̅ quantity AB by applying the rules of our AND truth table, we get C by 1 1 0 1 1 1 inverting C, and we get the output X by applying our OR truth table rules to the values in the AB column and the C̅ column. 1 1 1 1 0 1

5 Practice Problem For the logic circuit given below, determine the Boolean expression for the output X, and construct the corresponding truth table.

Note that there are other (easier) ways to simplify Boolean expressions based on certain laws and identities, but that’s beyond the scope of this course. However, if you’re interested, you should search online for “Simplifying Boolean Expressions” and see the amazing methods that have been developed!

4. Adding Binary Numbers One necessary function for a computer is to add two binary number together. First we will examine adding two one-bit (one digit) numbers. The possible values for each input are 0 and 1 and therefore the possible outcomes are 0, 1, and 2. Unfortunately, a 2 will not fit in a binary numbering system and we must carry a digit. This is the same as in normal decimal addition: 6 +5 Truth Table for half-adder 11 A B C S The result (decimal 11) doesn’t fit into just the ones place, and therefore we carry a ten to the tens place. Using this same idea in the context of 0 0 0 0 binary addition, the possible outcomes of adding binary 0 and binary 1 0 1 0 1 are 00, 01, and 10. The first number is the Carry (C), and the second number is the Sum (S). This is shown on the truth table to the right. 1 0 0 1

1 1 1 0 Note that our Carry bit (C) can be represented as the output of A AND B, i.e. 퐶 = 퐴 ∙ 퐵. The Sum S is a little more complex, but can be represented as: 푆 = 퐴̅ ∙ 퐵 + 퐴 ∙ 퐵̅

We can use the following combinations of logic gates2 to obtain C and S from the inputs A and B: 푨

푩 C 푺 푩

Together this is called a half-adder, which adds two one bit numbers together.

2 We should note that there is often more than one way to implement a particular truth table with logic gates. Perhaps you noticed here that we could have used an XOR gate to obtain S from inputs A and B, instead of using the combination of AND, NOT, and OR gates shown here. In SX9, we’ll create a half-adder circuit out of four NAND gates and one NOT gate. 6 But what happens when you have bigger numbers? This gets slightly more complex. Again, we can use our intuition from decimal addition as a reference point. For example, to perform the following addition 36 Truth Table for full-adder +25 61 A B Cin Cout S we calculate the sum of 6 and 5, and then carry a 10 into the tens 0 0 0 0 0 column. This is equivalent to adding 3 numbers together in the tens column (1+3+2). This carries over to the binary realm. To 0 0 1 0 1 perform the following binary addition 0 1 0 0 1 012 +012 0 1 1 1 0 102 1 0 0 0 1 we have to carry a bit from the far right digit to the next digit. This means that we actually have to add three binary numbers 1 0 1 1 0 together in the second column, i.e. the Carry In (Cin) and the two 1 1 0 1 0 0 bits that are already there. Thus a full-adder requires three inputs (A, B and Cin) and two outputs (Cout and S). The truth 1 1 1 1 1 table is shown to the right. You’re not required to know how to build a full-adder from logic gates, but you can do an internet search for various implementations if you’re interested. (See https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder for example.)

Circuit that outputs Cout and S can be created and multiple full-adders and half-adder chained together to add multiple bits together. For example, the circuit shown here can add two 4-bit numbers, where HA represents a Half Adder and FA represents a Full Adder.

A3 B3 A2 B2 A1 B1 A0 B0

Cout FA FA FA HA

S3 S2 S1 S0

7