Chapter 8
The Boolean view
Everything so far ran the machine as a program, an interpreter reading its lines. This chapter runs the same machine as a computer's hardware does: a clock, and at each tick, one step of the machine, a fixed bank of Boolean gates, AND, OR and the complement, that takes bits in and puts bits out. The gates are the same at every tick, for every program and every input. They read the line's two opcode bits and the four state bits, and they write four state bits. They see bits and only bits: what a program means, which of the 16 problems it decides, even that problems exist, all lies outside them.
One step, four functions §
A step reads six bits: the line's opcode, op_hb and op_lb, and the machine's state, the two bits of AX, hb and lb, and the flags halted and accepted. It writes the four state bits again, and each new bit is a Boolean function of its own of those six. The gates know bits, and the machine is defined by its opcodes, so a step first decodes: the line's two bits name one of the four opcodes, and AX's two bits name its value. Here it is in Python (python/steps.py), where ~, & and | are the complement, AND and OR:
def opcodes(op_hb, op_lb):
is_nada = ~op_hb & ~op_lb # 00
is_zero = ~op_hb & op_lb # 01
is_decr = op_hb & ~op_lb # 10
is_mod2 = op_hb & op_lb # 11
return is_nada, is_zero, is_decr, is_mod2
# AX's two bits, hb and lb, name its value; the step needs three of them.
def values(hb, lb):
ax_is_0 = ~hb & ~lb
ax_is_2 = hb & ~lb
ax_is_3 = hb & lb
return ax_is_0, ax_is_2, ax_is_3
Each is_ wire is 1 for exactly one opcode, and each ax_is_ wire for exactly one value. Then each new bit says, opcode by opcode, what happens to it:
def next_hb(op_hb, op_lb, hb, lb, halted, accepted):
is_nada, is_zero, is_decr, is_mod2 = opcodes(op_hb, op_lb)
ax_is_0, ax_is_2, ax_is_3 = values(hb, lb)
# A halted machine keeps its high bit. A running one: nada and zero keep
# it, decr sets it only when taking 3 to 2, and mod2 clears it.
return (halted & hb) | (~halted & ((is_nada & hb) | (is_zero & hb) | (is_decr & ax_is_3)))
def next_lb(op_hb, op_lb, hb, lb, halted, accepted):
is_nada, is_zero, is_decr, is_mod2 = opcodes(op_hb, op_lb)
ax_is_0, ax_is_2, ax_is_3 = values(hb, lb)
# A halted machine keeps its low bit. A running one: nada, zero and mod2
# keep it, and decr sets it only when taking 2 to 1.
return (halted & lb) | (
~halted & ((is_nada & lb) | (is_zero & lb) | (is_mod2 & lb) | (is_decr & ax_is_2))
)
def next_halted(op_hb, op_lb, hb, lb, halted, accepted):
is_nada, is_zero, is_decr, is_mod2 = opcodes(op_hb, op_lb)
ax_is_0, ax_is_2, ax_is_3 = values(hb, lb)
# Halted already, or a running machine's decr finds AX at 0.
return halted | (~halted & (is_decr & ax_is_0))
def next_accepted(op_hb, op_lb, hb, lb, halted, accepted):
is_nada, is_zero, is_decr, is_mod2 = opcodes(op_hb, op_lb)
ax_is_0, ax_is_2, ax_is_3 = values(hb, lb)
# Accepted already, or a running machine's zero finds AX at 0.
return accepted | (~halted & (is_zero & ax_is_0))
These are the machine's rules, one bit at a time. nada and zero leave AX alone. decr takes 3 to 2, 2 to 1 and 1 to 0, so the new high bit is 1 only when it starts at 3, and the new low bit only when it starts at 2; at 0 it halts. mod2 keeps the low bit and clears the high one. zero at 0 accepts. A halted machine keeps every bit as it is, and once a flag is on it stays on.
Six steps, everything fixed §
Now fix the input and the program. With AX = 2 and program 2916 (decr, mod2, zero, decr, zero, nada), each of the six bits a step reads is a known 0 or 1, and so is each bit it writes. Composing the six steps is running the machine, one row a step:
hb lb halted accepted
start 1 0 0 0
step 1 decr 10 0 1 0 0
step 2 mod2 11 0 1 0 0
step 3 zero 01 0 1 0 0
step 4 decr 10 0 0 0 0
step 5 zero 01 0 0 0 1
step 6 nada 00 0 0 0 1
The last row's accepted is 1: the program accepts 2. python/steps.py runs each of the 4,096 programs on all four inputs this way and holds every answer to an ordinary interpreter written beside it.
Four inputs, one verdict §
Whether a program decides a problem is more gates of the same kind. Run the six ticks once for each input, four copies side by side, and each copy leaves one accepted bit. The problem enters only here, as four more bits, the want bits: input n's bit is 1 when n is in the problem. Each accepted bit is compared with its want bit, a gate that gives 1 when the two agree, and one AND of the four comparisons is the verdict. For the program above and the problem {1, 2, 3}, the four accepted bits are 0, 1, 1, 1, the want bits are 0, 1, 1, 1, and the verdict is 1. The gates matched bits; what {1, 2, 3} is stayed outside them.
“Six expressions, composed”, in the second part, keeps the same gates and the same six ticks, and leaves the program's bits unknown.