CodeRigor

LESSON

Capstone: a CPU that runs your programs

Decode 16-bit instructions, derive control signals from the flags, and run programs on an 8-bit CPU model: multiplication, Fibonacci, a RAM array sum and a loop.

This lesson puts the earlier parts of the course together: a machine that takes the words your assembler produces and runs them. This lesson pairs with 8-bit CPU control logic: Part 1 and Conditional jump instructions by Ben Eater, linked under Watch & read. The videos cover how a real breadboard CPU fetches an instruction, how control signals are looked up, and how conditional jumps are added. You build a software model of the same cycle: decode, control, and a Cpu class with fetch, step and run.

The machine keeps four 8-bit registers, a program counter pc, a RAM, an output list and two flags, Z (the last ALU result was zero) and C (it carried out of the top bit, or for SUB did not borrow). One instruction takes the same path every time, and the first diagram shows it.

One instruction, from pc to the next pc
start / 5
Fetch moves pc forward before anything else happens. Control sees the flags as they stood before this instruction; only a taken jump writes pc again.
One instruction, from pc to the next pc
Fetch moves pc forward before anything else happens. Control sees the flags as they stood before this instruction; only a taken jump writes pc again.

Decoding is bit slicing. The opcode is the top four bits of the word, rd the next two, rs the two after that and n the low byte.

Python
word = 0x3600
print(word >> 12, (word >> 10) & 3, (word >> 8) & 3, word & 255)

That prints opcode 3, which is ADD, with rd = 1 and rs = 2. Control is a table: LDI writes a register from the number, MOV from another register, LD from RAM, the five ALU instructions from the ALU, ST writes RAM, OUT appends to the output. A conditional jump has one extra input. JZ sets its jump signal to the Z flag, JNZ to its opposite and JC to C, so the datapath never needs to know about conditions.

Registers are 8 bits wide, so the ALU keeps the low 8 bits and reports what fell off. For ADD the carry is a sum above 255. For SUB the carry means no borrow: it is set when rd is greater than or equal to rs as unsigned numbers, and clear when rd is smaller. This is the same convention as the ALU lesson of this course. Z looks at the stored result.

Python
print((200 + 100) & 255, 200 + 100 > 255)     # ADD: 44, carry set
print((3 - 5) & 255, 3 >= 5)                   # SUB: 254, carry clear (borrow)
print((5 - 5) & 255, 5 >= 5)                   # SUB: 0, carry set (no borrow)
print((255 + 1) & 255)                         # 0: so Z is set as well as C

Now a worked run. The countdown program is six words, 0x1005 0x1401 0xE000 0x4100 0xC002 0xF000: load 5 and 1, then loop over OUT r0, SUB r0, r1 and JNZ back to address 2, and halt. It runs 18 instructions: 2 setup, 5 laps of 3, and the HALT. Each SUB leaves r0 at 4, 3, 2, 1 and finally 0. Only that last one sets Z, so only then does JNZ fall through.

The countdown program, pc at the start of each step
start / 7
The jump goes back to address 2, the first word of the loop. On the last lap SUB makes r0 zero, Z is set and pc continues to HALT.
The countdown program, pc at the start of each step
The jump goes back to address 2, the first word of the loop. On the last lap SUB makes r0 zero, Z is set and pc continues to HALT.

Two mistakes account for most broken machines. The first is letting loads write the flags. If LDI sets Z from the loaded value, any constant loaded between a comparison and its branch silently replaces the comparison. Programs that branch directly after the arithmetic still work, so the mistake survives simple tests. The second concerns pc. Fetch has already advanced it, so a taken jump overwrites it with the target. Writing the target plus one, or adding the target to pc, makes every loop start one word late or run off the end, while straight-line code stays correct.

A smaller trap sits in the flags themselves. Z is about the stored 8-bit result, so 255 + 1 sets both flags, and after SUB a clear C means a borrow happened, so rd was smaller than rs. A subtraction of equal numbers sets C, not clears it.

How do you know the machine is right? Three kinds of evidence. Hand-checked small cases: ADD of 200 and 100 leaves 44 with C set. Whole programs with known answers: 6 times 7 by repeated addition leaves 42, Fibonacci stops with the carry after printing 144, and an array stored in RAM sums as expected, each with an exact instruction count. And an independent interpreter written from the encoding, which runs random straight-line programs and compares every register, RAM word, flag and output. The comparison cannot test the jumps in that interpreter, so loops are covered only by the programs above.

What this does not show: the model is a specification of behaviour, not of timing or gates. It says nothing about how many clock cycles each instruction takes, and it does not prove that the gates built in earlier lessons implement it. It shows that the rules you wrote can run real programs.

Watch & read

Free lectures, talks and readings that cover this skill from another angle. All resources →

The video this lesson is paired with: its chapters cover defining a program, running it by hand, the instruction fetch cycle and executing a load and an add. Watch it for the fetch-then-execute rhythm, then build that cycle in software.

The second paired video: its chapters cover a lookup table for control signals, adding conditional jump instructions and a multiplication program. It is the hardware view of what your control function does with the flags.