Professional Documents
Culture Documents
Chapter 3
Signals and Data Types
1
Objectives
• Object Classes and Object Types
• Signal Objects
• Scalar Types
• Type Std_Logic
• Scalar Literals and Scalar Constants
• Composite Types
• Arrays
• Types Unsigned and Signed
• Composite Literals and Composite Constants
• Integer Types
• Port Types for Synthesis
• Operators and Expressions
2
Object Classes
• An object’s class represents the nature of the object and
how it is used, for example, whether the object’s value
can be modified and whether it has a time element
associated with it.
• VHDL has four classes:
• Signal: an object with a current value and projected (future)
values. The projected values can be changed using signal
assignment statements.
• Constant: an object whose value cannot be changed after it
is initially specified.
• Variable: an object with only a current value. A variable’s
value can be changed using variable assignment statements.
• File: an object that consists of a sequence of values of a
specified type.
3
Object Types
6
Signal Objects
7
Scope and Visibility
• For each signal declared, VHDL language rules define a
certain region of text, called scope or namespace, where
the signal’s name can be used.
• A signal is said to be visible (accessible) within its
scope.
• Outside of its scope, a signal is unknown. Any attempt to
use a signal outside of its scope causes a compilation
error.
• A signal’s scope is determined by where it is declared.
• A signal can be declared in the declarative part of an
entity declaration, the declarative part of an architecture
body, or in a package declaration.
8
Port Visibility & Signal Visibility
• Port Visibility A port’s name is visible in the entity
declaration in which it is declared and in any
architecture bodies associated with that entity
declaration.
• A component declaration in the declarative part of
an architecture makes that component’s port signals
visible in that architecture.
• Signal Visibility A signal declared in the declarative
part of an architecture body can only be used within
that architecture; it is not visible outside of that
architecture.(information hiding)
9
Port Declaration
• Example:
port (x, y: in std_logic;
eq: out std_logic);
• or
port (x: in std_logic;
y: in std_logic;
eq: out std_logic);
10
1-bit Comparator Example
library ieee;
use ieee.std_logic_1164.all;
entity compare is
port (x, y: in std_logic;
eq: out std_logic);
end compare;
architecture dataflow of compare is
begin
eq <=(not x and not y) or (x and y);
end dataflow;
Since x, y, and eq are declared in the port clause for compare, these signals are
visible (can be used) in any architecture associated with compare
11
Signal Declaration
13
Scalar Types
14
Type Declaration
16
Enumeration Types
17
Enumeration Types
• Examples:
18
Enumeration Types
• Literal Overloading
20
Type Character
Signal x: bit;
23
Subtype Declarations
• A type derived from another type is a subtype. The type from
which the subtype is derived is its base type.
• Creating and using a subtype allows us to make it clear which
values from the base type are valid for an object.
subtype identifier is [ resolution_function_name ] type_mark [ constraint ] ;
27
Unresolved & Resolved Types
• Type std_ulogic is an unresolved type.
• It is illegal for two (or more) sources to drive the same signal if
that signal is of an unresolved type.
• A resolved type is a type declared with a resolution function. A
resolution function is a function that defines, for all possible
combinations of one or more source values, the resulting
(resolved) value of a signal.
28
Std_logic as a Resolved Subtype
• Std_logic is a subtype of
std_ulogic and is declared
in package
STD_LOGIC_1164 as:
subtype std_logic is resolved std_ulogic;
• Std_logic’s resolution
function resolved is defined
in package STD_LOGIC_
1164. In the case of
multiple sources for a
signal, the effect of the
multiple source values is
resolved to a single value.
29
Scalar Constants
• A constant is an object that is assigned a value only
once, normally when it is declared.
• The value of a constant cannot be changed in a
program. We can think of a constant as a literal
value with a name and type.
• Constants are used to make programs more
readable and to make it easier to change a value
throughout a program, by changing the value only in
the constant’s declaration.
constant identifier_list : subtype_indication := expression ;
constant high_impedance : std_logic := 'Z';
30
Composite Type
• A composite type consists of a collection of related elements that form
either an array or a record.
31
Array Types
• Array types group elements of the same type together
as a single object. Arrays can be one dimensional or
multidimensional.
• A one-dimensional array is also called a vector. It has an
index whose value selects an element in the array by its
position.
• An element of a multidimensional array is selected
using its indices.
• A one-dimensional array is commonly used to represent
a bus in a digital system; for example, the address bus,
data bus, or control bus of a computer.
• Even though a two-dimensional array is not supported
for synthesis, an array of arrays is supported.
32
Unconstrained Array
• Before an array object can be declared, its array type
must be declared using an array type declaration.
• One way to declare an array type is as an
unconstrained array.
• An unconstrained array (unbounded array) is an array
whose dimensions and indices types are specified when
the array type is declared, but the bounds for each
dimension are left unspecified.
33
Type Std_logic_vector
• Std_logic_vector is a one-dimensional unconstrained array of
elements of type std_logic that is defined in package
STD_LOGIC_1164 as:
type std_logic_vector is array ( natural range <> ) of std_logic;
• When we declare an object of an unconstrained array type, we must
specify a constraint for the index bounds.
• the index range can be either an ascending (integer to integer) or
descending (integer downto integer)For example:
35
Modeling Wires and Buses
SIGNAL a : STD_LOGIC;
a
1 wire
SIGNAL b : STD_LOGIC_VECTOR(7 DOWNTO 0);
b
8 bus
36
Constrained Array
37
Treating an Array
• We can treat an array as a single composite object
signal x, y : std_logic_vector (3 downto 0);
x <= y;
• Sometimes we want to deal with a single array element or a subset of
consecutive array elements.
x(3) <= '0';
x <= ('1','0','1','1');
x(3) <= '1';
x(2) <= '0'; named association
positional association
x(1) <= '1';
x(0) <= '1';
x <= (3 =>'1', 1 => '1', 0 => '1', 2 => '0');
38
Bit String Literals
• A bit string literal is a special form of string literal used to
represent a sequence of bit values as either binary,
octal, or hexadecimal numeric data.
• A bit string literal can contain any of the characters from
0 through 9, and a through f, or A through F.
• It must be prefixed by a B, O, or X to indicate binary,
octal, or hexadecimal base, respectively.
low_address <= B"01111001";
low_address <= X"79";
40
Array aggregate
43
Relational operators for array
44
Composite Constants
45
Types signed and unsigned
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
...
signal s1, s2, s3, s4, s5, s6: std_logic_vector(3 downto 0);
signal u1, u2, u3, u4, u6, u7: unsigned(3 downto 0);
signal sg: signed(3 downto 0);
50
Example Continue….
• Ok
u3 <= u2 + u1; --- ok, both operands unsigned
u4 <= u2 + 1; --- ok, operands unsigned and natural
• Wrong
u5 <= sg; -- type mismatch
u6 <= 5; -- type mismatch
• Fix
u5 <= unsigned(sg); -- type casting
u6 <= to_unsigned(5,4); -- conversion function
51
Example Continue….
• Wrong
u7 <= sg + u1; -- + undefined over the types
• Fix
u7 <= unsigned(sg) + u1; -- ok, but be careful
• Wrong
s3 <= u3; -- type mismatch
s4 <= 5; -- type mismatch
• Fix
s3 <= std_logic_vector(u3); -- type casting
s4 <= std_logic_vector(to_unsigned(5,4));
52
Example Continue….
• Wrong
s5 <= s2 + s1; + undefined over std_logic_vector
s6 <= s2 + 1; + undefined
• Fix
s5 <= std_logic_vector(unsigned(s2) + unsigned(s1));
s6 <= std_logic_vector(unsigned(s2) + 1);
53
Note
54
Integer Type
• An integer type has values that are whole numbers in a
specified range.
• Type integer is declared in package STANDARD
• There are two predefined type integer subtypes: natural and
positive are defined in package STANDARD.
• Integer literals can be represented as decimal literals or based
literals
• A decimal literal representing an integer can be simply a sequence
of digits or exponential notation can be used.
100 or 1E2 or 1e2
• A based literal uses an explicitly specified base between 2 and 16.
10#100# or 2#1100100# or 2#0110_0100# or 16#64#
55
Synthesis of an Unconstrained Integer
y <= to_unsigned(i,8);
x <= std_logic_vector(to_unsigned(i,8));
57
Homework#2
58
Finally!!
?
Any Question
59