Programming the FPGC
There are two ways to put a program on the FPGC, and the leftmost DIP switch chooses which one is used after a reset:
| Way | Where the program goes | Switch | When it runs | For |
|---|---|---|---|---|
| over the UART | SDRAM | off | right away; it’s gone after a power cycle | trying out a program while writing it |
| into the flash | flash 1 | on | after every reset and power cycle, without a PC | a 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 uartloadSo 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.binuartload 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: readyWith 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.binboot: err 00000000 pc 00000000
boot: ready
boot: ok <crc>
flash: ready
flash: ok <crc>
flash: written <crc>
boot: err 00000000 pc 00000000
boot: readyThen 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| Option | What |
|---|---|
--port | the FPGC’s serial port, /dev/ttyUSB0 by default |
--baud | the UART’s speed, 1,000,000 by default, as the FPGC’s UART |
--send | a file to send to the program once it’s running: see sending a file |
What it does
- 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 printsboot: flash okinstead, uartload stops, and says to turn the switch off. - It waits for
boot: ready, at most 1 second. - It sends the load command:
L, the length, the program, and its CRC-32. - It waits for the boot ROM’s answer. With
boot: okand 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. - 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
| File | What |
|---|---|
protocol.py | the load command and the answers, without the serial port |
board.py | the 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.py | the command |
The tests are in Toolchain/uartload/tests, with a boot ROM that is faked on the PC.