hey kremlin how do FPGAs work

that’s a great question kremlin, lets investigate

FPGAs are digital machines. here is an image of one:

this probably looks a lot like the CPU in your computer, to a degree. your CPU is an example of an ASIC, or “application specific integrated circuit”. ASIC is a very tricky term, that can very misleadingly be explained as “an integrated circuit purpose-built for a single application and no other”

this is to say that your CPU is a compilation of deliberately and exactingly modeled circuitry that has been permanently etched onto a wafer of silicon. it is application-specific, meaning it can perform no other job or function beyond what it is defined to do (process instructions). your CPU can never magically re-wire itself to do something like, say, understand SPARC instructions. it can’t re-wire itself to function as a hardware video decoder or magic bitcoin miner or whatever

that is not the case with FPGAs !

FPGA stands for field programmable gate array and encapsulates the idea of programmable logic. while CPUs are built for one purpose, and can only serve one purpose on a hardware level, FPGAs are built for no specific purpose and can be programmed to perform any number of functions

but this “programming” is unlike what you understand programming to be. your view of programming probably involves writing C or C++ or java or python or whatever, compiling it into a machine-code executable file, and running it on a CPU. that’s all fine and dandy, but understand you are only writing software that executes on our aforementioned CPU. all the logic and control-flow and data-moving you’re doing in your program is in the context of 1s and 0s flying in between your CPU/cache and memory and peripherals. your logic has been translated into a crazy software world that only makes sense to your application-specific, “single job” CPU

such is not the case, with FPGAs !

just like CPUs, FPGAs have a number of pins: these are the little silverish metal legs protruding from the black square in the image above. all of these pins are made of conductive metal that electricity can flow through. this is how information flows in and out of your FPGA, or any other IC. now, in your head, draw a diagonal line across the black square. you should sense that half of the black square’s pins fall to the right side of the line, and the other half to the left half of the line. for simplicity’s sake, think of the left side as “inputs” and the right side is “outputs”

the little black square (chip) receives electricity on the input pins. the electricity provided is always at exactly either five volts (representing a one) or at zero volts (representing a zero). the output pins expel electricity, rather than receives, at exactly the same two levels (1 or 0)

let me visualize this:

the green side is inputs, the red side outputs. but what happens in between the inputs and outputs? well, that is up to you. let’s imagine, again for the sake of simplicity, that each one of these pins is connected in an 8x8 grid. you have 8 input pins and 8 output pins and they all are connected to each other:

now, at every internal intersection of wires in this grid, imagine there to be something called a logic cell (purple) and at every endpoint leading to a red output wire there to be something called a functional output (blue):

now, at every logic cell is a small digital machine made up of transistors that can direct the flow of electricity through the grid, from input 1s and 0s to output 1s and 0s. every logic cell is connected to four wires, at the north/south/east/west positions (as seen in the grid diagram). these transistors can be programmed to perform logic on these wires, i.e. it can see any of the four as inputs or outputs, and can do simple logic to pass signals from input wires to output wires. let’s look at a simple example:

here, three of the four connected wires (north, west, and south wires) are programmed to be inputs. the single red, eastward wire is output. the logic cell is programmed via a look-up table to output ‘1′ on red only if all three green inputs are also ‘1′

it is now an AND gate

this may sound rudimentary to you, but understand that real FPGAs are not 8x8. they are 64x64 or 256x256. they have a whole lot more room for logic to be placed than our example above. this capacity for programming leads to a generic device that can be programmed to do whatever, and complete that ‘whatever’ task at a hardware level, maybe one hundred thousand times faster and more efficiently than in software. by layering logic like this, you can make the FPGA perform any number of complex tasks. for example, on a reasonably powerful xilinx 7 series FPGA, you can simulate fucking eight super nintendo consoles at once. now compare that to cycle-perfect SNES emulators that take the full power of a sandbridge intel CPU to run in software!

anyway, as data flows from the input pins and through the programmable logic cells, the eventually end up at endpoints, these can be anything. on my small FPGA, the DE0 nano, most are just these macroscopic GPIO pins i can connect stuff to:

you see on the top/bottom there are those two rows of pins? that is where the voltage ends up. it can also end up as an input to a DAC for example, which would allow you to write a functional MP3 decoder and have it output as something you could touch to a speaker wire and have it play music


how are these things programmed to begin with? luckily i have already covered this. FPGAs are programmed in hardware description languages, HDLs

these things are great for engineers. they allow us to write HDL files that describe exactly how a product should work, and let us test our logic on a generic, re-usable device to see if it checks out. then we can move on to making ASICs or whatever

these things are used, often, in place of ASICs. in the case of a, say, microwave oven display panel, you can use an FPGA instead of spending all the time and money to make your own specific chip. you just buy a lot of 10,000 FPGAs and program them all to run your microwave oven time-keeping program or whatever, and it works fine. and it’s cheap and efficient and ideal

this is only a taste of what FPGAs are capable of and especially if you are a young computer engineering or computer science major, you will probably end up working with these and i deplore you to ask me questions about them