Unit I HDL Design
Unit I HDL Design
Technology
UNIT I: HDL Design
Contents of Unit 1:
Data Objects
Data Types
Entity
Architecture
Modeling Styles
Sequential statements
Concurrent statements
Packages
Subprograms
Attributes
VHDL Test bench
Test benches using text files
VHDL modeling of combinational, sequential logics
FSM
Introduction to VHDL
Hardware Description Language
Stands for VHSIC Hardware
Description Language
VHSIC- Very High Speed Integrated
Circuits
VHDL is a IEEE standard
It is standard, technology/vendor
independent language
Applications are FPGA, CPLD &
ASIC programming
DATA OBJECTS
Signal
Variable
Constant
SIGNAL
Represents interconnection wires
Declared in
1. Entity declaration (referenced
by any architecture for that
entity)
2. Architecture declaration
(referenced by that architecture
only)
3. Package declaration (Global)
SIGNAL
Syntax:
e.g:
SIGNAL X: bit:=‘1’;
VARIABLE
Used for local storage
Declared in
1. Process (referenced by that
process only)
2. Subprograms
VARIABLE
SYNTAX:
VARIABLE variable_name:
variable_type [:=value]
e.g:
VARIABLE Y: std_logic:=‘0’;
CONSTANT
Used for specific values
Declared in
1. Entity declaration (referenced
by any architecture for that
entity)
2. Architecture declaration
(referenced by that architecture
only)
3. Package declaration (Global)
CONSTANT
Syntax:
e.g:
User-defined
1. Integer
2. Enumerated
Pre-defined:
BIT & BIT_VECTOR
2 level logic
e.g:
Signal y:bit:=‘1’;
signal x:bit_vector(3 downto 0);
x<=“1101”;
“<=“ - used for assigning value to
a signal
“:=“- used for assigning value
to a variable
STD_LOGIC & STD_LOGIC_VECTOR
e.g:
signal x:std_logic_vector(3 downto 0):=“1111”;
2D array – user defined
Syntax:
type type_name is array (specification) of
data_type;
e.g:
Type matrix is array(0 to 3) of
std_logic_vector(7 downto 0);
signal x: matrix;
VHDL CODE
Contains:
Library declarations- 2 lines:
name of library, use clause
Entity- list of all input, output
ports
Architecture- description of how
the circuit behaves- 2 parts :
declarative part where constants
and signals are declared, code
part
e.g:
Library ieee;
Use ieee.std_logic_1164.all; ---- specifies multi-level logic like when std_logic data type used
Use std.standard.all;
Library work;
Use work.all; ---- files are stored .vhd, all files created by complier, simulator
Entity new is
Port (a, b, c, clk : in bit;
D: out bit);
End new;
Architecture circuit of new is
Signal t:bit;
Begin
Process (clk)
Begin
If(clk’ event and clk=‘1’) then
T<=a and b;
D<=t or c;
End if;
End process;
End circuit;
MODELING STYLES
Three types:
1. Concurrent or data flow
Intermediate level of abstraction – working of
the system described in terms of data transfer
between registers
2. Sequential or Behavioral
describes circuit or system at a high level of
abstraction – only overall behavior specified
3. Structural
low level of abstraction - components used
and structure of the interconnection between
components specified
CONCURRENT STYLE
Entity add is
port (a, b: in std_logic;
sum, carry: out std_logic);
End add;
Architecture add1of add is
begin
sum<= a xor b;
carry<= a and b;
End add1;
------------------------------------------------------------------
entity mux is
port (a, b, c, d: in std_logic; sel : in std_logic_vector(1
downto 0); y: out std_logic);
end mux;
CONCURRENT STATEMENTS
When – 2 forms:
1. when/else
2. with/select/when
When/else
architecture muxa of mux is
begin
y<=a when sel=“00” else
b when sel=“01” else
c when sel=“10” else
d;
end muxa;
With/select/when
architecture muxb of mux is
begin
with sel select
y<=a when “00”
b when “01”
c when “10”
d when others;
end muxb;
CONCURRENT STATEMENTS
Generate – allows a section of code to
be repeated.
typical – for generate
e.g – shift register
Block- 1. Simple block, 2. Guarded block
1. Simple block- used for partitioning of
code, so code is more readable,
manageable
2. Guarded block- guard expression
(Boolean expression), guarded
statement
GUARDED BLOCK
Architecture dff1 of dff is
begin
b1: block(clk’ event and clk=‘1’) --- guard
expression
begin
q<=guarded d; ---- guarded statement
component and1 is
port (l, m: in bit; n: out bit);
end component;
begin
x1: xor1 port map (a, b, sum);----component
instantiation
library ieee;
use ieee.std_logic_1164.all;
package my_package is
type state is (st1,st2,st3,st4);
constant vec : std_logic_vector(7 downto 0);
component xor1 is
port (x, y: in bit; z: out bit);
end component;
end my_package;
PACKAGE
package my_package1 is
component xor1 is
port (x, y: in bit; z: out bit);
end component;
function rising_edge (signal s:std_logic) return
Boolean;
end my_package1;
package body my_package1 is
function rising_edge (signal s:std_logic) return
Boolean is
begin
return (s’event and s=‘1’)
end rising_edge;
end my_package1;
PACKAGE
Library ieee;
Use ieee.std_logic_1164.all;
Use work.my_package1.all;
entity add is
port(a, b: in bit; sum, carry: out bit);
end add;
architecture add1 of add is
begin
x1: xor1 port map (a, b, sum);
carry<= a and b;
end add1;
PACKAGE
Library ieee;
Use ieee.std_logic_1164.all;
Use work.my_package1.all;
entity dff is
port( d, clk : in bit; q: out bit);
end dff;
architecture dff1 of dff is
begin
process (clk)
begin
if rising_edge (clk) then
q<=d;
end if;
end process;
end dff1;
ATTRIBUTES
Five classes of predefined attributes:
1. Value attributes: returns a constant
value
2. Function attributes: calls a function
that returns a value
3. Signal attributes: creates a new
signal
4. Type attributes: returns a type
name
5. Range attributes: return a range
ATTRIBUTES- Value Kind
Attributes specified by character “’”
and then attribute name. Object
preceding ‘ is the object that the
attribute is attached to.
1. T’left: returns left bound of type or
subtype
2. T’right: returns right bound of type or
subtype
3. T’high: returns upper bound of type
or subtype
4. T’low: returns lower bound of type or
subtype
ATTRIBUTES: Value kind
e.g:
Architecture arch of b is
type color is (blue, green, yellow, red);
signal color1, color2, color3, color4:color;
begin
color1<=color’left; ----returns blue
color2<= color’right; ----returns red
color3<=color’high; ----returns red
color4<=color’low; ----returns blue
end arch;
ATTRIBUTES: Value kind
Type bit4 is array(0 to 3) of bit;
Variable len : integer;
Len:= bit4’length; ----returns 4
ATTRIBUTES: Function
kind
A function call occurs that uses
the value of the input argument
to return a value. Three types:
1. Function type attributes: return
type value
2. Function array attribute: return
array bound
3. Function signal attributes: return
signal history information
ATTRIBUTES: Function
type
1. T’pos(value) – returns position number of
value passed in
2. T’val (value) – returns value from position
number passed in
3. T’succ (value) – returns next value in type
after input value
4. T’pred (value) – returns previous value in
type after input value
5. T’leftof (value) – returns value
immediately to the left of the input value
6. T’rightof (value) – returns value
immediately to the right of the input value
ATTRIBUTES: Function
type
e.g:
type color is (red, yellow, green, blue,
purple, orange);
signal color1: color;
color1’pos(red) -----returns 0
color1’val(2) ------returns green
color1’succ(blue) ----returns purple
color1’pred (blue) ----returns green
color1’rightof (blue) ----returns purple
color1’leftof (blue) ----returns green
ATTRIBUTES: Function signal
Used to return information about the
behavior of the signals
1. s’event – returns true if event occurred
during current delta
2. s’active - returns true if transaction
occurred during current delta
3. s’last_event – returns time elapsed since
last event on signal
4. s’last_value – returns value of s before last
event
5. s’last_active – returns time elapsed since
the last time signal was active
ATTRIBUTE - USER DEFINED
type color is (red, green, blue,
white);
- Encoding is binary by default
To change encoding, user
defined attribute is used:
Combinational
Input output
Circuit
Next
State
Present Sequential
Circuit
State
Clock Reset
FINITE STATE MACHINES
Generalized code for lower section- Sequential
Circuit:
process(clk, rst)
begin
if (rst = ‘1’) then
present_state<=st0;
elsif (clk’event and clk=‘1’) then
present_state<=next_state;
end if;
end process;
FINITE STATE MACHINES
Generalized code for upper section- combinational circuit:
process (input, present_state)
begin
case present_state is
when state0 =>
if (input = ----) then
output<=value1;
next_state<=state1;
else -----
end if;
when state1 =>
if (input = ----) then
output<=value1;
next_state<=state2;
else -----
end if;
-------
end case;
end process;
FINITE STATE MACHINES
e.g: Moore type
rst
d=1
State d=1 State
A B
(x=a) (x=b)
d=0 d=0
FINITE STATE MACHINES Design
Template 1
VHDL Code
entity fsm is
port (a, b, d, clk, rst: in bit; x: out bit);
end fsm;
architecture fsm1 of fsm is
type state is (stateA, stateB);
signal prs_state, nxt_state: state;
begin
----lower section------
process(clk, rst)
begin
if (rst = ‘1’) then
prs_state<=stateA;
elsif (clk’event and clk=‘1’) then
prs_state<=nxt_state;
end if;
end process;
FINITE STATE MACHINES
------Upper section -------------
process (input, prs_state)
begin
case prs_state is
when stateA =>
x<=a;
if (d=‘1’) then
nxt_state<=stateB;
else nxt_state<=stateA;
end if;
when stateB =>
x<=b;
if (d=‘1’) then
nxt_state<=stateA;
else nxt_state<=stateB;
end if;
end case;
end process;
end fsm1;
FINITE STATE MACHINES Design
Template 2
entity fsm is
port (a, b, d, clk, rst: in bit; x: out bit);
end fsm;
architecture fsm1 of fsm is
type state is (stateA, stateB);
signal prs_state, nxt_state: state;
signal temp: bit;
begin
----lower section------
process(clk, rst)
begin
if (rst = ‘1’) then
prs_state<=stateA;
elsif (clk’event and clk=‘1’) then
prs_state<=nxt_state;
x<=temp;
end if;
end process;
FINITE STATE MACHINES
------Upper section -------------
process (input, prs_state)
begin
case prs_state is
when stateA =>
if (d=‘1’) then
nxt_state<=stateB; temp<=a;
else nxt_state<=stateA; temp<=b;
end if;
when stateB =>
temp<=b;
if (d=‘1’) then
nxt_state<=stateA; temp<=b;
else nxt_state<=stateB; temp<=a;
end if;
end case;
end process;
end fsm1;
TEST BENCH
Used to verify the functionality of
a design
The test bench instantiates the
Design Under Test (DUT)
The test bench provides
necessary inputs to the DUT and
examines the output from DUT
VHDL provides the capability of
writing test bench in the same
language
TEST BENCH
TEST BENCH
TESTER DUT
TEST BENCH
entity dff is
port (d, clk, rst: in bit; q: out bit);
end dff;
------Test bench-------
entity testbench is
end testbench;
architecture behave of testbench is
component dff is
port (d, clk, rst: in bit; q: out bit);
end component;
signal d, clk, rst, q : bit;
TEST BENCH
begin
u1: dff port map (d=>d, clk=>clk, rst=>rst, q=>q);
ta: process
begin
rst<=‘1’;
wait for 10ns;
rst<=‘0’;
d<=‘1’;
wait for 50ns;
d<=‘0’;
wait for 50ns;
end process;
tb: process
begin
clk<=‘1’;
Wait for 10ns;
clk<=‘0;
wait for 10ns;
end process;
End behave;
TEST BENCH
entity mux is
port (in: in std_logic_vector (3 downto 0); sel: in
std_logic_vector(1 downto 0); y:out std_logic);
end mux;
------Test bench--------
entity testmux is
end tetsmux;
architecture behave of mux is
component mux is
port (in: in std_logic_vector (3 downto 0); sel: in
std_logic_vector(1 downto 0); y:out std_logic);
end component;
signal in: std_logic_vector (3 downto 0);
signal sel: std_logic_vector(1 downto 0);
signal y: std_logic;
TEST BENCH
begin
u1: mux port map(in=>in, sel=>sel, y=>y);
tb: process
begin
sel<=“00”;
in<=“0001”;
wait for 100ns;
sel<=“01”;
in<=“0010”;
wait for 100ns;
sel<=“10”;
in<=“0100”;
sel<=“11”;
in<=“1000”;
end process;
end behave;
TEST BENCH USING TEXT FILE
Dumping results into a text file:
The values of signals at specific times
can be written into a text file using
WRITE procedures provided in the
predefined TEXTIO package.
Reading vectors from a text file:
Using READ procedures provided in the
predefined TEXTIO package, reading
stimulus from a text file is possible.
• Refer to page nos- 271 – 280 of T3
METASTABILITY
Itis the state of the circuit which is neither
‘0’ nor ‘1’
As a result circuit acts in unpredictable way
and may lead to system failure
Metastable states are avoidable in fully
synchronous systems when input set up and
hold times of flip flop are completely satisfied
But for asynchronous inputs connected to flip
flops, the set up and hold time
A circuit in metastable state eventually
settles in one of the stable states but the
time required to recover is not predictable
METASTABILITY
Waveforms
METASTABILITY
Synchronizer
REFERENCES
Reference Book:
1.Charles Roth, “Digital System
Design using VHDL”, Cengage
Learning
Text Book:
1. Douglas Perry, “VHDL”, McGraw Hill
2. Volnei Pedroni, “Circuit Design with
VHDL” , PHI
3. J. Bhasker, “A VHDL Primer”,
Pearson Education
TOPIC WISE REFERENCES
Data Objects – T1,T2
Data Types – T1, T2
Entity - T2, R1
Architecture – T2
Modeling Styles – T2, R1
Sequential statements – T1, T2, R1
Concurrent statements - T1, T2, R1
Packages – T1, T2
Subprograms – T1, T2
Attributes – T1, T3
VHDL Test bench – T3
VHDL modeling of combinational, sequential logics –
T1,T2,R1
FSM – T2
Metastability - Internet