8 bit computer from a single NAND gate to a working CPU that runs a neural network it trained itself (No PyTorch nor numpy).
Quartz starts with just a NAND and builds up: gates -> ALU -> registers and RAM -> CPU with custom instruction set -> assembler and runtime -> fixed-point math in assembly -> neural network. Project demo is a digit classifier whose forward pass executes on the gate built CPU. Beneath those is a fully design verified processor.
NAND >> NOT/AND/OR/XOR/MUX >> adder >> ALU + flags >> registers / RAM / index reg >> ISA >> CPU >> assembler >> runtime >> fixed-point math >> MLP (XOR, digits)
375 written and passing tests are included
Built in layers. It begins with a single bit represented as a Python int and a single NAND gate. Every other gate is derived from that. Those gates wire together to a ripple-carry adder and an 8 bit ALU, and the ALU computes its status flags from gates. Memory is a D-latch, a multiplexer choosing between hold & load and latches stack up into registers, a reg file, RAM, and a 16-bit program counter, completing the datapath.
On top of that is the instruction set architecture. Two
CPUs implement that one ISA. GateCPU pushes every value through the gate-built
hardware, albeit authentic, but thousands of NAND evaluations per ins. FastCPU
emulates the same ISA directly in Python, which is way faster. A conformance harness proves they agree on every register, flag, and memory cell at every
step.
Everything else builds on that, from a two pass assembler and disassembler, step debugger, a data bridge that loads files from a host folder into RAM, a Q4.4 fixed point math library written in assembly, and lastly the neural networks which encompasses a forward pass and a digit classifier running on the gates.
digital design. Everything is built bottom-up from NAND: combinational logic (gates -> ripple-carry adder -> ALU), sequential logic (D-latch -> register -> register file -> RAM -> program counter), a datapath, control flow, and status flags. processor is also design-verified where a gate level CPU and an independent fast emulator of the same ISA are proven bit-identical by a conformance harness that compares every register, flag, and memory cell at every clk step.
Machine learning. forward pass and fixed-point kernels (signed multiply, dot products, ReLU, argmax) are written as assembly on a custom 8 bit ISA. You can watch what a single multiply accumulate costs in CPU cycles, see why a 1/16th step fixed-point format underflows gradients and how a wider accumulator fixes it. A 2-2-1 net trains itself to compute XOR; a 64-6-3 net classifies 8x8 digit bitmaps loaded 'from a folder on disk' and run on the gates.
pip install -e ".[dev]"
pytest -q # 375 tests
# run assembly programs on the machine
python -m quartz.run programs/countdown.asm # OUT: [5, 4, 3, 2, 1]
python -m quartz.run programs/square.asm # OUT: [144, 1] (20*20 = 400)
python -m quartz.run programs/add.asm --debug # one step trace
python -m quartz.run programs/countdown.asm --gate-level # slow, authentic gate CPUTrain a net and run:
from quartz.asm import assemble_file
from quartz.cpu import GateCPU
from quartz.fixedpoint import to_fixed, from_fixed
from quartz.train import train_xor, quantize, lay_weights
params, _ = train_xor() # SGD learns XOR from scratch
q = quantize(params) # weights to Q4.4 bytes
img = assemble_file("programs/xor_forward.asm")
cpu = GateCPU(img); lay_weights(cpu, q) # learned weights into RAM
cpu.ram[0x10], cpu.ram[0x11] = to_fixed(1), to_fixed(0)
cpu.run()
print(round(from_fixed(cpu.out[0]))) # 1 XOR 0=1 computed on gates8 general registers (R0-R7, 8 bit), a 16-bit program counter and stack pointer, an H register for high byte of multiply, a 16-bit index register Pfor walking
arrays, and Z/C/N flags. 8-bit data, 16-bit (little-endian) addresses, 4 KB
RAM.
| Group | Instructions |
|---|---|
| Move / memory | LDI, MOV, LD, ST, LDX, STX |
| Index register | SPL, SPH, LDP, INCP |
| ALU | ADD, ADC, SUB, AND, OR, XOR, NOT, INC, DEC, SHL, SHR |
| Multiply | MUL (8x8->16, into H:Rd), MFHI |
| Control | JMP, JZ, JNZ, JC, JN, CALL, RET |
| Misc | NOP, HLT, OUT |
tbd