VHDL Mansoor - Chapter4 - Operatots and Attributes and GENERIC
VHDL Mansoor - Chapter4 - Operatots and Attributes and GENERIC
2018 1
Chapter4: Operators and Attributes
2
Operators
• Assignment operators
• Logical operators
• Arithmetic operators
• Relational operators
• Shift operators
• Concatenation operators
3
Assignment Operators
<=
Signal <= value
:=
VARIABLE := value
CONSTANT:= value
GENERIC := value
=>
To assign values to individual vector elements or with OTHERS
SIGNAL w: STD_LOGIC_VECTOR(0 TO 7);
w <= "10000000“
w <= (0 =>'1', OTHERS =>'0') -- LSB is '1', the others are '0'
4
Logical Operators
5
Arithmetic Operators
Valid Data Types:
• INTEGER,
• SIGNED,
• UNSIGNED
• REAL (not synthesizable)
• std_logic_signed
• std_logic_unsigned
• STD_LOGIC_VECTOR (addition, subtraction)
+ Addition (Synthesizable)
- Subtraction (Synthesizable)
* Multiplication (Synthesizable)
/ Division only power of two dividers (shift operation) are allowed!
** Exponentiation
MOD Modulus --the remainder of y/x with the signal of x
REM Remainder --the remainder of y/x with the signal of y
ABS Absolute value
6
Comparison Operators
Valid Data Types:
• Any data type!
= Equal to
/= Not Equal to
< Less than
> Greater than
<= Less than or Equal
>= Greater than or Equal
7
Shift Operators
<left operand> shift operation <right operand>
Valid Data Types:
< BIT_VECTOR > shift operation < INTEGER >
9
Data Attributes
Pre-defined, synthesizable data attributes:
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
SIGNAL d : STD_LOGIC_VECTOR (7 DOWNTO 0);
10
Data Attributes Example
• Example: four equivalent LOOP statements for the signal x:
SIGNAL x: STD_LOGIC_VECTOR (0 TO 7);
11
Data Attributes…
• For enumerated data types:
12
Signal Attributes
Considering a signal s, then:
13
Signal Attributes Example
• Example: four equivalent assignments. They return TRUE when a
rising event (a change) occurs on clk.
WAIT UNTIL (clk'EVENT AND clk='1'); -- EVENT attribute used with WAIT
14
User-Defined Attributes
• User-defined attribute
• must be declared
• must be specified
• Attribute Declaration
• Attribute Specification
15
User-Defined Attribute example…
• Example:
16
User-Defined Attribute Example:
• Enumerated Encoding:
• enum_encoding attribute
17
Operator Overlaping:
• User-defined Operators
• Consider pre-defined arithmetic operators (+, -, *, /, etc.)
• Operating on certain data types (e.g. INTEGER)
• Not operating on BIT
• Specifying a new kind of addition (+) on type BIT_VECTORT
• Named as Operator Overloading
Example: Adding an integer to a binary 1-bit number
--Using the defined + operator
--Defining a new + operator
------------------------------
FUNCTION "+" (a: INTEGER, b: BIT) RETURN
SIGNAL inp1, outp: INTEGER RANGE 0 TO 15;
INTEGER IS
SIGNAL inp2: BIT;
BEGIN
(...)
IF (b='1') THEN RETURN a+1;
ELSE RETURN a; outp <= 3 + inp1 + inp2;
END IF; (...)
END "+"; ------------------------------
18
GENERIC:
• a way of specifying a generic parameter
• generic parameter:
• static
• modifiable
• adaptable to different applications
• more flexibility
• more reusability
• declaring GENERIC statement in the ENTITY
19
GENERIC Example:
• Example: ENTITY my_entity IS
• A generic parameter, INTEGER n=8 GENERIC (n : INTEGER := 8);
• When finding in the: PORT (...);
• ENTITY END my_entity;
• or the following ARCHITECTURES ARCHITECTURE my_architecture OF my_entity IS
• its value will be 8 ...
END my_architecture:
20
Examples…
• Providing some examples to illustrate use of:
• Operators
• Attributes
• GENERIC
• Still for establishing basic foundations of VHDL
• Some parts may be confusing
• After studying next chapters, return and read examples again
21
Example: generic m-by-n decoder
---------------------------------------------
LIBRARY ieee;
USE ieee.std_logic_1164.all;
---------------------------------------------
ENTITY decoder IS
PORT ( ena : IN STD_LOGIC;
sel : IN STD_LOGIC_VECTOR (2 DOWNTO 0); IF (ena='1') THEN
x : OUT STD_LOGIC_VECTOR (7 DOWNTO 0)); FOR i IN sel'RANGE LOOP -- sel range is 2 downto 0
END decoder; IF (sel(i)='1') THEN -- Bin-to-Integer conversion
--------------------------------------------- temp2:=2*temp2+1;
ARCHITECTURE generic_decoder OF decoder IS ELSE
BEGIN temp2 := 2*temp2;
PROCESS (ena, sel) END IF;
END LOOP;
VARIABLE temp1 : STD_LOGIC_VECTOR (x'HIGH DOWNTO 0); temp1(temp2):='0';
VARIABLE temp2 : INTEGER RANGE 0 TO x'HIGH; END IF;
BEGIN x <= temp1;
temp1 := (OTHERS => '1'); END PROCESS;
temp2 := 0; END generic_decoder;
22
Generic decoder simulation results
• In the example: m =3 and n =8
• the use of GENERIC would have made it clearer
• that m and n are indeed generic parameters.
23
Example: Generic Parity Detector
1 --------------------------------------------
2 ENTITY parity_det IS --Assign a value rather than n=7, then the code will work for
3 GENERIC (n : INTEGER := 7); --any other vector size!
4 PORT ( input: IN BIT_VECTOR (n DOWNTO 0);
5 output: OUT BIT);
6 END parity_det;
7 --------------------------------------------
8 ARCHITECTURE parity OF parity_det IS
9 BEGIN
10 PROCESS (input) Question:This Circuits is based on……
11 VARIABLE temp: BIT;
Even Parity /Odd Parity
12 BEGIN
13 temp := '0';
14 FOR i IN input'RANGE LOOP
15 temp := temp XOR input(i);
16 END LOOP;
17 output <= temp;
18 END PROCESS;
19 END parity;
20 -------------------------------------------- 24
Example 4.3: Generic Parity Generator
1 -----------------------------------------------
2 ENTITY parity_gen IS
3 GENERIC (n : INTEGER := 7);
4 PORT ( input: IN BIT_VECTOR (n-1 DOWNTO 0);
5 output: OUT BIT_VECTOR (n DOWNTO 0));
6 END parity_gen;
7 -----------------------------------------------
8 ARCHITECTURE parity OF parity_gen IS
9 BEGIN
10 PROCESS (input)
11 VARIABLE temp1: BIT;
12 VARIABLE temp2: BIT_VECTOR (output'RANGE); Question:This Circuits is based on……
13 BEGIN Even Parity /Odd Parity
14 temp1 := '0';
15 FOR i IN input'RANGE LOOP
16 temp1 := temp1 XOR input(i);
17 temp2(i) := input(i);
18 END LOOP;
19 temp2(output'HIGH) := temp1;
20 output <= temp2;
21 END PROCESS;
22 END parity;
23 ----------------------------------------------- 25
Summary: Operators
26
Summary: Attributes
27
Problems:
Considering the following declarations:
SIGNAL a : BIT := '1';
SIGNAL b : BIT_VECTOR (3 DOWNTO 0) := "1100";
SIGNAL c : BIT_VECTOR (3 DOWNTO 0) := "0010";
SIGNAL d : BIT_VECTOR (7 DOWNTO 0);
SIGNAL e : INTEGER RANGE 0 TO 255;
SIGNAL f : INTEGER RANGE -128 TO 127;
29
Problems:
3) Verify whether operations are legal/illegal. Briefly justify your answers. Considering the following declarations:
b(0) AND a -> legal/illegal why?________ SIGNAL a : BIT := '1';
a + d(7) -> legal/illegal why?________ SIGNAL b : BIT_VECTOR (3 DOWNTO 0) := "1100";
NOT b XNOR c -> legal/illegal why?________ SIGNAL c : BIT_VECTOR (3 DOWNTO 0) := "0010";
c+d -> legal/illegal why?________ SIGNAL d : BIT_VECTOR (7 DOWNTO 0);
e–f -> legal/illegal why?________ SIGNAL e : INTEGER RANGE 0 TO 255;
IF (b<c) ... -> legal/illegal why?________ SIGNAL f : INTEGER RANGE -128 TO 127;
IF (b>=a) ... -> legal/illegal why?________
IF (f/=e) ... -> legal/illegal why?________
IF (e>d) ... -> legal/illegal why?________
b sra 1 -> legal/illegal why?________
c srl -2 -> legal/illegal why?________
f ror 3 -> legal/illegal why?________
e*3 -> legal/illegal why?________
5**5 -> legal/illegal why?________
f/4 -> legal/illegal why?________
e/3 -> legal/illegal why?________
d <= c -> legal/illegal why?________
d(6 DOWNTO 3) := b -> legal/illegal why?________
e <= d -> legal/illegal why?________
f := 100 -> legal/illegal why?________
30
Problems:
4) Rewrite m-by-n decoder example code to make it truly generic.
Guides:
• Two values must be changed:
• the range of sel (line 7)
• the range of x (line 8)
• 4.a) Replace upper range limits of sel and x by an attribute which is a
function of n
• 4.b) Modify binary-to-integer conversion code to still work here!
• assuming that sel had been declared as an INTEGER
• Guide: the range of sel must be specified in terms of n
31