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.
What is Nand2Tetris?
Section titled “What is Nand2Tetris?”Nand2Tetris (also known as “The Elements of Computing Systems”) teaches you how computers work by having you build one from scratch:
- Project 1 - Boolean Logic (NOT, AND, OR, XOR, MUX, DMUX)
- Project 2 - Arithmetic (HalfAdder, FullAdder, ALU)
- Project 3 - Memory (DFF, Register, RAM)
- Project 5 - CPU and Computer
Why Hardcaml?
Section titled “Why Hardcaml?”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
How It Works
Section titled “How It Works”The N2t_chips Library
Section titled “The N2t_chips Library”All chips are available through the N2t_chips module:
(* Use helper functions for concise syntax *)let not_a = N2t_chips.not_ scope i.alet 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 }Exercise Structure
Section titled “Exercise Structure”Each chip has:
- Stub - Template code you complete (Nand2Tetris exercises)
- Solution - Reference implementation (Nand2Tetris Solutions)
- Test - Pre-written tests to verify your implementation
Key Convention
Section titled “Key Convention”N2T and Hardcaml have different MUX conventions:
| Convention | sel=0 | sel=1 |
|---|---|---|
| N2T | out = a | out = b |
Hardcaml mux2 | out = low | out = high |
The mux16_ helper follows N2T convention, so use it for clarity.
Getting Started
Section titled “Getting Started”- Open the IDE
- Select a chip from “Nand2Tetris” dropdown
- Implement the
createfunction usingN2t_chips.Nand.nand - Click Run to test
- Check “Nand2Tetris Solutions” to compare
Project Overview
Section titled “Project Overview”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.
Tips for Success
Section titled “Tips for Success”- Start simple - NOT is just
NAND(a, a) - Build hierarchically - Use previous chips to build new ones
- Check waveforms - Visual debugging is your friend
- Compare solutions - Learn from the reference implementations