Showing posts with label vhdl. Show all posts
Showing posts with label vhdl. Show all posts

Tuesday, 17 December 2019

VHDL code for Buffer

library ieee;
use ieee.std_logic_1164.all;

entity sr is 
    port (
        clk,s,r:in std_logic;
        m,n,q,qn:inout std_logic);
    end sr;

architecture struct of sr is 
    component srlatch
        port(
            s,r:in std_logic;
            q,qn:inout std_logic);
        end component;
        
    begin 
        m<=(s nand clk);
        n<=(r nand clk);
        a1<=srlatch port map(m,n,q,qn);
    end struct;

VHDL code for SR Flip Flop

library ieee;
use ieee.std_logic_1164.all;

entity sr is 
    port (
        clk,s,r:in std_logic;
        m,n,q,qn:inout std_logic);
    end sr;

architecture struct of sr is 
    component srlatch
        port(
            s,r:in std_logic;
            q,qn:inout std_logic);
        end component;
        
    begin 
        m<=(s nand clk);
        n<=(r nand clk);
        a1<=srlatch port map(m,n,q,qn);
    end struct;

VHDL code for T Flip Flop

library ieee;
use ieee.std_logic_1164.all;

entity t is 
    port (
        t,clk:in std_logic;
        q,qn:out std_logic);
    end t;
    
architecture behav of t is 
    begin 
        process(t,clk)
        variable temp:std_logic;
        begin 
            if(clk='1' and rising_edge(clk)) then 
                if(t='0'then
                    temp:=temp;
                else 
                    temp:=not temp;
                end if;
            end if;
            q<=temp;
            qn<=not temp;
            end process;
        end behav;

VHDL code for Counter

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;

entity count is 
    port(
        reset,clk:in std_logic;
        d_out:out std_logic_vector(2 downto 0));
    end count;
    
architecture behav of count is
    begin
        process(clk,reset)
        variable m:std_logic_vector(2 downto 0);
        begin 
            if(reset='1'then 
                m:="000";
            elsif (rising_edge(clk)) then 
                m:=m+1;
            end if ;
            d_out<=m;
            end process;
        end behav;

VHDL code for causing DELAY

library IEEE;
use IEEE.std_logic_1164.all;

ENTITY gate IS
    port(
            i1,i2:in bit;
            o1:out bit);
    end gate;
    
architecture single_delay of gate is 
    begin 
        o1<=(i1 nand i2) after 5ns;
    end single_delay;

VHDL code for Encoder

library ieee;
use ieee.std_logic_1164.all;

entity enc is 
    port (
        d3,d2,d1,d0:in bit;
        o2,o1:out bit);
    end enc;
    
architecture single of enc is 
    begin 
        o2<=((d3 xor d2) and (not d1) and (not d0));
        o1<=((d3 xor d1) and (not d2) and (not d0));
    end single;

VHDL code for Full Adder

library IEEE;
use IEEE.std_logic_1164.all;

entity full_adder is
    port (
        i1,i2,i3:in std_logic;
        sum,carry:out std_logic);
    end full_adder;
    
architecture single_delay of full_adder is 
    signal s1,s2,s3: std_logic;
    begin 
        s1<=(i1 xor i2) after 5ns;
        s2<=(i1 and i2) after 5ns;
        sum<=(s1 xor i3) after 5ns;
        s3<=(s1 and i3) after 5ns;
        carry<=(s3 or s2) after 5ns;
    end single_delay;
       

VHDL code for Inverter Gate

library IEEE;
use IEEE.std_logic_1164.all;

ENTITY inv IS
    PORT(
            i1:IN BIT;
            o1:OUT BIT);
    END inv;
    
ARCHITECTURE single_delay OF inv IS
    BEGIN 
        o1<=not(i1) after 5ns;
    END single_delay;

VHDL code of JK Flip Flop

library ieee;
use ieee.std_logic_1164.all;
entity jk is 
    port(
        j,k,clk:in std_logic;
        q,qn:out std_logic);
    end jk;

architecture behav of jk is 
    begin   
        process(j,k,clk)
        variable temp:std_logic;
        begin 
            if (clk='1' and rising_edge(clk)) then
                if(j='0' and k='0'then    
                    temp:=temp;
                elsif (j='0' and k='1'then
                    temp:='0';
                elsif (j='1' and k='0'then 
                    temp:='1';
                else    
                    temp:=not temp;
                end if;
            end if;
            q<=temp;
            qn<=not temp;
            end process;
        end behav;

VHDL code for 4:1 Multiplexer MUX

library IEEE;
use IEEE.std_logic_1164.all;

entity mux is 
    port (
        c1,c2,i1,i2,i3,i4:in std_logic;
        o1:out std_logic);
    end mux;

architecture single_delay of mux is
    signal s1,s2,s3,s4:std_logic;
    begin 
        s1<=((not s2) and (not s1) and i1 ) after 5ns;
        s2<=((not s2) and s1 and i2) after 5ns;
        s3<=((s2) and (not s1) and i3) after 5ns;
        s4<=(s2 and s1 and i4) after 5ns;
        o1<=(s1 or s2 or s3 or s4) after 5ns;
    end single_delay;

VHDL code for 2:4 Decoder

library IEEE;
use IEEE.std_logic_1164.all;

entity decoder is
    port (
        i1,i2:in STD_logic;
        o1,o2,o3,o4:out STD_logic);
    end decoder;
   
architecture single_delay of decoder is
    begin
        o1<=((not i1) and (not i2)) after 5ns;
        o2<=((not i1) and i2) after 5ns;
        o3<=(i1 and (not i2)) after 5ns;
        o4<=(i1 and i2) after 5ns;
    end single_delay;

VHDL code for 2 bit binary adder

library IEEE;
use IEEE.std_logic_1164.all;

ENTITY gate2 IS
    PORT(
            i1,i2:IN BIT;
            o1:OUT BIT);
    END gate2;
   
ARCHITECTURE single_delay OF gate2 IS
    BEGIN
        o1<=(i1 AND i2) after 5ns;
    END single_delay;

VHDL code for COMPARATOR

library ieee;
use ieee.std_logic_1164.all;

entity comp is
    port(
        a,b:in bit;
        o1,o2,o3:out bit);
    end comp;
   
architecture single of comp is
    begin
        o1<=(a and (not b));
        o2<=(a xnor b);
        o3<=((not a) and b);
       
    end single;