This document discusses how to program FPGAs using free and open source software tools. It explains what an FPGA is and how it works, and walks through examples of using Verilog to program simple logic gates and circuits on an FPGA board. These include turning on an LED, implementing a NAND gate in hardware, and using the FPGA as an interactive input/output device. The document promotes the IceStorm toolchain for programming Lattice FPGA boards and outlines several other hardware description languages and frameworks. It concludes by discussing future developments that could expand the use of open source FPGA programming.
3. From NAND to Tetris
Pramode C.E FPGA hacking with Free Software Tools
4. Can you build the Nand2Tetris CPU on real h/w without having to
wire up hundreds of gates on prototyping boards?
Pramode C.E FPGA hacking with Free Software Tools
6. How does an FPGA work?
Pramode C.E FPGA hacking with Free Software Tools
7. Programming an FPGA
Circuit is described using Verilog / VHDL
Simulated to ensure correct operation
Synthesized to a netlist
Place-and-route
Send the "bitstream" to the FPGA
Pramode C.E FPGA hacking with Free Software Tools
8. Programming an FPGA
Simulation can be done using purely free software tools
(example: iverilog)
Proprietary tools required for generating the "bitstream" which
will congure the FPGA.
Pramode C.E FPGA hacking with Free Software Tools
9. The IceStorm Project
Home Page: cliord.at/icestorm
Target: Lattice Semiconductor's ICE40 FPGA's
Main Tools: yosys (synthesis), arachne-pnr (place and
route),icepack(le format conversion),iceprog(device
programming)
Pramode C.E FPGA hacking with Free Software Tools
10. Hello, World!
A simple Verilog model:
module And2(input a, b, output c);
assign c = a b;
endmodule
Pramode C.E FPGA hacking with Free Software Tools
11. A NAND Gate
module Nand2(output c, input a, b);
assign c = ~(a b);
endmodule
Pramode C.E FPGA hacking with Free Software Tools
12. A NAND Gate - by combining modules
module Nand2(output c, input a, b);
wire f;
and G1(f, a, b);
not G2(c, f);
endmodule
Pramode C.E FPGA hacking with Free Software Tools
13. But where is the hardware??
The Lattice IceStick evaluation board
Pramode C.E FPGA hacking with Free Software Tools
14. LED On!
Put on LED1 on the IceStick board. A le with pin-mappings for
LED1,LED2 etc is needed for proper operation.
module Led(output LED1, LED2, LED3, LED4, LED5);
assign LED1 = 1;
assign LED2 = 0;
assign LED3 = 0;
assign LED4 = 0;
assign LED5 = 0;
endmodule
Pramode C.E FPGA hacking with Free Software Tools
15. LED On!
Here is the build script for the previous program(led.v is the verilog
source and led.pcf is the pin mapping):
yosys -p synth_ice40 -blif led.blif led.v
arachne-pnr -d 1k -p led.pcf led.blif -o led.asc
icepack led.asc led.bin
iceprog led.bin
Pramode C.E FPGA hacking with Free Software Tools
16. LED On!
And here is led.pcf:
set_io LED1 99 # red
set_io LED2 98 # red
set_io LED3 97 # red
set_io LED4 96 # red
set_io LED5 95 # green
Pramode C.E FPGA hacking with Free Software Tools
17. Concurrency and Hardware
y = (a or b) and (c or d) implemented on an FPGA:
Pramode C.E FPGA hacking with Free Software Tools
18. Concurrency and Hardware
y = (a or b) and (c or d) evaluated by a microprocessor(note:
output produced by gcc -S):
movl a, %edx
movl b, %eax
movl %edx, %ecx
orl %eax, %ecx
movl c, %edx
movl d, %eax
orl %edx, %eax
andl %ecx, %eax
Pramode C.E FPGA hacking with Free Software Tools
19. A NAND Gate - in real Hardware!
PMOD1 and PMOD2 are pins 1 and 2 on the PMOD connector
of the IceStick board.
module Nand2(input PMOD1, PMOD2,
output LED1,LED2,LED3,LED4,LED5);
// Nand gate o/p is on LED1 only
assign LED1 = ~(PMOD1 PMOD2);
assign LED2 = 0;
assign LED3 = 0;
assign LED4 = 0;
assign LED5 = 0;
endmodule
Pramode C.E FPGA hacking with Free Software Tools
20. WhoAmI?
module WhoAmI(input PMOD1,PMOD2,PMOD3,
output LED1,LED2,LED3,LED4,LED5);
// only LED1 is significant
wire f1,f2,f3,f4;
assign f1 = (~PMOD1) PMOD2 (~PMOD3);
assign f2 = (~PMOD1) PMOD2 PMOD3;
assign f3 = PMOD1 (~PMOD2) PMOD3;
assign f4 = PMOD1 PMOD2 PMOD3;
assign LED1 = f1 | f2 | f3 | f4;
//assign LED2,3,4,5 to 0 ... lines not shown.
endmodule
Pramode C.E FPGA hacking with Free Software Tools
21. WhoAmI?
module WhoAmI(input PMOD1,PMOD2,PMOD3,
output LED1,LED2,LED3,LED4,LED5);
// only LED1 is significant
always @(*)
begin
if(PMOD1 == 0) LED1 = PMOD2;
else LED1 = PMOD3;
end
//assign LED2,3,4,5 to 0 ... lines not shown
endmodule
Pramode C.E FPGA hacking with Free Software Tools
25. More demos!
Rotating LED's
Random bit generation using a Linear Feedback Shift Register
A UART transmitter
A full CPU running a FORTH system!
Pramode C.E FPGA hacking with Free Software Tools