A riff on Cook-Levin Chapters

Chapter 14

The machine as a circuit

“Parts for a small computer” drew every part with its gates. From here on each part is a box, known by its name and its wires, and this chapter wires the boxes into a stepper: one step of the machine. The circuit is combinational: the whole run is laid out in space, and every wire settles at once. “The Cook-Levin machine”, in the second part, turns the same circuit around.

The parts as boxes §

A stepper has ten boxes: the four instructions, the hold, one mux4 for each of hb', lb', accepts and halts, and the latches. The table counts each box's wires from the netlist, with the gates inside:

box wires in wires out gates
nada 2 2 0
zero 2 3 2
decr 2 3 4
mod2 2 2 0
hold 3 2 3
hb mux 6 1 12
lb mux 6 1 12
accepts mux 6 1 12
halts mux 6 1 12
latches 4 2 2

A stepper, from its boxes §

A stepper has six wires in, AX's two bits, the flags halted and accepted, and the line's two bits, and four wires out, the same state for the next step. All four instructions work on AX at once. The hold turns the line into the select bits, and each mux4 passes the chosen instruction's wire; the accepts mux takes zero's flag and a 0 for each other instruction, and the halts mux takes decr's flag and a 0 for the others, so only the chosen instruction's flag counts. The latches keep the flags. A number on a line is how many wires it carries.

AX nada 2 zero 2 decr 2 mod2 2 line hold 2 halted latches 0 accepts mux 3 halts mux 3 accepted hb mux lb mux 2 2 2 2 hb' lb' halted' accepted' AX nada 2 zero 2 decr 2 mod2 2 line hold 2 halted latches 0 accepts mux 3 halts mux 3 accepted hb mux lb mux 2 2 2 2 hb' lb' halted' accepted'
A stepper, ten boxes

tests/test_circuit.c holds the stepper to sat_step on all 64 settings of its six inputs. The flags agree on every setting, and AX on every setting but decr at 0, where the decrementer reads 1 and sat_step keeps 0; the machine has halted there, and its verdict is settled. Its gates, box by box:

part NOT AND OR gates
nada 0 0 0 0
zero 1 0 1 2
decr 2 1 1 4
mod2 0 0 0 0
hold 1 2 0 3
hb mux 3 6 3 12
lb mux 3 6 3 12
accepts mux 3 6 3 12
halts mux 3 6 3 12
latches 0 0 2 2
the stepper 16 27 16 59
a core, six steppers 96 162 96 354
the decider, four cores and the comparators 392 659 388 1,439

A stepper as equations §

Each gate is one equation: its output named on the left, its gate and inputs on the right, in the order the simulator evaluates them, so each name is defined before it is read. A wire is named for the outermost box that hands it out (hb' is the stepper's own output, decr.hb' the decrementer's), else for its gate inside its box (hb_mux.low.pick0). Here are a stepper's 59, as circuit_main prints them:

inputs: hb lb halted accepted op_hb op_lb
zero.hb_or_lb = hb OR lb
zero.accepts = NOT zero.hb_or_lb
decr.hb' = hb AND lb
decr.lb' = NOT lb
decr.hb_or_lb = hb OR lb
decr.halts = NOT decr.hb_or_lb
hold.running = NOT halted
hold.sel_hb = op_hb AND hold.running
hold.sel_lb = op_lb AND hold.running
hb_mux.low.not_s = NOT hold.sel_lb
hb_mux.low.pick0 = hb AND hb_mux.low.not_s
hb_mux.low.pick1 = hb AND hold.sel_lb
hb_mux.low.out = hb_mux.low.pick0 OR hb_mux.low.pick1
hb_mux.high.not_s = NOT hold.sel_lb
hb_mux.high.pick0 = decr.hb' AND hb_mux.high.not_s
hb_mux.high.pick1 = 0 AND hold.sel_lb
hb_mux.high.out = hb_mux.high.pick0 OR hb_mux.high.pick1
hb_mux.top.not_s = NOT hold.sel_hb
hb_mux.top.pick0 = hb_mux.low.out AND hb_mux.top.not_s
hb_mux.top.pick1 = hb_mux.high.out AND hold.sel_hb
hb' = hb_mux.top.pick0 OR hb_mux.top.pick1
lb_mux.low.not_s = NOT hold.sel_lb
lb_mux.low.pick0 = lb AND lb_mux.low.not_s
lb_mux.low.pick1 = lb AND hold.sel_lb
lb_mux.low.out = lb_mux.low.pick0 OR lb_mux.low.pick1
lb_mux.high.not_s = NOT hold.sel_lb
lb_mux.high.pick0 = decr.lb' AND lb_mux.high.not_s
lb_mux.high.pick1 = lb AND hold.sel_lb
lb_mux.high.out = lb_mux.high.pick0 OR lb_mux.high.pick1
lb_mux.top.not_s = NOT hold.sel_hb
lb_mux.top.pick0 = lb_mux.low.out AND lb_mux.top.not_s
lb_mux.top.pick1 = lb_mux.high.out AND hold.sel_hb
lb' = lb_mux.top.pick0 OR lb_mux.top.pick1
accepts_mux.low.not_s = NOT hold.sel_lb
accepts_mux.low.pick0 = 0 AND accepts_mux.low.not_s
accepts_mux.low.pick1 = zero.accepts AND hold.sel_lb
accepts_mux.low.out = accepts_mux.low.pick0 OR accepts_mux.low.pick1
accepts_mux.high.not_s = NOT hold.sel_lb
accepts_mux.high.pick0 = 0 AND accepts_mux.high.not_s
accepts_mux.high.pick1 = 0 AND hold.sel_lb
accepts_mux.high.out = accepts_mux.high.pick0 OR accepts_mux.high.pick1
accepts_mux.top.not_s = NOT hold.sel_hb
accepts_mux.top.pick0 = accepts_mux.low.out AND accepts_mux.top.not_s
accepts_mux.top.pick1 = accepts_mux.high.out AND hold.sel_hb
accepts_mux.out = accepts_mux.top.pick0 OR accepts_mux.top.pick1
halts_mux.low.not_s = NOT hold.sel_lb
halts_mux.low.pick0 = 0 AND halts_mux.low.not_s
halts_mux.low.pick1 = 0 AND hold.sel_lb
halts_mux.low.out = halts_mux.low.pick0 OR halts_mux.low.pick1
halts_mux.high.not_s = NOT hold.sel_lb
halts_mux.high.pick0 = decr.halts AND halts_mux.high.not_s
halts_mux.high.pick1 = 0 AND hold.sel_lb
halts_mux.high.out = halts_mux.high.pick0 OR halts_mux.high.pick1
halts_mux.top.not_s = NOT hold.sel_hb
halts_mux.top.pick0 = halts_mux.low.out AND halts_mux.top.not_s
halts_mux.top.pick1 = halts_mux.high.out AND hold.sel_hb
halts_mux.out = halts_mux.top.pick0 OR halts_mux.top.pick1
halted' = halted OR halts_mux.out
accepted' = accepted OR accepts_mux.out

The stepper of the second part §

sat_step in src/sat.c, the stepper of “The Cook-Levin machine”, computes the same flags in 34 gates. It writes each NOT as a literal's sign, which is free in a formula, so it counts AND and OR alone; counted that way, this stepper has 43. The two differ in kind. sat_step decodes AX and the line into one-hot wires, gates each opcode's wires with an enable, and folds away every constant. This stepper keeps AX as two bits, chooses with multiplexers, and feeds the flag muxes their constant 0s: those two muxes alone are 18 AND and OR gates, where sat_step spends one AND on each flag.