Unit 3: Choosing
Sending signals down the right road.
Unit 3 of 8 in Digital logic for kids. Its 4 lessons are Two Roads In, Building Mux, Two Roads Out and The Switchboard — below is everything each one explains, and a question or two from it to try.
Every chip on this page was wired up and run on the gate-level simulator before it shipped, and prints exactly the table shown.
This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.
🔀 Two Roads In
A chip that picks
Every gate so far answered a question. This one makes a choice.
Mux (short for multiplexer) has three wires in: a, b, and sel. Whatever sel says, that is the one that comes out.
sel is 0 → you get a. sel is 1 → you get b. It is a railway points lever.
HDL
CHIP Mux {
IN a, b, sel;
OUT out;
PARTS:
Not(in=sel, out=nsel);
And(a=a, b=nsel, out=pa);
And(a=b, b=sel, out=pb);
Or(a=pa, b=pb, out=out);
}
USE Mux;
SET a=1, b=0, sel=0;
SHOW out;
SET sel=1;
SHOW out;
It prints
out=1 out=0
Why this matters more than it looks
A Mux is how one piece of hardware does more than one job.
Build something that adds, build something that compares, wire both to a Mux, and now a single wire decides which answer you get. That is exactly how the calculator inside a real CPU works — you will build it in unit 6.
Try it yourself
a is 0, b is 1, sel is 1. What comes out?
- 1, because sel picks b
- 0, because sel picks a
- 1, because a is 0
- 0
What does this print?
HDL
CHIP Mux {
IN a, b, sel;
OUT out;
PARTS:
Not(in=sel, out=nsel);
And(a=a, b=nsel, out=pa);
And(a=b, b=sel, out=pb);
Or(a=pa, b=pb, out=out);
}
USE Mux;
SET a=0, b=1, sel=0;
SHOW out;
SET sel=1;
SHOW out;
Answer them in the app
🔧 Building Mux
How the choosing actually happens
There is no "if" in hardware. Both roads are always live. The trick is to switch one of them off.
And(a, nsel) lets a through only while sel is 0. And(b, sel) lets b through only while sel is 1. One of the two is always dead — so an Or at the end just collects whichever one survived.
HDL
CHIP Mux {
IN a, b, sel;
OUT out;
PARTS:
Not(in=sel, out=nsel);
And(a=a, b=nsel, out=pa);
And(a=b, b=sel, out=pb);
Or(a=pa, b=pb, out=out);
}
TABLE Mux;
It prints
a b sel | out 0 0 0 | 0 0 0 1 | 0 0 1 0 | 0 0 1 1 | 1 1 0 0 | 1 1 0 1 | 0 1 1 0 | 1 1 1 1 | 1
Try it yourself
Why can the Or at the end never get 1 from both sides at once?
- Because sel and nsel are opposites, so one road is always switched off
- Because Or ignores its second input
- Because a and b are never both 1
- It can — that is a bug
Answer it in the app
🍴 Two Roads Out
DMux is a Mux running backwards
DMux takes one wire in and sends it down one of two roads. The other road gets 0.
This is how a computer says "put this number in *that* box, not the other one". Same lever, other direction.
HDL
CHIP DMux {
IN in, sel;
OUT a, b;
PARTS:
Not(in=sel, out=nsel);
And(a=in, b=nsel, out=a);
And(a=in, b=sel, out=b);
}
TABLE DMux;
It prints
in sel | a b 0 0 | 0 0 0 1 | 0 0 1 0 | 1 0 1 1 | 0 1
A chip can have more than one output
DMux is the first chip here with two wires coming out. OUT a, b; lists them both, and each one needs a part that gives it a value.
SHOW with no names after it prints every output at once.
HDL
CHIP DMux {
IN in, sel;
OUT a, b;
PARTS:
Not(in=sel, out=nsel);
And(a=in, b=nsel, out=a);
And(a=in, b=sel, out=b);
}
USE DMux;
SET in=1, sel=1;
SHOW;
It prints
a=0 b=1
Try it yourself
Look at the first two rows. Why is everything 0 there?
- Because in is 0, so there is nothing to send down either road
- Because sel is broken
- Because DMux only works when sel is 1
- Because a and b cancel out
Answer it in the app
🏆 The Switchboard
Try it yourself
Which is true about Mux?
- Both inputs are always live; sel just decides which one survives
- sel turns the chip on and off
- It runs a only when sel is 1
- It adds a and b together
Answer it in the app