esign of basic combinational and sequential
esign of basic combinational and sequential
Program:
VHDL code for D flip-flop:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity D_FLIPFLOP_SOURCE is
Q, Qb : out STD_LOGIC);
end D_FLIPFLOP_SOURCE;
begin
begin
Q <= '0';
elsif (rising_edge(CLK)) then ---this is for data flip-flop, for delay flip-flop use negative edge
Q <= D;
Qb <= not D;
end if;
end process;
end Behavioral;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity DFF_tb is
end entity;
architecture tb of DFF_tb is
component D_FLIPFLOP_SOURCE is
Q, Qb : out STD_LOGIC);
end component ;
begin
D => D,
Q => Q,
Qb => Qb);
Clock : process
begin
end process;
stim : process
begin
D <= '0';
D <= '1';
end process;
end tb;
Truth table for D flip flop:
Adder:
Program:
library ieee;
use ieee.std_logic_1164.all;
entity FA_8bit is
port(x,y : in std_logic_vector(7 downto 0);
cin : in std_logic;
sum : out std_logic_vector(7 downto 0);
co : out std_logic);
end FA_8bit;
component full_adder is
port (p,q,r:in std_logic; sm,cr: out std_logic);
end component;
begin
a0:full_adder port map (x(0),y(0),cin,sum(0),cary(0));
a1:full_adder port map (x(1),y(1),cary(0),sum(1),cary(1));
a2:full_adder port map (x(2),y(2),cary(1),sum(2),cary(2));
a3:full_adder port map (x(3),y(3),cary(2),sum(3),cary(3));
a4:full_adder port map (x(4),y(4),cary(3),sum(4),cary(4));
a5:full_adder port map (x(5),y(5),cary(4),sum(5),cary(5));
a6:full_adder port map (x(6),y(6),cary(5),sum(6),cary(6));
a7:full_adder port map (x(7),y(7),cary(6),sum(7),co);
end FA_arch;
library ieee;
use ieee.std_logic_1164.all;
entity full_adder is
port (p,q,r:in std_logic; sm,cr: out std_logic);
end full_adder;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
entity tb_FA_8bit is
end tb_FA_8bit;
component FA_8bit is
port(
cin : in std_logic;
co : out std_logic
);
end component;
-- Testbench signals
begin
x => x_tb,
y => y_tb,
co => co_tb
);
-- Stimulus process
stim_proc: process
begin
-- Test case 1: 5 + 10
-- Stop simulation
wait;
end process;
end behavior;
Multiplier(2bit):
Program:
library ieee;
use ieee.std_logic_1164.all;
entity AND2 is
port(
A,B: in BIT;
x : out BIT);
end AND2;
begin
x <= A and B;
end behavioral;
entity half_adder is
);
end half_adder;
begin
end arch;
entity multiply_struct is
);
end multiply_struct;
component AND2
port(
A,B: in BIT;
X : out BIT);
end component;
component half_adder
end component;
signal S1,S2,S3,S4:BIT;
begin
end architecture;
use ieee.std_logic_1164.all;
entity multiply_behav_tb is
end multiply_behav_tb;
architecture tb of multiply_behav_tb is
component multiply_behav is
);
end component;
begin
A => A,
B => B,
P => P);
Force:process
begin
A <= "00";
B <= "00";
A <= "00";
B <= "01";
A <= "00";
B <= "10";
A <= "00";
B <= "11";
A <= "01";
B <= "00";
A <= "01";
B <= "01";
A <= "01";
B <= "10";
A <= "01";
B <= "11";
A <= "10";
B <= "00";
A <= "10";
B <= "01";
A <= "10";
B <= "10";
A <= "10";
B <= "11";
A <= "11";
B <= "00";
A <= "11";
B <= "01";
A <= "11";
B <= "10";
A <= "11";
B <= "11";
wait;
end process;
end tb;
Program:
4 bit universal shift register:
library ieee;
use ieee.std_logic_1164.all;
entity uni_shift is
end uni_shift;
begin
case mode is
when "00" => null; -- "Do Nothing" mode: retain current flip-flop outputs
when "01" => q <= (q srl 1) or (sr_in & "000") ; -- Shift Right Serial Input
when "10" => q <= (q sll 1) or ("000" & sl_in) ; -- Shift Left Serial Input
end case;
end if;
end process;
end behav;
Test-bench:
library ieee;
use ieee.std_logic_1164.all;
entity tb_uni_shift is
end tb_uni_shift;
component uni_shift
port (
);
end component;
begin
q => q_tb
);
clk_process : process
begin
while now < 200 ns loop
end loop;
wait;
end process;
-- Stimulus process
stim_proc: process
begin
-- Hold
-- Hold again
mode_tb <= "00";
wait;
end process;
end behavior;
Program:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
USE ieee.numeric_std.ALL;
entity Single_port_RAM_VHDL is
port(
);
end Single_port_RAM_VHDL;
x"55",x"66",x"77",x"67",-- 0x00:
x"99",x"00",x"00",x"11",-- 0x04:
x"00",x"00",x"00",x"00",-- 0x08:
x"00",x"00",x"00",x"00",-- 0x0C:
x"00",x"00",x"00",x"00",-- 0x10:
x"00",x"00",x"00",x"00",-- 0x14:
x"00",x"00",x"00",x"00",-- 0x18:
x"00",x"00",x"00",x"00",-- 0x1C:
x"00",x"00",x"00",x"00",-- 0x20:
x"00",x"00",x"00",x"00",-- 0x24:
x"00",x"00",x"00",x"00",-- 0x28:
x"00",x"00",x"00",x"00",-- 0x2C:
x"00",x"00",x"00",x"00",-- 0x30:
x"00",x"00",x"00",x"00",-- 0x34:
x"00",x"00",x"00",x"00",-- 0x38:
x"00",x"00",x"00",x"00",-- 0x3C:
x"00",x"00",x"00",x"00",-- 0x40:
x"00",x"00",x"00",x"00",-- 0x44:
x"00",x"00",x"00",x"00",-- 0x48:
x"00",x"00",x"00",x"00",-- 0x4C:
x"00",x"00",x"00",x"00",-- 0x50:
x"00",x"00",x"00",x"00",-- 0x54:
x"00",x"00",x"00",x"00",-- 0x58:
x"00",x"00",x"00",x"00",-- 0x5C:
x"00",x"00",x"00",x"00",
x"00",x"00",x"00",x"00",
x"00",x"00",x"00",x"00",
x"00",x"00",x"00",x"00",
x"00",x"00",x"00",x"00",
x"00",x"00",x"00",x"00",
x"00",x"00",x"00",x"00",
x"00",x"00",x"00",x"00"
);
begin
process(RAM_CLOCK)
begin
if(rising_edge(RAM_CLOCK)) then
end if;
end if;
end process;
end Behavioral;
Test-Bench :
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
USE ieee.std_logic_unsigned.ALL;
ENTITY tb_RAM_VHDL IS
END tb_RAM_VHDL;
COMPONENT Single_port_RAM_VHDL
PORT(
RAM_WR : IN std_logic;
RAM_CLOCK : IN std_logic;
);
END COMPONENT;
);
RAM_CLOCK_process :process
begin
end process;
stim_proc: process
begin
for i in 0 to 5 loop
end loop;
for i in 0 to 5 loop
end loop;
wait;
end process;
END;
Program:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity moore is
Port ( clk : in STD_LOGIC;
din : in STD_LOGIC;
rst : in STD_LOGIC;
dout : out STD_LOGIC);
end moore;
architecture Behavioral of moore is
type state is (st0, st1, st2, st3);
signal present_state, next_state : state;
begin
synchronous_process: process (clk)
begin
if rising_edge(clk) then
if (rst = '1') then
present_state <= st0;
else
present_state <= next_state;
end if;
end if;
end process;
output_decoder : process(present_state, din)
begin
next_state <= st0; case (present_state) is when st0 =>
if (din = '1') then
next_state <= st1;
else
next_state <= st0; end if; when st1 =>
if (din = '1') then
next_state <= st1;
else
next_state <= st2; end if; when st2 =>
if (din = '1') then
next_state <= st3;
else
next_state <= st0; end if; when st3 =>
if (din = '1') then
next_state <= st1;
else
next_state <= st2; end if; when others =>
next_state <= st0; end case; end process;
next_state_decoder : process(present_state) begin case (present_state) is when st0 =>
dout <= '0'; when st1 =>
dout <= '0'; when st2 =>
dout <= '0'; when st3 =>
dout <= '1'; when others =>
dout <= '0';
end case;
end process;
Test-bench for Sequence detector (101) using mealy state machine:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity mealy is
Port ( clk : in STD_LOGIC;
din : in STD_LOGIC;
rst : in STD_LOGIC;
dout : out STD_LOGIC);
end mealy;
architecture Behavioral of mealy is
type state is (st0, st1, st2, st3);
signal present_state, next_state : state;
begin
syncronous_process : process (clk)
begin
if rising_edge(clk) then
if (rst = '1') then
present_state <= st0;
else
present_state <= next_state;
end if;
end if;
end process;
next_state_and_output_decoder : process(present_state, din)
begin
dout <= '0'; case (present_state) is when st0 =>
if (din = '1') then
next_state <= st1;
dout <= '0';
else
next_state <= st0;
dout <= '0'; end if; when St1 =>
if (din = '1') then
next_state <= st1;
dout <= '0';
else
next_state <= st2;
dout <= '0'; end if; when St2 =>
if (din = '1') then
next_state <= st1;
dout <= '1';
else
next_state <= st0;
dout <= '0'; end if; when others =>
next_state <= st0;
dout <= '0';
end case;
end process;
end Behavioral;
Program(up-counter):
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity SOURCE is
end SOURCE;
begin
process (CLK,RST)
begin
if (RST = '1')then
elsif(rising_edge(CLK))then
COUNT <= COUNT+1;
end if;
end process;
end Behavioral;
Test-bench (up-counter):
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity sync_upcounter_tb is
end entity;
architecture tb of sync_upcounter_tb is
component SOURCE is
end component;
begin
clock: process
begin
end tb;
program(down-count):
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity down_count is
end down_count;
begin
process(clk,rst)
begin
if(rst='1')then
temp<="1111";
elsif(rising_edge(clk))then
temp<=temp-1;
end if;
end process;
Test-Bench(down-count):
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity sync_downcounter_tb is
end entity;
architecture tb of sync_downcounter_tb is
component down_count is
end component;
begin
clock: process
begin
end process;
end tb;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity up_down_counter is
port (
clk : in std_logic;
reset : in std_logic;
up_down : in std_logic;
);
end up_down_counter;
begin
process(clk, reset)
begin
else
end if;
end if;
end process;
end behavioral;
Test-Bench:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity tb_up_down_counter is
end tb_up_down_counter;
component up_down_counter
port (
clk : in std_logic;
reset : in std_logic;
up_down : in std_logic;
);
end component;
begin
uut: up_down_counter
port map (
);
clk_process : process
begin
end loop;
wait;
end process;
-- Stimulus
stim_proc: process
begin
-- Reset counter
-- Hold simulation
wait;
end process;
end test;