Tutorial FPGA Verilog SP24
Tutorial FPGA Verilog SP24
Introduction to FPGAs
& the Verilog HDL
Spring 2024
Learning Goals
• Understand FPGA’s structure and functionalities
• Verilog basics
– Signal, vectors, constants
– Operators, modules
– Wire, register types
• Verilog coding styles: structural v.s. behavioral
• Continuous assignment
• Procedural assignment
– Blocking v.s. non-blocking assignment
• Understand Verilog testbenches
Gate-level entry
Transistor-level entry
McKinsey S-Curve
Effort
(CAD tool effort)
Less human effort
ENGRD 2300
[source: Keutzer] FPGAs & Verilog Tutorial 4
Hardware Description Languages
• Hardware Description Language(HDL)
– A language for describing hardware designs
• Advantages of HDLs
– Efficiently code large, complex designs
• Program at a more abstract level than schematics
• More readable than schematics
– EDA/CAD tools automatically generate hardware
• Industry standards
– Verilog
– VHDL
– SystemVerilog: a successor to Verilog, gaining
popularity
• Reading: Chapter 4
ENGRD 2300 FPGAs & Verilog Tutorial 5
HDL-based Design Flow: Overview
• HDL code written to describe hardware after
brainstorming over a given problem
• Uses
– Accelerating/prototyping algorithms in hardware
– variety of fields like defense, medical, machine learning
… among others
ENGRD 2300 FPGAs & Verilog Tutorial 11
• Let’s look at a couple of Verilog programs mapped
to FPGAs
7 segments defined ON or
OFF for all 4-bit inputs
4 inputs connected
to switches
• Binary constants
4’b1001 – 8’b00000001
– 4’b0111
• Decimal constants
– 4’d10
Base (b, d, h, o)
– 8’d345
– 32’d65536
Decimal number representing bit width
• Arithmetic operators
+ Addition / Division << Shift left
– Subtraction % Modulus >> Shift right
* Multiplication
• Declarations
– Describe names and types of inputs and outputs
– Describe local signals, variables, constants, etc.
not U1(noti0,i0);
not U2(noti1,i1);
and U3(y0,noti0,noti1,en);
Statements
and U4(y1, i0,noti1,en);
and U5(y2,noti0, i1,en);
and U6(y3, i0, i1,en);
endmodule
Verilog declarations
Hierarchy statements
Module B Module C
A module can
instantiate declarations declarations
statements
• Behavioral More efficient - we’ll mostly use this style in our labs
wire temp_value;
– Assignment:
y out
NOT not0(inv_sel,select);
AND and0 (tx, x, inv_sel);
AND and1 (ty, y, select);
ty
OR or0 (out, tx, ty);
select
endmodule
The order of the gates instantiation does not matter.
Essentially describing the schematic textually.
nand U1( );
….
endmodule
endmodule
• Example:
– assign y0 = en & ~i0 & ~i1;
endmodule
endmodule
endmodule
• Simple alternative
– always @ (*) Used to describe Combinational Logic
– Blocking assignment
•variable name = expression ; More later!
– Nonblocking assignment
•variable name <= expression ;
Y Z
Y Z
• Automation
Verilog Testbench
• Example
// sets the granularity at which we simulate
`timescale 1 ps / 1 ps
module treg4bit_test();
// outputs
wire [3:0] OUT;
always
begin
CLK50 = 1'b0;
CLK50 = #10000 1'b1;
#10000;
end
#10000 means a delay i.e. wait
for 10000 time units before
changing the value of CLK50
initial
begin
// reset circuit
...
// test cases
...
end
// reset circuit
RESET = 1'b1;
IN = 4'b0;
#20000;
// test whether reset is correct
...
$stop;
• Terminate simulation
$finish;