DEV Community

Cover image for I built a small RISC-V computer inside a Tang Nano 9K FPGA
sinhaparth5
sinhaparth5

Posted on Originally published at edu.parthsinha.com

I built a small RISC-V computer inside a Tang Nano 9K FPGA

I built a small RISC-V computer inside the Gowin GW1NR-9 FPGA on a Tang Nano 9K, a board that costs around $15 to $20, using only open-source tools: Yosys, nextpnr, Apicula and openFPGALoader. It ended up with a PicoRV32 CPU, 16 KB of RAM, a hand-written UART, a custom hardware accelerator and an HDMI text console. I wrote it up as five parts. Each one adds one block, explains the ideas it needs, and then shows what I built and measured.

The five parts

  • Part 1: an open-source toolchain and a blinking counter. What synthesis, place and route, bitstreams and constraint files are, then a six-line counter that drives six LEDs. It uses 33 of the chip's 8,640 LUT4s.
  • Part 2: a UART from scratch. Baud rates, mid-bit sampling and synchronizers, then a transmitter and receiver in about 40 lines of Verilog, tested in simulation first and then echoing typed characters on the board.
  • Part 3: a PicoRV32 CPU running C. RV32IMC, memory-mapped I/O and linker scripts. The firmware is baked into the bitstream, so there's no bootloader. CPU, RAM and UART together take 47% of the LUTs and close timing at 51 MHz against a 27 MHz clock.
  • Part 4: a hardware accelerator, 46× faster than C. A memory-mapped xorshift32 block that runs one step per clock, plus a small shell with peek, poke and bench commands.
  • Part 5: an HDMI text console. Pixel clocks, video timing and TMDS encoding, then a 90×30 text display that mirrors the shell. It scrolls by changing one register instead of copying the screen.

The 46× number

On the board, bench 1000 took 50,015 cycles in C and 1,066 cycles on the accelerator, with identical results. The accelerator itself does one step per clock, so about 66 of those cycles are the cost of talking to it over the bus. That also means a single step at a time would be slower in hardware than in software. The speedup only shows up when each call does enough work.

What's not done yet

Parts 1 to 4 run on the board. Part 5 passes its simulation tests and builds a bitstream, but I haven't connected it to a monitor yet, so how it looks on a real screen is still unknown.

Things that went wrong

Each part has the problems I hit along the way. A few of them: on Linux, ModemManager kept probing the board's serial port, so the shell received its own output back as typed commands. A testbench passed even though the design was broken, and I only found out by feeding it bad input on purpose. And the first HDMI bitstream failed to pack until I switched from true LVDS buffers to emulated ones.

The code is on GitHub, with one folder per part: https://github.com/sinhaparth5/tangnano9k-riscv-soc

If LUTs and flip-flops are new to you, start with my notes on what's inside an FPGA: https://edu.parthsinha.com/posts/understanding-fpga/

If you've put a soft CPU on a small FPGA, what did you add after the UART?

Top comments (0)