First FPGA Program in Verilog.

In one of my previous articles I described how to flash an FPGA with existing firmware; in this one we'll try to write the firmware ourselves. Let's start simple: blink an LED. To write the program we'll use Verilog, a language similar to C.

First, let's create a project.
First FPGA Program in Verilog.

Specify the folder where the project will be stored, the project name and the name of the top-level module.
First FPGA Program in Verilog.

Specify that the project will be empty.
First FPGA Program in Verilog.

This step is for adding files; we skip it.
First FPGA Program in Verilog.

Select the chip we are going to program.
First FPGA Program in Verilog.

This step offers to choose debugging tools for the project; we skip it.
First FPGA Program in Verilog.

A checklist for verifying the project settings. If necessary, any of the settings can be changed by going back a few steps using the Back button.
First FPGA Program in Verilog.


I think many of you are wondering what a top-level module is.
The inputs and outputs of the top-level module are connected directly to the chip pins, and the top-level module also contains all the other modules.
Since our project is simple, there will be only one module, and it will also be the top-level module.

Once the project is created, let's open the development board schematic and see which pin the LED is connected to and which pin receives the clock from the oscillator.
First FPGA Program in Verilog.

First FPGA Program in Verilog.


Now we can move on to writing the program itself. Create a new Verilog HDL File via File->New.
First FPGA Program in Verilog.

And write the following code, which toggles the LEDs twice per second.

module first_project(input clk, output reg red_led, output reg green_led);

reg[24:0] counter;

   always@(posedge clk)
	begin 
		counter<=counter+1;
		if(counter == 25'd25000000)
		begin
			green_led <= green_led^1'b1;
			red_led <= red_led^1'b1;
			counter <= 0;
		end
	end

endmodule

Next, we synthesize the project.
First FPGA Program in Verilog.

First FPGA Program in Verilog.

Now we need to assign real chip pins to the signals in the Pin Planner tab.
First FPGA Program in Verilog.

First FPGA Program in Verilog.

In the location column I specified which pins correspond to which signals. I'd like to note that you can't assign just any chip pin to a signal, because the pins are divided into 5 groups, each with its own purpose; they are marked with a red square in the screenshot. The first group is the usual input/output pins, the second group receives the reference voltages for each bank separately, the third and fifth are the PLL input and output, and the clock signal is fed to the pins of the fourth group.

Now we need to run the compilation.
First FPGA Program in Verilog.


Compilation produces a .sof file that can be loaded into the FPGA.
First FPGA Program in Verilog.

First FPGA Program in Verilog.

First FPGA Program in Verilog.

First FPGA Program in Verilog.

First FPGA Program in Verilog.

First FPGA Program in Verilog.