VHDL Tutorial: S.M.K.Rahman
VHDL Tutorial: S.M.K.Rahman
S.M.K.Rahman
What is VHDL?
IEEE Industry Standard hardware description language Description language for both simulation and synthesis Offshoot of Very High Speed Integrated Circuit (VHSIC) DOD program in early 1980s
Entity
Defines interface to outside world, i.e input and output pins Serves same function as a schematic symbol
Inputs
Ports
Defined in ENTITY Ports can be IN, OUT, INOUT
Architecture
Defines implementation of design, i.e. logic equations Serves same function as a schematic
ENTITY example IS
PORT ( END example; a : in BIT; b : out BIT);
ENTITY and ARCHITECTURE make a pair linked by name
ARCHITECTURE pld OF example IS BEGIN ARCHITECTURE defines implementation b <= a; END pld;
PROCESS Statement
Groups sequential statements
PROCESS Statement
Using the Sensitivity List
PROCESS (sensitivity_list) BEGIN -- Sequential statement #1 -- ..... -- Sequential statement #N END PROCESS; This process is executed after a change in any signal in the Sensitivity List
PROCESS Statement
Using the WAIT statement: PROCESS BEGIN
WAIT condition -- Sequential statement #1 -- ..... -- Sequential statement #N
END PROCESS;
This process is executed when the WAIT conditionis true!
PROCESS Statement
Use LABELS for organization:
label: PROCESS (sensitivity list) BEGIN -- Sequential statement #1 -- ..... -- Sequential statement #2 END PROCESS label;
Conditional
Selected
IF Statement
Chooses action based on condition Allows ELSIF, ELSE statements Must be inside PROCESS
IF Statement Example
ENTITY if_ex IS PORT ( sel, a, b : in BIT; y : out BIT ); END if_ex; ARCHITECTURE if_ex OF if_ex IS BEGIN PROCESS (sel, a, b) BEGIN IF sel = '1' THEN y <= a; ELSE y <= b; END IF; END PROCESS; END if_ex;
VHDL Variables
ENTITY var_ex IS PORT ( x, a, b :IN BIT; z :OUT BIT); END var_ex; ARCHITECTURE example OF var_ex IS BEGIN PROCESS (x, a, b) VARIABLE tmp :BIT ; VARIABLE tmp holds BEGIN intermediate value IF (x = '1') THEN tmp := a AND b; z <= tmp; ELSE z <= '1'; END IF; END PROCESS; END example;
Resulting Schematic
VHDL Signals
ENTITY sig_ex IS PORT ( a, b, c y END sig_ex; :IN BIT; :OUT BIT);
ARCHITECTURE example OF sig_ex IS SIGNAL temp BEGIN temp <= a XOR b; y <= temp AND c; END example; :BIT;
Resulting Schematic
VHDL Signals
ENTITY mul IS PORT (a, b, c, selx, sely : IN BIT; data_out : OUT BIT); END mul; ARCHITECTURE ex OF mul IS SIGNAL temp : BIT; BEGIN process_a: PROCESS (a, b, selx) BEGIN IF (selx = 0 THEN temp <= a; ELSE temp <= b; END IF; END PROCESS process_a;
Resulting Schematic
Generated from process_a Processes interconnected by SIGNAL temp
SCOPE:
BEHAVIOR:
New value ARCHITECTURE wrong OF bad IS is not yet SIGNAL val : INTEGER RANGEavailable
0 TO 3; BEGIN PROCESS (i0, i1, i2, i3, a, b) BEGIN val <= 0; IF (a = 1 THEN val <= val + 1; END IF;
IF (b = 1 THEN val <= val + 2; END IF; CASE val IS WHEN 0 => q <= i0; WHEN 1 => q <= i1; WHEN 2 => q <= i2; WHEN 3 => q <= i3; END CASE; END PROCESS; END wrong;
Data Types
VHDL is a strongly typed language, i.e disparate data types may not be assigned to each other
Data Types
Bus Implementation
VHDL offers vector types to implement buses Common vector types are: bit_vector, std_logic_vector Examples: SIGNAL fred_bus :bit_vector (7 downto 0); SIGNAL barney_bus :std_logic_vector (3 downto 0); SIGNAL betty_bus :std_logic_vector (0 to 3);
Bus Assignment
Reference entire bus fred_bus <= 11111111; Reference one bit of a bus bus (3) <= 1;
Enumerated Types
Enumerated types are the most common usercreated types Enumerated types are used primarily for state machines Example: TYPE country IS (Germany, USA, Italy, Japan); TYPE state_type IS (state_a, state_b, state_c);
VHDL PACKAGEs
What are packages? Packages are a collection of elements including data type descriptions
They can be shared by multiple designs/designers You can use standard packages which are included with VHDL or create your own
VHDL Packages
Commonly Used Packages:
IEEE.std_logic_arith - arithmetic functions
IEEE.std_logic_signed - signed arithmetic
functions
IEEE.std_logic_unsigned - unsigned arithmetic
functions
IEEE.std_logic_1164 - std_logic and related
functions
Packages in MAX+PLUS II
Example: LIBRARY IEEE; USE IEEE.std_logic_1164.all; USE IEEE.std_logic_arith.all; USE IEEE.std_logic_unsigned.all;
User-defined Package
User-defined packages must be in the same directory as the design To use your new packages:
LIBRARY WORK; USE WORK.<package name>.all;
CASE Statements
Used to generate combinatorial logic Must specify all possibilities with a WHEN OTHERS statement
CASE val IS WHEN 00 => q <= i0; WHEN 01 => q <= i1; WHEN OTHERS => q <= X; END CASE; val and i0, i1 will be input to combinatorial logic q will be output to combinatorial logic
How do you use arithmetic and boolean functions with other data types? Operator Overloading
ENTITY overload IS PORT ( a : IN std_logic_vector (3 downto 0); b : IN std_logic_vector (3 downto 0); sum : OUT std_logic_vector (4 downto 0)); END overload; ARCHITECTURE example OF overload IS BEGIN adder_body:PROCESS (a, b) BEGIN sum <= a + b; END PROCESS adder_body; END example;
Synthesis Rules
Not all processes are synthesizable To be synthesizable, one of these must be true: Combinatorial circuit: Sensitive to all input signals Registered circuit: Sensitive to a single clock edge and optional asynchronous clear/preset/load signals
Latch Inference
VHDL code describes behavior of D-Type Flip-Flop This implementation uses PROCESS sensitivity list Clock is only signal in sensitivity list
PROCESS (clk) BEGIN IF clk = 1 THEN q <= d; END IF; END PROCESS;
Flip-Flop Inference
Flip-Flop Inference
Implementation using PROCESS with WAIT statement PROCESS
BEGIN WAIT UNTIL clk = 1 q <= d; END PROCESS;
Gated Clocks
Clock must be gated outside of PROCESS description Must define new clock as a signal
ARCHITECTURE ex OF gatedclock IS SIGNAL gclock : std_logic; BEGIN gclock <= clka AND clkb; PROCESS (gclock) BEGIN IF gclock = '1' THEN q <= d; END IF; END PROCESS; END ex;
Module Generation
The VHDL synthesizer generates modules for each arithmetic function entered in a design These modules are then converted to structures that are optimized for the target device Example: Adder structure for FLEX is ripple-carry Adder structure for MAX is carry-look ahead Family-specific module generation is automatic
Module Generation
FLEX
a <= b + c;
c
Counters
Counters are just accumulators that always add a 1
ARCHITECTURE example OF counter IS BEGIN PROCESS (clk) VARIABLE count : std_logic_vector (7 downto 0); BEGIN IF clk = '1' THEN count := count + 1; END IF; q <= count; END PROCESS; END example;
Counters
Component Instantiation
The Upper-level design must have a COMPONENT declaration for a lower-level design before instantiating it
ARCHITECTURE upper OF top IS SIGNAL count : std_logic; COMPONENT simpcnt PORT ( clk : IN bit; q : OUT std_logic); END COMPONENT; BEGIN u1 : simpcnt PORT MAP (clk => sysclk, q => count); COMPONENT declared here
Macrofunction Instantiation
All of the Altera macrofunctions and primitive components are declared in the VHDL package:
ALTERA.maxplus2.all
Within this package, all component ports are of type STD_LOGIC or STD_LOGIC_VECTOR
Macrofunction Instantiation
LIBRARY IEEE; USE IEEE.std_logic_1164.ALL; LIBRARY ALTERA; USE ALTERA.maxplus2.ALL; ENTITY macro IS PORT( clock, enable : IN std_logic; Qa, Qb, Qc, Qd : OUT std_logic); END macro; ARCHITECTURE example OF macro IS BEGIN u1 : gray4 PORT MAP (clk => clock, ena => enable, qa => Qa, qb => Qb, qc => Qc, qd => Qd); END example;
Use the ALTERA library for macrofunction instantiation so that component declarations are not needed.