ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 13.06.2025
Просмотров: 3465
Скачиваний: 2
Data Types |
45 |
Table P3.2
Dimension |
Legal or illegal |
||
Assignment |
(on each side) |
(why) |
|
a <= x(2); |
|||
b <= x(2); |
|||
b <= y(3,5); |
|||
b <= w(5)(3); |
|||
y(1)(0) <= z(7); |
|||
x(0) |
<= y(0,0); |
||
x <= |
"1110000"; |
||
a <= |
"0000000"; |
||
y(1) |
<= x; |
||
w(0) |
<= y; |
||
w(1) |
<= (7=>'1', OTHERS=>'0'); |
||
y(1) |
<= (0=>'0', OTHERS=>'1'); |
||
w(2)(7 DOWNTO 0) <= x;
w(0)(7 DOWNTO 6) <= z(5 DOWNTO 4); x(3) <= x(5 DOWNTO 5);
b <= x(5 DOWNTO 5);
y <= ((OTHERS=>'0'), (OTHERS=>'0'), (OTHERS=>'0'), "10000001");
z(6) <= x(5);
z(6 DOWNTO 4) <= x(5 DOWNTO 3); z(6 DOWNTO 4) <= y(5 DOWNTO 3); y(6 DOWNTO 4) <= z(3 TO 5); y(0, 7 DOWNTO 0) <= z;
w(2,2) <= '1';
TLFeBOOK
TLFeBOOK
4 Operators and Attributes
The purpose of this chapter, along with the preceding chapters, is to lay the basic foundations of VHDL, so in the next chapter we can start dealing with actual circuit designs. It is indeed impossible—or little productive, at least—to write any code efficiently without undertaking first the sacrifice of understanding data types, operators, and attributes well.
Operators and attributes constitute a relatively long list of general VHDL constructs, which are often examined only sparsely. We have collected them together in a specific chapter in order to provide a complete and more consistent view.
At the end of the chapter, a few design examples will be presented. However, due to the fact that this is still a ‘‘foundation’’ chapter, the examples are merely illustrative, like those in the preceding chapters. As mentioned above, we will start dealing with actual designs in chapter 5.
4.1Operators
VHDL provides several kinds of pre-defined operators:
Assignment operators
Logical operators
Arithmetic operators
Relational operators
Shift operators
Concatenation operators
Each of these categories is described below.
Assignment Operators
Are used to assign values to signals, variables, and constants. They are:
<¼ Used to assign a value to a SIGNAL.
:¼ Used to assign a value to a VARIABLE, CONSTANT, or GENERIC. Used also for establishing initial values.
¼> Used to assign values to individual vector elements or with OTHERS.
Example: Consider the following signal and variable declarations:
SIGNAL x : STD_LOGIC;
VARIABLE y : STD_LOGIC_VECTOR(3 DOWNTO 0); -- Leftmost bit is MSB
TLFeBOOK
48 |
Chapter 4 |
SIGNAL w: STD_LOGIC_VECTOR(0 TO 7); |
-- |
Rightmost bit is |
||
-- |
MSB |
|||
Then the following assignments are legal: |
||||
x <= '1'; |
-- '1' is assigned to SIGNAL x |
using "<=" |
||
y := "0000"; |
-- "0000" is assigned to VARIABLE y using ":=" |
|||
w <= "10000000"; |
-- LSB is '1', the others are '0' |
|||
w <= (0 =>'1', OTHERS =>'0'); |
-- LSB is '1', the others are '0' |
|||
Logical Operators
Used to perform logical operations. The data must be of type BIT, STD_LOGIC, or STD_ULOGIC (or, obviously, their respective extensions, BIT_VECTOR, STD_LOGIC_VECTOR, or STD_ULOGIC_VECTOR). The logical operators are:
NOT
AND
OR
NAND
NOR
XOR
XNOR
Notes: The NOT operator has precedence over the others. The XNOR operator was introduced in VHDL93.
Examples:
y <= NOT a AND b; |
-- (a'.b) |
|||
y |
<= NOT (a |
AND b); |
-- |
(a.b)' |
y |
<= a NAND |
b; |
-- |
(a.b)' |
Arithmetic Operators
Used to perform arithmetic operations. The data can be of type INTEGER, SIGNED, UNSIGNED, or REAL (recall that the last cannot be synthesized directly). Also, if the std_logic_signed or the std_logic_unsigned package of the ieee library is used, then STD_LOGIC_VECTOR can also be employed directly in addition and subtraction operations (as seen in section 3.6).
TLFeBOOK
Operators and Attributes |
49 |
þAddition
Subtraction
*Multiplication
/Division
** Exponentiation
MOD Modulus
REM Remainder
ABS Absolute value
There are no synthesis restrictions regarding addition and subtraction, and the same is generally true for multiplication. For division, only power of two dividers (shift operation) are allowed. For exponentiation, only static values of base and exponent are accepted. Regarding the mod and rem operators, y mod x returns the remainder of y/x with the signal of x, while y rem x returns the remainder of y/x with the signal of y. Finally, abs returns the absolute value. With respect to the last three operators (mod, rem, abs), there generally is little or no synthesis support.
Comparison Operators
Used for making comparisons. The data can be of any of the types listed above. The relational (comparison) operators are:
¼Equal to
=¼ Not equal to
<Less than
>Greater than
<¼ Less than or equal to
>¼ Greater than or equal to
Shift Operators
Used for shifting data. They were introduced in VHDL93. Their syntax is the following: 3left operand4 3shift operation4 3right operand4. The left operand must be of type BIT_VECTOR, while the right operand must be an INTEGER (þ or in front of it is accepted). The shift operators are:
sll |
Shift left logic |
– positions on the right are filled with ‘0’s |
|
srl |
Shift right logic |
– positions on the left are filled with ‘0’s |
TLFeBOOK
Operators and Attributes |
51 |
Data Attributes
The pre-defined, synthesizable data attributes are the following:
d’LOW: Returns lower array index
d’HIGH: Returns upper array index
d’LEFT: Returns leftmost array index
d’RIGHT: Returns rightmost array index
d’LENGTH: Returns vector size
d’RANGE: Returns vector range
d’REVERSE_RANGE: Returns vector range in reverse order
Example: Consider the following signal:
SIGNAL d : STD_LOGIC_VECTOR (7 DOWNTO 0);
Then:
d'LOW=0, d'HIGH=7, d'LEFT=7, d'RIGHT=0, d'LENGTH=8, d'RANGE=(7 downto 0), d'REVERSE_RANGE=(0 to 7).
Example: Consider the following signal:
SIGNAL x: STD_LOGIC_VECTOR (0 TO 7);
Then all four LOOP statements below are synthesizable and equivalent.
FOR i IN RANGE (0 TO 7) LOOP ...
FOR i IN x'RANGE LOOP ...
FOR i IN RANGE (x'LOW TO x'HIGH) LOOP ...
FOR i IN RANGE (0 TO x'LENGTH-1) LOOP ...
If the signal is of enumerated type, then:
d’VAL(pos): Returns value in the position specified
d’POS(value): Returns position of the value specified
d’LEFTOF(value): Returns value in the position to the left of the value specified
d’VAL(row, column): Returns value in the position specified; etc.
There is little or no synthesis support for enumerated data type attributes.
TLFeBOOK
52 |
Chapter 4 |
Signal Attributes
Let us consider a signal s. Then:
s’EVENT: Returns true when an event occurs on s
s’STABLE: Returns true if no event has occurred on s
s’ACTIVE: Returns true if s ¼ ‘1’
s’QUIET 3time4: Returns true if no event has occurred during the time specified
s’LAST_EVENT: Returns the time elapsed since last event
s’LAST_ACTIVE: Returns the time elapsed since last s ¼ ‘1’
s’LAST_VALUE: Returns the value of s before the last event; etc.
Though most signal attributes are for simulation purposes only, the first two in the list above are synthesizable, s’EVENT being the most often used of them all.
Example: All four assignments shown below are synthesizable and equivalent. They return TRUE when an event (a change) occurs on clk, AND if such event is upward (in other words, when a rising edge occurs on clk).
IF (clk'EVENT AND clk='1')... |
-- EVENT attribute used |
-- with IF |
|
IF (NOT clk'STABLE AND clk='1')... |
-- STABLE attribute used |
-- with IF |
|
WAIT UNTIL (clk'EVENT AND clk='1'); |
-- EVENT attribute used |
-- with WAIT |
|
IF RISING_EDGE(clk)... |
-- call to a function |
4.3User-Defined Attributes
We saw above attributes of the type HIGH, RANGE, EVENT, etc. Those are all pre-defined in VHDL87. However, VHDL also allows the construction of userdefined attributes.
To employ a user-defined attribute, it must be declared and specified. The syntax is the following:
Attribute declaration:
ATTRIBUTE attribute_name: attribute_type;
TLFeBOOK
Operators and Attributes |
53 |
Attribute specification:
ATTRIBUTE attribute_name OF target_name: class IS value;
where:
attribute_type: any data type (BIT, INTEGER, STD_LOGIC_VECTOR, etc.) class: TYPE, SIGNAL, FUNCTION, etc.
value: ‘0’, 27, ‘‘00 11 10 01’’, etc.
Example:
ATTRIBUTE |
number_of_inputs: INTEGER; |
-- |
declaration |
|
ATTRIBUTE |
number_of_inputs OF nand3: SIGNAL IS 3; -- |
specification |
||
... |
||||
inputs <= |
nand3'number_of_pins; |
-- attribute call, returns 3 |
||
Example: Enumerated encoding.
A popular user-defined attribute, which is provided by synthesis tool vendors, is the enum_encoding attribute. By default, enumerated data types are encoded sequentially. Thus, if we consider the enumerated data type color shown below:
TYPE color IS (red, green, blue, white);
its states will be encoded as red ¼ ‘‘00’’, green ¼ ‘‘01’’, blue ¼ ‘‘10’’, and white ¼ ‘‘11’’. Enum_encoding allows the default encoding (sequential) to be changed. Thus the following encoding scheme could be employed, for example:
ATTRIBUTE enum_encoding OF color: TYPE IS "11 00 10 01";
A user-defined attribute can be declared anywhere, except in a PACKAGE BODY. When not recognized by the synthesis tool, it is simply ignored, or a warning is issued.
4.4Operator Overloading
We have just seen that attributes can be user-defined. The same is true for operators. As an example, let us consider the pre-defined arithmetic operators seen in section 4.1 (þ, , *, /, etc.). They specify arithmetic operations between data of certain types
TLFeBOOK
54 |
Chapter 4 |
(INTEGER, for example). For instance, the pre-defined ‘‘þ’’ operator does not allow addition between data of type BIT.
We can define our own operators, using the same name as the pre-defined ones. For example, we could use ‘‘þ’’ to indicate a new kind of addition, this time between values of type BIT_VECTOR. This technique is called operator overloading.
Example: Consider that we want to add an integer to a binary 1-bit number. Then the following FUNCTION could be used (details on how to construct and use a FUNCTION will be seen in chapter 11):
--------------------------------------
FUNCTION "+" (a: INTEGER, b: BIT) RETURN INTEGER IS
BEGIN
IF (b='1') THEN RETURN a+1;
ELSE RETURN a;
END IF;
END "+";
--------------------------------------
A call to the function above could thus be the following:
------------------------------
SIGNAL inp1, outp: INTEGER RANGE 0 TO 15;
SIGNAL inp2: BIT;
(...)
outp <= 3 + inp1 + inp2; (...)
------------------------------
In ‘‘outp<=3+inp1+inp2;’’, the first ‘‘+’’ is the pre-defined addition operator (adds two integers), while the second is the overloaded user-defined addition operator (adds an integer and a bit).
4.5 GENERIC
As the name suggests, GENERIC is a way of specifying a generic parameter (that is, a static parameter that can be easily modified and adapted to di¤erent applications). The purpose is to confer the code more flexibility and reusability.
A GENERIC statement, when employed, must be declared in the ENTITY. The specified parameter will then be truly global (that is, visible to the whole design, including the ENTITY itself ). Its syntax is shown below.
TLFeBOOK
Operators and Attributes |
55 |
GENERIC (parameter_name : parameter_type := parameter_value);
Example: The GENERIC statement below specifies a parameter called n, of type INTEGER, whose default value is 8. Therefore, whenever n is found in the ENTITY itself or in the ARCHITECTURE (one or more) that follows, its value will be assumed to be 8.
ENTITY my_entity IS
GENERIC (n : INTEGER := 8);
PORT (...);
END my_entity;
ARCHITECTURE my_architecture OF my_entity IS
...
END my_architecture:
More than one GENERIC parameter can be specified in an ENTITY. For example:
GENERIC (n: INTEGER := 8; vector: BIT_VECTOR := "00001111");
Complete design examples, further illustrating the use of GENERIC and other attributes and operators, are presented below.
4.6Examples
We show now a few complete design examples, with the purpose of further illustrating the use of operators, attributes and GENERIC. Recall, however, that so far we have just worked on establishing the basic foundations of VHDL, with the formal discussion on coding techniques starting only in the next chapter (chapter 5). Therefore, a first-time VHDL student should not feel discouraged if the constructs in the examples look still unfamiliar. Instead, you may have a look at the examples now, and then, after studying chapters 5 to 7, return and reexamine them.
Example 4.1: Generic Decoder
Figure 4.1 shows the top-level diagram of a generic m-by-n decoder. The circuit has two inputs, sel (m bits) and ena (single bit), and one output, x (n bits). We assume that n is a power of two, so m ¼ log2n. If ena ¼ ‘0’, then all bits of x should be high;
TLFeBOOK