Coa Lab Work Pravin Yadav
Coa Lab Work Pravin Yadav
Architecture
Lab
1
List of Experiments
Computer Organisation and Architecture Lab
Index
6-9
Implementation of n-bit Carry Propagation Adder using VHDL (Behavioral
2 b)
Model)
Design of a Primitive ALU with Control Unit for Add, Multiply, AND, OR
6 23 - 26
operations
2
Experiment 1
Aim:
To implement and simulate Half Adder, Half Subtractor, Full Adder, and Full Subtractor circuits
using VHDL in Xilinx Vivado.
Theory:
1. Half Adder:
A combinational circuit that adds two single-bit binary numbers (A and B) and produces a sum (S)
and a carry (C) as outputs.
• Sum (S) = A ⊕ B
• Carry (C) = A · B
2. Full Adder:
A circuit that adds three inputs: two significant bits (A and B) and a carry-in (Cin). It outputs a sum
and a carry-out.
3. Half Subtractor:
A combinational circuit that subtracts one binary digit from another and outputs the difference and
borrow.
• Difference (D) = A ⊕ B
• Borrow (B) = A̅ · B
4. Full Subtractor:
A circuit that subtracts two bits A and B, considering a borrow-in (Bin), and outputs a difference
and borrow-out.
3
• Difference (D) = A ⊕ B ⊕ Bin
• Software:
Half Adder
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity half_adder is
Port ( A : in STD_LOGIC;
B : in STD_LOGIC;
Sum : out STD_LOGIC;
Carry : out STD_LOGIC);
end half_adder;
Full Adder (can be implemented using two half adders and an OR gate)
4
Procedure:
1. Launch Vivado Design Suite.
2. Create a new project for each circuit (Half Adder, Full Adder, etc.).
3. Write the VHDL code and save it with .vhdl or .vhd extension.
7. (Optional) Implement the design on FPGA board using synthesis and implementation tools.
Observation:
Result:
The Half Adder, Half Subtractor, Full Adder, and Full Subtractor were successfully implemented
and simulated using VHDL on Vivado.
5
Experiment 2
a) Implementation of Full Adder using VHDL (Behavioral Model)
Aim:
• (a) To implement a 1-bit Full Adder using the Behavioral Modeling approach in VHDL.
Theory:
Full Adder:
A full adder adds three bits: A, B, and Carry-in (Cin). It outputs a Sum and Carry-out (Cout).
A carry propagation adder connects multiple full adders in series. Each stage passes its Cout to the
next adder as Cin.
For n bits, we need n full adders connected in cascade. Each full adder operates on bits A(i), B(i),
and a carry-in.
• Software:
• Hardware (Optional):
6
VHDL Code:
a) Full Adder (1-bit) - Behavioral Model
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity full_adder is
Port ( A : in STD_LOGIC;
B : in STD_LOGIC;
Cin : in STD_LOGIC;
Sum : out STD_LOGIC;
Cout : out STD_LOGIC);
end full_adder;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity n_bit_adder is
Generic ( N : integer := 4 ); -- Change N for different bit-widths
Port ( A : in STD_LOGIC_VECTOR(N-1 downto 0);
B : in STD_LOGIC_VECTOR(N-1 downto 0);
Cin : in STD_LOGIC;
Sum : out STD_LOGIC_VECTOR(N-1 downto 0);
Cout : out STD_LOGIC);
end n_bit_adder;
7
architecture Behavioral of n_bit_adder is
signal temp : STD_LOGIC_VECTOR(N downto 0);
begin
process(A, B, Cin)
begin
temp := ('0' & A) + ('0' & B) + Cin;
Sum <= temp(N-1 downto 0);
Cout <= temp(N);
end process;
end Behavioral;
Procedure:
2. Write and save the VHDL code for Full Adder and n-bit Adder.
Result:
• The 1-bit Full Adder was successfully implemented and simulated using Behavioral
Modeling in VHDL.
8
• The n-bit Carry Propagation Adder (4-bit) was also implemented using Behavioral Modeling
and verified through simulation.
9
Experiment 3
Aim:
To implement a 4:1 Multiplexer using 2:1 Multiplexers in VHDL using the Structural Modeling
approach.
Theory:
A Multiplexer (MUX) is a combinational circuit that selects binary information from one of many
input lines and directs it to a single output line based on select inputs.
• 1 output: Y
• Use two 2:1 MUXes in the first stage to handle inputs I0/I1 and I2/I3
• Use one 2:1 MUX in the second stage to select between outputs of the first stage.
10
Software Requirements:
• VHDL simulator
VHDL Code:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity mux2x1 is
Port ( A : in STD_LOGIC;
B : in STD_LOGIC;
Sel : in STD_LOGIC;
Y : out STD_LOGIC);
end mux2x1;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity mux4x1_structural is
Port ( I0, I1, I2, I3 : in STD_LOGIC;
S : in STD_LOGIC_VECTOR(1 downto 0);
Y : out STD_LOGIC);
end mux4x1_structural;
component mux2x1
Port ( A : in STD_LOGIC;
11
B : in STD_LOGIC;
Sel : in STD_LOGIC;
Y : out STD_LOGIC);
end component;
begin
U1: mux2x1 port map(A => I0, B => I1, Sel => S(0), Y => Y1);
U2: mux2x1 port map(A => I2, B => I3, Sel => S(0), Y => Y2);
U3: mux2x1 port map(A => Y1, B => Y2, Sel => S(1), Y => Y);
end Structural;
Procedure:
1. Open Vivado and create a new project.
2. Write the VHDL code for both the 2:1 MUX and 4:1 MUX.
3. Add both files to the project and declare the 2:1 MUX as a component inside the 4:1 MUX
entity.
S1 S0 I0 I1 I2 I3 Output Y
0 0 1 0 0 0 1
0 1 0 1 0 0 1
1 0 0 0 1 0 1
1 1 0 0 0 1 1
12
Result:
The 4:1 Multiplexer was successfully implemented using 2:1 Multiplexers in Structural
Modeling with VHDL and simulated using Vivado.
13
Experiment 4
Aim:
To implement D Flip-Flop, J-K Flip-Flop, and T Flip-Flop using VHDL (Behavioral Modeling)
and simulate their operation.
Theory:
Flip-Flops are basic memory elements used in sequential circuits to store binary data. They operate
on the clock signal and change state according to their characteristic equations.
• On every rising edge of the clock, the output follows the input D.
2. J-K Flip-Flop:
• Characteristic Equation:
• J = 0, K = 0 → No change
• J = 0, K = 1 → Reset (Q = 0)
• J = 1, K = 0 → Set (Q = 1)
• J = 1, K = 1 → Toggle
14
3. T Flip-Flop (Toggle Flip-Flop):
• Characteristic Equation:
• T = 0 → No change
Software Requirement:
VHDL Code:
1. D Flip-Flop
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity d_flip_flop is
Port ( D : in STD_LOGIC;
CLK : in STD_LOGIC;
Q : out STD_LOGIC);
end d_flip_flop;
15
2. J-K Flip-Flop
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity jk_flip_flop is
Port ( J : in STD_LOGIC;
K : in STD_LOGIC;
CLK : in STD_LOGIC;
Q : out STD_LOGIC);
end jk_flip_flop;
3. T Flip-Flop
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity t_flip_flop is
Port ( T : in STD_LOGIC;
CLK : in STD_LOGIC;
16
Q : out STD_LOGIC);
end t_flip_flop;
Procedure:
2. Write and save the code for each flip-flop (D, JK, T).
17
Result:
D Flip-Flop, J-K Flip-Flop, and T Flip-Flop were successfully implemented using VHDL
behavioral modeling and verified through simulation.
18
Experiment 5
Aim:
To implement a sequence detector using VHDL and verify its operation via simulation.
Theory:
A sequence detector is a Finite State Machine (FSM) designed to detect a particular pattern of
bits in a serial input stream. There are two types of sequence detectors:
We will implement a Mealy-based sequence detector for detecting a "1011" bit sequence in a
serial input stream.
19
We reset back to appropriate states depending on the input to allow overlapping sequences (e.g.,
detecting “1011011”).
Software Requirements:
entity sequence_detector is
Port ( clk : in STD_LOGIC;
rst : in STD_LOGIC;
din : in STD_LOGIC;
dout : out STD_LOGIC);
end sequence_detector;
20
type state_type is (S0, S1, S2, S3);
signal state, next_state : state_type;
begin
process(clk, rst)
begin
if rst = '1' then
state <= S0;
elsif rising_edge(clk) then
state <= next_state;
end if;
end process;
process(state, din)
begin
case state is
when S0 =>
if din = '1' then
next_state <= S1;
else
next_state <= S0;
end if;
dout <= '0';
when S1 =>
if din = '0' then
next_state <= S2;
else
next_state <= S1;
end if;
dout <= '0';
when S2 =>
if din = '1' then
next_state <= S3;
else
next_state <= S0;
end if;
dout <= '0';
21
when S3 =>
if din = '1' then
next_state <= S1;
dout <= '1';
else
next_state <= S2;
dout <= '0';
end if;
end case;
end process;
end Behavioral;
Procedure:
Result:
The sequence detector for the pattern “1011” was successfully implemented using VHDL
behavioral modeling and verified through simulation.
22
23
Experiment 6
Aim:
To design and implement a primitive ALU integrated with a Control Unit that can perform the
following operations based on instruction input:
• i) Add
• ii) Multiply
• iii) AND
• iv) OR
Theory:
An Arithmetic Logic Unit (ALU) is a key component of the CPU that performs arithmetic and
logical operations. A Control Unit (CU) generates control signals that dictate the operation to be
performed by the ALU.
Operation Table:
24
Op Code Operation Description
01 MUL A*B
11 OR A OR B (bitwise)
Software Requirements:
VHDL Code:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity alu_with_control is
Port (
A : in STD_LOGIC_VECTOR(3 downto 0);
B : in STD_LOGIC_VECTOR(3 downto 0);
Op : in STD_LOGIC_VECTOR(1 downto 0);
Y : out STD_LOGIC_VECTOR(7 downto 0)
);
end alu_with_control;
process(A, B, Op)
begin
case Op is
when "00" => -- ADD
result <= ("0000" & A) + ("0000" & B);
when "01" => -- MULTIPLY
result <= std_logic_vector(unsigned(A) * unsigned(B));
25
when "10" => -- AND
result <= "000000" & (A and B);
when "11" => -- OR
result <= "000000" & (A or B);
when others =>
result <= (others => '0');
end case;
end process;
Y <= result;
end Behavioral;
Procedure:
4. Simulate the project and verify the output for all four operations.
26
Result:
A primitive ALU with control unit was successfully designed in VHDL to perform addition,
multiplication, bitwise AND, and bitwise OR based on the 2-bit control signal. The operation was
verified through simulation.
27