CodeRigor

COURSE / REAL PRACTICE

Build a computer from gates

Build a working 8-bit computer in plain Python, starting from a single NAND gate: adders, an ALU, flip-flops, RAM, an assembler and a CPU that runs your programs. Lessons pair with public videos by Sebastian Lague and Ben Eater. Unofficial practice material, not affiliated with the videos’ authors.

Free to useLessons & exercises7 authored lessons3 units
Start with the first lesson.

Read the explanation and diagrams, then work through the practice. Your browser profile supplies your saved progress.

What you’ll work toward

Assemble logic gates into arithmetic, memory and control, verify each part over its whole input range, and run assembled programs on the CPU you built.

Read an explanation, inspect the diagrams and work through the available checks, debugging exercises and builds. Feedback and prerequisite links guide the next step.

Before you start

This course builds on these skills:

Individual lessons can add prerequisites. Your lesson progress shows your actual readiness.

A look inside the first lesson

Every gate from NAND

Python
def nand(a, b):
    return 0 if (a and b) else 1

def xor(a, b):
    shared = nand(a, b)
    return nand(nand(a, shared), nand(b, shared))

print([xor(a, b) for a in (0, 1) for b in (0, 1)])

Your lesson sequence

Follow the same units and lessons in either view. Prerequisite links and your practice progress stay with each lesson.

UNIT 013 lessons Logic and arithmetic
  1. LESSON 01.01

    Every gate from NAND

    Build NOT, AND, OR, XOR, multiplexers and any truth table from one NAND gate, and prove each circuit against Python's operators by counting gates.

    Open lesson
  2. LESSON 01.02

    Adders and two’s complement

    Build the half adder, the full adder and a ripple-carry adder from bit operations, then add negation, subtraction and overflow detection in two's complement.

    Open lesson
  3. LESSON 01.03

    An 8-bit ALU with flags

    Build an eight-operation ALU from bit lists, set zero, negative, carry and overflow flags, and decide signed and unsigned comparisons from them.

    Open lesson
UNIT 022 lessons Memory
  1. LESSON 02.01

    Latches, flip-flops and the clock

    Build an SR latch, a D latch, a master-slave flip-flop, a register with load and a counter from NAND gates, and tell edge triggering from transparency.

    Open lesson
  2. LESSON 02.02

    Registers and RAM

    Build a decoder, a register file, a RAM from latch rows, a direct-mapped cache and a tri-state bus, and check them against a dictionary memory model.

    Open lesson
UNIT 032 lessons A small computer
  1. LESSON 03.01

    An assembler for your machine

    Write a two-pass assembler and disassembler for a 16-bit machine: labels, constants, data words, precise errors, and a round trip that holds for every word.

    Open lesson
  2. LESSON 03.02

    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.

    Open lesson

WATCH ALONG

Videos for these lessons

One hand-picked talk, lecture or tutorial per lesson, in course order. They play here when you press play; nothing loads from the video provider before that.

6 videos· 2 h 3 min

  1. 01Every gate from NAND

    A tour from logic gates to a working adder and ALU, built in a simulator. Watch the Logic Gates and The Simulation chapters first, then build the same gates here out of one primitive.

  2. 02An 8-bit ALU with flags

    Covers an arithmetic logic unit built around a 4-bit adder, how subtraction reuses the adder through two's complement, and the XOR gate as a conditional inverter. Watch it before building the ADD and SUB paths.

  3. 03Latches, flip-flops and the clock

    Covers the set-reset latch, the data latch, the clock, the edge-triggered flip-flop and registers built from them. Watch it first, then build the same parts here as NAND gates and test them against a plain reference.

  4. 04Registers and RAM

    Builds memory in a logic simulator from a grid of latches, with a decoder choosing the cell, then moves to larger RAM, asynchronous and synchronous versions, and a short note on caches. Watch it first, then write the same pieces in code.

  5. 05An assembler for your machine

    The video this lesson is paired with: its chapters go from writing machine code by hand to assembly language, opcodes, labels and a looping program. Watch it for the idea of a program being text that becomes numbers, then build that translator for your own machine.

  6. 06Capstone: a CPU that runs your programs

    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.