D Flip-Flop: VHDL Code
D Flip-Flop: VHDL Code
VHDL CODE
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity d_ff is
end d_ff;
begin
process(d,clk,q)
begin
end if;
end process;
end Behavioral;
TEST BENCH
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
USE ieee.std_logic_unsigned.all;
USE ieee.numeric_std.ALL;
ENTITY d_ff_tb_vhd IS
END d_ff_tb_vhd;
END COMPONENT;
--Inputs
--BiDirs
SIGNAL q : std_logic;
BEGIN
d => d,
q => q
);
tb : PROCESS
BEGIN
d<='1';
d<='0';
d<='1';
d<='0';
wait;
2
END PROCESS;
END;
SR FLIP-FLOP
VHDL CODE
entity SR_FF is
end SR_FF;
signal qtmp:std_logic:='0';
begin
process(s,r,clk)
begin
if(rising_edge(clk))then
qtmp<=qtmp;
qtmp<='1';
qtmp<='0';
else
qtmp<='Z';
3
end if;
end if;
end process;
q<=qtmp;
end Behavioral;
(OR)
entity SR_FF is
end SR_FF;
begin
process(s,r,clk)
begin
if(rising_edge(clk))then
Q<=Q;
Q<='1';
Q<='0';
else
Q<='Z';
end if;
end if;
end process;
end Behavioral;
(OR)
entity SR_FF is
4
Port ( CLK,S,R : in STD_LOGIC; Q : inout STD_LOGIC:=’0’);
end SR_FF;
begin
process(s,r,clk)
begin
if(rising_edge(clk))then
end if;
end process;
end Behavioral;
TEST BENCH
ENTITY sr_ff_tb_vhd IS
END sr_ff_tb_vhd;
COMPONENT SR_FF
END COMPONENT;
--Inputs
--Outputs
SIGNAL Q : std_logic;
BEGIN
S => S,
R => R,
5
CLK => CLK,
Q => Q
);
tb : PROCESS
BEGIN
s<='0';r<='1';
s<='1';r<='0';
s<='0';r<='0';
s<='0';r<='1';
wait;
END PROCESS;
END;
JK FLIP-FLOP
VHDL CODE
6
entity JK_FF is
end JK_FF;
signal qtmp:std_logic:='0';
begin
process(j,k,clk)
begin
if(rising_edge(clk))then
qtmp<=qtmp;
qtmp<='1';
qtmp<='0';
else
qtmp<=not qtmp;
end if;
end if;
end process;
q<=qtmp;
end Behavioral;
TEST BENCH
ENTITY jk_ff_tb_vhd IS
END jk_ff_tb_vhd;
COMPONENT JK_FF
7
clk : IN std_logic; q : OUT std_logic );
END COMPONENT;
SIGNAL q : std_logic;
BEGIN
j => j,
k => k,
q => q );
tb : PROCESS
BEGIN
j<='0'; k<='1';
j<='1';k<='1';
j<='1';k<='0';
j<='0';k<='0';
j<='0';k<='1';
wait;
END PROCESS;
END;
8
T FLIP-FLOP
VHDL CODE
entity T_FF is
end T_FF;
signal qtmp:std_logic:='0';
begin
process(t,clk)
begin
if(rising_edge(clk))then
if(t='0')then
qtmp<=qtmp;
else
qtmp<=not qtmp;
end if;
end if;
end process;
q<=qtmp;
end Behavioral;
9
TEST BENCH
ENTITY t_ff_tb_vhd IS
END t_ff_tb_vhd;
COMPONENT T_FF
END COMPONENT;
SIGNAL q : std_logic;
BEGIN
t => t,
q => q
);
tb : PROCESS
BEGIN
t<='1';
t<='0';
t<='1';
t<='0';
wait;
10
END PROCESS;
END;
end T_FF;
begin
process(t,clk)
begin
if(rising_edge(clk))then
if(t='0')then
q<=q;
else
q<= not q;
end if;
end if;
end process;
end Behavioral;
11
end T_FF;
begin
process(t,clk)
variable qtmp:std_logic:='0';
begin
if(rising_edge(clk))then
if(t='0')then
qtmp:=qtmp;
else
qtmp:=not qtmp;
end if;
end if;
end process;
end Behavioral;
NOTE: Draw logic symbol, logic ckt, truth table for D, SR, JK, T flip-flops.
entity SYN3_UP is
end SYN3_UP;
12
begin
process(clk,rst)
begin
if(rst='1')then
qtmp<="000";
else
qtmp<=qtmp+1;
end if;
end if;
end process;
q<=qtmp;
end Behavioral;
TEST BENCH
ENTITY syn3_up_tb_vhd IS
END syn3_up_tb_vhd;
COMPONENT SYN3_UP
END COMPONENT;
BEGIN
13
clk => clk,
q => q
);
tb : PROCESS
BEGIN
rst<='0';
wait;
END PROCESS;
END;
entity SYN3_UP is
end SYN3_UP;
begin
14
process(clk,rst)
begin
if(rst='1')then
qtmp:="000";
else
if(falling_edge(clk))then
qtmp:=qtmp+1;
end if;
end if;
q<=qtmp;
end process;
end Behavioral;
entity SYN4_DOWN is
rst : in STD_LOGIC;
end SYN4_DOWN;
begin
process(clk,rst)
begin
if(rst='1')then
15
qtmp<="0000";
else
if(falling_edge(clk))then
qtmp<=qtmp-1;
end if;
end if;
end process;
q<=qtmp;
end Behavioral;
TEST BENCH
entity BCD_SYN is
rst : in STD_LOGIC;
end BCD_SYN;
16
architecture Behavioral of BCD_SYN is
begin
process(clk,rst)
begin
if(rst='1')then
qtmp<="0000";
else
if(falling_edge(clk))then
if(qtmp<9)then
qtmp<=qtmp+1;
else
qtmp<="0000";
end if;
end if;
end if;
end process;
q<=qtmp;
end Behavioral;
TEST BENCH
17
3-BIT SYNCHRONOUS UP/DOWN COUNTER
VHDL CODE
entity SYN3_UP_DOWN is
end SYN3_UP_DOWN;
begin
process(clk,rst,up_downbar)
begin
if(rst='1')then
qtmp<="000";
else
if(falling_edge(clk))then
if(up_downbar='1')then
qtmp<=qtmp+1;
else
qtmp<=qtmp-1;
end if;
18
end if;
end if;
end process;
q<=qtmp;
end Behavioral;
TEST BENCH
ENTITY syn3_up_down_tb_vhd IS
END syn3_up_down_tb_vhd;
COMPONENT SYN3_UP_DOWN
END COMPONENT;
BEGIN
q => q
);
tb : PROCESS
BEGIN
19
wait for 30 ns;
rst<='0';
up_downbar<='1';
wait;
END PROCESS;
END;
entity T_FF is
end T_FF;
begin
process(t,clk)
begin
if(falling_edge(clk))then
if(t='0')then
q<=q;
20
else
q<=not q;
end if;
end if;
end process;
entity ASYN3_UP is
clk : in STD_LOGIC;
end ASYN3_UP;
component t_ff
end component;
begin
end structural;
TEST BENCH(counter)
ENTITY asyn3_up_tb_vhd IS
END asyn3_up_tb_vhd;
COMPONENT ASYN3_UP
21
PORT(t : in STD_LOGIC_VECTOR (2 downto 0);
END COMPONENT;
BEGIN
q => q
);
tb : PROCESS
BEGIN
wait;
END PROCESS;
END;
NOTE: for all counters draw state diagram and logic diagram
22
FREQUENCY DIVIDER(DIVIDE BY 8)
VHDL CODE
entity frequency_divider_by8 is
end frequency_divider_by8;
begin
process (clk)
begin
q<= q + 1;
end if;
end process ;
end behavioral;
TEST BENCH
ENTITY frequency_divide_by8_tb_vhd IS
END frequency_divide_by8_tb_vhd;
COMPONENT frequency_divider_by8
END COMPONENT;
BEGIN
23
uut: frequency_divider_by8 PORT MAP(clk => clk, out_clk => out_clk );
tb : PROCESS
BEGIN
wait;
FREQUENCY DIVIDER(DIVIDE BY 4)
VHDL CODE
entity frequency_divider_by4 is
end frequency_divider_by4;
begin
process (clk)
begin
q<= q + 1;
end if;
end process ;
end behavioral;
24
NOTE: for divide by 8 freq divider draw 3-bit asynchronous counter logic diag.
for divide by 4 freq divider draw 2-bit asynchronous counter logic diag.
25
serout<=tmp(0);
end Behavioral;
TEST BENCH
ENTITY SISO_RIGHT_8bit_tb_vhd IS
END SISO_RIGHT_8bit_tb_vhd;
ARCHITECTURE behavior OF SISO_RIGHT_8bit_tb_vhd IS
COMPONENT SISO_RIGHT_8bit
PORT(clk : IN std_logic; serin : IN std_logic; serout : OUT std_logic );
END COMPONENT;
SIGNAL clk : std_logic := '1';
SIGNAL serin : std_logic := '0';
SIGNAL serout : std_logic;
BEGIN
uut: SISO_RIGHT_8bit PORT MAP(clk => clk, serin => serin, serout => serout);
clk<=not clk after 25 ns;
tb : PROCESS
BEGIN
serin<='1';
wait for 50 ns;
serin<='0';
wait for 50 ns;
serin<='0';
wait for 50 ns;
serin<='1';
26
wait;
END PROCESS;
END;
27
tmp(7-i)<=tmp(6-i);
end loop;
tmp(0)<=serin;
end if;
end process;
end Behavioral;
TEST BENCH
Refer 8-bit SISO right shift register test bench (no changes)
28
pout<=tmp;
end Behavioral;
TEST BENCH
ENTITY sipo_left_4bit_tb_vhd IS
END sipo_left_4bit_tb_vhd;
ARCHITECTURE behavior OF sipo_left_4bit_tb_vhd IS
COMPONENT sipo_left_4bit
PORT(serin : IN std_logic; clk : IN std_logic;
pout : OUT std_logic_vector(3 downto 0) );
END COMPONENT;
SIGNAL serin : std_logic := '1';
SIGNAL clk : std_logic := '1';
SIGNAL pout : std_logic_vector(3 downto 0);
BEGIN
uut: sipo PORT MAP(serin => serin,clk => clk, pout => pout );
clk<=not clk after 25 ns;
tb : PROCESS
BEGIN
wait for 500 ns;
serin<='0';
wait;
END PROCESS; END;
29
4-BIT PISO RIGHT SHIFT REGISTER
VHDL CODE
entity piso_right_4bit is
loadbar_shift : in STD_LOGIC;
end piso_right_4bit;
begin
process(clk,loadbar_shift,pin)
begin
if(rising_edge(clk))then
if(loadbar_shift='0')then
tmp<=pin;
else
sout<=tmp(0);
30
end if;
end if;
end process;
end Behavioral;
TEST BENCH
ENTITY piso_right_4bit _tb_vhd IS
END piso_right_4bit _tb_vhd;
ARCHITECTURE behavior OF piso_right_4bit _tb_vhd IS
COMPONENT pis
PORT(clk : IN std_logic; loadbar_shift : IN std_logic;
pin : IN std_logic_vector(3 downto 0); sout : OUT std_logic );
END COMPONENT;
SIGNAL clk : std_logic := '1';
SIGNAL loadbar_shift : std_logic := '0';
SIGNAL pin : std_logic_vector(3 downto 0) := "1111";
SIGNAL sout : std_logic;
BEGIN
uut: pis PORT MAP(clk => clk, loadbar_shift => loadbar_shift, pin => pin,
sout => sout );
clk<=not clk after 25 ns;
tb : PROCESS
BEGIN
wait for 70 ns;
loadbar_shift<='1';
wait for 400 ns;
31
loadbar_shift<='0';
pin<="1011";
wait for 70 ns;
loadbar_shift<='1';
wait;
END PROCESS;
JK to T flip-flop conversion
VHDL CODE
entity JK2T is
Port ( t : in STD_LOGIC;
j : inout STD_LOGIC;
k : inout STD_LOGIC;
clk : in STD_LOGIC;
q : inout STD_LOGIC:='0');
end JK2T;
architecture Behavioral of JK2T is
begin
32
process(t,clk,j,k)
begin
if(clk'event and clk='1')then
j<=t;
k<=t;
if(j='0' and k='0')then
q<=q;
elsif(j='1' and k='1')then
q<=not q;
end if;
end if;
end process;
end Behavioral;
TEST BENCH
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
USE ieee.std_logic_unsigned.all;
USE ieee.numeric_std.ALL;
ENTITY JK2T_TB_vhd IS
END JK2T_TB_vhd;
ARCHITECTURE behavior OF JK2T_TB_vhd IS
- Component Declaration for the Unit Under Test (UUT)
COMPONENT JK2T
PORT(t : IN std_logic;clk : IN std_logic; j : INOUT std_logic; k : INOUT std_logic;
33
q : INOUT std_logic);
END COMPONENT;
--Inputs
SIGNAL t : std_logic := '0';
SIGNAL clk : std_logic := '0';
--BiDirs
SIGNAL j : std_logic;
SIGNAL k : std_logic;
SIGNAL q : std_logic;
BEGIN
-- Instantiate the Unit Under Test (UUT)
uut: JK2T PORT MAP(
t => t,
j => j,
k => k,
clk => clk,
q => q);
clk<=not clk after 50 ns;
tb : PROCESS
BEGIN
t<='1';
wait for 100 ns;
t<='0';
wait for 100 ns;
34
t<='1';
wait;
END PROCESS;
END;
JK to D flip-flop conversion
VHDL CODE
entity JK2D is
Port ( d : in STD_LOGIC;
j : inout STD_LOGIC;
k : inout STD_LOGIC;
clk : in STD_LOGIC;
q : inout STD_LOGIC:='0');
end JK2D;
architecture Behavioral of JK2D is
begin
process(d,clk,j,k,q)
35
begin
j<=d; k<=not d;
if(clk'event and clk='1')then
if((j='0' and k='1')or (j='1' and k='0'))then
q<=d;
end if;
end if;
end process;
end Behavioral;
TEST BENCH
ENTITY JK2D_TB_vhd IS
END JK2D_TB_vhd;
ARCHITECTURE behavior OF JK2D_TB_vhd IS
-- Component Declaration for the Unit Under Test (UUT)
COMPONENT JK2D
PORT(d : IN std_logic; clk : IN std_logic; j : INOUT std_logic;
k : INOUT std_logic; q : INOUT std_logic);
END COMPONENT;
--Inputs
SIGNAL d : std_logic := '0';
SIGNAL clk : std_logic := '0';
--BiDirs
SIGNAL j : std_logic;
SIGNAL k : std_logic;
36
SIGNAL q : std_logic;
BEGIN
-- Instantiate the Unit Under Test (UUT)
uut: JK2D PORT MAP(d => d,j => j,k => k,clk => clk,q => q);
clk<=not clk after 50 ns;
tb : PROCESS
BEGIN
d<='1';
wait for 100 ns;
d<='0';
wait for 100 ns;
d<='1';
wait;
END PROCESS;
END;
37