Introduction To VHDL: Unit-1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 59

Unit-1

Introduction to VHDL
HDL introduction
• Hardware Description Language (HDL) is an essential computer
aided design (CAD) tool for synthesis of digital systems.
• Requirement of HDL:- Due to complexity, digital systems
cannot be easily realized using discrete integrated circuits(ICs).
• Solution:- 1) Application Specific Integrated Circuits (ASICs)
• 2) Field-Programmable Gate Arrays (FPGAs)
• HDL to describe the system to be implemented in a computer-
language code similar to C/C++.
• Simulators and test benches debug the program.
• Types of HDL:- VHDL & Verilog HDL

• VHDL:- VHSIC HDL


Very High Speed Integrated Circuit Hardware
Description Language (VHSIC HDL)

• Verilog HDL

* In book and in PPTs VHDL stands for VHSIC HDL


and Verilog HDL stands for Verilog.
Capabilities of VHDL and Verilog
• An exchange medium between chip vendors and CAD tool users.
• Used as a communication medium between different CAD and CAE tools.
• Supports hierarchy.
• Supports flexible design methodologies.
• The language is not technology-specific.
• It supports both synchronous and asynchronous timing models.
• Various digital modeling techniques such as finite-state machine descriptions,
algorithmic descriptions, and boolean equations can be modeled using the
language.
• The language is publicly available, i.e. It is not proprietory.
• The language supports three basic different description styles: structural,
dataflow, and behavioral.
Capabilities cont...

• Arbitrarily large designs can be modelled.


• Test benches can be written using the same language to test other VHDL
models.
Hardware Abstraction
• VHDL is used to describe a model for a digital hardware device.
• This model specifies the external & internal views of the device.
• Internal view specifies structure whereas external view specifies
interfaces.
• The device to device model mapping is strictly a one to many.
That is, a hardware device may have many device models.
• Even though entity 1 through N represent N different entities
from the VHDL point of view, in reality they represent the
same hardware device.

7
HDL module structure
• Example:- Consider Half adder ckt.

S = ā b + a bb = a EXOR b

C=ab
VHDL Structure:-

 It has two major constructs: entity and architecture.


 Entity describes input and output of the system (names are
user identifiable).
 Architecture describes actual system and function of output
with input.
 VHDL is case insensitive.
 Entity name should start with an alphabetical letter and can
include the special character underscore (_).
 The inputs and outputs are called input ports and output
ports.
• Entity of VHDL program of half adder is as follows:-
entity Half_adder is
port(a: in bit;
b : in bit;
S : out bit;
C: out bit);
end half_adder;

Rules for writing Entity:-


The word entity is a predefined word.
The name of the entity is Half_adder.
name is user selected and does not convey any information about the system
The above entity has two input ports and two output ports.
The term ‘is’ is a predefined word and must be written after the name of the entity.
The word port is predefined.
The port assignment as input or output is denoted by ‘in bit’ and ‘out bit’ respectively.
The type of these input signals is bit and bit allows the signal to take only either logic 0
or logic 1.
The order is irrelevent.
The last line of the entity’s code uses the predefined word end, and it ends the entity.
Architecture of VHDL program of half adder
 The second construct of the VHDL module is the architecture.
 Architecture has to be bound to an entity.
 Multiple architectures can be bound to the same entity, but each
architecture can be bound to only one entity.

Rules for writing Architecture:-


 The architecture is declared by the predefined word architecture, followed by a user-
selected name.
The name is followed by the predefined word ‘of’ followed by the name of the entity.
The predefined word of binds the architecture ‘dtfl _half’ to the entity ‘Half_adder’.

architecture dtfl_half of Half_adder is


begin
S <= a xor b; -- statement 1
C <= a and b; -- statement 2
--Blank lines are allowed
end dtfl_half;
Rules cont...
 The architecture ‘dtfl _half’ recognizes the information declared in the
entity, such as the name and type of ports a, b, S, and C.
 After entering the name of the entity, the predefined word is must be
entered.
 The architecture’s body starts with the predefined word begin.
 Two hyphens (--) signal that a comment follows.
 The architecture is concluded by the predefined word end. The name of
the architecture can follow, if desired, the predefined word end.
 Leaving blank line(s) is allowed in the module
Verilog structure

 Verilog module has declaration and body.


 In the declaration, the name, inputs and outputs of the
module are entered.
 The body shows the relationship between the inputs and the
outputs.
Rules for writing Verilog program:-
 In contrast to VHDL, Verilog is case sensitive.
 The name of the module should start with an alphabetical letter and can
include the special character underscore (_).
 The declaration of the module starts with the predefined word module
followed by the user-selected name.
 The names of the inputs and outputs are written inside parentheses,
separated by a comma.

module Half_adder(a,b,S,C);
input a,b;
output S, C;
// Blank lines are allowed
assign S = a ^ b; // statement 1
assign C= a & b; // statement 2
endmodule
Rules cont...
• The parenthesis is followed by a semicolon.
• The order of writing the
• Input and output ports inside the parentheses is irrelevant.
• Carriage return here does not indicate a new statement, the semicolon
does.
• Statements 1 and 2 are signal assignment statements.
entity Half_adder is
port(a: in bit;
b : in bit;
S : out bit;
C: out bit); module Half_adder(a,b,S,C);
end half_adder; input a,b;
output S, C;
architecture dtfl_half of Half_adder is // Blank lines are allowed
begin assign S = a ^ b; // statement 1
S <= a xor b; -- statement 1 assign C= a & b; // statement 2
C <= a and b; -- statement 2 endmodule
--Blank lines are allowed
end dtfl_half;
Types Of Description
 There are various types of code writing techniques are
present, selection of one particular style depends on the
available information.
 Types of description are:-
1.Data flow description
2.Behavioral description
3.Structural description
4.Switch level description
5.Mixed type description
6.Mixed language description
1. Data flow description
 It describes how data flows from input to output.
 Usually, the description is done by writing the Boolean function of the
outputs.
 The data-flow statements are concurrent.

2. Behavioral description
 It describes how output behaves with perticular input.
 Example:- In the half adder, the S output can be described as “1” if the
inputs a and b are not equal, otherwise S = “0” & output C can be
described as acquiring a value of “1” only if each input (a and b) is “1”
 In behavioral description the architecture (VHDL) or the module (Verilog)
contains the predefined word process (VHDL) or always or initial (Verilog).
 Behavioral description is usually used when the Boolean function or the
digital logic of the system is hard to obtain.
architecture beh_half of Half_adder is
begin
process (a, b)
begin
if (a /= b) then
S <= ‘1’;
else
S <= ‘0’;
--Blank lines are allowed
end if;
end process;
end beh_half;
3. Structural Description
 It models the system as components or gates.
 This description is identified by the presence of the keyword component in
the architecture (VHDL) or gates construct such as and, or, not in the
module (Verilog).

4. Switch-Level Description
 It is the lowest level of description.
 The system is described using switches or transistors.
 Some of the Verilog predefined words used in the switch level description
are nmos, pmos, cmos, tranif0, tran, and tranif1.
 VHDL does not have built-in switch-level primitives, but a construct
package can be built to include such primitives.
architecture Invert_switch of Inverter is
component nmos --nmos is one of the key words for switch-level.
port (O1: out std_logic; I1, I2 : in std_logic);
end component;
component pmos --pmos is one of the key words for switch-level.
port (O1: out std_logic ;I1, I2 : in std_logic);
end component;
for all: pmos use entity work. mos (pmos_behavioral);
for all: nmos use entity work. mos (nmos_behavioral);
--The above two statements are referring to a package mos
constant vdd: std_logic := ‘1’;
constant gnd : std_logic:= ‘0’;
begin
p1 : pmos port map (y, vdd, a);
n1: nmos port map (y, gnd, a);
end Invert_switch;
5. Mixed-Type Description
 Mixed-type or mixed-style descriptions are those that use
more than one type or style of the above-mentioned
descriptions.
 Most of the descriptions of moderate to large systems are
mixed. Some parts of the system may be described using one
type and others using other types of description.

6. Mixed-Language Description
• The user now can write a module in one language (VHDL or
Verilog) and invoke or import a construct (entity or module)
written in the other language.
VHDL Ports
• in: The port is only an input port. In any assignment statement, the port
should appear only on the right-hand side of the statement (i.e., the port
is read).
• out: The port is only an output port. In any assignment statement, the
port should appear only on the left-hand side of the statement (i.e., the
port is updated).
• buffer: The port can be used as both an input and output but can have
only one source (i.e., limited fan out). The port can appear on either the
left- or right-hand side of an assignment statement. A buffer port can only
be connected to another buffer port or to a signal that also has only one
source.
• inout: The port can be used as both an input and output.
• linkage: Same as inout but the port can only correspond to a signal.
Verilog Ports
 input: The port is only an input port. In any assignment statement, the
port should appear only on the right-hand side of the statement (i.e., the
port is read).
 output: The port is an output port. In contrast to VHDL, the Verilog output
port can appear in either side of the assignment statement.
 inout: The port can be used as both an input and output. The inout port
represents a bidirectional bus.
Operators

 HDL has following types of operators:


1. Logical operators
2. Relational operators
3. Arithmetic operators
4. Shift operators
1. Logical Operators
 These operators perform logical operations, such as AND, OR,
NAND,NOR, NOT, and XOR.
 The operation can be on two operands or on a single operand.
The operand can be single or multiple bits.

A. VHDL Logical Operators


 These operators should appear only on the right-hand side of
statements.
 The operators are bitwise; they operate on corresponding bits
of two signals.
B. Verilog Logical Operators
 Verilog logical operators can be classified into three groups: bitwise,
Boolean logical, and reduction.
 The bitwise operators operate on the corresponding bits of two operands.
Consider the statement: Z= X & Y, if X is the four-bit signal 1011, and Y is
the four-bit signal 1010, then Z = 1010.
 Other types of logical operators are the Boolean logical operators. For
example, consider the statement Z = X && Y where && is the Boolean
logical AND operator. If X = 1011 and Y = 0001, then Z = 1. If X = 1010 and
Y = 0101, then Z = 0.
 The third type of logical operator is the reduction operator. Reduction
operators operate on a single operand. The result is in Boolean. For
example, in the statement Y = &X, where & is the reduction AND operator,
and assuming X = 1010, then Y = (1 & 0 & 1 & 0) = 0.
Bitwise Logical Operator
Verilog Boolean Logical Operators

Verilog Reduction Logical Operators


2. Relational Operators
 Relational operators are implemented to compare the values of two
objects.
A. VHDL Relational Operators
 VHDL has extensive relational operators. Their main implementations are
in the if and case statements.
The following statements demonstrate the implementation of some of the
above relational operators.
If (A = B) then .....
A is compared to B. If A is equal to B, the value of the expression (A = B) is
true (1); otherwise, it is false (0).

B. Verilog Relational Operators


• For the equality operator (==) and inequality operator (!=), the result can
be of type unknown (x) if any of the operands include “don’t care,”
“unknown (x),” or “high impedance z.”

if (A === B)…..
• This is a bit-by-bit comparison. A or B can include x or high impedance Z;
the result is true (1) if all bits of A match that of B. Otherwise, the result is
false (0).
3. Arithmetic Operators
A. VHDL Arithmetic Operators
• VHDL arithmetic operators operate on numeric and physical operand types.
• Physical data types are those that can be measured in units, such as time.

B. Verilog Arithmetic Operators


• Verilog, in contrast to VHDL, is not extensive type-oriented language.
• Accordingly, for most operations, only one type of operation is expected for
each operator.

• Arithmetic Operator Precedence


• The precedence of the arithmetic operators in VHDL or Verilog is the same
as in C. The precedence of the major operators is listed below from highest
to lowest:
**
* / mod (%)
+ -
4. Shift and Rotate Operators
• Shift and rotate operators are implemented in many applications, such as
in multiplication and division.

B. VHDL Shift/Rotate Operators


• Shift operators are unary operators; they operate on a single operand.
• To understand the function of these operators, assume that operand A is
the four-bit vector 1110.
110x

1100
B. Verilog Shift Operators
Data Types
 As HDL is implemented to describe the hardware of a system, to match the
need for describing the hardware data or operands used in the language
must have several types.

A. VHDL Data Types


 VHDL is a type-oriented language; many operations will not be executed if
the right type for the operands has not been chosen.
 The type of any element or object in VHDL determines the allowed values
that element can assume. Objects in VHDL can be signal, variable or
constant.
1. Scalar Types
 The values that a scalar can assume are numeric.
 Numeric values can be integer, real, physical (such as time), Boolean (0 or 1), or
characters when stored as American Standard Code for Information Interchange
(ASCII) or compatible code.

1.1 Bit Type


• The only values allowed here are 0 or 1.
• It is used to describe a signal that takes only 1 (high) or 0 (low). The signal
cannot take other values such as high impedance (open).
1.2 Boolean Type
• This type can assume two values: false (0) or true (1). Both true and false are
predefined words.
1.3 Integer Type
• As the name indicates, this type covers all integer values; the values can be
negative or positive. The default range is from –2,147,483,648 to
+2,147,483,647.
• The user can specify a shorter range by using the predefined word range.
1.4 Real Type
• This type accepts fractions, such as .4502, 12.5, and –5.2 E–10
where E–10 = 10–10.
1.5 Character Type
• This type includes characters that can be used to print a message using,
• for example, the predefined command report, such as:
report (“Variable x is greater than Y”);
1.6 Physical Type
• This type has values that can be measured in units, such as time (e.g.,
second, millisecond, microsecond) and voltage (e.g., volt, millivolt,
microvolts).
• An example of type time is: constant Delay_inv : time := 1 ns;
2. Composite Types
• The composite type is a collection of values. There are three
composite types: bit vector, arrays, and records.
2.1 Bit_vector Type
• The bit_vector type represents an array of bits; each element of the array
is a single bit. The following example illustrates the implementation of
type bit_vector:
Port (I1 : in bit; I2 : in bit_vector (5 downto 0); Sum : out bit);
2.2 Array Type
• This type is declared by using the predefined word array. For example, the
following statements declare the variable memory to be a
singledimensional array of eight elements, and each element is an integer:
subtype wordN is integer;
type intg is array (7 downto 0) of wordN;
2.3 Record Type
 An object of record type is composed of elements of the same or different
types. An example of record type is shown below: Type forecast is record
Tempr : integer range -100 to 100;

3. Access Types
 Values belonging to an access type are pointers to objects of
other types. For example:
type ptr_weathr is access forecast;
B. Verilog Data Types
1. Nets
 Nets are declared by the predefined word wire. Nets have
values that change continuously by the circuits that are
driving them.

Value Definition
0 Logic 0 (false)
1 Logic 1 (true)
X Unknown
Z High impedance
2. Register
 Register, in contrast to nets, stores values until they are
updated.
 Register, as its name suggests, represents data-storage
elements. Register is declared by the predefined word reg.

Value Definition
0 Logic 0 (false)
1 Logic 1 (true)
X Unknown
Z High impedance
3. Vectors
 Vectors are multiple bits. A register or a net can be declared
as a vector. Vectors are declared by brackets [ ]. Examples of
vectors are:

wire [3:0] a = 4’b1010;


reg [7:0] total = 8’d12;

 The first statement declares a net ‘a’. It has four bits, and its
initial value is 1010 (b stands for bit). The second statement
declares a register ‘total’. Its size is eight bits, and its value is
decimal 12 (d stands for decimal). Vectors are implemented in
almost all subsequent chapters.
4. Integers
 Integers are declared by the predefined word ‘integer’.

5. Real
 Real (floating-point) numbers are declared with the
predefined word real. Examples of real values are 2.4, 56.3,
and 5e12. The value 5e12 is equal to 5 × 1012.
VHDL and verilog comparison

• Verilog is considered better when describing a system at the


gate or transistor level due to its use of predefined primitives
at this level.
• VHDL is considered better at the system level; multiple
entity/architecture pairs lead to flexibility and ease in writing
code for complex systems.
 Data Types

• VHDL: Definitely a type-oriented language, and VHDL types


are built in or users can create and define them. User-defined
types give the user a tool to write the code effectively; these
types also support flexible coding. VHDL can handle objects
with multidimensional array types.
• Verilog: Compared to VHDL, Verilog data types are very
simple and easy to use. All types are defined by the language.
 Ease of Learning

• VHDL: For beginners, VHDL may seem hard to learn because


of its rigid type requirements. Advanced users, however, may
find these rigid type requirements easier to handle.
• Verilog: Easy to learn, Verilog users just write the module
without worrying about what library or package should be
attached. Many of the statements in the language are very
similar to those in C language.
 Libraries and Packages

• VHDL: Libraries and packages can be easily attached to the


standard VHDL package. Packages can include procedures and
functions, and the package can be made available to any
module that needs to use it. Packages are used to target a
certain design.
• Verilog: Libraries are not as easily implemented as in VHDL,
however the basic Verilog package includes several libraries as
integer part of the package.
VHDL Codes
Half adder

Library ieee;
use ieee.std_logic_1164.all;
entity half_adder is
port(a,b:in bit; sum,carry:out bit);
end half_adder;

architecture data of half_adder is


begin
sum<= a xor b;
carry <= a and b;
end data;
Full Adder

Library ieee;
use ieee.std_logic_1164.all;
entity full_adder is
port(a,b,c:in bit;
sum,carry:out bit);
end full_adder;

architecture data of full_adder is


begin sum<= a xor b xor c;
carry <= ((a and b) or (b and c) or (a and c));
end data;
• VHDL Code for a Multiplexer

Library ieee;
use ieee.std_logic_1164.all;
entity mux is
port(S1,S0,D0,D1,D2,D3:in bit;
Y:out bit);
end mux;

architecture data of mux is


begin
Y<= (not S0 and not S1 and D0) or (S0 and not S1 and D1) or (not S0 and
S1 and D2) or (S0 and S1 and D3);
end data;
• VHDL Code for a Demultiplexer

Library ieee;
use ieee.std_logic_1164.all;
entity demux is
port(S1,S0,D:in bit;
Y0,Y1,Y2,Y3:out bit);
end demux;

architecture data of demux is


begin
Y0<= ((Not S0) and (Not S1) and D);
Y1<= ((Not S0) and S1 and D);
Y2<= (S0 and (Not S1) and D);
Y3<= (S0 and S1 and D);
end data;
• VHDL Code – 4 bit Parity Checker

library ieee;
use ieee.std_logic_1164.all;
entity parity_checker is
port (a0,a1,a2,a3 : in std_logic;
p : out std_logic);
end parity_checker;

architecture gcoej of parity_checker is


begin
p <= (((a0 xor a1) xor a2) xor a3);
end Gcoej;
• Verilog code for 4-bit counter

module counter(count,enable,clk,rst_n);
input enable,clk,rst_n;
output reg[3:0] count;
always @(posedge clk or negedge rst_n)
begin
if(~rst_n) counter <= 4'b0000;
else if(enable) counter <= counter + 4'b0001;
end
Verilog code for ALU

You might also like