VHDL Data Types and Operators
VHDL Data Types and Operators
2009 O. Adekunle
!! !!
2009 O. Adekunle
!! !!
!! What main example did we use? D flip-flop !! What are 3 important sections of VHDL code structure? !! Library, Entity, and Architecture !! In the section that you implement the behavior, is the default flow sequential or concurrent? Concurrent
2009 O. Adekunle
2009 O. Adekunle
2009 O. Adekunle
2009 O. Adekunle
Data types BIT, BIT_VECTOR STD_LOGIC, STD_LOGIC_VECTOR BOOLEAN INTEGER SIGNED UNSIGNED User-dened integer type User-dened enumerated type SUBTYPE
Synthesizable values 0, 1 X, 0, 1, Z (resolved) True, False From -2,147,483,647 to +2,147,483,647 From -2,147,483,647 to -2,147,483,647 (binary rep). From 0 to +2,147,483,647 (binary rep). Subset of INTEGER Collection enumerated by user Subset of any type ( pre- or userdened)
2009 O. Adekunle
Data types BIT, BIT_VECTOR STD_LOGIC, STD_LOGIC_VECTOR BOOLEAN INTEGER SIGNED UNSIGNED User-dened integer type User-dened enumerated type SUBTYPE
Synthesizable values 0, 1 X, 0, 1, Z (resolved) True, False From -2,147,483,647 to +2,147,483,647 From -2,147,483,647 to -2,147,483,647 (binary rep). From 0 to +2,147,483,647 (binary rep). Subset of INTEGER Collection enumerated by user Subset of any type ( pre- or userdened) Predefined
2009 O. Adekunle
They are parts of packages/libraries !! Package standard of library std: Defines BIT, BOOLEAN, and INTEGER data types !! Do you remember how you would include this in your VHDL code?
!!
2009 O. Adekunle
10
They are parts of packages/libraries !! Package standard of library std: Defines BIT, BOOLEAN, and INTEGER data types. !! Do you remember how you would include this in your VHDL code? !! LIBRARY STD; !! USE STD.STANDARD.ALL;
!!
2009 O. Adekunle
11
They are parts of packages/libraries !! Package standard of library std: Defines BIT, BOOLEAN, and INTEGER data types. !! Do you remember how you would include this in your VHDL code? !! LIBRARY STD; !! USE STD.STANDARD.ALL;
!!
2009 O. Adekunle
12
!!
2009 O. Adekunle
13
!!
SIGNAL x: BIT; -- x is declared as a one-digit signal of type BIT. SIGNAL y: BIT_VECTOR (3 DOWNTO 0); -- y is a 4-bit vector, w/ the leftmost bit as MSB. SIGNAL w: BIT_VECTOR (0 TO 7); -- w is an 8-bit vector, w/ the rightmost bit as MSB. IMPORTANT: To assign a value to these signals you use <= operator
2009 O. Adekunle
14
!!
SIGNAL x: BIT; -- x is declared as a one-digit signal of type BIT. SIGNAL y: BIT_VECTOR (3 DOWNTO 0); -- y is a 4-bit vector, w/ the leftmost bit as MSB. SIGNAL w: BIT_VECTOR (0 TO 7); -- w is an 8-bit vector, w/ the rightmost bit as MSB. x <= '1'; -- x is assigned value 1. Single quotes (' ') -- are used for a single bit.
2009 O. Adekunle 15
!!
SIGNAL y: BIT_VECTOR (3 DOWNTO 0); -- y is a 4-bit vector, w/ the leftmost bit as MSB. SIGNAL w: BIT_VECTOR (0 TO 7); -- w is an 8-bit vector, w/ the rightmost bit as MSB. x <= '1'; -- x is assigned value 1. Single quotes (' ') -- are used for a single bit. y <= "0111"; -- y is assigned "0111 (MSB='0'). Double -- quotes (" ") are used for vectors.
2009 O. Adekunle 16
!!
SIGNAL w: BIT_VECTOR (0 TO 7); -- w is an 8-bit vector, w/ the rightmost bit as MSB. x <= '1'; -- x is assigned value 1. Single quotes (' ') -- are used for a single bit. y <= "0111"; -- y is assigned "0111 (MSB='0'). Double -- quotes (" ") are used for vectors. w <= "01110001"; -- w is assigned "01110001 (MSB='1').
2009 O. Adekunle
17
!!
x <= '1'; -- x is assigned value 1. Single quotes (' ') -- are used for a single bit. y <= "0111"; -- y is assigned "0111 (MSB='0'). Double -- quotes (" ") are used for vectors. w <= "01110001"; -- w is assigned "01110001 (MSB='1').
2009 O. Adekunle
18
2009 O. Adekunle
19
2009 O. Adekunle
20
Package STD_LOGIC_1164 of library IEEE: Defines STD_LOGIC !! STD_LOGIC similar to BIT and what you will be using most in course
!!
Value X 0 1 Z W L H Definition Forcing Unknown (synthesizable unknown) Forcing Low Forcing High High impedance Weak unknown Weak low Weak high Dont care
2009 O. Adekunle 21
Synthesizable
Package STD_LOGIC_1164 of library IEEE: Defines STD_LOGIC !! STD_LOGIC similar to BIT and what you will be using most in course
!!
Value X 0 1 Z W L H Definition Forcing Unknown (synthesizable unknown) Forcing Low Forcing High High impedance Weak unknown Weak low Weak high Dont care
2009 O. Adekunle 22
Synthesizable
!!
2009 O. Adekunle
23
!!
2009 O. Adekunle
24
SIGNAL a: BIT; SIGNAL b: BIT_VECTOR(7 DOWNTO 0); SIGNAL c: STD_LOGIC; SIGNAL d: STD_LOGIC_VECTOR(7 DOWNTO 0); SIGNAL e: INTEGER RANGE 0 TO 255; -- Assignments across data types are illegal. a <= b(5); -b(0) <= a; -c <= d(5); -d(0) <= c; -a <= c; -b <= d; -e <= b; e <= d; -- 2009 O. Adekunle 25
SIGNAL a: BIT; SIGNAL b: BIT_VECTOR(7 DOWNTO 0); SIGNAL c: STD_LOGIC; SIGNAL d: STD_LOGIC_VECTOR(7 DOWNTO 0); SIGNAL e: INTEGER RANGE 0 TO 255; -- Assignments across data types are illegal a <= b(5); -- legal (same scalar type: BIT) b(0) <= a; -- legal (same scalar type: BIT) c <= d(5); -- legal (same scalar type: STD_LOGIC) d(0) <= c; -- legal (same scalar type: STD_LOGIC) a <= c; -- illegal (type mismatch: BIT x STD_LOGIC) b <= d; -- illegal (type mismatch: BIT_VECTOR x -- STD_LOGIC_VECTOR) e <= b; -- illegal (type mismatch: INTEGER x BIT_VECTOR) e <= d; -- illegal (type mismatch: INTEGER x -- STD_LOGIC_VECTOR)
2009 O. Adekunle
26
2009 O. Adekunle
27
!!
!!
2009 O. Adekunle
28
!!
Integers Ex:
!! !! !! !!
TYPE my_integer IS RANGE -32 TO 32; -- A user-defined subset of integers. TYPE student_grade IS RANGE 0 TO 100; -- A user-defined subset of integers or naturals.
!!
Enumerated EX:
!! TYPE state IS (idle, forward, backward, stop); !! -- An enumerated data type, typical of finite state machines. !! TYPE color IS (red, green, blue, white); !! -- Another enumerated data type.
2009 O. Adekunle
29
2009 O. Adekunle
30
Any type with the addition of a constraint !! Main reason for subtypes
!!
!!
Examples:
!! Operations between different types arent allowed !! Operations between type and subtype are allowed
SUBTYPE my_logic IS STD_LOGIC RANGE '0' TO '1'; SIGNAL a: BIT; SIGNAL b: STD_LOGIC; SIGNAL c: my_logic; ... b <= a; -b <= c; - 2009 O. Adekunle 31
Any type with the addition of a constraint !! Main reason for subtypes
!!
!!
Examples:
!! Operations between different types arent allowed !! Operations between type and subtype are allowed
SUBTYPE my_logic IS STD_LOGIC RANGE '0' TO '1'; SIGNAL a: BIT; SIGNAL b: STD_LOGIC; SIGNAL c: my_logic; ... b <= a; -- illegal (type mismatch: BIT versus STD_LOGIC) b <= c; -- legal (same "base" type: STD_LOGIC)
2009 O. Adekunle 32
2009 O. Adekunle
33
!!
Some operators do different things in different contexts EX: <= is used as less than or equal too
!! IF (a <= b) THEN
!!
!!
!!
2009 O. Adekunle
34
2009 O. Adekunle
35
Are used to assign values to signals, variables, and constants !! Three assignment operators:
!!
!! <= 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
!!
!!
!! SIGNAL x : STD_LOGIC; !! VARIABLE y : STD_LOGIC_VECTOR(3 DOWNTO 0); -Leftmost bit is MSB !! SIGNAL w: STD_LOGIC_VECTOR(0 TO 7); -- Rightmost bit is -- MSB !! !! !! !! !! !! !! x <= '1'; -y := "0000"; -w <= "10000000"; w <= (0 =>'1', OTHERS =>'0'); - 2009 O. Adekunle 37
!!
!! SIGNAL x : STD_LOGIC; !! VARIABLE y : STD_LOGIC_VECTOR(3 DOWNTO 0); -Leftmost bit is MSB !! SIGNAL w: STD_LOGIC_VECTOR(0 TO 7); -- Rightmost bit is -- MSB !! !! !! !! !! !! !! x <= '1'; -- '1' is assigned to SIGNAL x using "<=" y := "0000"; -- "0000" is assigned to VARIABLE y using ":=" w <= "10000000"; w <= (0 =>'1', OTHERS =>'0'); -- LSB is '1', the others are '0'
2009 O. Adekunle 38
2009 O. Adekunle
39
!!
Main thing to keep in mind is that the NOT operator has precedence over all other operators !! Examples:
!!
!! y <= NOT a AND b; -- (a'.b) !! y <= NOT (a AND b); -- (a.b) !! y <= a NAND b; -- (a.b)'
2009 O. Adekunle
40
2009 O. Adekunle
41
Arithmetic can be performed on INTEGER, SIGNED, and UNSIGNED !! 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
!!
2009 O. Adekunle
42
!!
Again the operators are similar to what you are used too
!! + Addition !! - Subtraction !! * Multiplication !! / Division !! ** Exponentiation !! MOD Modulus !! REM Remainder !! ABS Absolute value
2009 O. Adekunle
43
2009 O. Adekunle
44
Used for making comparisons !! Similar to what you are used to !! Can be only used within IF and WAIT statements !! = Equal, !! /= Not equal to, !! < Less than, !! > Greater than, !! <= Less than or equal to, !! >= Greater than or equal to
!!
2009 O. Adekunle 45
!!
Code example
SIGNAL counter : INTEGER; SIGNAL clock: INTEGER; PROCESS (counter) BEGIN IF (counter <= 10) THEN clock <= 0; ELSIF (clock <= 20) THEN clock <= 1; END IF; END PROCESS;
----
2009 O. Adekunle
46
!!
Code example
-- Signals entity port definitions SIGNAL counter : INTEGER; -- counts from 1 to 20 then resets SIGNAL clock: INTEGER; -- Excerpt of code PROCESS (counter) BEGIN IF (counter <= 10) THEN clock <= 0; ELSIF (counter > 10) THEN clock <= 1; END IF; END PROCESS;
2009 O. Adekunle
---47
!!
Code example
-- Signals entity port definitions SIGNAL counter : INTEGER; -- counts from 1 to 20 then resets SIGNAL clock: INTEGER; -- Excerpt of code PROCESS (counter) BEGIN IF (counter <= 10) THEN equal to clock <= 0; ELSIF (counter > 10) THEN clock <= 1; END IF; END PROCESS;
2009 O. Adekunle
2009 O. Adekunle
49
!! dLOW: Returns lower array index !! dHIGH: Returns upper array index !! dLEFT: Returns leftmost array index !! dRIGHT: Returns rightmost array index !! dLENGTH: Returns vector size !! dRANGE: Returns vector range !! dREVERSE_RANGE: Returns vector range in reverse order
2009 O. Adekunle
50
!!
If given: Then:
!!
2009 O. Adekunle
51
!!
If given: Then:
!!
--lower array index --high array index --leftmost array index --rightmost array index --size
2009 O. Adekunle
52
!!
Or if given:
!!
RANGE (0 TO 7) LOOP ... x'RANGE LOOP ... RANGE (x'LOW TO x'HIGH) LOOP ... RANGE (0 TO x'LENGTH-1) LOOP ...
2009 O. Adekunle
53
!! sEVENT: Returns true when an event occurs on s !! sSTABLE: Returns true if no event has occurred on s !! sACTIVE: Returns true if s = 1 !! sQUIET <time>: Returns true if no event has occurred during the time specied !! sLAST_EVENT: Returns the time elapsed since last event !! sLAST_ACTIVE: Returns the time elapsed since last s = 1 !! sLAST_VALUE: Returns the value of s before the last event.
2009 O. Adekunle 54
2009 O. Adekunle
55
What if you want a parity detector? !! Parity detector: a circuit that provide
!!
!! output = 0 when the number of 1s in the input vector is even !! output 1 otherwise
input (1 downto 0)
output
2009 O. Adekunle
56
!!
input (2 downto 0)
output
ENTITY parity_det IS PORT ( input: IN STD_LOGIC_VECTOR (2 DOWNTO 0); output: OUT BIT); END parity_det;
2009 O. Adekunle
57
!!
input (2 downto 0)
output
!!
ENTITY parity_det IS PORT ( input: IN STD_LOGIC_VECTOR (2 DOWNTO 0); output: OUT BIT); END parity_det;
input (3 downto 0)
output
ENTITY parity_det IS PORT ( input: IN STD_LOGIC_VECTOR (3 DOWNTO 0); output: OUT BIT); END parity_det;
2009 O. Adekunle
58
!!
input (2 downto 0)
output
!!
ENTITY parity_det IS PORT ( input: IN STD_LOGIC_VECTOR (2 DOWNTO 0); output: OUT BIT); END parity_det;
input (3 downto 0)
output
!!
Redundant much?
ENTITY parity_det IS PORT ( input: IN STD_LOGIC_VECTOR (3 DOWNTO 0); output: OUT BIT); END parity_det;
2009 O. Adekunle
59
A way of specifying a generic parameter !! Different for different applications !! Example syntax:
!! !!
2009 O. Adekunle
60
Next class
2009 O. Adekunle
61