Thursday 15 December 2016

VHDL Codes For Logical Gates

The following are the codes for some of the logical gates  along with the test bench in behavioral style.

1. AND GATE
  • SOURCE CODE
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity a1 is
    Port ( i1 : in  std_logic;
           i2 : in  std_logic;
           o : out  std_logic);
end a1;
architecture Behavioral of a1 is
begin
o<= i1 and i2;
end Behavioral;
  • TEST BENCH
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;

ENTITY aa_tb IS
END aa_tb;

ARCHITECTURE behavior OF aa_tb IS

    -- Component Declaration for the Unit Under Test (UUT)

    COMPONENT aa
    PORT(
         i1 : std_logic  ;
         i2 : std_logic  ;
         o : std_logic
        );
    END COMPONENT;
   
   --Inputs
   signal i1 : std_logic := '0';
   signal i2 : std_logic := '0';
  --Outputs
   signal o : std_logic;

BEGIN

 -- Instantiate the Unit Under Test (UUT)
   uut: aa PORT MAP (
          i1 => i1,
          i2 => i2,
          o => o
        );
   -- Stimulus process
   stim_proc: process
   begin
      -- hold reset state for 100 ns.
     i1<='0'; i2<='0';
      wait for 100 ns;
  i1<='0'; i2<='1';
      wait for 100 ns;
  i1<='1'; i2<='0';
      wait for 100 ns;
  i1<='1'; i2<='1';
      wait for 100 ns; 
   end process;
END;

Waveform:
And Gate Test Bench


2. XOR GATE
  • SOURCE CODE
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity xx is
    Port ( a : in  STD_LOGIC;
           b : in  STD_LOGIC;
           c : out  STD_LOGIC);
end xx;
architecture Behavioral of xx is
begin
c<= a xor b;
end Behavioral;
  • TEST BENCH
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
ENTITY xox IS
END xox;
ARCHITECTURE behavior OF xox IS
    -- Component Declaration for the Unit Under Test (UUT)
    COMPONENT xx
    PORT(
         a : IN  STD_LOGIC;
         b : IN  STD_LOGIC;
         c : OUT  STD_LOGIC
        );
    END COMPONENT;
   --Inputs
   signal a : STD_LOGIC := '0';
   signal b : STD_LOGIC := '0';

                --Outputs
   signal c : STD_LOGIC;
BEGIN
                -- Instantiate the Unit Under Test (UUT)
   uut: xx PORT MAP (
          a => a,
          b => b,
          c => c
        );
   -- Stimulus process
   stim_proc: process
   begin                
a<='0'; b<='0';
      wait for 100 ns;         
a<='0'; b<='1';
      wait for 100 ns;         
                                a<='1'; b<='0';
      wait for 100 ns;         
                                a<='1'; b<='1';
      wait for 100 ns;         
   end process;

END;

Waveform:
Xor gate test bench

3. NOR GATE
  • SOURCE CODE
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity n1 is
    Port ( x : in  STD_LOGIC;
           y : in  STD_LOGIC;
           z : out STD_LOGIC);
end n1;
architecture Behavioral of n1 is
begin
z<= x nor y;
end Behavioral;
  •  TEST BENCH
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
ENTITY nn IS
END nn;
ARCHITECTURE behavior OF nn IS
    -- Component Declaration for the Unit Under Test (UUT)
    COMPONENT n1
    PORT(
         x : IN  STD_LOGIC;
         y : IN  STD_LOGIC;
         z : OUT  STD_LOGIC
        );
    END COMPONENT;
 
   --Inputs
   signal x : STD_LOGIC := '0';
   signal y : STD_LOGIC := '0';

                --Outputs
   signal z : STD_LOGIC;
BEGIN
                -- Instantiate the Unit Under Test (UUT)
   uut: n1 PORT MAP (
          x => x,
          y => y,
          z => z
        );
   -- Stimulus process
   stim_proc: process
   begin                
      x<='0'; y<='0'; wait for 100 ns;
      x<='0'; y<='1'; wait for 100 ns;
       x<='1'; y<='0'; wait for 100 ns;
       x<='1'; y<='1'; wait for 100 ns                             
   end process;
END;

Waveform:
Nor gate test bench


No comments:

Post a Comment