FPGA Center



 * Inverter

 * AND Gate

 * NAND Gate  

 * OR Gate  

 * NOR Gate  

 * XOR Gate  

 * XNOR Gate  

 * Binary Adder  

 * BCD Adder  

 * Comparator  

  * Priority Encoder

BINARY  ADDER

We will add two values. First of all we will design a module that adds two one-bit binary numbers. Then we will see how to add bigger numbers (N bits) by using this module.

1 Bit Adder

Lets draw the truth table first.

  

We can get the equetion below from the truth table.

Si  = Ai xor Bi xor Ci
Ci+1  = Ai.Bi + Ai.Ci + Bi.Ci = Ai.Bi + (Ai xor Bi).Ci


library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity Bit_Adder is
    Port ( Ci : in  STD_LOGIC;
               Ai : in  STD_LOGIC;
               Bi : in  STD_LOGIC;
               Si : out  STD_LOGIC;
               Cn : out  STD_LOGIC);
   end Bit_Adder;

architecture Behavioral of Bit_Adder is
  begin
      Si<=Ai xor Bi xor Ci;
       Cn<=(Ai and Bi) or ((Ai xor Bi) and Ci);
end Behavioral;

After simulating this code with ModelSim, you will get a plot below.

N Bit Adder

In this project, we will use 1-Bit addet modules to form an N-bit adder. Besides we will define the capacity as generic in order to allow user to change the capacity of the adder.


library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

 entity N_BIT_ADDER is
    generic( N:integer:=5);
    Port ( Cin : in  STD_LOGIC;
               A : in  STD_LOGIC_VECTOR (N-1 downto 0);
               B : in  STD_LOGIC_VECTOR (N-1 downto 0);
               S : out  STD_LOGIC_VECTOR (N-1 downto 0);
               Cout : out  STD_LOGIC);
    end N_BIT_ADDER;

architecture Behavioral of N_BIT_ADDER is
COMPONENT Bit_Adder
            PORT(Ci : IN std_logic;
                        Ai : IN std_logic;
                        Bi : IN std_logic;         
                        Si : OUT std_logic;
                        Cn : OUT std_logic   );
            END COMPONENT;
           
signal temp:std_logic_vector(N-2 downto 0);
begin
 U1: for I in A'range generate
           begin
                    U2:  if I=A'right generate
                                begin
                                    U6:Bit_Adder PORT MAP( Ci => Cin ,  Ai => A(I),  Bi => B(I), Si =>S(I) , Cn =>temp(I) );
                                end generate;
                    U3:  if I= A'left generate                                 
                                begin
                                     U7: Bit_Adder PORT MAP(  Ci => temp(I-1) , Ai => A(I), Bi => B(I), Si =>S(I) , Cn => Cout);
                               end generate;                                  
                    U4: if (I < A'left) and I > A'right generate                             
                                 begin
                                    U8:Bit_Adder PORT MAP( Ci => temp(I-1) , Ai => A(I), Bi => B(I), Si =>S(I) , Cn =>temp(I) );
                                 end generate;
         end generate;
end Behavioral;

After simulating this code with ModelSim, you will get a plot below.

project                                                                                                                                  Test_bench

                                                                                                                                    

 

                                                                                                                                                                                                                                                                   

Home | Fpga | VHDL | VHDL Dictionary | Digital Design | Simulation | PCB | Examples | Contact Us
Copyright © 2010 - 2013 FPGAcenter. All Rights Reserved.