VHDL
VHDL
• Entity
• Architecture
• Package
• Configuration
• Library
Each module corresponds to a design entity in VHDL. Each design entity
has two parts:
2
BASIC STRUCTURES IN VHDL
• Entity declaration
• Architecture bodies
3
ENTITY DECLARATIONS
The entity declaration provides an external view of a component but does
not provide information about how a component is implemented. The
syntax is ;
entity entity_name is
[generic (generic_declarations);]
[port (port_declarations);]
{entity_declarative_item{constants, types, signals};}
end [entity_name];
[ ] : square bracket denotes optional parameters.
| : vertical bar indicates a choice among alternatives.
{ } : a choice of none, one or more items can be made.
4
GENERIC DECLARATIONS
The generic_declaration declares constants that can be used to control the
structure or behaviour of the entity. The syntax is ;
generic (
constant_name : type [:=init_value]
{;constant_name : type [:=init_value]}
);
value for the constant.
5
PORT DECLARATIONS
The port_declaration specifies the input and output ports of the entity.
port (
port_name : [mode] type [:=init_value]
{; port_name : [mode] type [:=init_value]}
);
VHDL is not case sensitive, so xyz=xYz=XYZ !!!
6
PORT DECLARATIONS
There are four port modes :
• in : can only be read. It is used for input only (can be only on the
right side of the assignment).
• out : can only be assigned a value. It is used for output only (can
be only on the left side of the assignment).
• inout : can be read and assigned a value. It can have more than one
driver (can be both on the right and left side of the
assignment).
• buffer : can be read and assigned a value. It can have only one driver
(can be both on the right and left side of the assignment).
Inout is a bidirectional port whereas buffer is a unidirectional one. The
entity_declarative_item declares some constants, types or signals that can
be used in the implementation of the entity. 7
PORT DECLARATIONS
Example :
entity xxx is
port ( A : in integer ;
B : in integer ;
C : out integer ;
D : inout integer ;
E : buffer integer) ;
end xxx ;
architecture bhv of xxx is
begin
process(A, B)
begin
C <= A ; (valid : A is assigned to C)
A <= B ; (not valid : A is an input port so cannot be assigned a value, A is on the left side)
E <= D + 1 ; (valid : D is inout, so it can be both assigned and read)
D <= C + 1 ; (not valid : C is out port, so cannot be read for input, C is on the right side)
end process ;
end bhv ;
8
ENTITY DECLARATION EXAMPLES
Figure-1 shows the interface of a one-bit adder. The entity name of the
component is FULL_ADDER. It has input ports A, B and CIN which are
of data type BIT, and output ports SUM and COUT which are also type
BIT. A corresponding VHDL description is shown below.
A B
entity FULL_ADDER is
port ( A, B, CIN : in BIT ;
SUM, COUT : out BIT ); COUT
CIN
FULL_ADDER
end FULL_ADDER ;
SUM
9
ENTITY DECLARATION EXAMPLES
We can control the structure and timing of an entity using generic
constants. For example, in the following VHDL description generic
constant N is used to specify the number of bits for the adder. During the
simulation or the synthesis process, the actual value for each generic
constant can be changed.
entity ADDER is
generic ( N : INTEGER := 4 ;
M : TIME := 10ns );
port ( A, B : in BIT_VECTOR (N-1 downto 0);
CIN : in BIT ;
SUM : out BIT_VECTOR (N-1 downto 0);
COUT : out BIT );
end ADDER ;
10
ENTITY DECLARATION EXAMPLES
COUT CIN
FULL_ADDER
11
ARCHITECTURES
An architecture provides an “internal” view of an entity. An entity may
have more than one architecture. It defines the relationships between the
inputs and the outputs of a design entity which may be expressed in terms
of :
• behavioural style
• d atfa low style
• structural style
declaration section where signals, types, constants, components, and
subprograms are declared, followed by a collection of concurrent
statements.
12
ARCHITECTURES
An architecture is declared using the following syntax :
13
BEHAVIORAL STYLE
ARCHITECTURES
A behavioural style specifies what a particular system does in a program
like description using processes, but provides no details as to how a design
is to be implemented. The primary unit of a behaviour description in
VHDL is the process. The example below shows a behavioural
description of a full_adder.
Example :
begin
if (A=‘0’ and B=‘0’ and CIN=‘0’) then
SUM <= ‘0’;
COUT <=‘0’; 14
BEHAVIORAL STYLE
ARCHITECTURES
elsif (A=‘0’ and B=‘0’ and CIN=‘1’) or
(A=‘0’ and B=‘1’ and CIN=‘0’) or
(A=‘1’ and B=‘0’ and CIN=‘1’) then
SUM <= ‘1’;
COUT <=‘0’;
elsif (A=‘0’ and B=‘1’ and CIN=‘1’) or
(A=‘1’ and B=‘0’ and CIN=‘1’) or
(A=‘1’ and B=‘1’ and CIN=‘0’) then
SUM <= ‘0’;
COUT <=‘1’;
elsif (A=‘1’ and B=‘1’ and CIN=‘1’) then
SUM <= ‘1’;
COUT <=‘1’;
end if ;
end process ; 15
DATAFLOW STYLE
ARCHITECTURES
A dataflow style specifies a system as a concurrent representation of the
flow of control and movement of data. It models the information flow or
dataflow behaviour, over time, of combinational logic functions such as
adders, comparators, decoders, and primitive logic gates. The example
below illustrates an architecture DATAFLOW of entity FULL_ADDER.
Example :
S <= A xor B ;
SUM <= S xor CIN after 10ns ;
COUT <= (A and B) or (S and CIN) after 5ns ;
end DATAFLOW ; 16
STRUCTURAL STYLE
ARCHITECTURES
A structural style defines the structural implementation using component
declarations and component instantiations. The following shows a
structural description of the same FULL_ADDER. Two types of
components are defined in this example, HALF_ADDER and OR_GATE.
Example :
end component ;
OR_GATE instance. The HALF_ADDER instance can be bound to
another entity which consists of an XOR gate and an AND gate.
18
PACKAGES
The primary purpose of a package is to collect elements that can be shared
(globally) among two or more design units. It contains some common
data types, constants, and subprogram specifications.
The separation between package declaration and package body serves the
same purpose as the separation between the entity declaration and
architecture body.
19
PACKAGES
The package syntax is :
package package_name is
{package_declarative_item}
end [package_name] ;
Packages will be defined more in detail in latter slights !!!
20
PACKAGE EXAMPLE
The example below shows a package declaration. The package name is
EX_PKG. Since we define a procedure called incrementer, we need to
define the behaviour of the function separately in a package body.
package EX_PKG is
subtype INT8 is INTEGER range 0 to 255 ;
constant ZERO : INT8 := 0 ;
procedure Incrementer ( variable Count : inout INT8 ) ;
end EX_PKG ;
package body EX_PKG is
procedure Icrementer (variable Data : inout INT8) is
begin
if (Count >= MAX) then Count := ZERO ;
else Count := Count + 1 ;
end if ;
end Incrementer ;
end EX_PKG ;
21
CONFIGURATIONS
An entity may have several architectures. During the design process, a
designer may want to experiment with different variations of a design by
selecting different architectures. Configurations can be used to provide
fast substitutions of component instances of a structural design. The
syntax is :
{configuration_item}
end for ;
22
CONFIGURATION EXAMPLE
For our FULL_ADDER entity we have three architectures and for
structural architecture we use two HALF_ADDER’s and one OR_GATE.
The following example shows a configuration of entity FULL_ADDER.
The name of the configuration is arbitrary (FADD_CONFIG). The
STRUCTURE refers to the architecture of entity FULL_ADDER to be
configured. Assume that we have already compiled HALF_ADDER and
OR_GATE entities to the library burcin and HALF_ADDER entity has
got more than one architecture one of which is STRUCTURE.
for HA1, HA2 : HALF_ADDER use entity burcin.HALF_ADDER(STRUCTURE) ;
for OR1 : OR_GATE use entity burcin.OR_GATE ;
end for ;
end FADD_CONFIG ; 23
DESIGN LIBRARIES
The results of a VHDL compilation (analyze) are kept inside of a library
for subsequent simulation, for use as a component in other designs. A
design library can contain the following library units :
• Packages
• Entities
• Architectures
• Configurations
you want but you cannot nest them !!!
24
DESIGN LIBRARIES
To open a library to access a compiled entity as a part of a new VHDL
design, you first need to declare the library name. The syntax is :
You can access compiled units from a VHDL library up to three levels of
name. The syntax is :
25
LIBRARY EXAMPLE
We create a package to store a constant which can be used in many
designs. And we compile it to a library called burcin.
Package my_pkg is
constant delay : time := 10ns ;
end my_pkg ;
S <= A xor B ;
SUM <= S xor CIN after burcin.my_pkg.delay ;
COUT <= (A and B) or (S and CIN) after 5ns ;
end DATAFLOW ; 26
DATA OBJECTS
A data object holds a value of a specific type. There are three classes of
data objects in VHDL :
• constants
• variables
• signals
The class of the object is specified by a reserved word that appears at the
beginning of the declaration of that object.
27
CONSTANTS
Examples :
constant CHAR7 : BIT_VECTOR (4 downto 0) := “00111” ;
constant MSB : INTEGER := 5 ;
28
VARIABLES
Variables are used to hold temporary data. They can only be declared in a
process or a subprogram. The syntax is :
Examples :
variable X , Y : BIT ;
variable TEMP : BIT_VECTOR (8 downto 0) ;
variable DELAY : INTEGER range 0 to 15 := 5 ;
29
SIGNALS
Signals connect design entities together and communicates changes in
values between processes. They can be interpreted as wires or busses in an
actual circuit. Signals can be declared in packages (global signals),
entities (entity global signals), architectures (architecture global signals)
and blocks. The syntax is :
Examples :
signal TEMP : STD_LOGIC_VECTOR (8 downto 0) ;
signal COUNT : INTEGER range 0 to 100 := 5 ;
30
DATA TYPES
All data objects in VHDL must be defined with a data type. A type
declaration defines the name of the type and the range of the type. Type
declarations are allowed in package declaration sections, entity
declaration sections, architecture declaration sections, subprogram
declaration sections, and process declaration sections. Data types
include :
• Enumeration types
• I ntegetr ypes
• Predefined VHDL data types
• Array types
• Record types
• STD_LOGIC data type
• SIGNED and UNSIGNED data types
31
• Subtypes
ENUMERATION TYPES
An enumeration type is defined by listing all possible values of that type.
All the values are user_defined. These values can be identifiers or single
character literals. An identifier is a name such as blue, ball, monday.
Character literals are single characters enclosed in quotes such as ‘x’, ‘0’.
The syntax is :
type COLOR is (RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE) ;
type DAY is (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY) ;
type STD_LOGIC is (‘U’, ‘X’, ‘0’, ‘1’, ‘Z’, ‘W’, ‘L’, ‘H’, ‘_’) ;
32
ENUMERATION TYPES
By default the initial value is the lowest (leftmost) value of range for that
type !!!
33
INTEGER TYPES
Integer types are for mathematical integers. They are useful for counting,
indexing, and controlling loops. In most VHDL implementations typical
range is -2,147,483,647 to +2,147,483,647. The syntax is :
Examples :
34
PREDEFINED VHDL DATA TYPES
IEEE predefined two site_specific packages : STANDART and TEXTIO
in the STD library. Each contains a standard set of types and operations.
The following shows a summary of data types defined in the
STANDARD package.
are represented by themselves in single quotation marks.
• INTEGER : Represents positive and negative numbers. Range is
specified from -2,147,483,647 to +2,147,483,647 . Mathematical
functions like add, subtract, multiply, divide apply to integer types. 35
PREDEFINED VHDL DATA TYPES
36
PREDEFINED VHDL DATA TYPES
37
ARRAY TYPES
Array types group one or more elements of the same type together as a
single object. There are two types of array :
where array_type_name is the name of the constrained array type,
discrete_range is a subrange of another integer type or an enumeration
type, and subtype_indication is the type of each array element. 38
ARRAY TYPES
An unconstrained array type is a type whose index range is not defined.
But index type is defined. The syntax of an unconstrained array type is :
Example :
A1 is an array of 32 elements in which each element is of type INTEGER.
The other examples show how BIT_VECTOR and STRING types are
created in STANDARD package. 39
ARRAY TYPES
To use an unconstrained array type, the index range has to be
specified!!!
Example :
Index range determines the number of elements in the array and their
direction (low to high | high downto low).
40
ARRAY TYPES
VHDL allows declaration of multiple dimensional arrays which can be
used in modelling of RAMs and ROMs.
Example :
(‘1’, ‘1’, ‘0’, ‘0’) ) ;
X := ROM(4,3) ;
Example :
DATE : DATE_TYPE ;
end record ;
42
RECORD TYPES
signal S : HOLIDAY ;
variable T1 : integer range 1900 to 1999 ;
variable T2 : DATE_TYPE ;
T1 := S . YEAR ;
T2 := S . DATE ;
S . DAY <= 30 ;
43
STD_LOGIC TYPES
To model a signal line with more than two values (‘0’ and ‘1’), VHDL
defines nine strengths with in a standard package. The nine values
include:
‘H’ -- Weak high
‘_’ -- Don’t care
); 44
STD_LOGIC TYPES
To use the definitions and functions of the Standard Logic Package, the
following statements have to be included in the program !!!
Library IEEE ;
use IEEE.STD_LOGIC_1164.all ;
45
SIGNED and UNSIGNED
DATA TYPES
Both signed and unsinged data types are defined in the Standard
Synthesis packages, NUMERIC_BIT and NUMERIC_STD. Objects with
UNSIGNED type are interpreted as unsigned binary integers and objects
with SIGNED type are interpreted as two’s complement binary integers.
The definitions of the data types are :
use IEEE.STD_LOGIC_1164.all ;
use IEEE.NUMERIC_BIT.all ;
use IEEE.NUMERIC_BIT.all ; 46
SUBTYPES
Example :
47
OPERATORS
VHDL provides six classes of operators. Each operator has a precedence
level. All operators in the same class have the same precedence level.
48
LOGICAL OPERATORS
Logical operators and, or, nand, nor, xor, and not accept operands of
pre_defined type BIT, BOOLEAN and array type of BIT. Operands must
be the same type and length.
Example :
49
RELATIONAL OPERATORS
Example :
50
ADDING OPERATORS
Adding operators include “+”, “-” and “&”.the concatenation operator is
“&” is supported for all register array objects. It builds a register array by
combining the operands. An unsigned (signed) number can operate with
both integers and bit_vectors !!!
Example :
“ABC” & “xyz” results in : “ABCxyz”
“1010” & “1” results in : “10101”
51
OPERANDS
In an expression, the operator uses the operands to compute its value.
Operands can themselves be expressions. Operands in an expression
include :
• Literals
• Identifiers
• Indexed names
• Slice names
• Attribute names
• Aggregates
• Qualified expressions
• Function calls
• Type conversions
52
LITERALS
Literals (constants) can be classified into two groups :
• Scalar Type
character
bit
std_logic
boolean
real
integer
time
• Array Type
string
bit_vector
53
std_logic_vector
CHARACTER LITERALS
A character literal defines a value by using a single character enclosed in
single quotes : ‘x’. Generally VHDL is not case sensitive however it does
consider case for character literals. For example , ‘a’ is not the same as
‘A’. Character literal can be anything defined in the Standard package.
Default value is NUL.
Example :
‘A’
‘a’
‘‘
‘’’
character’(‘1’)
(character literal is not the same as bit_literal ‘1’ or integer 1, so it may be necessary to
provide the type name)
54
STRING LITERALS
A character string is an array of characters. Literal character strings are
enclosed in double quotes.
Example :
55
BIT LITERALS
A bit literal represents two discrete values by using the character literals
‘0’ and ‘1’. Sometimes it may be necessary to make the bit type literal
explicit to distinguish it from a character.
Example :
‘1’
‘0’
bit’(‘1’)
56
BIT_VECTOR LITERALS
A bit_vector literal is an array of bits enclosed in double quotes.
Example :
“00110101”
x”00FF”
b”10111”
o”277”
bit_vector’(“10”)
‘x’ is used for hexadecimal values, ‘b’ for binary, ‘o’ for octal.
57
STD_LOGIC LITERALS
A standard logic literal is one of the nine values defined in the standard
package which should be given in upper case letters and single quote
marks.
Example :
‘L’
‘H’
‘_’
58
STD_LOGIC_VECTOR LITERALS
A standard logic vector literal is an array of std_logic elements given in
double quotes.
Example :
“10_1Z”
“UUUU”
signed’(“1011”)
59
BOOLEAN LITERALS
Example :
true
false
True
TRUE (not case sensitive)
60
REAL LITERALS
A real literal represents the real numbers between -1.0E+38 and 1.0E+38.
Synthesis tools typically do not support either real arithmetic or real
literals, but simulators do support type real.
Example :
-1.0
-1.0E+10
61
INTEGER LITERALS
An integer literal represents the integer numbers between -2,147,483,647
and 2,147,483,647.
Example :
+1
862 NOT 862.0
-257
+123_456
16#00FF#
base_n#number# means number is defined in base n, where n is 2 to 16.
62
TIME (Physical) LITERALS
The only predefined physical type is time.
Example :
10 ns
100 us
6.3 ns
63
IDENTIFIERS
An identifier is a simple name. It is a name for a constant, a variable, a
signal, an entity, a port, a subprogram, and a parameter declaration. A
name must begin with an alphabetic letter followed by letters, underscores
or digits. Underscore ‘_’ cannot be the last character. VHDL identifiers
are not case sensitive. There are some reserved words in VHDL such as
entity, port etc. which cannot be used as an identifier.
Example :
64
INDEXED NAMES
An index name identifies one element of an array object. The syntax is :
array_name ( expression)
Example :
variable ADDR : INTEGER range 0 to 7 ;
variable DATA : INTEGER range 0 to 123 ;
DATA := DATA_ARRAY (ADDR) ;
65
SLICE NAMES and ALIASES
Slice names identify a sequence of elements of an array object. The
direction must be consistent with the direction of the identifier’s array
type. An alias creates a new name for all or part of the range of an array
object.
Example :
alias A4 : BIT is A1(3) ;
66
ATTRIBUTE NAMES
An attribute takes a variable or signal of a given type and returns a value.
The following are some commonly used predefined attributes :
• left : returns the index of the leftmost element of the data type.
• right : returns the index of the rightmost element of the data type.
• high : returns the index of the highest element of the data type.
• low : returns the index of the lowest element of the data type.
• range : determines the index range.
• reverse_range : determines the index reverse_range.
• length : returns the number of elements of a bit_vector.
• event : represents whether there is a change in the signal value at the
current simulation time (associated with signals).
67
ATTRIBUTE NAMES
Example :
68
AGGREGATES
An aggregate can be used to assign values to an object of array type or
record type during the initial declaration or in an assignment statement.
Example :
In the second line we define an array whose element number (index-range) is given by
color_list. Since color_list includes 4 elements, color_array type also includes 4 elements all
of which are bit_vector. Instead of color_list we may use (range 0 to 3) , because this
definition only defines the range not the element types.
69
QUALIFIED EXPRESSIONS
A qualified expression states the type of the operand. The syntax is :
type_name’(expression)
Example :
color2’(red)
70
TYPE CONVERSIONS
A type conversion provides for explicit conversion between closely
related types. The syntax is :
type_name(expression)
Example :
71
SEQUENTIAL STATEMENTS
Sequential statements specify the step by step behaviour of the process.
They are executed starting from the first statement, then second, third until
the last statement. The statements within a process are sequential
statements whereas the process itself is a concurrent statement. The
following are the sequential statements defined in VHDL :
• ASSERTION statements
• LOOP statements
• NEXT statements 72
SEQUENTIAL STATEMENTS
• EXIT statements
• WAIT statements
• PROCEDURE calls
• RETURN statements
73
VARIABLE ASSIGNMENT
STATEMENTS
A variable assignment statement replaces the current value of a variable
with a new value specified by an expression. The variable and the result of
the expression must be of the same type. The syntax is :
target_variable := expression ;
Variables declared within a process cannot pass values outside of the
process ; that is they are local to a process or subprogram !!!
74
VARIABLE ASSIGNMENT
STATEMENTS
Example :
end process ;
75
SIGNAL ASSIGNMENT
STATEMENTS
A signal assignment statement replaces the current value of a signal with
a new value specified by an expression. The signal and the result of the
expression must be of the same type. The syntax is :
when a signal is assigned, the assignment will not take effect immediately,
instead will be scheduled to a future simulation time. There are two types
of delay that can be applied when scheduling signal assignments :
• Transport delay
• Inertial delay
76
TRANSPORT DELAY
If the delay time implies a transaction that follows (in time) already
scheduled transactions, the new transaction is added to the end of all the
others.
If the delay time implies a transaction that precedes (in time) already
scheduled transactions, the new transaction overrides all the others.
77
TRANSPORT DELAY
…….
Process (…….)
begin
S <= transport 1 after 1 ns, 3 after 3 ns, 5 after 5 ns ;
S <= transport 4 after 4 ns ;
end ;
…….
S
4
3
1
If the delay time implies a transaction that follows (in time) and is
different from (in value) the transactions already scheduled by other
statements, the new transaction overrides the others. If the value is the
same the new transaction is added to the end.
If the delay time implies a transaction that precedes (in time) already
scheduled transactions, the new transaction overrides all the others.
……….
S
Process (…….) 5
begin 3
S <= 3 after 5 ns ; 3
S <= 5 after 5 ns ; 1
1ns 3ns 5ns t
end ;
……….
80
INERTIAL DELAY
……….
Process (…….)
begin
S <= 1 after 1 ns, 3 after 3 ns, 5 after 5 ns, 6 after 6 ns ;
S <= 3 after 4 ns, 4 after 5 ns ;
end ;
……….
S
4
3
1
A A
S S
10ns 20ns 30ns 40ns t 10ns 20ns 30ns 40ns t
82
ZERO DELAY and DELTA DELAY
Variable assignments are executed in zero time. However VHDL uses
delta time concept for signal assignments. Each signal assignment
statement is executed after a delta time.
process (CLK)
signal A : integer := 5 ;
B, C : integer := 0 ; CLK
variable D : integer := 0 ; A 5 1 2
begin
A <= 1; B 0 5
A <= 2; C 0 3
B <= A; 0 3
D
D := 3;
C <= D; Initial 0- 1∆ 2∆ 0+
end process ; values
83
ZERO DELAY and DELTA DELAY
The process is activated by any change in the CLK signal. The CLK
changes in zero time. 0- and 0+ are both 0 for a simulator. The interval,
two delta (2∆) is a virtual concept. A signal assignment is executed after a
delta delay however variable assignments are executed in zero time. The
first assignment is a signal assignment, therefore A will be assigned “1”
after a delta time. The second assignment is also a signal assignment so A
will be “2” after two delta time. Third assignment assigns signal B, the
initial value of A (the value at 0- time) because delta time concept is
virtual. So B takes “5” after a delta time. Fourth assignment is a variable
assignment, so it will be executed without delta delay. The last assignment
is again a signal assignment ; signal C takes the value of D after a delta
time. Since D is “3” at zero time C is assigned to “3”.
This is why signal assignments should be avoided in processes. If we
define signal A as a variable B takes the value of “2” .
84
IF STATEMENTS
if condition then
{sequential_statement}
{elsif condition then
{sequential_statement}}
[else
{sequential_statement}]
end if ;
85
IF STATEMENTS
The final else is treated as “elsif TRUE then”, so if none of the conditions
before else clause are TRUE , then else statements will be executed.
86
IF STATEMENTS
Example :
end process ;
87
CASE STATEMENTS
The case statement selects, for execution, one of a number of alternative
sequences of statements. The chosen alternative is defined by the value of
an expression. The syntax is :
case expression is
when choices =>
{sequential_statement}}
{when choices =>
{sequential_statement}}
end case ;
Each choice must be of the same type as the expression. Each value must
be represented once and only once in the set of choices of the case
statement. If no others choice is presented, all possible values of the
expression must be covered by the set of choices. 88
CASE STATEMENTS
Example :
signal S1 : INTEGER range 0 to 7 ;
signal I1, I2, I3 : BIT ;
process (S1, I1, I2, I3)
begin
case S1 is
when 0 | 2 =>
OU <= ‘0’ ;
when 1 =>
OU <= I1 ;
when 3 to 5 =>
OU <= I2 ;
when others =>
OU <= I3 ;
end case ;
end process ;
89
NULL STATEMENTS
There is no action for a null statement in VHDL. The system will ignore
the null statement and proceed to the next statement. This statement is
usually used to explicitly state that no action is to be performed when a
condition is true. The syntax is :
null ;
Example :
variable A, B : INTEGER range 0 to 31 ;
case A is
when 0 to 12 =>
B := A ;
when others =>
null ;
end case ;
90
ASSERTION STATEMENTS
During simulation, it is convenient to output a text string message as a
warning or error message. The assert statement allows for testing a
condition and issuing a message. The assert statement checks to
determine if a specified condition is true, and displays a message if the
condition is false. The syntax is :
SEVERITY_LEVEL. There are four levels of severity : FAILURE,
ERROR, WARNING, NOTE. The severity level is used (in the simulator)
either to terminate a simulation run or just to give a warning message and
91
continue.
ASSERTION STATEMENTS
The assert statement is useful for timing checks, out-of-range conditions,
etc.
Example :
assert (false)
report “starting simulation” ;
92
LOOP STATEMENTS
A loop statement include a sequence of statements to be executed
repeatedly, zero or more times. The syntax is :
There are two different styles of the loop statement : FOR LOOP and
WHILE LOOP. These are called iteration schemes. You can also define
a loop without an iteration scheme which means repeated execution of the
statements. However in such a case you have to use a wait statement and
an exit statement.
93
LOOP STATEMENTS
Example : The following example shows two nested loops without an
iteration scheme (for or while).
count_down : process
variable min, sec : integer range 0 to 60 ;
begin
l1 : loop
l2 : loop
exit l2 when (sec = 0) ;
wait until CLK’event and CLK =‘1’ ;
sec := sec -1 ;
end loop l2 ;
exit l1 when (min = 0) ;
min := min - 1;
sec := 60 ;
end loop l1 ;
end process count_down ;
94
FOR LOOP STATEMENTS
A for loop is a sequential statement in a process that iterates over a
number of values. The loop index does not have to be declared, and it can
be reassigned a value within the loop. It is by default integer.
Example :
for i in 1 to 10 loop
a(i) := i * i ;
end loop ;
end loop ;
95
WHILE LOOP STATEMENTS
A while loop executes the loop body by first evaluating the condition. If
the condition is TRUE, then the loop is executed.
Example :
process
variable a, b, c, d : integer ;
begin
……….
while ((a + b) > (c+d)) loop
a := a-1 ;
c := c+b ;
b := b-d ;
end loop ;
…………
end process ;
96
NEXT STATEMENTS
The next statement skips execution to the next iteration of an enclosing
loop statement (called loop_label in the syntax). If the loop_label is
absent, the next statement applies to the innermost enclosing loop. The
syntax is :
Example :
l1 : while a < 10 loop
l2 : while b < 20 loop
.
next l1 when a = b ;
.
end loop l2 ;
end loop l1 ;
97
EXIT STATEMENTS
The exit statement completes the execution of an enclosing LOOP
statement (called loop_label in the syntax) and continues with the next
statement after the exited loop. If the loop_label is absent, the exit
statement applies to the innermost enclosing loop. The syntax is :
Example :
for a in 0 to 10 loop
exit when X(a) = 0 ;
Y(a) := X(a) ;
end loop ;
98
WAIT STATEMENTS
The exit statement causes a simulator to suspend execution of a process
statement or a subprogram, until some conditions are met. The objects
being waited upon should be signals. The syntax is :
wait
[on signal_name {, signal_name}]
[until boolean_expression]
[for time_expression];
Example :
wait on a, b ;
wait until x < 10 ;
wait for 10 us ;
wait on a,b until (x < 10) for 10 us ;
wait until (CLK’event and CLK = ‘1’) ; (waits for the rising edge of the CLK!!!) 99
PROCEDURE CALLS
100
RETURN STATEMENTS
Burcin PAK
101
CONCURRENT STATEMENTS
Concurrent statements are executed in parallel at the same simulated
time. It does not matter on the order they appear in the architecture.
Concurrent statements pass information through signals. The following
are the concurrent statements defined in VHDL :
Burcin PAK
• Concurrent ASSERTION statements
102
PROCESS STATEMENTS
A process is composed of a set of sequential statements, but processes are
themselves concurrent statements. All the processes in a design execute
concurrently. However, at any given time only one sequential statement is
executed within each process. A process communicates with the rest of a
design by reading or writing values to and from signals or ports declared
Burcin PAK
end process [label];
103
PROCESS STATEMENTS
A process_declaration_part defines objects that are local to the process,
and can have any of the following items :
• variable declaration
• constant declaration
Burcin PAK
We have already described sequential_statements.
104
PROCESS STATEMENTS
wait on sensitivity_list ;
Burcin PAK
105
PROCESS STATEMENTS
Example :
architecture A2 of example is
signal i1, i2, i3, i4, and_out, or_out : bit ;
begin
Burcin PAK
end process pr2;
end A2 ;
106
CONCURRENT SIGNAL
ASSIGNMENTS
Another form of a signal assignment is a concurrent signal assignment,
which is used outside of a process, but within an architecture. The syntax
is :
Any signal on the right side of the assignment is like a sensitivity list
Burcin PAK
element.
107
CONCURRENT SIGNAL
ASSIGNMENTS
Example : All the examples are equivalent.
architecture A3 of example is
architecture A1 of example is
signal i1, i2, i3, i4, and_out, or_out : bit ;
signal i1, i2, i3, i4, and_out, or_out : bit ;
begin
begin
process
and_out <= i1 and i2 and i3 and i4 ;
begin
or_out <= i1 or i2 or i3 or i4 ;
and_out <= i1 and i2 and i3 and i4 ;
end A1 ;
end A3 ;
architecture A2 of example is
signal i1, i2, i3, i4, and_out, or_out : bit ;
begin
process(i1, i2, i3, i4)
begin
and_out <= i1 and i2 and i3 and i4 ;
end process ;
Burcin PAK
process(i1, i2, i3, i4)
begin
or_out <= i1 or i2 or i3 or i4 ;
end process ;
end A2 ;
108
CONDITIONAL SIGNAL
ASSIGNMENTS
A conditional signal assignment is a concurrent statement and has one
target, but can have more than one expression. Except for the final
expression, each expression goes with a certain condition. The conditions
are evaluated sequentially. If one condition evaluates to TRUE, then the
corresponding expression is used ; otherwise the remaining expression is
used. One and only one expression is used at a time. The syntax is :
Burcin PAK
statement which contains an if statement.
architecture A1 of example is
signal a, b, c, d : integer ;
begin
a <= b when (d > 10) else
c when (d > 5) else
d;
architecture A2 of example is
signal a, b, c, d : integer ;
begin
process(b, c, d)
begin
if (d > 10) then
a <= b ;
Burcin PAK
elsif (d > 5) then
a <= c ;
else
a <= d ;
end if ;
end process ;
end A2 ;
110
SELECTED SIGNAL ASSIGNMENTS
A selective signal assignment can have only one target and can have only
one with expression. This value is tested for a match in a manner similar
to the case statement. It runs whenever any change occurs to the selected
signal. The syntax is :
Burcin PAK
You cannot use selected signal assignments in a process !!!
111
SELECTED SIGNAL ASSIGNMENTS
Example : The examples are equivalent.
with SEL select
z <= a when 0 | 1 | 2 ,
b when 3 to 10 ,
c when others ;
Burcin PAK
z <= c ;
end case ;
end process ;
112
BLOCK STATEMENTS
Blocks allow the designer to logically group sections of a concurrent
model, sections that are not scheduled to be used in other models (and
that’s when blocks are used instead of components). Blocks are used to
organise a set of concurrent statements hierarchically. The syntax is :
Burcin PAK
A block declarative part defines objects that are local to the block, and can
have any of the following items
113
BLOCK STATEMENTS
A block declarative part defines objects that are local to the block, and
can have any of the following items :
• signal declaration
• constant declaration
Burcin PAK
Objects declared in a block are visible to that block and all blocks nested
within. When a child block declares an object with the same name as the
114
one in the parent block, child’s declaration overrides the parent’s.
BLOCK STATEMENTS
Example : In the next example, block B1-1 is nested within block B1. Both
B1 and B1-1 declare a signal named S. the signal S used in the
block B1-1 will be the one declared within block B1-1, while
the S used in block B2 is the one declared in B1.
Burcin PAK
end block B1 ;
B2 : block
begin
out2 <= S ;
end block B2 ;
end BHV ; 115
CONCURRENT PROCEDURE CALLS
Burcin PAK
116
CONCURRENT PROCEDURE CALLS
Example : The examples are equivalent.
architecture ……
begin
procedure_any (a, b) ;
architecture ……
begin
process
begin
Burcin PAK
procedure_any (a, b) ;
wait on a, b ;
end process ;
end ….. ; 117
CONCURRENT ASSERTION
STATEMENTS
The concurrent assertion statement performs the same action and is used
for the same reason as the sequential assertion statements within a process.
This statement is used for simulation purpose only and will be ignored
Burcin PAK
118
SUBPROGRAMS
Burcin PAK
119
FUNCTIONS
Functions :
Burcin PAK
120
FUNCTIONS
The syntax is :
The identifier defines the name of the function, and the interface_list
Burcin PAK
defines the formal parameters of the function. Each parameter is defined
using the following syntax :
121
FUNCTIONS
[class] name_list [mode] type_name [:= expression] ;
where the class of a object refers to constant or signal and the mode of a
object must be in. If no mode is specified the parameter is interpreted as
mode in. If no class is specified, parameters are interpreted as class
Example :
process By default will be understood as :
function c_to_f (c : real) return real is
Burcin PAK
end c_to_f ;
Procedures :
Burcin PAK
123
PROCEDURES
The syntax is :
The identifier defines the name of the procedure, and the interface_list
Burcin PAK
defines the formal parameters of the procedure. Each parameter is defined
using the following syntax :
124
PROCEDURES
where the class of a object refers to constant, variable or signal and the
mode of a object may be in, out or inout. If no mode is specified the
Burcin PAK
125
PROCEDURES
Example :
procedure parity (A : in bit_vector (0 to 7) ; By default will be understood as :
result1, result2 : out bit) is
variable temp : bit ; variable result1, result2 : out bit ;
begin
temp := ‘0’ ;
for I in 0 to 7 loop
Burcin PAK
. Procedure call :
parity ( y(15 downto 8) , TOP, dummy) ; parity (x, y, z); (variable x, y : bit ; NOT signal x, y : bit;)
parity ( y(7 downto 0) , BOTTOM, dummy) ;
ODD := TOP xor BOTTOM ; parity (A => x, result1 => y, result2 => z) ;
end process ;
end BHV;
126
PACKAGES
You create a package to store common subprograms, data types,
constants, etc. that you can use in more than one design. A package
consists of two parts : a package declaration section and a package body.
The package declaration defines the interface for the package. The syntax
is :
Burcin PAK
127
PACKAGES
The package_declarative_item can be any of these :
• type declaration
• subtype declaration
• signal declaration
Burcin PAK
Signal declarations in a package pose some problems in synthesis
because a signal cannot be shared by two entities. A common solution is
to make it a global signal !!! 128
PACKAGES
The package body specifies the actual behaviour of the package. A
package body always has the same name as its corresponding package
declaration. The syntax is :
package body package_name is
Burcin PAK
• constant declaration
• use clause
• subprogram body 129
PACKAGES
library IEEE ;
use IEEE.NUMERIC_BIT.all ;
package PKG is
subtype MONTH_TYPE is integer range 0 to 12 ;
subtype DAY_TYPE is INTEGER range 0 to 31 ;
subtype BCD4_TYPE is unsigned (3 downto 0) ;
subtype BCD5_TYPE is unsigned (4 downto 0) ;
Burcin PAK
when ‘0’ => V := V1 ;
when ‘1’ => V := V2 ;
end case ;
return (V) ;
end BCD_INC ;
end PKG ;
130
MODELLING AT THE STRUCTURAL
LEVEL
A digital system is usually represented as a hierarchical collection of
components. Each component has a set of ports which communicate with
the other components. In a VHDL description, a design hierarchy is
introduced through component declarations and component instantiation
statements.
Both the process statements and the and the component instantiation
Burcin PAK
statements must be enclosed in an architecture body.
131
COMPONENT DECLARATIONS
An architecture body can use other entities described separately and
placed in the design libraries using component declaration and
component instantiation statements. In a design description, each
component declaration statement corresponds to an entity. The component
declaration statement is similar to the entity specification statement in that
component component_name
[ port ( local_port_declarations) ]
end component ;
Burcin PAK
component_name represents the name of the entity, and
port_declarations are the same as that defined for entity declaration. 132
COMPONENT INSTANTIATIONS
A component defined in an architecture may be instantiated using
component instantiation statement. At the point of instantiation, only the
external view of the component ( the name, type, direction of its ports) is
visible, signals internal to the component are not visible. The syntax is :
Burcin PAK
A component instantiation statement must be preceded by an
instantiation_label.
133
COMPONENT INSTANTIATIONS
Figure shows the interface and also the implementation of a full adder. In
this implementation, three types of components : OR2_gate, AND2_gate,
and XOR_gate are used to build the full adder circuit. The following
shows the entity and architecture specifications of the components used in
the full adder design.
library IEEE ;
use IEEE.STD_LOGIC_1164.all ;
entity XOR_gate is
port ( I0, I1 : in STD_LOGIC ; O : out STD_LOGIC );
end XOR_gate ;
architecture BHV of XOR_gate is
Burcin PAK
begin
O <= I0 xor I1 ;
end BHV ;
135
COMPONENT INSTANTIATIONS
library IEEE ;
use IEEE.STD_LOGIC_1164.all ;
entity FULL_ADDER is
port ( A, B, Cin : in STD_LOGIC ;
Sum, Cout : out STD_LOGIC );
end FULL_ADDER ;
architecture IMP of FULL_ADDER is
Burcin PAK
U1 : XOR_gate port map (I0 => A, I1 => B, O => N1 ) ;
U2 : AND2_gate port map ( A, B, N2 ) ;
U3 : AND2_gate port map ( Cin, N1, N3 ) ;
U4 : XOR_gate port map ( Cin, N1, Sum ) ;
U5 : OR2_gate port map ( N3, N2, Cout ) ;
end IMP ;
136
GENERATE STATEMENTS
The generate statement is a concurrent statement that has to be defined in
an architecture. It is used to describe replicated structures. The syntax is :
There are two kinds of generation_scheme : the for scheme and the if
scheme. A for scheme is used to describe a regular structure. It declares a
generate parameter and a discrete range just as the for_scheme which
Burcin PAK
defines a loop parameter and a discrete range in a sequential loop
statement. The generate parameter needs not to be declared. Its value
may be read but cannot be assigned or passed outside a generate
statement. 137
GENERATE STATEMENTS
Figure shows a four-bit adder which includes four FULL_ADDER
components.
Burcin PAK
end IMP ;
139
GENERATE STATEMENTS
Burcin PAK
140
GENERATE STATEMENTS
architecture IMP of FULL_ADDER4 is
signal X, Y, Z : STD_LOGIC_VECTOR ( 3 downto 0 ) ;
signal Cout : STD_LOGIC ;
signal TMP : STD_LOGIC_VECTOR ( 4 downto 1 ) ;
component FULL_ADDER
port ( A, B, Cin : in STD_LOGIC ;
Sum, Cout : out STD_LOGIC );
Burcin PAK
FA : FULL_ADDER port map ( X( I ), Y( I ), TMP ( I ), Z ( I ), TMP ( I + 1 ) ) ;
end generate ;
end generate ;
Cout <= TMP ( 4 ) ;
end IMP ;
141
CONFIGURATION SPECIFICATIONS
library IEEE ;
use IEEE.STD_LOGIC_1164.all ;
Burcin PAK
entity FULL_ADDER is
port ( A, B, Cin : in STD_LOGIC ;
Sum, Cout : out STD_LOGIC );
end FULL_ADDER ;
142
CONFIGURATION SPECIFICATIONS
architecture IMP of FULL_ADDER is
component XOR_gate
port ( I0, I1 : in STD_LOGIC ; O : out STD_LOGIC );
end component ;
component AND2_gate
port ( I0, I1 : in STD_LOGIC ; O : out STD_LOGIC );
end component ;
Burcin PAK
U2 : AND2_gate port map ( A, B, N2 ) ;
U3 : AND2_gate port map ( Cin, N1, N3 ) ;
U4 : XOR_gate port map ( Cin, N1, Sum ) ;
U5 : OR2_gate port map ( N3, N2, Cout ) ;
end IMP ;
143
MODELLING A TEST BENCH
To test a compiled VHDL design, a test bench is needed. A test bench
does not have external ports. To test our FULL_ADDER we can use the
following test bench :
Burcin PAK
signal A, B, Cin , Sum, Cout : STD_LOGIC ;
begin
U1 : FULL_ADDER port map ( A, B, Cin, Sum, Cout ) ;
144
MODELLING A TEST BENCH
Burcin PAK
You don’t need to use identifiers (U2, U3, U4) for signal assignments.
145
MODELLING AT THE RT LEVEL
A Register Transfer Level (RTL) design consists of a set of registers
connected by combinational logic as shown :
Burcin PAK
All the input signals must be listed in the sensitivity list !!!
147
LATCHES
Flip-flops and latches are two commonly used one-bit memory devices. A
flip-flop is an edge triggered memory device. A latch is a level sensitive
memory device. In general latches are synthesized from incompletely
specified conditional expressions in a combinational description. Any
signal or variable that is not driven under all conditions becomes a latched
Burcin PAK
if (S = ‘1’) then
Data_out <= Data_in ;
end if ;
end process ; 148
LATCHES
To avoid having a latch inferred, assign a value to all signal under all
conditions. Adding an else statement to the previous example will cause
the synthesizer to realize an and gate.
Burcin PAK
end process ;
149
LATCHES
We can specify a latch with an asynchronous reset or an asynchronous
preset.
Burcin PAK
Instead of Data_out <= ‘0’ assignment , we may assign ‘1’ for
asynchronous preset.
150
FLIP-FLOPS
A process with if signal leading edge (or falling edge) statements or wait
signal’event statements is called a clocked process. An edge triggered flip-
flop will be generated if a signal assignment is executed on the leading
edge ( or falling edge) of another signal.
Burcin PAK
end process ;
151
FLIP-FLOPS
Variables can also generate flip-flops. Since the variable is defined in the
process itself, and its value never leaves the process, the only time a
variable generates a flip-flop is when the variable is used before it is
assigned in a clocked process !!!
process (CLK) process (CLK)
Burcin PAK
3 flip-flops are generated. 1 flip-flop, 2 wires are
generated.
152
SYNCHRONOUS SETS AND RESETS
Synchronous inputs set (preset) or reset (clear) the output of flip-flops
while the clock edge is active. At all other times, changes on these inputs
are not noticed by the memory element.
Burcin PAK
Data_out <= Data_in ;
end if ;
Note that it does not matter whether or not signals Data_in
end if ; and S_RST are in the sensitivity list. Because their change
end process ; does not result in any action in the first if statement.
153
ASYNCHRONOUS SETS AND
RESETS
Asynchronous inputs set (preset) or reset (clear) the output of flip-flops
independently of the clock.
Burcin PAK
end if ;
end process ;
154
ASYNCHRONOUS SETS AND
RESETS
It is possible to describe a flip-flop with more than one asynchronous
inputs. In the following example we have 3 asynchronous inputs. Preset
has precedence than reset signal. If there is not a flip-flop with an enable
input in the library, then the generated circuit will be like the second one.
Burcin PAK
if ( EN = ‘1’ ) then
Data_out <= Data_in ;
end if ;
end if ;
end process ;
155
SYNCHRONOUS AND
COMBINATIONAL RTL CIRCUITS
We can divide the statements of an RTL process into several synchronous
and combinational sections.
Burcin PAK
sensitivity list.
156
SYNCHRONOUS AND
COMBINATIONAL RTL CIRCUITS
entity PULSER is
port ( CLK , PB : in bit ;
PB_PULSE : out bit ) ;
end PULSER ;
architecture BHV of PULSER is
Burcin PAK
end if ;
PB_PULSE <= (not Q1) nor Q2 ;
end process ;
end BHV ; 157
REGISTERS
Various types of registers are used in a circuit. The following example
shows a four-bit register which is asynchronously presets to “1100”.
Burcin PAK
end process ;
158
SHIFT REGISTERS
A register capable of shifting its binary information either to the right or to
the left is called a shift register. The logical configuration of a shift
register consists of a chain of flip-flops connected in cascade, with the
output of one flip-flop connected to the input of the next flip-flop. All flip-
flops receive a common clock pulse that causes the data to shift from one
Burcin PAK
REG := Din & REG (3 downto 1) ;
end if ;
Dout <= REG (0) ;
end process ; 159
ASYNCHRONOUS COUNTERS
An asynchronous counter is the one whose state changes are not
controlled by a synchronizing clock pulse.
signal CLK, RESET : bit ;
signal COUNT : bit_vector ( 3 downto 0) ;
process (CLK, COUNT, RESET)
begin
Burcin PAK
end if ;
if (COUNT(2)’event and COUNT(2) = ‘1’) then
COUNT (3) <= not COUNT (3) ;
end if ;
end if ;
end process ; 160
SYNCHRONOUS COUNTERS
If all the flip-flops of a counter are controlled by a common clock signal,
it is a synchronous counter..
signal CLK, RESET , load , Count, UpDown : bit ;
signal Datain : integer range 0 to 15 ;
signal Reg : integer range 0 to 15 := 0 ;
process (CLK, RESET)
Burcin PAK
Reg <= (Reg - 1) mod 16 ;
end if ;
end if ;
end if ;
end if ;
end process ; 161
TRI-STATE BUFFERS
Besides 0 and 1, there is a third signal value in digital systems : the high
impedance state (Z). Among the predefined types of package
STANDARD, there is no type to describe the high impedance value.
STD_LOGIC type must be used !!!
Burcin PAK
else
Dout <= Din ;
end if ;
end process ;
end IMP ;
162
BUSSES
The designer must guarantee no more than one buffer will be in the active
Burcin PAK
STD_LOGIC and STD_LOGIC_VACTOR can have multiple drivers.
163
BUSSES
library IEEE ;
use IEEE . STD_LOGIC_1164.all ;
entity BUS is
port ( S : in STD_LOGIC_VECTOR ( 1 downto 0) ;
OE : buffer STD_LOGIC_VECTOR ( 3 downto 0) ;
R0, R1, R2, R3 : in STD_LOGIC_VECTOR ( 7 downto 0) ;
Burcin PAK
when “10” => OE <= “0100” ;
when “11” => OE <= “1000” ;
when others => null ;
end case ;
end process ;
164
BUSSES
process ( S )
begin
case ( S ) is
when “00” => OE <= “0001” ;
when “01” => OE <= “0010” ;
when “10” => OE <= “0100” ;
Burcin PAK
165