Compiling C# Programs into FPGA Circuits: Factorial Example

This blog article gives a practical introduction to how C# programs can be compiled into FPGA circuits using the Kiwi system developed by myself and David Greaves at the University of Cambridge Computer Lab. Our starting point will be a C# program developed inside Visual Studio 2010 for computing factorial:

image

This program can be compiled and executed inside Visual Studio to report the factorial of 5 (i.e. 120).

image

However, using our Kiwi system this program can also be converted into a Verilog circuit. The program is decorated with some custom attributes which specify which static method should be turned into hardware; the number of bits used to represent input and output ports and how the circuit should behave relative to a circuit clock.

The generated Verilog netlist can be simulated using a Verilog simulator. The Modelsim simulator output produced by simulating this circuit with an input of 5 for the factorial circuit is shown below. After a few clock cycles the result 120 is produced and the done output bit goes high.

kiwi_fac_modelsim

Or if you like you can run the simulation on the command line:

image

The generated Verilog can be used as input to the Xilinx design tools to generate a programming bit-stream to program an FPGA. The screen-shot below also includes a top-level module which instantiates some debugging input and sets the default input to the factorial circuit to be 5.

image

The generated programming bit-stream can then be run on a real FPGA board. I used a Xilinx ML605 which has a Xilinx Virtex-6 XC6VLX240T-1FG1156 FPGA on it:

image 

I can check the functionality of the circuit by running a software logic analyzer on my PC which reports the value of internal signals of the circuit running on the FPGA.

kiwi_fac_chipscope

The logic analyzer shows that this circuit indeed computes the factorial of 5 to be 120 after 4 clock cycles. This circuit is running at 100MHz.

You can get a warm and fuzzy feeling by looking at the floor-plan of the generated circuit (light blue specks on the middle left hand side) which also includes the debug circuitry required to support the logic analyzer:

image

This lower-level view shows the actual routing tracks for the wires.

image

Comments

  • Anonymous
    October 18, 2010
    This is really interesting. What about hw-sw codesing? Can code be automatically partitioned?

  • Anonymous
    October 18, 2010
    A veyr good question! We've considered cross boundary method calls which get implemented with FIFOs (using Xilinx's FSL link) where the software runs on a Microblaze processor and the hardware speaks to the processor using a protocol devised by Xilinx. However, we do not have anythying stable implemented right now.

  • Anonymous
    October 21, 2010
    what are your thoughts on c to fpga software?   I know there's a few open source projects, but also commerical ones that have been around for  a while (impulse-c, catalyticinc/Celoxica,  en.wikipedia.org/.../C_to_HDL ) Any experience with these?

  • Anonymous
    October 21, 2010
    The comment has been removed

  • Anonymous
    October 21, 2010
    The comment has been removed

  • Anonymous
    October 21, 2010
    @Sameer: you can use any library methods that conform to the constraints for what we can synthesize into circuits. So currently things like static methods that behave in a C-like manner are supported although we plan to wider the language coverage to a wider class of operations and to dynamic memory allocation (by Microsoft website has a couple of papers that describe how to compile malloc/free to gates).

  • Anonymous
    April 04, 2011
    Hi Satnam, Where can I find the kivi tool software?  Does your tools support Altera FPGA Stratix board?  I'm really interested about convert C#... What the different your C# to Verilog?  Is this optimized code? Thanks, Sean

  • Anonymous
    December 26, 2011
    I think you've done really great job here guys. Thank you very much

  • Anonymous
    January 23, 2012
    The comment has been removed

  • Anonymous
    June 18, 2012
    The comment has been removed

  • Anonymous
    June 27, 2012
    The comment has been removed

  • Anonymous
    October 22, 2014
    Hello. Is this project still alive? Is it possible to get binaries or sources for research? Thanks.