VHDL: A Tutorial!
VHDL: A Tutorial!
Mani B. Srivastava
UCLA - EE
OUTLINE
n Language features
mbs
2
WHAT IS VHDL?
- Timing constructs
mbs
3
A NAND Gate Example
A Y
B
-- black-box definition (interface)
entity NAND is
generic ( Tpd : time := 0 ns );
port ( A, B : in bit; Y : out bit );
end entity;
-- an implementation (contents)
architecture BEHAVIOR_1 of NAND is
begin
Y <= A nand B after Tpd;
end BEHAVIOR_1;
Important Concepts
entity
architecture
generic
port
waveform assignment
mbs
4
Another Implementation of NAND
A Y
B
Important Concepts
multiple architectures
signal
concurrent statements
mbs
5
Yet More NAND Gates!!!
entity NAND_N is
generic ( N : integer := 4; Tpd : time);
port ( A, B : in bit_vector(1 to N);
Y : out bit_vector(1 to N));
end NAND_N;
Important Concepts
process
variable
wait
sequential statements
events
mbs
6
The process Statement
mbs
7
The wait Statement
examples:
-- wait for a rising or falling edge on CLK
wait on CLK;
wait until CLK’EVENT; -- this is equivalent to the above
-- wait for 10 ns
wait until 10 ns;
mbs
8
A Simple Producer-Consumer Example
DATA
P REQ C
ACK
entity producer_consumer is
end producer_comsumer;
mbs
9
Producer-Consumer contd. : 4-ϕ case
DATA
P REQ C
ACK
mbs
10
Muller C-Element
C C
entity MULLER_C_ELEMENT is
port (A,B : in bit; C : out bit);
end MULLER_C_ELEMENT;
architecture BEHAVIOR is
begin
process begin
wait until A=’1’and B=’1’;
C <= ‘1’;
wait until A=’0’and B=’0’;
C <= ‘0’;
end process;
end BEHAVIOR;
mbs
11
An Edge-Triggered D Flip-Flop
entity DFF is
generic (T_setup, T_hold, T_delay : time:=0 ns);
port (D, CLK: in bit; Q : out bit);
begin
-- check setup time
assert not (CLK’EVENT and CLK=’1’and
D’LAST_EVENT < T_setup)
report “Setup violation”
severity WARNING;
-- check hold time
assert not (CLK’DELAYED(T_hold)’EVENT and
CLK’DELAYED(Thold)=’1’and
D’LAST_EVENT < T_hold)
report “Hold violation”
severity WARNING;
end DFF;
architecture BEHAVIOR of DFF is
begin
process begin
wait on CLK until CLK=’1’;
Q <= D after T_delay;
end process;
end BEHAVIOR;
mbs
12
Behavior vs Structure Description
A
A
Y G2 Y
Y = A.B+A.B G1 G4
G3
B
B
mbs
13
The Generate Statement
entity RIPPLE_ADDER is
port (A, B : in bit_vector; CIN : in bit;
SUM : out bit_vector; COUT : out bit);
begin
assert A’LENGTH=B’LENGTH and
A’LENGTH=SUM’LENGTH
report “Bad port connections”
severity ERROR;
end;
architecture STRUCTURE of RIPPLE_ADDER is
alias IN1 : bit_vector(0 to A’LENGTH-1) is A;
alias IN2 : bit_vector(0 to A’LENGTH-1) is B;
alias S : bit_vector(0 to A’LENGTH-1) is SUM;
signal C : bit_vector(IN1’RANGE );
component FULL_ADDER port (A,B,CIN: in bit; S, COUT: out bit);
end component;
begin
L1: for I in S’RANGE generate
L2: if I=0 generate
FA1: FULL_ADDER
port map (IN1(0),IN2(0),CIN,S(0),C(0));
end generate;
L3: if I>0 generate
FA2: FULL_ADDER
port map (IN1(I),IN2(I),C(I-1),S(I),C(I));
end generate;
end generate;
COUT <= C(C’HIGH);
end STRUCTURE;
mbs
14
Concurrent vs Sequential Statements
n Concurrent Statements
Process independent sequential process
Block groups concurrent statements
Concurrent Procedure convenient syntax for
Concurrent Assertion commonly occurring form
Concurrent Signal Assignment of processes
Component Instantiation structure decomposition
Generate Statement regular description
n Sequential Statements
Wait synchronization of processes
Assertion
Signal Assignment
Variable Assignment
Procedure Call
If
Case
Loop (for, while)
Next
Exit
Return
Null
mbs
15
VHDL’s Model of a System
P1 P2
KERNEL
or
SCHEDULER
PROCESS
P3 user-defined
processes
mbs
16
Simplified Anatomy of the
VHDL Kernel Process
vhdl_simulator_kernel()
{
/* initialization phase */
time = 0 ns;
for (each process P) {
run P until it suspends;
}
while TRUE do {
/* this is one simulation cycle ... */
if (no driver is active) {
time = next time at which a driver is active
or a process resumes;
if (time = TIME’HIGH) break;
}
update_signals(); /* events may occur */
for (each process P) {
if (P is sensitive to signal S and an event has
occurred on S in this cycle) {
resume P; /* put it on a list ... */
}
}
for (each process P that has just resumed) {
run P until it suspends;
}
}
}
mbs
17
Signals versus Variables
mbs
18
TRANSACTION SCHEDULING MODEL
TRANSPORT vs INERTIAL DELAY
0 2 4 6
0 2 4 6 0 2 3 5 7
3 5 7 0 2 3 5 7
3 5 7
0 2 4 6
0 2 4 6 0 3 5 7
3 5 7 0 3 5 7
Projected waveform
3 5 7 Preemptive timing
Transport delay
Inertial delay
mbs
19
Signals with Multiple Drivers
A B
Y <= A; -- in process1
and, Y <= B; -- in process2
mbs
21
How do VHDL and THOR differ?
THOR_PROCESS: process
begin
thor_init_section();
while TRUE loop
wait on list_of_input_and_biput_signals;
thor_body_section();
end loop;
end process THOR_PROCESS;
mbs
22
Using VHDL like C!
example:
mbs
23
Language Features: TYPES
VHDL TYPES :
SCALAR
ENUMERATION e.g. character, bit, boolean
INTEGER e.g. integer
FLOATING e.g. real
PHYSICAL e.g. time
COMPOSITE
ARRAY e.g. bit_vector, string
RECORD
ACCESS
FILE
examples:
type bit is (‘0’, ‘1’);
type thor_bit is (‘U‘, ‘0’, ‘1’, ‘Z’);
type memory_address is range 0 to 2**32-1;
type small_float is range 0.0 to 1.0;
type weight is range 0 to 1E8
units
Gm; -- base unit
Kg = 1000 Gm; -- kilogram
Tonne = 1000 Kg; -- tonne
end units;
mbs
24
Language Features: SUBTYPES
examples:
subtype natural is integer range 0 to integer’HIGH;
subtype good_thor_bit is thor_bit range ‘0’to ‘1’;
subtype small_float is real range 0.0 to 1.0;
examples of Array and Record types:
-- unconstrained array (defines an array type)
type bit_vector is array (natural range <>) of bit;
-- constrained array (define an array type and subtype)
type word is array (0 to 31) of bit;
-- another unconstrained array
type memory is array (natural range <>) of word;
-- following is illegal!
type memory is array (natural range <>) of bit_vector;
-- an example record
type PERSON is
record
name : string(1 to 20);
age : integer range 0 to 150;
end record;
mbs
25
Language Features: OVERLOADING
example:
mbs
26
Language Features: CONFIGURATIONS
entity data_path is
...
end data_path;
architecture INCOMPLETE of data_path is
component alu
port(function : in alu_function;
op1, op2 : in bit_vector_32;
result : out bit_vector_32);
end component;
begin
...
end INCOMPLETE;
configuration DEMO_CONFIG of data_path is
for INCOMPLETE
for all:alu
use entity work.alu_cell(BEHAVIOR)
port map (function_code => function,
operand1 => op1, operand2 => op2,
result => result, flags => open);
end for;
end for;
end DEMO_CONFIG;
mbs
27
Language Features: PACKAGES
A package has a
- declaration (interface), and a
- body (contents) [optional]
example:
package SIMPLE_THOR is
type thor_bit is (‘U’, ‘0’,’1’,’Z’);
function “and”(L,R: thor_bit) return thor_bit;
function “or”(L,R:thor_bit) return thor_bit;
...
end SIMPLE_THOR;
mbs
28
Language Features: DESIGN UNITS
and LIBRARIES
mbs
29
Logic Simulation In VHDL
mbs
30
High-Level Message Queue Simulation
P_sender P_receiver
message
queue
queue
P_sender process P_receiver
mbs
31
MCC VHDL Simulator
VHDL Source
ANALYZER
IR (Intermediate Representation)
COMPILER
C Source
C COMPILER
va
Object File
Pattern Language
LINKER
PICL
Executable Binary
EXECUTION
vs
Binary Patter Output TEXT
PATPRN
mbs
32
Problems in VHDL
mbs
33