Professional Documents
Culture Documents
Packages
1
VHDL – A Strongly typed language
• VHDL is a strongly typed language. This means that it is hard
(sometimes annoyingly hard) to get a construct through the compile
stage. The types must be absolutely correct to be able to simulate it.
– Even if the types are correct, you might find that the simulation doesn’t
work properly. Some types or bit ranges are only checked during
simulation.
– Once the compiler and simulator stops complaining, it will be correct by
construction.
Types
Access Composite
Array Record
Scalar
Integer Physical
Real Enumerated
4
Scalar Types - Integer
• Integer
– Minimum range for any implementation as defined by standard:
- 2,147,483,647 to 2,147,483,647
– Integer assignment example
5
Scalar Types - Real
• Real
– Minimum range for any implementation as defined by standard:
- 1.0E38 to 1.0E38
– Real assignment example
8
Enumerated IEEE data types:
std_logic
Type std_ulogic is ('U', 'X', '0', '1', 'Z', 'W', 'L', 'H', '-');
'U' -- Uninitialized
'X' -- Forcing unknown
'0' -- Forcing zero
'1' -- Forcing one
'Z' -- High Impedance
'W' -- Weak Unknown
'L' -- Weak Low
'H' -- Weak High
'-' -- Don’t Care
9
Testing all combinations
in an enumerated type
type mvl7 is ('X', '0', '1', 'Z', 'W', 'L', 'H');
signal a:mvl7;
…
process
begin
for i in mvl7 loop -- loop variable i becomes of mvl7 type
a <= i; -- a must be declared as mvl7
wait 10 ns;
end loop;
end process;
• Physical
– Can be user defined range
– Physical type example
11
Scalar Types - Time
12
Composite Types - Array (ascending)
• Array
– Used to collect one or more elements of a similar type in a single
construct
– Elements can be any VHDL data type
– Sample one-dimensional array (vector)
0...element numbers... 31
0 ...array values... 1
VARIABLE X: data_bus;
VARIABLE Y: BIT;
15...element numbers... 0
0 ...array values... 1
VARIABLE X: register;
VARIABLE Y: BIT;
9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9
<=
<=
15
Composite Types - Generic Array Ranges
Generic parameter
16
Array Standard Types:
Bit_vector and String
type Bit is ('0', '1');
type Bit_vector is array(integer range <>) of bit;
17
Array IEEE data types:
std_logic_vector
Type std_ulogic is ('U', 'X', '0', '1', 'Z', 'W', 'L', 'H', '-');
type std_logic is resolved std_ulogic; -- resolution function call
type std_logic_vector is array (integer range <>) of std_logic;
18
Composite Types - Record
• Records
– Used to collect one or more elements of different types in a single
construct
– Elements can be of any VHDL data type
– Elements are accessed through field name
– Sample record statement
TYPE binary IS ( ON, OFF );
TYPE switch_info IS
RECORD
status : binary;
IDnumber : integer;
END RECORD;
• Access
– Similar to pointers in other languages
– Allows for dynamic allocation of storage (=c.f. malloc in C)
– Useful for simulating queues, fifos, etc. Used for accessing the
file system (see the LINE type, which is an access string type).
count:=new natural;
count.ALL=10;
deallocate(count);
20
Access Standard Types: TEXTIO
• The VHDL file system is defined in the package
TEXTIO in the STD library.
– It uses an access type to write strings to the file
– Access types are not synthesizable
– We will talk more about the file system in a later lecture
21
Example: Dynamic Pointers
22
Example: Dynamic Pointers (ctd.)
23
Subtype
• Subtype
– Allows for user defined constraints on a data type
– May include entire range of base type
– Assignments that are out of the subtype range result in error
– Subtype example
24
Potential Problems (subtype)
• Objects defined by subtypes derived from the same base type are
considered being of the same type
– Example
PROCESS
SUBTYPE smallintA IS integer (RANGE 0 TO 10);
SUBTYPE smallintB IS integer (RANGE 0 TO 15);
VARIABLE A: smallintA := 5;
VARIABLE B: smallintB := 8;
VARIABLE C: integer;
VARIABLE D: smallintB;
BEGIN
C := B * A; -- OK
B := B+1; -- OK, but will eventually overflow
D := B * A; -- OK, but will result in an overflow
END;
25
Natural and Positive Integers
26
Attributes
• Attributes are used to retrieve information about
the object associated with it
• Syntax:
<object>’<attribute_name>
27
Type Attributes
Integer’high = 2,147,483,647
Integer’low = -2,147,483,647
Bit’left = '0'
Bit’right = '1'
28
Array Type attributes
a_type’range = 7 downto 0
a_type’reversed_range = 0 to 7
a_type’length = 8
29
Multi-dimensional Vector & Array assignments
regs(2)<=regs(0)+regs(1);
30
Alias Statement
op1<="0000";
op2<="0001"; instruction 31..24 23..20 19..16 15..12 11..0
op3<="0010"; instr op1 op2 op3 offset
b<="000000010010";
b<=B"000000010010";
b<=B"0000_0001_0010";
b<=X"012"; -- Hexadecimal assignment
b<=O"0022"; -- Octal Assignment
32
Aggregates
33
Aggregates (ctd.)
signal rec:rec_type:=(1,1.0,"1111");
34
LHS Aggregates
Signal a,b,cin,cout,sum:std_logic;
Process(a,b,cin)
variable carry:std_logic;
begin
carry:=(a AND b) OR (a AND cin) OR (b AND cin);
(cout,sum)<=carry & (a XOR b XOR cin);
End process;
Left bit Right bit
35
Type Conversion
36
Type conversion (Similar Base)
signal i:integer;
signal r:real;
i<=integer(r);
r<=real(i);
37
Type conversion (Same Base)
signal i:integer;
signal b:bit_vector(3 downto 0)
i<=bits2int(b);
b<=int2bits(i,4);
39
Example: Integer to Bits and vice
versa
package my_package is
Function int2bits(value:integer;ret_size:integer) return bit_vector;
Function bits2int(value:bit_vector) return integer;
end my_package;
40
Example: Integer to Bits
package body my_package is
Function int2bits(value:integer;ret_size:integer) return bit_vector is
variable res:bit_vector(ret_size-1 downto 0);
variable tmp:integer;
begin
tmp:=value;
for i in 0 to ret_size-1 loop
if (tmp mod 2=1) then
res(i):=‘1’;
tmp:=tmp-1;
else
res(i):=‘0’;
end if;
tmp:=tmp/2;
end loop;
return res;
end int2bits;
41
Example: Bits to Integer
43
VHDL Hierarchy
Package
Concurrent Process
Concurrent
Statements
Statements
44 Sequential Statements
VHDL Packages
• Packages encapsulate elements that can be globally shared among
two or more design units
• A package consists of two parts
Definition of all
elements contained
in the package
45
Packages
• What can you put in a package?
– Subprograms (i.e., functions and procedures)
– Data and type declarations such as
• User record definitions
• User types and enumerated types
• Constants
• Files
• Aliases
• Attributes
– Component declarations
• Entities and Architectures cannot be declared or defined in a
package
• To use a package, make it visible via the “use” language construct
46
Subprograms
47
Functions
• Produce a single return value
• Called by expressions
• Can not modify the parameters passed to it
• Requires a RETURN statement
48
Functions
49
Procedures
• Produce many output values
• Are invoked by statements
• May modify the parameters
END add_bits3;
Name of procedure
52
Procedure calling parameters
53
Operators
54
Built-in Operators
• Logical operators
– AND, OR, NAND, NOR, XOR, XNOR (XNOR in VHDL’93 on only !!!)
• Relational operators
– =, /=, <, <=, >, >=
• Addition operators
– +, -, &
• Multiplication operators
– *, /, mod, rem
• Miscellaneous operators
– **, abs, not
55
Synthesis of Logical Operators
...
signal A, B, C: BIT_VECTOR(3 downto 0);
signal D, E, F, G: BIT_VECTOR(1 downto 0);
signal H, I, J, K: BIT;
signal L, M, N, O, P: BOOLEAN;
...
A <= B and C;
D <= E or F or G;
H <= (I nand J) nand K;
L <= (M xor N) and (O xor P);
56
Synthesis of relational operators
signal A, B: BIT_VECTOR(3 downto 0);
signal C, D: BIT_VECTOR(1 downto 0);
signal E, F, G, H, I, J: BOOLEAN;
G <= (A = B);
H <= (C < D);
I <= (C >= D);
J <= (E > F);
57
Miscellaneous Operators
• The concatenation operator &
VARIABLE shifted, shiftin : BIT_VECTOR (0 TO 3);
...
shifted := shiftin(1 TO 3) & '0';
0 1 2 3
SHIFTIN 1 0 0 1
SHIFTED 0 0 1 0
58
Operator overloading
• If one of the data types in the operator is not supported, the operator
has to be overloaded.
59
Operator overloading (ctd.)
Most definitions that ever will be needed are defined in the different IEEE
std_logic packages!!!
60
Potential problems (overloaded functions)
61
Resolving Difficulties
• Overloaded items cannot be resolved if the argument types include
common literals, i.e.,
TYPE twobit IS ('0', '1');
TYPE fourbit IS ('U', '0', '1', 'Z');
FUNCTION abc (x: twobit) RETURN INTEGER;
FUNCTION abc (x: fourbit) RETURN INTEGER;
....
y <= abc('0'); -- Which function do we use?
use ieee.std_logic_arith.all; -- defines the types signed and unsigned (Synopsys std)
use ieee.numeric_std.all; -- alternative for declaring signed and unsigned (IEEE std)
63
Floating Point Added in VHDL-2008
64
New subtypes
• VHDL-2008 introduces overloads of the
std_logic_vector to support synthesis of IEEE
floating and fixed point implementations:
65
Floating-Point
66
IEEE-754
67
Floating-Point Numbers (IEEE)
• Exponent Values 1 to 254: normalized non-zero floating-point
numbers; biased exponent (-126...+127)
• Exponent of zero and fraction of zero: positive or negative zero
• Exponent of ones and fraction of zero: positive or negative
infinity
• Exponent of ones with a non-zero fraction: NotANumber (NAN -
Exception Condition)
• Exponent of zero and fraction of non-zero: Denormalized
number (true exponent is –126), represent numbers from
0 to 2-126
FlP(B) = (-1)s * (0.m) * 2-126
• There is also a standard for a 64-bit numbers, and 128-bit
numbers (IEEE 874)
68
VHDL-2008: Overloaded operators
• Implementation of
– Relational operators
– Logical operators
– Arithmetic operators
• Yes, it is now possible to synthesize mod, rem and /...
69
Restrictions
• The std_logic additions for supporting floating
point operators are synthesizable, but
– Excellent for modelling
– Not so good for synthesis
• Pipelining does not seem to work properly
70