Skip to content

Nand2Tetris Overview

This section guides you through implementing the Nand2Tetris course using Hardcaml. You’ll build a complete computer starting from a single NAND gate.

Nand2Tetris (also known as “The Elements of Computing Systems”) teaches you how computers work by having you build one from scratch:

  1. Project 1 - Boolean Logic (NOT, AND, OR, XOR, MUX, DMUX)
  2. Project 2 - Arithmetic (HalfAdder, FullAdder, ALU)
  3. Project 3 - Memory (DFF, Register, RAM)
  4. Project 5 - CPU and Computer

The original N2T course uses HDL (Hardware Description Language). Hardcaml provides:

  • Type safety - Catch wiring errors at compile time
  • Better tooling - Real IDE with syntax highlighting
  • Powerful testing - Waveform visualization built-in
  • Real HDL output - Generate Verilog for synthesis

All chips are available through the N2t_chips module:

(* Use helper functions for concise syntax *)
let not_a = N2t_chips.not_ scope i.a
let and_ab = N2t_chips.and_ scope i.a i.b
(* Or use modules directly *)
let o = N2t_chips.Not.create scope { N2t_chips.Not.I.a = i.a }

Each chip has:

  • Stub - Template code you complete (Nand2Tetris exercises)
  • Solution - Reference implementation (Nand2Tetris Solutions)
  • Test - Pre-written tests to verify your implementation

N2T and Hardcaml have different MUX conventions:

Conventionsel=0sel=1
N2Tout = aout = b
Hardcaml mux2out = lowout = high

The mux16_ helper follows N2T convention, so use it for clarity.

  1. Open the IDE
  2. Select a chip from “Nand2Tetris” dropdown
  3. Implement the create function using N2t_chips.Nand.nand
  4. Click Run to test
  5. Check “Nand2Tetris Solutions” to compare

Build all basic gates from NAND: NOT, AND, OR, XOR, MUX, DMUX, plus 16-bit variants.

Build arithmetic circuits: HalfAdder, FullAdder, Add16, Inc16, and the ALU.

Build sequential circuits: DFF, Bit, Register, RAM8 through RAM16K, and PC.

Build the complete Hack computer: Memory (RAM/Screen/Keyboard), CPU, and the full Computer system.

  1. Start simple - NOT is just NAND(a, a)
  2. Build hierarchically - Use previous chips to build new ones
  3. Check waveforms - Visual debugging is your friend
  4. Compare solutions - Learn from the reference implementations