VHDL Lab Programs
VHDL Lab Programs
VHDL Lab Programs
VENKATRAO
Page 0 of 50
1. HALF ADDER
Aim: To design the half adder using dataflow model. Theory: A half-adder an arithmetic circuit block that can be used to add two bits. Such a circuit thus has two inputs that represent the two bits to be added and two outputs, with one producing the SUM output and the other producing the CARRY. The program gives the sum of Boolean addition of inputs a, b. It indicates if a carry is generated. Sum= A XOR B; Carry= A AND B; Schematic diagram:
P.VENKATRAO (M.Tech)
Page 1 of 50
VHDL code: library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity half_adder1 is Port ( a,b : in sum,carry : out end half_adder1; architecture Behavioral of half_adder1 is begin sum<= a xor b; carry<= a and b; end Behavioral; VHDL Test-bench: LIBRARY ieee; USE ieee.std_logic_1164.ALL; ENTITY HA_tb IS END HA_tb; ARCHITECTURE behavior OF HA_tb IS COMPONENT half_adder1
P.VENKATRAO (M.Tech) Page 2 of 50
STD_LOGIC;
STD_LOGIC);
--Inputs signal a : std_logic := '0'; signal b : std_logic := '0'; --Outputs signal sum : std_logic; signal carry : std_logic; BEGIN -- Instantiate the Unit Under Test (UUT) uut: half_adder1 PORT MAP ( a => a, b => b, sum => sum, carry => carry ); stim_proc: process begin a<='0'; b<='0'; wait for 10 ns; a<='0'; b<='1'; wait for 10 ns;
P.VENKATRAO (M.Tech)
Page 3 of 50
a<='1'; b<='0'; wait for 10 ns; a<='1'; b<='1'; wait for 10 ns; wait; end process; END; Simulated waveforms:
Figure 2. Simulated waveform of half adder. Conclusion: Half adder is implemented using VHDL in data flow model.
2. FULL ADDER
Aim: To design the full adder in dataflow model. Theory: Full adder circuit is an arithmetic circuit block that can be used to add three bits to produce a SUM and a CARRY output. Such a building block becomes a necessity when it comes to adding binary numbers with a large number of bits. The full adder circuit overcomes the limitation of the half-adder, which can be used to add two bits only. The program gives the sum of Boolean addition of inputs a, b, c. It indicates if a carry is generated. SUM= A xor B xor C CARRY= (A and B) or (B and C) or(C and A)
P.VENKATRAO (M.Tech)
Page 4 of 50
Schematic diagram:
Figure 3. Full adder circuit diagram. Truth table: Table 2: Truth Table of Full Adder
A 0 0 0 0 1 1 1 1 B 0 0 1 1 0 0 1 1 Cin 0 1 0 1 0 1 0 1 Sum 0 1 1 0 1 0 0 1 Carry 0 0 0 1 0 1 1 1
entity fulladder1 is Port ( a,b,c : in sum,carry : out end fulladder1; architecture Behavioral of fulladder1 is begin sum<=a xor b xor c; carry<= (a and b)or(b and c) or (c and a); end Behavioral; VHDL Test-bench: LIBRARY ieee; USE ieee.std_logic_1164.ALL; ENTITY fulladder_tb IS END fulladder_tb; ARCHITECTURE behavior OF fulladder_tb IS -- Component Declaration for the Unit Under Test (UUT) COMPONENT fulladder1 PORT( a : IN b : IN c : IN std_logic; std_logic; std_logic; std_logic; std_logic STD_LOGIC; STD_LOGIC);
P.VENKATRAO (M.Tech)
Page 6 of 50
--Inputs signal a : std_logic := '0'; signal b : std_logic := '0'; signal c : std_logic := '0'; --Outputs signal sum : std_logic; signal carry : std_logic; BEGIN -- Instantiate the Unit Under Test (UUT) uut: fulladder1 PORT MAP ( a => a, b => b, c => c, sum => sum, carry => carry ); stim_proc: process begin a<='0';b<='0';c<='0'; wait for 10 ns; a<='0';b<='0';c<='1' ;wait for 10 ns; a<='0';b<='1';c<='0' ;wait for 10 ns; a<='0';b<='1';c<='1' ;wait for 10 ns; a<='1';b<='0';c<='0' ;wait for 10 ns; a<='1';b<='0';c<='1' ;wait for 10 ns; a<='1';b<='1';c<='0' ;wait for 10 ns; a<='1';b<='1';c<='1' ;wait for 10 ns; wait; end process; END;
P.VENKATRAO (M.Tech) Page 7 of 50
Simulated waveform:
Figure 4.Simulated waveform for different inputs of full adder circuit diagram. Conclusion: Full adder is implemented using VHDL in data flow model.
3. COMPARATOR (4 BIT)
P.VENKATRAO (M.Tech)
Page 8 of 50
Figure 5. Magnitude comparator diagram. Truth Table: Table 3. Truth Table of comparator
Inputs A>B A=B A<B Equal 0 1 0 Greater 1 0 0 Smaller 0 0 1
VHDL Code: Library IEEE; Use IEEE.STD_LOGIC_1164.ALL; entity comparator1 is Port ( a,b : in STD_LOGIC_VECTOR (3 downto 0); STD_LOGIC);
Page 9 of 50
equal,greater,lesser : out
P.VENKATRAO (M.Tech)
end comparator1; architecture Behavioral of comparator1 is begin equal<= '1' when (a=b) else '0' ; greater<= '1' when (a>b) else '0' ; lesser<= '1' when (a<b) else '0' ; end Behavioral; VHDL Test-Bench: LIBRARY ieee; USE ieee.std_logic_1164.ALL; ENTITY comparator_tb IS END comparator_tb; ARCHITECTURE behavior OF comparator_tb IS -- Component Declaration for the Unit Under Test (UUT) COMPONENT comparator1 PORT( a : IN b : IN std_logic_vector(3 downto 0); std_logic_vector(3 downto 0); std_logic; std_logic; std_logic
P.VENKATRAO (M.Tech)
Page 10 of 50
--Inputs signal a :std_logic_vector(3 downto 0):=(others => '0'); signal b :std_logic_vector(3 downto 0):=(others => '0'); --Outputs signal equal : std_logic; signal greater : std_logic; signal lesser : std_logic;
BEGIN -- Instantiate the Unit Under Test (UUT) uut: comparator1 PORT MAP ( a => a, b => b, equal => equal, greater => greater, lesser => lesser ); stim_proc: process begin a<="0000"; b<="0000"; wait for 10 ns; a<="0001"; b<="0010"; wait for 10 ns; a<="0011"; b<="0100"; wait for 10 ns; a<="0101"; b<="0100"; wait for 10 ns; a<="0111"; b<="1000"; wait for 10 ns; wait; end process; END;
P.VENKATRAO (M.Tech)
Page 11 of 50
Simulated waveform:
Figure 6. Simulated waveform of magnitude comparator of 4- bit. Conclusion: The coding for comparator is done and output waveforms for the comparator are successfully plotted.
P.VENKATRAO (M.Tech)
Page 12 of 50
Schematic diagram:
Figure 7: Schematic of 16-bit shift register VHDL Code: library IEEE; use IEEE.STD_LOGIC_1164.ALL;
entity shiftregister1 is Port ( data : in STD_LOGIC ; clk : in STD_LOGIC; STD_LOGIC_VECTOR (15 downto 0) );
architecture Behavioral of shiftregister1 is signal temp : std_logic_vector (15 downto 0):=X"0000"; begin temp<=data & temp(15 downto 1)when(clk='1'and clk'event); output<=temp;
P.VENKATRAO (M.Tech)
Page 13 of 50
end Behavioral;
VHDL Test Bench: LIBRARY ieee; USE ieee.std_logic_1164.ALL; ENTITY SHIFT_REG_TB IS END SHIFT_REG_TB; ARCHITECTURE behavior OF SHIFT_REG_TB IS -- Component Declaration for the Unit Under Test (UUT) COMPONENT shiftregister1 PORT( data : IN clk : IN std_logic; std_logic; std_logic_vector(15 downto 0)
--Inputs signal data : std_logic := '0'; signalclk : std_logic := '0'; --Outputs signal output : std_logic_vector(15 downto 0); -- Clock period definitions
P.VENKATRAO (M.Tech) Page 14 of 50
constantclk_period : time := 10 ns; BEGIN uut: shiftregister1 PORT MAP ( data => data, clk =>clk, output => output ); clk_process :process begin clk<= '0'; wait for clk_period/2; clk<= '1'; wait for clk_period/2; end process; -- Stimulus process stim_proc: process begin data<='1'; wait for 10 ns; end process; END;
P.VENKATRAO (M.Tech)
Page 15 of 50
Simulated waveform:
Figure 8. Simulated waveforms of the shift register 16-bit. Conclusion: The shift register of 16 bit is implemented using VHDL in data flow model
P.VENKATRAO (M.Tech)
Page 16 of 50
Figure 9: Simulated diagram of Multiplexer. Truth Table: Table 4: Truth table of Multiplexer. S(0) S(1) Y 0 0 1 1 VHDL coding: library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; entity multiplexer is Port ( i : in s : in y : out end multiplexer; architecture Behavioral of multiplexer is begin with s select
P.VENKATRAO (M.Tech) Page 17 of 50
0 1 0 1
y <= i(0) when "00" , i(1) when "01" , i(2) when "10" , i(3) when "11" , 'Z' when others; end Behavioral; VHDL Test bench: LIBRARY ieee; USE ieee.std_logic_1164.ALL; USE ieee.std_logic_unsigned.all; USE ieee.numeric_std.ALL; ENTITY test_multiplexer IS END test_multiplexer; ARCHITECTURE behavior OF test_multiplexer IS COMPONENT multiplexer PORT( i : IN s : IN y : OUT ); END COMPONENT; signali :std_logic_vector(3 downto 0) := (others => '0'); signal s :std_logic_vector(1 downto 0) := (others => '0'); signal y : std_logic; BEGIN uut: multiplexer PORT MAP ( i =>i, std_logic_vector(3 downto 0); std_logic_vector(1 downto 0); std_logic
P.VENKATRAO (M.Tech)
Page 18 of 50
s => s, y => y ); stim_proc: process begin i(0)<='1';i(1)<='0';i(2)<='0';i(3)<='0';s(0)<='0';s(1) <='0';wait for 100 ns; i(0)<='0';i(1)<='1';i(2)<='0';i(3)<='0';s(0)<='1';s(1) <='0';wait for 100 ns; i(0)<='0';i(1)<='0';i(2)<='1';i(3)<='0';s(0)<='0';s(1) <='1';wait for 100 ns; i(0)<='0';i(1)<='0';i(2)<='0';i(3)<='1';s(0)<='1';s(1) <='1';wait for 100 ns; wait; end process; END; Stimulated waveforms:
Figure 10. Stimulated waveform for multiplexer. Conclusion: Multiplexer with tristating is implemented using VHDL in data flow model.
P.VENKATRAO (M.Tech)
Page 19 of 50
Schematic diagram:
P.VENKATRAO (M.Tech)
Page 20 of 50
VHDL Code: library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity decoder2x4 is Port ( en : in sel : in y : out end decoder2x4; architecture Behavioral of decoder2x4 is begin y<= "ZZZZ" when en='0' else "0001" when sel="00" else "0010" when sel="01" else "0100" when sel="10" else "1000"; End Behavioral; VHDL Test Bench: LIBRARY ieee; USE ieee.std_logic_1164.ALL; ENTITY decoder_tb IS
P.VENKATRAO (M.Tech) Page 21 of 50
END decoder_tb; ARCHITECTURE behavior OF decoder_tb IS -- Component Declaration for the Unit Under Test (UUT) COMPONENT decoder2x4 PORT( en : IN sel : IN y : OUT ); END COMPONENT; --Inputs signal en : std_logic := '0'; signalsel '0'); --Outputs signal y : std_logic_vector(3 downto 0); -- appropriate port name BEGIN -- Instantiate the Unit Under Test (UUT) uut: decoder2x4 PORT MAP ( en => en, sel =>sel, y => y ); -- Stimulus process stim_proc: process begin
P.VENKATRAO (M.Tech) Page 22 of 50
std_logic_vector(1
downto
0)
:=
(others
=>
sel<="00" ;en<='1'; wait for 100 ns; sel<="01" ;en<='1'; wait for 100 ns; sel<="10" ;en<='1'; wait for 100 ns; sel<="11" ;en<='1'; wait for 100 ns; sel<="00" ;en<='0'; wait for 100 ns; sel<="01" ;en<='1'; wait for 100 ns; sel<="10" ;en<='0'; wait for 100 ns; sel<="11" ;en<='1'; wait for 100 ns; wait; end process; END; Simulated waveform:
Conclusion:
Decoder with tristating using VHDL is implemented in data flow model.
P.VENKATRAO (M.Tech)
Page 23 of 50
7. BINARY COUNTER (4-BIT) Aim: To simulate a 4-bit binary counter using VHDL. Theory:
A counter that can change state in either direction, under the control of an up/down selector input, is known as an up/down counter. When the selector is in the up state, the counter increments its value. When the selector is in the down state, the counter decrements the count. Here we design a 4bit binary counter using behavioral model. The circuit is working with raising edge of clock. When raising edge of clock happened the count will increment Schematic diagram:
Figure 13.Circuit diagram of binary counter VHDL Code: library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; entity counter is Port (count :inoutstd_logic_vector(3 downto 0):="0000"; urd,clk :in std_logic;
P.VENKATRAO (M.Tech) Page 24 of 50
reset :in std_logic); end counter; architecture Behavioral of counter is begin process(clk,urd) begin if(clk='1' and clk'event) then if reset='1' then count<="0000"; elsif(urd='1') then count<=count+"0001"; elsif(urd='0') then count<=count-"0001"; end if; end if; end process; end Behavioral; VHDL Test Bench: LIBRARY ieee; USE ieee.std_logic_1164.ALL; USE ieee.std_logic_unsigned.all; USE ieee.numeric_std.ALL; ENTITY counter_t IS END counter_t; ARCHITECTURE behavior OF counter_t IS -- Component Declaration for the Unit Under Test (UUT) COMPONENT counter PORT(count : INOUT std_logic_vector(3 downto 0); urd : IN std_logic; clk : IN std_logic; reset :in std_logic
P.VENKATRAO (M.Tech) Page 25 of 50
); END COMPONENT; --Inputs signalurd : std_logic := '0'; signalclk : std_logic := '0'; signalreset:std_logic := '0'; --BiDirs signal count : std_logic_vector(3 downto 0); -- Clock period definitions constantclk_period : time := 20ns; BEGIN -- Instantiatethe Unit Under Test (UUT) uut: counter PORT MAP ( count => count, urd =>urd, clk =>clk, reset=> reset ); -- Clock process definitions clk_process :process begin clk<= '0'; wait for clk_period/2; clk<= '1'; wait for clk_period/2; end process; -- Stimulus process stim_proc: process begin urd<='1','0' after 300 ns; reset<='0','1' after 100 ns,'0' after 120ns; wait for clk_period*10;
P.VENKATRAO (M.Tech) Page 26 of 50
8. DECADE COUNTER
Aim: To simulate a decade counter using VHDL. Theory: The decade counter is also known as a mod-counter when it counts to ten (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9). This program builds an up/down decade counter (mod-10) which counts on each rising edge of clock pulse. Schematic Diagram:
VHDL Code: library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; entitycdecade is port( count : inoutstd_logic_vector(3 downto 0):="0000"; urd,clk :in std_logic; reset :in std_logic); endcdecade; architecture Behavioral of cdecade is begin process(clk,urd) begin if(clk='1' and clk'event) then if reset='1' then count<="0000"; elsif(urd='1') then count<=count+"0001"; if (count+"0001")>"1010" then count<="0000"; end if; elsif(urd='0') then count<=count-"0001"; if (count-"0001")>="1111" then count<="1010"; end if; end if; end if; end process; end Behavioral;
P.VENKATRAO (M.Tech) Page 28 of 50
VHDL Test Bench: LIBRARY ieee; USE ieee.std_logic_1164.ALL; ENTITY cdecade_t IS END cdecade_t; ARCHITECTURE behavior OF cdecade_t IS -- Component Declaration for the Unit Under Test (UUT) COMPONENT cdecade PORT( count : INOUT std_logic_vector(3 downto 0); urd : IN std_logic; clk : IN std_logic; reset : IN std_logic ); END COMPONENT; --Inputs signalurd : std_logic := '0'; signalclk : std_logic := '0'; signal reset : std_logic := '0'; --BiDirs signal count : std_logic_vector(3 downto 0); -- Clock period definitions constantclk_period : time := 20 ns; BEGIN -- Instantiate the Unit Under Test (UUT) uut: cdecade PORT MAP ( count => count, urd =>urd, clk =>clk, reset => reset );
P.VENKATRAO (M.Tech) Page 29 of 50
-- Clock process definitions clk_process :process begin clk<= '0'; wait for clk_period/2; clk<= '1'; wait for clk_period/2; end process; -- Stimulus process stim_proc: process begin urd<='1','0' after 300 ns; reset<='0','1' after 240 ns,'0' after 260ns; wait for clk_period*10; wait; end process; END; Simulated waveforms:
Figure 16: Simulated waveform for decade counter CONCLUSION: Binary counter is implemented using VHDL in behavioral model.
P.VENKATRAO (M.Tech)
Page 30 of 50
9(a).BCD ADDER
Aim: To design BCD adder in behavioral modeling using VHDL coding. Theory: The BCD adder counts only up to 9, when the count becomes more than 9 or carry will be generate it adds binary number 6 to it. Schematic diagram: The gate level schematic diagram for BCD adder is
Figure 17: Schematic diagram for BCD adder. VHDL coding: Library IEEE; Use IEEE.STD_LOGIC_1164.ALL; Use IEEE.STD_LOGIC_ARITH.ALL; Use IEEE.STD_LOGIC_UNSIGNED.ALL; Entity adder is Port ( a,b : in c :inout End adder; Architecture Behavioral of adder is
P.VENKATRAO (M.Tech) Page 31 of 50
begin process (a,b) variabletemp:std_logic_vector(4 downto 0):="00000"; begin temp := ('0' & a) + ('0' & b); if (temp(3 downto 0) >= "1010") then temp:= temp+ "00110"; end if; c<=temp; end process; end Behavioral;
VHDL Test bench: LIBRARY ieee; USE ieee.std_logic_1164.ALL; USE ieee.std_logic_unsigned.all; USE ieee.numeric_std.ALL; ENTITY test_bcdadder IS END test_bcdadder; ARCHITECTURE behavior OF test_bcdadder IS -- Component Declaration for the Unit Under Test (UUT)
P.VENKATRAO (M.Tech)
Page 32 of 50
COMPONENT adder PORT( a : IN b : IN ); END COMPONENT; --Inputs signal a : std_logic_vector(3 downto 0) := (others => '0'); signal b : std_logic_vector(3 downto 0) := (others => '0'); --BiDirs signal c : std_logic_vector(4 downto 0); BEGIN -- Instantiate the Unit Under Test (UUT) uut: adder PORT MAP ( a => a, b => b, c => c ); stim_proc: process begin a<="1000";b<="0011"; wait; end process; END; std_logic_vector(3 downto 0); std_logic_vector(3 downto 0); std_logic_vector(4 downto 0)
c : INOUT
P.VENKATRAO (M.Tech)
Page 33 of 50
Stimulated waveforms:
Figure 18: Stimulated waveform for BCD adder. CONCLUSION: BCD adder is implemented using VHDL in behavioral model.
9(b).BCD SUBTRACTOR
Aim: To design BCD subtractor in behavioral modeling using VHDL coding. Theory: The binary subtractor works on 2s complement form. Two's complement subtraction is the binary addition of the minuend to the 2's complement of the subtrahend (adding a negative number is the same as subtracting a positive one). Schematic diagram: The RTL gate level schematic diagram for BCD subtractor is
VHDL Coding: library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; entitybcdsubractor is Port ( a,b : in c : out endbcdsubractor; architecture Behavioral of bcdsubractor is begin process (a,b) variable temp: STD_LOGIC_VECTOR (4 downto 0):=(others=>'0'); begin temp:= ( '0' & a)-('0' & b); if (a < b) then temp:= not(temp) + "10001"; end if; c<=temp; end process; end Behavioral; VHDL Test bench: LIBRARY ieee; USE ieee.std_logic_1164.ALL; USE ieee.std_logic_unsigned.all; USE ieee.numeric_std.ALL; STD_LOGIC_VECTOR (3 downto 0); STD_LOGIC_VECTOR (4 downto 0));
P.VENKATRAO (M.Tech)
Page 35 of 50
ENTITY test IS END test; ARCHITECTURE behavior OF test IS COMPONENT bcdsubractor PORT( a : IN b : IN c : OUT ); END COMPONENT; --Inputs signal a : std_logic_vector(3 downto 0) := (others => '0'); signal b : std_logic_vector(3 downto 0) := (others => '0'); --Outputs signal c : std_logic_vector(4 downto 0); BEGIN -- Instantiate the Unit Under Test (UUT) uut: bcdsubractor PORT MAP ( a => a, b => b, c => c ); -- Stimulus process stim_proc: process begin a<="1000";b<="0111";wait for 100 ns; a<="1000";b<="1011"; wait; end process; END;
P.VENKATRAO (M.Tech) Page 36 of 50
Stimulated waveform:
Figure 20: Stimulated waveform for BCD Subtractor. Conclusion: BCD Subtractor is implemented using VHDL in behavioral flow model.
10. D FLIP-FLOP
Aim: To design a D flip-flop in behavioral flow modeling using VHDL coding. Theory: D flip-flop gives a one bit delay to the input at the rising edge of the clock. The D flipflop tracks the input, making transitions with match those of the input D. The D stands for "data"; this flip-flop stores the value that is on the data line. It can be thought of as a basic memory cell. A D flip-flop can be made from a set/reset flip-flop by tying the set to the reset through an inverter. The result may be clocked. Schematic diagram: The RTL gate level schematic diagram for flip-flop.
VHDL Coding: library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; entitydff is Port ( d : in q :inout qn : out clock : in enddff; architecture Behavioral of dff is begin process (reset,d,clock) begin if (reset='1') then q<='0'; elsif(clock='1' and clock'event) then q<=d;
P.VENKATRAO (M.Tech) Page 38 of 50
reset : in
VHDL Test bench: LIBRARY ieee; USE ieee.std_logic_1164.ALL; USE ieee.std_logic_unsigned.all; USE ieee.numeric_std.ALL; ENTITY testdff IS END testdff; ARCHITECTURE behavior OF testdff IS -- Component Declaration for the Unit Under Test (UUT) COMPONENT dff PORT( d : IN std_logic; std_logic; std_logic; std_logic; std_logic); ---Inputs signal d : std_logic := '0'; signal reset : std_logic := '0'; signal clock : std_logic := '0'; --BiDirs
P.VENKATRAO (M.Tech) Page 39 of 50
signal q : std_logic; --Outputs signalqn : std_logic; -- Clock period definitions constantclock_period : time := 10 ns; BEGIN -- Instantiate the Unit Under Test (UUT) uut: dff PORT MAP ( d => d, reset => reset, q => q, qn =>qn, clock => clock ); -- Clock process definitions clock_process :process begin clock<= '0'; wait for clock_period/2; clock<= '1'; wait for clock_period/2; end process; stim_proc: process begin d<='0';wait for 50 ns; d<='1';wait for 50 ns; d<='0';wait for 50 ns;
P.VENKATRAO (M.Tech) Page 40 of 50
d<='1';wait for 50 ns; d<='0';wait for 50 ns; d<='1';wait for 50 ns; wait; end process; END; Stimulated Wave form:
Figure 22: Stimulated wave form for D flip-flop. Conclusion: Delay flip-flop is implemented using VHDL in behavioral model.
P.VENKATRAO (M.Tech)
Page 41 of 50
Schematic diagram:
Figure 23. Internal block diagram of T-flip flop. Truth Table: Table 7: Truth table for Toggle flip-flop.
Q(t) 0 1 0 1
T 0 0 1 1
Q(t+1) 0 1 1 0
VHDL Code: library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; entitytff is Port ( t : in STD_LOGIC; STD_LOGIC; STD_LOGIC; STD_LOGIC; STD_LOGIC); clock : in q :inout qn : out
reset : in
P.VENKATRAO (M.Tech)
Page 42 of 50
endtff; architecture Behavioral of tff is begin process (t,clock,reset) begin if(reset='1' ) then q <='0'; elsif(clock='1' and clock'event)and t=1then q<= not t; end if; qn<= not q; end process; end Behavioral;
VHDL Test bench: LIBRARY ieee; USE ieee.std_logic_1164.ALL; USE ieee.std_logic_unsigned.all; USE ieee.numeric_std.ALL; ENTITY testtff IS END testtff; ARCHITECTURE behavior OF testtff IS -- Component Declaration for the Unit Under Test (UUT) COMPONENT tff PORT( t : IN std_logic; std_logic;
Page 43 of 50
clock : IN
P.VENKATRAO (M.Tech)
--Inputs signal t : std_logic := '0'; signal clock : std_logic := '0'; signal reset : std_logic := '0'; --BiDirs signal q : std_logic; --Outputs signalqn : std_logic; -- Clock period definitions constantclock_period : time := 50 ns; BEGIN -- Instantiate the Unit Under Test (UUT) uut: tff PORT MAP ( t => t, clock => clock, q => q, qn =>qn, reset => reset );
P.VENKATRAO (M.Tech)
Page 44 of 50
-- Clock process definitions clock process :process begin clock<= '0'; wait for clock_period/2; clock<= '1'; wait for clock_period/2; end process; -- Stimulus process stim_proc: process begin t<='0'; wait for 100 ns; t<='1'; wait for 100 ns; t<='0'; wait for 100 ns; t<='1'; wait for 100 ns; t<='0'; wait for 100 ns; wait; end process; END; Stimulate waveform:
Figure 24:Stimulated waveform of T-flip flop. Conclusion: Toggle flip-flop is implemented using VHDL in behavioral model.
P.VENKATRAO (M.Tech)
Page 45 of 50
12. JK FLIP-FLOP
Aim: To implement a JK FF using VHDL coding in behavioral model. Theory: The J-K flip-flop is the most versatile of the basic flip-flops. It has the input- following character of the clocked D flip-flop but has two inputs, traditionally labeled J and K. If J and K are different then the output Q takes the value of J at the next clock edge. This program builds a JK-FF which gives output according to the following truth table on each rising edge of clock pulse. Schematic diagram:
Figure 25: Schematic diagram of jk flip-flop Truth table: Table 8: Truth table for JK flip-flop.
j 0 0 0 1
k 0 1 1 1
Q(t+1) On 0 1 Qn_bar
P.VENKATRAO (M.Tech)
Page 46 of 50
VHDL Code: library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; entityjk is Port ( jkin: in std_logic_vector(1 downto 0); clock : in q : out qn: out endjk; architecture Behavioral of jk is signal state : std_logic ; begin process(jkin,clock) begin if ( clock='1' and clock'event) then casejkin is when "11" => state <= not state; when "00" => state <= state; when "01" => state <='0'; when "10" => state <= '1'; when others => null; end case; end if; q <= state;
P.VENKATRAO (M.Tech) Page 47 of 50
STD_LOGIC;
STD_LOGIC:='0'; STD_LOGIC:='1');
qn<= not state; end process; end Behavioral; VHDL Test Bench: LIBRARY ieee; USE ieee.std_logic_1164.ALL; USE ieee.std_logic_unsigned.all; USE ieee.numeric_std.ALL; ENTITY jktest IS END jktest; ARCHITECTURE behavior OF jktest IS COMPONENT jk PORT( jkin : IN clock : IN q : OUT qn : OUT ); END COMPONENT; --Inputs signaljkin : std_logic_vector(1 downto 0:= (others => '0'); signal clock : std_logic := '0'; --Outputs signal q : std_logic; signalqn : std_logic; -- Clock period definitions
P.VENKATRAO (M.Tech) Page 48 of 50
constantclock_period : time := 10 ns; BEGIN -- Instantiate the Unit Under Test (UUT) uut: jk PORT MAP ( jkin =>jkin, clock => clock, q => q, qn =>qn ); -- Clock process definitions clock_process :process begin clock<= '0'; wait for clock_period/2; clock<= '1'; wait for clock_period/2; end process;
-- Stimulus process stim_proc: process begin jkin<="00"; wait for 100 ns; jkin<="01"; wait for 100 ns; jkin<="10"; wait for 100 ns; jkin<="11"; wait for 100 ns; wait; end process; END;
P.VENKATRAO (M.Tech) Page 49 of 50
Stimulated Waveform:
Figure 26: Stimulated wave form for Jk Flop-Flop. Conclusion: JK Flip-Flop is implemented using VHDL in behavioral model.
P.VENKATRAO (M.Tech)
Page 50 of 50