VHDL Coding
VHDL Coding
VHDL stands for Very high-speed integrated circuit (VHSIC) Hardware Description Language. It is a
programming language used to model a digital system by dataflow, behavioral and structural style
of modeling. This language was first introduced in 1981 for the department of Defense (DoD)
under the VHSIC program.
Describing a Design
In VHDL an entity is used to describe a hardware module. An entity can be described using,
• Entity declaration
• Architecture
• Configuration
• Package declaration
• Package body
Entity Declaration
It defines the names, input output signals and modes of a hardware module.
The entity declaration describes the input and output of a design entity and parametric values.
The input and output I/O could be the I/O of an entity which is also the device level description-
I/O of a chip. It also can represent a schematic symbol which describe components connection to
the rest of the design.
Syntax −
entity entity_name is
Port declaration;
end entity_name;
An entity declaration should start with ‘entity’ and end with ‘end’ keywords. The direction will be
input, output or inout.
Ports
Each I/O signal in an entity declaration is referred as a port which is analogous to a pin in a
schematic symbol. The set of ports defined for the entity is referred as a part of declaration. Each
port has a mode which describes the direction in which data is transferred through the port. The
following modes are considered:-
in : data flows only into entity. Mode is used for clock input, control input and unidirectional data
inputs.
Out : - data flows only from source to output of entity. It is used for Count output.
Buffer : Internal feedback which is needed to declare port as mode buffer or declared a separate
signal for use within architecture body a port that is declared as mode buffer is similar to a port
that is declared as Mode out.
in/out : bidirectional declaration in which mode is out allows data flow into and out of the entity.
Example:-
a, b : in STD_logic_vector(3 down to 0)
C1 : in STD_logic
C0 : out STD_logic) ;
end add4;
A(3:0) C1 sum(3:0)
B(3:0) CO
The entity declaration for add4 can be processed in VHDL software/compiler if it is written as
follows:
library ieee:
a, b : in STD_logic_vector(3 down to 0)
C1 : in STD_logic
C0 : out STD_logic) ;
end add4;
Architecture −
The architecture body describes the contents of an entity. There are three types of style used for
description of architecture body-behavioral description, dataflow description and structural
description.
Syntax −
begin
Statements;
end architecture_name;
Here, we should specify the entity name for which we are writing the architecture body. The
architecture statements should be inside the ‘begin’ and ‘énd’ keyword. Architecture declarative
part may contain variables, constants, or component declaration.
Behavioral Modeling
In this modeling style, the behavior of an entity as set of statements is executed sequentially in the
specified order. Only statements placed inside a PROCESS, FUNCTION, or PROCEDURE are
sequential.
Rather than specifying the structure or netlist of a circuit, the designs are specified with a set of
statements that model function or behaviour of entity or port of entity.
Sequential statements are used to model the function of devices used in the circuit.
PROCESSES, FUNCTIONS, and PROCEDURES are the only sections of code that are executed
sequentially.
However, as a whole, any of these blocks is still concurrent with any other statements placed
outside it.
One important aspect of behavior code is that it is not limited to sequential logic. Indeed, with it,
we can build sequential circuits as well as combinational circuits.
The behavior statements are IF, WAIT, CASE, and LOOP. VARIABLES are also restricted and
they are supposed to be used in sequential code only. VARIABLE can never be global, so its value
cannot be passed out directly.
Example:
library ieee:
a, b : in STD_logic_vector(3 down to 0)
end eqcomp4;
begin
comp process(a, b)
Begin
If a = b, then
equals <= 1
else
equals <= 0
end if
end behavioral
Data Flow Modeling
In this modeling style, the flow of data through the entity is expressed using concurrent (parallel)
signal. The concurrent statements in VHDL are WHEN and GENERATE.
Data flow style specified how data will be transform from signal to signal and input to output
without the use of sequential statements.
The difference between behaviour style and data flow style is that one uses process and other uses
how data will flow.
In the data flow descriptions conditional statement when-else or selected signal assignment (with-
select-when) statements rather than algorithms statement are used for representing simple
equations.
Besides them, assignments using only operators (AND, NOT, +, *, all, etc.) can also be used to
construct code.
Finally, a special kind of assignment, called BLOCK, can also be employed in this kind of code.
• Operators
• The WHEN statement (WHEN/ELSE or WITH/SELECT/WHEN);
• The GENERATE statement;
• The BLOCK statement
Library ieee:
use ieee : std_logic_1164 all:
use work.gatepkg.all;
architecture structure of eqcomp4 is
signal x : std_logic_vector(0 to 3)
begin
n0 : xnor port map (a(0), b(0), x(0));
n1 : xnor port map (a(1), b(1), x(1));
n2 : xnor port map (a(2), b(2), x(2));
n3 : xnor port map (a(3), b(3), x(3));
end structure;
• In the above example, separate entity declaration and architecture body pairs are created for
the and4, xnor2 and eqcomp4 design entities.
• The structural design methodology is often used to decomposed a design into manageable
units and is also used to attain higher degree of control over synthesis than other
architectures.
Logic Operation – AND GATE
X Y Z
0 0 0
0 1 0
1 0 0
1 1 1
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
Waveforms
Logic Operation – OR Gate
X Y Z
0 0 0
0 1 1
1 0 1
1 1 1
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
Waveforms
Logic Operation – NOT Gate
X Y
0 1
1 0
VHDL code for NOT gate
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
X Y z
0 0 1
0 1 1
1 0 1
1 1 0
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
Waveforms
Logic Operation – NOR Gate
X Y z
0 0 1
0 1 0
1 0 0
1 1 0
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
Waveforms
Logic Operation – XOR Gate
X Y Z
0 0 1
0 1 1
1 0 1
1 1 0
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
Waveforms
Logic Operation – X-NOR Gate
X Y Z
0 0 1
0 1 1
1 0 1
1 1 0
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
Waveforms
VHDL Programming Combinational Circuits
VHDL code for Half Adder
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity ha1 is
Port ( A : in std_logic;
B : in std_logic;
SUM : out std_logic;
CARRY : out std_logic);
end ha1;
Waveforms
entity fa is
Port ( a : in std_logic;
b : in std_logic;
c : in std_logic;
sum : out std_logic;
carry : out std_logic);
end fa;
architecture fa1 of fa is
begin
sum<= a xor b xor c;
carry<=(a and b)or(b and c)or(c and a);
end fa1;
VHDL code for full adder using structural method
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity FAdder is
Port ( FA, FB, FC : in STD_LOGIC;
FS, FCA : out STD_LOGIC);
end FAdder;
Waveforms
VHDL Code for a Half-Subtractor
Half
Library ieee;
use ieee.std_logic_1164.all;
entity half_sub is
port(a,c:in STD_LOGIC;
STD_LOGIC
d,b:out STD_LOGIC);
STD_LOGIC
end half_sub;
Waveforms
entity full_sub is
port(a,b,c:in STD_LOGIC; sub,borrow:out STD_LOGIC);
STD_LOGIC
end full_sub;
Waveforms
VHDL code for 2x1 Multiplexer
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity mux21 is
Port ( A : in std_logic;
B : in std_logic;
SEL : in std_logic;
X : out std_logic);
end mux21;
architecture mx of mux21
m is
begin
process(A,B,SEL)
begin
case SEL is
when "0"=>
X <= A;
when "1"=>
X <= B;
end case;
end process;
end mx;
entity mux is
port(S1,S0,D0,D1,D2,D3:in
S1,S0,D0,D1,D2,D3:in STD_LOGIC;
Y:out STD_LOGIC);
);
end mux;
Waveforms
VHDL Code for a Demultiplexer
Library ieee;
use ieee.std_logic_1164.all;
entity demux is
port(S1,S0,D:in STD_LOGIC;
STD_LOGIC
Y0,Y1,Y2,Y3:out STD_LOGIC);
end demux;
Waveforms
entity enc is
port(i0,i1,i2,i3,i4,i5,i6,i7:in STD_LOGIC;
o0,o1,o2: out STD_LOGIC);
STD_LOGIC
end enc;
Waveforms
VHDL Code for a 3 x 8 Decoder
library ieee;
use ieee.std_logic_1164.all;
entity dec is
port(i0,i1,i2:in STD_LOGIC;
STD_LOGIC
o0,o1,o2,o3,o4,o5,o6,o7: out STD_LOGIC);
end dec;
Waveforms
VHDL Code – 4 bit Parallel adder
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity pa is
port(a : in STD_LOGIC_VECTOR(3 downto 0);
b : in STD_LOGIC_VECTOR(3 downto 0);
ca : out STD_LOGIC;
sum : out STD_LOGIC_VECTOR(3 downto 0)
);
end pa;
architecture vcgandhi of pa is
Component fa is
port (a : in STD_LOGIC;
b : in STD_LOGIC;
c : in STD_LOGIC;
sum : out STD_LOGIC;
ca : out STD_LOGIC
);
end component;
signal s : std_logic_vector (2 downto 0);
signal temp: std_logic;
begin
temp<='0';
u0 : fa port map (a(0),b(0),temp,sum(0),s(0));
u1 : fa port map (a(1),b(1),s(0),sum(1),s(1));
u2 : fa port map (a(2),b(2),s(1),sum(2),s(2));
ue : fa port map (a(3),b(3),s(2),sum(3),ca);
3),b(3),s(2),sum(3),ca);
end vcgandhi;
Waveforms
VHDL Code – 4 bit Parity Checker
library ieee;
use ieee.std_logic_1164.all;
entity parity_checker is
port (a0,a1,a2,a3 : in std_logic;
p : out std_logic);
end parity_checker;
Waveforms
entity paritygen is
port (a0, a1, a2, a3: in std_logic;
std_logic
p_odd, p_even: out std_logic);
end paritygen;
Waveforms
VHDL
HDL Programming for Sequential Circuits
S-R Latch
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity S_R_latch_top is
Port ( S : in STD_LOGIC;
R : in STD_LOGIC;
Q : out STD_LOGIC);
end S_R_latch_top;
end Behavioral;
Waveforms
VHDL Code for a D Latch
library ieee; Waveforms
use ieee.std_logic_1164.all;
entity DL is
port(d:in STD_LOGIC;
q,qbar:buffer STD_LOGIC);
end DL;
architecture DLL of DL is
signal s1,r1:bit;
begin
q<= d nand qbar;
qbar<= d nand q;
end DLL;
entity srflip is
port(r, s, clk : in STD_LOGIC;
STD_LOGIC
q, qbar : buffer STD_LOGIC);
STD_LOGIC
end srflip;
Waveforms
VHDL code for a JK Flip Flop
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity jk is
port(
j : in STD_LOGIC;
k : in STD_LOGIC;
clk : in STD_LOGIC;
reset : in STD_LOGIC;
q : out STD_LOGIC;
qb : out STD_LOGIC);
end jk;
architecture jkff1 of jk is
begin
jkff : process (j,k,clk,reset) is
variable
iable m : std_logic := '0';
begin
if (reset = '1') then
m : = '0';
elsif (rising_edge (clk)) then
if (j/ = k) then
m : = j;
elsif (j = '1' and k = '1') then
m : = not m;
end if;
end if;
q <= m;
qb <= not m;
end process jkff;
end jkff1;
Waveforms
VHDL Code for a D Flip Flop
Library ieee;
use ieee.std_logic_1164.all;
entity dflip is
port(d,clk:in STD_LOGIC;
STD_LOGIC
q,qbar:buffer STD_LOGIC);
STD_LOGIC
end dflip;
Waveforms
VHDL Code for a T Flip Flop
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity Toggle_flip_flop is
port(
t : in STD_LOGIC;
clk : in STD_LOGIC;
reset : in STD_LOGIC;
dout : out STD_LOGIC
);
end Toggle_flip_flop;
begin
if (reset = '1') then
m : = '0';
elsif (rising_edge (clk)) then
if (t = '1') then
m : = not m;
end if;
end if;
dout < = m;
end process tff;
end tff1;
Waveforms
VHDL Code for a 4 - bit Up Counter
library IEEE;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity counter is
Clock, CLR : in std_logic;
port(Clock,
Q : out std_logic_vector(3 downto 0)
);
end counter;
begin
if (CLR = '1') then
tmp < = "0000";
elsif (Clock'event and Clock = '1') then
tmp
mp <= tmp + 1;
end if;
end process;
Q <= tmp;
end upcounter;
Waveforms
VHDL Code for a 4-bit
4 Down Counter
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity dcounter is
port(Clock, CLR : in std_logic;
Q : out std_logic_vector(3 downto 0));
end dcounter;
begin
process (Clock, CLR)
begin
if (CLR = '1') then
tmp <= "1111";
elsif (Clock'event and Clock = '1') then
tmp <= tmp - 1;
end if;
end process;
Q <= tmp;
end downcounter;
Waveforms
Shift Registers in VHDL
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity shift_register_top is
Port ( CLK : in STD_LOGIC;
D : in STD_LOGIC;
LED : out STD_LOGIC_VECTOR(7 downto 0));
end shift_register_top;
-- shift register
process (clock_div(4))
begin
if (CLK'event and CLK = '1') then
t
shift_reg(7) <= D;
shift_reg(6) <= shift_reg(7);
shift_reg(5) <= shift_reg(6);
shift_reg(4) <= shift_reg(5);
shift_reg(3) <= shift_reg(4);
shift_reg(2) <= shift_reg(3);
shift_reg(1) <= shift_reg(2);
shift_reg(0) <= shift_reg(1);
end if;
end process;
end Behavioral;
//=====================================================================
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity shift_register2 is
Port ( D : in STD_LOGIC;
CLK : in STD_LOGIC;
LED : out STD_LOGIC_VECTOR (7 downto 0));
end shift_register2;
-- clock divider
process (CLK)
begin
if (CLK'event and CLK = '1') then
clock_div <= clock_div + '1';
end if;
end process;
-- shift register
process (clock_div(4))
begin
if (clock_div(4)'event and clock_div(4) = '1') then
shift_reg(6 downto 0) <= shift_reg(7 downto 1);
shift_reg(7) <= D;
end if;
end process;