VHDL Examples
VHDL Examples
VHDL Examples
VHDL EXAMPLES
AND- GATE
Library IEEE;
Use ieee.std_logic_1164.all;
Entity and1 is
Port (a, b: in std_logic;
Z: out std_logic);
End and1;
Architecture and_gate of and1 is
Begin
z<=a and b;
End and_gate;
OR- GATE
Library IEEE;
Use ieee.std_logic_1164.all;
Entity Or1 is
Port (a, b: in bit;
Y: out bit);
End Or1;
Architecture orgate of Or1 is
Begin
y <= a or b;
End orgate;
1
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
NOT-GATE
Library IEEE;
Use ieee.std_logic_1164.all;
Entity not1 is
Port (a: in bit;
Y: out bit);
End not1;
Architecture not_gate of not1 is
Begin
Y <= not a;
End not_gate;
HALF ADDER
Library IEEE;
Use ieee.std_logic_1164.all;
Use ieee.numeric_std.all;
Entity halfadder is
Port (a, b: in bit;
S, c: out bit);
End halfadder;
Architecture ha_arch of halfadder is
Begin
s<= a xor b;
c<= a and b;
End ha_arch;
2
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
3
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
FULL ADDER
Library IEEE;
Use ieee.std_logic_1164.all;
Entity fulladder is
Port (a, b, c: in bit;
S, y: out bit);
End fulladder;
Architecture fa_arch of fulladder is
Begin
s<= (a xor b) xor c;
y<= (a and b) or (a and c) or (b and c);
End fa_arch;
HALF SUBTRACTOR
Library IEEE;
Use ieee.std_logic_1164.all;
Entity HSub is
Port (A, B: in bit;
O, Y: out bit);
End HSub;
Architecture half_sub of HSub is
Begin
O<= A xor B;
Y<= (NOT A) AND B;
END half_sub;
4
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
Library IEEE;
Use IEEE.STD_Logic_1164.all;
Entity h_adder is
Port (HA, HB:in std_logic;
HS,HC:out std_logic);
end h_adder;
architecture half of h_adder is
begin
HS<= HA XOR HB;
HC<= HA AND HB;
end half;
Library IEEE;
Use IEEE.STD_Logic_1164.all;
Use IEEE.Numeric_STD.all;
Entity Full_adder is
Port (A, B, C: in std_logic;
Cout, Sum: out std_logic);
End Full_adder;
Architecture f_adder of Full_adder is
Component h_adder
port (HA, HB: in std_logic;
HS, HC: out std_logic);
End component;
Signal S1, C1, C2: std_logic;
Begin
HA1: h_adder port map (HA=> A, HB=> B, HS => S1, HC => C1);
HA2: h_adder port map (HA =>S1, HB=> C, HS => Sum, HC => C2);
Cout <= C1 or C2;
End f_adder
5
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
FULL SUBTRACTOR
Library IEEE;
Use ieee.std_logic_1164.all;
Use ieee.numeric_std.all;
Entity f_sub is
Port (A, B, C: in bit;
D, Y: out bit);
End f_sub;
Architecture full_sub of f_sub is
Begin
Y<= a xor b xor c;
d<= ((not a) and b) or ((not a) and c) or (c and b);
End full_sub;
6
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
7
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
Library IEEE;
Use ieee.std_logic_1164.all;
Use ieee.numeric_std.all;
Entity mux1 is
Port(s :in std_ulogic_vector (1 downto 0);
i :in std_ulogic_vector (3 downto 0);
Z: out std_logic);
End mux1;
Architecture mux2 of mux1 is
Begin
Process (s, i)
Begin
If ((not s(0) and not s(1)) ='1') then
z<= i(0);
Elsif ((not s(0) and s(1)) ='1') then
z <= i(1);
Elsif ((s(0) and not s(1)) ='1') then
z<= i(2);
Else TRUTH TABLE
z<=i(3);
End if;
End process;
End mux2;
8
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
Library IEEE;
Use ieee.std_logic_1164.all;
Entity de_mux is
Port (E, S0,S1: in std_logic ;
D: in std_logic;
Y: out std_ulogic_vector (3 downto 0));
End de_mux;
Architecture de_mux1 of de_mux is
Begin
Y (0) <= ((not E) and (not S0) and (not S1) and D);
Y (1) <= ((not E) and (not S0) and (S1) and D);
Y (2) <= ((not E) and (S0) and (not S1) and D);
Y (3) <= ((not E) and (S0) and (S1) and D);
End de_mux1;
9
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
10
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
Library IEEE;
Use ieee.std_logic_1164.all;
Use ieee.numeric_std.all;
Entity decoder is
Port ( I : in std_ulogic_vector (1 downto 0);
E : in std_logic;
y : out std_ulogic_vector (3 downto 0));
End decoder;
Architecture de_c of decoder is
Begin TRUTH TABLE
Process (I)
Begin
If (E = '0') then
Case I is
When "00" => y <= "0001" ;
When "01" => y <= "0010" ;
When "10" => y <= "0100" ;
When "11" => y <= "1000" ;
When others => y <= "0000" ;
End case;
Else y<= "0000";
End if;
End process;
End de_c ;
11
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
Library IEEE;
Use ieee.std_logic_1164.all;
Use ieee.numeric_std.all;
Entity fulladder is FULLADDER
Port (a, b, c: in std_logic;
S, y: out std_logic);
End fulladder;
Architecture fa_arch of fulladder is
Begin
s<= (a xor b) xor c;
y<= (a and b) or (a and c) or (b and c);
End fa_arch;
Library IEEE;
Use ieee.std_logic_1164.all;
Use ieee.numeric_std.all;
Entity binary_adder is
Port (
m: in std_ulogic_vector (3 downto 0);
N: in std_ulogic_vector (3 downto 0);
O: out std_ulogic_vector (4 downto 0));
End binary_adder;
Architecture bi_add of binary_adder is
Component fulladder
Port (a, b, c: in std_logic; S, y: out std_logic);
End component;
Signal C: std_ulogic_vector (4 downto 0) ;
Begin
c (0)<='0';
FA1: fulladder port map (m (0), n(0), c(0), o(0), c(1));
FA2: fulladder port map (m (1), n(1), c(1), o(1), c(2));
FA3: fulladder port map (m(2), n(2), c(2), o(2), c(3));
FA4: fulladder port map (m(3), n(3), c(3), o(3), c(4));
o (4)<=c(4);
End bi_add;
12
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
13
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
14
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
15
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
Library IEEE;
Use ieee.std_logic_1164.all;
Entity nand2 is
Port (a, b: in std_logic;
Y: out std_logic);
End nand2;
Architecture nand1 of nand2 is
Begin
y<= a nand b;
End nand1;
Library IEEE;
Use ieee.std_logic_1164.all;
Entity sr_latch is
Port(s, r: in std_logic;
Q, qbar: inout std_logic);
End sr_latch;
Architecture sr_l of sr_latch is
Component nand2
Port (a, b: in std_logic; FUNCTION TABLE
Y: out std_logic);
End component;
Begin
n: nand2 port map (s, qbar, Q );
m: nand2 port map (r, Q, qbar );
End sr_l;
16
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
17
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
D-FLIPFLOP
Library IEEE;
Use ieee.std_logic_1164.all;
Entity d_ff is
Port (d, clk: in std_logic;
q: out std_logic);
End d_ff;
Architecture d_flip of d_ff is
Begin
Process (clk)
Begin
if clk ='1' and clk'event then
q <= d;
End if;
End process;
End d_flip;
T-FLIPFLOP
Library IEEE;
Use ieee.std_logic_1164.all;
Entity tff is
Port (t, clk: in std_logic;
q: inout std_logic);
End tff;
Architecture tff1 of tff is
Begin
Process (t, clk)
Begin
q<='0';
If (clk='1' and clk'event) then
If (t='1') then
q<= not q;
Else
q<= q;
End if;
End if;
End process;
End tff1;
18
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
SR-FLIPFLOP
Library IEEE;
Use ieee.std_logic_1164.all;
Use ieee.numeric_std.all;
Entity nand2 is
Port (a, b: in std_logic;
Y: out std_logic);
End nand2;
Architecture nand1 of nand2 is
Begin
y<= a nand b;
End nand1;
Library IEEE;
Use ieee.std_logic_1164.all;
Use ieee.numeric_std.all;
Entity sr_ff is
Port(s, r, clk: in std_logic;
Q, qbar: inout std_logic);
End sr_ff;
Architecture sr_f of sr_ff is
Component nand2
Port (a, b: in std_logic;
Y: out std_logic);
End component;
Signal o: std_ulogic_vector (1 downto 0);
Begin
l: nand2 port map (s, clk, o(0));
p: nand2 port map (r, clk, o(1));
n: nand2 port map (o(0), qbar, q);
m: nand2 port map (o(1), Q, qbar);
End sr_f ;
19
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
JK FLIP-FLOP
Library IEEE;
Use ieee.std_logic_1164.all;
Entity jkf is
Port (j, k, clk: in std_logic;
Q, QBAR: inout std_logic);
End jkf;
Architecture jkff of jkf is
Begin
Process (clk, j, k)
Begin
If (clk='1' and clk'event) then
If (j='0' and k='0') then
Q <= Q;
QBAR <= QBAR;
Elsif (j='0' and k='1') then
Q <= '0';
QBAR <= '1';
Elsif (j='1' and k='0') then TRUTH TABLE
Q <= '1';
QBAR <= '0';
Elsif (j='1' and k='1') then
Q <= not q;
QBAR <= not qbar;
End if;
End if;
End process;
End jkff;
20
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
End process;
Q <= state;
Qbar <= not state;
End behv;
21
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
22
VIGNAN INSTITUTE OF TECHONOLOGY AND MANAGEMENT
UP-COUNTER
Library IEEE;
Use ieee.std_logic_1164.all;
Use ieee.std_logic_unsigned.all;
Entity up_counter is
Port (cout : out std_logic_vector (7 downto 0);
Enable: in std_logic;
Clk : in std_logic;
Reset: in std_logic);
End entity;
Architecture struct1 of up_counter is
Signal Count: std_logic_vector (7 downto 0);
Begin
Process (clk, reset) begin
If (reset = '1') then
Count <= (others=>'0');
Elsif (rising_edge (clk)) then
If (enable = '1') then
Count <= count + 1;
End if;
End if;
End process;
Cout <= count;
End sruuct1;
23