Gate Logic
From gate diagrams to Hardware Description Language (HDL).
Give Boolean Functions a Physical Form
A logic gate gives a physical form to a Boolean function.
A Gate Implements a Boolean Function
a, boutA gate is a physical device that implements a Boolean function.
Input pins receive binary values. The gate computes the function and places the result on its output pin.
For every input combination, the output must agree with the function's truth table.
Next: Learn the standard symbols used to draw gates.
Six Familiar Functions, Six Gate Symbols
A small circle on the output means negation. The extra curved line distinguishes XOR from OR.
Primitive Gates Can Build Composite Gates
Primitive gate: a gate available as a basic building block. For our computer, NAND is the primitive gate.
Composite gate: a gate constructed by connecting gates already available to us.
out = (a · b) · c
out = a · (b · c)
Draw the Gate Diagrams
Use only AND, OR, and NOT gates. Label every internal wire.
a · b + a · b
a · b
Next: Replace the drawing with a precise textual construction.
Connect Expressions and Gate Diagrams
Write the truth table for a + b · c.
Draw its gate diagram using AND, OR, and NOT gates. Label every internal wire.
Draw a gate diagram for the same function using only NAND gates.
Next: Describe a circuit precisely without relying on a drawing.
Hardware Description Language
A Hardware Description Language (HDL) describes the parts of a circuit and how their pins are connected.
Diagrams communicate structure, but large designs become difficult to reproduce and change.
HDL states which parts exist and exactly how their pins are connected.
Test the described circuit before building a physical device.
We move from diagrams to HDL because a textual description is precise, easier to change, and can be tested by a hardware simulator.
Describe the XOR Construction in HDL
CHIP Xor {
IN a, b;
OUT out;
PARTS:
Not(in=a, out=nota);
Not(in=b, out=notb);
And(a=a, b=notb, out=w1);
And(a=nota, b=b, out=w2);
Or(a=w1, b=w2, out=out);
}
CHIP, IN, OUTThe chip's name and the pins visible to someone using it.
PARTS:The lower-level gates used to build XOR.
Values flow through nota, notb, w1, and w2.
The Header Specifies the Interface
CHIP Xor {
IN a, b;
OUT out;
PARTS:
Not(in=a, out=nota);
Not(in=b, out=notb);
And(a=a, b=notb, out=w1);
And(a=nota, b=b, out=w2);
Or(a=w1, b=w2, out=out);
}Xora, boutThe interface tells us how to connect and use the chip.
The PARTS Section Connects Lower-Level Gates
CHIP Xor {
IN a, b;
OUT out;
PARTS:
Not(in=a, out=nota);
Not(in=b, out=notb);
And(a=a, b=notb, out=w1);
And(a=nota, b=b, out=w2);
Or(a=w1, b=w2, out=out);
}a→Not→notab→Not→notba, notb→And→w1nota, b→And→w2w1, w2→Or→outnota, notb, w1, and w2 are internal wires, created by naming connections.
Test Every XOR Input Combination
load Xor.hdl,
output-list a, b, out;
set a 0, set b 0, eval, output;
set a 0, set b 1, eval, output;
set a 1, set b 0, eval, output;
set a 1, set b 1, eval, output;| a | b | out |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
All four possible inputs are tested.
Next: State clearly what every chip is required to do.
What Is a Specification?
A specification states the chip's name, inputs, outputs, and required function. It says what the chip must do, independently of how it is built.
Example specification
in=0, then out=1; else out=0.a=1 and b=1, then out=0; else out=1.a=1 and b=1, then out=1; else out=0.a=1 or b=1, then out=1; else out=0.a and b differ, then out=1; else out=0.Multiplexor: Choose One Input
A multiplexor (Mux) uses the selection bit sel to choose which input reaches out.
Symbolic representation
Truth table
| a | b | sel | out |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 0 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | 0 | 1 |
| 0 | 0 | 1 | 0 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 1 | 1 |
Abbreviated truth table
| sel | out |
|---|---|
| 0 | a |
| 1 | b |
Specification
sel=0, then out=a; else out=b.Let a=1 and b=0. Predict out for both values of sel.
sel=0: out=1 | sel=1: out=0
Build a Mux in Two Ways
Draw a gate diagram for Mux and then write its HDL description.
Draw a NAND-only gate diagram for Mux and then write its HDL description.
sel=0, then out=a; else out=b.Use the specification to check both implementations for all eight input combinations.
Demultiplexor: Choose One Destination
A demultiplexor (DMux) sends in to output a or output b. The unselected output is 0.
Symbolic representation
Truth table
| in | sel | a | b |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 1 | 0 | 1 | 0 |
| 0 | 1 | 0 | 0 |
| 1 | 1 | 0 | 1 |
Abbreviated truth table
| sel | a | b |
|---|---|---|
| 0 | in | 0 |
| 1 | 0 | in |
Specification
sel=0, then a=in and b=0; else a=0 and b=in.Next: Apply one gate operation to a group of bits.
A Multi-Bit Gate Operates on a Bus
A bus is a group of parallel wires. A 16-bit bus carries sixteen binary values at the same time.
Example: multi-bit NOT
i, out[i]=Not(in[i]).0000000000000011Required output:1111111111111100Other examples
i, out[i]=And(a[i],b[i]).i, out[i]=Or(a[i],b[i]).sel=0, then out=a; else out=b.Each bit position is processed independently; neighboring positions do not interact.
Or8Way Reduces Eight Inputs to One Bit
in[0]in[1]...in[7]out1, then out=1; else out=0.Or16 operates pairwise on two buses. Or8Way combines eight input bits into one output bit.
Mux4Way16 and Mux8Way16
| sel | out |
|---|---|
| 00 | a |
| 01 | b |
| 10 | c |
| 11 | d |
Two bits encode four choices: 00 to 11.
Three bits encode eight choices: 000 to 111.
DMux4Way and DMux8Way
in to the selected output; set the other outputs to 0.| sel | a | b | c | d |
|---|---|---|---|---|
| 00 | in | 0 | 0 | 0 |
| 01 | 0 | in | 0 | 0 |
| 10 | 0 | 0 | in | 0 |
| 11 | 0 | 0 | 0 | in |
in to the selected output; set the other seven outputs to 0.Three selector bits choose one of eight destinations.
Next: Implement the specified gates in HDL.
Implement the Following Gates in HDL
For every named chip below, write an HDL implementation using only NAND or chips that you have already implemented from NAND.
- Read its specification.
- Plan the internal gate connections.
- Write the HDL.
- Run the supplied test script and correct every failing case.
Check Your Understanding
For a Mux with a=0 and b=1, find out for both values of sel.
For a DMux with in=1, find (a,b) for both values of sel.
If in[16]=0000000000000101, what is the output of Not16?
Why does Mux8Way16 need three selector bits?
How is Or8Way different from Or16?
For DMux4Way with in=1 and sel=10, write a,b,c,d.
Represent Numbers and Perform Arithmetic
How can gates represent numbers and perform operations such as addition and multiplication?