4-bit_adder_cum_subtractor
0 Stars     858 Views    

Author: TMSY Tutorials

Project access type: Public

Description:

AIM:

To write the Verilog code for 4-bit carry adder – cum subtractor and obtain the simulation, synthesis results using Xilinx ISE 14.7 tool.

APPARATUS:

  1. PC with Windows 10
  2. Xilinx ISE Software 14.7 Tool

THEORY:

FULL ADDER

A full adder is a digital circuit that performs the addition of two binary numbers along with a carry input. It has three inputs: a, b, and cin (carry input), and two outputs: sum and carry.

The truth table of a full adder is as follows:

abcincarrysum0000000101010010111010001101101101011111

In this truth table, ‘a’ and ‘b’ are the two input bits, ‘cin’ is the carry input, ‘sum’ is the sum output, and ‘carry’ is the carry output. The full adder circuit can be implemented using logic gates such as AND, OR, and XOR gates. In this truth table, ‘a’ and ‘b’ are the two input bits, ‘cin’ is the carry input, ‘sum’ is the sum output, and ‘carry’ is the carry output. The full adder circuit can be implemented using logic gates such as AND, OR, and XOR gates.

A full adder can be used as a building block for constructing larger adders such as a ripple-carry adder, carry-lookahead adder, or carry-select adder.

FULL SUBTRACTOR

A full subtractor is a digital circuit that performs the subtraction of two binary digits and produces a difference and a borrow. Similar to a full adder, a full subtractor takes into account a borrow input from previous stages of subtraction.

A full subtractor has three inputs, ‘a’, ‘b’, and ‘bin’ (borrow in), and two outputs, ‘difference’ and ‘borrow’.

The truth table of a full subtractor is as follows:

abbindifferenceborrow0000000111010110110110010101001100011110

In this truth table, the ‘difference’ output difference is the XOR of the input bits ‘a’ and ‘b’ and the ‘borrow’ output is the AND of the complement of ‘a’ and ‘b’, as well as the complement of the borrow input ‘bin’.

A full subtractor can be implemented using logic gates such as XOR, AND, and NOT gates. A cascade of full subtractors can be used for performing multi-bit subtraction. However, similar to the ripple carry adder, the ripple borrow propagate delay can lead to longer propagation delays for multi-bit subtraction.

4-BIT BINARY ADDER AND SUBTRACTOR

Figure 1 Block Diagram of 4-bit Adder and Subtractor

Figure 2 Simulated Diagram of 4-bit Adder and Subtractor when cin = 0

Figure 3 Simulated Diagram of 4-bit Adder and Subtractor when cin = 1

  • A 4-bit binary adder and subtractor is a digital circuit that can perform both addition and subtraction of two 4-bit binary numbers.
  • To implement a 4-bit binary adder and subtractor, we can use a combination of a 4-bit ripple carry adder and a 4-bit ripple carry subtractor.
  • The 4-bit ripple carry adder takes two 4-bit binary numbers as inputs and produces a 4-bit binary sum output. The 4-bit ripple carry subtractor takes two 4-bit binary numbers as inputs and produces a 4-bit binary difference output.
  • To perform addition or subtraction using the same circuit, we can use an XOR gate to switch between the two modes of operation. When the XOR input is 0, the circuit will perform addition, and when the XOR input is 1, the circuit will perform subtraction.
  • In this circuit, a3-a0 and b3-b0 are the two 4-bit binary numbers to be added or subtracted. s3-s0 is the 4-bit binary output of the adder or subtractor. The XOR gate selects between addition and subtraction modes based on the value of the XOR input. c2-c0 are the carry inputs to the full adders in the adder and subtractor circuits.
  • The operation of the 4-bit binary adder and subtractor can be understood as follows:

For addition mode:

  • The 4-bit binary numbers a3-a0 and b3-b0 are added using a 4-bit ripple carry adder to produce a 4-bit binary sum output s3-s0.
  • The XOR input is set to ‘0’ to select the addition mode, and the circuit performs addition.

For subtraction mode:

  • The 4-bit binary numbers a3-a0 and b3-b0 are subtracted using a 4-bit ripple carry subtractor to produce a 4-bit binary difference output s3-s0.
  • The XOR input is set to ‘1’ to select the subtraction mode, and the circuit performs subtraction.
  • Note that when performing subtraction, we need to use two's complement representation of the second input number (B) to obtain its negative value, which is added to the first input number (A) to perform subtraction.

PROGRAM:

SOURCE CODE:

Step 1: Full Adder

module fulladder(sum,carry,a,b,cin); output sum; output carry; input a; input b; input cin; wire w1,w2,w3; xor x1(w1,a,b); and x2(w2,a,b); xor x3(sum,w1,cin); and x4(w3,w1,cin); or x5(carry,w2,w3); endmodule

Step 2: 4-Bit Carry Adder cum Subtractor

module binary_adder_subtractor(sum,cout,a,b,cin); output [3:0]sum; output cout; input [3:0]a,b; input cin; wire c0,c1,c2; wire w1,w2,w3,w4; xor n1(w1,b[0],cin); xor n2(w2,b[1],cin); xor n3(w3,b[2],cin); xor n4(w4,b[3],cin); fulladder FA0(sum[0],c0,a[0],w1,cin); fulladder FA1(sum[1],c1,a[1],w2,c0); fulladder FA2(sum[2],c2,a[2],w3,c1); fulladder FA3(sum[3],cout,a[3],w4,c2); endmodule

TEST BENCH:

module binary_adder_subtractor_TB; // Inputs reg [3:0] a; reg [3:0] b; reg cin; // Outputs wire [3:0] sum; wire cout; binary_adder_subtractor uut(sum,cout,a,b,cin); initial begin $monitor($time,"a=%b,b=%b,cin=%b,cout=%b,sum=%b",a,b,cin,cout,sum); // Initialize Inputs a = 4'b0000; b = 4'b0000; cin = 1'b0; end always #20 a = a + 1; always #10 b = b + 1; always #05 cin = cin + 1; endmodule

RESULT

The Synthesis and Simulation results for the 4-bit Carry Adder cum Subtractor are obtained using the Xilinx ISE 14.7 tool.

Video Explanation

Execution

Created: Apr 01, 2024

Updated: Apr 01, 2024


Comments

You must login before you can post a comment.