Red Pitaya - FPGA Ebook Part 2 - Basics of Verilog FPGA Programming Language
Red Pitaya - FPGA Ebook Part 2 - Basics of Verilog FPGA Programming Language
programming language
FPGA e-book
part 2
1
CONTENT
03 8. Basics of Verilog FPGA programming language - FPGA and Red Pitaya
2
8. FPGA and Red Pitaya
In a project of a digital system the developer studies a problem and builds a combination of logic gates, registers,
flip-flops, and more complex blocks like RAM, ROM, processors, etc. Today with the advance of the technology
we can implement the project with a FPGA (Field Programmable Gate Array). The FPGA is made with lots of
logic blocks and each block contains logic gates, multiplexers, registers, etc. To implement the project the
user of the FPGA has to make connections between these logic blocks and this is done with a HDL (Hardware
Description Language).
Red Pitaya uses Verilog and System Verilog as a HDL. The Red Pitaya board has a programmable logic made
by Xilinx and to write it to describe your digital system you must use the software Vivado. Vivado is used to
write your digital system with a HDL and to implement your system in the programmable logic. The result of the
implementation of a Vivado project is a file called bitstream that has an extension .bit, that has the information
about the connections of logic blocks that will be used and the connections between them.
Every application of Red Pitaya (Osciloscope, Signal Generator, etc) uses a specific bitstream file, that implements
the digital system needed for the application.
3
9. Basics of Verilog FPGA programming language
9.1. Introduction
Verilog is a HDL, it is very similar to the C programming language and it was developed in 1985. Verilog became
popular because it is easy to learn if you have some programming experience with C. With Verilog you can
describe the system in a sequential or in a combinational way and the most popular tools that are used to
develop digital systems support Verilog.
In Verilog the system is described with inputs, outputs and modules that implement some logic function. The
system designer will implement modules and the software will optimize and implement the system to use less
logic cells as possible, in Red Pitaya case the software used is Vivado. Modules can be implemented inside
other modules, a module output can be another module input. A module is declared using the words module
and endmodule, the function, the inputs and outputs are declared inside it. Some characteristics of Verilog are:
• It is case sensitive, for example an input named adc_in is different from an input named Adc_in.
• Line comments are made with // code, and block comments with /* code block */.
module projtest(
input [17:0] in_a,
input [17:0] in_b,
output [35:0] out_f
);
// design_1 instanciation1( .input_a(in_a), .input_b(in_b),
.clk(clock), .outp(out_f) );
assign out_f = in_a * in_b;
endmodule
4
10. Values
10.1. Port Types
The port types can be, input, output and inout.
10.2.1 Net
Net declarations can be wire, tri, supply0 and supply1.
10.2.2 Reg
Reg declarations can be reg, integer, time and realtime. A variable of type reg can be designated only by
a procedure statement, task, or function. A variable of type reg cannot be the output of a port or an assign
statement.
Examples:
122 - Unsized number with no base, so it is a 32 bit wide number with the value 122 in the decimal base.
3’b010 - 3 bit wide with the value 010 in base two or 2 in decimal base.
Example:
10.7. References
Verilog HDL Basics - Altera
6
11. Operators
11.1. Arithmetic Operators
For the FPGA, division and multiplication are very expensive and sometimes you cannot synthesize division.
If you use Z or X for values the result is unknown. The operations treat the values as unsigned. If a = 5, b=10,
c=2’b01 and d = 2’b0Z .
+ Add b + c = 11
- Subtract b - c = 9, -b=-10
/ Divide b/a=2
* Multiply a * b = 50
% Modulus b%a=0
7
11.4. Relational Operators
These operators compare operands and results a 1 bit scalar boolean value. The case equality and inequality
operators can be used for unknown or high impedance values (z or x) and if the two operands are unknown the
result is a 1. If a = 3’b010, b = 3’b100, c = 3’b111, d = 3’b01z and e = 3’b01X .
== Equality a == b = 1’b0
!= Inequality a != b = 1’b1
8
11.7. Other Operators
These are operators used for condition testing and to create vectors. If a = 4’b1010 and b = 4’b10X0.
Operators precedence
+, -, !, ~ (Unary)
+,- (Binary)
<<, >>
<,>,<=,>=
==, !=
&
^, ^~ or ~^
&&
||
?:
11.9. References
Verilog HDL Basics - Altera
9
12. Assignments
12.1. Continuous Assignment Statements
In Verilog the statements that are outside an always or initial block are called continuous. For example in the
next code the net out changes when a or b changes:
wire out;
assign out = a + b;
When the right-hand side (RHS) changes the left-hand side updates immediately. RHS can be a net, reg, or
functions calls and LHS must be a net.
The always block executes when its pre-defined funtions, or its inputs changes. The pre-defined functions
are posedge and negedge, and they are used with clock variables to detect when it rises to 1 or decays to 0.
Example:
10
12.3.1. Blocking
12.3.2. Non-Blocking
12.3. References
Verilog HDL Basics - Altera
11
13.2. Case Statements
Case the variable or expression is equal some value inside the case block the statement where it is true is
executed.
reg a;
case (a)
1'b0 : statement1;
1'b1 : statement2;
1'bx : statement3;
1'bz : statement4;
endcase
There are also the forms of the case block that are casez and casex. The casez treats the values as don’t cares
and all ‘z’ is represented by ‘?’ . The casex is the same but treats ‘x’ and ‘z’ as don’t cares. The default option is
used when any other condition is met.
13.3.1. Forever
initial
begin
forever
#25 clock = ~clock; //executes forever and at 25 periods of time clock changes state
end
13.3.2. Repeat
if(rotate == 1)
repeat(8) // if rotates = 1 rotates data 8 times
begin
tmp = data[15];
data = data << 1 + tmp;
end
12
13.4. While Loop
Executes the block if the condition is true, repeats the test and executes again until the condition is not met.
always@(a or b) begin
end
always@(a or b) begin
for (i = 0; i < 16; i = i +1) begin
a <= a + 1; // executes this code 16 times
end
end
13.6. References
Verilog HDL Basics - Altera
14.1. Task
Tasks are subroutines that can be called anytime in the module they are defined, but it is possible to define them
in a different file and include the file in the module. Some charachteristics of tasks are:
• Tasks can have any number of inputs and outputs, functions can have only one output.
• If a variable is declared within the task it is local to the task and can’t be used outside the task.
• Tasks are called with statements and cannot be used in a expression, functions can be used in a expression.
13
Task example:
task convert;
input [7:0] adc_in;
output [7:0] out;
begin
out = (9/5) *( adc_in + 32)
end
endtask
always @ (adc_a)
begin
convert (adc_a, adc_a_conv);
end
always @ (adc_b)
begin
convert (adc_b, adc_b_conv);
end
endmodule
14.2. Functions
Functions are like tasks, with some differences. Functions cannot drive more than one output and cannot have
time delays. Some differences are:
• Functions cannot include timing delays, like posedge, negedge, simulation delay, which means that functions
implement combitional logic.
• Functions can have any number of inputs but only one output.
• The variables declared within the function are local to that function.
• The order of declaration within the function are considered and have to be the same as the caller.
• Functions can use and modify global variables, when no local variables are used.
• Functions can call other functions, but cannot call tasks. Function example:
14
module function_calling(a, b, c, d, e, f);
input a, b, c, d, e ;
output f;
wire f;
function myfunction;
input a, b, c, d;
begin
myfunction = ((a+b) + (c-d));
end
endfunction
endmodule
14.3. References
Verilog HDL Basics - Altera, ASIC World (http://www.asic-world.com/)
15
CONTACT
DETAILS:
[email protected]
www.redpitaya.com
“In 2013, a group of engineers with 20 years of electronics design experience in hardware for par-
ticle accelerators got together and created the first Red Pitaya board. Red Pitaya was created with
one important mission – to transform any home office or classroom into a professional engineering
lab at a low price point.
We are proud to say that with Red Pitaya you can replace many expensive lab instruments and have
excellent price performance. In the past seven years, we have worked with more than 30,000 satis-
fied customers.” - Red Pitaya Team.
16