Skip to content
Programming the FPGC

Programming the FPGC

For programmers

There are two ways to put a program on the FPGC, and the leftmost DIP switch chooses which one is used after a reset:

WayWhere the program goesSwitchWhen it runsFor
over the UARTSDRAMoffright away; it’s gone after a power cycletrying out a program while writing it
into the flashflash 1onafter every reset and power cycle, without a PCa program that stays, later the operating system

Both start from a binary image from the assembler, assembled for address 0. Both go through uartload on the PC, which is in Toolchain/uartload.

The FPGA’s own design, in the board’s configuration flash, is something else: see building and programming.

After a reset

The boot ROM runs after every reset, and checks the leftmost DIP switch:

reset ──> boot ROM
            │
            ├── switch off ──────────────────────────────> boot: ready, waits for uartload
            │
            └── switch on ──> a valid program in flash 1?
                                │
                                ├── yes ──> runs it
                                │
                                └── no ───> boot: ready, waits for uartload

So with the switch off, the FPGC always waits for the PC. With the switch on, it runs the program in flash 1, and only waits for the PC when the flash has no valid program: when it’s empty, or was only half written.

Running a program over the UART

With the switch off:

b32asm hello.asm hello.bin
uartload hello.bin

uartload resets the FPGC, sends the program to the boot ROM, which writes it into SDRAM and runs it. Then uartload shows what the program sends, until it jumps back to the boot ROM:

boot: err 00000000 pc 00000000
boot: ready
boot: ok 9afbeb43
hello, world
boot: err 00000000 pc 00000000
boot: ready

With the switch on, uartload can’t do this: after its reset, the program from the flash runs instead of the boot ROM’s UART loader. uartload sees the boot ROM’s boot: flash ok, stops, and says to turn the switch off.

Writing a program into the flash

Also with the switch off: uartload runs the flash writer, a program that receives another program and writes it into flash 1.

b32asm flash_writer.asm flash_writer.bin
b32asm program.asm program.bin
uartload flash_writer.bin --send program.bin
boot: err 00000000 pc 00000000
boot: ready
boot: ok <crc>
flash: ready
flash: ok <crc>
flash: written <crc>
boot: err 00000000 pc 00000000
boot: ready

Then turn the switch on, and reset the FPGC with the button or a power cycle. The boot ROM prints boot: flash ok <crc>, and runs the program. To go back to programming over the UART, turn the switch off. The program stays in the flash until the flash writer writes another one.

In the repository, fpgc board flash <program.asm> does these three steps.

uartload

uartload [--port PORT] [--baud BAUD] [--send FILE] program.bin
OptionWhat
--portthe FPGC’s serial port, /dev/ttyUSB0 by default
--baudthe UART’s speed, 1,000,000 by default, as the FPGC’s UART
--senda file to send to the program once it’s running: see sending a file

What it does

  1. It resets the FPGC, by setting RTS without DTR for 10 ms (see reset). The boot ROM prints the error register, and then boot: ready. If the boot ROM prints boot: flash ok instead, uartload stops, and says to turn the switch off.
  2. It waits for boot: ready, at most 1 second.
  3. It sends the load command: L, the length, the program, and its CRC-32.
  4. It waits for the boot ROM’s answer. With boot: ok and the right CRC, the program runs. Otherwise it starts again with a reset, up to 3 times in all, and then stops with an error.
  5. It shows what the FPGC sends, until the program jumps back to the boot ROM (the next boot: ready), or until Ctrl-C.

The protocol, with every byte and every answer, is on the boot ROM page. At 1 Mbaud, a program loads at about 100,000 bytes per second.

Sending a file to a program

With --send FILE, the program that uartload runs gets a file the same way the boot ROM gets a program. After step 4, uartload waits for a line <name>: ready from the program, sends L, the length, the file and its CRC-32, and waits for <name>: ok <crc>, with up to 3 tries on <name>: bad. Then it goes on with step 5.

uartload doesn’t know what the program does with the file: the flash writer writes it into the flash, but any program that announces itself and answers this way can get one.

The source files

FileWhat
protocol.pythe load command and the answers, without the serial port
board.pythe FPGC over its serial port: the reset, the load with its retries, sending a file to a program, and reading the lines it sends
cli.pythe command

The tests are in Toolchain/uartload/tests, with a boot ROM that is faked on the PC.

Last updated on