Professional Documents
Culture Documents
Ch ter
Programming
11 in
VHDL
11.1 Introduction
VHDL (VHSIC hardware description language) is a hardware description lan-
guage used in electronic design automation to describe digital and mixed-signal
systems such as field programmable gate arrays and integrated circuits where
VHSIC stands for Very High Speed Integrated Circuit.
The VHSIC hardware description language is an industry standard used to
describe hardware from the abstraction to the concrete level. VHDL resulted from
work done in 70s and early 80s by the US Department of Defence. In 1986,
VHDL was proposed as an IEEE standard. It went through a number of revisions
and changes until it was adopted as the IEEE 1076 standard in December 1987.
Library <Library_name>;
11.2 Digital Electronics, an easy approach to learn
For example, one particularly useful standard library is called IEEE, this stands
for the Institute of Electrical and Electronics Engineers (IEEE), which is a profes-
sional Society for Electrical and Electronics Engineers. It is introduced by means
of the command
Library IEEE;
Once a library is referenced, any definition from the library may be used in writing
a VHDL description of a digital system.
Information in library is contained in subgroups called packages. A particular
useful package is called IEEE 1164, which is based on the VHDL language defi-
nition. This defines types and functions that are convenient to use in writing code,
and it is usually placed in library.
In VHDL, we have different data types which can be used as per the needs of
the designer. Some common data type are:
Programming in VHDL 11.3
1) Bit
It is a very basic data type. The variable which is declared as bit data type can
have only two values, 0 & 1.
Syntax:
SIGNAL bit name: BIT :=0
Bit name <= 1;
The operator := is used for variable assignment, that is, we may change the
value later.
The operator <= is used for fixed assignment, we cannot change the value of
the variable which is assigned by using the operator <=.
Here, in the above syntax, SIGNAL is a keyword to initialize a bit variable in
the architecture.
2) Bit vector
A bit vector data type is used for array of bits. It must be declared with a value for
the vector range.
Syntax:
SIGNAL bit ArrayName : bit vector (3 down to)) :=0000;
An entire bit vector can be assigned to a value by using double quotes; as
shown below.
bitArrayName <= 1101;
The above statement assigns (1101)2 = 13 to variable bitArrayName
3) Boolean
Boolean is a standard VHDL data type typically used as a variable or as a constant
to signify some sort of condition.
Syntax:
SIGNAL BooleanName : Boolean := false;
11.4 Digital Electronics, an easy approach to learn
4) Character
The character data type is a predefined type declared in the STANDARD package
and the value of this type are the 256 character of the ISO 8859-1 (Latin 1)
The set of predefined operation for the CHARACTER type contains all rela-
tional functions: =, /=, <, <=, >, >=
5) Integer
The VHDL integer type has similar semantics to the integer type in most other
computer language. It is defined to have at least the range -2 31 to 231 -1.
6) Std-ulogic
The std-ulogic types are part of the IEEE library, and can have the values:
U Uninitialized
X Forcing unknown
0 Forcing 0
1 Forcing 1
Z High impedance
W Weak Unknown
L Weak 0
# Weak 1
dont care
Syntax:
SIGNAL StdLogicName: stdulogic := 0;
StdLogicName <=Z;
8) Std logic
Std logic data type is derived from std ulogic. Specifically it is a resolved
std ulogic. In VHDL, if a data type (that is std ulogic) doesnt have a function
defined for it, then it is called resolved, i.e., if two or more drivers are allocated
for a signal, the compiler will compile.
For example,
a <= 1
a <= 1 or 0
If signal a is of type std ulogic, then any compiler will immediately flag
this code as an error & there wont be any need to simulate to find it (infect the
simulator wont even let u get started)
U Uninitialized
X Forcing unknown
0 Forcing 0
1 Forcing 1
Z High impedance
W Weak Unknown
L Weak 0
H Weak 1
Dont care
Syntax is same as that of std ulogic.
Syntax refers to the word usage and order that must be used to write state-
ment and also to the punctuation requirements that defines special points in the
statement, such as the end of a command.
11.5 Operators
Table 11.2 VHDL operators
Sl. No. Classes
1 Logical operator and or nand nor xor x-nor
2 Relational operator = /= < <= > >=
3 Shift operator sll srl sla sra rol ror
4 Additional Operator + = &
5 Unary operator + -
6 Multiplying Operator * / mod rem
7 Miscellaneous Operator ** abs not
Programming in VHDL 11.7
A) Logical operators
The logical operators (and, or, xor, nand, nor, xnor) are defined for the bit,
Boolean, std logic and std ulogic, data types and their vectors. They are
used to define Boolean logic expression or to perform, bit by bit operations on
arrays of bits. They give a result of the same type as the operand (bit, Boolean
etc). These operators can be applied to signals, variables and constants.
Note: The NAND and NOR operator are not associative. One should use
parenthesis in a sequence of nand and nor operator to prevent a syn-
tax error. As for example, X nand Y nand Z will give a syntax error and
should be written as (X nand Y) nand Z.
B) Relational operators
The relational operators test the relative values of two scalar types and give the
result as Boolean output of TRUE or FALSE.
Table 11.3 Description of relational operators
Operators Description Operand types Result type
= Equality Any type Boolean
/= Inequality Any type Boolean
< Smaller than Scalar or discrete array type Boolean
<= Smaller than or equal Scalar or discrete array type Boolean
> Greater than Scalar or discrete array type Boolean
>= Greater than or equal Scalar or discrete array type Boolean
11.8 Digital Electronics, an easy approach to learn
Note: The symbol of operator <= (smaller or equal to) is the same one as the
assignment operator used to assign a value to a signal or variable.
C) Shift operators
These operators perform a bit-wise shift or rotate operation on a one dimensional
array of elements of the type bit (or std logic) or Boolean.
Table 11.4 Description of shift operators
Operators Description Operand types Result
type
sll Shift left log- Left: any one dimensional array type with ele- Same as
ical (fill right ments of type bit or Boolean Right: integer left type
vacant bit with
0)
srl Shift right Left: any one dimensional array type with ele- Same as
logical (fill left ments of type bit or Boolean Right: integer left type
vacant bit with
0)
sla Shift left arith- Left: any one dimensional array type with ele- Same as
metic (fill right ments of type bit or Boolean Right: integer left type
vacant bit with
right most bit)
sra Shift right Left: any one dimensional array type with ele- Same as
arithmetic (fill ments of type bit or Boolean Right: integer left type
left vacant bit
with right most
bit)
rol Rotate left (cir- Left: any one dimensional array type with ele- Same as
cular) ments of type bit or Boolean Right: integer left type
ror Rotate right Left: any one dimensional array type with ele- Same as
(circular) ments of type bit or Boolean Right: integer left type
The operand resides on the left of the operator and the number (integer) of
shifts resides on the right side of the operator. As for example,
Variable NUM1 : bit vector:=10010110
NUM1 srl 2;
Will result in, NUM=00100101.
D) Addition operators
The addition operators are used to perform arithmetic operation (addition and
subtraction) on operands of any numeric type.
Programming in VHDL 11.9
E) Unary operators
The unary operator + and - are used to specify the sign of a numeric type.
Table 11.6 Description of unary operators
Operators Description Operand type Result type
+ Identity Any Numeric type Same type
- Negation Any Numeric type Same type
F) Multiplying operators
The multiplying operators are used to perform mathematical function on numeric
types (integer or floating point).
Table 11.7 Description of multiplying operators
Operators Description left operand types Right operand types Result type
Any integer or Same type Same type
floating type
* Multiplication Any physical Integer or real Same as left
type type
Any integer or Any physical Same as right
real type type
Any integer or Same type Same type
floating type
/ Any physical Integer or real Same as left
type type
Any integer or Any physical Integer
real type type
Mod Modulus Any integer type Same type
rem Remainder Any integer type Same type
G) Miscellaneous operators
These are the absolute value and exponentiation operators that can be applied to
numeric types.
11.10 Digital Electronics, an easy approach to learn
The logical negation (not) results in the inverse polarity but of the same type.
Table 11.8 Description of miscellaneous operators
Operators Description left operand types Right operand types Result type
** Exponentiation Integer type Integer type Same as left
Floating type Integer type
abs Absolute value Any numeric type Same as left
not Logical negation Any bit or Boolean type Same type
11.6 Identifier
Identifiers are user-defined words used to name objects in VHDL models. We
have seen examples of identifiers for input and output signals as well as the name
of a design entity and architecture body. When choosing an identifier one needs
to follow these basic rules:
(a) May contain only alphanumeric characteristics (A to Z, a through z, 0 to 9)
and the underscore ( ) character.
(b) The first character must be a letter and the last character cant be an under-
score.
(c) An identifier cant include two consecutive underscore.
(d) An identifier is case insensitive, (and2 or AND2 or And2 refer to the same
variable)
(e) An object can be of any length.
(f) An identifier cant be a keyword.
Examples of valid identifiers are: X10, x10, my gate1, my gate 1, etc.
Some invalid identifiers are: X10, mygate@input, my gate, gate-input,
etc.
Signals
Signals are declared outside the process using the following statement:
Signal list of signal names:type [:=initial values];
For example, signal sum, carry:std logic
Constant
A constant can have a single value of a given type and cant 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. Constant can be declared at the start of
an architecture and then can be used anywhere within the architecture. Constant
declared within a process can only be used inside that specific process.
For example,
Constant rise fall time:=2ns
Variable
A variable can have a single value, as with a constant but a variable can be updated
using a variable assignment statement. The variable is updated without any delay
as soon as the statement is executed. Variable must be declared inside a process
(and are local to the process). The variable declaration is as follows,
Variable list of variable names: type [:=initial value]
For example,
Variable cntr bit: bit:=0;
direction may be input, output or inout. An entity may have no port declaration
also.
Table 11.9 Port description
Direction Description
In Port can be read
Out Port can be written
Inout Port can be read and written
buffer Port can be read and written, it can have only one source
An entity is declared below and each term are described for better understand-
ing,
Architecture
It describes the internal description of design or it tells what is there inside design.
Each entity has at least one architecture. But, an entity can have many architec-
tures. Architecture can be described, using structural, data, behavioral or mixed
style.
Architecture can be used to describe a design at different levels of abstraction
like gate level, register transfer level (RTL) or behavior level.
Syntax:
architecture architecture name of entity name is
begin
statements;
end architecture name;
Here we should specify the entity name for which we are writing the archi-
tecture body. The architecture statements should be inside the begin and end
keyword. Architecture declarative part may contain variables, constants or com-
ponent declaration.
Programming in VHDL 11.13
internal wiring
(architecture body)
(data flow, behavioral
structural or mixed)
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
11.14 Digital Electronics, an easy approach to learn
entity half_adder is
port (A : in bit;
B : in bit;
sum : out bit;
carry : out bit);
end half_adder;
architecture dataflow of half_adder is
begin
sum <= A xor B;
carry <= A and B;
end dataflow;
Here STD LOGIC 1164 is an IEEE standard which defines a nine value logic
type, called STD ULOGIC. use is a keyword, which imports all the decla-
rations from this package. The architecture body consists of concurrent signal
assignments, which describes the functionality of the design. Whenever there is a
change in RHS, the expression is evaluated and the value is assigned to LHS.
Note: In data flow modeling, the VHDL programs are based on Boolean expres-
sion/logic expression.
Example 11.1 Write a VHDL code for AND gate using data using dataflow
model.
I Solution
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity AND_2 is
port ( A, B : in std_logic;
F : out std_logic);
end AND_2;
I Solution
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity expression is
port ( A, B, C, D : in std_logic;
F : out std_logic);
end expression;
Process
A process statement is the main construct in behavioral modelling that allows
us to use sequential statements to describe the behavior of system overtime. A
process is declared within architecture and is concurrent statement. However the
statements inside a process are executed sequentially.
The syntax for a process statement is:
[process label:]process[(sensitivity list)][is]
[process declaration]
begin
list of sequential statements such as:
signal assignments
11.16 Digital Electronics, an easy approach to learn
variable assignments
case statement
exit statement
if statement
loop statement
next statement
null statement
procedure call
wait statement
end process[process label]
if statement
The if statement is used to select one or none of a collection of statements depend-
ing on some condition expression. Note that conditions are expressions resulting
in Boolean values. If the condition is evaluated to be true, the corresponding
statement list is executed. Otherwise, if the else clause is present, the else
statement is executed. The example 11.3 well describes the behavioral modeling
in VHDL.
Example 11.3 Write a VHDL code for OR gate using behavioral code.
I Solution
The behavioral VHDL code is based on truth table, hence the truth table for OR
gate is given by,
Table 11.10 Truth table for OR gate
A B F
0 0 0
0 1 1
1 0 1
1 1 1
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
Programming in VHDL 11.17
entity OR_2 is
port( A, B : in std_logic;
F : out std_logic);
end OR_2;
elseif statement
Multiple conditions can be built with if statement followed by elseif clause(s).
Example 11.4 Write a VHDL code for the truth table shown below, using be-
havioral modeling.
Table 11.11 Truth table
A B F
0 0 0
0 1 1
1 0 Z
I Solution
The behavioral VHDL code is based on truth table.
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity model is
port( A, B : in std_logic;
F : out std_logic);
11.18 Digital Electronics, an easy approach to learn
end model;
case statement
The case statement is used to select a collection of statement based on the range of
values of a given expression called selection expression. The selection must be of
discrete type or a one dimensional array. For the matching choice the expression
is selected and statement link is executed. All the choices must be distinct, all
values must be represented in the choice lists. If some choice is not specified,
then the default choice must be indicated through the use of other keyword.
Example 11.5 Write a VHDL code to implement a 4:1 MUX using VHDL code
(use case statement).
I Solution
The case statement comes under behavioral modeling, and behavioral modeling
is based on truth table, hence the truth table for 4:1 MUX is given by,
Table 11.12 Truth table for 4:1 MUX
S1 S0 Output(F)
0 0 I0
0 1 I1
1 0 I2
1 1 I3
Programming in VHDL 11.19
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity MUX4_1 is
port(S1,S0: in std_logic;
I0, I1, I2, I3: in std_logic;
output : out std_logic);
end MUX4_1;
case sel is
when "00" => output<=I0;
when "01" => output<=I1;
when "10" => output<=I2;
when "11" => output<=I3;
when others => output<=X;
end case;
end process;
end behavioral;
with select
It is another constraint to model a behavioral problem in VHDL. The same exam-
ple of MUX which is discussed in previous example (11.4) using case statement
can also be implemented using with select as discussed below,
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity MUX4_1 is
11.20 Digital Electronics, an easy approach to learn
port(S1,S0: in std_logic;
I0, I1, I2, I3: in std_logic;
F : out std_logic);
end MUX4_1;
when else
It is another constraint to model a behavioral problem in VHDL. The same exam-
ple of MUX which is discussed in previous example (11.4) using case statement
can also be implemented using when else as discussed below,
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity MUX4_1 is
port(S1,S0: in std_logic;
I0, I1, I2, I3: in std_logic;
F : out std_logic);
end MUX4_1;
A
G1
B X1 F
G3
C X2
G2
D
AOI - network
F = A.B + C.D
F = X 1 + X2
The three equations for F , X1 and X2 provide the basic structure of the logic
network in which they provide the information needed to reconstruct the logic
diagram exactly as it was originally presented, and structural models are written
using the same concept.
11.22 Digital Electronics, an easy approach to learn
entity AOI_network is
port(A, B, C, D: in bit;
F : out bit);
end AOI_network;
the listing below builds the network using component architecture structural
of AOI network,
define an AND gate as a component
component AND_2
port(X, Y: in bit;
Z : out bit);
end component;
component OR_2
port(X, Y: in bit;
Z : out bit);
end component;
signal X1,X2:bit;
Programming in VHDL 11.23
begin
G1:AND_2 port map(A,B,X1);
G2:AND_2 port map(C,D,X2);
G3:OR_2 port map(X1,X2,F);
end structural;
To use a module as a component in this network, it must have been previously de-
fined by entity and architecture statements. This means that, we have to include
declarations such as:
For AND 2 component,
entity AND_2
port(u, v: in bit;
q : out bit);
end AND_2;
For OR 2 component,
entity OR_2
port(u, v: in bit;
q : out bit);
end OR_2;
In the complete VHDL listing of the network, the concept of component can be
understood using the concept of a design library. A design library is a collection
of different modules, each defined by entity and architecture statements. Once the
11.24 Digital Electronics, an easy approach to learn
cells are defined in the library, we may use copies of the cells in our design via
the component command. This is called instancing the cell, and the component
itself is called an instance of the original logic diagram.
The Fig.11.4 portrays the concept of a library and instancing. Note that any
instance is not the same as withdrawing the cell from the library. Instead, an
instance should be viewed as literal copy of the cell that is currently stored in
the library. This means that if we change the cell in the library, all the instances
changes accordingly. A cell may be instanced as many times as desired in the
network. It is easy to see that, this saves time and work in creating complex
networks, since the library entries only need to be defined once.
AND2 OR2
u u
q q
v v
Next, let us examine the use of the keyword signal. Signal is used to define
the internal identifiers X1 and X2 that defines the outputs of the AND2 gates G 1
and G2 in the original logic diagram. The signal definitions are different from
the identifier used in the port statement that they exist inside the module. Internal
identifiers are then used to wire the gates together to form the port connections.
Once the component has been declared, the network is described by the use
of the port map statements. These provide the wiring diagram information by
specifying how component signals interface.
Lets examine the entries,
the port map specifies the internal wiring
begin
G1:AND_2 port map(A,B,X1);
G2:AND_2 port map(C,D,X2);
G3:OR_2 port map(X1,X2,F);
end structural;
Programming in VHDL 11.25
tells us that gate G1 is the AND2 component and is wired to the port identifiers A,
B and internal signal X1 . The order of the signal in the AND2 component state-
ment was given as [u, v, q] corresponds to input1(A), input2(B) and output(X 1 ).
The port map maintains this implied order.
Similarly,
is another AND2 instance that maps the AOI module inputs C and D to the inputs
of the AND2 gate and produces an output X2 .
Finally,
says that G3 uses the internal signals X1 and X2 as inputs to the OR2 instance and
provide the output F of the module AOI network. This completes the structural
listings.
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity xorGate is
port( A, B : in std_logic;
F : out std_logic);
end xorGate;
11.26 Digital Electronics, an easy approach to learn
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity NANDGATE2 is
port( A, B : in std_logic;
F : out std_logic);
end NANDGATE2;
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity half_adder is
Port ( A : in std_logic;
B : in std_logic;
Programming in VHDL 11.27
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity fulladd is
Port(Cin,A,B:in std_logic;
S,cr:out std_logic);
end full add;
architecture dataflow of fulladd is
begin
s <= A xor B xor Cin ;
Cout <= (A and B) or (Cin and A) or (Cin and B);
end dataflow ;
Library IEEE;
11.28 Digital Electronics, an easy approach to learn
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity half_sub is
Port ( A : in std_logic;
B : in std_logic;
diff : out std_logic;
borrow : out std_logic);
end half_sub;
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity fullsub is
Port(Cin,A,B:in std_logic;
diff,br:out std_logic);
end fullsub;
architecture dataflow of fullsub is
begin
diff <= A xor B xor Cin ;
br <= (cin and (A xnor B)) or (not(A) and B);
end dataflow ;
Programming in VHDL 11.29
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity MUX_4 is
port (
A : in std_logic;
B : in std_logic;
C : in std_logic;
D : in std_logic;
s : in std_logic_vector(1 downto 0);
F : out std_logic
);
end MUX_4;
process(A,B,C,D,S)
begin
case S is
when "00" => F <= A;
when "01" => F <= B;
when "10" => F <= C;
11.30 Digital Electronics, an easy approach to learn
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity demux1_4 is
port (yo,y1,y2,y3 : out std_logic;
s : in std_logic_vector(1 downto 0);
bitin : in std_logic );
end demux1_4;
begin
process(bitin,)
begin
case s is
when "00"=>y0<= bitin; y1<=0;y2<=0;y3 <=0;
when "01"=>y1<= bitin; y0<=0;y2<=0;y3 <=0;
when "10"=>y2<= bitin; y0<=0;y1<=0;y3<=0;
Programming in VHDL 11.31
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity decoder3_8 is
port ( s : in std_logic_vector (2 downto 0);
y : out std_logic_vector (7 downto 0) );
end decoder3_8;
end behavioral;
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity compare is
port ( A, B : in std_logic_vector(3 downto 0) ;
AeqB, AgtB, AltB : out std_logic ) ;
end compare ;
architecture behavioral of compare is
begin
AeqB <= 1 when A = B else 0 ;
AgtB <= 1 when A > B else 0 ;
AltB <= 1 when A < B else 0 ;
end behavioral ;
Library IEEE;
Programming in VHDL 11.33
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity bin_gray is
port(b:in std_logic_vector(3 downto 0);
g:out std_logic_vector(3 downto 0));
end bin_gray;
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity gray_bin is
port(g:in std_logic_vector(3 downto 0);
b:inout std_logic_vector(3 downto 0));
11.34 Digital Electronics, an easy approach to learn
end gray_bin;
R Qn
S Qn
So, by considering the above logic diagram the VHDL code may be written
as,
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity SR_latch is
port(S,R : in std_logic;
q,nq : inout std_logic);
end SR_latch;
So, by considering the above truth table the VHDL code may be written as,
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity d_ff is
port(d,clock :in std_logic;
Q:out std_logic);
end d_ff;
begin
process(clock)
begin
end behavioral;
11.36 Digital Electronics, an easy approach to learn
So, by considering the above truth table the VHDL code may be written as,
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity JK_ff is
port(J,K,clock :in std_logic;
Qn:inout std_logic);
end JK_ff;
begin
process(clock,J,K)
begin
end behavioral;
So, by considering the above truth table the VHDL code may be written as,
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity T_ff is
port(T,clock :in std_logic;
Qn:inout std_logic);
end T_ff;
begin
process(clock,T)
begin
if (T = 0)then
Qn<=Qn;
else
Qn<=not(Qn);
end if;
end if;
end process;
end behavioral;
CL CL CL CL
Clear
From the above logic diagram the VHDL code using structural modelling
can be written as,
library IEEE;
use IEEE.STD_LOGIC_1164.All;
use IEEE.STD_LOGIC_ARITH.All;
use IEEE.STD_LOGIC_UNSIGNED.All;
entity siso is
port (si: in std_logic;
clk: in std_logic;
so: out std_logic);
end siso;
Programming in VHDL 11.39
CL CL CL CL
Clear
Data out
From the above logic diagram the VHDL code using structural modelling
can be written as,
library IEEE;
use IEEE.STD_LOGIC_1164.All;
use IEEE.STD_LOGIC_ARITH.All;
use IEEE.STD_LOGIC_UNSIGNED.All;
entity sipo is
11.40 Digital Electronics, an easy approach to learn
D0 D1 D2 D3
SHIFT/LOAD
LOAD SHIFT
X X X
D0 Q0 D1 Q1 D2 Q2 D3 Q3
Serial
CLK CLK CLK CLK output
CLK
From the above logic diagram the VHDL code using structural modelling
can be written as,
Programming in VHDL 11.41
library IEEE;
use IEEE.STD_LOGIC_1164.All;
use IEEE.STD_LOGIC_ARITH.All;
use IEEE.STD_LOGIC_UNSIGNED.All;
entity piso is
port (pi: in std_logic _vector (3 downto 0);
c: in std_logic;
clk: in std_logic;
so:inout std_logic);
end piso;
D QA D QB D QC D QD
CL CL CL CL
Clear
Data out
From the above logic diagram the VHDL code using structural modelling
can be written as,
library IEEE;
use IEEE.STD_LOGIC_1164.All;
use IEEE.STD_LOGIC_ARITH.All;
use IEEE.STD_LOGIC_UNSIGNED.All;
entity pipo is
port (pi: in std_logic _vector (3 downto 0);
clk: in std_logic;
po:inout std_logic _vector (3 downto 0));
end pipo;
begin
L1:d_ff port map(pi(3), clk, po(3),d(3));
L2:d_ff port map(pi(2), clk, po(2),d(2));
L3:d_ff port map(pi(1), clk, po(1),d(1));
L4:d_ff port map(pi(0), clk, po(0),d(0));
end structural;
Note that each bit in this four-bit sequence toggles when the bit before it
(the bit having a lesser significance, or place-weight), toggles in a particular
direction: from 1 to 0.
This key point is used in the code given below,
library IEEE;
use IEEE.STD_LOGIC_1164.All;
11.44 Digital Electronics, an easy approach to learn
use IEEE.STD_LOGIC_ARITH.All;
use IEEE.STD_LOGIC_UNSIGNED.All;
entity ripple_count is
port (ce: in std_logic;
clk: in std_logic;
n:in std_logic _vector (3 downto 0);
q:inout std_logic _vector (3 downto 0));
end ripple_count;
reset
PRE PRE PRE PRE
D3 Q3 D2 Q2 D1 Q1 D0 Q0
FF3 FF2 FF1 FF0
Q3 Q2 Q1 Q0
CLK
From the above logic diagram the VHDL code using structural modelling
can be written as,
library IEEE;
use IEEE.STD_LOGIC_1164.All;
use IEEE.STD_LOGIC_ARITH.All;
use IEEE.STD_LOGIC_UNSIGNED.All;
entity Johnson is
port(clk, reset: in std_logic;
q,qbar: inout std_logic_vector(3 downto 0));
end Johnson;
library IEEE;
use IEEE.STD_LOGIC_1164.All;
use IEEE.STD_LOGIC_ARITH.All;
use IEEE.STD_LOGIC_UNSIGNED.All;
entity ring is
port(clk, clr: in std_logic;
q: inout std_logic_vector(3 downto 0));
end ring;
library IEEE;
use IEEE.STD_LOGIC_1164.All;
use IEEE.STD_LOGIC_ARITH.All;
use IEEE.STD_LOGIC_UNSIGNED.All;
entity up_down is
port(clk, rst: in std_logic;
ud:in std_logic;
q: inout std_logic_vector(3 downto 0));
end up_down;