← Back to Tech Projects

Custom GPU

NanoGPU is a programmable compute engine built in Verilog on a Tang Nano 20K FPGA. The project grew from a single arithmetic core into a four-lane processor, a dedicated matrix accelerator, and an on-board neural network inference engine. The board ran at 27 MHz.

FPGA bring-up and toolchain

The board uses a Gowin GW2AR-LV18QN88C8/I7 FPGA in the QN88 package. Icarus Verilog ran the testbenches. Yosys mapped the RTL, nextpnr placed and routed it against the 27 MHz constraint, and Apycula packed the bitstream. openFPGALoader used JTAG to load configuration SRAM. A blink test verified programming before compute tests added result comparison over the board’s USB-to-UART bridge.

Linux lsusb output listing the FTDI Dual UART FIFO bridge with USB ID 0403:6010
Original USB enumeration capture. Linux identified the FTDI dual-channel bridge at USB ID 0403:6010.
Terminal output reporting a 6 MHz JTAG clock and the Gowin GW2A(R)-18(C) device
Original JTAG bring-up capture. The 6 MHz JTAG clock applies to programming and device identification.

Programmable parallel arithmetic

Four SIMD lanes share one opcode and operate on independent inputs. Each lane accepts two unsigned 8-bit operands and returns a 16-bit result. The maximum sum is 255 + 255 = 510 and the maximum product is 255 × 255 = 65,025. Both fit in 16 bits. AND and XOR zero-extend their results. A dependent instruction reads the low byte of the previous result, equivalent to reducing it modulo 256.

Each 16-bit instruction starts with a 4-bit opcode. The controller fetches from writable instruction RAM and commits each result before fetching the next instruction. Eight vector registers hold lane results, and four data banks let a load or store move four 16-bit values together. A Python assembler and a framed USB/UART protocol can load programs and data, start execution, and read results while keeping the same bitstream.

Instruction timing includes fetch
OpcodeInstructionCyclesEffect
0x0LOAD rd, addr3Read one 64-bit vector from the four data banks
0x1STORE rs, addr2Write all four 16-bit lanes
0x2–0x5ADD / MUL / AND / XOR4Decode, run the registered SIMD stage, and commit
0x6JMP addr2Replace the program counter with an 8-bit address
0x7HALT2Stop execution

For an arithmetic instruction, bits 15–12 encode the opcode. Bits 11–9, 8–6, and 5–3 select the destination and two source registers. For example,MUL r2, r0, r1 encodes as0x3408. Two LOADs, a MUL, a STORE, and a HALT take 3 + 3 + 4 + 2 + 2 = 14 cycles. At 27 MHz that is 0.519 µs for the measured four-element vector multiply.

The 115,200-baud 8N1 link uses ten wire bits per byte, giving a raw ceiling of 11,520 B/s. A 14-byte request plus a 14-byte response carries eight vector payload bytes, so protocol overhead reduces the ceiling to 11,520 × 8 ÷ 28 ≈ 3,291 B/s. The board measured 1,337 B/s upload and 1,334 B/s readback with 512-byte payloads. CRC checks and serialized ownership protect transfers and processor memory.

Programmable test outputs

The host uploaded three programs into the same FPGA image and checked the four output lanes against NumPy. Vector ADD returned (510, 172, 18, 217), element-wise MUL returned (37,995, 836, 2,750, 54,315), and the dependent MUL → XOR → AND → ADD program returned (403, 128, 223, 45) in 29 cycles. The ADD result of 510 exercises the carry beyond the input width.

A 4×4 systolic matrix accelerator

A matrix product computes one dot product per output element. The 16 processing elements pass signed INT8 operands to neighboring cells while keeping INT32 partial sums local. Larger matrices are tiled to reuse the same array.

At each step, the input slice contains up to four A values and four B values. Skewing the inputs makes operands for the same k arrive together. A moves right and B moves down, so neighboring cells reuse each operand without re-reading it from RAM. The eight-byte input slice corresponds to a feed rate of 8 × 27 MHz = 216 MB/s inside the FPGA. The QN88 board’s UART bandwidth is measured separately.

Cij = ∑k Aik × Bkj

Sixteen cells at 27 MHz give a theoretical peak of 432 million multiply-accumulates per second. The verified 4×4 test performed 64 MACs in 11 cycles, or 157 million MAC/s, including pipeline fill and drain. This used 64 ÷ (16 × 11) = 36.4% of the available cell cycles. The internal time was 0.407 µs, while UART upload, control, and readback took the full transaction to 155.38 ms. NumPy took a median 4.878 µs for the same small product on the host.

Matrix tests verified on the FPGA against NumPy
MatricesEngineCyclesFPGA timeEnd-to-end
4×4 · 4×4Systolic array110.407 µs155.38 ms
4×4 · 4×4SIMD products + host reduction1947.185 µs692.53 ms
3×7 · 7×2Systolic array140.519 µs135.43 ms
5×65 · 65×6Tiled systolic array31611.704 µs3,574.78 ms

Neural network inference on the board

A trained MNIST classifier maps 784 pixels through 32 ReLU units to 10 output scores. The layers contain 784 × 32 + 32 × 10 = 25,408 INT8 weights. Including INT32 biases and packing padding, the uploaded model occupies 25,640 bytes and remains in FPGA RAM between images. The board performs matrix multiplication, bias addition, requantization, ReLU, and argmax.

xi = pixeli − 128

zj = b1,j + ∑i W1,ji xi

hj = clamp(zj >> 14, 0, 127)

scorek = b2,k + ∑j W2,kj hj

prediction = argmaxk scorek

The weights use per-layer power-of-two scales, and each bias is quantized in the corresponding product scale. The arithmetic right shift, saturation, and tie rule are reproduced by the integer reference. The network was trained for 12 epochs in PyTorch, and the hidden shift was selected using 4,096 held-out training images for calibration.

The float and quantized models scored 94.45% and 94.30% on the MNIST test set. Hardware matched every checked intermediate tensor and all 15 reference predictions, including five incorrect classifications. One inference took 6,551 cycles (242.6 µs internally). Uploading an image and retrieving a prediction took about 605 ms with the model already resident.

Dataset and hardware test protocol

MNIST consists of 28×28 grayscale handwritten digits labeled 0–9. Of its 60,000 training images, 55,904 were used to fit the network and 4,096 were reserved to calibrate the integer model. The 94.45% and 94.30% accuracy figures come from the separate 10,000-image test set, evaluated in software.

The physical FPGA check used a diagnostic subset of 15 test images. It included one correctly classified image per digit and five known errors from the integer reference. For each input the host compared 32 signed INT32 preactivations, 32 quantized hidden values, ten signed INT32 output scores, the predicted digit, and the cycle counter. All comparisons passed. The deliberately selected subset measures FPGA agreement with the reference. Classification accuracy is measured across the full test set.

What the board returned

The ten output integers are scores in digit order from 0 to 9. The FPGA returns the digit with the largest score. For test index 3, score 1,467 at position 0 wins and the label is 0. For index 8, score 874 at position 6 beats score 519 at position 5. The reference and FPGA both predict 6 for this image even though its label is 5.

Index 3 | label 0 | prediction 0 | 6,551 cycles
[1467, -996, -108, -67, -1696, 205, 113, -88, -478, -140]

Index 8 | label 5 | prediction 6 | 6,551 cycles
[-655, -899, -302, -965, -10, 519, 874, -706, 402, -305]
Fifteen MNIST test images with labels and board-matched predictions, including five errors
The actual verification inputs, annotated with the bit-exact reference predictions that matched the board. The bottom row shows five model errors against the dataset labels.