A riff on Cook-Levin Chapters

Chapter 13

Parts for a small computer

This chapter and the two after it build the machine as hardware, a plain digital circuit, from the bottom up. Each part is drawn once with all its gates, and from then on it is a box with named wires in and out. “The machine as a circuit” wires the boxes into a stepper, and “The circuit, in full” chains the steppers into the whole machine.

Gates and wires §

Three kinds of gate are allowed, AND, OR and NOT, and every part is made of them alone. The drawings use the standard symbols: AND is a D, OR a shield, NOT a triangle with a bubble. An OR followed by a NOT is drawn as one shield with a bubble, a NOR; it still counts as two gates. A dot marks a place where a wire branches, and a 0 is a wire tied to 0.

AX travels as its two bits, hb and lb. Each part is a literal transcription of one rule of the machine. The parts are functions in src/circuit.c, which records every gate in a netlist; src/circuit_main.c draws each schematic from that netlist and fills each table by running the netlist's simulator on every input. The book's build draws the schematics and holds the tables to its output, byte for byte, and tests/test_circuit.c checks the same answers.

The four instructions §

Each opcode is a small circuit from (hb, lb) to (hb', lb'), plus a flag for zero and decr.

hb′ lb′ hb lb
nada: both bits pass, no gates
0 hb′ lb′ hb lb
mod2: hb' tied to 0, lb passes, no gates

mod2 takes 2 from AX when it is 2 or 3, which is the same as clearing the high bit.

hb′ lb′ accepts hb lb NOR
zero: AX passes, and a NOR accepts at 0

zero accepts exactly when both bits are 0. A circuit runs every step, so after an accept the machine keeps stepping, and the accepted latch holds the verdict.

hb′ lb′ halts hb lb AND NOR
decr: the two-bit decrementer, and a NOR that halts at 0

decr is the textbook decrementer: the low bit always flips, and the high bit survives only when the low bit pays for the subtraction, 3 to 2. At 0 it halts; its bits then read 0 1, and the halt keeps the machine from going on.

Each cell is hb' and lb', with the flag when it is on:

AX hb lb nada zero decr mod2
0 0 0 0 0 0 0, accepts 0 1, halts 0 0
1 0 1 0 1 0 1 0 0 0 1
2 1 0 1 0 1 0 0 1 0 0
3 1 1 1 1 1 1 1 0 0 1

The multiplexer §

A 2-way multiplexer, a mux2, passes one of two inputs: in 0 when its select s is 0, in 1 when it is 1. It is (in 0 AND NOT s) OR (in 1 AND s), four gates.

out in 0 s in 1 AND AND OR
A mux2: four gates
s in 0 in 1 out
0 0 0 0
0 0 1 0
0 1 0 1
0 1 1 1
1 0 0 0
1 0 1 1
1 1 0 0
1 1 1 1

From here on a mux2 is a box. Three of them make a 4-way multiplexer, a mux4: two choose within the pairs by the low select bit, and the third chooses between those by the high one. It passes input k when the select bits spell k, and it is 12 gates.

s_lb low high in 0 in 1 in 2 in 3 s_hb top out s_lb low high in 0 in 1 in 2 in 3 s_hb top out
A mux4, three mux2 boxes

The hold §

A halted machine stays as it is. The hold ANDs each of the line's two bits with NOT halted, so a halted machine selects 00, nada: its bits pass and its flags are 0. That costs three gates and reuses nada.

sel_hb sel_lb op_hb halted op_lb AND AND
The hold: three gates
halted op_hb op_lb sel_hb sel_lb
0 0 0 0 0
0 0 1 0 1
0 1 0 1 0
0 1 1 1 1
1 0 0 0 0
1 0 1 0 0
1 1 0 0 0
1 1 1 0 0

The latches §

A flag, once on, stays on: accepted' is accepted OR accepts, and halted' is halted OR halts.

halted′ accepted′ halted halts accepted accepts OR OR
The latches: two ORs
accepted accepts accepted'
0 0 0
0 1 1
1 0 1
1 1 1