VHDL Language
VHDL Language
Synopsys @ 2000
Contents
! VHDL Overview
! Main Design Units
! Data Types
! VHDL Objects
! Process Statements
! Data flow Statements
! Concurrent Statements
! Hierarchical Designs
! Test Bench
Synopsys @ 2000
VHDL Overview
Synopsys @ 2000
VHDL
What is VHDL?
A Documentation language - interconnections, interface, hierarchy
A Simulation language - behavior, RTL, netlist
A Synthesis language - technology independent logic design
An extendable language
User package
Vendor package
Standard package
VHDL Language
(STD-1076)
Synopsys @ 2000
VHDL Language
VHDL has all the characteristics of a modern programming language
Data types - predefined and user defined
Variables, Signals, and Constants
Expressions - relational, logical, arithmetic,
Sequential statements:
If, Case, For loop
Subprograms
Concurrent statements:
Signal assignment,
concurrent processes, component instances
Packages
Configurations
Dynamic memory allocation
File input/output
Synopsys @ 2000
Synopsys @ 2000
Architectural Definition
RTL
VHDL Source Code
Validate
Simulate Chip at
RTL level
Logic Synthesis
Verification
Simulate Chip at
Gate level
Synopsys @ 2000
Vendor
Netlist
Chip layout
&
Fabrication
Synopsys @ 2000
Library IEEE;
Use IEEE.Std_Logic_1164.All;
Library &
package declaration
entity AND2 is
port ( A, B : in std_logic;
C : out std_logic );
end AND2;
Entity declaration
Synopsys @ 2000
Architecture Specification
Configuration definition
A
C
B
Synopsys @ 2000
Entity Specification
keyword
entity name
entity AND2 is
port ( A, B : in std_logic;
C : out std_logic );
end AND2;
Port name,
direction &
data type
Synopsys @ 2000
in bit;
in std_logic;
out std_logic
out std_logic_vector(7 downto 0)
Entity Exercise
Complete the entity description given below.
All ports are assumed to be of type std_logic.
Clr
Entity _______ is
Q
port (
DFF
Clk
Qn
Set
);
end ______ ;
Synopsys @ 2000
Architecture Specification
keyword
Architecture Name
Entity Name
begin
C <= A and B ;
end RTL;
Describe the
behavior of the
design
Synopsys @ 2000
Configuration
! Primary design unit like entity & architecture
! Binds component instances (architecture) to
entity
! Creates simulation objects
! Facilitates port re-mapping
Synopsys @ 2000
Configuration
keyword
Configuration Name
Entity Name
Architecture Name
Configuration Examples
Entity XR2 is
port ()
end XR2 ;
Architecture fast of XR2 is
end fast;
end small;
Synopsys @ 2000
Packages
! User defined or vendor provided
! Stores declaration of common data types,
constants, component declaration etc.
! Allows sharing of design units
! Package STANDARD included as default
! Packages are supplied in libraries
! Package STD_LOGIC supplied in IEEE library
Synopsys @ 2000
Package Example
keyword
Package Name
Package math is
CONSTANT MEAN_VALUE : INTEGER := 5 ;
end math;
Synopsys @ 2000
Library IEEE ;
Use IEEE.Std_logic_1164.all;
...
Synopsys @ 2000
IEEE Packages
! std_logic_1164
! std_logic_misc
! std_logic_components
! std_logic_textio
! std_logic_arith
! std_logic_unsigned
! std_logic_signed
Synopsys @ 2000
Libraries
Certain VHDL objects are kept in a library
! package - shared declarations
! architecture - shared designs
! entity - shared design interfaces
! configuration - shared design versions
VHDL expects:
Library Synopsys
Use Synopsys.Distributions.All ;
Library IEEE;
Use IEEE.Std_logic_1164.all;
Use IEEE.Std_logic_unsigned.all;
Synopsys @ 2000
Data Types
Synopsys @ 2000
Synopsys @ 2000
Data Types
VHDL provides package STANDARD containing pre-defined types:
! Characters
! String - An array of characters
! Bit
! Bit_vector - An array of bits
! Boolean
! Integer
! Real
! Time
Synopsys @ 2000
Bit literal
! Bit can only be 0 or 1
! Bit_vector is an array of bits defined in double
quotes: 0011 01101111
! Defined in package Standard
! Base precedes sequence
Binary
Octal
Hex
X
Example:
Synopsys @ 2000
X7E
Boolean literal
Synopsys @ 2000
Real Literal
! Represents floating point numbers
! NOT synthesizable
! Format is + or - number.number [E + or - number]
! Examples:
1.0
1
-1.0 E 10
5.3
6 E -10
Synopsys @ 2000
Integer range
! Represents integer values with fixed points
! Operators such as +, -, * can be used
! Minimum/Maximum range depends on the
implementation but must cover the range 2,147,483,647 to +2,147,483,647
! Users can specify a range constraint
! Example: 123 15 - 21
Synopsys @ 2000
Range Constraint
! Simulator checks for valid type and range on
assignment
! Range constraint specifies a restricted range
! Example:
integer range 0 to 9
real range 1.0 to 1.1
! Used to qualify the intended usage of data
Synopsys @ 2000
Back to Bit_Vector
! VHDL has package Standard declarations including
BIT_VECTOR which is an array of bits
! Assignment examples to C of type bit_vector of 4-bits:
C := 1010
Constant bit_vector
C := (1, 0, 1 , 0)
4 BIT Aggregate
C := 3
Invalid
Synopsys @ 2000
Declaring a Bit_Vector
! Declaration must provide constraint on size
variable C : Bit_Vector (0 to 3) ;
variable D : Bit_Vector (3 downto 0) ;
C := 1010;
C(0)
C(1)
C(2)
C(3)
D(1)
D(0)
D := C
1
D(3)
0
D(2)
Slice of an Array
! A subscripted array reference can be used in
either side of a signal or variable assignment
statement
! Example:
Port (
A : in bit_vector (0 to 3);
C : out bit_vector (8 downto 1);
Synopsys @ 2000
Direction in declaration
and slice must be the same
Bit_Vector Example
Architecture
Begin
Process
variable data : bit_vector (0 to 31) ;
variable start : integer range 0 to 24 ;
variable data_out : bit_vector (0 to 7) ;
Begin
For I in 0 to 7 loop
data_out(I) <= data(I + start);
end loop;
end Process
Synopsys @ 2000
uninitialized
unknown
high impedance (tri-state)
weak unknown
weak 0,1
dont care
Synopsys @ 2000
Array of Arrays
Syntax:
type Array_type_name is array (integer_range) of type_name;
Example
type mem_type is array (0 to 65535) of std_logic_vector (7 downto 0);
signal memory : mem_type
Synopsys @ 2000
Physical Time
Type TIME is defined in package Standard
Unit
fs
ps
ns
us
ms
sec
min
hr
femtoseconds
picoseconds
nanoseconds
microseconds
miliseconds
second
minute
hour
Example:
Synopsys @ 2000
Enumerated Types
! Defines legal values through an enumerated list
of character literals (X) or identifiers
! Examples
type instruction is (add, sub, lda);
type value4 is (X, 0, 1, Z);
! Uses symbolic codes instead of numeric values
! Provides for more abstract representation in
source design
Synopsys @ 2000
Synopsys @ 2000
Qualified Expression
! Example:
type months is (May, June, July);
type name is (April, June, Judy);
June is ambiguous type in some contexts
! To resolve ambiguity, use Qualified Expression
months(June)
name(June)
Synopsys @ 2000
Synopsys @ 2000
VHDL Objects
Synopsys @ 2000
VHDL Objects
! Signal - global wires which use up simulator
time
! Constant - name specifies value
! Variable - local storage or wires
! VHDL Objects names are not case sensitive but EDA tools
may have their restriction on this.
! Do not use VHDL reserved words
! Make sure you have a consistent naming style
Synopsys @ 2000
Signals
! Can be scalar (single bit) or array and can be initialized.
Example:
signal identifier : data_type := initial_value
optional
signal reset : bit := 0 ;
signal internal_bus : std_logic_vector (7 downto 0) ;
signal dec_int : integer range 0 to 4 ;
Synopsys @ 2000
Signal (cont.)
! Represents global data storage or wires
! Declared in Architecture
! Assigned with
<=
(pronounced as gets)
Signal Declaration
! Defined before the begin clause in an
Architecture
! Example
Architecture RTL of AN2 is
signal xyz : bit := 1 ;
signal abc : std_logic ;
Begin
end RTL ;
Synopsys @ 2000
Internal signals
defined within this
region
Constants
! Names constant values
constant identifier : data_type := value ;
! Scalar example:
constant pi : real := 3.1416 ;
! Array example:
constant mask : bit_vector(3 downto 0) := 0011 ;
! Generally defined in a VHDL package
Synopsys @ 2000
Variables
! Can be scalar or array and can be initialized
! Syntax:
variable identifier : type := initial_value
! Example:
variable xyz : bit ;
variable xyz : integer range 0 to 7 := 7 ;
variable dbus : std_logic_vector (3 downto 0) ;
Synopsys @ 2000
Variables (cont.)
! Local data inside a process (covered later)
! Assigned immediately, no simulated time
! Right hand side type must match left hand side
! Assigned with :=
! Example:
xyz := 0 ;
data := 3 ;
dbus := 1011 ;
Synopsys @ 2000
Variable Declaration
! Defined before the begin clause in an Process
! Example:
end process;
...
end RTL ;
Synopsys @ 2000
variables
defined within this
region
Signal
Visibility
...
end process;
...
end RTL ;
Synopsys @ 2000
Variable
Visibility
Operators
! Logical : and or nand nor xor
! relation:
! adding
+-&
! sign
+-
! multiplying operator
! misc.
low precedence
* / mod rem
** abs not
A+B*C
valid
A and B or C
not valid
high precedence
Synopsys @ 2000
Process Statements
Synopsys @ 2000
Process Statements
! Defines a regions in Architectures where
sequential statements are executed
! Provides programming language-like capability
using temporary variables
! Used for behavioral descriptions
! In simulation all processes are started and
executed up to a wait statement
! Must contain either a wait statement or
sensitivity list
! Behaves as an infinite loop, except for the wait
Synopsys @ 2000
Process Example
Entity nand2 is
port (a,b : in bit ;
c: out bit );
end nand2 ;
sequential statements
Synopsys @ 2000
Parallel Process
Architecture RTL
Begin
A: Process
begin
if (true) then
wait
end Process A ;
B: Process
begin
if (true) then
wait
end Process B ;
end RTL;
Synopsys @ 2000
Process Statements:
Sequential Statements
Synopsys @ 2000
Sequential Statements
! Only valid inside a Process
! Statements include:
Assignment with logic/arithmetic operators
IF
CASE
LOOP
Wait
Synopsys @ 2000
If and ELSIF
If then
statements;
...
end if;
If then
statement 1;
else
statement 2;
end if;
If then
statement 1;
elsif then
statement 2;
elsif then
statement 3;
else
statement 3;
...
end if;
If Statement Example
If enable = 1 then
Q <= D ;
end if;
If RESET = 1then
Q <= 0 ;
elsif CLKevent and CLK=1 then
Q <= D ;
end if;
Synopsys @ 2000
If SEL=00 then
DATA <= 0 ;
elsif SEL=01 then
DATA <= 1 ;
elsif SEL=10 then
DATA <= 2 ;
else
DATA <= 3;
Case Statement
case expression is
when value_1 =>
s1; s2; sn;
when value_2 =>
s1; s2; sn;
No begin/end required
around these group of
statements
Synopsys @ 2000
Synopsys @ 2000
Loop Statement
! Optional label for each loop
! Can have more then one loop in a process
! Two types of loop: FOR LOOP & WHILE LOOP
! FOR LOOP iterates for fixed number of times
! WHILE LOOP iterates until a condition is
satisfied
! Nested loops are allowed
! Use NEXT to skip a single iteration
! Use EXIT to exit a loop before it completes
Synopsys @ 2000
Next Example
Max: FOR IND in 0 to max_limits LOOP
next when ( VALUE(IND) < biggest ) ;
biggest := VALUE(IND) ;
Synopsys @ 2000
Exit Example
Outside: For IND in 0 to max loop
Inside: For JND in 0 to max loop
Exit Outside when a(I) < 0 ;
power(IND, JND) := a(IND) ** JND
end loop Inside;
end loop Outside;
Synopsys @ 2000
Synopsys @ 2000
Wait Statements
! Suspends process execution
! Three types:
WAIT FOR
( not synthesizable )
WAIT UNTIL
( synthesizable partially )
WAIT ON
( not synthesizable )
* covered later
Synopsys @ 2000
Synopsys @ 2000
Subprograms
Synopsys @ 2000
Procedures
! Considered a statement not an expression
(does not have a value)
! Can modify parameter so can return more then
one value
! In, Out, Inout parameter modes
! Parameters can be variables or signals
! Declared in package, process or architecture
Synopsys @ 2000
Procedure Syntax
Procedure procedure_name
( formal_parameter_list ) is
Procedure
Declaration
procedure_declaration ;
begin
sequential_statements ;
end procedure_name ;
Synopsys @ 2000
Procedure
Body
Procedure Example
Procedure vector_to_int
(Z : in bit_vector (0 to 7);
signal zero_flag : out boolean;
q : inout integer ) is
begin
q:= 0;
zero_flag <= true;
for i in 0 to 7 loop
q : q*2;
if (Z(i) = 1) then
q := q + 1;
zero_flag <= false;
end if;
end loop
end vector_to_int;
Synopsys @ 2000
Process
begin
vector_to_int
(my_vector,
test_flag,
result) ;
...
Function
! Returns only ONE value
! Contains sequential statements: if, case, loop
etc.
! Converts from one type to another
! Some built in function (refer to VHDL manuals)
! Some vendor or user defined
Function name (parameters) return TYPE is
variable declarations
begin
sequential statements ;
return () ;
end name;
Synopsys @ 2000
Function Example
Function func3 ( A, B : in real) return real is
begin
return A * B * 2.0 ;
end func3;
Function BigNum (A, B : in integer)
return std_logic is
begin
if (A >B) then
return 1 ;
else
return 0 ;
end BigNum ;
Architecture
begin
signal temp : std_logic ;
process
X <= 6;
Y <= 7;
temp <= BigNum (X, Y);
...
Synopsys @ 2000
Process Statements:
Communications
Synopsys @ 2000
Synopsys @ 2000
Local
Variable
Last: Process
signal
Local
Variable
Synopsys @ 2000
end Want_First
Want_Last: Process
begin
wait until First_Done = 1 ;
Synopsys @ 2000
C <= A <= B ;
C <= ( A <= B ) ;
Synopsys @ 2000
A <= B after 10 NS ;
B
A
B
0 5
10
A
10
Synopsys @ 2000
20
B
0 5
A
10 15
Synopsys @ 2000
Synopsys @ 2000
Simulation Time
Now
50
100
150
sys_clk
sys_clk2
200
Synopsys @ 2000
Synopsys @ 2000
Synopsys @ 2000
Synopsys @ 2000
end ...
! Builds combinational circuitry
! If B or C changes then statement 1 is evaluated
! If E or F changes then statement 2 is evaluated
Synopsys @ 2000
Example:
Z <= A when (X > 3) else B;
R <= S when (Y > 4) else
T when (Y < 4) else
U;
Can have only one target per assignment
Synopsys @ 2000
Synopsys @ 2000
Concurrent Statements
Synopsys @ 2000
Concurrent Statements
! Process
each process from begin to end is one
concurrent statement
! Component Instance
! Data Flow statements
Synopsys @ 2000
Synopsys @ 2000
Variable
! Values updated
without delay
Synopsys @ 2000
! Variables updated
immediately within the
process
Process (clk)
begin
if clkevent and clk=1 then
Q <= D;
end if;
end process
Process
begin
if clk = 1 then
Q <= D;
end if;
wait on clk;
end process
Drivers
! Drivers are created by signal assignment
statements
! Drivers contain present and future values
! A driver is a contributor to a signal value
! Value of a signal is the resolution of all the
driver values
! Multiple concurrent assignments to the same
signal may be ERROR prone ...
Synopsys @ 2000
Multiple Assignment
Example:
Architecture concurrent
begin
C <= A ;
C <= B ;
Architecture concurrent
begin
Process (A,B)
begin
C <= A ;
C <= B ;
end process ;
end concurrent;
Hierarchical Design
Synopsys @ 2000
Design Hierarchy
! A VHDL design consists of a hierarchy of
components compiled from behavioral, data
flow or structural level architecture
! All components used must exist as compiled
designs in a library
! A components declaration is required before a
components is instanced
! A design containing components requires a
configuration (simulator dependent)
Compare
XR2
Synopsys @ 2000
INV
Component Declaration
Architecture S of compare
signal I : bit;
component XR2
port (X,Y : in bit ;
Z : out bit );
end component;
component INV port (X : in bit;
Z : out bit);
end component;
begin
U1: XR2 port map (A,B,I) ;
U2: INV port map (I,C);
end S;
Component
Declaration
Use work.xyz_gates.all;
Architecture S of compare
signal I : bit;
begin
U1: XR2 port map (A,B,I) ;
U2: INV port map (I,C);
end S;
Component
Instantiation
Synopsys @ 2000
Gates in a Library
Entity NAND2 is
port (A,B : in std_logic; C : out std_logic);
end NAND2;
Architecture ARC1 of NAND2 is
begin
C <= NOT (A and B) after 1.1 ns ;
end ARC1;
Component Declarations
Architecture S of compare
signal I : bit;
component XR2
port (X,Y : in bit ;
Z : out bit );
end component;
component INV port (X : in bit;
Z : out bit);
end component;
begin
U1: XR2 port map (A,B,I) ;
U2: INV port map (I,C);
end S;
Entity XR2 is
port (X,Y : in std_logic; Z: out std_logic);
end NAND2;
Architecture ARC1 of XR2 is
begin
Z <= X xor Y;
end ARC1;
Component Instantiation
compare
A
Xr2
B
INV
Syntax:
instance_name : component_name ( port mapping) ;
Example:
U1: XR2 port map (A,B,I) ;
U2: INV port map (I,C);
Positional Association
Named Association
Synopsys @ 2000
Entity compare is
port (A,B : in bit ;
C : out bit );
end compare;
Architecture S of compare
signal I : bit;
component XR2
port (X,Y : in bit ;
Z : out bit );
end component;
component INV port (X : in bit;
Z : out bit);
end component;
begin
U1: XR2 port map (A,B,I) ;
U2: INV port map (I,C);
end S;
Synopsys @ 2000
Test Bench
Synopsys @ 2000
Simulation Environment
control
commands
OUTPUT
INPUT
Textual messages
models
test vectors
Simulator
Tabular output
libraries
Graphical waveform
Assembly/
Microcode
Visual drawings
Feedback
Synopsys @ 2000
Synopsys @ 2000
Synopsys @ 2000
Process
Stimulus
and
results
testing
Synopsys @ 2000
Component
Unit
Under
Test
(UUT)
Synopsys @ 2000
Synopsys @ 2000
Entity testbench is
end tesbench
architecture test of testbench is
signal in_sig: std_logic_vector (3 downto 0);
signal out_sig : std_logic_vector (3 downto 0);
component TOP
(A : in std_logic_vector (3 downto 0);
B :out std_logic_vector (3 downto)) );
end component ;
begin
UUT Instantiation
UUT : TOP port map (in_sig, out_sig) ;
Stimulus definition
Synopsys @ 2000
Process
begin
in_sig <= 0000 ;
wait for 10 ns ;
in_sig <= 0001 ;
...
TextIO
! Predefined package in library STD, requires the
use of : use STD.TEXTIO.all;
! Is a procedure that allows reading and writing
of ASCII text files
! Text files are treated as a group of lines
! Requires the use of procedure read() and
readline()
Synopsys @ 2000
readline
read
17
Synopsys @ 2000
TextIO Example
Use STD.TEXTIO.ALL;
.
.
.
Process
File infile : Text is in /path/test/example1.vec;
File outfile : Text is out /path/test/results.dat;
Variable out_line, my_line : line ;
Variable int_val : integer;
begin
while NOT (ENDFILE (infile)) loop
readline (infile, my_line) ;
read ( my_line, inv_val );
int_val := int_val ** 2 ;
write (out_line, int_val);
writeline (outfile, out_line) ;
end loop;
...
Synopsys @ 2000
Example1.vec
10
20
50
1_2_3
87
52
results.dat
100
400
2500
15129
7569
Sample Codes
Synopsys @ 2000
Library IEEE;
Use IEEE.std_logic_1164.all;
Entity TRI is
port (A, SEL : in std_logic ;
B : out std_logic );
end TRI;
Architecture RTL of TRI is
begin
B <= A when (SEL = 1) else Z ;
end RTL;
Entity TRI is
port (A, SEL : in std_logic ;
B : out std_logic );
end TRI;
Architecture RTL of TRI is
begin
Process (A, SEL)
begin
if (SEL = 1) then
B <= A ;
else
B <= Z ;
end if ;
end process;
end RTL;
SEL
Synopsys @ 2000
Signal Attribute
Syntax:
Signal_name event
returns true if an event occurred in the current simulation
time step
Example:
If clkevent and clk = 1 -- wait for rising edge of clk
if clkevent and clk = 0 -- wait for falling edge of clk
Synopsys @ 2000
Synopsys @ 2000
Inferring D Flip-Flop
Library IEEE;
Use IEEE.std_logic_1164.all;
Entity DFF is
port (D, CLK : in std_logic;
Q: out std_logic );
end DFF;
Synopsys @ 2000
Synopsys @ 2000
Synopsys @ 2000
Synopsys @ 2000
Synopsys @ 2000
State Machine
Library IEEE;
Use IEEE.std_logic_1164.all;
entity SM is
port (rst, clk : in std_logic ;
a,b,c : in std_logic ;
q : out std_logic );
end SM;
Synopsys @ 2000
State Machine -2
Architecture RTL of SM is
type state_type is ( S1, S2, S3, S4) ;
signal current_state, next_state : state_type ;
begin
P1: process (rst, clk)
begin
if rst = 1 then
current_state <= S1 ;
elsif clkevent and clk=1 then
current_state <= next_state;
end if;
end process P1;
Synopsys @ 2000
VHDL Summary
! A complete design is an interconnection of
component designs
! Each component design has a compiled entity
and architecture
! All communications occurs through ports
declared in the entity specification while
matching signal types, sizes and directions
! An architecture may be modeled at the
behavioral, data flow or structural level
! Packages contains commonly used
declarations
! A library contains reusable VHDL design units
Synopsys @ 2000