Rogram No: 1 AIM: Write HDL Code To Realize All The Logic Gates
Rogram No: 1 AIM: Write HDL Code To Realize All The Logic Gates
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity gates is
Port ( Ain : in std_logic; ---- First Input
Bin : in std_logic; ---- Second Input
Op_not : out std_logic;
Op_or : out std_logic;
Op_and : out std_logic;
Op_nor : out std_logic;
Op_nand : out std_logic;
Op_xor : out std_logic;
Op_xnor : out std_logic);
end gates;
architecture Behavioral of gates is
begin
Op_not <= not Ain;
Op_or <= Ain or Bin;
Op_and <= Ain and Bin;
Op_nor <= Ain nor Bin;
Op_nand <= Ain nand Bin;
Op_xor <= Ain xor Bin;
Op_xnor <= Ain xnor Bin;
end Behavioral;
Verilog Program
input Ain,Bin;
endmodule
PROGRAM NO: 2
VHDL Program
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity d2_4mx is
port ( a,b: in std_logic;
s0,s1 : in std_logic;
y0,y1,y2,y3 : out std_logic);
end d2_4mx;
Verilog Program
module dec2_4(a,b,s0,s1,y0,y1,y2,y3);
input a,b,s0,s1;
output y0,y1,y2,y3;
VHDL Program
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity mux8_1 is
port (I0,I1,I2,I3,I4,I5,I6,I7,s0,s1,s2: in std_logic;
y:out std_logic);
end mux8_1;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity eight_onemux is
end eight_onemux;
begin
process(a,b,c,d,e,f,g,h,sel)
begin
if(sel="000") then
y:=a;
elsif
(sel="001") then
y:=b;
elsif
(sel="010") then
y:=c;
elsif
(sel="011") then
y:=d;
elsif
(sel="100") then
y:=e;
elsif
(sel="101") then
y:=f; elsif
(sel="110") then
y:=g; else
y:=h;
end if;
output <= y;
end process;
end behave;
Multiplexer 8to1dataflow modeling using Verilog.
Verilog Program
module mux8to1(I0,I1,I2,I3,I4,I5,I6,I7,s0,s1,s2,y);
input I0,I1,I2,I3,I4,I5,I6,I7,s0,s1,s2;
output y;
endmodule
4) 8 to 1 multiplexer in behavioural modeling.
Verilog Program
module mux8to1(I,sel,y);
input [7:0]I;
input [2:0]sel;
output reg y;
always@(I,sel)
begin
if(sel=="000") y=I[0];
else
y=I[7];
end
endmodule
c) 4 bit binary to grey converter
VHDL Program
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity bin2_gray is
end bin2_gray;
begin
process(a)
begin
o(3)<= a(3);
end process;
end behave;
Verilog Program
module bin_grey(a,o);
input [3:0] a;
output [3:0] o;
reg [3:0] o;
always @ (a)
begin
o[3] = a[3];
o[2] = (a[3]^a[2]);
o[1] = (a[2]^a[1]);
o[0] = (a[1]^a[0]);
end
endmodule
d) 4 bit comparator in dataflow modeling
VHDL Program
entity com_pr is
Port ( a,b : in STD_LOGIC_VECTOR (1 downto 0);
aeqb : out STD_LOGIC;
agtb : out STD_LOGIC;
altb : out STD_LOGIC);
end com_pr;
agtb <= (not a(1) and a(0) and not b(1) and not b(0)) or
(a(1) and not a(0) and not b(1)) or
(a(1) and a(0));
end Behavioral;
e) 4 bit comparator in behavioural modeling
VHDL Program
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity comparator is
altb,aeqb,agtb:out std_logic);
end comparator;
begin
process(a,b)
begin
altb <='0';
aeqb<='0';
agtb<='1';
elsif
(a=b)then
altb <='0';
aeqb<='1';
agtb<='0';
else
altb <='1';
aeqb<='0';
agtb<='0';
end if;
end process;
end behave;
3. Write a HDL code to describe the functions of a Full Adder using
VHDL Program
a) Dataflow modeling
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity fa_ader is
port(a,b,c:in std_logic;
sum,carry:out std_logic);
end fa_ader;
architecture behave of fa_ader is
signal q,l,n,p: bit;
begin
q<=a xor b;
l<=a and b;
n<=b and c;
p<=c and a;
sum<=q xor c;
carry<=l xor n xor p;
end behave;
b) Behavioral modeling
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity full_adder is
port(a,b,cin:in bit;
sum,carry:out bit);
end full_adder;
begin
process(a,b,cin)
variable s,c:bit;
begin
s:='0'; c:='0';
s:='1'; c:='0';
s:='1'; c:='0';
s:='0'; c:='1';
s:='1'; c:='0';
elsif (cin='1' and a='0' and b='1') then
s:='0'; c:='1';
s:='0'; c:='1';
else
s:='1'; c:='1';
end if;
sum <= s;
carry <=c;
end process;
end behave;
c) Structural modeling.
I. entity xrgate is
Port ( j,k : in STD_LOGIC;
l : out STD_LOGIC);
end xrgate;
I. entity angate is
Port ( p,q : in STD_LOGIC;
r : out STD_LOGIC);
end angate;
entity ader_struct is
Port ( a,b,cin : in STD_LOGIC;
sum, carry : out STD_LOGIC);
end ader_struct;
component angate is
Port ( p,q : in STD_LOGIC;
r : out STD_LOGIC);
end component;
component org is
Port ( p,q : in STD_LOGIC;
r : out STD_LOGIC);
end component;
component xrgate is
Port ( j,k : in STD_LOGIC;
l : out STD_LOGIC);
end component;
signal m,n,o:STD_LOGIC;
begin
end Behavioral;
4. Write a model for 32 bit ALU to perform according to given table below
0 000 Ain
00 001 Ain – 1
0 010 1 Ain + 1
0 011 Bin
0 100 Bin – 1
0 101 Bin + 1
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity ari_thop is
end ari_thop;
begin
a+b ;
end Behavioral;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity logic_op is
end logic_op;
begin
p xnor q;
end Behavioral;
2 to 1 mux
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity mx2_1pro is
s : in STD_LOGIC;
end mx2_1pro;
begin
y;
end Behavioral;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity ALU_MAIN is
en : in STD_LOGIC;
end ALU_MAIN;
component ari_thop is
end component;
component logic_op is
end component;
component mx2_1pro is
Port (x,y : in STD_LOGIC_VECTOR (31 downto 0);
s : in STD_LOGIC;
end component;
begin
end Behavioral;
5. develop the HDL code for the following flip-flops, SR, JK, D, T
SR flip-flop
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity rs_ff is
port(r,s,clk : in std_logic;
q : inout std_logic:='0';
end rs_ff;
signal m:std_logic:='0';
signal n:std_logic:='1';
begin
end flip;
MSJK flip-flop
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity jkff is
port(j,k,clk,rst:in bit;
end jkff;
begin
process(rst,clk,j,k)
variable a:bit:='0';
begin
if (rst='1') then
a :='0';
a := a;
elsif
a := '1';
else
a := not a;
end if;
end if;
q <= a;
end process;
end behave;
D-Flip-flop
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity DFF is
port (
end DFF;
begin
process(clk)
begin
q<=d;
end if;
end process;
end d_ff_arch;
T-Flip-flop
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity TFF is
end TFF;
begin
process(clk)
begin
q<=not t;
end if;
end process;
end t_ff_arch;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity sync_counter is
end sync_counter;
begin
process (CLK)
begin
ELSE
end if;
COUNT<= TEMP;
end if;
end process;
end counter_arch;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity async_counter is
end async_counter;
begin
process (CLK,RESET)
begin
COUNT<= TEMP;
end if;
end process;
end counter_arch;