Unit 8: The Computer
Counting, choosing, remembering — in a circle.
Unit 8 of 8 in Digital logic for kids. Its 4 lessons are Where Am I?, Jumping, Round and Round and The Machine — 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.
📍 Where Am I?
A register that counts itself
A program is a list of steps, and something has to keep track of which step you are on. That something is the program counter.
It is a register wired into an adder that adds one — and then straight back into itself. Every tick it becomes one bigger than it was.
A loop, with a register in it to make it legal.
HDL
CHIP Counter {
IN i3, i2, i1, i0, load, inc, reset;
OUT o3, o2, o1, o0;
PARTS:
PC4(i3=i3, i2=i2, i1=i1, i0=i0, load=load, inc=inc, reset=reset,
o3=o3, o2=o2, o1=o1, o0=o0);
}
USE Counter;
SET i3=0, i2=0, i1=0, i0=0, load=0, inc=1, reset=0;
SHOW;
CLOCK;
SHOW;
CLOCK;
SHOW;
CLOCK;
SHOW;
It prints
o3=0 o2=0 o1=0 o0=0 o3=0 o2=0 o1=0 o0=1 o3=0 o2=0 o1=1 o0=0 o3=0 o2=0 o1=1 o0=1
Try it yourself
What are the three things a program counter has to be able to do?
- Count up, jump to a given number, and go back to zero
- Add, subtract and compare
- Read, write and erase
- Start, stop and pause
What is the last line this prints?
HDL
CHIP Counter {
IN i3, i2, i1, i0, load, inc, reset;
OUT o3, o2, o1, o0;
PARTS:
PC4(i3=i3, i2=i2, i1=i1, i0=i0, load=load, inc=inc, reset=reset,
o3=o3, o2=o2, o1=o1, o0=o0);
}
USE Counter;
SET i3=0, i2=0, i1=0, i0=0, load=0, inc=1, reset=0;
CLOCK;
SHOW;
CLOCK;
SHOW;
CLOCK;
SHOW;
CLOCK;
SHOW;
CLOCK;
SHOW;
Answer them in the app
🦘 Jumping
Every loop you have ever written
Counting up gets you through a program once. Jumping is what makes loops and choices possible: instead of "one bigger", the counter takes a number you hand it.
load=1 jumps to whatever is on the input wires. reset=1 goes back to 0. Three Muxes decide between the three, one after another.
HDL
CHIP Counter {
IN i3, i2, i1, i0, load, inc, reset;
OUT o3, o2, o1, o0;
PARTS:
PC4(i3=i3, i2=i2, i1=i1, i0=i0, load=load, inc=inc, reset=reset,
o3=o3, o2=o2, o1=o1, o0=o0);
}
USE Counter;
SET i3=1, i2=0, i1=0, i0=1, load=0, inc=1, reset=0;
CLOCK;
CLOCK;
SHOW;
SET load=1;
CLOCK;
SHOW;
SET load=0, reset=1;
CLOCK;
SHOW;
It prints
o3=0 o2=0 o1=1 o0=0 o3=1 o2=0 o1=0 o0=1 o3=0 o2=0 o1=0 o0=0
That is if. That is while.
Everything a program can do about *where to go next* is one wire deciding whether the counter loads or counts.
if, while, for, calling a function and coming back — all of it, down here, is a Mux picking between "one bigger" and "this number instead".
Try it yourself
The input wires were 1001 the whole time. Why did the counter only jump to 9 on the third tick?
- Because load was 0 until then, so the Mux was ignoring those wires
- Because it had to count up to 9 first
- Because reset was on
- Because 1001 takes three ticks to load
A program says "if the answer was zero, go back to the start". Which wires do that?
- The ALU’s zr flag, wired to the counter’s load
- The ALU’s ng flag, wired to reset
- The clock
- The carry wire from the adder
Answer them in the app
⚙️ Round and Round
The whole machine, in one sentence
A computer does the same four things forever:
1. The counter says which step we are on.
2. That step is fetched from memory.
3. The ALU works something out.
4. A register remembers the answer — and the counter moves on.
Then it does it again. Billions of times a second, and never anything else.
A tiny machine that really runs
Here all three parts are wired into one circle: a counter that counts, an adder, and a register keeping a running total of everything the counter has said.
Watch the totals: 0, then 1, then 3, then 6. It is adding up 1 + 2 + 3 as it goes — and the total is always one tick behind the counter, because both registers grab their values at the same instant.
HDL
CHIP Machine {
IN go, reset;
OUT o3, o2, o1, o0, acc3, acc2, acc1, acc0;
PARTS:
PC4(i3=0, i2=0, i1=0, i0=0, load=0, inc=go, reset=reset,
o3=o3, o2=o2, o1=o1, o0=o0);
Add4(a3=acc3, a2=acc2, a1=acc1, a0=acc0, b3=o3, b2=o2, b1=o1, b0=o0,
s3=n3, s2=n2, s1=n1, s0=n0);
Register4(i3=n3, i2=n2, i1=n1, i0=n0, load=go,
o3=acc3, o2=acc2, o1=acc1, o0=acc0);
}
USE Machine;
SET go=1, reset=0;
CLOCK;
SHOW;
CLOCK;
SHOW;
CLOCK;
SHOW;
CLOCK;
SHOW;
It prints
o3=0 o2=0 o1=0 o0=1 acc3=0 acc2=0 acc1=0 acc0=0 o3=0 o2=0 o1=1 o0=0 acc3=0 acc2=0 acc1=0 acc0=1 o3=0 o2=0 o1=1 o0=1 acc3=0 acc2=0 acc1=1 acc0=1 o3=0 o2=1 o1=0 o0=0 acc3=0 acc2=1 acc1=1 acc0=0
Try it yourself
Why is the running total always one step behind the counter?
- Both registers latch at the same instant, so the adder is still seeing the counter’s old value
- The adder is slow
- Because the total starts at 0
- Because go is 1
What is missing from this machine that a real computer has?
- Memory holding instructions that say what to do at each step
- A clock
- An ALU
- A program counter
Answer them in the app
🏆 The Machine
You built a computer
Start to finish, everything you made came out of one gate that says "not both", plus a flip-flop that waits for the beat.
Not, And, Or, Xor. Choosing. Numbers. Adding. A calculator that does six jobs. Memory. A counter that can jump.
That is a computer. The one you are holding is the same, only wider and very much faster — and made of exactly this.
Try it yourself
Which of these is NOT built out of Nand gates in a real computer?
- Nothing — the whole thing is, apart from the flip-flops holding the state
- The ALU
- The program counter
- The multiplexers
What does this print?
HDL
CHIP Step {
IN go;
OUT o3, o2, o1, o0, zero;
PARTS:
PC4(i3=0, i2=0, i1=0, i0=0, load=0, inc=go, reset=0,
o3=o3, o2=o2, o1=o1, o0=o0);
IsZero4(i3=o3, i2=o2, i1=o1, i0=o0, out=zero);
}
USE Step;
SET go=1;
SHOW zero;
CLOCK;
SHOW zero;
Answer them in the app