Skip to content
Toolchain part 4 - Booting from SPI flash

Toolchain part 4 - Booting from SPI flash

2026-11-28·
Bart Pleiter

With the UART bootloader, the FPGC runs any program the PC sends it, but only while a PC is connected. Now that the SPI flash works, the boot ROM can also load a program from there, so the FPGC can start on its own after a power cycle. This post is about that second way of booting, and about how a program gets into the flash in the first place.

Two SPI flashes, and a third one

The FPGC board has three flash chips, which is a bit confusing at first:

  • A relatively small one (8 MiB) that holds the FPGA’s configuration: the bitstream that turns the FPGA into the FPGC. The FPGA reads it by itself at power-on, before any of my logic exists.
  • Flash 1 (16 MiB), for the program that the boot ROM loads: later BDOS, the operating system.
  • Flash 2 (16 MiB), for files, later.

So after a power cycle, two things happen one after the other: the FPGA configures itself from its own flash, and then the boot ROM in the FPGC loads a program from flash 1.

UART or flash

The boot ROM needs to know where to load from. In the previous FPGC version, this was already decided by a DIP switch on the board, and that works fine: the leftmost of the four switches indicates flash when it’s moved up, and UART when it’s moved down.

The boot ROM checks the switch after a reset, right after it printed the error register. With the switch off, nothing changes: boot: ready, and it waits for UART data. With the switch on, it reads the program from flash 1 into SDRAM and runs it.

The image in flash

For the format in the flash, I simply reused the UART protocol from the previous post, without the L:

BytesWhat
4length of the program in bytes
Nthe program itself
4CRC-32 of the program

So the boot ROM reads the length, reads the program into SDRAM while it computes the CRC-32, and compares it with the CRC at the end. The same function computes the CRC for both the UART and the flash, a word at a time.

This also makes the boot ROM safe against an empty or broken flash. An erased flash reads all 1s, so its length is FFFFFFFF, which is more than the flash can hold. And a program that was only half written has a wrong CRC. In both cases, the boot ROM says so, and falls back to the UART:

boot: err 00000000 pc 00000000
boot: flash bad length
boot: ready

So with the switch on, the FPGC never gets stuck: either the program in the flash runs, or the PC can load a new one.

The flash is read with a plain one-line read, word by word with the read-ahead from the SPI post, at about 6 MB/s. The CRC, one bit at a time, is slower than that. But a boot program is small, and for a program of a few hundred kilobytes, it all takes a fraction of a second.

Note that this CRC-32 check at the end is an improvement of the previous FPGC revisions, which did not verify the program. This was actually fine most of the time, since 0xFFFFFFFF (the value of an erased flash) equals the halt instruction, so errors would most likely result in just halting the CPU.

Writing the SPI flash

The boot ROM only reads the SPI flash. Writing it is the job of a separate program, the flash writer, which the boot ROM loads over the UART like any other program. That keeps the boot ROM small, and the flash writer can be changed without rebuilding the FPGA design.

The flash writer receives the program with the same protocol as the boot ROM: it prints flash: ready, and answers with flash: ok or flash: bad. Then it writes the SPI flash in the way flash memory needs:

  1. It erases the blocks of 64 KiB that the program needs. A flash can only change bits from 1 to 0, so a block has to be erased (to all 1s) before anything new can go there.
  2. It programs the image, a page of 256 bytes at a time, and waits after each page until the flash is done.
  3. It reads everything back, with the fast quad read, and compares every word with what it received.
  4. It prints flash: written, with the CRC, and jumps back to the boot ROM.

Most of the time goes into the flash itself: erasing a block takes about 0.15 s, and programming a page about 0.4 ms.

Sending a file to a program

On the PC, uartload needed one more feature: after loading the flash writer, it has to send it the program for the flash. I made that generic: with --send, uartload waits until the running program prints a line ending in : ready, and then sends the file with the same protocol, including the retries on a wrong CRC. It doesn’t know anything about flashes, so any program can receive a file this way later.

~/repos/FPGC$ uartload build/flash_writer.bin --send build/hello.bin
boot: err 00000000 pc 00000000
boot: ready
boot: ok 01d75b8d
flash: ready
flash: ok 9afbeb43
flash: written 9afbeb43
boot: err 00000000 pc 00000000
boot: ready

With the switch on, uartload can’t load programs anymore: when it resets the FPGC, the program from the flash starts instead of the UART bootloader. uartload recognizes the boot ROM’s line for that, and says to turn the switch off.

Hardware test

For a hardware test, I wrote a small program to the flash that prints a line and halts, turned the switch on, and reset the board:

boot: err 00000000 pc 00000000
boot: flash ok 8c8ff112
hello from the flash

Until now, I always used the .sof file from Quartus to program the FPGA, which works just like the UART bootloader in the FPGC as in it gets erased after a power cycle, and the previous FPGC version was still on the FPGA’s configuration flash. I now updated that bitstream config with the new FPGC design, so on a power cycle the FPGA configures itself with the new FPGC design, and automatically loads and runs the program from the SPI flash. While this sounds silly, it is still a nice milestone to have the FPGC work independently, without needing a PC to load its programs.

Next steps

I have not decided the next step yet, as there are many logical directions to take from here. I’m just going to list some of them here and you will see in the next post what I ended up doing:

  • Tile based rendering for the GPU, first graphical output on the screen.
  • A post on the PCB I already made a year ago
  • A start on the C compiler
  • The CLI tools to interact with Quartus
  • Something else perhaps
Last updated on