Professional Documents
Culture Documents
TRUTH TABLE:
a b c(and) d (or) e(not) f(nand ) g(nor) h (xor) i(xnor)
0 0
0 1
1 0
1 1
VHDL VERILOG
library IEEE; module gate1(a,b,c,d,e,f,g,h,i);
use IEEE.STD_LOGIC_1164.ALL; input a, b;
use IEEE.STD_LOGIC_ARITH.ALL; output c,d,e,f,g,h,i;
use IEEE.STD_LOGIC_UNSIGNED.ALL; assign c=a & b;
entity logicgate is assign d=a | b;
Port ( a,b : in std_logic; assign e= ~a;
c,d,e,f,g,h,i : out std_logic); assign f= ~(a & b);
end logicgate; assign g= ~(a | b);
architecture Behavioral of logicgate is assign h= a ^ b;
begin assign i= ~( a ^ b);
c<= a and b; endmodule
d<= a or b;
e<= not a
f<= a nand b;
g<= a nor b;
h<= a xor b;
i<= a xnor b;
end Behavioral;
2. HALF ADDER:
TRUTH TABLE:
a b sum(s) carry(c) EXPRESSIONS: S = A B
0 0 0 0 C = AB
0 1 1 0
1 0 1 0
1 1 0 1
Dataflow:
VHDL VERILOG
library IEEE; module half(a,b,s,c);
use IEEE.STD_LOGIC_1164.ALL; input a,b;
use IEEE.STD_LOGIC_ARITH.ALL; output s,c;
use assign s=a ^ b;
IEEE.STD_LOGIC_UNSIGNED.ALL; assign c=a & b;
entity half is endmodule
Port ( a,b : in std_logic;
s,c : out std_logic);
end half;
architecture dataflow of half is
begin
s<= a xor b;
c <= a and b;
end dataflow;
Behavioral :
VHDL VERILOG
library IEEE; module half(a,b,s,c);
use IEEE.STD_LOGIC_1164.ALL; input a,b;
use IEEE.STD_LOGIC_ARITH.ALL; output s,c;
use IEEE.STD_LOGIC_UNSIGNED.ALL; reg s,c;
entity half is always@ (a,b)
Port ( a,b : in std_logic; begin
s,c : out std_logic); s=a ^ b;
end half; c=a & b;
architecture Behavioral of half is end
begin endmodule
process(a,b)
begin
s<= a xor b;
c <= a and b;
end process;
end Behavioral;
Structural:
VHDL VERILOG
library IEEE; module half(a,b,s,c);
use IEEE.STD_LOGIC_1164.ALL; input a,b;
use IEEE.STD_LOGIC_ARITH.ALL; output s,c;
use IEEE.STD_LOGIC_UNSIGNED.ALL; xor u1(s,a,b);
entity half is and u2(c,a,b);
Port ( a,b : in std_logic; endmodule
s,c : out std_logic);
end half;
architecture structural of half is
component xor1
port (x,y:in std_logic;
z:out std_logic);
end component;
component and1
port (l,m:in std_logic;
n:out std_logic)
end component;
begin
u1:xor1 port map(a,b,s);
u2:and1 port map(a,b,c);
end structural;
Note: Separate Entity and Architecture should be written for each component along with
library and package statement.
3. FULL ADDER:
TRUTH TABLE:
a b c s cout
EXPRESSIONS: S = A B Ci
0 0 0 0 0 CO = AB+BC+CA
0 0 1 1 0
0 1 0 1 0
0 1 1 0 1
1 0 0 1 0
1 0 1 0 1
1 1 0 0 1
1 1 1 1 1
Dataflow:
VHDL VERILOG
library IEEE; module half(a,b,ci,s,co);
use IEEE.STD_LOGIC_1164.ALL; input a,b,ci;
use IEEE.STD_LOGIC_ARITH.ALL; output s,co;
use IEEE.STD_LOGIC_UNSIGNED.ALL; assign s=a^b^ci;
entity full is assign co=(a & b) | (b & ci) | (ci & a);
port ( a,b,c : in std_logic; endmodule
s,co : out std_logic);
end full;
architecture Behavioral of full is
begin
s<= a xor b xor c;
co <= (a and b) or (b and c) or (c and a);
end Behavioral;
Behavioral:
VHDL VERILOG
library IEEE; module half(a,b,ci,s,co);
use IEEE.STD_LOGIC_1164.ALL; input a,b,ci;
use IEEE.STD_LOGIC_ARITH.ALL; output s,co;
use IEEE.STD_LOGIC_UNSIGNED.ALL; reg s,co;
entity full is always@ (a,b,ci)
port ( a,b,c : in std_logic; begin
s,co : out std_logic); s=a^b^ci;
end full; co=(a & b) | (b & ci) | (ci & a);
architecture Behavioral of full is end
begin endmodule
process(a,b,c)
begin
s<= a xor b xor c;
co <= (a and b) or (b and c) or (c and a);
end process;
end Behavioral;
Structural:
VHDL VERILOG
library IEEE; module full(a,b,cin, s,cout);
use IEEE.STD_LOGIC_1164.ALL; input a,b,cin;
use IEEE.STD_LOGIC_ARITH.ALL; output s,cout;
use IEEE.STD_LOGIC_UNSIGNED.ALL; wire s1,s2,s3,s4;
entity full is xor u1(s1,a,b);
port ( a,b,cin : in STD_LOGIC; xor u2(s,s1,cin);
s,cout : out STD_LOGIC); and u3(s2,a,b);
end full; and u4(s3,b,cin);
architecture structural of full is and u5(s4,cin,a);
component xor1 or u6(cout,s2,s3,s4);
port(x,y:in std_logic; endmodule
z:out std_logic);
end component;
component and1
port(l,m:in std_logic;
n:out std_logic);
end component;
component or1
port(p,q,r:in std_logic;
r:out std_logic);
end component;
signal s1,s2,s3,s4:std_logic;
begin
u1:xor1 port map(a,b,s1);
u2:xor1 port map(s1,cin,s);
u3:and1 port map(a,b,s2);
u4:and1 port map(b,cin,s3);
u5:and1 port map(cin,a ,s4);
u6:or1 port map(s2,s3,s4, cout);
end structural;
Note: Separate Entity and Architecture should be written for each component along with
library and package statement.
Dept. of E&C, Ekalavya Institute of Tech. 12
HDL Lab Manual
d(7) d(6) d(5) d(4) d(3) d(2) d(1) d(0) s(2) s(1) s(0) f
X X X X X X X 0 0 0 0 0
X X X X X X X 1 0 0 0 1
X X X X X X 0 X 0 0 1 0
X X X X X X 1 X 0 0 1 1
X X X X X 0 X X 0 1 0 0
X X X X X 1 X X 0 1 0 1
X X X X 0 X X X 0 1 1 0
X X X X 1 X X X 0 1 1 1
X X X 0 X X X X 1 0 0 0
X X X 1 X X X X 1 0 0 1
X X 0 X X X X X 1 0 1 0
X X 1 X X X X X 1 0 1 1
X 0 X X X X X X 1 1 0 0
X 1 X X X X X X 1 1 0 1
0 X X X X X X X 1 1 1 0
1 X X X X X X X 1 1 1 1
VHDL VERILOG
library IEEE; module mux(d, s, f);
use IEEE.STD_LOGIC_1164.ALL; input [7:0] d;
use IEEE.STD_LOGIC_ARITH.ALL; input [2:0] s;
use IEEE.STD_LOGIC_UNSIGNED.ALL; output f;
entity mux is reg f;
Port ( d : in std_logic_vector (7 downto 0); always @ (d,s)
s : in std_logic_vector (2 downto 0); begin
f : out std_logic); case (s)
end mux; 3'b000: f=d[0];
architecture Behavioral of mux is 3'b001: f=d[1];
begin 3'b010: f=d[2];
process(d,s) 3'b011: f=d[3];
begin 3'b100: f=d[4];
case(s) is 3'b101: f=d[5];
when "000" =>f<=d(0); 3'b110: f=d[6];
when "001" =>f<=d(1); 3’b111: f=d[7];
when "010" =>f<=d(2); endcase
when "011" =>f<=d(3); end
when "100" =>f<=d(4); endmodule
when "101" =>f<=d(5);
when "110" =>f<=d(6);
when "111" =>f<=d(7);
when others=>null;
end case;
end process;
end Behavioral;
5: DEMULTIPLEXER (1:8):
TRUTH TABLE:
s(2) s(1) s(0) f y(7) y(6) y(5) y(4) y(3) y(2) y(1) y(0)
0 0 0 0 X X X X X X X 0
0 0 0 1 X X X X X X X 1
0 0 1 0 X X X X X X 0 X
0 0 1 1 X X X X X X 1 X
0 1 0 0 X X X X X 0 X X
0 1 0 1 X X X X X 1 X X
0 1 1 0 X X X X 0 X X X
0 1 1 1 X X X X 1 X X X
1 0 0 0 X X X 0 X X X X
1 0 0 1 X X X 1 X X X X
1 0 1 0 X X 0 X X X X X
1 0 1 1 X X 1 X X X X X
1 1 0 0 X 0 X X X X X X
1 1 0 1 X 1 X X X X X X
1 1 1 0 0 X X X X X X X
VHDL VERILOG
library IEEE; module demux (f, s, y);
use IEEE.STD_LOGIC_1164.ALL; input f;
use IEEE.STD_LOGIC_ARITH.ALL; input [2:0] s;
use IEEE.STD_LOGIC_UNSIGNED.ALL; output [7:0] y;
entity dmux is reg[7:0] y;
port(f:in std_logic; always @ (s,f)
s:in std_logic_vector(2 downto 0); begin
y:out std_logic_vector(7 downto 0)); case (s)
end demux; 3'b000:y[0]=f;
architectural behavioral of dmux is 3'b001:y[1]=f;
begin 3'b010:y[2]=f;
process(f,s) 3'b011:y[3]=f;
begin 3'b100:y[4]=f;
case(s) is 3'b101:y[5]=f;
when "000" =>y(0)<=f; 3'b110:y[6]=f;
when "001" =>y(1)<=f; 3’b111:y[7]=f;
when "010" =>y(2)<=f; endcase
when "011" =>y(3)<=f; end
when "100" =>y(4)<=f; endmodule
when "101" =>y(5)<=f;
when "110" =>y(6)<=f;
when "111" =>y(7)<=f;
when others=>null;
end case;
end process;
end behavioral;
Dept. of E&C, Ekalavya Institute of Tech. 14
HDL Lab Manual
VHDL VERILOG
library IEEE; module encod(y, s);
use IEEE.STD_LOGIC_1164.ALL; input [7:0]s;
use IEEE.STD_LOGIC_ARITH.ALL; output [2:0] y;
use IEEE.STD_LOGIC_UNSIGNED.ALL; reg [2:0]y;
entity enco is always @(s)
Port ( s : in std_logic_vector (7 downto 0); begin
y : out std_logic_vector (2 downto 0)); case (s)
end enco; 8'd1:y=3'd0;
architecture Behavioral of enco is 8'd2:y=3'd1;
begin 8'd4:y=3'd2;
process(s) 8'd8:y=3'd3;
begin 8'd16:y=3'd4;
case(s) is 8'd32:y=3'd5;
when "00000001" =>y<="000"; 8'd64:y=3'd6;
when "00000010" =>y<="001"; 8’d128:y=3'd7;
when "00000100" =>y<="010"; endcase
when "00001000" =>y<="011"; end
when "00010000" =>y<="100"; endmodule
when "00100000" =>y<="101";
when "01000000" =>y<="110";
when "10000000" =>y<="111";
when others=>null;
end case;
end process;
end behavioral;
INPUT OUTPUT
i(7) i(6) i(5) i(4) i(3) i(2) i(1) i(0) y(2) y(1) y(0)
1 X X X X X X X 1 1 1
0 1 X X X X X X 1 1 0
0 0 1 X X X X X 1 0 1
0 0 0 1 X X X X 1 0 0
0 0 0 0 1 X X X 0 1 1
0 0 1 0 0 1 X X 0 1 0
0 1 0 0 0 0 1 X 0 0 1
1 0 0 0 0 0 0 1 0 0 0
VHDL VERILOG
library IEEE; module encod(y, i);
use IEEE.STD_LOGIC_1164.ALL; input [7:0] i;
use IEEE.STD_LOGIC_ARITH.ALL; output [2:0] y;
use IEEE.STD_LOGIC_UNSIGNED.ALL; reg [2:0] y;
entity enc is always @(i)
Port ( i : in std_logic_vector(7 downto 0); begin
y : out std_logic_vector(2 downto 0)); casex (y)
end enc; 8'b1XXXXXXX:y=3'd7;
architecture Behavioral of enc is 8'b01XXXXXX:y=3'd6;
begin 8'b001XXXXX:y=3'd5;
process(i) 8'b0001XXXX:y=3'd4;
begin 8'b00001XXX:y=3'd3;
if i(7)='1' then y<="111"; 8'b000001XX:y=3'd2;
elsif i(6)='1' then y<="110"; 8'b0000001X:y=3'd1;
elsif i(5)='1' then y<="101"; 8’b00000001:y=3'd0;
elsif i(4)='1' then y<="100"; endcase
elsif i(3)='1' then y<="011"; end
elsif i(2)='1' then y<="010"; endmodule
elsif i(1)='1' then y<="001";
elsif i(0)='1' then y<="000";
else
y<="111";
end if;
end process;
end Behavioral;
7. 3:8 DECODER:
INPUT
OUTPUT
s s(1) s(0) y(7) y(6) y(5) y(4) y(3) y(2) y(1) y(0)
(2)
0 0 0 0 0 0 0 0 0 0 1
0 0 1 0 0 0 0 0 0 1 0
0 1 0 0 0 0 0 0 1 0 0
0 1 1 0 0 0 0 1 0 0 0
1 0 0 0 0 0 1 0 0 0 0
1 0 1 0 0 1 0 0 0 0 0
1 1 0 0 1 0 0 0 0 0 0
1 1 1 1 0 0 0 0 0 0 0
VHDL VERILOG
library IEEE; module decod(s, y);
use IEEE.STD_LOGIC_1164.ALL; input [2:0] s;
use IEEE.STD_LOGIC_ARITH.ALL; output [7:0] y;
use IEEE.STD_LOGIC_UNSIGNED.ALL; reg [7:0]y;
entity dec1 is always @(s)
Port ( s : in std_logic_vector (2 downto 0); begin
y : out std_logic_vector (7 downto 0)); case (s)
end dec1; 3'd0:y=8'd1;
architecture Behavioral of dec1 is 3'd1:y=8'd2;
begin 3'd2:y=8'd4;
process(s) 3'd3:y=8'd8;
begin 3'd4:y=8'd16;
case(s) is 3'd5:y=8'd32;
when "000" =>y<="00000001" ; 3'd6:y=8'd64;
when "001" =>y<="00000010"; 3’d7: y=8'd128;
when "010" =>y<="00000100"; endcase
when "011" =>y<="00001000"; end
when "100" =>y<="00010000"; endmodule
when "101" =>y<="00100000";
when "110" =>y<="01000000";
when "111" =>y<="10000000";
when others=>null;
end case;
end process;
end Behavioral;
8. TWO-BIT COMPARATOR:
TRUTH TABLE:
A1 A0 B1 B0 Y1 (A > B) Y2 (A = B) Y3 (A < B)
0 0 0 0 0 1 0
0 0 0 1 0 0 1
0 0 1 0 0 0 1
0 0 1 1 0 0 1
0 1 0 0 1 0 0
0 1 0 1 0 1 0
0 1 1 0 0 0 1
0 1 1 1 0 0 1
1 0 0 0 1 0 0
1 0 0 1 1 0 0
1 0 1 0 0 1 0
1 0 1 1 0 0 1
1 1 0 0 1 0 0
1 1 0 1 1 0 0
1 1 1 0 1 0 0
1 1 1 1 0 1 0
VHDL VERILOG
library IEEE; module comparator (a,b,y);
use IEEE.STD_LOGIC_1164.ALL; input [1:0] a,b;
use IEEE.STD_LOGIC_ARITH.ALL; output [2:0] y;
use IEEE.STD_LOGIC_UNSIGNED.ALL; reg [2:0] y;
entity comp is always@(a,b)
Port ( a,b : in std_logic_vector (1 downto 0); begin
y : out std_logic_vector (2 downto 0)); if(a==b)
end comp; y=3’b010;
architecture Behavioral of comp is else if(a>b)
begin y=3’b100;
process(a,b) else
begin y=3’b001;
if (a>b) then end
y<=”100”; endmodule
elsif (a<b) then
y<= “001”;
else
y<= “010”;
end if;
end process;
end Behavioral;
Inputs Outputs
0 0 0 0 0 0 0 0
EXPRESSIONS: G(0)=B(0) B(1)
0 0 0 1 0 0 0 1
0 0 1 0 0 0 1 1 G(1)=B(1) B(2)
G(2)=B(2) B(3)
0 0 1 1 0 0 1 0
G(3)=B(3)
0 1 0 0 0 1 1 0
0 1 0 1 0 1 1 1
0 1 1 0 0 1 0 1
0 1 1 1 0 1 0 0
1 0 0 0 1 1 0 0
1 0 0 1 1 1 0 1
1 0 1 0 1 1 1 1
1 0 1 1 1 1 1 0
1 1 0 0 1 0 1 0
1 1 0 1 1 0 1 1
1 1 1 0 1 0 0 1
1 1 1 1 1 0 0 0
VHDL VERILOG
library IEEE; module bgv(b, g);
use IEEE.STD_LOGIC_1164.ALL; input [3:0] b;
use IEEE.STD_LOGIC_ARITH.ALL; output [3:0] g;
use IEEE.STD_LOGIC_UNSIGNED.ALL; reg[3:0]g;
entity Binary_Gray is always@(b)
port( b: in std_logic_vector(3 downto 0); begin
g: out std_logic_vector(3 downto 0)); g[0]=b[0]^b[1];
end binary_gray; g[1]=b[1]^b[2];
architecture behavioral of Binary_gray is g[2]=b[2]^b[3];
begin g[3]=b[3];
g(3)<= b(3); end
g(2)<= b(3) xor b(2); endmodule
g(1)<= b(2) xor b(1);
g(0)<= b(1) xor b(0);
end behavioral;
10.a) SR FLIP-FLOP:
TRUTH TABLE:-
VHDL VERILOG
library IEEE; module srff(clk,rst, sr,q,qb);
use IEEE.STD_LOGIC_1164.ALL; input clk,rst;
use IEEE.STD_LOGIC_ARITH.ALL; input [1:0] sr;
use IEEE.STD_LOGIC_UNSIGNED.ALL; output q,qb;
entity srff is reg q,qb;
Port ( s,r,clk,rst : in STD_LOGIC; always@(posedge clk)
q : out STD_LOGIC; begin
qb : out STD_LOGIC); if(rst==1)
end srff; begin
architecture Behavioral of srff is q=1’b0;qb=1’b1;
begin end
process(clk,rst) else
begin begin
if(rst='1')then case(sr)
q<='0';qb<='1'; 2'b00:begin q=q;qb=qb; end
elsif rising_edge(clk)then 2'b01:begin q=1’b0;qb=1’b1;end
if(s='0' and r='0')then 2'b10:begin q=1’b1;qb=1’b0;end
q<=q;qb<=qb; 2'b11:begin q=1’bz;qb=1’bz;end
elsif(s='0' and r='1')then default:begin q=1’b0;qb=1’b1;end
q<='0';qb<='1'; endcase
elsif(s='1' and r='0')then end
q<='1';qb<='0'; end
elsif(s='1' and r='1')then endmodule
q<='X';qb<='X';
end if;
end if;
end process;
end Behavioral;
TRUTH TABLE:
Qn+1
Reset D Clock Qn+1 bar
1 0 X 0 1
0 0 Clk 0 1
0 1 clk 1 0
VHDL VERILOG
library IEEE; module dff(clk,rst,d,q,qb);
use IEEE.STD_LOGIC_1164.ALL; input clk;
use IEEE.STD_LOGIC_ARITH.ALL; input rst,d;
use IEEE.STD_LOGIC_UNSIGNED.ALL; output q,qb;
entity dff is reg q, qb;
Port ( d,res,clk : in STD_LOGIC; always@(posedge clk)
q: inout STD_LOGIC; begin
qb : out STD_LOGIC); if(rst==1)
end dff; begin
architecture Behavioral of dff is q=1’b0;
begin end
process(clk) else
begin begin
if (res ='1') then q=d;
q<='0'; end
elsif rising_edge(clk) then qb=~q;
q<=d; end
end if; endmodule
qb<= not q;
end process;
end Behavioral;
TRUTH TABLE:
0 1 0 Clk 1 0 Set
0 1 1 clk Qn bar Qn Toggle
VHDL VERILOG
library IEEE; module jkff(jk, clk,rst, q,qb);
use IEEE.STD_LOGIC_1164.ALL; input [1:0]jk;
use IEEE.STD_LOGIC_ARITH.ALL; input clk,rst;
use IEEE.STD_LOGIC_UNSIGNED.ALL; output q,qb;
entity jk is reg q,qb;
Port ( j,k,clk,reset : in STD_LOGIC; wire clkd;
q : inout STD_LOGIC; reg [20:0] COUNT;
qb : out STD_LOGIC); initial COUNT=0;
end jk; assign clkd=COUNT[20];
always @(posedge clk)
architecture Behavioral of jk is begin
signal div:std_logic_vector(22 downto 0); COUNT = COUNT + 1;
signal clkd:std_logic; end
begin always@(posedge clkd)
---------------------------------------- begin
process(clk) if(rst==1)
begin begin
if rising_edge(clk) then ----CLOCK DIVIDER q=1’b0;
div<= div+1; end
end if; else
end process; begin
clkd<=div(22); case(jk)
---------------------------------------- 2'b00:q=q;
-----for simulation clkd is replaced by clk 2'b01:q=1’b0;
process(clkd,reset) 2'b10:q=1’b1;
begin default:q=~(q);
if(reset='1')then endcase
q<= '0'; end
elsif rising_edge(clkd)then qb=~q;
if(j='0' and k='0')then end
q<= q; endmodule
elsif(j='0' and k='1')then
q<= '0';
elsif(j='1' and k='0')then
q<= '1';
elsif(j='1' and k='1')then
q<= not q;
end if;
end if;
10 d) T FLIP-FLOP:
TRUTHTABLE:
Reset T Clock Qn+1 Qn+1 bar
1 X X 0 1
0 0 Clk Qn Qn bar
0 1 Clk Qn bar Qn
VHDL VERILOG
library IEEE; module tff(clk,rst,t, q,qb);
use IEEE.STD_LOGIC_1164.ALL; input clk,rst,t;
use IEEE.STD_LOGIC_ARITH.ALL; output q,qb;
use IEEE.STD_LOGIC_UNSIGNED.ALL; reg q,qb;
entity tf is reg [19:0] COUNT;
Port ( t,clk,rst : in STD_LOGIC; wire clkd;
q : inout STD_LOGIC; initial COUNT=0;
qb : out STD_LOGIC); assign clkd=COUNT[19];
always @(posedge clk)
end tf; begin
COUNT = COUNT + 1;
architecture Behavioral of tf is end
signal div:std_logic_vector(22 downto 0); always@(posedge clkd)
signal clkd:std_logic; begin
begin if(rst==1)
---------------------------------------------- begin
process(clk) q=0;
begin end
if rising_edge(clk)then ----CLOCK DIVIDER else
div<= div+1; begin
end if; case(t)
end process; 1'b0: q=q;
clkd<=div(20); 1'b1: q=~(q);
---------------------------------------------- endcase
-----for simulation clkd is replaced by clk end
process(clkd,rst) qb=~q;
begin end
if(rst='1')then endmodule
q<='0';
elsif rising_edge(clkd) then
if( t ='1') then
q<= not q;
else
q<=q;
end if;
end if;
qb<=not q;
end process;
end Behavioral;
RST CLOCK QD QC QB QA
1 ↑ 0 0 0 0
0 ↑ 0 0 0 0
0 ↑ 0 0 0 1
0 ↑ 0 0 1 0
0 ↑ 0 1 0 0
0 ↑ 0 1 0 1
0 ↑ 0 1 1 0
0 ↑ 0 1 1 1
0 ↑ 1 0 0 0
0 ↑ 1 0 0 1
0 ↑ 1 0 1 0
0 ↑ 1 0 1 1
0 ↑ 1 1 0 0
0 ↑ 1 1 0 1
0 ↑ 1 1 1 0
0 ↑ 1 1 1 1
VHDL VERILOG
library IEEE; module asynup(clk,rst,q);
use IEEE.STD_LOGIC_1164.ALL; input clk,rst;
use IEEE.STD_LOGIC_ARITH.ALL; output [3:0] q;
use IEEE.STD_LOGIC_UNSIGNED.ALL; reg [3:0] q;
entity up is reg [22:0] COUNT;
Port ( rs,clk : in STD_LOGIC; wire clkd;
q : inout std_logic_vector (3 downto 0)); initial COUNT=0;
end up; assign clkd=COUNT[22];
architecture Behavioral of up is always @(posedge clk)
signal div:std_logic_vector(22 downto 0); begin
signal temp:std_logic_vector (3 downto 0); COUNT = COUNT + 1;
signal clkd:std_logic; end
begin always@(posedge clkd)
------------------------------- begin
process(clk) if(rst==1)
begin begin
if rising_edge(clk)then ----CLOCK DIVIDER q=4'b0000; /*FOR ASYNCRONOUS
div<= div+1; DOWN COUNTER CHANGE q=4'b1111*/
end if; end
end process; else
clkd<=div(22); begin
-----for simulation clkd is replaced by clk q=q+1; /*FOR ASYNCRONOUS DOWN
process(clkd,rs) COUNTER CHANGE Q=Q-1*/
begin end
if(rs='1')then temp<=(others=>'0'); end
-----for down counter temp<="1111"; endmodule
elsif rising_edge(clkd ) then
temp<=temp+1;
------for down counter temp<= temp-1;
Dept. of E&C, Ekalavya Institute of Tech. 24
HDL Lab Manual
q<= temp;
end if;
end process;
end Behavioral;
TRUTHTABLE :
RST CLOCK QD QC QB QA
1 ↑ 0 0 0 0
0 ↑ 0 0 0 0
0 ↑ 0 0 0 1
0 ↑ 0 0 1 0
0 ↑ 0 1 0 0
0 ↑ 0 1 0 1
0 ↑ 0 1 1 0
0 ↑ 0 1 1 1
0 ↑ 1 0 0 0
0 ↑ 1 0 0 1
VHDL VERILOG
VHDL VERILOG
VHDL VERILOG
library IEEE; module aluver(a,b,en,op,c,d,cy);
use IEEE.STD_LOGIC_1164.ALL; input [31:0] a,b;
use IEEE.STD_LOGIC_ARITH.ALL; input en;
use IEEE.STD_LOGIC_UNSIGNED.ALL; input [3:0] op;
entity alu is output [31:0] c,d;
Port ( a,b : in std_logic_vector(31 downto 0); output cy;
c,d: out std_logic_vector(31 downto 0); reg [31:0] c,d;
op: in std_logic_vector(3 downto 0); reg [32:0]cl;
en: in std_logic; reg [63:0]e;
cy: out std_logic); always @(a,b,en,op,e,cl)
end alu; begin
architecture Behavioral of alu is if (en==1)
signal c1:std_logic_vector(32 downto 0); case (op)
signal e:std_logic_vector(63 downto 0); 4'b0001:cl=a+b;
begin 4'b0010:if (a<b)
process (a,b,en,op,e,c1) begin
begin c=b-a;
if en='1' then end
case op is else
when "0001"=>c1<=('0'& a)+('0'& b); begin
c<=c1(31 downto 0); c=a-b;
cy<=c1(32); end
when"0010"=>if(a<b)then c<=b-a; 4'b0011:c=~a;
else c<=a-b; 4'b0100:e=a*b;
end if; 4'b0101:c=a&b;
when"0011"=>c<=not a; 4'b0110:c=~(a&b);
when"0100"=>e<=a*b; 4'b0111:c=a^b;
d<=e(63 downto 32); endcase
c<=e(31 downto 0); else
when"0101"=>c<=a and b; begin
when"0110"=>c<=a nand b; c=32'bZ;
when"0111"=>c<=a xor b; end
when “1000”=>c<=a or b; end
when others=>null; endmodule
end case;
else
c<=(others=>'Z');
d<=(others=>'Z');
end if;
end process;
end behavioral;
INTERFACING PROGRAMS
NOTE: The following procedure is common for Saw tooth, Ramp, Triangular and Square wave programs.
Procedure:
1. Make the connection between FRC5 of the FPGA board to the DAC connector of the VTU
card2.
2. Make the connection between FRC1 of the FPGA board to the Dip switch connector of the VTU
card2.
3. Connect the downloading cable and power supply to the FPGA board.
4. Then open the Xilinx iMPACT software (refer ISE flow) select the slave serial mode and select
the respective BIT file and click program.
5. Make the reset switch on (active low) and analyze the data.
b. Write a VHDL code to generate Stair case waveforms using DAC.
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity triangular_wave is
Port ( clk : in std_logic;
rst : in std_logic;
dac_out : out std_logic_vector(0 to 7));
end triangular_wave ;
architecture Behavioral of triangular_wave is
signal counter : std_logic_vector(0 to 8);
signal temp : std_logic_vector(3 downto 0);
signal en :std_logic;
begin
process(clk)
begin
if rising_edge(clk) then
temp <= temp + '1' ;
end if;
end process;
process(temp(3))
begin
if rst='1' then
counter <= "000000000";
elsif rising_edge(temp(3)) then
counter <= counter + 1 ;
if counter(0)='1' then
dac_out <=counter(1 to 8);
else
dac_out <=not(counter(1 to 8));
end if;
end if;
end process;
end Behavioral;
Library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.STD_LOGIC_UNSIGNED.all;
Library UNISIM;
use UNISIM.vcomponents.all;
entity dcmotor is
generic(bits : integer := 8 ); -- number of bits used for duty cycle.
begin
-- select the appropriate lines for setting frequency
CLK_DIV: process (CLK, DIV_REG) -- clock divider
begin
if (CLK'event and CLK='1') then
DIV_REG <= DIV_REG + 1;
end if;
end process;
DDCLK<=DIV_REG(12);
---------------------------- END OF CLOCK DIVIDER -------------------------------------------------
tick <= row(0) and row(1) and row(2) and row(3);
process(tick)
begin
if falling_edge(tick) then
case row is
when "1110" => duty_cycle <= 255 ; --motor speed 1
when "1101" => duty_cycle <= 200 ; --motor speed 2
when "1011" => duty_cycle <= 150 ; --motor speed 3
when "0111" => duty_cycle <= 100 ; --motor speed 4
when others => duty_cycle <= 100;
end case;
end if;
end process;
process(DDCLK, reset)
begin
if reset = '0' then
counter <= (others => '0');
PWM<="01";
elsif (DDCLK'event and DDCLK = '1') then
counter <= counter + 1;
if counter >= duty_cycle then
pwm(1) <= '0';
else
pwm(1) <= '1';
end if; end if;
end process;
rly<=DIR --motor direction control
end dcmotor1;
Procedure:
1. Make the connection between FRC9 of the FPGA board to the DC motor connector of
the VTU card2.
2. Make the connection between FRC7 of the FPGA board to the Keyboard connector of
the VTU card2.
3. Make the connection between FRC1 of the FPGA board to the Dip switch connector of
the VTU card2.
4. Connect the downloading cable and power supply to the FPGA board.
5. Then open the Xilinx iMPACT software (refer ISE flow) select the slave serial mode
and select the respective BIT file and click program.
6. Make the reset switch on (active low).
7. Press the HEX keys and analyze the speed changes.
entity STEPPER is
Port ( dout : out std_logic_vector(3 downto 0);
clk,reset: in std_logic;
row:in std_logic_vector(1 downto 0);
dir:in std_logic);
end STEPPER;
begin
process(clk)
begin
if rising_edge (clk) then
clk_div <= clk_div + '1';
end if;
end process;
begin
if reset='0' then
shift_reg <= "1001";
elsif rising_edge(clk_int) then
if dir='0' then
shift_reg <= shift_reg(0) & shift_reg(3 downto 1);
else
shift_reg<=shift_reg(2 downto 0) & shift_reg(3);
end if;
end if;
end process;
dout <= shift_reg;
end Behavioral;
Procedure:
1. Make the connection between FRC9 of the FPGA board to the Stepper motor
connector of
the VTU card2.
2. Make the connection between FRC7 of the FPGA board to the Keyboard connector of
the
VTU card2.
3. Make the connection between FRC1 of the FPGA board to the Dip switch connector of
the
VTU card2.
4. Connect the downloading cable and power supply to the FPGA board.
5. Then open the Xilinx iMPACT software (refer ISE flow) select the slave serial mode
and
select the respective BIT file and click program.
6. Make the reset switch on (active low).
7. Press the HEX keys and analyze the speed changes.
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity extlight is
Port ( cntrl1,cntrl2 : in std_logic;
light : out std_logic);
end extlight;
begin
light<= cntrl1 XOR cntrl2 ;
end Behavioral;
Procedure:
1. Make the connection between FRC9 of the FPGA board to the External light
connector of the VTU card2.
2. Make the connection between FRC1 of the FPGA board to the Dip switch connector of
the VTU card2.
3. Connect the downloading cable and power supply to the FPGA board.
4. Then open the Xilinx IMPACT software (refer ISE flow) select the slave serial mode
and select the respective BIT file and click program.
5. Make the reset switch on (active low) and analyze the data.