Es l4 Full Report
Es l4 Full Report
IN STITU TE OF EN G IN EER IN G
P U LC H OW K C A M P U S
A Lab R eport On
Em bedded System s
C om binational Logic D esign U sing V H D L
SU B M ITTED B Y SU B M ITTED TO
N am e: Bikash Pokhrel
Department of Electronics and
R oll N o: 078BEI010
Computer Engineering
Lab D ate: 2081-10-16
Objectives
To enable us to write VHDL code for a Field Programmable Gate Array (FPGA) capable of:
Equipment Required
• Software: Intel Quartus Prime (Altera Quartus), ModelSim Intel FPGA Edition
Theory
Introduction to VHDL
VHDL (Very High-Speed Integrated Circuit Hardware Description Language) is a hardware description language
used to model, simulate, and implement digital circuits. It enables designers to describe the behavior and
structure of electronic circuits in a textual form. VHDL supports various abstraction levels, making it suitable
for designing complex digital systems. It is case-insensitive, strongly typed, and supports both concurrent and
sequential modeling, ensuring robust and reusable designs.
• Entity Declaration: Defines the external interface of the circuit, specifying input and output ports.
• Architecture: Describes the internal behavior or structural interconnections of the circuit.
• Configuration: Allows selecting different architectures for an entity.
• Package and Package Body: Enables modularity by defining reusable components, constants, and
functions.
1
Applications of VHDL
VHDL is widely used in digital circuit design due to its flexibility and hardware abstraction capabilities. Some
common applications include:
• Design of combinational and sequential circuits such as adders, multiplexers, counters, and memory units.
• Implementation of digital signal processing (DSP) systems.
• Development of Field Programmable Gate Array (FPGA) and Application-Specific Integrated Circuit
(ASIC) designs.
• Hardware prototyping and simulation before fabrication.
• May have longer synthesis and simulation times for complex designs.
2
architecture behavioral of AND_Gate is
begin
process (A , B )
begin
if ( A = ’1 ’ and B = ’1 ’) then
Y <= ’1 ’;
else
Y <= ’0 ’;
end if ;
end process ;
end behavioral ;
Problems
Q1. Write a VHDL code to implement the given logic circuit, which has four inputs
(x1, x2, x3, x4) and one output (f). The implementation should be provided in
the following architectural styles:
1. Dataflow Style
2. Behavioral Style
3. Structural Style
Write a VHDL test bench to verify the operation of the logic circuit and provide
a simulation waveform depicting all possible input cases.
library ieee ;
use ieee . std_logic_1164 . all ;
entity combinational_logic is
port (
3
x1 , x2 , x3 , x4 : in std_logic ;
f : out std_logic
);
end entity combinational_logic ;
library ieee ;
use ieee . std_logic_1164 . all ;
entity c ombinational_logic_Behavioral is
port (
x1 , x2 , x3 , x4 : in std_logic ;
f : out std_logic
);
end entity combinational_logic_Behavioral ;
library ieee ;
use ieee . std_logic_1164 . all ;
entity c ombinational_logic_Structured is
port (
x1 , x2 , x3 , x4 : in std_logic ;
f : out std_logic
);
end entity combinational_logic_Structured ;
component not_gate
port ( x : in std_logic ; z : out std_logic ) ;
end component ;
4
component or_gate
port ( x , y : in std_logic ; z : out std_logic ) ;
end component ;
signal s1 , s2 , s3 , s4 , s5 , s6 , s7 : std_logic ;
begin
u0 : and_gate port map ( x1 , x2 , s1 ) ;
u1 : not_gate port map ( x2 , s2 ) ;
u3 : and_gate port map ( x3 , s2 , s3 ) ;
u4 : or_gate port map ( s1 , s3 , s4 ) ;
u5 : and_gate port map ( x4 , x3 , s5 ) ;
u6 : and_gate port map ( s2 , s5 , s6 ) ;
u7 : or_gate port map ( s4 , s6 , f ) ;
end architecture Structural ;
library IEEE ;
use IEEE . STD_LOGIC_1164 . ALL ;
entity and_gate is
Port ( x , y : in STD_LOGIC ;
z : out STD_LOGIC ) ;
end and_gate ;
NOT Gate
library IEEE ;
use IEEE . STD_LOGIC_1164 . ALL ;
entity not_gate is
Port ( x : in STD_LOGIC ;
z : out STD_LOGIC ) ;
end not_gate ;
OR Gate
library IEEE ;
5
use IEEE . STD_LOGIC_1164 . ALL ;
entity or_gate is
Port ( x , y : in STD_LOGIC ;
z : out STD_LOGIC ) ;
end or_gate ;
library ieee ;
use ieee . std_logic_1164 . ALL ;
entity test_q1_th is
end test_q1_th ;
begin
UUT : combinational_logic port map (
x1 = > test_vector (0) ,
x2 = > test_vector (1) ,
x3 = > test_vector (2) ,
x4 = > test_vector (3) ,
f = > f_tb
);
6
Simulation Waveform
After running the test bench in ModelSim or Quartus, the following simulation waveform is obtained, verifying
the circuit operation.
Q2. Write VHDL code to design a logic circuit that implements the truth table of
a BCD-to-Gray code converter.
1. Use Karnaugh maps to simplify the output functions.
2. Provide the following architectural styles:
(a) Dataflow Style
(b) Behavioral Style
(c) Structural Style using only NOR gates
3. Write a VHDL test bench to verify the operation of the logic circuit.
4. Provide a simulation waveform depicting all possible input cases.
a) Dataflow Style
library IEEE ;
use IEEE . STD_LOGIC_1164 . ALL ;
entity bcd_gray_code is
port (
a , b , c , d : in std_logic ;
w , x , y , z : out std_logic
);
end bcd_gray_code ;
7
w <= a ;
x <= a xor b ;
y <= b xor c ;
z <= c xor d ;
end Behavioral ;
b) Behavioral Style
library ieee ;
use ieee . std_logic_1164 . all ;
entity bcd_gray_code_behavioural is
port (
a , b , c , d : in std_logic ;
w , x , y , z : out std_logic
);
end entity bcd_gray_code_behavioural ;
library IEEE ;
use IEEE . STD_LOGIC_1164 . ALL ;
entity bcd_gray_code_structured is
Port ( a , b , c , d : in STD_LOGIC ;
w , x , y , z : out STD_LOGIC ) ;
end bcd_gray_code_structured ;
8
u1 : nor_gate port map ( s1 , b , s2 ) ;
u2 : nor_gate port map (b , b , s3 ) ;
u3 : nor_gate port map ( s3 , a , s4 ) ;
u4 : nor_gate port map ( s2 , s4 , s5 ) ;
u5 : nor_gate port map ( s5 , s5 , x ) ;
u6 : nor_gate port map ( s3 , c , s6 ) ;
u7 : nor_gate port map (c , c , s7 ) ;
u8 : nor_gate port map ( s7 , b , s8 ) ;
u9 : nor_gate port map ( s6 , s8 , s9 ) ;
u10 : nor_gate port map ( s9 , s9 , y ) ;
u11 : nor_gate port map ( s7 , d , s10 ) ;
u12 : nor_gate port map (d , d , s11 ) ;
u13 : nor_gate port map ( s11 , c , s12 ) ;
u14 : nor_gate port map ( s10 , s12 , s13 ) ;
u15 : nor_gate port map ( s13 , s13 , z ) ;
end Structured ;
nor_gate
library IEEE ;
use IEEE . STD_LOGIC_1164 . ALL ;
entity nor_gate is
Port ( x : in STD_LOGIC ;
y : in STD_LOGIC ;
z : out STD_LOGIC ) ;
end nor_gate ;
LIBRARY ieee ;
USE ieee . std_logic_1164 . ALL ;
ENTITY question_two_th IS
END question_two_th ;
9
signal w_tb , x_tb , y_tb , z_tb : std_logic ;
begin
-- Instantiate the Unit Under Test ( UUT )
UUT : bcd_gray_code_behavioural PORT MAP (
a = > test_vector (0) ,
b = > test_vector (1) ,
c = > test_vector (2) ,
d = > test_vector (3) ,
w = > w_tb ,
x = > x_tb ,
y = > y_tb ,
z = > z_tb
);
Q3. Write a VHDL code to implement the logic function (F) with three input
variables (x1, x2, and x3). The function (F) is equal to 1 if and only if two variables
are equal to 1; otherwise, it is equal to 0.
1. Provide the following architectural styles:
(a) Dataflow Style
10
(b) Behavioral Style
(c) Structural Style using only NOR gates
2. Write a VHDL test bench to verify the operation of the logic circuit.
3. Provide a simulation waveform depicting all possible input cases.
a) Dataflow Style
library IEEE ;
use IEEE . STD_LOGIC_1164 . ALL ;
entity logic_function_dataflow is
Port ( x1 , x2 , x3 : in STD_LOGIC ;
F : out STD_LOGIC ) ;
end logic_function_dataflow ;
b) Behavioral Style
library IEEE ;
use IEEE . STD_LOGIC_1164 . ALL ;
entity logic_function_behavioral is
Port ( x1 , x2 , x3 : in STD_LOGIC ;
F : out STD_LOGIC ) ;
end logic_function_behavioral ;
library IEEE ;
use IEEE . STD_LOGIC_1164 . ALL ;
entity logic_function_structural is
Port ( x1 , x2 , x3 : in STD_LOGIC ;
F : out STD_LOGIC ) ;
end logic_function_structural ;
11
architecture Structural of logic_function_structural is
component nor_gate is
Port ( a , b , c : in STD_LOGIC ;
z : out STD_LOGIC ) ;
end component ;
signal s1 , s2 , s3 , s4 , s5 , s6 , s7 : std_logic ;
begin
u0 : nor_gate port map ( x1 , x1 , x1 , s1 ) ;
u1 : nor_gate port map ( x2 , x2 , x2 , s2 ) ;
u2 : nor_gate port map ( x3 , x3 , x3 , s3 ) ;
u3 : nor_gate port map ( x1 , s2 , s3 , s4 ) ;
u4 : nor_gate port map ( s1 , x2 , s3 , s5 ) ;
u5 : nor_gate port map ( s1 , s2 , x3 , s6 ) ;
u6 : nor_gate port map ( s4 , s5 , s6 , s7 ) ;
u7 : nor_gate port map ( s7 , s7 , s7 , F ) ;
end Structural ;
library IEEE ;
use IEEE . STD_LOGIC_1164 . ALL ;
entity nor_gate is
Port ( a , b , c : in STD_LOGIC ;
z : out STD_LOGIC ) ;
end nor_gate ;
LIBRARY ieee ;
USE ieee . std_logic_1164 . ALL ;
ENTITY test_logic_function IS
END test_logic_function ;
12
signal F_tb : std_logic ;
begin
-- Instantiate the Unit Under Test ( UUT )
uut : logic_function_dataflow PORT MAP (
x1 = > test_vector (0) ,
x2 = > test_vector (1) ,
x3 = > test_vector (2) ,
F = > F_tb
);
Q4. Write VHDL code to implement the implicit sum of products (SOP) and
product of sums (POS) logic functions.
• Draw a truth table for the function (F), and use Karnaugh maps to simplify.
• Provide the following architectural styles:
1. Dataflow Style
2. Behavioral Style
3. Structural Style using only NOR gates
• Write a VHDL test bench to verify the operation of the logic circuit.
13
• Provide a simulation waveform depicting all possible input cases.
a) Dataflow Style
library IEEE ;
use IEEE . STD_LOGIC_1164 . ALL ;
entity sop_pos_dataflow is
Port ( x1 , x2 , x3 , x4 : in STD_LOGIC ;
f1 , f2 : out STD_LOGIC ) ;
end sop_pos_dataflow ;
b) Behavioral Style
library IEEE ;
use IEEE . STD_LOGIC_1164 . ALL ;
entity sop_pos_behavioral is
Port ( x1 , x2 , x3 , x4 : in STD_LOGIC ;
f1 , f2 : out STD_LOGIC ) ;
end sop_pos_behavioral ;
14
c) Structural Style using only NOR gates
library IEEE ;
use IEEE . STD_LOGIC_1164 . ALL ;
entity sop_pos_dataflow is
Port ( x1 , x2 , x3 , x4 : in STD_LOGIC ;
f1 , f2 : out STD_LOGIC ) ;
end sop_pos_dataflow ;
component nor_gate_3input is
Port ( e , f , g : in STD_LOGIC ;
h : out STD_LOGIC ) ;
end component ;
entity nor_gate_2input is
Port ( x : in STD_LOGIC ;
y : in STD_LOGIC ;
f : out STD_LOGIC ) ;
end nor_gate_2input ;
15
begin
f <= not ( x or y ) ;
end Behavioral ;
nor gate 3input
library IEEE ;
use IEEE . STD_LOGIC_1164 . ALL ;
entity nor_gate_3input is
Port ( e : in STD_LOGIC ;
f : in STD_LOGIC ;
g : in STD_LOGIC ;
h : out STD_LOGIC ) ;
end nor_gate_3input ;
LIBRARY ieee ;
USE ieee . std_logic_1164 . ALL ;
ENTITY test_sop_pos IS
END test_sop_pos ;
BEGIN
uut : sop_pos_dataflow PORT MAP (
x1 = > test_vector (0) ,
x2 = > test_vector (1) ,
x3 = > test_vector (2) ,
x4 = > test_vector (3) ,
f1 = > f1_out ,
f2 = > f2_out
);
16
test_vector <= " 0000 " ,
" 0001 " after 100 ns ,
" 0010 " after 200 ns ,
" 0011 " after 300 ns ,
" 0100 " after 400 ns ,
" 0101 " after 500 ns ,
" 0110 " after 600 ns ,
" 0111 " after 700 ns ,
" 1000 " after 800 ns ,
" 1001 " after 900 ns ;
END ;
Q5. Write VHDL code to implement a 2:1 MUX having inputs x1 and x2, select
line s and output y.
1. Provide the following architectural implementations:
(a) Using WHEN-ELSE statement
(b) Using IF-THEN-ELSE statement
2. Write a VHDL test bench to verify the operation of the 2:1 MUX.
3. Provide a simulation waveform depicting all possible input cases.
library IEEE ;
use IEEE . STD_LOGIC_1164 . ALL ;
entity mux_2 is
17
Port (
x1 , x2 , s : in STD_LOGIC ;
y : out STD_LOGIC ) ;
end mux_2 ;
library IEEE ;
use IEEE . STD_LOGIC_1164 . ALL ;
entity mux_2b is
Port (
x1 , x2 , s : in STD_LOGIC ;
y : out STD_LOGIC ) ;
end mux_2b ;
LIBRARY ieee ;
USE ieee . std_logic_1164 . ALL ;
ENTITY test_mux_2b IS
END test_mux_2b ;
18
ARCHITECTURE behavior OF test_mux_2b IS
-- Component Declaration for the Unit Under Test ( UUT )
COMPONENT mux_2b
PORT (
x1 , x2 , s : IN std_logic ;
y : OUT std_logic
);
END COMPONENT ;
19
6. Write VHDL code to implement a 4:1 MUX having inputs x1, x2, x3 and x4,
select lines s1, s0 and output y using three 2:1 multiplexers as the basic building
blocks.
• Use a hierarchical design approach:
1. Create component definitions in separate (.vhd) files
2. Use either Dataflow or Behavioral or Structural design styles
3. Use Structural design style for the 4:1 MUX architecture:
– Make use of 2:1 MUX component declaration
– Make use of 2:1 MUX component instantiation
4. Write a VHDL test bench to verify the operation of the 4:1 MUX.
5. Provide a simulation waveform depicting all possible input cases.
library IEEE ;
use IEEE . STD_LOGIC_1164 . ALL ;
entity MUX_2 is
Port (
x , y , s : in STD_LOGIC ;
z : out STD_LOGIC
);
end MUX_2 ;
20
process (s , x , y )
begin
case s is
when ’0 ’ = >
z <= x ;
when ’1 ’ = >
z <= y ;
when others = >
z <= ’0 ’;
end case ;
end process ;
end Behavioral ;
library IEEE ;
use IEEE . STD_LOGIC_1164 . ALL ;
entity MUX_4 is
Port (
x1 : in STD_LOGIC ;
x2 : in STD_LOGIC ;
x3 : in STD_LOGIC ;
x4 : in STD_LOGIC ;
s1 : in STD_LOGIC ;
s0 : in STD_LOGIC ;
Y : out STD_LOGIC
);
end MUX_4 ;
begin
u0 : MUX_2 port map ( x1 , x2 , s0 , s_0 ) ;
u1 : MUX_2 port map ( x3 , x4 , s0 , s_1 ) ;
u2 : MUX_2 port map ( s_0 , s_1 , s1 , Y ) ;
end Behavioral ;
LIBRARY ieee ;
USE ieee . std_logic_1164 . ALL ;
ENTITY test_mux_4 IS
21
END test_mux_4 ;
BEGIN
uut : MUX_4 PORT MAP (
x1 = > test_vector (0) ,
x2 = > test_vector (1) ,
x3 = > test_vector (2) ,
x4 = > test_vector (3) ,
s1 = > test_vector (4) ,
s0 = > test_vector (5) ,
Y = > Y_TB
);
22
Figure 8: Simulation waveform
7. Write VHDL code to implement a 4-bit adder/subtractor using four 1-bit full
adders.
• Use a Structural architecture style with hierarchical design approach:
1. Use 1-bit adder as the basic building block
2. Implement the 4-bit adder/subtractor using four 1-bit full adders.
• Write a VHDL test bench to verify the operation of the 4-bit adder/subtractor.
• Provide a simulation waveform depicting all possible input cases.
library IEEE ;
use IEEE . STD_LOGIC_1164 . ALL ;
entity adder_1b is
Port (
x , y , z : in STD_LOGIC ;
s , c : out STD_LOGIC
);
end adder_1b ;
23
library IEEE ;
use IEEE . STD_LOGIC_1164 . ALL ;
entity adder_4b is
Port (
a , b : in STD_LOGIC_VECTOR (3 downto 0) ;
s : out STD_LOGIC_VECTOR (3 downto 0) ;
c : inout STD_LOGIC_VECTOR (4 downto 0)
);
end adder_4b ;
LIBRARY ieee ;
USE ieee . std_logic_1164 . ALL ;
ENTITY test_adder4 IS
END test_adder4 ;
24
BEGIN
uut : adder_4b PORT MAP (
a = > test_vector (3 downto 0) ,
b = > test_vector (7 downto 4) ,
s = > output1 ,
c (0) = > o ,
c (4 downto 1) = > output2
);
Discussion
In this lab, we wrote VHDL programs for various combinational logic circuits. The circuits implemented
include a BCD to Gray converter, a multiplexer, and an adder. To verify the correctness of these programs, we
wrote test benches and analyzed the waveforms produced during simulation. The programs were written using
different VHDL design styles, including Dataflow, Behavioral, and Structural styles.
In our design, to store a single bit, we used the std logic type, and to store multiple bits, we used
std vector. In some of the programs, we first simplified the Boolean expressions using Karnaugh Maps (K-
map) before implementing them. This helped in reducing the complexity of the logic and ensuring the efficiency
of the designs.
Throughout the coding process, we learned the importance of selecting the appropriate design style and how
to handle different types of logic operations effectively in VHDL. Writing test benches allowed us to verify the
25
correctness of the designs and identify potential issues early on. These activities deepened our understanding
of VHDL and combinational circuit design.
Conclusion
In this lab, we successfully wrote VHDL programs for combinational logic circuits such as the BCD to Gray
converter, multiplexer, and adder. We verified the designs through test benches and waveform analysis. By
using various design styles, we gained a deeper understanding of VHDL programming and digital circuit design.
26