You are on page 1of 82

FPGA System Design with

Verilog
A Workshop Prepared for
Rose-Hulman Ventures
Ed Doering
Workshop Goals
 Gain familiarity with FPGA devices
 Gain familiarity with HDL design methods
 Implement basic designs in hardware

Aug 9, 2001 FPGA System Design with Verilo 2


g
Agenda
FPGA Overview 8:30 - 9:15
Verilog Overview 9:15 - 10:00
Combinational Circuits 10:15 - 11:00
Lab Projects I 11:00 - 12:00
Sequential Circuits 1:15 - 2:00
Lab Projects II 2:00 - 3:00
Lab Projects III 3:15 - 4:00
Aug 9, 2001 FPGA System Design with Verilo 3
g
FPGA Overview

Aug 9, 2001 FPGA System Design with Verilo 4


g
What is an FPGA?
 Field Programmable Gate Array
 Blank slate for your digital hardware system

Aug 9, 2001 FPGA System Design with Verilo 5


g
FPGA in Context
 Microprocessor/microcontroller
 Executes a program

 Fixed hardware and interconnections

 Full-custom IC
 Design at the transistor level

Aug 9, 2001 FPGA System Design with Verilo 6


g
FPGA in Context
 Semicustom IC
 Standard cell (CBIC, ASIC)

 Masked gate array (MGA)

 Programmable logic device (PLD)

 PLD
 Complex PLD (CPLD)

 FPGA

Aug 9, 2001 FPGA System Design with Verilo 7


g
When to Use an FPGA
 Design economics
 Shortest time to market

 Lowest NRE cost

 Highest unit cost

 Make quick grab for market share, then do


cost reduction with ASICs

Aug 9, 2001 FPGA System Design with Verilo 8


g
FPGA Pictures
 Board
 Packages
 Wafer
 Die photos
 FPGA

 Pentium II microprocessor

Sources: http://www.xilinx.com/company/press/products/pictures2.htm,
http://micro.magnet.fsu.edu/chipshots/pentium/

Aug 9, 2001 FPGA System Design with Verilo 9


g
Internal Architecture
 Array of Configurable Logic Blocks (CLBs)
 User-defined (SRAM-based) interconnect
between CLBs
 Dedicated resources
 Power distribution

 Clock distribution

 Programmable I/O blocks (IOBs)

Aug 9, 2001 FPGA System Design with Verilo 10


g
Configurable Logic Block

Source: Smith, M.J.S., Application-Specific Integrated Circuits,Addison-Wesley, 1997.


Aug 9, 2001 FPGA System Design with Verilo 11
g
Programmable I/O Block

Source: Smith, M.J.S., Application-Specific Integrated Circuits, ddison-Wesley, 1997.

Aug 9, 2001 FPGA System Design with Verilo 12


g
PLD Vendors
Total 1999 PLD Market = $2.6B

Other
9%
Actel
7% Xilinx
35%
Lattice
16%

Altera
33%
Source: Xilinx University Program Workshop Notes

Aug 9, 2001 FPGA System Design with Verilo 13


g
Xilinx FPGA Product Families
 Virtex-II (“Platform FPGA, ” 10M gates)
 Virtex (1M gates), Virtex-E (3M gates)
 Spartan (low cost ASIC replacement)
 XC4000 (first FPGA family, now with
enhancements)

Aug 9, 2001 FPGA System Design with Verilo 14


g
Altera FPGA Product Families
 APEX-II (up to 7M gates)
 APEX20K (up to 1.5M gates)
 Mercury (ASIC replacement, “ASSP”)
 FLEX 10K

Aug 9, 2001 FPGA System Design with Verilo 15


g
Hybrid FPGA / Microcontroller
 Triscend E5 CSoC (configurable system-
on-chip)
 8-bit 8051-based microcontroller

 40K system gates

 Triscend A7 CSoC
 32-bit ARM7TDMI processor

 40K system gates

Aug 9, 2001 FPGA System Design with Verilo 16


g
Hybrid FPGA / Microcontroller
 Atmel FPSLIC (Field Programmable
System-Level Integrated Circuit)
 5K to 40K system FPGA gates

 8-bit AVR RISC microprocessor core

 Microcontroller peripherals

 36K program and data RAM

Aug 9, 2001 FPGA System Design with Verilo 17


g
Choosing CPLD or FPGA
 CPLD
 Nonvolatile (ROM- or EEPROM-based)

 Predictable delays (no routing)

 “Register poor” (relatively few FFs)

 Low-to-medium density

 For simple, fast logic with many inputs

 Specialized decoders, combinational circuits,

counters
Aug 9, 2001 FPGA System Design with Verilo 18
g
Choosing CPLD or FPGA
 FPGA
 Volatile (SRAM-based)

 Configuration must be stored externally (serial

EEPROM)
 Permits field upgrades, reconfigurable computing

 Variable routing delays

 “Register rich” (relatively many FFs)

 For arbitrary digital systems, system-on-chip (SoC),

medium-to-high density

Aug 9, 2001 FPGA System Design with Verilo 19


g
Verilog Overview

Aug 9, 2001 FPGA System Design with Verilo 20


g
Hardware Description Languages
 Verilog  VHDL
 Gateway Design  Origins in DoD
Automation (1983; VHSIC program
proprietary) (1980’s)
 Acquired by
 IEEE standard in
Cadence 1989
 IEEE standard in
1987
 Similar to ADA
1995
 Similar to C

Aug 9, 2001 FPGA System Design with Verilo 21


g
Verilog vs. VHDL
 Verilog is less “wordy”
 Industry is about 50%-50%
 VHDL more common in adademe
 I think Verilog is easier to learn

Aug 9, 2001 FPGA System Design with Verilo 22


g
What is “HDL”?
 HDL = Hardware Description Language
 A text-based method for describing
hardware to a synthesis tool

Aug 9, 2001 FPGA System Design with Verilo 23


g
HDL Advantages Over
Schematic Entry
 Produce correct designs in less time
 Produce larger and more complex systems
per unit time
 Shifts focus to specifying functionality
 Synthesis tools automate details of
connecting gates and devices

Aug 9, 2001 FPGA System Design with Verilo 24


g
Design Flow Comparison
Schematic Text

Netlist HDL Sim

Gate-Level Hardware
Simulation Synthesis

Implement Netlist

Implement
Aug 9, 2001 FPGA System Design with Verilo 25
g
Key Advantages of HDL-Based
Design Methodology
 Operate at higher level of abstraction
 Can debug earlier (behavioral simulator)
 Parameterized design, easy to make
wholesale modifications to a design (e.g.,
bus width)

Aug 9, 2001 FPGA System Design with Verilo 26


g
Key Advantages of HDL-Based
Design Methodology
 Can quickly specify desired behavior
 Example: Up-counter with reset

if (reset == 1)
count <= 0;
else
count <= count + 1;

Aug 9, 2001 FPGA System Design with Verilo 27


g
Key Advantages of HDL-Based
Design Methodology
 Can easily target multiple devices (eases
product migration)

HDL

FPGA ASIC

Aug 9, 2001 FPGA System Design with Verilo 28


g
Key Advantages of HDL-Based
Design Methodology
 HDL is more universal than schematic tools
 Promotes design reuse
 Promotes integration of third party designs,
or IP (intellectual property)

Aug 9, 2001 FPGA System Design with Verilo 29


g
What HDL is NOT
 HDL is not a programming language
(HDL is a description language)

 HDL is not highly abstract, e.g., implement


the DSP algorithm y(n) = 0.75y(n-1) +
0.3x(n)
(HDL is at the RTL level (register transfer))

Aug 9, 2001 FPGA System Design with Verilo 30


g
Synthesizable Subset
 Verilog (and VHDL) began life as
simulation and modeling tools
 Hardware synthesis developed during the
1990’s
 Need to use a subset of Verilog and specific
coding styles to allow synthesis tool to infer
correct (and realizable) hardware

Aug 9, 2001 FPGA System Design with Verilo 31


g
Synthesizable Subset
Use this to write
testbenches for
behavioral simulation

Verilog

Synthesizable
Verilog

Use this to
make hardware
in FPGA
Aug 9, 2001 FPGA System Design with Verilo 32
g
Most Likely Learning Hurdle
 May try to write HDL code as if it will
eventually be “executed” by some
mysterious processor device in the FPGA
 Code is written sequentially (like a
program), but you are simply writing
descriptions of the various hardware entities
in your system

Aug 9, 2001 FPGA System Design with Verilo 33


g
Verilog:
Combinational
Circuits
Aug 9, 2001 FPGA System Design with Verilo 34
g
A Gradual Introduction
 New concepts in boldface
 Verilog keywords in italic
 Refer to your handout...

Aug 9, 2001 FPGA System Design with Verilo 35


g
“Do Nothing” Circuit
module Gadget;
endmodule

Aug 9, 2001 FPGA System Design with Verilo 36


g
“Do Nothing” with I/O
module Gadget (a,b,c);
// Port modes
input a,b;
output c;
endmodule

Aug 9, 2001 FPGA System Design with Verilo 37


g
NAND Gate: Continuous
Assignment
module Gadget (a,b,c);

/* Port modes */
input a,b;
output c;

// Functionality
assign c = ~(a & b);
endmodule
Aug 9, 2001 FPGA System Design with Verilo 38
g
Bitwise Operators
~ NOT
& AND
| OR
^ EXOR

Aug 9, 2001 FPGA System Design with Verilo 39


g
NAND Gate: Procedural
Assignment
module Gadget (a,b,c);

// Port modes
input a,b;
output c;

// Registered identifiers
reg c;

// Functionality
always @ (a or b)
c <= ~(a & b);
endmodule

Aug 9, 2001 FPGA System Design with Verilo 40


g
Two Gates
module Gadget (a,b,c,d);

// Port modes
input a,b;
output c;
output d;

// Registered identifiers
reg c,d;

// Functionality
always @ (a or b) begin
c <= ~(a & b);
d <= a ^ b;
end
endmodule

Aug 9, 2001 FPGA System Design with Verilo 41


g
Two-Input MUX
module Mux2 (
A, // A input
B, // B input
Sel, // Selector
Y // Output
);

// Port modes
input A,B,Sel;
output Y;

// Registered identifiers
reg Y;

// Functionality
always @ (A or B or Sel)
if (Sel==0)
Y <= A;
else
Y <= B;
endmodule

Aug 9, 2001 FPGA System Design with Verilo 42


g
Relational Operators

== Equal to
!= Not equal
< Less than
> Greater than
<= Less than or equal
>= Greater than or equal
&& AND
|| OR
Aug 9, 2001 FPGA System Design with Verilo 43
g
More Operators

>> Shift right


<< Shift left
+ Add
- Subtract
* Multiply
/ Divide Not likely to synthesize!

% Modulus
Aug 9, 2001 FPGA System Design with Verilo 44
g
MUX Again...
// Functionality
always @ (A or B or Sel)
if (Sel)
Y <= B;
else
Y <= A;

Aug 9, 2001 FPGA System Design with Verilo 45


g
... and Again!
// Functionality
always @ (A or B or Sel)
Y <= (Sel) ? B : A;

Aug 9, 2001 FPGA System Design with Verilo 46


g
4-Input MUX
module Mux4 (
Data, // Data input
Sel, // Selector
Y // Output
);

// Port modes
input [3:0] Data;
input [1:0] Sel;
output Y;

// Registered identifiers
reg Y;

// Functionality
always @ (Data or Sel)
if (Sel == 0)
Y <= Data[0];
else if (Sel == 1)
Y <= Data[1];
else if (Sel == 2)
Y <= Data[2];
else Y <= Data[3];

endmodule

Aug 9, 2001 FPGA System Design with Verilo 47


g
4-Input MUX Using ‘case’
// Functionality
always @ (Data or Sel)
case (Sel)
0: Y <= Data[0];
1: Y <= Data[1];
2: Y <= Data[2];
3: Y <= Data[3];
default: Y <= Data[0];
endcase
Aug 9, 2001 FPGA System Design with Verilo 48
g
Custom MUX
module Mux16 (
Data, // Data input
Sel, // Selector
Y // Output
);

// Port modes
input [15:0] Data;
input [3:0] Sel;
output Y;

// Registered identifiers
reg Y;

// Functionality
always @ (Data or Sel)
casez (Sel)
4’b0000: Y <= Data[0];
4’b0001: Y <= Data[1];
4’b01??: Y <= Data[2];
default: Y <= Data[3];
endcase

endmodule

Aug 9, 2001 FPGA System Design with Verilo 49


g
Code Translator (Truth Table)
module Code_Translator (
Code_In,
Code_Out,
);

// Port modes
input [2:0] Code_In;
output [2:0] Code_Out;

// Registered identifiers
reg [2:0] Code_Out;

// Functionality
always @ (Code_In)
case (Code_In)
3’b000: Code_Out <= 3’b101;
3’b001: Code_Out <= 3’b111;
3’b010: Code_Out <= 3’b001;
3’b011: Code_Out <= 3’b000;
3’b100: Code_Out <= 3’b100;
3’b101: Code_Out <= 3’b010;
3’b110: Code_Out <= 3’b110;
3’b111: Code_Out <= 3’b011;
endcase

endmodule

Aug 9, 2001 FPGA System Design with Verilo 50


g
Miscellaneous Techniques
{a,b}

{Data[13:12],a,b,Data[11:0]}

{16{a}}

assign Y = (en) ? X : 1’bz;

Aug 9, 2001 FPGA System Design with Verilo 51


g
4-Bit Magnitude Comparator
module Compare4 (
A, // Data input A
B, // Data input B
AltB, // A is less than B
AeqB, // A is equal to B
AgtB // A is > than B
);

// Port modes
input [3:0] A,B;
output AltB,AeqB,AgtB;

// Registered identifiers
reg AltB,AeqB,AgtB;

// Functionality
always @ (A or B) begin
AltB <= (A < B);
AeqB <= (A == B);
AgtB <= (A > B);
end
endmodule

Aug 9, 2001 FPGA System Design with Verilo 52


g
Parameterized Design
module Compare4 (
A, // Data input A
B, // Data input B
AltB, // A is less than B
AeqB, // A is equal to B
AgtB // A is > than B
);

// Define bus width


parameter BusWidth = 8;

// Port modes
input [BusWidth-1:0] A,B;
...
...

Aug 9, 2001 FPGA System Design with Verilo 53


g
Parameterized Design (Using
Compiler Directive)
`define BusWidth = 8;

Aug 9, 2001 FPGA System Design with Verilo 54


g
Lab Projects I

Aug 9, 2001 FPGA System Design with Verilo 55


g
Simulating Your Design
 See beginning of From Concept to Working
FPGA Chip: Step-by-Step Instructions
 Demo:
 Verilog Boilerplate Generator

 Silos 2001 Behavioral Simulator

Aug 9, 2001 FPGA System Design with Verilo 56


g
Combinational Design Projects
 Pick several of the following designs:
 3-to-8 decoder with output enable

 8-to-3 priority encoder

 16-bit three-input adder

 10-bit barrel shifter

 Enter your design and verify in Silos

Aug 9, 2001 FPGA System Design with Verilo 57


g
Verilog:
Sequential
Circuits
Aug 9, 2001 FPGA System Design with Verilo 58
g
A Gradual Introduction
 New concepts in boldface
 Verilog keywords in italics
 Refer to your handout...

Aug 9, 2001 FPGA System Design with Verilo 59


g
D Flip-Flop
module D_FF (D,Clock,Q);

/* Port modes */
input D,Clock;
output Q;

// Registered identifiers
reg Q;

// Functionality
always @ (posedge Clock)
Q <= D;

endmodule

Aug 9, 2001 FPGA System Design with Verilo 60


g
T Flip-Flop, Falling Edge
module T_FF (T,Clock,Q);

/* Port modes */
input T,Clock;
output Q,;

// Registered identifiers
reg Q;

// Functionality
always @ (negedge Clock)
if (T == 1)
Q <= ~Q;

endmodule

Aug 9, 2001 FPGA System Design with Verilo 61


g
T Flip-Flop, Dual Outputs
module T_FF (T,Clock,Q,_Q);

/* Port modes */
input T,Clock;
output Q,_Q;

// Registered identifiers
reg Q;

// Functionality
always @ (negedge Clock)
if (T == 1)
Q <= ~Q;

assign _Q = ~Q;

endmodule

Aug 9, 2001 FPGA System Design with Verilo 62


g
Flip-Flop with Async. Reset
module D_FF (D,Clock,Q,_Q,Reset);

/* Port modes */
input D,Clock,Reset;
output Q,_Q;

// Registered identifiers
reg Q;

// Functionality
always @ (negedge Clock or
posedge Reset)
if (Reset == 1)
Q <= 0;
else
Q <= D;

assign _Q = ~Q;

endmodule

Aug 9, 2001 FPGA System Design with Verilo 63


g
Flip-Flop with Async. Preset
module D_FF (D,Clock,Q,_Preset);

/* Port modes */
input D,Clock,_Preset;
output Q;

// Registered identifiers
reg Q;

// Functionality
always @ (posedge Clock or
negedge _Preset)
if (_Preset == 0)
Q <= 1;
else
Q <= D;

endmodule

Aug 9, 2001 FPGA System Design with Verilo 64


g
Flip-Flop, Synchronous Reset
module D_FF (D,Clock,Q,Reset);

/* Port modes */
input D,Clock,Reset;
output Q;

// Registered identifiers
reg Q;

// Functionality
always @ (posedge Clock)
Q <= (Reset)? 0 : D;

endmodule

Aug 9, 2001 FPGA System Design with Verilo 65


g
A Brief Sermon...
NOTE: Avoid the temptation to design arbitrary flip-flop
behavior, e.g., ability to trigger on both edges of the
clock, ability to trigger on multiple clock signals, etc.
The hardware synthesis tool does not “magically”
create new hardware from thin air! You have to write
circuit descriptions that are realizable, that is, can be
mapped onto existing (known) hardware elements
such as standard D flip-flops.
Bottom line: Use the constructs listed above exactly as
shown... don’t invent your own!!

Aug 9, 2001 FPGA System Design with Verilo 66


g
Data Register
module Reg16 (D,Clock,Q,Reset);

/* Port modes */
input [15:0] D;
input Clock,Reset;
output [15:0] Q;

// Registered identifiers
reg [15:0] Q;

// Functionality
always @ (posedge Clock or
posedge Reset)
if (Reset == 1)
Q <= 0;
else
Q <= D;

endmodule

Aug 9, 2001 FPGA System Design with Verilo 67


g
Up Counter with Async. Reset
module CountUp (Clock,Reset,Q);

// Port modes
input Clock,Reset;
output [7:0] Q;

// Registered identifiers
reg [7:0] Q;

// Functionality
always @ (posedge Clock or
posedge Reset)
if (Reset == 1)
Q <= 0;
else
Q <= Q + 1;

endmodule

Aug 9, 2001 FPGA System Design with Verilo 68


g
Up Counter with Enable
// Functionality
always @ (posedge Clock or
posedge Reset)
if (Reset == 1)
Q <= 0;
else
Q <= (Enable) ? Q + 1 : Q;

Aug 9, 2001 FPGA System Design with Verilo 69


g
Down Counter
module CountDown (Clock,Reset,Enable,Init,Q);

// Port modes
input Clock,Reset,Enable,Init;
output [7:0] Q;

// Registered identifiers
reg [7:0] Q;

// Functionality
always @ (posedge Clock or
posedge Reset)
if (Reset == 1)
Q <= 0;
else
if (Init)
Q <= 8’hFF;
else if (Enable)
Q <= Q – 1;

endmodule

Aug 9, 2001 FPGA System Design with Verilo 70


g
Loadable Shift Register
module Shifter (Clock,Reset,Load,D,Q);

// Port modes
input Clock,Reset,Load;
input [7:0] D;
output [7:0] Q;

// Registered identifiers
reg [7:0] Q;

// Functionality
always @ (posedge Clock or
posedge Reset)
if (Reset == 1)
Q <= 0;
else
Q <= (Load) ? D :
{1’b0,Q[7:1]};

endmodule

Aug 9, 2001 FPGA System Design with Verilo 71


g
Clock Divider
parameter MaxCount = 12;
 
always @ (posedge Clock or posedge Reset)
if (Reset) begin
ClockDiv <= 0;
SlowClock <= 0;
end
else if (ClockDiv == MaxCount)
begin
SlowClock <= 1;
ClockDiv <= 0;
end
else begin
SlowClock <= 0;
ClockDiv <= ClockDiv+1;
end

Aug 9, 2001 FPGA System Design with Verilo 72


g
Design Rules
 Generally speaking, follow good design
practice for digital systems, e.g., avoid
gated clocks
 In particular, strive to write descriptions
that make all flip-flop clocks connect to the
master clock
 Use asynchronous reset on all flip-flops

Aug 9, 2001 FPGA System Design with Verilo 73


g
Design Rules (cont’d)
 Use a systematic naming convention for
identifiers:
 i$Identifier Module input
 o$Identifier Module output
 r$Identifier Registered
 p$Identifier Parameter
 w$Identifier Wire
Aug 9, 2001 FPGA System Design with Verilo 74
g
Topics for Future Exploration
 Finite State Machines
 One-hot encoding

 ‘casez’ (efficient next-state decoder)

 Data path / controller architecture


 Detailed design rules for FPGAs
 Timing and performance tuning
 Instantiation, multi-module systems
 Testbench design
Aug 9, 2001 FPGA System Design with Verilo 75
g
References
 http://www.rose-
hulman.edu/~doering/fpga_workshop/refere
nces.htm

Aug 9, 2001 FPGA System Design with Verilo 76


g
Lab Projects II

Aug 9, 2001 FPGA System Design with Verilo 77


g
Development Procedure
 Introduction to the XS-40 board
 Review the step-by-step development
procedure

Aug 9, 2001 FPGA System Design with Verilo 78


g
First Design Project
 Implement the following system:

A B (use Pin 19)

(use Pin 44) SimpleSystem


C (use Pin 25)

B = A, C = ~A

Aug 9, 2001 FPGA System Design with Verilo 79


g
Lab Projects III

Aug 9, 2001 FPGA System Design with Verilo 80


g
10-Second Count Down Timer
 Design and implement a 10 second counter
(9 down to 0) with pause and reset inputs
 Display the count value on the 7-segment
display
 Use the 100 MHz on-board clock
 Use the lower two bits of the PC parallel
port for the pause and reset input signals

Aug 9, 2001 FPGA System Design with Verilo 81


g
FPGA System Design with
Verilog
A Workshop Prepared for
Rose-Hulman Ventures
Ed Doering

You might also like