02.-Introduction VHDL Programming
02.-Introduction VHDL Programming
2
Introduction
Hardware Description Lenguaje
• A hardware description language is not a program, but a code, which describes the behavior of a digital circuit.
• VHDL stands for Very High Speed Integrated Circuits (VHSIC) Hardware Description Language (HDL)
• It is used to build digital system/circuit using Programmable Logic Device like CPLD ( Complex Programmable Logic
Device) or FPGA (Field Programmable Gate Array)
• VHDL program (code) is used to implement digital circuit inside CPLD / FPGA, or it can be used to fabricate ASIC
(Application Specific Integrated Circuit)
• In 1980s US Department of Defense (DoD) initiate the VHSIC program to standardize HDL from different companies.
This standardized the HDL.
• In 1985 the first version of VHDL was created by IBM, Texas Instruments and Intermatrix under contract of DoD.
• In 1987 the IEEE 1076 standard was published. Then standard IEEE 1164, was added to introduce multi-valued logic
system.
• Three common HDLs are Verilog ( IEEE 1364), VHDL and SystemC.
3
Introduction
Advantages of VHDL
• VHDL is vendor independent, portable (a simple component can be exported) and reusable.
• It is designed to work modularly based on “blocks”. Thereby a complex big system can be traced down to small
components.
• All statements in VHDL are executed concurrently (unless otherwise is desired by the programmer)
• It has IEEE and ANSI standard.
• Verilog and VHDL are similar. Both are useful as HDL tool. However:
• VHDL is strongly typed. This makes it harder to make mistakes as a beginner because the compiler will not allow you to write code that is in valid. Verilog is weakly typed.
It allows you to write code that is wrong, but more concise.
• Verilog looks closer to a software language like C. This makes it easier for someone who knows C well to read and understand what Verilog is doing.
• VHDL requires a lot of typing. Verilog generally requires less code to do the same thing.
• VHDL is very deterministic, where as Verilog is non-deterministic under certain circumstances.
4
Introduction
VHDL vs Verilog…There is any better?->NO
VHDL Verilog
Wordy Succinct
Widely used for FPGAs and military A better grasp on hardware modeling
5
Introduction
Design Flow
1.- First we write our VHDL code on a .vhd file. This code describe the circuit at Register
Transfer Level (RTL). (abstraction level for creating circuits).
2.- Then synthesis is executed. Here VHDL code is transformed into a netlist at gate level.
3.- A netlist optimization process is executed to reduce delay signals and/or space required by
the circuit. After that simulation can be done:
• Simulation can be including time delays or not (to test the logic).
• If simulation is not as expected, VHDL code must be modified.
4.- Finally the designed circuit is either: i) “printed” in a programmable device such as
FPGA/CPLD or ii) transformed into a MASK to create an ASIC (Application Specific Integrated
Circuit) by placing and routing the software (fitter). At this stage, the final device can be
simulated and verified again.
6
Introduction
How it works
We generate an “ENTITY”(circuit structure) which has 3 input ports (a,b,cin) and 2 output ports (s, count).
Besides that, its architecture (circuit functioning) describes how to assign values to s and count.
7
Introduction
Several possible implementations
Equtions of ARCHITECTURE described before can be implemented in several ways. The results depends on the
compiler/optimizer and more important, the target technology.
Simulation results from VHDL design: Where is the error? Check it!
8
END-Introduction
VHDL Programming Language
This section provides a brief overview of the
programming language and compilers.
9
Code Structure
Library, Entity, Architecture
Definition, operators, functions, procedures components,
constans, types.
10
Code Structure
Design Flow
--------------------------------------- • A minimum standalone code of VHDL is composed of Library, Entity and Architecture.
LIBRARY ieee; • LIBRARY declarations: Contains a list of all libraries to be used in the design.
USE ieee.std_logic_1164.all; • ENTITY: Specifies the I/O pins of the circuit.
--------------------------------------- • ARCHITECTURE: Contains the VHDL code proper, which describes how the
ENTITY full_adder IS circuit should behave (function).
PORT (a, b, cin: IN BIT;
s, cout: OUT BIT);
END full_adder;
--------------------------------------
ARCHITECTURE dataflow OF full_adder IS
BEGIN
s <= a XOR b XOR cin;
cout <= (a AND b) OR (a AND cin) OR
(b AND cin);
END dataflow;
11
Code Structure
Library
LIBRARY declarations: Contains a list of all libraries to be used in the design.
Syntax:
LIBRARY library_name;
USE library_name.package_name.package_parts;
12
Code Structure
Entity
ENTITY is a list with specifications of all input and output pins (PORTS) of the circuit.
Syntax:
NAND Gate
ENTITY entity_name IS
PORT (
port_name : signal_mode signal_type;
port_name : signal_mode signal_type;
...);
END entity_name;
Signal Mode can be IN, OUT, INOUT (bidirectional) or BUFFER (for output signals that are
used (read) internally).
Signal Type can be BIT, STD_LOGIC, INTEGER, etc. (we see this later).
Entity Name can be any name, except VHDL. It is the name of your entity and .vhd file
13
Code Structure
Architecture
ARCHITECTURE is the description of the functionality of the circuit.
Syntax (it has two parts, declarations and code)
• The declarative part (optional) declares signals and constants (among others).
• The code part define the function of the code.
´<=´Assign the result of “a AND b” to x. Mixed style is also allowed, using two or the three styles.
14
Code Structure
Exercises –FlipFlop Source Code
Implement a D-type flip-flop (DFF) and simulate its behavior to validate its code.
15
END-Code Structure
Library, Entity, Architecture
Definition, operators, functions, procedures components,
constans, types.
16
Data Types
Vector, bits, etc
This chapter discusses different data types availables in VHDL
17
Data Types
VHDL Pre-Defined Data Types
• Package standard of library std, defines:
BIT, BOOLEAN, INTEGER, and REAL data types.
• Package std_logic_1164 of library ieee, defines:
STD_LOGIC and STD_ULOGIC data types.
• Package numeric (formal std_logic_arith by synopsis) of library ieee, defines:
SIGNED and UNSIGNED data types, plus several data conversion functions,
like conv_integer(p), conv_unsigned(p, b), conv_signed(p, b), and conv_std_logic_vector(p, b).
• Packages std_logic_signed and std_logic_unsigned of library ieee: Contain functions that allow operations with
STD_LOGIC_VECTOR data to be performed as if the data were of type SIGNED or UNSIGNED, respectively
18
Data Types
VHDL Pre-Defined Data Types
• BIT : It can only have the value 0 or 1. When assigning a value of 0 or 1 to a BIT in VHDL code, the 0 or 1 must be
enclosed in single quotes: '0' or ‘1’.
Example:
SIGNAL x: BIT; -- x is declared as a one-digit signal of type BIT.
x <= '1’; -- x is a single-bit signal (as specified above), whose value is
• BIT_VECTOR: The BIT_VECTOR data type is the vector version of the BIT type consisting of two or more bits. Each bit
in a BIT_VECTOR can only have the value 0 or 1. When assigning a value to a BIT_VECTOR, the value must be enclosed
in double quotes, e.g. "1011" and the number of bits in the value must match the size of the BIT_VECTOR.
Example:
SIGNAL y: BIT_VECTOR (3 DOWNTO 0); -- y is a 4-bit vector, with the leftmost bit being the MSB.
y <= "0111"; -- y is a 4-bit signal (as specified above), whose value is "0111"
-- (MSB='0'). Notice that double quotes (" ") are used for vectors.
19
Data Types
VHDL Pre-Defined Data Types
• STD_LOGIC : Data type can have 8 logic values. When assigning the value, it must be in enclosed in single quotes.
Only four logic values are synthesizable, i.e. usable for programming FPGA or CPLD. The rest can be used for simulation.
• STD_LOGIC_VECTOR: Vector version of STD_LOGIC. Each bit can also take same 8 logic values. Value must be enclosed
in double quotes
SIGNAL x: STD_LOGIC; -- x is declared as a one-digit (scalar) signal of type STD_LOGIC.
SIGNAL y: STD_LOGIC_VECTOR (3 DOWNTO 0) := "0001"; -- y is declared as a 4-bit vector, with the leftmost bit being MSB.
-- The initial value (optional) of y is "0001“ (use ":=“)
20
Data Types
VHDL Pre-Defined Data Types Examples
21
Data Types
VHDL Pre-Defined Data Types
• BOOLEAN: True, False.
• INTEGER: 32-bit integers (from -2,147,483,647 to +2,147,483,647).
• NATURAL: Non-negative integers (from 0 to +2,147,483,647).
• REAL: Real numbers ranging from 1.0E38 to þ1.0E38. Not synthesizable.
• Physical literals: Used to inform physical quantities, like time, voltage, etc. Useful in simulations. Not synthesizable.
• Character literals: Single ASCII character or a string of such characters. Notsynthesizable.
• SIGNED and UNSIGNED: data types defined in the std_logic_arith package of the ieee library. They have the
appearance of STD_LOGIC_VECTOR, but accept arithmetic operations, which are typical of INTEGER data types
22
Data Types
VHDL User Define Data Types
• VHDL Allows user to define their own type of variables.
User-defined integer types:
TYPE my_integer IS RANGE -32 TO 32; -- A user-defined subset of integers.
TYPE student_grade IS RANGE 0 TO 100; -- A user-defined subset of integers or naturals.
23
Data Types
Signed and Unsigned Data Types
• These data type are defined in std_logic_arith of the package ieee.
• Syntax is like STD_LOCIG_VECTOR (not like integer):
• SIGNAL x: SIGNED (7 DOWNTO 0);
• SIGNAL y: UNSIGNED (0 TO 3);
• Unsigned uses all bits for number representation. Signed type uses “two complement” format.
• SIGNED and UNSIGNED are intended for arithmetic operations. (contrary to STD_LOGIC, which do not accept
arithmetic operations). However, logic operations are not accepted. There are no restrictions regarding relational
(comparison) operations.
25
Data Types
Data Conversion
• VHDL does not allow direct operations between data of different type.
• To convert data, it can be done manually based on a FUNCTION from a pre-defined PACKAGE.
• OR use the std_logic_1164 from ieee library which provides straightforward conversion when data are closely
related.
26
END-Data Types
Vector, bits, etc
This chapter discusses different data types availables in VHDL
27
Operators and Attributes
Logical, arithmetic, relational, etc
Operators and attributes are very important to develop a VHDL code, which
allows us to work with data.
28
Operators and Attributes
Operators
• This is a very simple, straightforward, but necessary chapter. It defines the set of tools that you must work with data.
We will start with operators.
• Operators are the key for managing data and making all the logic within or code.
29
Operators and Attributes
Operators
• Assignment operators are used to assign values to signals, variables and constants
<= Used to assign a value to a SIGNAL.
:= Used to assign a value to a VARIABLE, CONSTANT, or GENERIC. Used also for establishing initial values.
=> Used to assign values to individual vector elements or with OTHERS.
30
Operators and Attributes
Operators
• Logical operators are used to perform logical operations. Data must be BIT or STD_LOGIC or STD_ULOGIC (and its
vector forms), these are:
31
Operators and Attributes
Operators
• Arithmetic Operators are used to perform arithmetic operations.
Data must be INTEGER, UNSIGNED or REAL (this is not synthesized directly).
If std_logic_signed or std_logic_unsigned is used, STD_LOGIC_VECTOR can be also used directly for addition and
substraction.
• + Addition
• - Subtraction
• * Multiplication
• / Division -> Only power of two dividers are allows (shift operation).
• ** Exponentiation -> Only static values of base and exponent are allowed.
• MOD Modulus -> a MOD b returns the remainder of a/b with the sign of b
• REM Remainder -> a REM b returns the remainder of a/b with the sign of a
• ABS Absolute value. -> Return the absolute value
Note that MOD , REM and ABS are usually not synthesized.
32
Operators and Attributes
Operators
• Comparison Operators are used to perform comparisons. Data can be INTEGER, UNSIGNED or REAL (this is not
synthesized directly), STD_LOGIC and its vector versions.
• = Equal to
• /= Not Equal to
• < Less than
• > Greater than
• <= Less than or equal to
• >= Greater than or equal to
• sll Shift Left Logic -> positions on the right are filled with 0s
• srl Shift Right Logic -> positions on the left are filled with 0s
Examples:
“1100” sll 1 yields “1000”
“1100” srl 2 yields “0011”
33
Operators and Attributes
Signal Attributes (VHDL Prededined Attributes)
• Signal Attributes helps us to check the state of signals. Suppose a signal x, then (full list):
• x’EVENT Returns true when an event (a change) occurs in x
• x’STABLE Returns true when no event occurs in x
• x’ACTIVE Returns true if x=‘1’
• x’QUIET<time> Returns true if no event has occurred during “time”
• x’LAST_EVENT Return the time since the last event
• x’LAST_ACTIVE Return the time since the last event x=´1´
• x’LAST_VALUE Return the value of x before the event.
• Others see full list here
All these conditions are true
when a rising clk occurs. Note
that the latter instructions call
a function. (we see that later)
34
Operators and Attributes
USER Defined Operations
• Like attributes, operations can be also user defined. To define a new operation, only operators symbols or
combination of those can be used. See IEEE1076Std (you must be connected to UC network to be able to download
this file) to check all available operator symbols. Creating a new operation, based on available symbols is known as
“operator overloading”.
• Suppose you want to use the symbol + to add an integer to a binary 1-bit number.
35
Operators and Attributes
Summary
36
END-Operators and Attributes
Logical, arithmetic, relational, etc
Operators and attributes are very important to develop a VHDL code, which
allows us to work with data.
37
Concurrent Code
WHEN, GENERATE, BLOCK
We study the staments required to create a concurrent code.
38
Concurrent Code
Combinational vs Sequential Logic
• We have covered the basics of VHDL. Now we start describing how to create a code properly.
• A VHDL code can be concurrent or sequential
• For concurrent CODE we use the following statements in VHDL: WHEN and GENERATE
• Also, operators can be used to create concurrent code.
• Also, a special kind of assignment, namely BLOCK can be used.
• In a Combinational logic circuit, the output depends only on current inputs. (System does not require stored data)
• In a Sequential logic, the output depends on previous and/or current inputs. The system require stored data which is
feeded-back to the system.
39
Concurrent Code
Combinational vs Sequential Logic
• Not any circuit that contain storage elements (flip-flops) is a sequential logic!. DO not get confused! See this example:
In a RAM, like this figure, the storge elements are in forward path instead of
feedback. The memory-read operation depends only on the current address
vector applied to the RAM input. No access to previous data is needed!
40
Concurrent Code
Concurrent vs Sequential Code
• Only statements placed inside PROCESS, FUNCTION or PROCEDURE are sequential. (we see this in next section).
• Concurrent code is called dataflow.
• To build combinational logic circuits, we need concurrent code:
• Operators; 4 inputs
• The WHEN statement (WHEN/ELSE or WITH/SELECT/WHEN); 1 bit per input
• The GENERATE statement; Output is selected by s1, s0
• The BLOCK statement
41
Concurrent Code
WHEN (WHEN/ELSE and WITH/SELECT/WHEN)
• WHEN/ELSE syntax:
• assigment WHEN condition ELSE
• assigment WHEN condition ELSE
• …;
• WITH/SELECT/WHEN
• WITH identifier SELECT
assigment WHEN value,
assigment WHEN value,
…; Note that using WITH/SELECT/WHEN all
combinations must be listed/tested. A nice way
Note that WHEN can also assign:
to avoid a long list is using keywords like
WHEN value -- single value UNAFFECTED and OTHERS. (full list of keywords
WHEN value1 to value2 -- range, for enumerated data types only is in next slide)
WHEN value1 | value2 |... -- value1 or value2 or ...
42
Concurrent Code
WHEN (WHEN/ELSE and WITH/SELECT/WHEN)
Implement a multiplexer
4 inputs
1 bit per input
Output is selected by s1, s0
43
Concurrent Code
GENERATE
• It is another concurrent statement. It allow a section of a code to be repeated a number of times. It create several
instances (examples or cases of something) of the same assignment.
• GENERATE is used together with FOR (regular form) or IF (irregular form).
• Syntax FOR/GENERATE:
label: FOR identifier IN range GENERATE
(concurrent assignments)
END GENERATE;
Nested structures are allowed (i.e., a FOR/GENERATE within IF/GENERATE or other way around).
44
Concurrent Code
GENERATE-Example
Generate a circuit which receives an input vector of 4 bits and generate an output vector of 8bits. The 4-bits input
vector is contained in the last 4-bits of the output vector (other bits are 0). An additional input can shift the position
of the vector from 0 to 4 positions to the left. For instance, if input is 1010 and shift is 2, output is 00101000
label: BLOCK
[declarative part]
BEGIN
(concurrent statements)
END BLOCK label;
46
Concurrent Code
Guarded BLOCKS
• A guarded BLOCK is a special form of BLOCK which includes a guard expression. The statements within the guarded
BLOCK are only executed when the guard expression is true.
47
END-Concurrent Code
WHEN, GENERATE, BLOCK
We study the staments required to create a concurrent code.
48
Sequential Code
PROCESS, FUNCTIONS, PROCEDURES
We study the statements required to create a sequential code.
49
Sequential Code
Introduction
• Sequential code is not limited to sequential logic
• A Sequential block (code) is still executed concurrently with the rest of the code (outside the sequential part of the
code)
• A sequential code is writing inside a PROCESS, FUNCTION or PROCEDURE (we see functions and procedures in next
section) using the sequential statements IF, WAIT, CASE and LOOP.
• VARIABLES can be used only in sequential code.
• A VARIABLE can not be global. Therefore, its value can not be passed out directly.
50
Sequential Code
PROCESS
• Initializing PROCESS indicates that a piece of sequential code will be written.
• Sequential code is characterized by containing IF, WAIT, CASE and/or LOOP statements and by a sensitivity list.
• A PROCESS is executed every time a signal in the sensitivity list changes. (or the condition of WAIT is fulfilled)
• Syntax of PROCESS is:
[label:] PROCESS (sensitivity list)
[VARIABLE name type [range] [:= initial_value;]]
BEGIN
(sequential code)
END PROCESS [label];
51
Sequential Code
PROCESS-SIGNALS and VARIABLES
It is important to note the differences between SIGNAL and VARIABLE:
• To pass non-static values within and between circuits we can use SIGNALS or VARIABLES
• A SIGNAL can be declared in PACKAGE, ENTITY and ARCHITECTURE. It can be global.
• A VARIABLE can be declared only in a PROCESS. It is only local. (it can not go out of the PROCESS directly).
• To send out of process a VARIABLE, we need to assign its value to a SIGNAL.
• VARIABLES are immediately updated. Its new value can be used in next line of code. A signal within a PROCESS can
only guarantee its updated value the next time the code enters to the PROCESS.
• Remember that assignment for SIGNAL is “<=“ . Assignment for VARIABLE is “:=“.
52
Sequential Code
PROCESS-IF
• Syntax of IF/ELSE is:
IF conditions THEN assignments; Make a 1-digit counter (0 to 9) with 4bit output and a clk
ELSIF conditions THEN assignments; signal as input. Counter resets automatically (from 9 to 0).
...
ELSE assignments;
END IF;
• Example:
IF (x<y) THEN temp:="11111111";
ELSIF (x=y AND w='0') THEN temp:="11110000";
ELSE temp:=(OTHERS =>'0');
53
Sequential Code
PROCESS-WAIT
• Operator WAIT is similar to IF.
• PROCESS waits until its condition is fulfilled.
• When WAIT is used, no sensitivity list is allowed in PROCESS.
• There are three forms of WAIT, its syntaxis are:
54
Sequential Code 2-digit counter in 7 segment format
PROCESS-CASE
• Operator CASE syntax is:
CASE identifier IS
WHEN value => assignments;
WHEN value => assignments;
...
END CASE;
• CASE (sequential) might look similar to WHEN (concurrent), also all permutations
must be tested. However, CASE allow multiple assignments for each test
condition, WHEN allows only one.
55
Sequential Code
PROCESS-LOOP
• Operator LOOP is useful when a piece of code needs to be instantiated
several times. Loop can be in different forms, as following syntaxis:
FOR range must be static
FOR/LOOP:
[label:] FOR identifier IN range LOOP
(sequential statements)
END LOOP [label];
WHILE/LOOP:
[label:] WHILE condition LOOP
(sequential statements) It exits the loop as son as
END LOOP [label];
a value different to 0 is
found in data vector
EXIT is used to end/scape of the LOOP.
[label:] EXIT [label] [WHEN condition]; If I is equal to the value
NEXT is used for skipping the loop steps of “skip”, then this loop
[label:] NEXT [loop_label] [WHEN condition]; cycle is jumped and cycle
continues with the next i.
56
Sequential Code
Case and IF
• CASE and IF after optimization can (and usually do) generate same circuits. Examples for same Physical multiplexer:
• CASE and WHEN might look similar, but they are intended for different type of codes:
Equivalent codes,
But CASE is whitin
PROCESS
Sequential Code
Final Comments
• Sequential code can be used to implement sequential or combinational circuits.
• If the sequential code is intended for combinational circuit, the complete truth-table should be specified.
• Registers are necessary when making sequential circuits with sequential code.
• Register: sequential(clocked) memory storing devices.
Note: Latches appear in combinatorial logic where a variable does not get assigned a value in every possible path. You find latches by checking every if, else, case, when etc. to see if at
that point in the code a value has been assigned to every variable. This is not required for clocked circuits. There a variable holds its previous value if no assignment has been made.
END-Sequential Code
PROCESS, FUNCTIONS, PROCEDURES
We study the staments required to create a sequential code.
59
Signals and Variables
When and where to use them
We study the importance and differences between signals and variables
60
Signals and Variables
Introduction
• VHDL provides two objects to deal with non-static data values: SIGNAL and VARIABLE.
• VHDL provides CONSTANT and GENERIC for establishing default static values.
• CONSTANT and SIGNAL can be global (seen by the whole code) and used in sequential or concurrent code.
• VARIABLE is local and only used within the sequential code (PROCESS, FUNCTION or PROCEDURE).
61
Signals and Variables
CONSTANT
• CONSTANT is used to stablish default values. Syntax:
CONSTANT name : type := value;
A CONSTANT can be declared in a PACKAGE (truly global), ENTITY (global for the entity and all its architectures) or
ARCHITECTURE (global for this architecture).
CONSTANT are commonly used in PACKAGE or ARCHITECTURE, less common in ENTITY
62
Signals and Variables
SIGNAL
• SIGNAL is the object to pass values in and out of the circuit also among internal units.
• SIGNAL can be seen as the wires that interconnect circuits (all PORTS of an ENTITY are signals by default).
Syntax:
SIGNAL name : type [range] [:= initial_value];
A SIGNAL can be declared in a PACKAGE (truly global), ENTITY (global for the entity and all its architectures) or
ARCHITECTURE (global for this architecture).
A SIGNAL used in sequential code (PROCESS, PROCEDURE, FUNCTION) is not updated immediately (need conclusion of
the sequential code first).
63
Signals and Variables
SIGNAL
• This circuit counts the number of 1s in the input.
• The code is not properly written as the signal temp is not
updated until the PROCESS is finished. Therefore, we
should not assign a “new” value to it.
64
Signals and Variables
VARIABLE
• Unlike CONSTANT or SIGNAL, VARIABLE is local information. Only used in PROCESS FUNCTION or PROCEDURE.
• A VARIABLE is updated immediately. That is very good feature to work in sequential code.
VARIABLE name : type [range] [:= init_value];
• Obviously, a VARIABLE can be declared only on the declaration section of PROCESS, FUNCTION and PROCEDURE.
65
Sequential Code
SIGNAL vs VARIABLE Summary
Simulate and
compare results!
67
END-Signals and Variables
When and where to use them
We study the importance and differences between signals and variables
68
PACKAGES and COMPONENTS
SYSTEM DESIGN
It is time to integrate several circuits into a bigger system
69
FUNCTIONS and PROCEDURES
SYSTEM DESIGN
It is time to integrate several circuits into a bigger system
70
State Machines
Modeling Sequential Logic Circuits
Finite State Machines - FSM
71
Electrical Engineering Department
Pontificia Universidad Católica de Chile
peclab.ing.uc.cl
72