I've been doing Some Thinking, inspired by my new adventures with FPGAs. I've be...

@jpm@aus.social

I've been doing Some Thinking, inspired by my new adventures with FPGAs. I've been thinking about how you make APIs for #FPGA modules. The thing about FPGAs is that they're not sequential like CPUs - anything can happen at any time, no matter what state you’re in, so you need to be aware of what's going on around you at all times and either prevent something from happening that you're not prepared for or delay something from happening if you're not ready yet. It's a bit like programming with threads, but everything you do is a thread and every single thread is active all the time and will never be switched out.

Take an example of outputting a bunch of letters to an output device: in a single-threaded program you just call eg println() and out pops your characters. In a multi-threaded program you might need to make sure you're not calling println() at the same time as another thread if your println() doesn't do that for you (and quite a few of them do). Same with on a FPGA, only you have to bring your own println() too. And the output method. And the bits to glue them all together.

Now, the big "problem" with FPGAs is, there's no concept of subroutines as found in the CPU world. When you add code, it adds entire blocks of logic to the design, so every time you add eg println(), the entire println() routine is basically inlined into the program with the argument statically compiled in each time, but in a device with a strictly limited amount of space available.

So, what should a subroutine kinda look like in the FPGA world? I have no idea what experienced folks do, but the pattern I've stumbled into uses a pair of signals: a "start running this" signal that is sent to the function to start it running, and a "I've finished running" signal send from the signal back to the caller to indicate when it's finished, with all the logic buried inside the module including claiming mutexes etc.

Are there better ways to do this I'm not aware of yet?