Skip to content

About

A working 16-bit computer built from NAND up: 31 chips, an ALU, a CPU, and two Hack assembly programs. 37 test scripts pass from a clean clone.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Hack Computer

A working 16-bit computer, built up from a single primitive gate.

Every chip in this repository is composed from NAND and nothing else. Fifteen elementary gates become an ALU; the ALU plus a register file becomes a CPU; the CPU plus memory and a ROM becomes a machine that runs compiled programs. It multiplies two numbers with no multiply instruction and paints a screen from live keyboard input, because at this level the hardware provides neither.

This is the hardware track of The Elements of Computing Systems (Nisan & Schocken, MIT Press) — projects 1 through 5.

37 test scripts pass, 0 fail. ./run-tests.sh proves it in one command.


The build chain

Each layer may only use what the layers below it have already built.

       Nand                                  (the one primitive)
        |
  01 -- Not  And  Or  Xor  Mux  DMux         elementary logic
        Not16  And16  Or16  Mux16
        Or8Way  Mux4Way16  Mux8Way16  DMux4Way  DMux8Way
        |
  02 -- HalfAdder -> FullAdder -> Add16      arithmetic
        Inc16
        ALU                                  18 operations, one adder
        |
  03 -- DFF -> Bit -> Register               sequential logic
        RAM8 -> RAM64 -> RAM512 -> RAM4K -> RAM16K
        PC
        |
  05 -- CPU  +  Memory  +  ROM32K  =  Computer
        |
  04 -- Mult.asm   Fill.asm                  programs the machine runs
Directory Chips
01-boolean-logic Not And Or Xor Mux DMux, 16-bit forms, Or8Way, Mux4Way16, Mux8Way16, DMux4Way, DMux8Way
02-boolean-arithmetic HalfAdder FullAdder Add16 Inc16 ALU
03-sequential-logic Bit Register RAM8 RAM64 RAM512 RAM4K RAM16K PC
04-machine-language Mult.asm Fill.asm
05-computer-architecture CPU Memory Computer

Design notes

The ALU computes eighteen functions with one adder. Rather than switching between separate arithmetic units, both inputs pass through Mux16 stages that can zero them (zx, zy) and negate them (nx, ny) before a single add-or-and stage (f), with a final optional negation (no). Six control bits, eighteen operations, one piece of arithmetic hardware. Subtraction, negation and the constants all fall out of De Morgan equivalences applied to that one adder.

Memory scales by a factor of eight per layer. RAM8 is eight Registers behind a DMux8Way on the load line and a Mux8Way16 on the outputs. RAM64 is eight RAM8s wired identically, and so on up to RAM16K. The address splits cleanly: high bits select the bank, low bits recurse into it.

The CPU decodes one bit and fans out. instruction[15] distinguishes an address load from a computation. Everything else follows from it: the A register takes either the literal instruction or the ALU result; the D and M writes are gated on being a C-instruction, so an address load can never corrupt memory; and the three jump bits are resolved against the ALU status flags, where "greater than zero" is derived as not (zr or ng).

Memory maps three devices into one address space. The top two address bits route to RAM, Screen or Keyboard, so the CPU sees one flat 15-bit space and never knows that writing to 16384 lights a pixel.

The two programs

Mult.asm multiplies R0 by R1 into R2 by repeated addition, since the Hack instruction set has no multiply. It returns immediately when either operand is zero rather than entering the loop at all.

Fill.asm polls the keyboard and paints all 8192 screen words black while any key is held, white when none is. The fill bound is computed as SCREEN + 8192, which stops exactly one word below the keyboard register at 24576 — writing a single word further would clobber the very input it is polling.


Running it

Requires Java and the nand2tetris simulators, a free download that is not redistributed here.

git clone https://github.com/Khizarc/hack-computer.git
cd hack-computer

# Get the tools from https://www.nand2tetris.org/software and unzip them
export NAND2TETRIS_HOME=/path/to/nand2tetris

./run-tests.sh

The runner also finds the tools at ./tools or ../nand2tetris/tools if you would rather not set the variable. It selects the hardware simulator for chip tests and the CPU emulator for assembly, skips the two scripts that require a human at the keyboard, and exits non-zero on any failure, so it works unchanged as a CI gate.

$ ./run-tests.sh
ok    01-boolean-logic/And.tst
ok    01-boolean-logic/Mux8Way16.tst
ok    01-boolean-logic/Xor.tst
ok    02-boolean-arithmetic/ALU.tst
ok    02-boolean-arithmetic/Add16.tst
ok    03-sequential-logic/PC.tst
ok    03-sequential-logic/RAM16K.tst
ok    03-sequential-logic/Register.tst
skip  04-machine-language/Fill.tst                   (interactive: needs a keyboard)
ok    04-machine-language/FillAutomatic.tst
ok    04-machine-language/Mult.tst
ok    05-computer-architecture/CPU.tst
ok    05-computer-architecture/CPU-external.tst
ok    05-computer-architecture/ComputerAdd.tst
ok    05-computer-architecture/ComputerMax.tst
ok    05-computer-architecture/ComputerRect.tst
skip  05-computer-architecture/Memory.tst            (interactive: needs a keyboard)
ok    05-computer-architecture/MemoryAutomatic.tst
------------------------------------------------------------
37 passed, 0 failed, 2 skipped

(abridged — a full run lists all 39 scripts)

If the simulators are missing it says so and points you at them, rather than failing obscurely:

$ ./run-tests.sh
error: could not find the nand2tetris simulators.

Download the free course software from https://www.nand2tetris.org/software
(it needs a Java runtime), then either:

    export NAND2TETRIS_HOME=/path/to/nand2tetris
    ./run-tests.sh

or place the "tools" directory at the root of this repository.

To watch Fill.asm actually paint the screen, open 04-machine-language/Fill.asm in the CPU emulator GUI and hold down a key.


Development notes

The hardware was correct on arrival. All 35 upstream test scripts passed unmodified before any change was made, and no chip logic has been edited since. The work in this repository was closing the gap between passing and verifiable: two of the five projects could not be checked without a human at the keyboard, and one chip had no test at all.

Xor shipped with no test script

Project 1 included Xor.hdl but neither Xor.tst nor Xor.cmp. That left the one gate in the directory composed from other custom chips — rather than directly from NAND — as the only one never verified, which is precisely the one most worth checking. Added both files following the form of the sibling scripts; it passes across the full four-row truth table.

Memory.tst cannot terminate without a keyboard

The upstream script spins in while out <> 75 { tick, tock } waiting for someone to hold down K, then again on Y. Run headless it never returns — a 120-second timeout in the harness was the only thing that ended it, and a timeout is indistinguishable from a hung chip.

The cost was not cosmetic. It meant the RAM and Screen address decoding, the cross-segment overwrite checks, and the low-order address-bit checks had no automated coverage at all — on the chip where a wiring mistake is easiest to make and hardest to see. MemoryAutomatic.tst retains all 66 of those comparison rows, with expected values taken unchanged from the upstream Memory.cmp, and drops only the two keyboard waits. Memory.tst is kept for the interactive keyboard path and is skipped by the runner.

Fill.tst has the same defect

repeat { ticktock; } never returns. It is skipped in favour of the upstream FillAutomatic.tst, which drives the same program with scripted input.

run-tests.sh was committed non-executable

Committed at mode 100644, so ./run-tests.sh failed with permission denied on a clean clone — the one path nobody tests, because the author always has a working copy. Caught by cloning into a temporary directory and running it as a stranger would. Fixed to 100755.

Result: every chip and both programs are verified by a single command with no human in the loop, and the repository has been checked from a fresh clone.


Attribution

The chip implementations (.hdl) and the assembly programs (.asm) are my own work.

The test scripts, comparison files and .hack binaries (.tst, .cmp, .hack) are course materials from nand2tetris.org by Noam Nisan and Shimon Schocken, committed here so that results are reproducible rather than taken on trust. Each retains its original header. Xor.tst, Xor.cmp and MemoryAutomatic.tst are mine.

Licensed under the MIT License — see LICENSE.

About

A working 16-bit computer built from NAND up: 31 chips, an ALU, a CPU, and two Hack assembly programs. 37 test scripts pass from a clean clone.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages