Fundamentals of HDL Notes
Fundamentals of HDL Notes
10EC45
PART-A
UNIT 1:
Introduction: Why HDL? , A Brief History of HDL, Structure of HDL Module,
Operators, Data types, Types of Descriptions, simulation and synthesis, Brief comparison
of VHDL and Verilog
6 Hours
UNIT 2:
Data Flow Descriptions: Highlights of Data-Flow Descriptions, Structure of Data-Flow
Description,DataType-Vectors
6 Hours
UNIT 3:
Behavioral Descriptions: Behavioral Description highlights, structure of HDL
behavioral Description, The VHDL variable Assignment Statement, sequential
statements.
7 Hours
UNIT 4:
Structural Descriptions: Highlights of structural Description, Organization of the
structural Descriptions, Binding, state Machines, Generate, Generic, and Parameter
statements.
7 Hours
PART-B
UNIT 5: Procedures, Tasks, and Functions: Highlights of Procedures, tasks, and
Functions, Procedures and tasks, Functions.
Advanced HDL Descriptions: File Processing, Examples of File Processing
7 Hours
UNIT 6:
Mixed Type Descriptions: Why Mixed-Type Description? VHDL User-Defined
Types, VHDL Packages, Mixed-Type Description examples
6 Hours
Dept. of ECE, SJBIT
Page 1
Fundamentals of HDL
10EC45
UNIT 7:
Mixed Language Descriptions: Highlights of Mixed-Language Description, How to
invoke One language from the Other, Mixed-language Description Examples, Limitations
of
Mixed-Language
Description
7 Hours
UNIT 8:
Synthesis Basics: Highlights of Synthesis, Synthesis information from Entity and
Module, Mapping Process and Always in the Hardware Domain.
6 Hours
TEXT BOOKS:
1.
REFERENCE BOOKS:
1.
2.
VHDL
3.
4.
Page 2
Fundamentals of HDL
10EC45
INDEX SHEET
SL.NO
1
TOPIC
University syllabus
UNIT 1: Introduction
01
Assignment Questions
UNIT - 2: Data Flow Descriptions
01
Assignment Questions
UNIT - 3: Behavioral Descriptions
01
Assignment Questions
UNIT - 4: Structural Descriptions
01
Assignment Questions
UNIT - 5: Procedures, Tasks, and Functions
01
Assignment Questions
UNIT - 6: Mixed Type Descriptions
01
Assignment Questions
UNIT 7: Mixed Language Descriptions
01
Assignment Questions
UNIT 8: Synthesis Basics
01
Assignment Questions
PAGE NO.
1-2
4-32
33
34-44
45
46-71
72
73-121
122
123-181
182
183-228
2 29
230-256
2 57
258-287
288
Page 3
Fundamentals of HDL
10EC45
UNIT 1: INTRODUCTION
Syllabus of unit 1:
Hours :6
Why HDL? , A Brief History of HDL, Structure of HDL Module, Operators, Data
types, Types of Descriptions, simulation and synthesis, Brief comparison of VHDL and
Verilog
Recommended readings:
1.
VHDL
Page 4
Fundamentals of HDL
10EC45
UNIT1: INTRODUCTION
Introduction to VHDL:
VHDL stands for VHSIC (Very High Speed Integrated Circuits) Hardware Description
Language. In the mid-1980s the U.S. Department of Defense and the IEEE sponsored
the development of this hardware description language with the goal to develop very
high-speed integrated circuit. It has become now one of industrys standard languages
used to describe digital systems.
The other widely used hardware description language is Verilog. Both are powerful
languages that allow you to describe and simulate complex digital systems. A third HDL
language is ABEL (Advanced Boolean Equation Language) which was specifically
designed for Programmable Logic Devices (PLD). ABEL is less powerful than the other
two languages and is less popular in industry
VHDL versus conventional programming languages
(1) A hardware description language is inherently parallel, i.e. commands, which
correspond to logic gates, are executed (computed) in parallel, as soon as a new input
arrives.
(2) A HDL program mimics the behavior of a physical, usually digital, system.
(3) It also allows incorporation of timing specifications (gate delays) as well as to
describe a system as an interconnection of different components.
Levels of representation and abstraction
A digital system can be represented at different levels of abstraction [1]. This keeps the
description and design of complex systems manageable. Figure 1 shows different levels
of abstraction.
Page 5
Fundamentals of HDL
10EC45
The highest level of abstraction is the behavioral level that describes a system in terms
of what it does (or how it behaves) rather than in terms of its components and
interconnection between them. A behavioral description specifies the relationship
between the input and output signals. This could be a Boolean expression or a more
abstract description such as the Register Transfer or Algorithmic level.
As an example, let us consider a simple circuit that warns car passengers when the door
is open or the seatbelt is not used whenever the car key is inserted in the ignition lock At
the behavioral level this could be expressed as,
Warning = Ignition_on AND ( Door_open OR Seatbelt_off)
The structural level, on the other hand, describes a system as a collection of gates and
components that are interconnected to perform a desired function. A structural
description could be compared to a schematic of interconnected logic gates. It is a
representation that is usually closer to the physical realization of a system. For the
example above, the structural representation is shown in Figure 2 below.
Page 6
Fundamentals of HDL
10EC45
An entity always starts with the keyword entity, followed by its name and the
keyword is. Next are the port declarations using the keyword port. An entity
declaration always ends with the keyword end, optionally [] followed by the name of the
entity.
Example 1:
entity FULLADDER is
-- (After a double minus sign (-) the rest of
-- the line is treated as a comment)
--- Interface description of FULLADDER
port ( x, y, Ci: in bit;
S, CO: out bit);
end FULLADDER;
The module FULLADDER has five interface ports. Three of them
and Ci indicated by the VHDL keyword in. The remaining two
and
CO indicated by out. The signals going through these ports are chosen to be of the type
bit. The type bit consists of the two characters '0' and '1' and represents the binary logic
values of the signals.
xThe NAME_OF_ENTITY is a user-selected identifier
xsignal_names consists of a comma separated list of one or more user-selected
identifiers that specify external interface signals.
xmode: is one of the reserved words to indicate the signal direction:
Dept. of ECE, SJBIT
Page 7
Fundamentals of HDL
10EC45
entity AND3 is
port (in1, in2, in3: in std_logic;
out1: out std_logic);
end AND3;
The entity is called AND3 and has 3 input ports, in1, in2, in3 and one output port, out1
The name AND3 is an identifier. Inputs are denoted by the keyword in, and outputs by
the keyword out. Since VHDL is a strongly typed language, each port has a defined
type. In this case, we specified the std_logic type. This is the preferred type of digital
signals. In contrast to the bit type that can only have the values 1 and 0, the std_logic
and std_ulogic types can have nine values. This is important to describe a digital system
accurately including the binary values 0 and 1, as well as the unknown value X, the
uninitialized value U, - for dont care, Z for high impedance, and several symbols to
Dept. of ECE, SJBIT
Page 8
Fundamentals of HDL
10EC45
indicate the signal strength (e.g. L for weak 0, H for weak 1, W for weak unknown - see
section on Enumerated Types). The std_logic type is defined in the std_logic_1164
package of the IEEE library. The type defines the set of values an object can have. This
has the advantage that it helps with the creation of models and helps reduce errors. For
instance, if one tries to assign an illegal value to an object, the compiler will flag the
error.
Example 3:
entity mux4_to_1 is
port (I0,I1,I2,I3: in std_logic;
S: in std_logic_vector(1downto 0);
y: out std_logic);
end mux4_to_1;
Example 4:
D Flip-Flop:
Page 9
Fundamentals of HDL
10EC45
entity dff_sr is
port (D,CLK,S,R: in std_logic;
Q,Qb: out std_logic);
end dff_sr;
Architecture body
The architecture body specifies how the circuit operates and how it is implemented. As
discussed earlier, an entity or circuit can be specified in a variety of ways, such as
behavioral, structural (interconnected components), or a combination of the above.
The architecture body looks as follows,
The header line of the architecture body defines the architecture name, e.g.
behavioral, and associates it with the entity, BUZZER. The architecture name can be
any legal identifier. The main body of the architecture starts with the keyword begin and
gives the Boolean expression of the function. We will see later that a behavioral model
can be described in several other ways. The <= symbol represents an assignment
operator and assigns the value of the expression on the right to the signal on the left. The
architecture body ends with an end keyword followed by the architecture name.
Example 2:
The behavioral description of a 3 input AND gate is shown below.
entity AND3 is
port (in1, in2, in3: in std_logic;
out1: out std_logic);
end AND3;
architecture behavioral_2 of AND3 is
Page 10
Fundamentals of HDL
10EC45
begin
out1 <= in1 and in2 and in3;
end behavioral_2;
Example 3:
entity XNOR2 is
port (A, B: in std_logic;
Z: out std_logic);
end XNOR2;
architecture behavioral_xnor of XNOR2 is
-- signal declaration (of internal signals X, Y)
signal X, Y: std_logic;
begin
X <= A and B;
Y <= (not A) and (not B);
Z <= X or Y;
End behavioral_xnor;
Example 4:
SR Flip Flop:
entity SRFF is
port (S, R: in std_logic;
Q, Qb: out std_logic);
end SRFF;
architecture behavioral_2 of SRFF is
begin
Q <= NOT (S and Qb);
Qb <= NOT ( R and Q);
end behavioral_2;
The statements in the body of the architecture make use of logic operators. In addition,
other types of operators including relational, shift, arithmetic are allowed as well.
Concurrency
The signal assignments in the above examples are concurrent statements. This implies
that the statements are executed when one or more of the signals on the right hand side
change their value (i.e. an event occurs on one of the signals).
In general, a change of the current value of a signal is called an event. For instance,
when the input S (in SR FF) changes, the first expression gets evaluated, which changes
the value of Q, change in Q in turn triggers second expression and evaluates Qb. Thus Q
and Qb are updated concurrently.
There may be a propagation delay associated with this change. Digital systems are
basically data-driven and an event which occurs on one signal will lead to an event
on another signal, etc. Hence, the execution of the statements is determined by the
flow of signal values. As a result, the order in which these statements are given does
not matter (i.e., moving the statement for the output Z ahead of that for X and Y does
not change the outcome). This is in contrast to conventional, software programs that
execute the statements in a sequential or procedural manner.
Example 5
architecture CONCURRENT of FULLADDER is
begin
S <= x xor y xor Ci a fter 5 ns;
CO <= (x and y) or (y and Ci) or (x and Ci) after 3 ns;
end CONCURRENT;
Page 11
Fundamentals of HDL
10EC45
Two concurrent signal assignment statements describe the model of the entity
FULLADDER.
The symbol <= indicates the signal assignment. This means that the value on the right
side of the symbol is calculated and subsequently assigned to the signal on the left side.
A concurrent signal assignment is executed whenever the value of a signal in the
expression on the right side changes. Due to the fact that all signals used in this example
are declared as ports in the entity declaration the arch_declarative_part remains empty
Event Scheduling:
The mechanism of delaying the new value is called scheduling an event. In the above
example, assignment to signals S and CO does not happen instantly. The after (keyword)
clause delays the assignment of the new value to S and CO by 3 ns.
Example2:
architecture CONCURRENT_VERSION2 of FULLADDER is
signal PROD1, PROD2, PROD3 : bit;
begin
SUM <= A xor B xor C; -- statement 1
CARRY <= PROD1 or PROD2 or PROD3; -- statement 2
PROD1 <= A and B; -- statement 3
PROD2 <= B and C; -- statement 4
PROD3 <= A and C; -- statement 5
end CONCURRENT_VERSION2;
library ieee;
use ieee.std_logic_1164.all;
entity Mux is
port( I: in std_logic_vector(3 downto 0);
S: in std_logic_vector(1 downto 0);
y: out std_logic);
end Mux;
-- architecture using logic expression
architecture behv1 of Mux is
begin
y<= (not(s(0)) and not(s(1)) and I(0)) or(s(0) and not(s(1))
and I(1)) or (not(s(0)) and s(1) and I(2))or (s(0) and s(1) and
I(3));
end behv1;
-- Architecture using when..else:
architecture behv2 of Mux is
begin
y <= I(0) when S="00" else
I(1) when S="01" else
I(2) when S="10" else
I(3) when S="11" else
Z ;
Page 12
Fundamentals of HDL
10EC45
end behv2;
-- architecture using with select statement
architecture behv3 of Mux is
begin
with s select
y<=i(0) when 00,
i(1) when 01,
i(2) when 10,
i(3) when 11,
Z when others;
end behv3;
Note: Z high impedence state should be entered in capital Z
Example 2: SR flipflop using when else statement
entity SRFF is
port ( S, R: in bit;
Q, QB: inout bit);
end RSFF;
architecture beh of RSFF is
begin
Q <= Q when S= 0 and R = 0 else
0 when S = 0 and R = 1 else
1 when S = 1 and R = 0 else
Z;
QB <= not(Q);
end beh;
The statement WHEN..ELSE conditions are executed one at a time in sequential order
until the conditions of a statement are met. The first statement that matches the conditions
required assigns the value to the target signal. The target signal for this example is the
local signal Q. Depending on the values of signals S and R, the values Q,1,0 and Z are
assigned to Q.
If more than one statements conditions match, the first statement that matches does
the assign, and the other matching state.
In with select statement all the alternatives arte checked simultaneously to find a
matching pattern. Therefore the with select must cover all possible values of the
selector
Structural Descriptions
A description style where different components of an architecture and their
interconnections are specified is known as a VHDL structural description. Initially, these
components are declared and then components' instances are generated or instantiated. At
the same time, signals are mapped to the components' ports in order to connect them like
wires in hardware. VHDL simulator handles component instantiations as concurrent
assignments.
Syntax:
component declaration:
component component_name
[generic (generic_list: type_name [:= expression] {;
generic_list: type_name [:= expression]} );]
[port (signal_list: in|out|inout|buffer type_name {;
signal_list: in|out|inout|buffer type_name} );]
end component;
Page 13
Fundamentals of HDL
10EC45
component instantiation:
The mapping of ports to the connecting signals during the instantiation can be done
through the positional notation. Alternatively, it may be done by using the named
notation.
If one of the ports has no signal connected to it (this happens, for example, when there
are unused outputs), a reserved word open may be used.
Example 1:
Page 14
Fundamentals of HDL
10EC45
the schematic and the statement is called the component instantiation statement. The
first word of the component instantiation statement (u1:nand2) gives instance a name, u1,
and specifies that it is an instance of the component nand_gate. The next words describes
how the component is connected to the set of the design using the port map clause.
The port map clause specifies what signals of the design should be connected to the
interface of the component in the same order as they are listed in the component
declaration. The interface is specified in order as A, B and then C, so this instance
connects set to A, QBAR to B and Q to C. This corresponds to the way the top gate in the
schematic is connected. The second instance, named n2, connects RESET to A, Q to A,
and QBAR to C of a different instance of the same nand_gate component in the same
manner as shown in the schematic.
The structural description of a design is simply a textual description of a schematic. A list
of components and there connections in any language is also called a netlist. The
structural description of a design in VHDL is one of many means of specifying netlists.
Example 2: Four Bit Adder Illustrating a structural VHDL model:
Page 15
Fundamentals of HDL
10EC45
-- 4-bit adder
library ieee;
use ieee.std_logic_1164.all;
entity FOURBITADD is
port (a, b: in std_logic_vector(3 downto 0);
Cin : in std_logic;
sum: out std_logic_vector (3 downto 0);
Cout: out std_logic);
end FOURBITADD;
architecture fouradder_structure of FOURBITADD is
signal c: std_logic_vector (4 downto 0);
component FULLADDER
port(x, y, ci: in std_logic;
s, co: out std_logic);
end component;
begin
FA0: FULLADDER
port map (a(0), b(0), Cin, sum(0), c(1));
FA1: FULLADDER
port map (a(1), b(1), C(1), sum(1), c(2));
FA2: FULLADDER
port map (a(2), b(2), C(2), sum(2), c(3));
FA3: FULLADDER
port map (a(3), b(3), C(3), sum(3), c(4));
Cout <= c(4);
end fouradder_structure;
We needed to define the internal signals c (4 downto 0) to indicate the nets that connect
the output carry to the input carry of the next full adder. For the first input we used the
input signal Cin. For the last carry we defined c (4) as an internal signal. We could not
use the output signal Cout since VHDL does not allow the use of outputs as internal
signals! For this reason we had to define the internal carry c(4) and assign c(4) to the
output carry signal Cout.
VHDL supports different classes of operators that operate on signals, variables and
constants. The different classes of operators are summarized below.
Page 16
Fundamentals of HDL
10EC45
The order of precedence is the highest for the operators of class 7, followed by class 6
with the lowest precedence for class 1. Unless parentheses are used, the operators with
the highest precedence are applied first. Operators of the same class have the same
precedence and are applied from left to right in an expression. As an example, consider
the following std_ulogic_vectors, X (=010), Y(=10), and Z (10101). The expression
not X & Y xor Z rol 1
is equivalent to ((not X) & Y) xor (Z rol 1) = ((101) & 10) xor (01011) =(10110) xor
(01011) = 11101. The xor is executed on a bit-per-bit basis.
1. Logic operators
The logic operators (and, or, nand, nor, xor and xnor) are defined for the bit,
boolean, std_logic and std_ulogic types and their vectors. They are used to define
Boolean logic expression or to perform bit-per-bit operations on arrays of bits. They give
a result of the same type as the operand (Bit or Boolean). These operators can be applied
to signals, variables and constants.
Notice that the nand and nor operators are not associative. One should use parentheses in
a sequence of nand or nor operators to prevent a syntax error:
X nand Y nand Z will give a syntax error and should be written as (X nand Y) nand Z.
2. Relational operators
The relational operators test the relative values of two scalar types and give as result a
Boolean output of TRUE or FALSE.
Notice that symbol of the operator <= (smaller or equal to) is the same one as the
assignment operator used to assign a value to a signal or variable. In the following
examples the first <= symbol is the assignment operator. Some examples of relational
operations are:
variable STS : Boolean;
constant A : integer :=24;
constant B_COUNT : integer :=32;
constant C : integer :=14;
STS <= (A < B_COUNT) ; -- will assign the value TRUE to STS
STS <= ((A >= B_COUNT) or (A > C)); -- will result in TRUE
STS <= (std_logic (1, 0, 1) < std_logic(0, 1,1));--makes STS FALSE
type new_std_logic is (0, 1, Z, -);
Dept. of ECE, SJBIT
Page 17
Fundamentals of HDL
10EC45
The operand is on the left of the operator and the number (integer) of shifts is on the right
side of the operator. As an example,
variable NUM1 :bit_vector := 10010110;
NUM1 srl 2;
will result in the number 00100101.
When a negative integer is given, the opposite action occurs, i.e. a shift to the left will be
a shift to the right. As an example
NUM1 srl 2 would be equivalent to NUM1 sll 2 and give the result 01011000.
Other examples of shift operations are for the bit_vector A = 101001
variable A: bit_vector :=101001;
Page 18
Fundamentals of HDL
10EC45
4. Addition operators
The addition operators are used to perform arithmetic operation (addition and
subtraction) on operands of any numeric type. The concatenation (&) operator is used to
concatenate two vectors together to make a longer one. In order to use these operators
one has to specify the ieee.std_logic_unsigned.all or std_logic_arith package package in
addition to the ieee.std_logic_1164 package.
Page 19
Fundamentals of HDL
10EC45
6. Multiplying operators
The multiplying operators are used to perform mathematical functions on numeric types
(integer or floating point).
The multiplication operator is also defined when one of the operands is a physical type
and the other an integer or real type.
The remainder (rem) and modulus (mod) are defined as follows:
A rem B = A (A/B)*B (in which A/B in an integer)
A mod B = A B * N (in which N is an integer)
The result of the rem operator has the sign of its first operand while the result of the mod
Dept. of ECE, SJBIT
Page 20
Fundamentals of HDL
10EC45
To define new type user must create a type declaration. A type declaration defines the
name of the type and the range of the type.
Type declarations are allowed in
(i) Package declaration (ii) Entity Declaration (iii) Architecture Declaration
(iv)Subprogram Declaration (v) Process Declaration
Enumerated Types:
An Enumerated type is a very powerful tool for abstract modeling. All of the values of an
enumerated type are user defined. These values can be identifiers or single character
literals.
An identifier is like a name, for examples: day, black, x
Character literals are single characters enclosed in quotes, for example: x, I, o
Type Fourval is (x, o, I, z);
Type color is (red, yello, blue, green, orange);
Type Instruction is (add, sub, lda, ldb, sta, stb, outa, xfr);
Page 21
Fundamentals of HDL
10EC45
Package instr is
Type instruction is (add, sub, lda, ldb, sta, stb, outa, xfr);
End instr;
Use work.instr.all;
Entity mp is
PORT (instr: in Instruction;
Addr: in Integer;
Data: inout integer);
End mp;
Architecture mp of mp is
Begin
Process (instr)
type reg is array(0 to 255) of integer;
variable a,b: integer;
variable reg: reg;
begin
case instr is
when lda => a:=data;
when ldb => b:=data;
when add => a:=a+b;
when sub => a:=a-b;
when sta => reg(addr) := a;
when stb => reg(addr):= b;
when outa => data := a;
when xfr => a:=b;
end case;
end process;
end mp;
Physical types:
These are used to represent real world physical qualities such as length, mass, time and
current.
Examples:
Page 22
Fundamentals of HDL
10EC45
Mohms = 1000kohms;
end units;
(2) Type current is range 0 to 1E9
units
na;
ua = 1000na;
ma = 1000ua;
a = 1000ma;
end units;
Composite Types:
Composite types consist of array and record types.
xArray types are groups of elements of same type
xRecord allow the grouping of elements of different types
xArrays are used for modeling linear structures such as ROM, RAM
xRecords are useful for modeling data packets, instruction etc.
xA composite type can have a value belonging to either a scalar type, composite type
or an access type.
Array Type:
Array type groups are one or more elements of the same type together as a single object.
Each element of the array can be accessed by one or more array indices.
Elements of an array can be accessed by specifying the index values into the array.
X<= Address_bus(25); transfers 26th element of array Address_bus to X.
Y := ROM_data(10)(5); transfers the value of 5th element in 10th row.
Multi dimentional array types may also be defined with two or more dimensions. The
following example defines a two-dimensional array variable, which is a matrix of
integers with four rows and three columns:
Type matrix4x3 is array (1 to 4, 1 to 3) of integer;
Variable matrixA: matrix4x3 := ((1,2,3), (4,5,6), (7,8,9), (10,11,12));
Variable m:integer;
The viable matrixA, will be initialized to
123
456
789
10 11 12
The array element matrixA(3,2) references the element in the third row and second
column, which has a value of 8.
m := matrixA(3,2); m gets the value 8
Dept. of ECE, SJBIT
Page 23
Fundamentals of HDL
10EC45
Record Type:
Record Types group objects of many types together as a single object. Each element of
the record can be accessed by its field name.
Record elements can include elements of any type including arrays and records.
Elements of a record can be of the same type or different types.
Example:
Verilog Operators
Operators in Verilog are the same as operators in programming languages. They take two
values and compare or operate on them to yield a new result. Nearly all the operators in
Verilog are exactly the same as the ones in the C programming language.
Dept. of ECE, SJBIT
Page 24
Fundamentals of HDL
Operator Type
Arithmetic
Relational
10EC45
Operator
Operation
Symbol
Performed
Multiply
Division
Addition
Subtraction
Modulus
Unary plus
Unary minus
>
Greater than
<
Less Than
>=
<=
==
Equality
!=
Inequality
Logical Negation
&&
Logical And
||
Logical Or
>>
Right Shift
<<
Left Shift
Conditional
Conditional
Reduction
Bitwise negation
~&
Bitwise nand
Bitwise or
~|
Bitwise nor
Bitwise xor
Equality
Logical
Shift
Page 25
Fundamentals of HDL
Concatenation
10EC45
^~
Bitwise xnor
~^
Bitwise xnor
{}
Examples:
x = y + z; //x will get the value of y added to the value of z
x = 1 >> 6; //x will get the value of 1 shifted right by 5 positions
x = !y //x will get the value of y inverted. If y is 1, x is 0 and vise versa
Nets (i)
an be thought as hardware wires driven by logic
Equal z when unconnected
Various types of nets
wire
wand (wired-AND)
wor (wired-OR)
tri
(tri-state)
In following examples: Y is evaluated, automatically, every time A or B changes
Nets (ii)
A
B
wire Y; // declaration
assign Y = A & B;
A
B
Y
wand Y;
// declaration
assign Y = A;
assign Y = B;
Dept. of ECE, SJBIT
Page 26
Fundamentals of HDL
wor Y;
10EC45
// declaration
assign Y = A;
assign Y = B;
dr
A
Registers:
tri Y;
// declaration
assign Y = (dr) ? A : z;
Vectors:
Represent buses
wire [3:0] busA;
reg [1:4] busB;
reg [1:0] busC;
Left number is MS bit
Slice management
busC[1] = busA[2];
busC[0] = busA[1];
Page 27
Fundamentals of HDL
10EC45
Parameters:
Arrays (i):
Syntax
Strings:
Page 28
Fundamentals of HDL
%%
\\
\
10EC45
%
\
Styles(Types) of Descriptions:
Behavioral Descriptions
Structural Descriptions
Switch Level Descriptions
Data Flow Descriptions
Mixed Type Descriptions
Behavioral Descriptions:
VHDL Behavioral description
entity half_add is
port (I1, I2 : in bit; O1, O2 : out bit);
end half_add;
architecture behave_ex of half_add is
--The architecture consists of a process construct
begin
process (I1, I2)
--The above statement is process statement
O1 <= I1 xor I2 after 10 ns;
O2 <= I1 and I2 after 10 ns;
end process;
end behave_ex;
Verilog behavioral Description:
module half_add (I1, I2, O1, O2);
input I1, I2;
output O1, O2;
reg O1, O2;
always @(I1, I2)
//The above abatement is always
//The module consists of always construct
begin
#10 O1 = I1 ^ I2;
#10 O2 = I1& I2;
end
endmodule
VHDL Structural Descriptions:
entity system is
port (a, b : in bit;
sum, cout : out bit);
end system;
architecture struct_exple of system is
component xor2
--The above statement is a component statement
port(I1, I2 : in bit;
O1 : out bit);
begin
Page 29
Fundamentals of HDL
10EC45
end component;
component and2
port(I1, I2 : in bit;
O1 : out bit);
end component;
begin
X1 : xor2 port map (a, b, sum);
A1 : and2 port map (a, b, cout);
end struct_exple;
Verilog Structural Description:
module system(a, b, sum, cout);
input a, b;
output sum, cout;
xor X1(sum, a, b);
//The above statement is EXCLUSIVE-OR gate
and a1(cout, a, b);
//The above statement is AND gate
endmodule
Switch Level Descriptions:
VHDL Description:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity Inverter is
Port (y : out std_logic; a: in std_logic );
end Inverter;
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
--See details in Chapter 5
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;
Page 30
Fundamentals of HDL
10EC45
Page 31
Fundamentals of HDL
10EC45
Operators
VHDL:An extensive set of operators is available in VHDL,but it does not have
predefined unary operators.
Verilog: An extensive set of operators is also available in verilog. It also has
predefined unary operators.
Procedures and Tasks
VHDL:Concurrent procedure calls are allowed. This allows a function to be written
inside the procedures body.This feature may contribute to an easier way to describe a
complex system.
Verilog:Concurrent task calls are allowed.Functions are not allowed to be written in
the tasks body.
Page 32
Fundamentals of HDL
10EC45
ASSIGNMENT QUESTIONS
1) Explain entity and architecture with an example
2) Explain structure of verilog module with an example
3) Explain VHDL operators in detail.
4) Explain verilog operators in detail.
5) Explain how data types are classified in HDL. Mention the advantages of VHDL data
types over verilog.
6) Mention the types of HDL descriptions. Explain dataflow and behavioral descriptions
7) Describe different types of HDL description with suitable example.
8) Mention different styles (types) of descriptions. Explain mixed type and mixed
language descriptions.
9) Compare VHDL and Verilog
10) Write the result of all shift and rotate operations inVHDL after applying them to a 7
bit vector A = 1001010
11) Explain composite and access data types with an example for each.
12) Discuss different logical operators used in HDLs
Page 33
Fundamentals of HDL
10EC45
Syllabus of unit 2:
Highlights
of
Data-Flow
Description,DataType_Vectors.
Hours :6
Descriptions,
Structure
of
Data-Flow
Recommended readings:
1.
VHDL
Page 34
Fundamentals of HDL
10EC45
Syntax:
signal list_of_signal_names: type [ := initial value];
Examples:
signal SUM, CARRY: std_logic;
signal DATA_BUS: bit_vector (0 to 7);
signal VALUE: integer range 0 to 100;
Signals are updated after a delta delay.
Example:
SUM <= (A xor B);
The result of A xor B is transferred to SUM after a delay called simulation Delta
which is a infinitesimal small amount of time.
Constant:
Syntax:
Page 35
Fundamentals of HDL
10EC45
Page 36
Fundamentals of HDL
10EC45
Page 37
Fundamentals of HDL
10EC45
output Y;
wire S1, S2, S3, S4, S5;
/* Assume 7 time units delay for all and, or, not.
In Verilog we cannot use specific time units,
such as nanoseconds. The delay here is
expressed in simulation screen units. */
assign #7 Y = S4 | S5;
/ / st 1
assign #7 S4 = A & S2 & S1; //st2
assign #7 S5 = B & S3 & S1; //st3
assign #7 S2 = ~ SEL;
/ / st 4
assign #7 S3 = ~ S2;
/ / st 5
assign #7 S1 = ~ Gbar;
/ / st 6
endmodule
HDL Code for a 2x2 Unsigned Combinational Array MultiplierVHDL and
Verilog:
VHDL 2x2 Unsigned Combinational Array Multiplier Description :
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity mult_arry is
port (a, b : in std_logic_vector(1 downto 0);
P : out std_logic_vector (3 downto 0));
end mult_arry;
architecture MULT_DF of mult_arry is
begin
-- For simplicity propagation delay times are not considered
-- in this example.
P(0) <= a(0) and b(0);
P(1) <= (a(0) and b(1)) xor (a(1) and b(0));
P(2) <= (a(1) and b(1)) xor ((a(0) and b(1)) and (a(1) and
b(0)));
P(3) <= (a(1) and b(1)) and ((a(0) and b(1)) and (a(1) and
b(0)));
Dept. of ECE, SJBIT
Page 38
Fundamentals of HDL
10EC45
end MULT_DF;
Verilog 2x2 Unsigned Combinational Array Multiplier Description
module mult_arry (a, b, P);
input [1:0] a, b;
output [3:0] P;
/*For simplicity, propagation delay times are not
considered in this example.*/
assign P[0] = a[0] & b[0];
assign P[1] = (a[0] & b[1]) ^ (a[1] & b[0]);
assign P[2] = (a[1] & b[1]) ^ ((a[0] & b[1]) & (a[1] & b[0]));
assign P[3] = (a[1] & b[1]) & ((a[0] & b[1])& (a[1] & b[0]));
endmodule
HDL Code for a D-LatchVHDL and Verilog:
VHDL D-Latch Description:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity D_Latch is
port (D, E : in std_logic;
Q, Qbar : buffer std_logic);
-- Q and Qbar are declared as buffer because they act as
--both input and output, they appear on the right and left
--hand side of signal assignment statements. inout or
-- linkage could have been used instead of buffer.
end D_Latch;
architecture DL_DtFl of D_Latch is
constant Delay_EorD : Time := 9 ns;
constant Delay_inv : Time := 1 ns;
begin
--Assume 9-ns propagation delay time between
--E or D and Qbar; and 1 ns between Qbar and Q.
Qbar <= (D and E) nor (not E and Q) after Delay_EorD;
Q <= not Qbar after Delay_inv;
Dept. of ECE, SJBIT
Page 39
Fundamentals of HDL
10EC45
end DL_DtFl;
Verilog D-Latch Description:
module D_latch (D, E, Q, Qbar);
input D, E;
output Q, Qbar;
/* Verilog treats the ports as internal ports,
so Q and Qbar are not considered here as
both input and output. If the port is
connected externally as bidirectional,
then we should use inout. */
time Delay_EorD = 9;
time Delay_inv = 1;
assign #Delay_EorD Qbar = ~((E & D) |
(~E & Q));
assign #Delay_inv Q = ~ Qbar;
endmodule
HDL Code of a 2x2 Magnitude ComparatorVHDL and Verilog:
VHDL 2x2 Magnitude Comparator Description:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity COMPR_2 is
port (x, y : in std_logic_vector(1 downto 0); xgty,
xlty : buffer std_logic; xeqy : out std_logic);
end COMPR_2;
architecture COMPR_DFL of COMPR_2 is
begin
xgty <= (x(1) and not y(1)) or (x(0) and not y(1) and
not y(0)) or
x(0) and x(1) and not y(0));
xlty <= (y(1) and not x(1)) or ( not x(0) and y(0)
and y(1)) or
(not x(0) and not x(1) and y(0));
xeqy <= xgty nor xlty;
end COMPR_DFL;
Verilog 2x2 Magnitude Comparator Description
module compr_2 (x, y, xgty, xlty, xeqy);
input [1:0] x, y;
output xgty, xlty, xeqy;
assign xgty = (x[1] & ~ y[1]) | (x[0] & ~ y[1]
Dept. of ECE, SJBIT
Page 40
Fundamentals of HDL
10EC45
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity adders_RL is
port (x, y : in std_logic_vector (2 downto 0);
cin : in std_logic;
sum : out std_logic_vector (2 downto 0);
cout : out std_logic);
end adders_RL;
--I. RIPPLE-CARRY ADDER
architecture RCarry_DtFl of adders_RL is
--Assume 4.0-ns propagation delay for all gates.
signal c0, c1 : std_logic;
constant delay_gt : time := 4 ns;
begin
sum(0) <= (x(0) xor y(0)) xor cin after 2*delay_gt;
--Treat the above statement as two 2-input XOR.
sum(1) <= (x(1) xor y(1)) xor c0 after 2*delay_gt;
--Treat the above statement as two 2-input XOR.
Dept. of ECE, SJBIT
Page 41
Fundamentals of HDL
10EC45
Page 42
Fundamentals of HDL
10EC45
Page 43
Fundamentals of HDL
10EC45
Page 44
Fundamentals of HDL
10EC45
ASSIGNMENT QUESTIONS
1) With illustrations briefly discuss
i) Signal declaration & assignment statements
ii) Concurrent signal assignment statements &
iii) Constant declaration & assignment statements.
2) Explain how an object that has a width of more than 1 bit is declared in HDL using
vector data types. Give examples.
3) Explain signal declaration & signal assignment statements with relevant examples.
4) Write a data flow description (in both VHDL & Verilog) for a full adder with active
high enable.
5) Write HDL codes for 2X2 bit combinational array multiplier.
6) How do you assign delay to a signal assignment statement? Explain with an example
in VHDL & verilog
7) What is a vector? Give an example for VHDL & verilog vector data types.
8) With the help of a truth table and K maps write Boolean expression for a 2-bit
magnitude comparator, write VHDL/ verilog code
9) What are the data types available in VHDL?
10) Write a HDL Code of a 2x1 Multiplexer
Page 45
Fundamentals of HDL
10EC45
Syllabus of unit 3:
Hours :7
Behavioral Description highlights, structure of HDL behavioral Description, The VHDL
variable Assignment Statement, sequential
statements.
Recommended readings:
1.
VHDL
Page 46
Fundamentals of HDL
10EC45
A data object is created by an object declaration and has a value and type associated
with it. An object can be a Constant, Variable or a Signal.
Signals can be considered wires in a schematic that can have a current value and future
values, and that are a function of the signal assignment statements.
Variables and Constants are used to model the behavior of a circuit and are used in
processes, procedures and functions.
Signal
Signals are declared with the following statement:
signal list_of_signal_names: type [ := initial value] ;
signal SUM, CARRY: std_logic;
signal CLOCK: bit;
signal TRIGGER: integer :=0;
signal DATA_BUS: bit_vector (0 to 7);
signal VALUE: integer range 0 to 100;
Signals are updated when their signal assignment statement is executed, after a
certain delay, as illustrated below,
SUM <= (A xor B);
The result of A xor B is transferred to SUM after a delay called simulation Delta which
is a infinitesimal small amount of time.
One can also specify multiple waveforms using multiple events as illustrated below,
signal wavefrm : std_logic;
wavefrm <= 0, 1 after 5ns, 0 after 10ns, 1 after 20 ns;
Constant
A constant can have a single value of a given type and cannot be changed during the
simulation. A constant is declared as follows,
constant list_of_name_of_constant: type [ := initial value] ;
where the initial value is optional. Constants can be declared at the start of an
architecture and can then be used anywhere within the architecture. Constants declared
within a process can only be used inside that specific process.
constant RISE_FALL_TME: time := 2 ns;
constant DELAY1: time := 4 ns;
constant RISE_TIME, FALL_TIME: time:= 1 ns;
constant DATA_BUS: integer:= 16;
Variable
A variable can have a single value, as with a constant, but a variable can be updated
using a variable assignment statement.
(1) The variable is updated without any delay as soon as the statement is executed.
(2) Variables must be declared inside a process.
The variable declaration is as follows:
variable list_of_variable_names: type [ := initial value] ;
A few examples follow:
variable CNTR_BIT: bit :=0;
variable VAR1: boolean :=FALSE;
Dept. of ECE, SJBIT
Page 47
Fundamentals of HDL
10EC45
Page 48
Fundamentals of HDL
10EC45
s2 and s3. All the signals will be updated at Delta time after the TRIGGER has arrived.
Thus the signals will have these values: s1= 2, s2= 4 (ie 1(old value of s1) +3), s3=2(old
value of s2) and RESULT=6 ie (1+2+3)
process (sensitivity_list)
[proc_declarativ_part]
begin
[sequential_statement_part]
end process [proc_label];
The sensitivity_list is a list of signal names within round brackets, for example
(A, B, C).
xProcess without sensitivity list must contain wait statement. With wait
statements, the process is executed until it reaches a wait statement. At this
instance it gets explicitly suspended. The statements within the process are
handled like an endless loop which is suspended for some time by a wait
statement.
Syntax:
process
[proc_declarativ_part]
Page 49
Fundamentals of HDL
10EC45
begin
[sequential_statements]
wait ...; -- at least one wait statement
[sequential_statements]
end process [proc_label];
Syntax:
if condition then
sequential_statements
{elsif condition then
sequential_statements}
[else
sequential_statements]
end if;
This if statement has two main parts, the condition and the statement body. A condition is
any boolean expression (an expression that evaluates to TRUE and FALSE, such as
expressions using relational operators). The condition in the example uses the attribute
last_value, which is used to determine the last value that a signal had. Attributes can be
used to obtain a lot of auxiliary information about signals.
The execution of the if statement begins by evaluating the condition. If the condition
evaluates to the value TRUE then the statements in the statement body will be executed.
Otherwise, execution will continue after the end if and the statement body of the if
statement is skipped. Thus, the assignment statement in this example is executed every
time there is a rising edge on the signal x, counting the number of rising edges.
Page 50
Fundamentals of HDL
10EC45
clock='1' and clock'event This condition becomes true, when there is a event on the
clock and clock state is equal to one i.e. rising edge of the clock.
clock'event Event is an attribute on the signal to check whether any change in the
signal is present.
case statement:
This statement is also identical to switch statement found in C programming
language.
Syntax:
case expression is
{when choices => sequential_statements}
[when others => sequential_statements]
end case;
The case statement selects one of the branches of execution based on the value of
expression.
Choices may be expressed as single value or as a range of values.
Either all possible values of expression must be covered with choices or the case
statement has to be completed with an others branch.
Example1: VHDL code for 4:1 MUX (using case statement)
library ieee;
use ieee.std_logic_1164.all;
entity mux is
Port ( i : in std_logic_vector(3 downto 0);
s : in std_logic_vector(1 downto 0);
y : out std_logic);
end mux;
architecture Behavioral of mux is
begin
process(s,i)
begin
case s is
when "00"=> y<=i(0);
when "01"=> y<=i(1);
when "10"=> y<=i(2);
when "11"=> y<=i(3);
when others =>y<='Z';
end case ;
end process;
Dept. of ECE, SJBIT
Page 51
Fundamentals of HDL
10EC45
end Behavioral;
Loop statement
as a
local variable within the loop statement. Assigning a value to identifier or reading
it outside the loop is not possible. The for statement is used to execute a list of
statements several times.
Example 1: four bit parallel adder using for loop
library ieee;
use ieee.std_logic_1164.all;
entity FOURBITADD is
port (a, b: in std_logic_vector(3 downto 0);
Cin : in std_logic;
sum: out std_logic_vector (3 downto 0);
Cout: out std_logic);
end FOURBITADD;
architecture fouradder_loop of FOURBITADD is
loop
sequential_statements
exit when (condition);
end loop [loop_label];
Exit statement allows the user to terminate the loop.
Page 52
Fundamentals of HDL
10EC45
end loop.
W ith next
the remaining sequential statements of the loop are skipped and the next
iteration is started at the beginning of the loop.
The exit directive skips the remaining statements and all remaining loop iterations.
Syntax:
next [loop_label][when condition];
exit [loop_label][when condition];
Example for next statement:
Process (a, b)
Begin
for i in 0 to 15 loop
if (i = 7 ) then
next;
else
q(i)<=a(i) AND b(i);
end if;
end loop;
end process;
The loop statement logically ands array of a and b bits. And transfers result to q. This
behavior continues except for 7th element. When i=7 the execution starts from next
iteration. The statements after next are not executed for current iteration.
wait statement:
This statements may only be used in processes without a sensitivity_list. The purpose of
the wait statement is to control activation and suspension of the process.
Syntax:
wait on signal_names
wait until condition
wait for time_expression];
The arguments of the wait statement have the following interpretations:
1. wait on signal_names: The process gets suspended at this line until there is
an event
on at least one signal in the list signal_names. The signal_names are separated by
commas; brackets are not used. It can be compared to the sensitivity_list of the
process statement.
Page 53
Fundamentals of HDL
10EC45
begin
process
begin
--asynchronous reset input
if (reset=0) then q<=0;
-- clock rising edge
elsif (clock='1' and clock'event) then
q <= d;
end if;
wait on reset,clock;
end process;
end behv;
The statements within the process body are executed only when there
is an event on reset or event on clock.
2. wait until condition: The process gets suspended until the condition becomes true.
Example (synchronous reset input)
Process
Begin
Wait until clock=1 and clockevent
If (reset=0) then
Q<=0;
Else q<=d;
End if;
End process;
When the rising edge of the clock occurs, the Reset signal is tested
first. If Reset is 0, d is assigned to q output.
3. wait for time_expression: The process becomes suspended for the time specified
by
time_expression.
Process
Begin
A<=0; Wait for 5ns;
A<=1; Wait for 5ns;
End process;
In the above statement, it generates a clock for 5ns low state and 5ns high state.
4. wait without any argument: The process gets suspended until the end of the
simulation.
assertion statement:
Generating error or warning messages is possible also within the process environment.
Syntax:
assert condition
[report string_expr]
[severity failure|error|warning|note];
Example:
In JK or D Flip flop, if both asynchronous inputs Set and Reset are at logical 0 state,
changes output q an qb both to be at 1 and 1 which is the violation of Boolean law. This
condition can be verified by assert statement.
Assert (Set=1 or Reset = 1)
Report Set and Reset both are 0
Severity ERROR;
If we wish to check D input has stabilized before the clock input changes, then assert
statement can be used as shown.
Dept. of ECE, SJBIT
Page 54
Fundamentals of HDL
10EC45
Page 55
Fundamentals of HDL
10EC45
Page 56
Fundamentals of HDL
10EC45
Page 57
Fundamentals of HDL
10EC45
Y = 1'bz;
else
begin
if (SEL)
Y = B;
/* This is a procedural assignment. Procedural assignments
are used to assign values to variables declared as regs
(as Y here in this module). Procedural statements have
to appear inside always, blocks, initial, tasks, or functions*/
else
Y = A;
end
end
endmodule
HDL Description of a 2x1 Multiplexer Using ELSE-IFVHDL and Verilog
VHDL 2x1 Multiplexer Using ELSE-IF
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity MUXBH is
port (A, B, SEL, Gbar : in std_logic;
Y : out std_logic);
end MUXBH;
architecture MUX_bh of MUXBH is
begin
process (SEL, A, B, Gbar)
variable temp : std_logic;
begin
if (Gbar = '0') and (SEL = '1') then
temp := B;
elsif (Gbar = '0') and (SEL = '0')then
temp := A;
else
temp := 'Z'; -- Z is high impedance.
end if;
Y <= temp;
end process;
end MUX_bh;
Verilog 2x1 Multiplexer Using ELSE-IF
module MUXBH (A, B, SEL, Gbar, Y);
input A, B, SEL, Gbar;
output Y;
reg Y; /* since Y is an output and appears inside always,
Dept. of ECE, SJBIT
Page 58
Fundamentals of HDL
10EC45
Page 59
Fundamentals of HDL
10EC45
Page 60
Fundamentals of HDL
10EC45
Page 61
Fundamentals of HDL
10EC45
Page 62
Fundamentals of HDL
10EC45
HDL Code for a 3-Bit Binary Counter Using the Case Statement
VHDL 3-Bit Binary Counter Case Statement Description
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity CT_CASE is
port (clk, clr : in std_logic;
q : buffer std_logic_vector (2 downto 0));
end CT_CASE;
architecture ctr_case of CT_CASE is
begin
ctr : process(clk)
variable temp : std_logic_vector (2 downto 0) := "101";
--101 is the initial value, so the counter starts from 110
begin
if rising_edge (clk) then
if clr = '0' then
case temp is
when "000" => temp := "001";
when "001" => temp := "010";
when "010" => temp := "011";
when "011" => temp := "100";
when "100" => temp := "101";
when "101" => temp := "110";
when "110" => temp := "111";
when "111" => temp := "000";
when others => temp := "000";
end case;
else
temp := "000";
end if;
end if;
q <= temp;
end process ctr;
end ctr_case;
Verilog 3-Bit Binary Counter Case Statement Description
module CT_CASE (clk, clr, q);
input clk, clr;
output [2:0] q;
reg [2:0] q;
initial /* The initial procedure is to force the counter
to start from initial count q=110 */
Page 63
Fundamentals of HDL
10EC45
q = 3'b101;
always @ (posedge clk)
begin
if (clr == 0)
begin
case (q)
3'd0 : q = 3'd1;
3'd1 : q = 3'd2;
3'd2 : q = 3'd3;
3'd3 : q = 3'd4;
3'd4 : q = 3'd5;
3'd5 : q = 3'd6;
3'd6 : q = 3'd7;
3'd7 : q = 3'd0;
endcase
end
else
q = 3'b000;
end
endmodule
Behavioral Description of a 4-bit positive Edge Triggered counter
HDL Code for a 4-Bit Counter with Synchronous ClearVHDL and Verilog
VHDL 4-Bit Counter with Synchronous Clear Description
library ieee;
use ieee.std_logic_1164.all;
entity CNTR_LOP is
port (clk, clr : in std_logic; q :
buffer std_logic_vector (3 downto 0));
end CNTR_LOP;
architecture CTR_LOP of CNTR_LOP is
begin
ct : process(clk)
variable temp : std_logic_vector (3 downto 0) := "0000";
variable result : integer := 0;
begin
if rising_edge (clk) then
if (clr = '0') then
result := 0;
-- change binary to integer
lop1 : for i in 0 to 3 loop
if temp(i) = '1' then
result := result + 2**i;
end if;
end loop;
-- increment result to describe a counter
Dept. of ECE, SJBIT
Page 64
Fundamentals of HDL
10EC45
result := result + 1;
-- change integer to binary
for j in 0 to 3 loop
if (result MOD 2 = 1) then
temp (j) := '1';
else temp (j) := '0';
end if;
-- integer division by 2
result := result/2;
end loop;
else temp := "0000";
end if;
q <= temp;
end if;
end process ct;
end CTR_LOP;
Verilog 4-Bit Counter with Synchronous Clear Description
module CNTR_LOP (clk, clr, q);
input clk, clr;
output [3:0] q;
reg [3:0] q;
integer i, j, result;
initial
begin
q = 4'b0000; //initialize the count to 0
end
always @ (posedge clk)
begin
if (clr == 0)
begin
result = 0;
//change binary to integer
for (i = 0; i < 4; i = i + 1)
begin
if (q[i] == 1)
result = result + 2**i;
end
result = result + 1;
for (j = 0; j < 4; j = j + 1)
begin
if (result %2 == 1)
q[j] = 1;
else
q[j] = 0;
result = result/2;
Dept. of ECE, SJBIT
Page 65
Fundamentals of HDL
10EC45
end
end
else q = 4'b0000;
end
endmodule
Page 66
Fundamentals of HDL
10EC45
--Successive division by 2
result := result/2;
end loop;
q <= temp;
end if;
end process ct;
end CNTR_Hld;
Verilog 4-Bit Counter with Synchronous Hold Description
module CT_HOLD (clk, hold, q);
input clk, hold;
output [3:0] q;
reg [3:0] q;
integer i, result;
initial
begin
q = 4'b0000; //initialize the count to 0
end
always @ (posedge clk)
begin
result = 0;
//change binary to integer
for (i = 0; i <= 3; i = i + 1)
begin
if (q[i] == 1)
result = result + 2**i;
end
result = result + 1;
for (i = 0; i <= 3; i = i + 1)
begin
if (hold == 1)
i = 4; //4 is out of range, exit.
else
begin
if (result %2 == 1)
q[i] = 1;
else
q[i] = 0;
result = result/2;
end
end
end
endmodule
Page 67
Fundamentals of HDL
10EC45
Page 68
Fundamentals of HDL
10EC45
Booth Algorithm:
Page 69
Fundamentals of HDL
10EC45
case temp is
when "10" => sum (7 downto 4) :=
sum (7 downto 4) + Y1;
when "01" => sum (7 downto 4) :=
sum (7 downto 4) + Y;
when others => null;
end case;
sum := sum srl 1; --This is a logical
--shift of one position to the right
sum (7) := sum(6);
--The above two statements perform arithmetic shift where
--the sign of the number is preserved after the shift.
E1(0) := x(i);
end loop;
if (y = "1000") then
--If Y = 1000; then according to our code,
--Y1 = 1000 (-8 not 8 because Y1 is 4 bits only).
--The statement sum = -sum adjusts the answer.
sum := - sum;
end if;
z <= sum;
end process;
end booth_4;
Verilog 4x4-Bit Booth Algorithm
module booth (X, Y, Z);
input signed [3:0] X, Y;
output signed [7:0] Z;
reg signed [7:0] Z;
reg [1:0] temp;
integer i;
reg E1;
reg [3:0] Y1;
always @ (X, Y)
begin
Z = 8'd0;
E1 = 1'd0;
for (i = 0; i < 4; i = i + 1)
begin
temp = {X[i], E1};
Dept. of ECE, SJBIT
Page 70
Fundamentals of HDL
10EC45
Page 71
Fundamentals of HDL
10EC45
ASSIGNMENT QUESTIONS
1) Write behavioral description of half adder in VHDL and verilog with propagation
delay of 5nsec. Discuss the important features of their description in VHDL and
verilog
2) Explain the structure of various loop statements in HDL with examples
3) Explain verilog Repeat and Forever statements with an example
4) Explain different loop statements in
5) Explain IF and CASE statements with examples
6) Explain Booth algorithm with a flow chart. Write VHDL or verilog description for
4X4 bit booth algorithm.
7) Write VHDL code for a D-latch using variable assignment & signal assignment
statements with simulation waveforms clearly distinguish between the two statements.
8) Write a behavioral description of D- Latch using variable & signal assign
9) What is HDL? Why do you need it
Page 72
Fundamentals of HDL
10EC45
Syllabus of unit 4:
Hours :7
Highlights of structural Description, Organization of the structural Descriptions, Binding,
state Machines, Generate, Generic, and Parameter statements.
Recommended readings:
1.
VHDL
Page 73
Fundamentals of HDL
10EC45
Page 74
Fundamentals of HDL
10EC45
endmodule
Page 75
Fundamentals of HDL
10EC45
entity half_add is
port (a, b : in std_logic; S, C : out std_logic);
end half_add;
architecture HA_str of half_add is
component xor2
port (I1, I2 : in std_logic; O1 : out std_logic);
end component;
component and2
port (I1, I2 : in std_logic; O1 : out std_logic);
end component;
begin
X1 : xor2 port map (a, b, S);
A1 : and2 port map (a, b, C);
end HA_str;
Verilog Half Adder Description
module half_add (a, b, S, C);
input a, b;
output S, C;
xor (S, a, b);
and (C, a, b);
endmodule
BINDING
Page 76
Fundamentals of HDL
10EC45
Page 77
Fundamentals of HDL
10EC45
Page 78
Fundamentals of HDL
10EC45
use IEEE.STD_LOGIC_1164.ALL;
entity bind2 is
port (I1, I2 : in std_logic; O1 : out std_logic);
end bind2;
architecture xor2_0 of bind2 is
begin
O1 <= I1 xor I2; --This is exclusive-or with zero delay.
end xor2_0;
architecture and2_0 of bind2 is
begin
O1 <= I1 and I2; --This is a two input and gate with zero delay.
end and2_0;
architecture and2_7 of bind2 is
begin
O1 <= I1 and I2 after 7 ns; -- This is a two input and gate
-- with 7-ns delay.
end and2_7;
architecture or2_0 of bind2 is
begin
O1 <= I1 or I2; -- This is a two input or gate with zero delay.
end or2_0;
architecture or2_7 of bind2 is
begin
O1 <= I1 or I2 after 7 ns; -- This is a two input or gate
-- with 7-ns delay.
end or2_7;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity bind3 is
port (I1, I2, I3 : in std_logic; O1 : out std_logic);
end bind3;
architecture and3_0 of bind3 is
begin
O1 <= I1 and I2 and I3; -- This is a three input and gate
-- with zero delay.
end and3_0;
Page 79
Fundamentals of HDL
10EC45
Page 80
Fundamentals of HDL
10EC45
Page 81
Fundamentals of HDL
10EC45
Page 82
Fundamentals of HDL
10EC45
Page 83
Fundamentals of HDL
10EC45
Page 84
Fundamentals of HDL
10EC45
component and2
port (I1, I2 : in std_logic; O1 : out std_logic);
end component;
for A1 : and2 use entity work.bind2 (and2_0);
for X1 : xor2 use entity work.bind2 (xor2_0);
begin
X1 : xor2 port map (I1, I2, O1);
A1 : and2 port map (I1, I2, O2);
end HA;
HDL Description of a Full Adder (Figures 4.6a and 4.6b)VHDL and Verilog
VHDL Full Adder Description
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity FULL_ADDER is
Port (x, y, cin : in std_logic; sum, carry : out std_logic);
end FULL_ADDER;
architecture full_add of FULL_ADDER is
component HA
Port (I1, I2 : in std_logic; O1, O2 : out std_logic);
end component;
component or2
Port (I1, I2 : in std_logic; O1 : out std_logic);
end component;
for all : HA use entity work.bind22 (HA);
for all : or2 use entity work.bind2 (or2_0);
signal s0, c0, c1 : std_logic;
begin
HA1 : HA port map (y, cin, s0, c0);
HA2 : HA port map (x, s0, sum, c1);
r1 : or2 port map (c0, c1, carry);
end full_add;
Verilog Full Adder Description
module FULL_ADDER (x, y, cin, sum, carry);
input x, y, cin;
output sum, carry;
HA H1 (y, cin, s0, c0);
HA H2 (x, s0, sum, c1);
//The above two statements bind module HA
//to the present module FULL_ADDER
or (carry, c0, c1);
endmodule
Dept. of ECE, SJBIT
Page 85
Fundamentals of HDL
10EC45
Page 86
Fundamentals of HDL
10EC45
Page 87
Fundamentals of HDL
10EC45
Page 88
Fundamentals of HDL
10EC45
Page 89
Fundamentals of HDL
10EC45
end component;
for all : D_latch use entity work.bind22 (D_latch);
for all : inv use entity work.bind1 (inv_1);
signal clkb, clk2, Q0, Qb0 : std_logic;
begin
D0 : D_latch port map (D, clkb, Q0, Qb0);
D1 : D_latch port map (Q0, clk2, Q, Qbar);
in1 : inv port map (clk, clkb);
in2 : inv port map (clkb, clk2);
end D_FF;
Verilog Master-Slave D Flip-Flop
module D_FFMaster (D, clk, Q, Qbar);
input D, clk;
output Q, Qbar;
not #1 (clkb, clk);
not #1 (clk2, clkb);
D_latch D0 (D, clkb, Q0, Qb0);
D_latch D1 (Q0, clk2, Q, Qbar);
endmodule
module D_latch (D, E, Q, Qbar);
input D, E;
output Q, Qbar;
and #4 gate1 (s1, D, E);
and #4 gate2 (s2, Eb, Q);
not #1 (Eb, E);
nor #4 (Qbar, s1, s2);
not #1 (Q, Qbar);
endmodule
Page 90
Fundamentals of HDL
10EC45
Page 91
Fundamentals of HDL
10EC45
Page 92
Fundamentals of HDL
10EC45
Page 93
Fundamentals of HDL
10EC45
Page 94
Fundamentals of HDL
10EC45
component full_adder
port (I1, I2, I3 : in std_logic; O1, O2 : out std_logic);
end component;
component Inv
port (I1 : in std_logic; O1 : out std_logic);
end component;
component nor2
port (I1, I2 : in std_logic; O1 : out std_logic);
end component;
component and3
port (I1, I2, I3 : in std_logic; O1 : out std_logic);
end component;
for all : full_adder use entity work.bind32 (full_add);
for all : Inv use entity work.bind1 (inv_0);
for all : nor2 use entity work.bind2 (nor2_0);
for all : and3 use entity work.bind3 (and3_7);
--To reduce hazards, an AND gate is
--implemented with a 7-ns delay.
signal sum, Yb : std_logic_vector (2 downto 0);
signal carry : std_logic_vector (1 downto 0);
begin
in1 : inv port map (Y(0), Yb(0));
in2 : inv port map (Y(1), Yb(1));
in3 : inv port map (Y(2), Yb(2));
F0 : full_adder port map (X(0), Yb(0), '0', sum(0), carry(0));
F1 : full_adder port map (X(1), Yb(1), carry(0),
sum(1), carry(1));
F2 : full_adder port map (X(2), Yb(2), carry(1),
sum(2), xgty);
--The current module could have been linked to the 3-bit adders
--designed in Listing 4.18 instead of linking to
--F0, F1, and F2, as was done here.
a1 : and3 port map (sum(0), sum(1), sum(2), xeqy);
n1 : nor2 port map (xeqy, xgty, xlty);
end cmpare;
Verilog 3-Bit Comparator Using Adders
module three_bit_cmpare (X, Y, xgty, xlty, xeqy);
input [2:0] X, Y;
output xgty, xlty, xeqy;
wire [1:0] carry;
Dept. of ECE, SJBIT
Page 95
Fundamentals of HDL
10EC45
FIGURE4.15 K maps
Page 96
Fundamentals of HDL
10EC45
Page 97
Fundamentals of HDL
10EC45
Page 98
Fundamentals of HDL
10EC45
STATE MACHINES
FIGURE 4.17 Logic symbol of a 3-bit counter with active low clear.
FIGURE 4.18 State diagram of a 3-bit counter with active low clear.
Page 99
Fundamentals of HDL
10EC45
Page 100
Fundamentals of HDL
10EC45
FIGURE 4.20 Logic diagram of a 3-bit synchronous counter with active low clear using
JK master- slave flip flops.
HDL Description of a 3-Bit Synchronous Counter Using JK Master-Slave Flip-Flops
VHDL and Verilog
***Begin Listing***
VHDL 3-Bit Synchronous Counter Using JK Master-Slave Flip-Flops
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity countr_3 is
port(clk, clrbar : in std_logic;
q, qb : buffer std_logic_vector(2 downto 0));
end countr_3;
architecture CNTR3 of countr_3 is
--Start component declaration statements
--Some simulators will not allow mapping between
--buffer and out. In this
--case, change all out to buffer.
component JK_FF
port (I1, I2, I3 : in std_logic; O1, O2 : buffer std_logic);
end component;
component inv
port (I1 : in std_logic; O1 : out std_logic);
end component;
Page 101
Fundamentals of HDL
10EC45
component and2
port (I1, I2 : in std_logic; O1 : out std_logic);
end component;
component or2
port (I1, I2 : in std_logic; O1 : out std_logic);
end component;
for all : JK_FF use entity work.bind32 (JK_Master);
for all : inv use entity work.bind1 (inv_0);
for all : and2 use entity work.bind2 (and2_0);
for all : or2 use entity work.bind2 (or2_0);
signal J1, K1, J2, K2, clr, clrb1, s1 : std_logic;
begin
FF0 : JK_FF port map (clrb1, '1', clk, q(0), qb(0));
-- clrb1 has the same logic as clrbar
A1 : and2 port map (clrb1, q(0), J1);
inv1 : inv port map (clr, clrb1);
inv2 : inv port map (clrbar, clr);
r1 : or2 port map (q(0), clr, K1);
FF1 : JK_FF port map (J1, K1, clk, q(1), qb(1));
A2 : and2 port map (q(0), q(1), s1);
A3 : and2 port map (clrb1, s1, J2);
r2 : or2 port map (s1, clr, K2);
FF2 : JK_FF port map (J2, K2, clk, q(2), qb(2));
end CNTR3;
Verilog 3-Bit Synchronous Counter Using JK Master-Slave Flip-Flops
module countr_3 (clk, clrbar, q, qb);
input clk, clrbar;
output [2:0] q, qb;
JK_FF FF0(clrb1, 1'b1, clk, q[0], qb[0]);
// clrb1 has the same logic as clrbar
and A1 (J1, q[0], clrb1);
/*The name of the and gate A1 and all other
gates in this code are optional; it can be omitted.*/
not inv1 (clrb1, clr);
not inv2 (clr, clrbar);
or r1 (K1, q[0], clr);
JK_FF FF1 (J1, K1, clk, q[1], qb[1]);
Dept. of ECE, SJBIT
Page 102
Fundamentals of HDL
10EC45
Figure 4.21 simulation waveform of a 3-bit synchronous counter with active low clear.
Structural Description of a 3-Bit Synchronous Even Counter with active High Hold
Page 103
Fundamentals of HDL
10EC45
Page 104
Fundamentals of HDL
10EC45
HDL Description of a 3-Bit Synchronous Even Counter with HoldVHDL and Verilog
VHDL 3-Bit Synchronous Even Counter with Hold
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity CTR_EVEN is
port (H, clk : in std_logic;
Q, Qbar : buffer std_logic_vector (2 downto 0));
end CTR_EVEN;
architecture Counter_even of CTR_EVEN is
--Some simulators will not allow mapping between
--buffer and out. In this
--case, change all out to buffer.
component inv
port (I1 : in std_logic; O1 : out std_logic);
end component;
component and2
port (I1, I2 : in std_logic; O1 : out std_logic);
end component;
component or2
port (I1, I2 : in std_logic; O1 : out std_logic);
end component;
component and3
port (I1, I2, I3 : in std_logic; O1 : out std_logic);
end component;
component or3
port (I1, I2, I3 : in std_logic; O1 : out std_logic);
end component;
component D_FF
port (I1, I2 : in std_logic; O1, O2 : buffer std_logic);
end component;
for all : D_FF use entity work.bind22 (D_FFMaster);
for all : inv use entity work.bind1 (inv_0);
for all : and2 use entity work.bind2 (and2_0);
for all : and3 use entity work.bind3 (and3_0);
for all : or2 use entity work.bind2 (or2_0);
Dept. of ECE, SJBIT
Page 105
Fundamentals of HDL
10EC45
Page 106
Fundamentals of HDL
10EC45
Page 107
Fundamentals of HDL
10EC45
Page 108
Fundamentals of HDL
10EC45
Page 109
Fundamentals of HDL
10EC45
Page 110
Fundamentals of HDL
10EC45
component and2
port (I1, I2 : in std_logic; O1 : out std_logic);
end component;
component or2
port (I1, I2 : in std_logic; O1 : out std_logic);
end component;
component and3
port (I1, I2, I3 : in std_logic; O1 : out std_logic);
end component;
component JK_FF
port (I1, I2, I3 : in std_logic; O1, O2 : buffer std_logic);
end component;
for all : JK_FF use entity work.bind32 (JK_Master);
for all : inv use entity work.bind1 (inv_1);
for all : and2 use entity work.bind2 (and2_4);
for all : and3 use entity work.bind3 (and3_4);
for all : or2 use entity work.bind2 (or2_4);
signal clrbar, Dirbar, J1, K1, J2, K2 : std_logic;
signal s : std_logic_vector (11 downto 0);
begin
in1 : inv port map (clr, clrbar);
in2 : inv port map (Dir, Dirbar);
an1 : and2 port map (Dirbar, Qbar(0), s(0));
an2 : and2 port map (Dir, Q(0), s(1));
r1 : or2 port map (s(0), s(1), s(2));
an3 : and2 port map (s(2), clrbar, s(3));
r2 : or2 port map (s(3), clr, K1);
r3 : or2 port map (s(2), Q(1), s(4));
an4 : and2 port map (clrbar, s(4), J1);
an5 : and3 port map (Dirbar, Qbar(1), Qbar(0), s(5));
an6 : and3 port map (Dir, Q(1), Q(0), s(6));
r4 : or2 port map (s(6), s(5), s(7));
an7 : and2 port map (s(7), clrbar, J2);
r5 : or2 port map (J2, clr, K2);
JKFF0 : JK_FF port map (clrbar, '1', clk, Q(0), Qbar(0));
JKFF1 : JK_FF port map (J1, K1, clk, Q(1), Qbar(1));
JKFF2 : JK_FF port map (J2, K2, clk, Q(2), Qbar(2));
Page 111
Fundamentals of HDL
10EC45
Page 112
Fundamentals of HDL
10EC45
Page 113
Fundamentals of HDL
10EC45
Page 114
Fundamentals of HDL
10EC45
Page 115
Fundamentals of HDL
10EC45
component and2
port (I1, I2 : in std_logic; O1 : out std_logic);
end component;
component and3
port (I1, I2, I3 : in std_logic; O1 : out std_logic);
end component;
component and4
port (I1, I2, I3, I4 : in std_logic; O1 : out std_logic);
end component;
component or2
port (I1, I2 : in std_logic; O1 : out std_logic);
end component;
component or3
port (I1, I2, I3 : in std_logic; O1 : out std_logic);
end component;
component D_FF
port (I1, I2 : in std_logic; O1, O2 : buffer std_logic);
end component;
for all : D_FF use entity work.bind22 (D_FFMaster);
for all : buf use entity work.bind1 (buf_1);
for all : and2 use entity work.bind2 (and2_4);
for all : and3 use entity work.bind3 (and3_4);
for all : and4 use entity work.bind4 (and4_4);
for all : or2 use entity work.bind2 (or2_4);
for all : or3 use entity work.bind3 (or3_4);
signal s : std_logic_vector (6 downto 0);
signal D : std_logic_vector (3 downto 0);
begin
b1 : buf port map (Qbar(0), D(0));
DFF0 : D_FF port map (D(0), clk, Q(0), Qbar(0));
--Assume and gates and or gates have 4 ns propagation
--delay and invert has 1 ns.
a1 : and3 port map (Qbar(3), Qbar(1), Q(0), s(0));
a2 : and2 port map (Q(1), Qbar(0), s(1));
r1 : or2 port map (s(0), s(1), D(1));
DFF1 : D_FF port map (D(1), clk, Q(1), Qbar(1));
Dept. of ECE, SJBIT
Page 116
Fundamentals of HDL
10EC45
Page 117
Fundamentals of HDL
10EC45
endmodule
GENERATE (HDL), GENERIC(VHDL) AND PARAMETER(VERILOG)
HDL Description of N-Bit Magnitude Comparator Using Generate Statement
VHDL and Verilog
VHDL N-Bit Magnitude Comparator Using Generate Statement
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity compr_genr is
generic (N : integer := 3);
port (X, Y : in std_logic_vector (N downto 0);
xgty, xlty, xeqy : buffer std_logic);
end compr_genr;
architecture cmpare_str of compr_genr is
--Some simulators will not allow mapping between
--buffer and out. In this
--case, change all out to buffer.
component full_adder
port (I1, I2, I3 : in std_logic; O1, O2 : out std_logic);
end component;
component inv
port (I1 : in std_logic; O1 : out std_logic);
end component;
component nor2
port (I1, I2 : in std_logic; O1 : out std_logic);
end component;
component and2
port (I1, I2 : in std_logic; O1 : out std_logic);
end component;
signal sum, Yb : std_logic_vector (N downto 0);
signal carry, eq : std_logic_vector (N + 1 downto 0);
for all : full_adder use entity work.bind32 (full_add);
for all : inv use entity work.bind1 (inv_0);
for all : nor2 use entity work.bind2 (nor2_7);
for all : and2 use entity work.bind2 (and2_7);
begin
carry(0) <= '0';
eq(0) <= '1';
G1 : for i in 0 to N generate
Dept. of ECE, SJBIT
Page 118
Fundamentals of HDL
10EC45
Page 119
Fundamentals of HDL
10EC45
Page 120
Fundamentals of HDL
10EC45
begin : u
memory M1 (sel, R_W, Data_in [i], Data_out[i]);
end
endgenerate
endmodule
Page 121
Fundamentals of HDL
10EC45
ASSIGNMENT QUESTIONS
Page 122
Fundamentals of HDL
10EC45
Syllabus of unit 5:
Highlights
Functions.
of
Procedures,
Hours :7
tasks,
and
Functions,
Procedures
and
tasks,
Recommended readings:
1.
VHDL
Page 123
Fundamentals of HDL
10EC45
Page 124
Fundamentals of HDL
10EC45
Functions
A function call is the subprogram of the form that returns a value. It can also be defined
as a subprogram that either defines a algorithm for computing values or describes a
behavior. The important feature of the function is that they are used as expressions that
return values of specified type. This is the main difference from another type of
subprogram: procedures, which are used as statements. The results return by a function
can be either scalar or complex type.
Function Syntax
Functions can be either pure (default) or impure. Pure functions always return the same
value for the same set of actual parameters. Impure functions may return different values
for the same set of parameters. Additionally an impure function may have side effects
like updating objects outside their scope, which is not allowed in pure function.
The function definition consists of two parts:
1) Function declaration: this consists of the name, parameter list and type of values
returned by function
2) Function body: this contains local declaration of nested subprograms, types,
constants, variables, files, aliases, attributes and groups, as well as sequence of
statements specifying the algorithm performed by the function.
The function declaration is optional and function body, which contains the copy of it is
sufficient for correct specification. However, if a function declaration exists, the function
body declaration must exist in the given scope.
Functional Declaration:
The function declaration can be preceded by an optional reserved word pure or impure,
denoting the character of the function. If the reserved word is omitted it is assumed to be
Dept. of ECE, SJBIT
Page 125
Fundamentals of HDL
10EC45
pure by default.
The function name (id), which appears after the reserved word function can either be an
identifier or an operator symbol. Specification of new functions for existing operators is
allowed in VHDL and is called OPERATOR OVERLOADING.
The parameters of the function are by definition INPUTS and therefore they do not need
to have the mode (direction) explicitly specified. Only constants, signals and files can be
function parameters .The object class is specified by using the reserved words (constant,
signal or file respectively) preceding the parameter name. If no reserved word is used, it
is assumed that the parameter is a CONSTANT.
In case of signal parameters the attributes of the signal are passed into the function,
except for `STABLE, `QUIET, `TRANSACTION and `DELAYED, which may not be
accessed within the function.
Variable class is NOT allowed since the result of operations could be different when
different instantiations are executed. If a file parameter is used, it is necessary to specify
the type of data appearing in the opened file.
Function Body:
Function body contains a sequence of statements that specify the algorithm to be realized
within the function. When the function is called, the sequence of statements is executed..
A function body consists of two parts: declarations and sequential statements. At the end
of the function body, the reserved word END can be followed by an optional reserved
word FUNCTION and the function name.
Pure And Impure Functions
Pure Functions
Function Does Not Refer to Any Variables or Signals Declared by Parent_
Result of Function Only Depends on Parameters Passed to It
Always Returns the Same Value for Same Passed Parameters No Matter When It
Is Called
If Not Stated Explicitly, a Function Is Assumed to Be Pure
Impure Function
Can State Explicitly and Hence Use Parents Variables and/or Signals for
Function Computation
May Not Always Return the Same Value
Function Calling
Once Declared, Can Be Used in Any Expression_
A Function Is Not a Sequential Statement So It Is Called As Part of an Expression
Page 126
Fundamentals of HDL
10EC45
Example 1
The first function name above is called func_1, it has three parameters A,B and X,
all of the REAL types and returns a value also of REAL type.
The second function defines a new algorithm for executing multiplication. Note
that the operator is enclosed in double quotes and plays the role of the function
name.
The third is based on the signals as input parameters, which is denoted by the
reserved word signal preceding the parameters.
The fourth function declaration is a part of the function checking for end of file,
consisting of natural numbers. Note that the parameter list uses the Boolean type
declaration.
Example 2
Page 127
Fundamentals of HDL
10EC45
The case statement has been used to realize the function algorithm. The formal
parameter appearing in the declaration part is the value constant, which is a parameter
of the std_logic_vector type. This function returns a value of the same type.
The formal parameters: A, B and X are constants of the real type. The value returned
by this function is a result of calculating the A*X**2+B expression and it is also of
the real type.
Procedure
A procedure is a subprogram that defined as algorithm for computing values or exhibiting
behavior. Procedure call is a statement which encapsulates a collection of sequential
statements into a single statement. It may zero or more values .it may execute in zero or
more simulation time.
Procedure declarations can be nested__ Allows for recursive calls
Procedures can call other procedures
Procedure must be declared before use. It can be declared in any place where
declarations are allowed, however the place of declaration determines the scope
Cannot be used on right side of signal assignment expression since doesnt return
value
Procedure Syntax.
Page 128
Fundamentals of HDL
10EC45
Page 129
Fundamentals of HDL
10EC45
Procedure examples
Example 1
The above procedure declaration has two formal parameters: bi-directional X and Y
of real type.
Example 2
Dept. of ECE, SJBIT
Page 130
Fundamentals of HDL
10EC45
Procedure proc_1 has two formal parameters: the first one is a constant and it is in the
mode in and of the integer type, the second one is an output variable of the integer
type.
Procedure proc_2 has only one parameter, which is a bi-directional signal of type
std_logic.
Procedure Call
A procedure call is a sequential or concurrent statement, depending on where it is
used. A sequential procedure call is executed whenever control reaches it, while a
concurrent procedure call is activated whenever any of its parameters of in or inout
mode changes its value.
Tasks (Verilog)
Tasks are Verilog subprograms. They can be implemented to execute specified routines
repeatedly. The format in which the task is written can be divided into two parts: the
declaration and the body of the task. In the declaration, the name of the task is specified,
and the outputs and inputs of the task are listed. An example of task declaration is:
Page 131
Fundamentals of HDL
10EC45
task addr;
output cc,dd;
input aa,bb;
addr is the name(identifier) of the task. The outputs are cc and dd, and the inputs are aa
and bb, task is a predefined word. The body of the task shows the relationship between
the outputs and the inputs. An example of the body a task is:
begin
cc = aa ^ bb;
.
end
endtask
The body of the task cannot include always or initial. A task must be called within the
behavioral statement always or initial. An example of calling the task addr is as follows.
..
always @ ( a, b)
begin
addr(c,d,a,b);
end
addr is the name of the task,and inputs a and b are passed to aa and bb. The outputs of the
task cc and dd, after execution, are passed to c and d,respectively.
HDL Description of a Full Adder Using Procedure and TaskVHDL and Verilog
Page 132
Fundamentals of HDL
10EC45
Page 133
Fundamentals of HDL
10EC45
endmodule
HDL Description of an N-Bit Ripple Carry Adder Using Procedure and Task
VHDL and Verilog
VHDL N-Bit Ripple Carry Adder Using Procedure
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity adder_ripple is
generic (N : integer := 3);
port (x, y : in std_logic_vector (N downto 0);
cin : in std_logic;
sum : out std_logic_vector (N downto 0);
cout : out std_logic);
end adder_ripple;
architecture adder of adder_ripple is
procedure Faddr (sf, cof : out std_logic;
af, bf, cinf : in std_logic) is
--This procedure describes a full adder
begin
sf := af xor bf xor cinf;
cof := (af and bf) or (af and cinf) or (bf and cinf);
end Faddr;
begin
addrpl : process (x, y, cin)
variable c1, c2, tem1, tem2 : std_logic;
variable cint : std_logic_vector (N+1 downto 0);
variable sum1 : std_logic_vector (N downto 0);
begin
cint(0) := cin;
for i in 0 to N loop
Faddr (sum1(i), cint(i+1), x(i), y(i), cint(i));
--The above statement is a call to the procedure Faddr
end loop;
sum <= sum1;
cout <= cint(N+1);
end process;
end adder;
Verilog N-Bit Ripple Carry Adder Using Task
Dept. of ECE, SJBIT
Page 134
Fundamentals of HDL
10EC45
Page 135
Fundamentals of HDL
10EC45
entity Bin_Int is
generic (N : natural := 3);
port (X_bin : unsigned (N downto 0);
Y_int : out natural; Z : out std_logic);
--Y is always positive
end Bin_Int;
architecture convert of Bin_Int is
procedure bti (bin : in unsigned; int : out natural;
signal Z : out std_logic) is
-- the procedure bti is to change binary to integer
-- Flag Z is chosen to be a signal rather than a variable
-- Since the binary vector is always positive,
-- use type natural for the output of the procedure.
variable result : natural;
begin
result := 0;
for i in bin'Range loop
--binRange represents the range of the unsigned vector bin
--Range is a predefined attribute
if bin(i) = '1' then
result := result + 2**i;
end if;
end loop;
int := result;
if (result = 0) then
Z <= '1';
else
Z <= '0';
end if;
end bti;
begin
process (X_bin)
variable tem : natural;
begin
bti (X_bin, tem, Z);
Y_int <= tem;
end process;
end convert;
Verilog: Converting an Unsigned Binary to an Integer Using Task
Dept. of ECE, SJBIT
Page 136
Fundamentals of HDL
10EC45
Page 137
Fundamentals of HDL
10EC45
Page 138
Fundamentals of HDL
10EC45
always @ (X_bin)
begin
binfloat (X_bin, Z);
end
task binfloat;
parameter P = N;
input [0:P] a;
output real float;
integer i;
begin
float = 0.0;
for (i = 0; i <= P; i = i + 1)
begin
if (a[i] == 1)
float = float + 1.0 / 2**(i+1);
//The above statement multiplies each bit by its weight
end
end
endtask
endmodule
Page 139
Fundamentals of HDL
10EC45
Page 140
Fundamentals of HDL
10EC45
***Begin Verilog***
Verilog: Converting an Unsigned Integer to Binary Using Task
module Int_Bin (X_bin, flag_even, Y_int );
/*In general Verilog, in contrast to VHDL, does not
strictly differentiate between integers and binaries;
for example, if bin is declared as a binary of width 4,
bin = bin/2 can be written, and the Verilog, but not
VHDL, performs this division as if bin is integer.
In the following, the corresponding VHDL program in
Listing 6.5a is just translated to practice with
the command task */
parameter N = 3;
output [N:0] X_bin;
output flag_even;
input [N:0] Y_int;
reg [N:0] X_bin;
reg flag_even;
always @ (Y_int)
begin
itb (Y_int, N, X_bin, flag_even);
end
task itb;
parameter P = N;
input integer int;
input N;
output [P:0] bin;
output flag;
integer j;
begin
if (int %2 == 0)
//The above statement checks int to see if it is even.
flag = 1'b1;
else
flag = 1'b0;
for (j = 0; j <= P; j = j + 1)
begin
if (int %2 == 1)
bin[j] = 1;
Dept. of ECE, SJBIT
Page 141
Fundamentals of HDL
10EC45
else
bin[j] = 0;
int = int/2;
end
end
endtask
endmodule
Page 142
Fundamentals of HDL
10EC45
Page 143
Fundamentals of HDL
10EC45
write bin = bin/2, and the Verilog (but not VHDL) will
perform this division. In the following, just translate
the corresponding VHDL counterpart program. */
parameter N = 3;
input signed [N:0] X_bin;
output integer Y_int;
output even_parity;
reg even_parity;
always @ (X_bin)
begin
sbti (Y_int, even_parity, N, X_bin);
end
task sbti;
parameter P = N;
output integer int;
output even;
input N;
input [P:0] bin;
integer i;
reg parity;
begin
int = 0;
parity = 0;
//change binary to integer
for (i = 0; i <= P; i = i + 1)
begin
if (bin[i] == 1)
begin
int = int + 2**i;
parity = parity + 1;
end
end
if ((parity % 2) == 1)
even = 0;
else
even = 1;
if (bin [P] == 1)
int = int - 2**(P+1);
Dept. of ECE, SJBIT
Page 144
Fundamentals of HDL
10EC45
end
endtask
endmodule
Page 145
Fundamentals of HDL
10EC45
else
temp_int := int;
end if;
for i in 0 to M loop
if (temp_int MOD 2 = 1) then
bin (i) := '1';
else
bin (i) := '0';
end if;
--integer division by 2
temp_int := temp_int/2;
end loop;
if (flag = '1') then
sbin := - bin;
else sbin := bin;
end if;
end sitb;
begin
process (Y_int)
variable tem : signed (N downto 0);
begin
sitb(tem, N, Y_int);
X_bin <= tem;
end process;
end convert;
HDL Code for Signed Vector Multiplication Using Procedure and TaskVHDL
and Verilog
VHDL: Signed Vector Multiplication Using Procedure
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.numeric_std.all;
entity Vector_Booth is
generic (N : integer := 3);
port (a0, a1, a2, b0, b1, b2 : in signed (N downto 0);
d : out signed (3*N downto 0));
end Vector_Booth;
architecture multiply of Vector_Booth is
procedure booth (X, Y : in signed (3 downto 0);
Dept. of ECE, SJBIT
Page 146
Fundamentals of HDL
10EC45
Page 147
Fundamentals of HDL
10EC45
Page 148
Fundamentals of HDL
10EC45
begin
process (a0, b0, a1, b1, a2, b2)
variable tem0, tem1, tem2 : signed ((2*N + 1) downto 0);
variable d_temp : signed (3*N downto 0);
variable temi0, temi1, temi2, temtotal : integer;
begin
--Find the partial products a0b0, a1b1, a2b2
booth (a0, b0, tem0);
booth (a1, b1, tem1);
booth (a2, b2, tem2);
-- Change the partial products to integers
sbti (tem0, (2*N+1), temi0);
sbti (tem1, (2*N+1), temi1);
sbti (tem2, (2*N+1), temi2);
-- Find the total integer sum of partial products
temtotal := temi0 + temi1 + temi2;
-- Change the integer to binary
sitb (d_temp, 3*N, temtotal);
d <= d_temp;
end process;
end multiply;
Verilog: Signed Vector Multiplication Using Task
module Vector_Booth (a0, a1, a2, b0, b1, b2, d);
parameter N = 3;
input signed [N:0] a0, a1, a2, b0, b1, b2;
output signed [3*N : 0] d;
reg signed [2*N+1 : 0] tem0, tem1, tem2;
reg signed [3*N : 0] d;
always @ (a0, b0, a1, b1, a2, b2)
begin
booth (a0, b0, tem0);
//booth is a task to multiply a0 x b0 = tem0
booth (a1, b1, tem1);
booth (a2, b2, tem2);
d = tem0 + tem1 + tem2;
end
Page 149
Fundamentals of HDL
10EC45
task booth;
input signed [3:0] X, Y;
output signed [7:0] Z;
reg signed [7:0] Z;
reg [1:0] temp;
integer i;
reg E1;
reg [3:0] Y1;
begin
Z = 8'd0;
E1 = 1'd0;
for (i = 0; i < 4; i = i + 1)
begin
temp = {X[i], E1}; //This is catenation
Y1 = -Y; //Y1 is the 2' complement of Y
case (temp)
2'd2 : Z [7:4] = Z [7:4] + Y1;
2'd1 : Z [7:4] = Z [7:4] + Y;
default : begin end
endcase
Z = Z >> 1; /*This is a logical shift of one position to
the right*/
Z[7] = Z[6];
/*The above two statements perform arithmetic shift
where the sign of the number is preserved after
the shift.*/
E1 = X[i];
end
if (Y == 4'b1000) Z = -Z;
/* If Y = 1000, then Y1 = 1000 (should be 8 not -8).
This error is because Y1 is 4 bits only.
The statement Z = -Z adjusts the value of Z. */
end
endtask
endmodule
Page 150
Fundamentals of HDL
10EC45
Page 151
Fundamentals of HDL
10EC45
Figure 6.8 Relationship between the substrate concentration S and rate of reaction V.
HDL Description for Enzyme Activity Using Procedure and TaskVHDL and
Verilog
VHDL: Enzyme Activity Using Procedure
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use ieee.std_logic_arith.all;
-- A description of enzyme-substrate binding mechanism.
-- S= (Vmax *S)/(S + M) where S is the substrate concentration,
-- M is the dissociation constant, and Vmax is the maximum
-- rate of reaction. In this example, Vmax = 1.
-- The inputs are S and M in binary (integer); the output v
-- is in Q4 format. This means that v is always less than
-- one, with the binary point placed to the left of the
-- most significant bit. For example, if v = 1010, the decimal
-- equivalent is .5 + .125 = 0.625.
-- To calculate v, convert S and M to unsigned integers,
-- find the real value of (S/(S + M)), convert this real
-- value to Q4 by multiplying it with 2**4 = 16, and
-- convert the integer to binary.
entity enzyme_beh is
port (S : in std_logic_vector (3 downto 0);
v : out std_logic_vector (3 downto 0);
M : in std_logic_vector(3 downto 0));
end enzyme_beh;
Dept. of ECE, SJBIT
Page 152
Fundamentals of HDL
10EC45
Page 153
Fundamentals of HDL
10EC45
end rti;
begin
P1 : process(S, M)
Variable S1, M1, v1 : integer;
Variable vr, vq4, vmax : real;
variable tem : std_logic_vector (3 downto 0);
begin
bti (S, s1); bti (M, m1);
vmax := 1.0;
vr := vmax*(1.0 *S1) / (S1 * 1.0 + M1 * 1.0);
vq4 := vr * 2**4;
rti (vq4, v1);
itb (v1, tem);
v <= tem;
end process P1;
end enzyme;
Verilog: Enzyme Activity Using Task
/* A description of enzyme-substrate binding mechanism.
S= (Vmax *S)/(S + M), where S is the substrate
concentration, M is the dissociation constant,
and Vmax is the maximum rate of reaction. In this example,
Vmax =1. The inputs are S and M in binary (integer);
the output v is in Q4 format. This means that v is always
less than 1, with the binary point placed to the left of
the most significant bit. For example, if v = 1010, the
decimal equivalent is .5 + .125 = 0.625. To calculate V,
find the real value of (S /(S + M)), convert this real
value to Q4 by multiplying it with 2**4 =16, and convert
the integer to binary. */
module enzyme_beh (S, M, V);
input [3:0] S, M;
output [3:0] V;
integer vmax;
reg [3:0] V;
real vr;
always @ (S, M)
begin
vmax = 1;
vr = vmax * (1.0 * S) / (S * 1.0 + M * 1.0);
vr = vr * 2**4;
rti (vr, V);
end
Page 154
Fundamentals of HDL
10EC45
task rti;
/* This task can be replaced by just one statement, v1= r.
Verilog, in contrast to VHDL, can handle different
types of the assignment statement. Verilog finds the
equivalent integer value v1 for the real r. The task has
been designed here only to match the VHDL procedure rti. */
input real r;
output [3:0] v1;
real temp;
begin
temp = r;
v1 = 4'b0000;
while (temp >= 0.5)
begin
v1 = v1 + 1;
temp = r - 1.0 * v1;
end
end
endtask
endmodule
Page 155
Fundamentals of HDL
10EC45
Page 156
Fundamentals of HDL
10EC45
end
endfunction
endmodule
HDL Function to Find the Greater of Two Signed NumbersVHDL and Verilog
VHDL Function to Find the Greater of Two Signed Numbers
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.numeric_std.all;
entity greater_2 is
port (x, y :in signed (3 downto 0); z :out signed (3 downto 0));
end greater_2;
architecture greater_2 of greater_2 is
function grt (a, b : signed (3 downto 0)) return signed is
-- The above statement declares a function by the name grt.
-- The inputs are 4-bit signed numbers.
variable temp : signed (3 downto 0);
begin
if (a >= b) then
temp := a;
else
temp := b;
end if;
return temp;
end grt;
begin
process (x, y)
begin
z <= grt (x, y); --This is a function call.
end process;
end greater_2;
Verilog Function to Find the Greater of Two Signed Numbers
module greater_2 (x, y, z);
input signed [3:0] x;
input signed [3:0] y;
output signed [3:0] z;
reg signed [3:0] z;
always @ (x, y)
Dept. of ECE, SJBIT
Page 157
Fundamentals of HDL
10EC45
begin
z = grt (x, y); //This is a function call.
end
function [3:0] grt;
/*The above statement declares a function by the name grt;
grt is also the output of the function*/
input signed [3:0] a, b;
/*The above statement declares two inputs to the function;
both are 4-bit signed numbers.*/
begin
if (a >= b)
grt = a;
else
grt = b;
end
endfunction
endmodule
HDL Code for y = 0 < x < 1VHDL and Verilog
VHDL Floating Sum Description
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity segma is
port (x : in std_logic_vector (0 to 3);
y: out std_logic_vector (15 downto 0));
end segma;
architecture segm_beh of segma is
procedure flt(a :in std_logic_vector(0 to 3); float: out real) is
--This function converts binary (fraction) to real
variable tem : real;
begin
tem := 0.0;
Dept. of ECE, SJBIT
Page 158
Fundamentals of HDL
10EC45
for i in 0 to 3 loop
if (a(i) = '1') then
tem := tem + 1.0 / (1.0 * 2**(i+1));
end if;
end loop;
float := tem;
end flt;
Procedure rti (r : in real; int : out integer) is
--This procedure converts real to integer
variable temp : real;
variable intg : integer := 0;
begin
temp := r;
while temp >= 0.5 loop
intg := intg + 1;
temp := r - 1.0 * intg;
end loop;
int := intg;
end rti;
procedure itb (bin : out std_logic_vector;
N : in integer; int : in integer) is
variable temp_int : integer := int;
begin
for i in 0 to N loop
if (temp_int MOD 2 = 1) then
bin(i) := '1';
else bin(i) := '0';
end if;
temp_int := temp_int/2;
end loop;
end itb;
function exp (a : in std_logic_vector (0 to 3))
return std_logic_vector is
variable z1 : real;
variable intgr : integer;
Dept. of ECE, SJBIT
Page 159
Fundamentals of HDL
10EC45
begin
sg1 : process (x)
variable tem : std_logic_vector (15 downto 0);
begin
tem := exp(x);
y <= tem;
end process sg1;
end segm_beh;
Verilog Floating Sum Description
module segma (x, y);
input [0:3] x;
// x is a fraction in Q4 format, 0 < x < 1.
output [15:0] y;
reg [15:0] y;
always @ (x)
begin
y = exp (x);
end
function real float;
//This function is to calculate the real value
//of a fraction in binary
input [0:3] a;
integer i;
begin
Dept. of ECE, SJBIT
Page 160
Fundamentals of HDL
10EC45
float = 0.0;
for (i = 0; i <= 3; i = i + 1)
begin
if (a[i] == 1)
float = float + 1.0 / 2**(i+1);
end
end
endfunction
function [15:0] rti;
input real r;
begin
rti = r;
end
endfunction
function [15:0] exp;
input [0:3] a;
real z1;
begin
z1 = float (a); //A call to function "float"
z1 = 1.0 - z1 + z1**2 - z1**3;
z1 = z1 * 2**16;
exp = rti(z1);
end
endfunction
endmodule
Page 161
Fundamentals of HDL
10EC45
Page 162
Fundamentals of HDL
10EC45
--Read the first line of the file and store the line in temp
readline (infile, temp);
-- temp now has the data: 12 -3 5
-- Read the first integer (12) from the line temp and store it
--in the integer variable count.
read (temp, count);
--count has the value of 12. Multiply by 2 and store in z
z <= 2 * count;
-- Read the second integer from the line temp and
-- store it in count
read (temp, count);
--count now has the value of -3
--Multiply by 5 and store in z1
z1 <= 5 * count;
-- read the third integer in line temp and store it in count
read (temp, count);
--Multiply by 3 and store in z2
z2 <= 3 * count;
--Read the second line and store it in temp
readline (infile, temp);
--temp has only the second line
--Read the first integer of the second line and store it in count
read (temp, count);
--Multiply by 4 and store in z3
z3 <= 4 * count;
--Close the infile
file_close (infile);
end process;
end FILE_BEHAVIOR;
Page 163
Fundamentals of HDL
10EC45
Page 164
Fundamentals of HDL
10EC45
Page 165
Fundamentals of HDL
10EC45
Page 166
Fundamentals of HDL
10EC45
Page 167
Fundamentals of HDL
10EC45
Page 168
Fundamentals of HDL
10EC45
Page 169
Fundamentals of HDL
10EC45
Page 170
Fundamentals of HDL
10EC45
use IEEE.STD_LOGIC_1164.all;
package array_pkg is
constant N : integer := 4;
--N+1 is the number of elements in the array.
subtype wordChr is character;
type string_chr is array (N downto 0) of wordChr;
end array_pkg;
--Start writing the code to find the assigned integer value
library ieee;
use ieee.std_logic_1164.all;
use std.textio.all;
use work.array_pkg.all;
entity OPCODES is
port (assmbly_code : in string_chr; z : out string_chr;
z1 : out integer);
end OPCODES;
architecture BEHAVIOR of OPCODES is
begin
process (assmbly_code)
file infile : text;
variable fstatus : file_open_status;
variable temp : string_chr := (' ', ' ', ' ', ' ', ' ');
variable tem_bin : integer;
variable regstr : line;
begin
file_open (fstatus, infile, "cods.txt", read_mode);
for i in 0 to 8 loop
- while loop could have been used instead of for loop. See
-- Exercise 8.3
readline (infile, regstr);
read (regstr, temp);
Dept. of ECE, SJBIT
Page 171
Fundamentals of HDL
10EC45
Page 172
Fundamentals of HDL
10EC45
Page 173
Fundamentals of HDL
10EC45
Page 174
Fundamentals of HDL
10EC45
-- are stored.
write (regstw, "Location
writeline (outfile, regstw);
Code Address");
for i in 0 to 11 loop
--while-loop could have been used instead of for-loop.
readline (infile, regstr);
read (regstr, temp);
if (temp = ('O', 'R', 'I', 'G', ' ')) then
read (regstr, ctr);
elsif (ctr = -1)then
-- If the code of the first line in the file is not ORIG
-- report an error
write (regstw, " ERROR: FIRST OPCODE SHOULD BE ORIG");
writeline (outfile, regstw);
exit;
else
read (regstr, addr);
write (regstw, ctr);
write (regstw, "
ctr := ctr + 1;
");
case temp is
when ('H', 'A', 'L', 'T', ' ') =>
code := 0;
write (regstw, code);
write (regstw, " ");
write (regstw, addr);
writeline (outfile, regstw);
Page 175
Fundamentals of HDL
10EC45
Page 176
Fundamentals of HDL
10EC45
Page 177
Fundamentals of HDL
10EC45
b = 2 * a;
end
initial
begin
ch1 = $fopen("file4.txt");
$fdisplay (ch1, "\n\t\t\t This is file4.txt \n");
$fdisplay (ch1, " Input a in Decimal\t\tOutput b in
Decimal\t\tOutput b in Binary\n ");
/*The above statement when entered in the Verilog module should
be entered in one line without carriage return */
$fmonitor (ch1,"\t%d\t\t\t%d\t\t\t%b \n", a,b, b);
end
endmodule
VHDL RECORD TYPE
VHDL Code for an Example of Record
The following is the code of the package weather_fcst
package weather_fcst is
Type cast is (rain, sunny, snow, cloudy);
Type weekdays is (Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday, Sunday);
Type forecast is
Record
Tempr : real range -100.0 to 100.0;
unit : string (1 to 3);
Day : weekdays;
Cond : cast;
end record;
end package weather_fcst;
-- Now we write the program
library ieee;
use ieee.std_logic_1164.all;
use std.textio.all;
use work.weather_fcst.all;
Page 178
Fundamentals of HDL
10EC45
entity WEATHER_FRCST is
port (Day_in : in weekdays; unit_in : in string (1 to 3);
out_temperature : out real;
out_unit : out string (1 to 3);
out_day : out weekdays; out_cond : out cast);
-- Type string is a predefined
end WEATHER_FRCST;
--Now we write the code
architecture behavoir_record of WEATHER_FRCST is
begin
process (Day_in, unit_in)
variable temp : forecast ;
begin
case Day_in is
when Monday =>
temp.cond := sunny;
if (unit_in = "CEN") then
temp.tempr := 35.6;
elsif (unit_in = "FEH") then
temp.tempr := 1.2 * 35.6 + 32.0;
else
report ("invalid units");
end if;
when Tuesday =>
temp.cond := rain;
if (unit_in = "CEN") then
temp.tempr := 30.2;
elsif (unit_in = "FEH") then
temp.tempr := 1.2 * 30.2 + 32.0;
else
report ("invalid units");
end if;
when Wednesday =>
temp.cond := sunny;
if (unit_in = "CEN") then
temp.tempr := 37.2;
Dept. of ECE, SJBIT
Page 179
Fundamentals of HDL
10EC45
Page 180
Fundamentals of HDL
10EC45
Page 181
Fundamentals of HDL
10EC45
ASSIGNMENT QUESTIONS
1)
2)
3)
4)
5)
Page 182
Fundamentals of HDL
10EC45
Syllabus of unit 6:
Hours :6
Why Mixed-Type Description? VHDL User-Defined Types, VHDL Packages, MixedType Description examples
Recommended readings:
1.
VHDL
Page 183
Fundamentals of HDL
10EC45
Page 184
Fundamentals of HDL
10EC45
Page 185
Fundamentals of HDL
10EC45
Package declaration
The Packages declaration is used to specify the external view of the items. The syntax
rule for the package declaration is as follows.
The identifier provides the name of the package. This name can be used any
where in the model to identify the model
The package declarations includes a collection of declaration such as
_ Type
_ Subtypes
_ Constants
_ Signal
_ Subprogram declarations etc
_ Aliases
_ components
The above declarations are available for the user of the packages.
The following are the advantages of the usage of packages
.
All the declarations are available to all models that use a package.
Many models can share these declarations. Thus, avoiding the need to rewrite
these declarations for every model.
The following are the points to be remembered on the package declaration
A package is a separate form of design unit, along with entity and architecture
bodies.
It is separately analyzed and placed in their working library.
Any model can access the items declared in the package by referring the name
of the declared item.
Package declaration syntax
Page 186
Fundamentals of HDL
10EC45
logically static.
Subprograms in the package declaration
Procedures and functions can be declared in the package declaration
Only the design model that uses the package can access the subprograms
declared in the package declarations.
The subprogram declaration includes only the information contained in the
header.
This does not specify the body of the subprogram. The package declaration
provides information regarding the external view of the subprogram without
the implementation details. This is called information hiding.
For every subprogram declaration there must be subprogram body in the
package body. The subprograms present in the package body but not declared
in the package declaration cant be accessed by the design models.
Package body
Each package declaration that includes a subprogram or a deferred constant must
have package body to fill the missing information. But the package body is not
required when the package declaration contains only type, subtype, signal or fully
specified constants. It may contain additional declarations which are local to the
package body but cannot declare signals in body. Only one package body per package
declaration is allowed.
Point to remember:
The package body starts with the key word package body
The identifier of the package body follows the keyword
The items declared in the package body must include full declarations of all
subprograms declared in the corresponding package declarations. These full
declarations must include subprogram headers as it appears in the package
declarations. This means that the names, modes typed and the default values
of each parameters must be repeated in exactly the same manner. In this
regard two variations are allowed:
_ A numerical literal may be written differently for example; in a
different base provided it has the same value.
_ A simple name consisting just of an identifier can be replaced by a
selected name, provided it refers to the same item.
A deferred constant declared in the package declaration must have its value
specified in the package body by declaration in the package body
A package body may include additional types, subtypes, constants and
subprograms. These items are included to implement the subprogram defined
in the package declaration. The items declared in the package declaration
Dept. of ECE, SJBIT
Page 187
Fundamentals of HDL
10EC45
Page 188
Fundamentals of HDL
10EC45
end addfour;
HDL Code for Finding the Greatest Element of an ArrayVHDL and Verilog
VHDL: Finding the Greatest Element of an Array
library IEEE;
use IEEE.STD_LOGIC_1164.all;
--Build a package for an array
package array_pkg is
constant N : integer := 4;
--N+1 is the number of elements in the array.
constant M : integer := 3;
--M+1 is the number of bits of each element
--of the array.
subtype wordN is std_logic_vector (M downto 0);
type strng is array (N downto 0) of wordN;
end array_pkg;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use work.array_pkg.all;
-- The above statement makes the package array_pkg visible in
-- this module.
entity array1 is
generic (N : integer :=4; M : integer := 3);
--N + 1 is the number of elements in the array; M = 1 is the
--number of bits of each element.
Port (a : inout strng; z : out std_logic_vector (M downto 0));
end array1;
Dept. of ECE, SJBIT
Page 189
Fundamentals of HDL
10EC45
Page 190
Fundamentals of HDL
10EC45
Page 191
Fundamentals of HDL
10EC45
else
$display (" grtst is greater than a");
// use the above statement to monitor the program
end
end
endmodule
Multiplication of Two Signed N-Element VectorsVHDL and Verilog
VHDL: Multiplication of Two Signed N-Element Vectors
library IEEE;
use IEEE.STD_LOGIC_1164.all;
use ieee.numeric_std.all;
package booth_pkg is
constant N : integer := 4;
--N + 1 is the number of elements in the array.
constant M: integer := 3;
--M + 1 is the number of bits of each element
--of the array.
subtype wordN is signed (M downto 0);
type strng is array (N downto 0) of wordN;
Page 192
Fundamentals of HDL
10EC45
Page 193
Fundamentals of HDL
10EC45
Page 194
Fundamentals of HDL
10EC45
for i in 0 to 4 loop
booth(a(i), b(i), temp);
--accumulate the partial products in the product temp5
temp5 := temp5 + temp;
end loop;
d <= temp5;
end process;
end multply;
Verilog: Multiplication of Two Signed N-Element Vectors
module vecor_multply (start, d);
parameter N = 4;
parameter M = 3;
input start;
output signed [3*N:0] d;
reg signed [M:0] a[0:N];
reg signed [M:0] b[0:N];
reg signed [3*N:0] d;
reg signed [3*N:0] temp;
integer i;
always @ (start)
begin
a[0] = 4'b1100;
a[1] = 4'b0000;
a[2] = 4'b1001;
a[3] = 4'b0011;
a[4] = 4'b1111;
b[0] = 4'b1010;
b[1] = 4'b0011;
b[2] = 4'b0111;
Dept. of ECE, SJBIT
Page 195
Fundamentals of HDL
10EC45
b[3] = 4'b1000;
b[4] = 4'b1000;
d = 0;
for (i = 0; i <= N; i = i + 1)
begin
booth (a[i], b[i], temp);
d = d + temp;
end
end
task booth;
input signed [3:0] X, Y;
output signed [7:0] Z;
reg signed [7:0] Z;
reg [1:0] temp;
integer i;
reg E1;
reg [3:0] Y1;
begin
Z = 8'd0;
E1 = 1'd0;
for (i = 0; i < 4; i = i + 1)
begin
temp = {X[i], E1}; //This is catenation
Y1 = -Y; //Y1 is the 2'complement of Y
case (temp)
2'd2 : Z[7:4] = Z[7:4] + Y1;
2'd1 : Z[7:4] = Z[7:4] + Y;
default : begin end
Page 196
Fundamentals of HDL
10EC45
endcase
Z = Z >> 1;
/*The above statement is a logical shift of
one position to the right*/
Z[7] = Z[6];
/*The above two statements perform arithmetic shift where
the sign of the number is preserved after the shift. */
E1 = X[i];
end
if (Y == 4'b1000)
/* If Y = 1000, then Y1 = 1000 (should be 8 not -8).
This error is because Y1 is 4 bits only.
The statement Z = -Z adjusts the value of Z. */
Z = -Z;
end
endtask
endmodule
VHDL Two-Dimensional Array
library IEEE;
use IEEE.STD_LOGIC_1164.all;
--Build a package to declare the array
package twodm_array is
constant N : integer := 4;
Page 197
Fundamentals of HDL
10EC45
end twodm_array;
Page 198
Fundamentals of HDL
10EC45
end twodm_array;
--Second, write the code for addition
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use work.twodm_array.all;
entity matrices is
Port (x, y : strng2; z : out strng2);
--strng2 type is 5x5 matrix
end matrices;
architecture sum of matrices is
Page 199
Fundamentals of HDL
10EC45
begin
com : process (x, y)
variable t : integer := 0;
begin
for i in 0 to 4 loop
for j in 0 to 4 loop
t := x(i)(j) + y(i)(j);
z(i)(j) <= t;
end loop;
end loop;
end process com;
end sum;
MIXED TYPE DESCRIPTION EXAMPLES
HDL Description of an ALUVHDL and Verilog
Page 200
Fundamentals of HDL
10EC45
Page 201
Fundamentals of HDL
10EC45
Page 202
Fundamentals of HDL
10EC45
Page 203
Fundamentals of HDL
10EC45
Page 204
Fundamentals of HDL
10EC45
reg [5:0] z;
wire [5:0] temp1;
wire [2:0] g, p;
wire c0, c1;
// The following is data-flow description
// for 3-bit lookahead adder
assign g[0] = a[0] & b[0];
assign g[1] = a[1] & b[1];
assign g[2] = a[2] & b[2];
assign p[0] = a[0] | b[0];
assign p[1] = a[1] | b[1];
assign p[2] = a[2] | b[2];
assign c0 = g[0] | (p[0] & cin);
assign c1 = g[1] | (p[1] & g[0]) | (p[1] & p[0] & cin);
assign temp1[3] = g[2] | (p[2] & g[1]) | (p[2] & p[1]
& g[0]) | (p[2] & p[1] & p[0] & cin);
// temp1[3] is the final carryout of the adders
assign temp1[0] = (p[0] ^ g[0]) ^ cin;
assign temp1[1] = (p[1] ^ g[1]) ^ c0;
assign temp1[2] = (p[2] ^ g[2]) ^ c1;
assign temp1[5:4] = 2'b00;
//The following is behavioral description
always @ (a, b, cin, opc, temp1)
begin
case (opc)
mul : z = a * b;
add : z = temp1;
Dept. of ECE, SJBIT
Page 205
Fundamentals of HDL
10EC45
divide : z = a / b;
nop : z = z;
endcase
end
endmodule
Page 206
Fundamentals of HDL
10EC45
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.numeric_std.all;
use work.array_pkg.all;
entity memory16x8 is
generic (N : integer := 15; M : integer := 7);
--N+1 is the number of words in the memory; M+1 is the
--number of bits of each word.
Port (Memory : inout strng; CS : in std_logic;
ABUS : in unsigned (3 downto 0);
Data_in : in std_logic_vector (7 downto 0);
R_WRbar : in std_logic;
Dept. of ECE, SJBIT
Page 207
Fundamentals of HDL
10EC45
Page 208
Fundamentals of HDL
10EC45
endmodule
Page 209
Fundamentals of HDL
10EC45
Page 210
Fundamentals of HDL
10EC45
Page 211
Fundamentals of HDL
10EC45
Z <= '0';
else
present := state3;
Z <= '0';
end if;
when state2 =>
if A ='1' then
present := state3;
Z <= '1';
else
present := state0;
Z <= '0';
end if;
when state3 =>
if A ='1' then
present := state0;
Z <= '0';
else
present := state2;
Z <= '0';
end if;
end case;
pres_st <= present;
end if;
end process FM;
end st_behavioral;
Verilog State Machine Description
`define state0 2'b00
`define state1 2'b01
`define state2 2'b10
`define state3 2'b11
Dept. of ECE, SJBIT
Page 212
Fundamentals of HDL
10EC45
Page 213
Fundamentals of HDL
10EC45
`state1 :
begin
if (A == 1)
begin
present = `state2;
Z = 1'b0;
end
else
begin
present = `state3;
Z = 1'b0;
end
end
`state2 :
begin
if (A == 1)
begin
present = `state3;
Z = 1'b1;
end
else
begin
present = `state0;
Z = 1'b0;
end
end
`state3 :
begin
if (A == 1)
Dept. of ECE, SJBIT
Page 214
Fundamentals of HDL
10EC45
begin
present = `state0;
Z = 1'b0;
end
else
begin
present = `state2;
Z = 1'b0;
end
end
endcase
pres_st = present;
end
endmodule
Page 215
Fundamentals of HDL
10EC45
Page 216
Fundamentals of HDL
10EC45
Page 217
Fundamentals of HDL
10EC45
Page 218
Fundamentals of HDL
10EC45
Page 219
Fundamentals of HDL
10EC45
Page 220
Fundamentals of HDL
10EC45
Page 221
Fundamentals of HDL
10EC45
Page 222
Fundamentals of HDL
10EC45
Page 223
Fundamentals of HDL
10EC45
//The above statement can be written using the reduction XOR as:
//assign z = ^ ACL[6:0];
assign clk = clk_master & ON_OFF;
always @ (Reset, PC, ACL, ACH, posedge(clk), z, pres_st)
begin
if (Reset == 1'b1)
begin
pres_st = state0;
Reset = 1'b0;
PC = 4'd0;
Memory [0] = 8'hE0; Memory [1] = 8'h29;
Memory [2] = 8'h8A; Memory [3] = 8'h4B;
Memory [4] = 8'h6C; Memory [5] = 8'h8D;
Memory [6] = 8'hCE; Memory [7] = 8'hA0;
Memory [8] = 8'h00; Memory [9] = 8'h0C;
Memory [10] = 8'h05; Memory [11] = 8'h04;
Memory [12] = 8'h09; Memory [13] = 8'h03;
Memory [14] = 8'h09;
Memory [15] = 8'h07;
end
case (pres_st)
state0 :
begin
next_st = state1;
AR = PC;
end
Dept. of ECE, SJBIT
Page 224
Fundamentals of HDL
10EC45
state1 :
//This is fetch cycle
begin
next_st = state2;
DR = Memory [AR];
end
state2 :
//This is fetch cycle
begin
next_st = state3;
PC = PC + 1;
IR = DR [7:5];
AR = DR [3:0];
end
state3 :
//This is execute cycle
begin
case (IR)
3'd7 :
//The op code is CLA
begin
ACL = 8'd0;
next_st = state0;
end
3'd1 :
//The op code is ADD
begin
DR = Memory [AR];
ACL = ACL + DR;
next_st = state0;
Dept. of ECE, SJBIT
Page 225
Fundamentals of HDL
10EC45
end
3'd2 :
//The op code is MULT
begin
DR = Memory [AR];
PR = ACL * DR;
ACL = PR [7:0];
ACH = PR [15:8];
next_st = state0;
end
3'd3 :
//The op code is DIVID
begin
DR = Memory [AR];
ACL = ACL / DR;
next_st = state0;
end
3'd4 :
//The op code is XOR
begin
DR = Memory [AR];
ACL = ACL ^ DR;
next_st = state0;
end
3'd6 :
//The op code is NAND
begin
DR = Memory [AR];
ACL = ~(ACL & DR);
next_st = state0;
Dept. of ECE, SJBIT
Page 226
Fundamentals of HDL
10EC45
end
3'd5 :
//The op code is PRITY
begin
ACL[7] = z;
next_st = state0;
end
3'd0 :
//The op code is HALT
begin
next_st = state3;
end
default :
begin
end
endcase
end
default :
begin
end
endcase
pres_st = next_st;
end
endmodule
Dept. of ECE, SJBIT
Page 227
Fundamentals of HDL
10EC45
Page 228
Fundamentals of HDL
10EC45
ASSIGNMENT QUESTIONS
Page 229
Fundamentals of HDL
10EC45
Syllabus of unit 7:
Hours :7
Recommended readings:
1.
VHDL
Page 230
Fundamentals of HDL
10EC45
Page 231
Fundamentals of HDL
10EC45
endmodule
library ieee;
use ieee.std_logic_1164.all;
entity VHD_enty is
port( x, y : in std_logic;
o1,o2 : out std_logic;
end VHD_enty;
architecture VHD_enty of VHD_enty is
begin
end VHD_enty;
How to Invoke a Verilog Module from a VHDL Module
In the VHDL module, we declare a component with the same name as the Verilog
module we want to invoke the name and port modes of the component should be identical
to the name and input/output modes of the Verilog module.
Invoking a Verilog Module from a VHDL Module
library ieee;
use ieee.std_logic_1164.all;
entity Ver_VHD is
port( a,b : in std_logic;
c : out std_logic;
end Ver_VHD;
architecture Ver_VHD of Ver_VHD is
component V_mod1
port(x,y: in std_logic;
z : out std_logic);
end component;
end Ver_VHD;
module V-mod1(x,y,z);
input x,y;
output z;
endmodule
MIXED LANGUAGE DESCRIPTION EXAMPLES
Invoking a VHDL Entity from a Verilog Module
VHDL entity is invoked in a Verilog module by instantiating the Verilog module with a
name that is identical to the entitys name.
Mixed-Language Description of a Full Adder
Page 232
Fundamentals of HDL
10EC45
Page 233
Fundamentals of HDL
10EC45
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
-- This is a VHDL data-flow code for a 3-bit carry-lookahead adder
entity adders_RL is
port (x, y : in std_logic_vector (2 downto 0);
cin : in std_logic;
sum : out std_logic_vector (2 downto 0);
cout : out std_logic);
--The entity name is identical to that of the Verilog module.
--The input and output ports have the same mode as the inputs
--and outputs of the Verilog module.
Dept. of ECE, SJBIT
Page 234
Fundamentals of HDL
10EC45
end adders_RL;
architecture lkh_DtFl of adders_RL is
signal c0, c1 : std_logic;
signal p, g : std_logic_vector (2 downto 0);
constant delay_gt : time := 0 ns;
--The gate propagation delay here is equal to 0.
begin
g(0) <= x(0) and y(0) after delay_gt;
g(1) <= x(1) and y(1) after delay_gt;
g(2) <= x(2) and y(2) after delay_gt;
p(0) <= x(0) or y(0) after delay_gt;
p(1) <= x(1) or y(1) after delay_gt;
p(2) <= x(2) or y(2) after delay_gt;
c0 <= g(0) or (p(0) and cin) after 2 * delay_gt;
c1 <= g(1) or (p(1) and g(0)) or (p(1) and
p(0) and cin) after 2 * delay_gt;
cout <= g(2) or (p(2) and g(1)) or (p(2) and p(1) and g(0)) or
(p(2) and p(1) and p(0) and cin) after 2 * delay_gt;
sum(0) <= (p(0) xor g(0)) xor cin after delay_gt;
sum(1) <= (p(1) xor g(1)) xor c0 after delay_gt;
sum(2) <= (p(2) xor g(2)) xor c1 after delay_gt;
end lkh_DtFl;
Page 235
Fundamentals of HDL
10EC45
Page 236
Fundamentals of HDL
10EC45
Page 237
Fundamentals of HDL
10EC45
Page 238
Fundamentals of HDL
10EC45
Page 239
Fundamentals of HDL
10EC45
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity D_Latch is
--The entity has the same name as the calling Verilog module
port (D, E : in std_logic;
Q, Qbar : buffer std_logic);
end D_Latch;
architecture DL_DtFl of D_Latch is
--This architecture describes a D-latch using
--data-flow description
constant Delay_EorD : Time := 9 ns;
constant Delay_inv : Time := 1 ns;
begin
Qbar <= (D and E) nor (not E and Q) after Delay_EorD;
Q <= not Qbar after Delay_inv;
end DL_DtFl;
Page 240
Fundamentals of HDL
10EC45
Page 241
Fundamentals of HDL
10EC45
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity FULL_ADDER is
Port (A, B, cin : in std_logic; sum_1, cout : out std_logic);
end FULL_ADDER;
architecture beh_vhdl of FULL_ADDER is
--This architecture is a behavioral description of a full adder
begin
oneBit : process (A, B, cin)
variable y : std_logic_vector (2 downto 0);
begin
Y := (A & B & Cin);
--The above statement is a concatenation of
--three bits A, B, and Cin
case y is
when "000" => sum_1 <= '0'; cout <= '0';
when "110" => sum_1 <= '0'; cout <= '1';
when "101" => sum_1 <= '0'; cout <= '1';
when "011" => sum_1 <= '0'; cout <= '1';
when "111" => sum_1 <= '1'; cout <= '1';
when others => sum_1 <= '1'; cout <= '0';
--Others here refer to 100, 001, 010
end case;
end process;
end beh_vhdl;
Invoking a Verilog Module from a VHDL Module
We can instantiate a Verilog module from a VHDL module by instantiating a component
in the VHDL module that has the same name and ports as the Verilog module. The
Verilog module should be the only construct that has the same name as the component.
Mixed-Language Description of an AND Gate
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
--This is the VHDL module
entity andgate is
port (a, b : in std_logic; c : out std_logic);
end andgate;
Dept. of ECE, SJBIT
Page 242
Fundamentals of HDL
10EC45
Page 243
Fundamentals of HDL
10EC45
jk1 : jk_verilog port map (Jx, Kx, clk, clx, Qx, Qxbar);
end JK_FF;
module jk_verilog (j, k, ck, clear, q, qb);
// The module name jk_verilog matches
// the name of the VHDL components
input j, k, ck, clear;
output q, qb;
--The input and output ports match those of the
--VHDL component, jk_verilog
reg q, qb;
reg [1:0] JK;
always @ (posedge ck, clear)
begin
if (clear == 1)
begin
q = 1'b0;
qb = 1'b1;
end
else
begin
JK = {j, k};
case (JK)
2'd0 : q = q;
2'd1 : q = 0;
2'd2 : q = 1;
2'd3 : q = ~q;
endcase
qb = ~q;
end
end
endmodule
Page 244
Fundamentals of HDL
10EC45
Page 245
Fundamentals of HDL
10EC45
Page 246
Fundamentals of HDL
10EC45
Page 247
Fundamentals of HDL
10EC45
FIGURE 9.10 Logic diagram of an n-bit synchronous counter with ripple-carry out.
Mixed-Language Description of an N-Bit Asynchronous Counter
--This is a VHDL module
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity asynch_ctrMx is
Generic (N : integer := 3);
port (clk, clear : in std_logic;
C, Cbar : out std_logic_vector (N-1 downto 0);
rco : out std_logic);
end asynch_ctrMx;
architecture CT_strgnt of asynch_ctrMx is
component jkff is
--This is a JK flip-flop with a clear bound to Verilog module jkff
port(j, k, clk, clear : in std_logic; q, qb : out std_logic);
end component;
component andgate is
--This is a three-input AND gate bound to Verilog module andgate
port (I1, I2, I3 : in std_logic; O1 : out std_logic);
end component;
signal h, l : std_logic;
signal s : std_logic_vector (N downto 0);
signal s1 : std_logic_vector (N downto 0);
signal C_tem : std_logic_vector (N-1 downto 0);
Dept. of ECE, SJBIT
Page 248
Fundamentals of HDL
10EC45
begin
h <= '1';
l <= '0';
s <= (C_tem & clk);
-- s is the concatenation of Q and clk. We need this
-- concatenation to describe the clock of each JK flip-flop.
s1(0) <= not clear;
Gnlop : for i in (N - 1) downto 0 generate
G1 : jkff port map (h, h, s(i), clear, C_tem(i), Cbar(i));
end generate GnLop;
C <= C_tem;
rc_gen : for i in (N - 2) downto 0 generate
--This loop to determine the ripple carry-out
rc : andgate port map (C_tem(i), C_tem(i+1), s1(i), s1(i+1));
end generate rc_gen;
rco <= s1(N-1);
end CT_strgnt;
module jkff (j, k, clk, clear, q, qb);
// This is a behavioral description of a JK flip-flop
input j, k, clk, clear;
output q, qb;
reg q, qb;
reg [1:0] JK;
always @ (posedge clk, clear)
begin
if (clear == 1)
begin
q = 1'b0;
qb = 1'b1;
end
else
begin
JK = {j,k};
case (JK)
2'd0 : q = q;
Dept. of ECE, SJBIT
Page 249
Fundamentals of HDL
10EC45
2'd1 : q = 0;
2'd2 : q = 1;
2'd3 : q = ~q;
endcase
qb = ~q;
end
end
endmodule
module andgate (I1, I2,I3, O1);
//This is a three-input AND gate
input I1, I2, I3;
output O1;
assign O1 = (I1 & I2 & I3);
endmodule
Page 250
Fundamentals of HDL
10EC45
end component;
component pmos_verlg
--This component, after linking to a Verilog module, behaves as a
--pmos switch
port (O1 : out std_logic; I1, I2 : in std_logic);
end component;
--constant vdd : std_logic := '1';
--constant gnd : std_logic := '0';
-- In Chapter 5 we wrote Vdd and gnd as constants.
-- Some VHDL/Verilog simulators do not transfer constants
-- between VHDL and Verilog. So we wrote them as signals.
signal vdd, gnd, Selbar, s0, s1, s2, s3 : std_logic;
begin
vdd <= '1';
gnd <= '0';
--Invert signal Sel. If the complement of Sel is available,
--then no need for the following pair of transistors.
v1 : pmos_verlg port map (Selbar, vdd, Sel);
v2 : nmos_verlg port map (Selbar, gnd, Sel);
--Write the pull-down combination
n1 : nmos_verlg port map (s0, gnd, E);
n2 : nmos_verlg port map (s1, s0, Sel);
n3 : nmos_verlg port map (ybar, s1, a);
n4 : nmos_verlg port map (s2, s0, Selbar);
n5 : nmos_verlg port map (ybar, s2, b);
--Write the pull-up combination
p1 : pmos_verlg port map (ybar, vdd, E);
p2 : pmos_verlg port map (ybar, s3, Sel);
p3 : pmos_verlg port map (ybar, s3, a);
p4 : pmos_verlg port map (s3, vdd, Selbar);
p5 : pmos_verlg port map (s3, vdd, b);
Page 251
Fundamentals of HDL
10EC45
end mux2x1switch;
Page 252
Fundamentals of HDL
10EC45
input [3:0] a;
output [3:0] b;
reg [3:0] b;
always @ (a)
begin
casex (a)
4'bxxx1 : b = 4'd1;
4'bxx10 : b = 4'd2;
4'bx100 : b = 4'd4;
4'b1000 : b = 4'd8;
default : b = 4'd0;
endcase
end
endmodule
Mixed-Language Description of a Simple RC Filter
Page 253
Fundamentals of HDL
10EC45
begin
result := 0;
lop1 : for i in a' range loop
if a(i) = '1' then
result := result + 2**i;
end if;
end loop;
return result;
end TO_Intgr;
component flter_RC
--The name of the component flter_RC is the same name as the
--Verilog module
port (I1, I2 : in std_logic_vector (3 downto 0); O1 : out
std_logic_vector (7 downto 0));
end component;
signal Hw_tmp : std_logic_vector (7 downto 0);
begin
dw : flter_RC port map (w, w_ctoff, Hw_tmp);
//output the data on a file
fl : process (w, w_ctoff, Hw_tmp)
file outfile : text;
variable fstatus : file_open_status;
variable temp : line;
variable Hw_int, w_int, w_ctoffintg : integer;
begin
--Files can take integer, real, or character;
--they cannot take std-logic-vector; so convert to integer.
Hw_int := TO_Intgr (Hw_tmp);
w_int := TO_Intgr (w);
w_ctoffintg := TO_Intgr (w_ctoff);
file_open (fstatus, outfile, "Wfile_int.txt", write_mode);
--The file name is Wfile_int.txt
--Write headings. Be sure the simulator supports formatted output.
--otherwise take out all formatted output statements
write (temp, "
This is a Simple
R-C Low Pass Filter");
--The above statement when entered in the VHDL module should be
--entered in one line without carriage return */
Dept. of ECE, SJBIT
Page 254
Fundamentals of HDL
10EC45
file_close (outfile);
Hw_vhd <= Hw_tmp;
end process fl;
end Filter_draw;
// Next we write the Verilog module;
// the module performs a real division
module flter_RC (I1, I2, O1);
//The module performs the real division O1 = 1/[(I1/I2)**2 + 1]
input [3:0] I1, I2;
output [7:0] O1;
reg [7:0] O1;
real S, s1;
always @ (I1, I2)
begin
s1 = ((1.0*I1)/(1.0 * I2))**2 ;
//we multiply by 1.0 so the division is done in real format.
S = 1.0 / (1.0 + s1);
O1 = 100.00 * S;
end
endmodule
Dept. of ECE, SJBIT
Page 255
Fundamentals of HDL
10EC45
Not all VHDL data types are supported in mixed-language description. Only bit,
bit_vector, std_logic, std_ulogic, std_logic_vector and std_ulogic_vector are
supported.
The VHDL port type buffer is not supported.
Only a VHDL component construct can invoke a Verilog module. We cannot
invoke a Verilog module from any other construct in the VHDL module.
A Verilog module can only invoke a VHDL entity. It cannot invoke any other
construct in the VHDL module, such as a procedure or function.
Page 256
Fundamentals of HDL
10EC45
ASSIGNMENT QUESTIONS
Hours :6
Recommended readings:
1.
VHDL
Page 257
Fundamentals of HDL
10EC45
Synthesis maps between the simulation (software) domain and the hardware
domain.
Synthesis can be viewed as reverse engineering. The user is provided with the
behavioral code and is asked to develop the logic diagram.
Not all HDL statements can be mapped into the hardware domain. The hardware
domain is limited to signals that can take zeros, ones or that are left open. The
hardware domain cannot differentiate, for example, between signals and variables,
as does the simulation (software) domain.
Several synthesis packages are available on the market. These packages can take
behavior code, map it, and produce a net list that is downloaded into the chip.
Page 258
Fundamentals of HDL
x
10EC45
Two synthesizers may synthesize the same code using a different number of the
same gates. This is due to the different approaches taken by the two synthesizers
to map the code.
Synthesis steps
Page 259
Fundamentals of HDL
10EC45
Page 260
Fundamentals of HDL
10EC45
Page 261
Fundamentals of HDL
10EC45
Page 262
Fundamentals of HDL
10EC45
Page 263
Fundamentals of HDL
10EC45
FIGURE 10.14 Gate level synthesis (a) Logic symbol (b) Gate-level logic diagram.
VHDL Code for a Signal-Assignment Statement, Y = 2 * X + 3VHDL and Verilog
VHDL Signal-Assignment Statement, Y = 2 * X + 3
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity sign_assn2 is
port (X : in unsigned (1 downto 0);
Y : out unsigned (3 downto 0));
end ASSN2;
[I adjusted the name of the entity to be the same as the Verilog]
architecture BEHAVIOR of sign_assn2 is
begin
P1 : process (X)
begin
Y <= 2 * X + 3;
end process P1;
end BEHAVIOR;
Verilog Signal-Assignment Statement, Y = 2 * X + 3
module sign_assn2 (X, Y);
input [1:0] X;
output [3:0] Y;
reg [3:0] Y;
always @ (X)
begin
Y = 2 * X + 3;
end
Dept. of ECE, SJBIT
Page 264
Fundamentals of HDL
10EC45
endmodule
FIGURE 10.15 Gate level synthesis (a) Logic symbol (b) Gate-level logic diagram.
Structural Verilog Code for the Logic Diagram in Figure 10.15b.
module sign_struc(X, Y);
input [1:0] X;
output [3:0] Y;
reg [3:0] Y;
always @ (X)
begin
Y[0] = 1'b1;
Y[1] = ~ X[0];
Y[2] = X[0] ^ X[1];
Y[3] = X[1] & X[0];
end
endmodule
Page 265
Fundamentals of HDL
10EC45
Page 266
Fundamentals of HDL
10EC45
FIGURE 10.17 Gate level synthesis (a) Logic symbol (b) Gate-level logic diagram.
Mapping Logical OperatorsVHDL and Verilog
VHDL: Mapping Logical Operators
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity decod_var is
port (a : in std_logic_vector (1 downto 0);
D : out std_logic_vector (3 downto 0));
end decod_var;
architecture Behavioral of decod_var is
begin
dec : process (a)
variable a0bar, a1bar : std_logic;
begin
a0bar := not a(0);
a1bar := not a(1);
D(0) <= not (a0bar and a1bar);
D(1) <= not (a0bar and a(1));
D(2) <= not (a(0) and a1bar);
D(3) <= not (a(0) and a(1));
end process dec;
end Behavioral;
Verilog: Mapping Logical Operators
module decod_var (a, D);
input [1:0] a;
Dept. of ECE, SJBIT
Page 267
Fundamentals of HDL
10EC45
output [3:0] D;
reg a0bar, a1bar;
reg [3:0] D;
always @ (a)
begin
a0bar = ~ a[0];
a1bar = ~ a[1];
D[0] = ~ (a0bar & a1bar);
D[1] = ~ (a0bar & a[1]);
D[2] = ~ (a[0] & a1bar);
D[3] = ~ (a[0] & a[1]);
end
endmodule
FIGURE 10.18 Gate level synthesis (a) Logic symbol (b) Gate-level logic diagram.
Page 268
Fundamentals of HDL
10EC45
Page 269
Fundamentals of HDL
end
10EC45
else
Y = X1;
Page 270
Fundamentals of HDL
10EC45
else
Y = 1'b0;
end
endmodule
Page 271
Fundamentals of HDL
10EC45
Page 272
Fundamentals of HDL
10EC45
Page 273
Fundamentals of HDL
10EC45
Page 274
Fundamentals of HDL
10EC45
Page 275
Fundamentals of HDL
10EC45
Page 276
Fundamentals of HDL
10EC45
Page 277
Fundamentals of HDL
10EC45
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use work.types.all;
entity state_machine is
port (A, clk : in std_logic; pres_st : buffer states;
Z : out std_logic);
end state_machine;
architecture st_behavioral of state_machine is
begin
FM : process (clk, pres_st, A)
variable present : states := state0;
Dept. of ECE, SJBIT
Page 278
Fundamentals of HDL
10EC45
begin
if (clk = '1' and clk'event) then
--clockevent is an attribute to the signal clk; the above if
-- Boolean expression means the positive edge of clk
case pres_st is
when state0 =>
if A ='1' then
present := state1;
Z <= '0';
else
present := state0;
Z <= '1';
end if;
when state1 => if A ='1' then
present := state2;
Z <= '0';
else
present := state3;
Z <= '0';
end if;
when state2 => if A ='1' then
present := state3;
Z <= '1';
else
present := state0;
Z <= '0';
end if;
when state3 => if A ='1' then
present := state0;
Z <= '1';
Dept. of ECE, SJBIT
Page 279
Fundamentals of HDL
10EC45
else
present := state2;
Z <= '1';
end if;
end case;
pres_st <= present;
end if;
end process FM;
end st_behavioral;
Page 280
Fundamentals of HDL
10EC45
entity listing10_32 is
port (a : in std_logic_vector (3 downto 0);
c : in integer range 0 to 15;
b : out std_logic_vector (3 downto 0));
end listing10_32;
architecture listing10_32 of listing10_32 is
begin
shfl : process (a, c)
variable result, j : integer;
Dept. of ECE, SJBIT
Page 281
Fundamentals of HDL
10EC45
result := 0;
lop1 : for i in 0 to 3 loop
if a(i) = '1' then
result := result + 2**i;
end if;
end loop;
if result > c then
lop2 : for i in 0 to 3 loop
j := (i + 2) mod 4;
temp (j) := a(i);
end loop;
else
lop3 : for i in 0 to 3 loop
j := (i + 1) mod 4;
temp (j) := a(i);
end loop;
end if;
b <= temp;
end process shfl;
end listing10_32;
Page 282
Fundamentals of HDL
10EC45
Page 283
Fundamentals of HDL
10EC45
Page 284
Fundamentals of HDL
10EC45
end Int_Bin;
architecture convert of Int_Bin is
procedure itb (bin : out std_logic_vector;
signal flag : out std_logic;
N : in integer; int : inout integer) is
begin
if (int MOD 2 = 0) then
flag <= '1';
else
flag <= '0';
end if;
for i in 0 to N loop
if (int MOD 2 = 1) then
bin (i) := '1';
else
bin (i) := '0';
end if;
int := int / 2;
end loop;
end itb;
begin
process (Y_int)
variable tem : std_logic_vector (N downto 0);
variable tem_int : integer;
begin
tem_int := Y_int;
itb (tem, flag_even, N, tem_int);
Dept. of ECE, SJBIT
Page 285
Fundamentals of HDL
10EC45
Page 286
Fundamentals of HDL
10EC45
Page 287
Fundamentals of HDL
10EC45
ASSIGNMENT QUESTIONS
1) Discuss some of the important facts related to synthesis. 7 Marks
2) Discuss synthesis information from entity with examples.8 Marks
3) Describe synthesis information extraction from entity and module with examples.
10 Marks
4) Explain mapping the signal assignment and variable assignment statements
to Gate-level with suitable examples. 10 Marks
5) Explain extraction of synthesis information from entity. 4 Marks
6) With an example explain verilog synthesis information extraction from module
inputs and outputs. 4 Marks
7) Write VHDL/verilog code for signal assignment statement Y = (2*X+3) for an
entity with one input X of 2-bits and one output Y of 4-bits. Show mapping of this
signal assignment to gate level. 12 Marks
Page 288