Algosup Fpga Course Day 1 Slides
Algosup Fpga Course Day 1 Slides
ALGOSUP
About Me – Russell Merrick
• Vision Pipeline FPGA Designer, BAE Systems
• High reliability, fast signals, video, on aircraft
• Started Nandland after frustrations as an FPGA
beginner
• Nandland.com
• Created the “Go Board”
• Writer of “Getting Started with FPGAs”
• YouTube channel
FPGA Introduction
• FPGA = Field Programmable Gate Array
• Unique Requirements
• Lots of Data
• Lots of Math
20 BILLION
• Moore’s Law
• # of transistors in an IC doubles every 2 years
TRANSITORS IN A
MODERN
PROCESSOR!
Transistors and Binary
• Transistors store 0, 1 state
• Binary is 0, 1 state
• We will introduce these two components and then learn how they
each work in projects
What is a LUT
• One of the 2 most critical components in your FPGA
Truth Table
Input A Input B Output Q
0 0 0
0 1 0
1 0 0
1 1 1
And Gate
Truth Table
Input A Input B Output Q
0 0 0
0 1 1
1 0 1
1 1 1
Not Gate
Truth Table
Input A Output Q
0 1
1 0
Nor Gate
Truth Table
Input A Input B Output Q
0 0 1
0 1 0
1 0 0
1 1 0
Xor Gate
Truth Table
Input A Input B Output Q
0 0 0
0 1 1
1 0 1
1 1 0
Nand Gate
Truth Table
Input A Input B Output Q
0 0 1
0 1 1
1 0 1
1 1 0
Combining Gates with Boolean Algebra
Create this circuit in logic.ly. Fill in the last column of the truth table
by going through each input combination
Combining Gates with Boolean Algebra
Truth Table – A+(C*B')
Input A Input B Input C Output Q
0 0 0 0
0 0 1 1
0 1 0 0
0 1 1 0
1 0 0 1
1 0 1 1 Create this circuit in logic.ly. Fill in the last
1 1 0 1 column of the truth table
1 1 1 1 by going through each input combination
The Look-Up Table does all Boolean Algebra!
And gates, Or Gates, Nor Gates, etc, DO NOT EXIST ON A REAL FPGA!
IN4
We can now remember past values! LUTs cannot, but Flip-Flops can!
Flip-Flop in Logic.ly
• Create this circuit
• Select D-Flip-Flop
• Select Clock
Monitor Go Board
VGA Cable
Frogger
• You control a frog’s movement on the monitor
• Buttons on Go Board are used to direct frog up, down, left right
F
Project Frogger – Car Control
Cars (C) appear
F
Project Frogger – Level Display
F When Frog reaches the top row, frog
resets to bottom
C
Level counter on 7-segment display
C
increments
C
C
C
C
Time check for lab component
Programming Language on FPGA
• Type of Hardware Description Language (HDL)
• We are literally designing wires, gates, etc, “describing hardware”
• Most similar to C
Don’t
• In general, if you’re forget
stuck, to thank
ask ChatGPT likethe AIasking
you’re a teacher
Lab Component
When Should/Shouldn’t you use FPGA
• Often need “smarts” in electronics
• Toys, Consumer Electronics, Industrial Robotics, etc, all needs it.
• Two possible options:
• FPGAs
• Microcontrollers
• (also ASICs for very high volume)
FPGA Basics
• Basic building blocks
• Look-Up Table (LUT)
• Flip-Flop/Register
• Often no CPU
Microcontroller Basics
• Has a processor (CPU)
• Often written in C
• Open this file and look at it, can manually edit if needed.
How do we know what is where?
• Go Board Schematic has this
information
## Push-Button Switches
set_io i_Switch_1 53
set_io i_Switch_2 51
Project – LUT
LUT, as And Gate
Truth Table
Input A Input B Output Q
0 0 0
0 1 0
1 0 0
1 1 1
Let’s describe some hardware
module project_gates
(input i_Switch_1, Save your file “project_gates.v”
input i_Switch_2,
output o_LED_1);
module / endmodule
assign o_LED_1 = i_Switch_1 & i_Switch_2;
Inputs first, outputs second
endmodule
1 I/O ports: 3
I/O primitives: 3
SB_IO 3 uses
Mapping Summary:
2 Total LUTs: 1 (0%)
Project: Implement Gates (~15 min)
• Add two new inputs (i_Switch_3 and i_Switch_4)
• Add one new output (o_LED_2)
• Make sure to add these to your constraint file too!
• i_Switch_3 = Pin 54
• i_Switch_4 = Pin 52
• o_LED_2 = Pin 57
• Notice that both AND gate and XOR gate are running in parallel and the
output is evaluated immediately.
Verilog Code – Project Solution
module project_gates
• Notice that these two assigns
(input i_Switch_1, will execute in parallel (at the
input i_Switch_2,
input i_Switch_3,
same time)
input i_Switch_4,
output o_LED_1,
output o_LED_2);
• They’re always “running” (and
assign o_LED_1 = i_Switch_1 & i_Switch_2; there’s no delay from input to
assign o_LED_2 = i_Switch_3 ^ i_Switch_4;
output)
endmodule
SW1 SW2 SW3 SW4 LED1 LED2 LED3
Project – Solution 0
0
0
0
0
0
0
1
1
1
0
0
1
1
0 0 1 0 0 0 0
0 0 1 1 0 0 0
0 1 0 0 0 0 0
0 1 0 1 1 1 1
0 1 1 0 0 0 0
0 1 1 1 1 1 0
1 0 0 0 1 0 1
1 0 0 1 1 0 1
1 0 1 0 1 1 1
1 0 1 1 1 1 1
1 1 0 0 0 0 0
1 1 0 1 1 1 1
1 1 1 0 1 1 0
1 1 1 1 1 1 1
Project - Code
module project_boolean_algebra
(input i_Switch_1,
input i_Switch_2,
input i_Switch_3,
input i_Switch_4,
output o_LED_1,
output o_LED_2,
output o_LED_3);
endmodule
module project_boolean_algebra_wires
Ternary Operator: ?
Demultiplexer (Demux)
• Opposite of mux
• Signal mapping
• Explicit connections between
module signals (left) and high level
signals (right)
Project – And Gates using Module
Instantiation
• Create a module called and_gate
• Two inputs, one output
• Performs AND operation
endmodule
Summary
• All projects have been working with LUTs only
• // is a comment in Verilog
Creating our first Flip-Flop
reg r_Switch_1;
i_Switch_1 r_Switch_1
always @(posedge i_Clock)
1 begin
r_Switch_1 <= i_Switch_1;
i_Clock end
If/else statements
always @(posedge i_Clock)
• We can use if statements within begin
always blocks
if (Value > Check)
begin
// Do something
• Allows us to check equality, end
compare numbers, check if a
else
pulse is high, etc. begin
// Do something else
end
• Use begin/end like {} end
Reg vs. Wire
• reg is reserved keyword in Verilog
• Short for “register” which is another word for Flip-Flop
CLK
Falling Edge
SW1 Detection
r_Switch_1
Block Diagram
• Suggest always block diagram
reg r_LED_1;
reg r_Switch_1;
• Use “<=” assignment for flip-
flops
always @(posedge i_Clk)
begin
// 1. Create r_Switch_1 flip-flop
• Inversion achieved with “~”
// 2. Look for falling edge condition (NOT gate)
(switch is released)
Falling Edge
assign o_LED_1 = r_LED_1; SW1 Detection
endmodule
r_Switch_1
Resource + Clock Report
• Resource Usage Report for LED_Toggle_Project
•
• Mapping to part: ice40hx1kvq100
• Cell usage:
• SB_DFF 2 uses
• SB_LUT4 1 use
•
1::Clock Frequency Summary
• I/O ports: 3
==========================================================
• I/O primitives: 3
Number of clocks: 1
• SB_GB_IO 1 use
Clock: i_Clk | Frequency: 654.05 MHz | Target: 25.00 MHz |
• SB_IO 2 uses
•
• I/O Register bits: 0
• 1 Register bits not including I/Os: 2 (0%)
• Total load per clock:
• 2 LED_Toggle_Project|i_Clk: 1
• Mapping Summary:
• 3 Total LUTs: 1 (0%)
Project 2 - Discussion
• First use of clock, needed for Flip-Flop!
• Flip-Flops store state (past/memory)
• Did this line look weird?
• r_LED_1 <= ~r_LED_1;
• Will be updated on NEXT clock cycle
• Challenge question: Why does the LED sometimes toggle, but not
always?
Combinational vs. Sequential Logic
• Combinational Logic
• Logic for which the outputs are determined from present inputs, with no
memory of the previous state
• Also called Combinational
• Sequential Logic
• Logic for which the outputs are determined from both present inputs and
previous outputs
• Also called Synchronous
• Flip flops are sequential, because they depend on the previous output
Combinational Always Blocks
• Recommend avoiding
• Every time you see an always block, you know it’s going to generate
sequential logic.
• In Simulation
• All signals become visible
• Makes the black box a white box
• Built-in Simulators
• Vivado
• Quartus
• Free Simulator
• EDA Playground
https://edaplayground.com
Writing Simple Testbench
module And_Gate_Project initial
(input i_Switch_1, begin
input i_Switch_2, $dumpfile("dump.vcd"); $dumpvars;
output o_LED_1); r_In1 <= 1'b0;
r_In2 <= 1'b0;
assign o_LED_1 = i_Switch_1 & i_Switch_2; #10;
r_In1 <= 1'b0;
endmodule r_In2 <= 1'b1;
#10;
module And_Gate_TB(); r_In1 <= 1'b1;
r_In2 <= 1'b0;
reg r_In1, r_In2; #10;
wire w_Out; r_In1 <= 1'b1;
r_In2 <= 1'b1;
And_Gate_Project UUT #10;
(.i_Switch_1(r_In1), $finish();
.i_Switch_2(r_In2), end
.o_LED_1(w_Out)); endmodule
Waveforms
• Can show all signals with your design
• Require visual inspection to show code is functioning as expected
• Great for debugging, but can be tedious in large designs
How to improve this testbench?
• Every time we run the simulation, we need to look at the waveforms
• Tedious, time-consuming, can be prone to error
• If you change/add code, how to you ensure you didn’t break old
stuff? (big issue!)
Self-Checking Testbenches
• Add “asserts” into your testbenches
• E.g. assert (expected_value == actual_value)
• Produces PASS/FAIL results, no waveform viewing required
• Can run multiple simulations quickly
• Can perform regression testing
• If you modify existing code, you can be confident you didn’t break
some old code
SystemVerilog
• Superset of Verilog (contains everything in Verilog, and much more)
assert (statement_to_check);