🚀 Alguni Start learning

Unit 6: The Calculator

One chip, many jobs.

Unit 6 of 8 in Digital logic for kids. Its 5 lessons are One Box, Many Jobs, Six Little Switches, Taking Away, Flags and The Calculator — 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.

🧮 One Box, Many Jobs

Do both, then pick one

A CPU does not have a separate chip for every sum. It has one chip that can do several things, with wires that say which.

The trick is the Mux from unit 3. Work out the And *and* the sum — both, always, at the same time — then let one control wire decide which answer leaves the box.

Here f chooses: 0 gives you And, 1 gives you plus.

HDL

CHIP Calc {
    IN x3, x2, x1, x0, y3, y2, y1, y0, f;
    OUT o3, o2, o1, o0;
    PARTS:
    And4(a3=x3, a2=x2, a1=x1, a0=x0, b3=y3, b2=y2, b1=y1, b0=y0,
         o3=and3, o2=and2, o1=and1, o0=and0);
    Add4(a3=x3, a2=x2, a1=x1, a0=x0, b3=y3, b2=y2, b1=y1, b0=y0,
         s3=sum3, s2=sum2, s1=sum1, s0=sum0);
    Mux4(a3=and3, a2=and2, a1=and1, a0=and0,
         b3=sum3, b2=sum2, b1=sum1, b0=sum0, sel=f,
         o3=o3, o2=o2, o1=o1, o0=o0);
}

USE Calc;
SET x3=0, x2=1, x1=1, x0=0, y3=0, y2=0, y1=1, y0=1, f=0;
SHOW;
SET f=1;
SHOW;

It prints

o3=0 o2=0 o1=1 o0=0
o3=1 o2=0 o1=0 o0=1

Try it yourself

That was 6 and 3. With f=1 it gave 1001. What is that?

  • 9, which is 6 + 3
  • 2, which is 6 and 3
  • 6
  • 3

Does the adder stop working when f is 0?

  • No. It always adds — the Mux just throws that answer away
  • Yes, f switches it off
  • Yes, otherwise it would waste electricity
  • Only if x is 0

Answer them in the app

🎛️ Six Little Switches

The real one has six controls

A real ALU adds four more switches, all of them tiny, in front of and behind the sum:

- zx — make x zero first
- nx — flip x first
- zy, ny — the same two, for y
- f — 0 for And, 1 for plus
- no — flip the answer on the way out

Six switches, 64 combinations. And that handful of Nots and Muxes is enough to compute almost everything a CPU ever needs.

Zero plus zero

Set zx and zy and both inputs become 0, whatever you fed in. Then f=1 adds them. The answer is 0 — and it is 0 no matter what x and y were.

That is how a CPU produces the number 0 without storing one anywhere.

HDL

CHIP Zero {
    IN x3, x2, x1, x0, y3, y2, y1, y0;
    OUT o3, o2, o1, o0;
    PARTS:
    ALU4(x3=x3, x2=x2, x1=x1, x0=x0, y3=y3, y2=y2, y1=y1, y0=y0,
         zx=1, nx=0, zy=1, ny=0, f=1, no=0,
         o3=o3, o2=o2, o1=o1, o0=o0);
}

USE Zero;
SET x3=1, x2=1, x1=1, x0=1, y3=1, y2=0, y1=1, y0=0;
SHOW;

It prints

o3=0 o2=0 o1=0 o0=0

Adding one, the ALU way

Here is a strange-looking setting: zx=0, nx=1, zy=1, ny=1, f=1, no=1. That works out as x + 1.

It is not obvious, and it does not need to be. What matters is that six switches in the right position turn one adder into "add one" — no extra chip.

HDL

CHIP Plus1 {
    IN x3, x2, x1, x0;
    OUT o3, o2, o1, o0;
    PARTS:
    ALU4(x3=x3, x2=x2, x1=x1, x0=x0, y3=0, y2=0, y1=0, y0=0,
         zx=0, nx=1, zy=1, ny=1, f=1, no=1,
         o3=o3, o2=o2, o1=o1, o0=o0);
}

USE Plus1;
SET x3=0, x2=1, x1=0, x0=1;
SHOW;

It prints

o3=0 o2=1 o1=1 o0=0

Try it yourself

With zx=1 and zy=1, what does f=0 (And) give you?

  • 0, because 0 and 0 is 0
  • 1
  • x
  • It is undefined

Six control wires. How many different settings is that?

  • 64
  • 12
  • 6
  • 36

Answer them in the app

➖ Taking Away

There is no subtractor

Nowhere in a CPU is there a chip that subtracts. There does not need to be one.

You learnt in unit 4 that flipping a number and adding one makes it negative. So x − y is just x + (−y) — the same adder, with the flip switches turned on.

zx=0, nx=1, zy=0, ny=0, f=1, no=1 is the setting for it.

HDL

CHIP Sub {
    IN x3, x2, x1, x0, y3, y2, y1, y0;
    OUT o3, o2, o1, o0;
    PARTS:
    ALU4(x3=x3, x2=x2, x1=x1, x0=x0, y3=y3, y2=y2, y1=y1, y0=y0,
         zx=0, nx=1, zy=0, ny=0, f=1, no=1,
         o3=o3, o2=o2, o1=o1, o0=o0);
}

USE Sub;
SET x3=0, x2=1, x1=1, x0=1, y3=0, y2=0, y1=1, y0=0;
SHOW;

It prints

o3=0 o2=1 o1=0 o0=1

Try it yourself

That was 7 − 2 and it printed 0101. Is that right?

  • Yes, 0101 is 5
  • No, it should be 0010
  • No, 0101 is 9
  • Only for positive numbers

What does this print? (It is 4 − 6.)

HDL

CHIP Sub {
    IN x3, x2, x1, x0, y3, y2, y1, y0;
    OUT o3, o2, o1, o0;
    PARTS:
    ALU4(x3=x3, x2=x2, x1=x1, x0=x0, y3=y3, y2=y2, y1=y1, y0=y0,
         zx=0, nx=1, zy=0, ny=0, f=1, no=1,
         o3=o3, o2=o2, o1=o1, o0=o0);
}

USE Sub;
SET x3=0, x2=1, x1=0, x0=0, y3=0, y2=1, y1=1, y0=0;
SHOW;

Answer them in the app

🚩 Flags

Two extra wires that decide everything

The ALU has two spare outputs that are not part of the answer:

- zr — 1 when the answer came out as zero
- ng — 1 when the answer is negative (its top wire is 1)

These are what if is made of. A program that says "if a equals b" really subtracts them and looks at zr.

HDL

CHIP Compare {
    IN x3, x2, x1, x0, y3, y2, y1, y0;
    OUT equal;
    PARTS:
    ALU4(x3=x3, x2=x2, x1=x1, x0=x0, y3=y3, y2=y2, y1=y1, y0=y0,
         zx=0, nx=1, zy=0, ny=0, f=1, no=1,
         zr=equal);
}

USE Compare;
SET x3=0, x2=1, x1=0, x0=1, y3=0, y2=1, y1=0, y0=1;
SHOW equal;
SET y0=0;
SHOW equal;

It prints

equal=1
equal=0

Try it yourself

How does that chip know the two numbers are equal?

  • It subtracts them, and zr says the answer was zero
  • It compares them wire by wire with Xor
  • It adds them and checks for 0
  • It looks at the top wire

You want to know whether x is less than y. What would you do?

  • Work out x − y and look at ng
  • Work out x + y and look at zr
  • Look at whether x is odd
  • It cannot be done with an ALU

Answer them in the app

🏆 The Calculator

Half a computer

You now have the part of a CPU that *does* things. Give it two numbers and six switches and it will add, subtract, compare, negate or zero them.

What it cannot do is remember anything. Turn the power off — or just change the inputs — and everything is gone. That is the next unit, and it needs a genuinely new idea: time.

Try it yourself

Why does an ALU work out every answer, even the ones it throws away?

  • Because wires are always carrying something — a Mux picks the answer, it does not stop the others happening
  • Because it is faster to do them all
  • Because the controls arrive late
  • It does not — it only computes the one it needs

What does this print?

HDL

CHIP Test {
    IN x3, x2, x1, x0, y3, y2, y1, y0;
    OUT zr, ng;
    PARTS:
    ALU4(x3=x3, x2=x2, x1=x1, x0=x0, y3=y3, y2=y2, y1=y1, y0=y0,
         zx=0, nx=1, zy=0, ny=0, f=1, no=1,
         zr=zr, ng=ng);
}

USE Test;
SET x3=0, x2=0, x1=1, x0=1, y3=0, y2=0, y1=1, y0=1;
SHOW;

Answer them in the app