Sei sulla pagina 1di 162

Introduction and Overview

VHDL Application Workshop page 1


What is VHDL ?
VHSIC (Very High Speed Integrated Circuit) Hardware Description Language
n Modelling DIGITAL Electronic Systems

n Both Concurrent and Sequential statements

VHDL Application Workshop page 2


VHDL History

n Department of Defense (DoD) developed in 1981


n IEEE 1076-1987 Standard (VHDL’87)
n IEEE 1076-1993 Standard (VHDL’93)
VHDL Application Workshop page 3
VHDL Limitation

VHDL Application Workshop page 4


Hardware Description Language (HDL)
The VHDL is a software programming language used to model the intended operation of a piece of
hardware, similar to Verilog-HDL.
Use of the VHDL Language
1. Documentation Language: To provide human and machine readable documentation
2. Design Language: To provide a structure reflecting hardware design and hierarchy and provide methods to
handle complexity by partitioning the design.
3. Verification Language: To provide a concurrent method verifying hardware interaction and provide
constructs for stimulating a design.
4. Test Language: To provide ability to generate test vectors, multiple testbench strategies and method
to write self checking code.
5. Synthesis Language: To provide high-level constructs that can be translated to boolean equations and
then translate them to gate as well as to provide constructs that can be optimized.

VHDL Application Workshop page 5


Electronic System Design
System Design Specification
Hardware/Software
Co-Design

Hardware Design Software Design


Specification Specification

ASIC

FPGA Boards and


PLD Systems
Std Parts

VHDL Application Workshop page 6


Top-Down Design
PCB 1 PCB 2
SYSTEM
The top-level system is modeled for
uP
ASIC1
ROM
ASIC2
RAM functionality and performance using a high-
level behavioural description.
FPGA

A B

Each major component is modeled at the


RTL code RTL code

C
RTL code

behavioural level and the design is simulated


ASIC
RTL RTL RTL
synthesis synthesis synthesis

again for functionality and performance.

ASIC

Test synthesis
Each major component is modeled at the
A B
gate level and the design is simulated again
for timing, functionality and performance.
C
ASIC

Layout synthesis

VHDL Application Workshop page 7


Levels of Abstraction : Capture

Editing VHDL Code Behavioural


V Block Capture Behavioural Synthesis
H System Level Tools RTL
D
L Logic Synthesis

Schematic Capture Logic


Place and Route

Layout Tools Layout

VHDL Application Workshop page 8


Levels of Abstraction : Definition
inputs
Function
outputs
Behavioural Hardware system specification
Standard parts models

Dff Dff RTL ASIC/FPGA design for synthesis


Synthesizable models

Gate level design


Logic
PLD design

Layout Full custom design

VHDL Application Workshop page 9


Tradeoffs Between Abstraction Levels
Faster
simulation/entry Less detailed
Behavioural

RTL

Logic

Layout
Slower More detailed
simulation/entry
VHDL Application Workshop page 10
The Benefits of Using VHDL
n Design at a higher level
u Find problems earlier
n Implementation independent
u Last minute changes
u Delay implementation decisions
n Flexibility
u Re-use
u Choice of tools, vendors
n Language based
u Faster design capture
u Easier to manage

VHDL Application Workshop page 11


VHDL Organisations
n VHDL International
u Promoting use of VHDL
u Quarterly publication, “Resource Book”
u “Simulator Certification Test Suite”
n IEEE Committees
u VASG : defining the language : various others too
(VASG : VHDL Analysis and Standardization Group)
n Europe
u European chapter of VASG
u VHDL Forum for CAD in Europe
u VHDL Newletter, published by Esprit project
u Regional user groups : VHDL-UK, etc . . .
n Newsgroups
u comp.lang.vhdl
VHDL Application Workshop page 12
VHDL Objects
Aim and Topics
n Introduction to :
u Entity
u Architecture
u Package
u Configuration

VHDL Application Workshop page 13


Introduction
n VHDL design consists of several separate four design units :
u Entity design unit
u Architecture design unit
u Configuration design unit
u Package design unit

component
Entity VHDL Entity
architecture architecture
configuration Compiler configuration
package package

Design Units Library


VHDL Application Workshop page 14
Component
VHDL Component
entity HALFADD is
port ( A, B : in bit; Entity declaration
SUM, CARRY : out bit );
end HALFADD;

architecture BEHAVE of HALFADD is


begin
SUM <= A xor B; Architecture declaration
CARRY <= A and B;
end BEHAVE;

VHDL Application Workshop page 15


Entity declaration
Syntax:
entity entity_name is
generic (generic_list);
port( port_name : <mode> port_type );
end entity_name;

< mode > = in, out, inout, buffer, linkage


in = Component only read the signal
out = Component only write to the signal
inout = Component read or write to the signal (bidirection signals)
buffer = Component write and read back the signal (no bidirectional)
linkage = Used only in the documentation

VHDL 93 ! Can now optionally include the reserved word entity after the reserved
word end in the entity declaration
VHDL Application Workshop page 16
Entity declaration example
entity MUX is
IN0
port ( IN0, IN1, SEL : in bit;
OUTPUT : out bit );
IN1 OUTPUT
end MUX; SEL

Propagation delay time


entity MUX is
IN0
generic( TPLH : time:= 3 ns;
TPHL : time:= 5 ns );
IN1 OUTPUT
port ( IN0, IN1, SEL : in bit; SEL
OUTPUT : out bit );
end entity MUX; tplh tphl
VHDL 93 VHDL Application Workshop page 17
Architecture Design Unit
n Structural or behavioural description
n Decribes an implementation of an entity
n Must be associated with a specific entity
n Single entity can have many architectures

VHDL Application Workshop page 18


Architecture Declaration
Syntax:
architecture architecture_name of entity_name is
declarations
begin
concurrent_statements
end architecture_name;

VHDL 93 ! Can now optionally include the reserved word architecture after the
reserved word end in the entity declaration

VHDL Application Workshop page 19


Architecture styles

Behavioural Algorithmic description

Dataflow Register Transfer Level (RTL)

Structural Netlist representation

Mixed-Level Behavioral, Dataflow, Structural

VHDL Application Workshop page 20


Behavioural style architecture
architecture BEHAVIOURAL of MUX is
begin
process (IN0, IN1, SEL)
begin
if (SEL = ‘0’) then
OUTPUT <= IN0;
else
OUTPUT <= IN1;
end if;
end process;
end BEHAVIOURAL;

VHDL Application Workshop page 21


Dataflow style architecture
architecture DATAFLOW of MUX is
begin
OUTPUT <= ( (not SEL) and IN0) or (SEL and IN1);
end DATAFLOW;
output = (sel . in0) + (sel. in1)
mux
in0 i1
g1
int1
and2
o1 g3
in1 int0 i2 i1 output
or2
g2 o1
i1 i2
sel i1 g0
and2
inv int2
o1 i2 o1

VHDL Application Workshop page 22


Structural style architecture
architecture STRUCTURAL of MUX is
Component component INV
declarations port (I1 : in bit; O1 : out bit );
end component;
component AND2
port (I1, I2 : in bit; O1 : out bit );
end component;
component OR2
port (I1, I2 : in bit; O1 : out bit );
end component;
signal INT0, INT1, INT2 : bit;
begin
G0 : INV : port map ( I1 => SEL, O1 => INT0);
G1 : AND2 : port map ( I1 => IN0, I2 => INT0, O1 => INT1);
G2 : AND2 : port map ( I1 => SEL, I2 => IN1, O1 => INT2);
G3 : OR2: port map ( I1 => INT1, I2 => INT2, O1 => OUTPUT);
end STRUCTURAL;
VHDL Application Workshop page 23
Component Declarations
architecture STRUCTURAL of MUX is
component INV
port (I1 : in bit; O1 : out bit );
n Component instantiated must be end component;
declared within architecture component AND2
port (I1, I2 : in bit; O1 : out bit );
n Not instantiating the entity, but end component;
a socket . . . component OR2
port (I1, I2 : in bit; O1 : out bit );
end component;
signal INT0, INT1, INT2 : bit;
begin
G0 : INV : port map ( I1 => SEL, O1 => INT0);
G1 : AND2 : port map ( I1 => IN0, I2 => INT0, O1 => INT1);
G2 : AND2 : port map ( I1 => SEL, I2 => IN1, O1 => INT2);
G3 : OR2: port map ( I1 => INT1, I2 => INT2, O1 => OUTPUT);
end STRUCTURAL;
Beware !
Component declaration before “begin” of architecture
VHDL Application Workshop page 24
Packages Design Unit

n Package declaration
u Subprogram declarations
u Type declarations
u Component declarations
u Deferred constant declaration
n Package Body
u Subprogram body
u Defered constant value

VHDL Application Workshop page 25


Packages declaration
Syntax:
package package_name is
[exported_subprogram_declarations]
[exported_constant_declarations]
[exported_components]
[exported_type_declarations]
[attribut_declarations]
[attribut_specifications]
end package_name;

package body package_name is


[exported_subprogram_declarations]
[internal_subprogram_declarations]
[internal_subprogram_bodies]
[internal_type_declarations]
end package_name;
VHDL Application Workshop page 26
Packages declaration example
package MY_PACKAGE is
type MY_TYPE1 is ...
type MY_TYPE2 is ...
function MY_MEAN (A, B, C: real) return real;
procedure MY_PROC1 ( …);

end MY_PACKAGE;
package body MY_PACKAGE is

function MY_MEAN (A, B, C : real) return real is
begin
return(A + B + C)/ 3;
end MY_MEAN;
...
end MY_PACKAGE;
VHDL Application Workshop page 27
Configuration Design Unit
n Select a particular architecture of an entity from library

VHDL Application Workshop page 28


Configuration declaration
Syntax:
configuration configuration_name of entity_name is
for architecture_name
end configuration_name ;

VHDL Application Workshop page 29


Configuration Example
A N_SUM configuration CFG_HALFADD of HALFADD is
for STRUCTURAL
B N_CARRY1
end for;
end CFG_HALFADD;
U1
n Selection of entities and architectures
u Select architecture of top level entity
A u Select entity/architecture pairs for component
SUM
instances
HALFADD
n Consider as “parts list” for hierarchy
B CARRY n Default configuration
u If names of entities & components match
u Last analysed architecture of enties
n Non-default configuration
u Map specifiec entities, architectures
VHDL Application Workshop page 30
Process
entity AND_OR is
Concurrent statement port(A, B : in bit;
Z_OR, Z_AND : out bit);
n Section containing sequential end AND_OR;
statements architecture BEHAVE of AND_OR is
Exist inside an architecture begin
n AND_OR_FUNC: process(A, B)
begin
n Multiple processes interact if (A='1' or B='1') then
concurrently Z_OR <= '1';
else
Z_OR <= '0';
end if;
Sequential statements
if (A='1' and B ='1') then
Z_AND <= '1';
else
Z_AND <= '0';
end if;
end process AND_OR_FUNC;
end BEHAVE;
VHDL Application Workshop page 31
Design Hierarchy
entity FULLADD is
port (A, B, CIN : in bit;
SUM, CARRY : out bit );
end FULLADD;
architecture STRUC of HALFADD is
signal N_SUM N_CARRY1, N_CARRY2 : bit;
- - other declarations
begin
U1 : HALFADD port map (A, B, N_SUM, N_CARRY1);
U2 : HALFADD port map (N_SUM, CIN, SUM, N_CARRY2);
U3 : ORGATE port map (N_CARRY2, N_CARRY1, CARRY);
end STRUC;

A N_SUM
SUM
B
u1 u2
N_CARRY2

CIN N_CARRY1 CARRY


u3

VHDL Application Workshop page 32


Library
n A collection of compiled design units
u Entity, Architecture, Package, Package
body, Configuration
n Physically exists as a directory
n Referred to by Library name
n Simulator startup file specifies
mapping
u Library name -> directory name
Library IEEE;
use IEEE.Std_Logic_1164.all; n Compile into “WORK”
Library name
entity INCOMP_IF is n WORK mapped to specific library
port ( EN, D : in std_ulogic; name
Q : out std_ulogic );
VHDL Application Workshop page 33
Comments
Comments
--------------------------------------------------------------------
-- This is a comment
-- Each line must begin with --
-- Comments end with a new line
--------------------------------------------------------------------
Library IEEE;
use IEEE.Std_Logic_1164.all;
entity FULLADD is
port (A, B, CIN : in std_logic;
SUM, CARRY : out std_logic );
end FULLADD;

VHDL Application Workshop page 34


Signals and Datatypes
Aim and Topics
n Introduction to :
u Type concept
u Standard types
u Scalar and Array Literal
u IEEE Standard Logic
u Object declaration
F Constant declaration
F Signal declaration
F Variable declaration

VHDL Application Workshop page 35


Concept of a Type
n Type must be defined when signal is declared
n Either in
u Port section of an entity
u Architecture, in a signal declaration
n Types must match up !

VHDL Application Workshop page 36


Standard Data Types
package STANDARD is
type boolean is (FALSE, TURE);
type bit is (‘0’ , ‘1’);
type character is (-- asc ii set );
type integer is range
type real is range
-- bit_vector, string, time
end STANDARD;

VHDL Application Workshop page 37


Standard Data Types Provided within VHDL
Type Range of values Examples declaration
integer -2,147,483,647 to +2,147,483,647 signal index : integer := 0;
real - 1.0E +38 to +1.0E +38 variable val : real := 1.0;
boolean (TURE, FALSE) variable test : boolean := TRUE;
character defined in package STANDARD variable term : character := ‘@’;
bit 0, 1 signal in1 : bit := ‘0’;
bit_vector array with each element of the type variable pc : bit_vector(31 downto 0);
time fs, ps, ns, us, ms, sec, min, hr variable delay : time := 25 ns;
string array with each element of type character variable name : string(1 to 4) := “tmec”;
natural 0 to the maximum integer value in the variable index : natural := 0;
implementation
positive 1 to the maximum integer value in the variable index : natural := 0;
implementation

VHDL Application Workshop page 38


Scalars and Array Literals
Scalar Type Array Type
character string
bit bit_vector
std_logic std_logic_vector
boolean
real
integer
time

VHDL Application Workshop page 39


The Problem With Type Bit
n Only has values ‘1’ and ‘0’
u Defualt initialisation to ‘0’
n Simulation and synthesis require other values
u Unknown
u High impedance
u Don’t care
u Different signal strengths

VHDL Application Workshop page 40


MVL Systems
n Problum with type BIT : use of Multi-Valued Logic System
(MVL)
u Enumerated type defining unknown, high-impedance, etc
n Until early 1992, no standard
n Standard now set
u Nine state system defined and agreed by IEEE
u Standard IEEE 1164

VHDL Application Workshop page 41


IEEE Standard Logic types

n In package “ Std_Logic_1164”
n “std_logic” has same values as “std_ulogic”
std_logic std_ulogic
std_logic_vector std_ulogic_vector

VHDL Application Workshop page 42


Rules for Using Std_Logic and Std_Ulogic

VHDL Application Workshop page 43


Object Declarations
Aim and Topics
n Introduction to :
u Constant declaration
u Signal declaration
u Variable declaration

VHDL Application Workshop page 44


Constant Declaration
Syntax :
constant constant_name : type := value;
n Constant is name assigned to a fixed value
n You need only change the constant declaration in one place
n Constant makes a design more readable and makes updating
code easy
Example
constant Vdd : real := - 4.5 ;
constant CYCLE : time := 100 ns ;
constant PI : real := 3.14 ;
constant FIVE : bit_vector := “0101” ;
constant TEMP : std_logic_vector(8 to 11) := “0101” ;
VHDL Application Workshop page 45
Signal Declaration
Syntax :
signal signal_name : type ;
OR signal signal_name : type := initial_value;
n Signal connect design entities together and communicate changes in
values between processes.
n Signal can be abstractions of physical wires, busses, or used to document
wires in an actural circuit.
n Signal with a type must be declared before the signal is used.
Example signal A_BUS, A_BUS, Z_BUS : bit_vector (3 downto 0);
signal A_BIT, B_BIT, C_BIT, D_BIT : bit;
signal BYTE : bit_vector (0 to 7);
signal COUNT : integer range 1 to 50;
signal GROUND: bit := ‘0’;
signal SYS_BUS : std_logic_vector (31 downto 0);
VHDL Application Workshop page 46
Signal Assignment
n Signal of same type
n Language defines
u bit_vector (array of bit)
u string (array of character)
n Define size when declaring signal/port

VHDL Application Workshop page 47


Signals and Drivers
n Signal value changed with “Signal Assignment Statement”
n Signal driver
n Types must match
n Resolved signal

VHDL Application Workshop page 48


Alias Declaration
Syntax :
alias alias_name : alias_type is object_name;

n An alias is an alternative name for an existing object


(signal, variable or constant)
n It dose not define a new object
Example
signal COUNT : bit_vector(7 downto 0);
alias SIGN_BIT : bit is COUNT(7);
alias LSB_BIT : bit is COUNT(0);
alias NIBBLE_L : bit_vector(3 downto 0) is COUNT(3 downto 0);
alias NIBBLE_H : bit_vector(3 downto 0) is COUNT(7 downto 4);
VHDL Application Workshop page 49
Variables

signal A, B, C, Y, Z : integer; n Declared within process


begin n Assignment immediately
P1 : process (A, B, C) u Like “software”
variable M, N : integer ;
begin n Only be used within process
M := A; u Called its “scope”
N := B; n Retains value
Z <= M + N; n Assigns :
M := C; u signal to variable
Y <= M + N; u variable to signal
end process P1;

VHDL Application Workshop page 50


Variable Declaration
Syntax :
variable variable_name : type;
OR variable variable_name : type := initial_value;
n Variable is a name assigned to a changing value within a process
n Variable assignment occurs immediately in simulation
n Variable can be used as a temporary simulation value
n Variable must be declared before it is used
Example
variable INDEX : integer range 1 to 50;
variable MEMORY : bit_vector(0 to 7);
variable X, Y : integer ;
variable CYCLE_TIME : TIME range 10 ns to 50 ns := 10 ns;
VHDL Application Workshop page 51
Variables Versus Signals
signal A, B, C, Y, Z : integer; signal A, B, C, Y, Z : integer;
begin signal M, N : integer;
P1 : process (A, B, C) begin
variable M, N : integer ; P1 : process (A, B, C, M, N)
begin begin
M := A; M <= A;
N := B; N <= B;
Z <= M + N; Z <= M + N;
M := C; M <= C;
Y <= M + N; Y <= M + N;
end process P1; end process P1;

VHDL Application Workshop page 52


Variables Usage Model
n Variables used for algorithms
u Assign signals to variables
u Perform algorithm
u Assign variables to signals
n Variable cannot be accessed outside of its
process

VHDL Application Workshop page 53


VHDL Operators
Aim and Topics
n Introduction to :
u Logical Operators
u Relational Operators
u Concatenation Operator
u Arithmetic Operators

VHDL Application Workshop page 54


Logical Operators
n and, or, nand, nor, xor (equal precedence)
n not (higher precedence)
n Pre-defined for
u bit
u bit_vector
u std_ulogic, std_logic
u std_ulogic_vector, std_logic_vector
n xnor
VHDL 93 ! Shift operators : sll (shift left logical), srl (shift right logical),
sla (shift left arithmetic) , sra (shift right arithmetic),
rol (rotate left logical), ror (rotate right logical)

VHDL Application Workshop page 55


Logical Operators : Examples

entity AND2 is
port ( A_BIT, B_BIT : in bit;
Z_BIT : out bit );
end AND2;
architecture RTL of AND2 is
begin A_BIT
Z_BIT <= A_BIT and B_BIT; B_BIT
Z_BIT

end RTL;

VHDL Application Workshop page 56


Logical Operators on Arrays
n Operands same length and type
n Operations on matching elements
signal A_BUS, B_BUS, Z_BUS : std_logic_vector (3 downto 0);
A_BUS(3)
Z_BUS <= A_BUS and B_BUS; B_BUS(3)
Z_BUS(3)

Equivalent to
A_BUS(2)
B_BUS(2) Z_BUS(2)

A_BUS(1)
Z_BUS(3) <= A_BUS(3) and B_BUS(3); B_BUS(1) Z_BUS(1)
Z_BUS(2) <= A_BUS(2) and B_BUS(2);
Z_BUS(1) <= A_BUS(1) and B_BUS(1);
A_BUS(0)
Z_BUS(0)
B_BUS(0)
Z_BUS(0) <= A_BUS(0) and B_BUS(0);

VHDL Application Workshop page 57


Relational Operators
n Result is boolean (TRUE or FALSE)
n Often used with if - then - else

VHDL Application Workshop page 58


Relational Operations : Rules
n Operands must be of the same type
n Arrays
u May be of different lengths
u Aligned left and compared
u Can lead to unusual result !
n No numerical meaning

VHDL Application Workshop page 59


Concatenation Operator
n Ampersand ( & ) means concatenation in VHDL
signal Z_BUS : bit_vector ( 3 downto 0 ) ;
signal A, B, C, D : bit ;
signal BYTE : bit_vector ( 7 downto 0 ) ;
signal A_BUS : bit_vector ( 3 downto 0 ) ;
signal B_BUS : bit_vector ( 3 downto 0 ) ;

Z_BUS <= A & B & C & D ;


concatenation
BYTE <= A_BUS & B_BUS ;

VHDL Application Workshop page 60


Arithmetic Operators
n Pre-defined for
u integer
u real (except mod and rem)
u Physical types ( e.g. time)
n Not defined for
u bit_vector
u std_ulogic_vector
u std_logic_vector
n Generally, operands must be
of the same type

VHDL Application Workshop page 61


Adder example

entity ADDER is
port ( A, B : in integer range 0 to 7 ;
Z : out integer range 0 to 15 ) ; a b
end ADDER;
architecture ARITHMETIC of ADDER is +
begin
Z <= A + B ;
end ARITHMETIC; z

VHDL Application Workshop page 62


Arithmetic of Time
n Testbenches, cell delays
n Multiplication / division
u Multiply / divide by integer /real
u returns type time

VHDL Application Workshop page 63


Sequential Statments
Aim and Topics
n To introduce some of the most commonly used
concurrent and sequential statements
n The process statement
n Sequential statements
u IF Statements
u CASE Statement
u LOOP Statement
u WAIT Statement

VHDL Application Workshop page 64


Process Statement
Syntax :
optional_label: process (optional_sensitivity_list)
subprogram_declarations
type_declarations
constant_declarations
variable_declarations
other_declarations
begin
sequential_statements
end process optional_label;

VHDL Application Workshop page 65


Process Statement example
label Sensitivity list
MUX : process (A, B, SEL)
begin
if (SEL = ‘1’) then
Z <= A; Sequential statements
else
Z <= B;
end if;
end process MUX;

VHDL Application Workshop page 66


Processes within VHDL Code
architecture A of E is
begin
- - concurrent statements
: process
P1P1: process
begin begin
- - sequential statements
- - sequential statements
endproccess
end proccessP1;P1;
- - concurrent statements
process
P2P2: :process
begin begin
- - sequential statements
- - sequential statements
endproccess
end proccessP2; P2;
end A;

VHDL Application Workshop page 67


IF Statement

Syntax : if
if condition then
sequential_statements
...
end if; condition ture

Sequential
Example Statements
false
if (A = ‘1’) then
COUNT := COUNT + 1 ;
end if; end if

VHDL Application Workshop page 68


IF-ELSE Statement
Syntax :
if condition then
sequential_statements if

...
else
sequential_statements false condition ture
end if;
Sequential Sequential

Example Statements Statements

if (A = ‘1’) then
COUNT := COUNT + 1 ; end if
else
COUNT := COUNT - 1 ;
end if;

VHDL Application Workshop page 69


IF-ELSIF Statement
Syntax :
if condition_1 then
sequential_statements if
elsif condition_2 then
sequential_statements
end if;
false condition ture

Sequential
condition ture
Statements
Example Sequential
Statements
if (A = ‘1’) then
COUNT := COUNT + 1 ;
false

elsif (A= ‘0’) then


COUNT := COUNT - 1 ;
end if

end if;

VHDL Application Workshop page 70


IF Example
Library IEEE;
use IEEE.Std_Logic_1164.all;
entity IF_EXAMPLE is
port ( A, B, C, D : in std_logic_vector ( 3 downto 0 );
SEL : in std_logic_vector ( 1 downto 0 );
Z : out std_logic_vector ( 3 downto 0 ) );
end IF_EXAMPLE;
architecture IF_STATE of IF_EXAMPLE is
begin
process (A, B, C, D, SEL)
begin
if (SEL = “00”) then
Z <= A ;
elsif (SEL = “01”) then
Z <= B ;
elsif (SEL = “10”) then
Z <= C ;
else
Z <= D ;
end if;
end process;
end IF_STATE;
VHDL Application Workshop page 71
CASE Statement
Syntax :
case expression is
when choice => sequential_statements ;
when choice => sequential_statements ;
...
end case;
Example
case SEL is n No choices may overlap
when “00” => Z <= A ;
when “01” => Z <= B ; n All choices must be specified
when “10” => Z <= C ; u specifically or with “others”
when others => Z <= D ;
end case ;

VHDL Application Workshop page 72


Defining Ranges
Library IEEE; Beware !
use IEEE.Std_Logic_1164.all;
n Array do not have a discrete sequence
entity CASE_EXAMPLE is of values associated with them
port ( A, B, C, SEL : in integer range 0 to 15 ;
Z : out integer range 0 to 15 );
end CASE_EXAMPLE;
case SEL is
architecture CASE_STATE of CASE_EXAMPLE is when “11” =>
begin when “00” to “10” =>
process (A, B, C, SEL) when others =>
begin
case SEL is
when 0 to 4 => Z <= B ;
when 5 => Z <= C ;
when others => Z <= A ;
end process;
end CASE_STATE;
VHDL Application Workshop page 73
LOOP Statements
n Avaiable in a process
u For Loop Statement
u While Loop Statement
n Label is optional
n Use range attributes
u ‘high
u ‘low
u ‘range
n For loop identifier
u not declared
u Can’t be altered
u not visible outside loop

VHDL Application Workshop page 74


For Loop Statement
Syntax :
optional_label:for parameter in range loop
sequential_statements
end loop optional_loop;
Example
LOOP1 : for INDEX in 0 to 7 loop
DOUT(INDEX) <= DIN(INDEX);
end loop LOOP1;

n Iterated around a loop


n Loop variable gets values in the range
n No need to declare loop variable
n It is illegal to make an assignment to
the loop variable
VHDL Application Workshop page 75
For Loop Example
Library IEEE;
use IEEE.Std_Logic_1164.all;
entity EX is
port ( A, B, C : in std_logic_vector( 4 downto 0 );
Q : out std_logic_vector( 4 downto 0 ) );
end EX;
architecture FOR_LOOP of EX is
begin
process (A, B, C)
begin
for I in 0 to 4 loop
if A(I) = ‘1’ then
Q(I) <= B(I);
else
Q(I) <= C(I);
end if;
end loop;
end process;
end;
VHDL Application Workshop page 76
While Loop Statement
Syntax :
optional_label: while condition loop
sequential_statements
end loop optional_label;
Example
P1 : process (A)
n Available in a process
variable INDEX : integer := 0 ; n Not generally synthesizable
begin n Useful in testbenches
LOOP1 : while INDEX < 8 loop u Reading from file . . .
DOUT(INDEX) <= DIN(INDEX); u Generation of clock for
INDEX := INDEX + 1 ; specific time
end loop LOOP1;
end process P1;

VHDL Application Workshop page 77


WAIT Statements
n Suspends execution of process
n Execution continues when statement satisfied
n Four formats . . .
Wait for :
wait for specific_time; A specific time
wait on signal_list; An event on signals (equivalent to a sensitivity list)
wait until condition; A condition to occur (requires event)
wait; Indefinitely (never to be reactivated)

VHDL Application Workshop page 78


Wait Examples
Library IEEE; STIMULUS : process
use IEEE.Std_Logic_1164.all; begin
entity D_FLOP is SEL <= ‘0’ ;
port ( D, CLK : in std_ulogic; BUS_B <= “0000”;
Q : out std_ulogic ); BUS_A <= “1111”;
end D_FLOP; wait for 10 ns;
SEL <= ‘1’ ;
architecture A of D_FLOP is wait for 10 ns;
- - ETC, ETC
begin No sensitivity list end process STIMULUS;
process
begin
wait until CLK’event and CLK = ‘1’;
Q <= D;
end process;
end A;

VHDL Application Workshop page 79


Concurrent Statements
Aim and Topics
n To introduce some of the most commonly
used concurrent
u Concurrent signal assignment
u Condition signal assignment
u Selected signal assignment
u BLOCK statement

VHDL Application Workshop page 80


Concurrent Assignment Statements
X <= A + B;
Z <= C + X;

Z <= C + X;
n Concurrent X <= A + B;
n Order independent
n What you write is what you get . . .
A
+ X
B
+ Z
C

VHDL Application Workshop page 81


Don’t Create Feedback Loops !

Y
X <= X + Y;

+ X

VHDL Application Workshop page 82


Concurrent Versus Sequential
architecture A of E is
begin
- - concurrent statements
: process
P1P1: process
begin begin
- - sequential statements
- - sequential statements
endproccess
end proccessP1;P1;
- - concurrent statements
process
P2P2: :process
begin begin
- - sequential statements
- - sequential statements
endproccess
end proccessP2; P2;
end A;

VHDL Application Workshop page 83


Concurrent Signal Assignments

Concurrent Form Sequential Form

architecture FORM_1 of V_VAR is architecture FORM_1 of V_VAR is


begin begin
OUTPUT_Q <= DATA_IN; process (DATA_IN)
end V_VAR; begin
OUTPUT_Q <= DATA_IN;
end process;
end V_VAR;

VHDL Application Workshop page 84


Multiple Concurrent Assignments
n Signals are “wired together”
n VHDL requires a resolution function

Z <= A + B;
Z <= C + D; A
+
B ? Z
C
+
D

VHDL Application Workshop page 85


Multiple Assignments in a Process
architecture CONCURRENT of MULTIPLE is architecture SEQUENTIAL of MULTIPLE is
signal A, B, C, D : std_ulogic; signal Z, A, B, C, D : std_ulogic;
signal Z : std_logic; begin
begin process (A, B, C, D)
Z <= A and B; begin
Z <= C and D; Z <= A and B;
end CONCURRENT; Z <= C and D;
end process;
end SEQUENTIAL;
A
and
B Z C
?
C and Z
and D
D
VHDL Application Workshop page 86
Multiple Assignments : Example
process (A, B, SEL) process (A, B, SEL)
begin begin
if (SEL = ‘1’) then Z <= B;
Z <= A; if (SEL = ‘1’) then
else
Z <= B; Z <= A;
end if; end if;
end process; end process;

n Last assignment takes effect


n After process suspends
n Code is equivalent

VHDL Application Workshop page 87


Conditional Signal Assignment
entity BRANCH is
port ( A, B, C, X : in integer range 0 to 7 ;
Z : out integer range 0 to 0 ); n Concurrent version of “ If ”
end BRANCH;
n Only one target
architecture USE_IF of BRANCH is n Must have an unconditional else
begin
process (A, B, C, X)
begin architecture USE_WHEN of BRANCH is
if (X > 5) then begin
Z <= A; Z <= A when X > 5 else
elsif (X < 5) then B when X < 5 else
Z <= B; C;
else end USE_WHEN ;
Z <= C;
end if;
end process;
end USE_IF;

VHDL Application Workshop page 88


Selected Signal Assignment
entity BRANCH is
port ( A, B, C, X : in integer range 0 to 7 ;
Z : out integer range 0 to 0 ); n Concurrent version of “case”
end BRANCH; n Only one target
architecture USE_IF of BRANCH is n Rule as for case
begin
process (A, B, C, X)
begin
case X is architecture USE_WITH of BRANCH is
when 0 to 4 => begin
Z <= B; with X select
when 5 => Z <= B when 0 to 4 ,
Z <= C; C when 5 ,
when OTHERS => A when OTHERS;
Z <= A; end USE_WITH;
end case;
end process;
end USE_IF;

VHDL Application Workshop page 89


Block/Guarded Statement
Syntax : n Concurrent statement
label: block (optional_guard_condition)
declarations n Without a guard condition a
begin block is a grouping together of
concurrent_statements
... concurrent statements within an
end block label;
architecture
Example
B1 : block (ENB = ‘1’) Level-Sensitive Latch
begin
DOUT <= guarded DIN;
end block B1;

B2 : block (CLK’event and CLK = ‘1’) Edge-Sensitive Flip-Flop


begin
DOUT <= guarded DIN;
end block B2;
VHDL Application Workshop page 90
Subprograms
Aim and Topics
n Explain these two types of subprogram
n Subprogram concepts
n Funtions
n Procedure
n Resolution functions

VHDL Application Workshop page 91


Functions and Procedures
-------------
--------- Input parameters n “Subprograms”
------------ ------
-------- -------
-------- u Sequential statements
---------
------ Return value --------
-------- execute in sequence like
---------- ------ F
“software”
n Function
u One return value
------------- Procedure
--------- Input parameters n
------------ ------ u Zero or more return value
-------- -------
--------
--------- --------
------ --------
---------- ------
Output parameters

VHDL Application Workshop page 92


Function declaration
Syntax :
function function_name (parameter_list) return type is
types, constants, other functions, other declarations
begin
sequential_statements
...
end function_name;
entity . . . is
end . . .
architecture . . . of . . . is
begin
process
Function Declaration
begin
Function Calls
end process;
end . . . ;

VHDL Application Workshop page 93


Function Examples
Parameter list
function PARITY_FUNC ( X : std_ulogic_vector )
return std_ulogic is
Function name variable TMP : std_ulogic ;
begin
TMP := ‘0’ ;
for J in X’range loop
TMP := TMP xor X(J);
end loop;
return TMP;
Returned value
end PARITY_FUNC;

VHDL Application Workshop page 94


Function Call
function PARITY_FUNC ( X : std_ulogic_vector )
return std_ulogic is
8 variable TMP : std_ulogic ;
DATA_BYTE begin
PARITY_BYTE TMP := ‘0’ ;
for J in X’range loop
16 TMP := TMP xor X(J);
DATA_WORD PARITY_WORD end loop;
return TMP;
end PARITY_FUNC;
entity PARITY is
port ( DATA_BYTE : in std_ulogic_vector(7 downto 0);
DATA_WORD : in std_ulogic_vector(15 downto 0);
PARITY_BYTE : out std_ulogic;
PARITY_WORD : out std_ulogic );
end PARITY;
architecture FUNC of PARITY is Actual parameter
- - function declaration
begin
PARITY_BYTE <= PARITY_FUNC(DATA_BYTE);
PARITY_WORD <= PARITY_FUNC(DATA_WORD);
end FUNC;
VHDL Application Workshop page 95
Procedure declaration
Syntax :
procedure procedure_name (parameter_list) is
types, constants, other functions, other declarations
begin
sequential_statements
...
end procedure_name;
entity . . . is
end . . .
architecture . . . of . . . is
begin
process
Procedure Declaration
begin
Procedure Calls
end process;
end . . . ;

VHDL Application Workshop page 96


Procedure Examples
Procedure name Parameter list

procedure PARITY_PROC ( X : std_ulogic_vector;


signal PARITY_BIT : out std_ulogic) is
variable TMP : std_ulogic;
begin
TMP := ‘0’;
for J in X’range loop
TMP := TMP xor X(J);
end loop;
PARITY_BIT <= TMP;
end PARITY_PROC;
Assignment to “out” parameters

VHDL Application Workshop page 97


Procedure Call
procedure PARITY_PROC ( X : std_ulogic_vector;
8 signal PARITY_BIT : out std_ulogic) is
DATA_BYTE variable TMP : std_ulogic;
PARITY_BYTE begin
TMP := ‘0’;
16 for J in X’range loop
DATA_WORD PARITY_WORD TMP := TMP xor X(J);
end loop;
PARITY_BIT <= TMP;
end PARITY_PROC;
entity PARITY is
port ( DATA_BYTE : in std_ulogic_vector(7 downto 0);
DATA_WORD : in std_ulogic_vector(15 downto 0);
PARITY_BYTE : out std_ulogic;
PARITY_WORD : out std_ulogic );
end PARITY; Actual paramenter
architecture PROC of PARITY is
- - procedure declaration
begin
PARITY_PROC(DATA_BYTE, PARITY_BYTE);
PARITY_PROC(DATA_WORD, PARITY_WORD);
end PROC;
VHDL Application Workshop page 98
Defining Subprograms
package P_FUNCS is n Package
architecture FUNCTIONS of PARITY is
function PARITY_FUNC ( X : std_ulogic_vector) n Architecture
return std_ulogic is
variable TMP : std_ulogic; n Process
begin
TMP : = '0' ;
for J in X'range loop function PARITY_FUNC( x : std_ulogic_vector)
TMP := TMP xor X(J); return std_ulogic ;
end loop; end P_FUNCS;
return TMP; package body P_FUNCS is
end PARITY_FUNC; function PARITY_FUNC(x : std_ulogic_vector)
begin return std_ulogic;
PARITY_BYTE <= PARITY_FUNC(DATA_BYTE); variable TMP : std_ulogic;
PARITY_WORD <= PARITY_FUNC(DATA_WORD); begin
end FUNCTIONS; TMP : = '0' ;
for J in X'range loop
TMP := TMP xor X(J);
end loop;
return TMP;
end PARITY_FUNC;
end P_FUNC

VHDL Application Workshop page 99


Type Qualification
use work.P_ARITHMETIC.all;
entity QUALIFY is
port (A, B : in UNSIGNED (3 downto 0); package P_ARITHMETIC is
EQU : out boolean ); ...
end QUALIFY; subtype UNSIGNED is std_logic_vector;
type SIGNED is array (INTEGER range <>) of std_ulogic;
...
architecture BEH of QUALIFY is function "<=" (L: UNSIGNED;R: UNSIGNED) return BOOLEAN;
begin function "<=" (L: SIGNED; R: SIGNED) return BOOLEAN;
process (A, B) function "<=" (L: UNSIGNED;R: SIGNED) return BOOLEAN;
begin function "<=" (L: SIGNED; R: UNSIGNED) return BOOLEAN;
EQL <= (A <= B); function "<=" (L: UNSIGNED;R: INTEGER) return BOOLEAN;
function "<=" (L: INTEGER; R: UNSIGNED) return BOOLEAN;
-- This is legal function "<=" (L: SIGNED; R: INTEGER) return BOOLEAN;
EQL <= (A <= "1001"); function "<=" (L: INTEGER; R: SIGNED) return BOOLEAN;
-- This is ambiguous ...
EQL <= (A <= UNSIGNED("1001"));
-- This is legal
end process;
end BEH;

VHDL Application Workshop page 100


Resolution Functions
architecture CONCURRENT of MULTIPLE is A
signal A, B, C, D : integer; +
signal Z : RESOLVED_INTEGER; B
begin ? Z
Z <= A + B; C
Z <= C + D; +
end CONCURRENT;
D
architecture SEQUENTIAL of MULTIPLE is
signal Z, A, B, C, D : integer;
begin C
process (A, B, C, D) + Z
begin
Z <= A + B; D
Z <= C + D;
end process;
end SEQUENTIAL;
VHDL Application Workshop page 101
Resolution Function Definition
subtype STD_LOGIC is RESOLVED STD_ULOGIC;
function RESOLVED (S : STD_ULOGIC_VECTOR) return STD_ULOGIC is
variable RESULT : STD_ULOGIC := '-'; --weakest state default
begin
if (S'lenght = 1) then return S(S'low);
else
-- Iterate through all inputs
for I in S'range loop
RESULT := RESOLUTION_TABLE(RESULT, S(i));
end loop; CONSTANT resolution_table : stdlogic_table := (
-- Return resultanty value -- ---------------------------------------------------------
return RESULT; -- | U X 0 1 Z W L H - | |
end if; -- ---------------------------------------------------------
end RESOLVED; ( 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U' ), -- | U |
( 'U', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X' ), -- | X |
( 'U', 'X', '0', 'X', '0', '0', '0', '0', 'X' ), -- | 0 |
( 'U', 'X', 'X', '1', '1', '1', '1', '1', 'X' ), -- | 1 |
( 'U', 'X', '0', '1', 'Z', 'W', 'L', 'H', 'X' ), -- | Z |
? ( 'U', 'X', '0', '1', 'W', 'W', 'W', 'W', 'X' ),-- | W |
( 'U', 'X', '0', '1', 'L', 'W', 'L', 'W', 'X' ), -- | L |
( 'U', 'X', '0', '1', 'H', 'W', 'W', 'H', 'X' ), -- | H |
( 'U', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X' ) -- | - |
);
VHDL Application Workshop page 102
Advanced Topics
Aim and Topics
n Advanced types
n Overloading
n Definition RTL Code
n Synthesis Coding Styles

VHDL Application Workshop page 103


Advanced Types

n Enumerated types
n Subtypes
n Composite types
u Arrays
u Array of array
u Record

VHDL Application Workshop page 104


Enumerated Type Definitions
n VHDL allows the user to define his own type
u “Enumerated Type”
n Types cannot be intermixed !

Beware !
Sythesis tools usually offer a way to map each enumeration to a bit pattern

VHDL Application Workshop page 105


Aggregates
signal A_BUS, A_BUS, Z_BUS : bit_vector (3 downto 0);
signal A_BIT, B_BIT, C_BIT, D_BIT : bit;
signal BYTE : bit_vector (7 downto 0);

Z_BUS <= ( A_BIT, B_BIT, C_BIT, D_BIT );

aggregates

BYTE <= ( 7 => ‘1’, 5 downto 1 => ‘1’, 6 => B_BIT, OTHERS => ‘0’ );

Beware !
Some low cost synthesis tools may not support aggregates

VHDL Application Workshop page 106


Subtype declaration

Syntax :
subtype subtype_name is base_type range range_constraint;

Example
type WORD is array (31 downto 0) of bit;
subtype NIBBLE is WORD range 3 downto 0;
subtype BYTE is WORD range 7 downto 0;
subtype MY_BUS is WORD range 15 downto 0;

VHDL Application Workshop page 107


Array type declaration
Syntax :
type type_name is array (array_range) of array_type;

Example
type WORD8 is array (1 to 8) of bit; Equivalent statements
type WORD8 is array (integer range 1 to 8) of bit;
type BYTE is array (7 downto 0) of bit;
type WORD is array (31 downto 0) of bit;
type MEMORY is array (0 to 4095) of word;
type T_CLOCK_TIME is array (3 downto 0) of integer;

VHDL Application Workshop page 108


Arrays Assignments
signal Z_BUS : bit_vector (3 downto 0);
signal C_BUS : bit_vector (0 to 3);

Z_BUS <= C_BUS;


Z_BUS (3) <---------- C_BUS (0)
Z_BUS (2) <---------- C_BUS (1)
Z_BUS (1) <---------- C_BUS (2)
Z_BUS (0) <---------- C_BUS (3)

Beware !
n Size of arrary on left and right must be equal

n Elements are assigned by postion, not element number

VHDL Application Workshop page 109


Slice of an Arrays
signal Z_BUS, ABUS : bit_vector (3 downto 0);
signal B_BIT : bit;
signal BYTE : bit_vector (7 downto 0);

BYTE(5 downto 2) <= A_BUS;


Z_BUS(1 downto 0) <= ‘0’ & B_BIT;

B_BIT <= A_BUS (0);


Z_BUS <= A_BUS;

Beware !
The slice direction must be the same as the signal declaration
BYTE(5 downto 2) <= A_BUS;
Z_BUS(0 to 1) <= ‘0’ & B_BIT;
VHDL Application Workshop page 110
Multi-Dimensional Arrays

type MEMORY is array ( 0 to 7, 0 to 3) of bit;


constant ROM : MEMORY := ( (‘0’, ‘0’, ‘0’, ‘0’),
(‘0’, ‘0’, ‘0’, ‘1’),
(‘0’, ‘0’, ‘1’, ‘0’),
(‘0’, ‘0’, ‘1’, ‘1’),
(‘0’, ‘1’, ‘0’, ‘0’),
(‘0’, ‘1’, ‘0’, ‘1’),
(‘0’, ‘1’, ‘1’, ‘0’),
(‘0’, ‘1’, ‘1’, ‘1’) );

Example Reference : DATA_BIT := ROM(5, 3);

VHDL Application Workshop page 111


Array of Arrays
type WORD is array ( 0 to 3 ) of bit;
type MEMORY is array (0 to 4) of WORD;
variable ADDR, INDEX : integer;
variable DATA : WORD;
constant ROM : MEMORY := ( (‘0’, ‘0’, ‘0’, ‘0’),
(‘0’, ‘0’, ‘0’, ‘1’),
(‘0’, ‘0’, ‘1’, ‘0’),
(‘0’, ‘0’, ‘1’, ‘1’),
(‘0’, ‘1’, ‘1’, ‘1’) );
DATA := ROM(addr) ;
ROM(ADDR) (INDEX)

To access a single bit

VHDL Application Workshop page 112


Records declaration
address

b7 b6 b5 b4 b3 b2 b1 b0
Parity (ODD)
data nibble A
‘1’ (high byte)
type T_PACKAGE is record
BYTE_ID : bit;
PARITY : bit;
ADDRESS : integer range 0 to 3;
DATA : bit_vector (3 downto 0 );
end record;
signal TX_DATA, RX_DATA : T_PACKAGE;
...
RX_DATA <= TX_DATA;
TX_DATA <= ( ‘1’, ‘0’, 2, “0101”);
TX_DATA.ADDRESS <= 3;
Reference name record
VHDL Application Workshop page 113
Overloading

std_ulogic_vector
std_ulogic_vector n Re-define operators
+
std_ulogic_vector n Different data types
n Called in context
std_ulogic_vector
std_ulogic_vector
+
integer

integer
std_ulogic_vector
+
std_ulogic_vector

VHDL Application Workshop page 114


Subprogram Overloading
package P_SUBP is
function SINE (L : integer) return real; --1
function SINE (L : real) return real; --2
function SINE (L : std_logic_vector) return real; --3
use work. P_SUBP.all;
entity OVERLOADED is
port ( A_BUS : in std_ulogic_vector (3 downto 0);
B_INT : in integer range 0 to 15;
C_REAL : in real;
A, B, C : out real );
end OVERLOADED;
architecture A of OVERLOADED is
begin
A <= SINE(A_BUS); Function 3
B <= SINE(B_INT); Function 1
C <= SINE(C_REAL); Function 2
end A;
n Any subprogram can be overloaded
VHDL Application Workshop page 115
Argument Overloading
package P_AVERAGE is
function AVERAGE (A, B : integer) return integer; --1
function AVERAGE (A, B, C : integer) return integer; --2
function AVERAGE (A, B, C, D : integer) return integer; --3
end P_AVERAGE ;
use work. P_AVERAGE.all;
entity OVERLOADED is
port ( A1, B1, C1, D1 : in integer;
V1, V2, V3 : out integer );
end OVERLOADED;
architecture ARG_OVER of OVERLOADED is
begin
V1 <= AVERAGE(A1, B1, C1); Function 2
V2 <= AVERAGE(A1, C1); Function 1
V3 <= AVERAGE(A1, B1, C1, D1); Function 3
end ARG_OVER;
arguments
VHDL Application Workshop page 116
Operator Overloading
package P_ARITHMETIC is
function “+” (L: std_ulogic_vector; R: std_ulogic_vector) return integer; --1
function “+” (L: std_ulogic_vector; R: std_ulogic_vector) return std_ulogic_vector; - - 2
function “+” (L: std_ulogic_vector; R: integer) return std_ulogic_vector; --3
function “+” (L: integer; R: std_ulogic_vector) return std_ulogic_vector; --4
use work.P_ARITHMETIC.all;
entity OVERLOADED is
port ( A_BUS, B_BUS : in std_ulogic_vector (3 downto 0);
A_INT, B_INT : in integer range 0 to 15;
Y_BUS, Z_BUS : out std_ulogic_vector (3 downto 0);
Y_INT, Z_INT : out integer range 0 to 15 );
end OVERLOADED;
architecture A of OVERLOADED is
begin
Y_INT <= A_INT + B_INT;
Z_INT <= A_BUS + B_BUS; Function 1
Z_BUS <= A_BUS + B_BUS; Function 2
Y_BUS <= A_BUS + A_INT; Function 3
Z_BUS <= A_BUS + B_INT; Function 4
end A;
VHDL Application Workshop page 117
RTL (Register Transfer Level) Style

VHDL Application Workshop page 118


Complete Sensitivity Lists

process (A, B, SEL) n List all signals read


begin
if (SEL = ‘1’) then n Synthesis : complete for combinational logic
Z <= A;
else
Z <= B;
end if;
end process;

VHDL Application Workshop page 119


Incomplete Assignments
Library IEEE;
use IEEE.Std_Logic_1164.all;
n What is the value of Q if EN = ‘0’ ?
entity INCOMP_IF is What hardware should be built if this
port ( EN, D : in std_ulogic; n
Q : out std_ulogic ); is synthesised?
architecture A of INCOMP_IF is * Answer Latch !
begin
process (EN, D)
begin
if (EN = ‘1’) then
Q <= D;
end if;
end process;
end A;

VHDL Application Workshop page 120


Rules for Synthesis of Combinational Logic
n Complete sensitivity list
n Default assignments to prevent latches

Sensitivity list
process (A, B, SEL) process (EN, D)
begin begin
if (SEL = ‘1’) then if (EN = ‘1’) then
Z <= A; Q <= D;
else end if;
Z <= B; end process;
end if;
end process;
What is the value of Q if EN = ‘0’ ?

VHDL Application Workshop page 121


“Ideal” Design Flow
n Ideally do RTL and gate simulation in same simulator
n Historical issues
u Library standrad
u Slow simulation

RTL Level VHDL

VHDL Library
Simulation
Library Synthesis

Gate Level VHDL Testbench

VHDL Application Workshop page 122


Describing a Rising Clock for Synthesis
process process
begin begin
wait until CLK’event and CLK = ‘1’ wait until CLK= ‘1’;
and CLK’last_value = ‘0’ ; Q <= D;
Q <= D; end process;
end process;

process process (CLK)


begin begin
wait until CLK’event and CLK = ‘1’; if (CLK = ‘1’) then
Q <= D; Q <= D;
end process; end if;
end process;
process (CLK)
begin
if (CLK’event and CLK = ‘1’) then
Q <= D;
end if;
end process;
VHDL Application Workshop page 123
Register Inference in Synthesis
Library IEEE;
use IEEE.Std_logic_1164.all; n Registers are infered on all signal
entity COUNTER is
port ( CLK : in std_ulogic;
assignments in clocked processes
Q : out integer range 0 to 15 );
end COUNTER;
architecture A of COUNTER is
signal COUNT : integer range 0 to 15 ; count
begin
process (CLK)
begin
q

if CLK’event and CLK = ‘1’ then


if (COUNT >= 9) then
COUNT <= 0; clk
else
COUNT <= COUNT + 1;
end if;
end if;
end process;
Q <= COUNT
end A;
VHDL Application Workshop page 124
Asynchronous Reset Registers
Library IEEE;
use IEEE.Std_logic_1164.all;
entity ASYNC_FLOP is
port ( D, CLK, RST : in std_ulogic;
Q : out std_ulogic ); n If / Elsif structure
end ASYNC_FLOP; u final elsif has edge
architecture B of ASYNC_FLOP is u no else
begin n Has a sensitivity list !!
process (CLK, RST)
begin
if (RST = ‘1’) then
Q <= ‘0’;
elsif (CLK’event and CLK = ‘1’) then
Q <= D;
end if;
end process;
end B;
VHDL Application Workshop page 125
Clocked Process Rules
process (CLK, RST)
begin
if (RST = ‘1’) then
- - reset all registers n Wait form
elsif (CLK’event and CLK = ‘1’) then u No sensitivity list
- - all combination logic n If form
end if; u clock and reset only in sensitivity list
end process; n All signals assigned get a register
process (CLK)
begin
if (CLK’event and CLK = ‘1’) then
- - all combination logic
end if;
end process;

VHDL Application Workshop page 126


Testbench Coding Styles
Aim and Topics
n Testbench configurations
n Stimulus styles
n Assertions

VHDL Application Workshop page 127


Testbench Organisation
Testbench
n Simple testbench
u Just send data to design
Design to
Stimulus u No interation
verify
u Few processes

n Sophisticated testbench
u Models environment around design
Testbench Design to u Talks to design
(environment) verify
u Evolves towards boards model

VHDL Application Workshop page 128


Stimulus from Loops
Library IEEE;
use IEEE.Std_logic_1164.all;
entity TESTBENCH is
end TESTBENCH;
architecture USE_LOOP of TESTBENCH is
signal A_BUS : std_ulogic_vector (7 downto 0);
begin
process
begin
for I in 0 to 4 loop
A_BUS <= To_Std_ulogic( I, A_BUS’length);
wait for 10 ns;
end loop;
end process;
end USE_LOOP;
VHDL Application Workshop page 129
Stimulus from Array Constant
architecture USE_ARRAY of TESTBENCH is
signal A_BUS : std_ulogic_vector (7 downto 0);
type T_DATA is array (0 to 4) of std_ulogic_vector(7 downto 0);
constant DATA : T_DATA := ( “00000000”,
“00000001”,
“00000010”,
“00000011”,
“00000100” );
begin
process
begin
for I in DATA’range loop
A_BUS <= DATA(I);
wait for 10 ns;
end loop;
end process;
end USE_ARRAY;

VHDL Application Workshop page 130


“In line” Stimulus
architecture IN_LINE of TESTBENCH is
signal A_BUS : std_ulogic_vector (7 downto 0);
begin
process
begin
A_BUS <= “00000000”;
wait for 10 ns;
A_BUS <= “00000001”;
wait for 10 ns;
- - ETC, ETC
end process;
end IN_LINE;

VHDL Application Workshop page 131


Assertions
Syntax :
assert condition
report string_expression
severity severity_level;
architecture BEHAVE of TESTBENCH is n Severity levels defined in
signal RESULT : bit; package STANDARD
...
begin u NOTE
... u WARNING
process u ERROR (default)
constant EXPECTED : bit := ‘1’; u FAILURE
begin (usually causes simulation to halt)
assert RESULT = EXPECTED
report “Unexperted result !”
severity ERROR;
wait; Print if condition is false
end process;
...
end BEHAVE; VHDL Application Workshop page 132
Creating Clock and Resets
signal CLK : std_logic := ‘0’;
constant PERIOD : time := 50 ns;
CLK <= not CLK after PERIOD/2;
RESET <= ‘1’ , ‘0’ after 3 * PERIOD;
constant INITIAL_CLOCK : std_logic := ‘1’;
constant MAX_CYCLES : integer := 1000 ;
constant SIM_END_TIME : time := PERIOD * MAX_CYCLES;
process
begin
while NOW <= SIM_END_TIME loop
CLK <= INITIAL_CLOCK;
wait for PERIOD/2;
CLK <= not INITIAL_CLOCK;
wait for PERIOD/2;
end loop;
assert FALSE
report “ Simulation is Over !”;
severity FAILURE;
end process;
VHDL Application Workshop page 133
Stimulus from a File

Testbench Design to
(environment) verify

n E.g. image data


n Created by another program/tool

VHDL Application Workshop page 134


Reading from a File
use Std.TextIO.all;
architecture READFILE of TESTBENCH is
signal A_INT, B_INT : integer;
begin
process
variable L : line; - - stroes a complete line from file
variable INT : integer; - - reference to specific file
file TEXT_FILE : text is in “stimulus.vec”;
begin
while not endfile(TEXT_FILE) loop
readline(TEXT_FILE, L); - - read a complete line into VECTOR
read(L, INT); - - read the first integer into INT
A_INT <= INT;
read(L, INT); - - read the second integer into INT
B_INT <= INT; 1 45
wait for 10 ns; 23 21
end loop; 32 1 stimulus.vec
end process; 45 54
end READFILE; 104 110
VHDL Application Workshop page 135
TextIO Capabilities
n Able to read/write
u bit, bit_vector
u boolean
Testbench Design to u character, string
(environment) verify u integer, real
u time
n Procedures used for file IO:
u read(. . .)
u readline(. . .)
u write(. . .)
u writeline(. . .)
n Reference
u LRM section 14.3

VHDL Application Workshop page 136


TextIO Advantages

Testbench Design to n Stimulus easy changing


(environment) verify n Less analysis/compile time

VHDL Application Workshop page 137


Use of Subprograms
procedure MEM_WRITE (DATA: in T_DATA; ADDR: in T_ADDR) is
begin
- - Memory write procedure
end MEM_WRITE;
procedure MEM_READ (DATA: out T_DATA; ADDR: in T_ADDR) is
begin
- - Memory write procedure
end MEM_READ; procedure MEM_INIT( ) is
begin
MEM_WRITE(“1000”, “00”);
MEM_WRITE(“0100”, “01”);
MEM_WRITE(“0010”, “10”);
MEM_WRITE(“0001”, “11”);
n Testbench is behavioural end MEM_INIT;
procedure MEM_TEST( ) is
variable VALUE_READ : T_DATA;
n Gets large quickly begin
MEM_INIT( );
n Use software style structure MEM_READ (VALUE_READ, “01”);
if VALUE_READ /= “0100” then
u Function and procedures ...
end MEM_TEST;

VHDL Application Workshop page 138


Output from Simulation

Testbench Design to
(environment) verify
input

Errors Warnings Visualisation Result for Result for Manufacturing


comparison analysis test vectors

VHDL Application Workshop page 139


Output from Simulation

Testbench Design to
(environment) verify
input

n Can output to
u Simulator output (assertion)
u File (textIO)

Errors Warnings n Best to keep errors and warnings


separate
u Errors : have to fix these !
u Warnings : help to debug issues

VHDL Application Workshop page 140


Visualised Output

Testbench Design to
(environment) verify
input

Visualised
Output
VHDL Application Workshop page 141
Results for Comparion
n Compare results for different
Testbench Design to simulations
(environment) verify n Essential for large designs
input u Studying waveforms very
tedious !
n If comparing gbetween
abstraction levels
Compare (diff) u (Behavioural/RTL, RTL/Gates)
u Output sequence of data values
u Time independent
Visualised Known good
Output results

VHDL Application Workshop page 142


Results for Analysis
n When simulation goes wrong
Testbench Design to n More detail than results for
(environment) verify comparison
input n Do not mix results for
comparison with results for
analysis

Results for
analysis

VHDL Application Workshop page 143


Manufacturing Test Vectors
n May use simulation output for
Testbench Design to part/all of manufacturing test
(environment) verify n Strobe values when they will
input be stable in gate level design

Manufacturing
test vectors

VHDL Application Workshop page 144


VHDL & Logic Synthesis
Aim and Topics
n Combinational circuit Synthesis
u Multiplexer
u Encoder & Decoder
u Tri-State buffer
u Bidirection buffer
u Arithmetic Logic Unit (ALU)
u Finite State Machine
n Sequential circuit Synthesis
u D-Latch
u Asynchonous Reset Flip-Flop
u Synchonous Reset Flip-Flop

VHDL Application Workshop page 145


Basic Design Methodology
Requirements

RTL Model Simulate

Synthesize

Gate-level
Model Simulate Test Bench

ASIC or FPGA Place & Route

Timing
Model Simulate
VHDL Application Workshop page 146
2 to 1 Multiplexer

Library IEEE;
use IEEE.Std_logic_1164.all;
Synthesized Circuit
entity MUX_2_1 is
port ( A, B : in std_logic;
SEL : in std_logic;
Q : out std_logic );
end MUX_2_1;
architecture B of MUX_2_1 is
begin
Q <= A when (SEL = '0') else B;
end B;

VHDL Application Workshop page 147


4 to 1 Multiplexer
Library IEEE;
use IEEE.Std_logic_1164.all;
entity MUX_4_1 is
port( A, B, C, D : in std_ulogic_vector(3 downto 0);
SEL : in std_ulogic_vector(1 downto 0);
Q : out std_ulogic_vector(3 downto 0) );
end MUX_4_1;
architecture B of MUX_4_1 is
begin
process(A, B, C, D, SEL)
begin
if (SEL = "00") then
Q <= A;
elsif (SEL = "01") then
Q <= B;
elsif (SEL = "10") then
Q <= C;
else
Q <= D;
end if;
end process;
end B;
Synthesized Circuit
VHDL Application Workshop page 148
8 to 3 Encoder Synthesized Circuit
library IEEE;
use IEEE.Std_Logic_1164.all;
entity ENCODE_8_3 is
port(A : in std_ulogic_vector(7 downto 0);
Q : out std_ulogic_vector(2 downto 0) );
end ENCODE_8_3;
architecture BEHAVE of ENCODE_8_3 is
begin
P1: process(A)
begin
case A is
when "00000001" => Q <= "000";
when "00000010" => Q <= "001";
when "00000100" => Q <= "010";
when "00001000" => Q <= "011";
when "00010000" => Q <= "100"; Q <= "000" when A = "00000001" else
when "00100000" => Q <= "101";
when "01000000" => Q <= "110"; "001" when A = "00000010" else
when "10000000" => Q <= "111"; "010" when A = "00000100" else
when others => Q <= "XXX"; "011" when A = "00001000" else
end case; "100" when A = "00010000" else
end process P1; "101" when A = "00100000" else
end BEHAVE; "110" when A = "01000000" else
"111" when A = "10000000" else
"XXX";
VHDL Application Workshop page 149
3 to 8 Decoder Synthesized Circuit
use IEEE.Std_Logic_1164.all;
entity DECODE_8_3 is
port(A : in std_ulogic_vector(2 downto 0);
Q : out std_ulogic_vector(7 downto 0) );
end DECODE_8_3;
architecture BEHAVE of DECODE_8_3 is
begin
P1: process(A)
begin
case A is
when "000" => Q <= "00000001";
when "001" => Q <= "00000010";
when "010" => Q <= "00000100";
when "011" => Q <= "00001000";
when "100" => Q <= "00010000";
when "101" => Q <= "00100000";
when "110" => Q <= "01000000";
when others => Q <= "10000000";
end case;
end process P1;
end BEHAVE;

VHDL Application Workshop page 150


Tri-State buffers
library IEEE; Synthesized Circuit
use IEEE.Std_Logic_1164.all;
entity TRI_STATE is
port(A : in std_ulogic_vector(7 downto 0);
EN : in std_ulogic;
Q : out std_ulogic_vector(7 downto 0) );
end TRI_STATE;
architecture RTL of TRI_STATE is
begin
process(A, EN)
begin
if (EN = '1') then
Q <= A;
else
Q <= "ZZZZZZZZ";
end if;
end process;
end RTL;

VHDL Application Workshop page 151


Bidirectional Buffers
library IEEE;
Synthesized Circuit
use IEEE.Std_Logic_1164.all;
entity BIDIR is
port (INP: in std_logic_vector(7 downto 0);
EN: in std_logic;
INOUTP: inout std_logic_vector(7 downto 0)
);
end BIDIR;
architecture RTL of BIDIR is
begin
INOUTP <= INP when EN = '1' else
"ZZZZZZZZ";
end RTL;

VHDL Application Workshop page 152


4-Bit Arithmetic Logic Unit (ALU)
A ALU4BIT Q begin
Q <= A and B process(A, B, CIN, MODE)
B Q <= A or B variable VSUM: std_logic_vector(3 downto 0);
variable CY: std_logic;
MODE Q <= A xor B COUT begin
CIN Q <= A + B case MODE is
when MODE_AND =>
Q <= A and B;
COUT <= '-';
Library IEEE; when MODE_OR =>
Use IEEE.Std_logic_1164.all; Q <= A or B;
COUT <= '-';
entity ALU4BIT is when MODE_XOR =>
port( A, B : in std_logic_vector(3 downto 0); Q <= A xor B;
MODE : in std_logic_vector(1 downto 0); COUT <= '-';
CIN : in std_logic; when others =>
Q : out std_logic_vector(3 downto 0); -- generate 4-bit adder
COUT : out std_logic ); CY := CIN;
end ALU4BIT; for I in 0 to 3 loop
VSUM(I) := (A(I) xor B(I)) xor CY;
architecture BEHAVE of ALU4BIT is CY := (A(I) and B(I)) or (CY and (A(I) or B(I)));
end loop;
constant MODE_AND: std_logic_vector(1 downto 0) := "00"; Q <= VSUM;
constant MODE_OR: std_logic_vector(1 downto 0) := "01"; COUT <= CY;
constant MODE_XOR: std_logic_vector(1 downto 0) := "10"; end case;
constant MODE_ADD: std_logic_vector(1 downto 0) := "11"; end process;
end BEHAVE;

VHDL Application Workshop page 153


Synthesized Circuit

VHDL Application Workshop page 154


Finite State Machine

VHDL Application Workshop page 155


Finite State Machine : Example
library IEEE; process (IN1, CUR_STATE)
use IEEE.Std_Logic_1164.all; begin
case CUR_STATE is
entity STATE_EX is when "00" =>
port( IN1, CLOCK, RESET : in std_logic; if (IN1 = '0’) then
OUT1 : out std_logic_vector (1 downto 0) ); NEXT_STATE <= "10";
end STATE_EX; OUT1 <= "00";
else
NEXT_STATE <= "01";
architecture STATE_EX_A of STATE_EX is OUT1 <= "10";
end if;
signal CUR_STATE : std_logic_vector( 1 downto 0); when "01" =>
signal NEXT_STATE : std_logic_vector(1 downto 0); if (IN1 = '0’) then
NEXT_STATE <= CUR_STATE;
OUT1 <= "01";
begin else
process(CLOCK, RESET) NEXT_STATE <= "10";
begin OUT1 <= "10";
if (CLOCK = '1' and CLOCK'event) then end if;
when "10" =>
if (RESET = '0’) then NEXT_STATE <= "11";
CUR_STATE <= "00"; OUT1 <= "10";
else when "11" =>
CUR_STATE <= NEXT_STATE; NEXT_STATE <= "00";
end if; OUT1 <= "10";
when others => null;
end if; end case;
end process; end process;
end STATE_EX_A;

VHDL Application Workshop page 156


Synthesized Circuit

VHDL Application Workshop page 157


D-Latchs
library IEEE;
use IEEE.Std_Logic_1164.all;
Synthesized Circuit
entity LATCH_4BIT is
port( D : in std_logic_vector(3 downto 0);
EN : in std_logic;
Q : out std_logic_vector(3 downto 0) );
end LATCH_4BIT;
architecture BEHAVE of LATCH_4BIT is
begin
P1: process(D, EN)
begin
if (EN = '1') then
Q <= D;
end if;
end process P1;
end BEHAVE; B1 : block (EN = ‘1’)
begin
Q <= guarded D;
end block ;

VHDL Application Workshop page 158


Asynchronous Reset Flip-Flop
Library IEEE;
use IEEE.Std_logic_1164.all; Synthesized Circuit
entity ASYNC_FLOP is
port ( D, CLK, RST : in std_ulogic;
Q : out std_ulogic );
end ASYNC_FLOP;
architecture B of ASYNC_FLOP is
begin
process (CLK, RST)
begin
if (RST = ‘1’) then
Q <= ‘0’;
elsif (CLK’event and CLK = ‘1’) then
Q <= D;
end if;
end process;
end B;

VHDL Application Workshop page 159


Synchronous Reset Flip-Flop
Library IEEE;
use IEEE.Std_logic_1164.all;
entity SYNC_FLOP is
port ( D, CLK, RST : in std_ulogic;
Synthesized Circuit
Q : out std_ulogic );
end SYNC_FLOP;
architecture B of SYNC_FLOP is
begin
process
begin
wait until (CLK’event and CLK = ‘1’);
if (RST = ‘1’) then
Q <= ‘0’;
else
Q <= D;
end if;
end process;
end B;

VHDL Application Workshop page 160


Synthesis Adder problem ?
A B
Q <= A + B +
Q
n Carry-look-ahead circuits are faster but larger than ripple-carry circuits
Synthesized Circuit
Ripple-carry Adder Carry-look-ahead Adder

VHDL Application Workshop page 161


CASE style systhesis problem ?
case b is
library ieee; when "01" => out1 <= '0';
use ieee.std_logic_1164.all; out2 <= '1';
when "10" => out1 <= '1';
entity case_latch is out2 <= '0';
port (in1, in2 : in std_logic; when others => out1 <= '1';
out1, out2 : out std_logic); out2 <= ’0';
end case_latch; end case;
architecture case_latch_a of case_latch is
signal b : std_logic_vector (1 downto 0);
begin
process (b)
begin
case b is
when "01" => out1 <= '0';
out2 <= '1';
when "10" => out1 <= '1';
out2 <= '0';
when others => out1 <= '1';
end case;
end process;
b <= in1 & in2;
end case_latch_a;

Latch VHDL Application Workshop page 162

Potrebbero piacerti anche