DEV Community

Cover image for What's inside an FPGA, explained with a Tang Nano 9K
sinhaparth5
sinhaparth5

Posted on Originally published at edu.parthsinha.com

What's inside an FPGA, explained with a Tang Nano 9K

Coming to FPGAs from an Arduino or ESP32, the hardest part is the change in mindset. You aren't writing a program for a CPU to run one line at a time. You're describing a circuit, and the tools build it out of blocks that are already sitting on the chip. I wrote up notes on what those blocks are, partly from EEVblog's "What Is An FPGA?" video, with the Tang Nano 9K as the running example.

The blocks

IOBs (input/output blocks) sit behind each physical pin and form a ring around the edge of the chip. They set the pin's direction, drive strength, slew rate and voltage standard, and each one has its own flip-flops, so a signal can be latched right at the pin. Drive strength isn't voltage, though. A 3.3 V pin driving a 5 V part like a WS2812 LED strip still needs a level shifter.

CLBs (configurable logic blocks) are where the logic happens, and there are no AND or OR gates in them as such. A LUT is a tiny truth table that stores the output for every input combination, so one LUT4 can implement any boolean function of four inputs. Next to the LUTs are D flip-flops for state and a carry chain that lets adders ripple between neighbouring cells without going out to the slower general routing.

All of that configuration lives in SRAM, which forgets everything when the power goes off. The compiled bitstream is kept in flash and loaded into the SRAM at power-up. The Tang Nano 9K's Gowin GW1NR-9 has the flash built in, so a design is running almost as soon as the board gets power.

What a blinker turns into

module led_blink (
    input  wire clk,
    output reg  led
);
    reg [23:0] counter = 0;

    always @(posedge clk) begin
        counter <= counter + 1'b1;
        led     <= counter[23];
    end
endmodule
Enter fullscreen mode Exit fullscreen mode

When the tools synthesize this, counter becomes 24 flip-flops, the + 1'b1 becomes LUTs plus the carry chain, and led is a register driving a pin, so it can go in that pin's IOB flip-flop. Which pin, and at what voltage, comes from a separate constraints file.

The post also lists the GW1NR-9's resources (8,640 LUT4s, 6,480 flip-flops, 26 block RAMs and 10 DSP multipliers), links the two Gowin documents I used, and looks at what has changed since that EEVblog video came out in 2013: power, $15 to $20 boards, free and open-source tools such as Yosys and nextpnr, and hard IP in the silicon.

https://edu.parthsinha.com/posts/understanding-fpga/

I later used the same board to build a small RISC-V computer, written up as a five-part series starting here: https://edu.parthsinha.com/posts/riscv-soc-1-toolchain-and-blink/

If you came to FPGAs from microcontrollers, what took you longest to unlearn?

Top comments (0)