All paths

Compile a language to the Kestrel CPU

Write a compiler in Python on a Raspberry Pi that turns Orbit into machine code for the Kestrel: assembler, code generation, registers, jumps, a call stack and an optimizer, ending with your language running on the CPU you built from NAND gates.

Raspberry Pi 4 Model BFree7 lessons7 h4 chapters3,100 XPCoding · Expert+400 XP

0 of 7 done

What you will build

3 projects, built step by step

7 lessons in 4 chapters, about 7 hours on a simulated Raspberry Pi 4 Model B. Free, in your browser, every step checked by the simulator.

Chapter 1 project

Code generation

Turn Orbit into Kestrel assembly: parse into a tree, then walk the tree and emit an instruction for every node. Registers handed out by depth, a tree too deep for four of them, and characters on the terminal.

Open it in the simulator
Chapter 2 project

Control flow

if and while compiled to labels and jumps, multiplication by a loop on a CPU with no multiply, not-equal as a subtraction, and a loop that never ends.

Open it in the simulator
Chapter 3 project

Optimizing

Make the code smaller and faster without changing what it prints: constant folding in the parser, and a peephole pass that drops address loads and reloads the Kestrel does not need. Measured in words and cycles.

Open it in the simulator
The map

Chapter by chapter

Each lesson opens when the one before it is done, and each chapter ends in a project.

Chapter 1

The target

The Kestrel, its words, and a tree turned into instructions.

  1. Open300 XP

    Targets and assembly

    Meet the target: the Kestrel, the CPU built from NAND gates in From gates to a computer. Write an assembler in Python that turns its assembly into the 16-bit words its ROM holds, then an emulator that runs them.

    0 of 5 steps

  2. ProjectLocked660 XP

    Code generation

    Turn Orbit into Kestrel assembly: parse into a tree, then walk the tree and emit an instruction for every node. Registers handed out by depth, a tree too deep for four of them, and characters on the terminal.

    0 of 5 steps

5 questions, graded as you go.
Chapter 2

Registers and jumps

Where each value lives, and if and while as jumps.

  1. Locked300 XP

    Registers

    Variables in the Kestrel's 8 bytes of RAM, one register kept back for addresses, assignment, picking the cheaper instruction for a constant, and the ninth variable that lands on the first.

    0 of 5 steps

  2. ProjectLocked660 XP

    Control flow

    if and while compiled to labels and jumps, multiplication by a loop on a CPU with no multiply, not-equal as a subtraction, and a loop that never ends.

    0 of 5 steps

5 questions, graded as you go.
Chapter 3

Calls and speed

A call stack on a CPU with no call instruction, then fewer words and cycles.

  1. Locked330 XP

    A stack and calls

    Functions on a CPU with no call instruction: a return number and a jump table the compiler writes, a stack in RAM so calls can nest, recursion, and a stack overflow you can watch.

    0 of 5 steps

  2. ProjectLocked600 XP

    Optimizing

    Make the code smaller and faster without changing what it prints: constant folding in the parser, and a peephole pass that drops address loads and reloads the Kestrel does not need. Measured in words and cycles.

    0 of 5 steps

5 questions, graded as you go.
Chapter 4

Boss

Your language, compiled on the Pi, running on gates.

  1. Locked250 XP

    Boss: your language on your CPU

    The Pi compiles an Orbit countdown with your compiler, and the words go into the program ROM of the computer you built from NAND gates. The Kestrel runs your language, and the terminal prints it.

    0 of 4 steps

4 questions, graded as you go.
Syllabus

Every lesson in the course

All 7 lessons, in order, each with what it covers. Any of them opens in the editor right away.

Chapter 1

The target

  1. Targets and assemblyMeet the target: the Kestrel, the CPU built from NAND gates in From gates to a computer. Write an assembler in Python that turns its assembly into the 16-bit words its ROM holds, then an emulator that runs them.
  2. Code generationTurn Orbit into Kestrel assembly: parse into a tree, then walk the tree and emit an instruction for every node. Registers handed out by depth, a tree too deep for four of them, and characters on the terminal.
Chapter 2

Registers and jumps

  1. RegistersVariables in the Kestrel's 8 bytes of RAM, one register kept back for addresses, assignment, picking the cheaper instruction for a constant, and the ninth variable that lands on the first.
  2. Control flowif and while compiled to labels and jumps, multiplication by a loop on a CPU with no multiply, not-equal as a subtraction, and a loop that never ends.
Chapter 3

Calls and speed

  1. A stack and callsFunctions on a CPU with no call instruction: a return number and a jump table the compiler writes, a stack in RAM so calls can nest, recursion, and a stack overflow you can watch.
  2. OptimizingMake the code smaller and faster without changing what it prints: constant folding in the parser, and a peephole pass that drops address loads and reloads the Kestrel does not need. Measured in words and cycles.
Chapter 4

Boss

  1. Boss: your language on your CPUThe Pi compiles an Orbit countdown with your compiler, and the words go into the program ROM of the computer you built from NAND gates. The Kestrel runs your language, and the terminal prints it.
Kits

Build it on your bench too

Every lesson runs in the simulator with nothing to buy. When you want the real thing, these kits have the parts.

Have a kit already?

Find your kit

Pick the starter kit you own and run every lesson in its manual with the same parts on the same pins.

See every kit
Questions

About Compile a language to the Kestrel CPU

Is Compile a language to the Kestrel CPU free?

Yes. All 7 lessons of Compile a language to the Kestrel CPU are free and run in your browser, with nothing to install and no parts to buy before you start.

Do I need a real Raspberry Pi 4 Model B?

No. Every lesson runs on a simulated Raspberry Pi 4 Model B with its parts on the canvas, and the simulator checks each step as you go. When you want to build it for real, the same wiring and code work on a real board.

How long does Compile a language to the Kestrel CPU take?

About 7 hours: 7 lessons in 4 chapters, each one a few short steps. Your progress is kept, so you can stop after any lesson and pick up where you left off.

What will I build?

Each chapter ends in a project that puts it together: Code generation, Control flow and Optimizing.

Where do I start?

With "Targets and assembly", and each lesson builds on the one before it. New to circuits? Electronics basics is the course that starts from zero.