Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
VHDL
Syntax Templates
and
Synthesis Constructs
Table of Contents
Syntax Templates
I. Module Structure
A. Library
B. Entity
C. Architecture
D. Component
II. Data Objects
A. Signal
B. Constant
C. Variable
III. Concurrent Statements
A. Boolean Equations
B. when-else Cond. Signal Assignment
C. with-select-when Cond. Signal Assignment
IV. Sequential Statements
A. Process
B. if-then-else Conditional Statement
C. case-when Conditional Statement
Synthesis Constructs
2
2
3
4
5
5
6
7
7
7
8
8
9
D. for-loop Statement
E. while-loop Statement
V.
Data Types
10
VI.
Conversion Functions
11
VII.
Operators
11
I.
Accumulator
12
II.
Adder
12
III.
Simple Comparator
12
IV.
Up/Down Counter
13
V.
Binary-to-Grey Converter
13
VI.
VII.
3-to8 Decoder
VIII.
8-to-3 Encoder
IX.
D-Type Flip-Flop
15
X.
Debouncing Circuit
16
XI.
4-to-1 Multiplexer
16
XII.
17
XIII.
17
XIV.
17
XV.
28
14
15
1/19
Syntax Templates
I. Module Structure
A. Library
Library: A collection of VHDL design units, typically used to hold reusable components and type declarations.
Package: A design unit often used to gather related component, type, constant, and subprogram declarations. Packages are compiled to
libraries.
Syntax:
library <library_name>;
use <library_name>.<package_name>.[all|<part>];
Example:
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
B. Entity
Entity: An abstraction of a system, board, chip, component, or logic gate. An entity, or design entity, is defined by an entity declaration and
architecture body.
Entity Declaration: A design unit that defines a design entitys interface.
Mode: Associated with ports declared in an entity declaration. A mode defines the direction of signal communication for an entity, and whether
a signal is readable, writable, or both.
In: used for signals (ports) that are inputs-only to an entity.
Out: used for signals that are outputs-only and for which the values are not required internal to an entity.
Buffer: used for signals that are outputs, but for which the values are required internal to the given entity.
Inout: used for signals that are truly bidirectional.
2/19
Syntax:
Example:
entity <entity_name> is
port (
<port_name> : <mode> <type>;
<other ports>
);
end <entity_name>;
entity or_entity is
port(
input_1: in std_logic;
input_2: in std_logic;
output: out std_logic
);
end or_entity;
C. Architecture
Architecture Body: A design unit that describes the behavior or structure of a design entity. An entity declaration is paired with an architecture
body to form a complete design entity.
Syntax:
Example:
{
|
|
|
|
<type-declaration>
<signal-declaration>
<constant-declaration>
<component-declaration>
}
begin
{ <process statement>
| <concurrent signal assignment statement>
| <component instantiation statement>
| }
end <arch_name>;
3/19
D. Component
Component: A design entity instantiated in another design entity. Components are used to create hierarchy and are essentials in structural
descriptions of a design entity.
Component Declaration: A template that defines the interface of a component. At the time of synthesis, a component must be bound to a specific
entity declaration and architecture body pair.
Syntax:
Example:
component <component_name> is
port (
-- port declarations as done
in entity declarations
);
end component;
component or_entity is
port(
input_1: in std_logic;
input_2: in std_logic;
output: out std_logic
);
end component;
Component Instantiation:
Syntax:
Example:
<component_identifier>: <component_name>
port map(
or_ent_1: or_entity
port map(
input_1 => input_1_sig,
input_2 => input_2_sig,
output => output_sig);
4/19
Signal
Signals are the most commonly used data object in synthesis designs. Signals have projected output waveforms. They are typically used to store
internal values or to connect components.
Signal Declaration:
Syntax:
Examples:
Signal Assignments are scheduled, not immediate; they update projected output waveforms.
Syntax:
Example:
B.
Constant
Constants are used to hold a static value; they are typically used to improve the readability and maintenance of code. Some common uses include
making understandable names for states in a state machine and setting maximum values for ranges of integers.
5/19
Constant Declaration:
Syntax:
Examples:
C.
Variable
Variables can be used in processes and subprograms, that is, in sequential areas only. The scope of a variable is the process or subprogram.
Variables are most commonly used as the indices of loops o for the calculation of intermediate values, or immediate assignment. To use
the value of a variable outside of the process or subprogram in which it was declared, the value of the variable must be assigned to a
signal.
Variable Declaration:
Syntax:
Examples:
Example:
<variable_name> := <expression>
boolean_v := true;
temp_v(3 downto 0) := sl_vector_signal(7 downto 4);
6/19
Example:
[not] <identifier> [
[and | or | nor | nand | xor | xnor | ]
[ <identifier> ]
];
Example:
X <= 1 when
Y <= signal_1
signal_2
signal_3
signal_4
b = c else 0;
when state = idle else
when state = state_1 else
when state = state_2 else
when others;
Example:
7/19
Example:
output_process: process(flag_signal)
<type-declaration>
| <constant-declaration>
| <variable-declaration>
| }
begin
{ <statement(s)> }
end process;
begin
if flag_signal = '1' then
output_vector <= "010";
else
output_vector <= "101";
end if;
end process;
NOTE: Its advisable to put all the signals used in the process into the sensitivity list.
Example:
8/19
end if;
Example:
case <expression> is
{ when <choice(s)> => <assignments>; }
when <choice(s)> => <assignments>;
end case;
case state is
when 00" => integer_signal <= 1;
when "11" => integer_signal <= 2;
when others => integer_signal <= 0;
end case;
D.
for-loop Statement
Syntax:
Example:
[<loop_lavel>:]
for <identifier> in <discrete_range> loop
<statement(s)>;
end loop [<loop_lavel>];
for_loop_1:
for i in 3 downto 0 loop
if reset(i) = '1' then
out_vector(i) <= 0;
end if;
end loop for_loop_1;
E.
while-loop Statement
Syntax:
Example:
[<loop_lavel>:]
while <condition> loop
<statement(s)>;
end loop [<loop_lavel>];
while_loop_1:
while (count > 0) loop
count := count - 1;
result <= result + data_in;
9/19
V. Data Types
A. Enumeration Types
Values are user-defined; commonly used to define states for a state machine.
Syntax:
Examples:
STD_LOGIC
STD_LOGIC_VECTOR
-- 'U','X','0','1','Z','W','L','H','-'
-- Natural Range of STD_LOGIC
C. boolean
BOOLEAN
-- True or False
-- '0','1
-- Array of bits
E. ranged types
There are a couple of ways to represent numbers in VHDL. One is to use the binary/hexadecimal representation afforded by the std_logic_vector or bit_vector. While this is
useful when representing physical signals, integers are easier to use. As such an integer type and two subtypes have been defined in VHDL, but they are not implemented in
wires. They are translated to busses. Thus, to limit the physical wires that are implemented by the design, and hence make the implementation of the design more efficient, its
advisable to limit integers to specified ranges.
ICTP Multidisciplinary Laboratory, MLAB, Trieste, Italy
10/19
INTEGER
NATURAL
POSITIVE
-- 32 or 64 bits
-- Integers >= 0
-- Integers > 0
VI Conversion Functions
The VHDL language allows the usage of conversion functions. These conversion functions are generally used to convert from one type to another.
There are multiple conversion functions provided in different libraries. This is why it is important to reference the correct library in which the
conversion function is available.
Addition
Subtraction
Multiplication
Divide
Modulus
Power Operator (i.e. 2**8 returns 256)
The following operators either concatenate several bits into a bus or replicate a bit or combination of bits multiple
times.
a & b & c
The following logical operators are used in conditional TRUE/FALSE statements such as an if statement in order to
specify the condition for the operation.
NOT
AND (NAND)
OR (NOR)
XOR (XNOR)
=
/=
<
Not True
Both Inputs True
Either Input True
Only one Input True
Inputs Equal
Inputs Not Equal
Less-than
11/19
<=
>
>=
Less-than or Equal
Greater-than
Greater-than or Equal
12/19
Synthesis Constructs
Accumulator with synchronous Reset and Chip Enable
process (<clock>, <reset>)
begin
if <clock>='1' and <clock>'event then
if <reset>='1' then
<accumulate_out> <= (others => '0');
elsif <clock_enable>='1' then
<accumulate_out> <= <accumulate_out> + <accumulate_in>;
end if;
end if;
end process;
Simple Comparator
process(<clock>,<reset>)
begin
if (<reset> = '1') then
<output> <= '0';
elsif (<clock>'event and <clock> ='1') then
if ( <input1> = <input2> ) then
<output> <= '1';
else
<output> <= '0';
end if;
end if;
end process;
"
13/19
14/19
3-to-8 Decoder
process(<clock>,<reset>,<input>)
begin
if ( <reset> = '1') then
<output> <= "00000000";
elsif ( <clock>'event and <clock> ='1') then
case <input> is
when "000" => <output> <= "00000001";
when "001" => <output> <= "00000010";
when "010" => <output> <= "00000100";
when "011" => <output> <= "00001000";
when "100" => <output> <= "00010000";
when "101" => <output> <= "00100000";
when "110" => <output> <= "01000000";
when "111" => <output> <= "10000000";
when others => "00000000";
end case;
end if;
end process;
15/19
8-to-3 Encoder
process(<clock>,<reset>,<input>)
begin
if ( <reset> = '1') then
<output> <= "000";
elsif ( <clock>'event and <clock> ='1')
case <input> is
when "00000001" => <output>
when "00000010" => <output>
when "00000100" => <output>
when "00001000" => <output>
when "00010000" => <output>
when "00100000" => <output>
when "01000000" => <output>
when "10000000" => <output>
when others
=> <output>
end case;
end if;
end process;
then
<=
<=
<=
<=
<=
<=
<=
<=
<=
"000";
"001";
"010";
"011";
"100";
"101";
"110";
"111";
"000";
16/19
Debouncing circuit
Provides a one-shot pulse from a non-clock input, with reset
D_IN: in STD_LOGIC;
RESET: in STD_LOGIC;
clock: in STD_LOGIC;
Q_OUT: out STD_LOGIC);
**Insert the following between the 'architecture' and 'begin' keywords**
signal Q1, Q2, Q3 : std_logic;
**Insert the following after the 'begin' keyword**
process(clock, RESET)
begin
if (RESET = '1') then
Q1 <= '0';
Q2 <= '0';
Q3 <= '0';
elsif (<clock>'event and <clock> = '1') then
Q1 <= D_IN;
Q2 <= Q1;
Q3 <= Q2;
end if;
end process;
17/19
18/19
19/19
20/19