VHDL Codes
VHDL Codes
com
-- FPGA projects, VHDL projects, Verilog projects
-- VHDL code for full adder
-- Structural code for full adder
library ieee;
use ieee.std_logic_1164.all;
entity Full_Adder_Structural_VHDL is
port(
X1, X2, Cin : in std_logic;
S, Cout : out std_logic
);
end Full_Adder_Structural_VHDL;
architecture structural of Full_Adder_Structural_VHDL is
signal a1, a2, a3: std_logic;
begin
a1 <= X1 xor X2;
a2 <= X1 and X2;
a3 <= a1 and Cin;
Cout <= a2 or a3;
S <= a1 xor Cin;
end structural;
Library IEEE;
USE IEEE.Std_logic_1164.all;
-- fpga4student.com
-- FPGA projects, VHDL projects, Verilog projects
-- VHDL code for full adder
-- Testbench code of the structural code for full adder
entity Testbench_structural_adder is
end Testbench_structural_adder;
end behavioral;
The VHDL code for the full adder using the behavioral
model:
-- fpga4student.com
-- FPGA projects, VHDL projects, Verilog projects
-- VHDL code for full adder
-- Behavioral code for full adder
library ieee;
use ieee.std_logic_1164.all;
use IEEE.STD_LOGIC_unsigned.ALL;
use IEEE.NUMERIC_STD.ALL;
entity Full_Adder_Behavioral_VHDL is
port(
X1, X2, Cin : in std_logic;
S, Cout : out std_logic
);
end Full_Adder_Behavioral_VHDL;
architecture Behavioral of Full_Adder_Behavioral_VHDL is
signal tmp: std_logic_vector(1 downto 0);
begin
process(X1,X2,Cin)
begin
tmp <= ('0'& X1) + ('0'& X2) +('0'& Cin) ;
end process;
S <= tmp(0);
Cout <= tmp(1);
end Behavioral;
Library IEEE;
USE IEEE.Std_logic_1164.all;
-- fpga4student.com
-- FPGA projects, VHDL projects, Verilog projects
-- VHDL code for full adder
-- Testbench code of the behavioral code for full adder
entity Testbench_behavioral_adder is
end Testbench_behavioral_adder;
end process;
end behavioral;
entity comparator is
port (
clock: in std_logic;
-- clock for synchronization
A,B: in std_logic_vector(7 downto 0);
-- Two inputs
IAB: in std_logic; -- Expansion input ( Active low)
Output: out std_logic -- Output = 0 when A = B
);
end comparator;
architecture Behavioral of comparator is
signal AB: std_logic_vector(7 downto 0); -- temporary variables
signal Result: std_logic;
begin
end Behavioral;
COMPONENT ALU
PORT(
ABUS : IN std_logic_vector(15 downto 0);
BBUS : IN std_logic_vector(15 downto 0);
ALUctrl : IN std_logic_vector(3 downto 0);
ALUOUT : OUT std_logic_vector(15 downto 0)
);
END COMPONENT;
--Inputs
signal ABUS : std_logic_vector(15 downto 0) := (others => '0');
signal BBUS : std_logic_vector(15 downto 0) := (others => '0');
signal ALUctrl : std_logic_vector(3 downto 0) := (others => '0');
--Outputs
signal ALUOUT : std_logic_vector(15 downto 0);
BEGIN
END;
END;
entity RisingEdge_DFlipFlop is
port(
Q : out std_logic;
Clk :in std_logic;
D :in std_logic
);
end RisingEdge_DFlipFlop;
architecture Behavioral of RisingEdge_DFlipFlop is
begin
process(Clk)
begin
if(rising_edge(Clk)) then
Q <= D;
end if;
end process;
end Behavioral;
entity RisingEdge_DFlipFlop_SyncReset is
port(
Q : out std_logic;
Clk :in std_logic;
sync_reset: in std_logic;
D :in std_logic
);
end RisingEdge_DFlipFlop_SyncReset;
architecture Behavioral of RisingEdge_DFlipFlop_SyncReset is
begin
process(Clk)
begin
if(rising_edge(Clk)) then
if(sync_reset='1') then
Q <= '0';
else
Q <= D;
end if;
end if;
end process;
end Behavioral;
entity RisingEdge_DFlipFlop_AsyncResetHigh is
port(
Q : out std_logic;
Clk :in std_logic;
sync_reset: in std_logic;
D :in std_logic
);
end RisingEdge_DFlipFlop_AsyncResetHigh;
architecture Behavioral of RisingEdge_DFlipFlop_AsyncResetHigh is
begin
process(Clk,sync_reset)
begin
if(sync_reset='1') then
Q <= '0';
elsif(rising_edge(Clk)) then
Q <= D;
end if;
end process;
end Behavioral;
entity RisingEdge_DFlipFlop_AsyncResetLow is
port(
Q : out std_logic;
Clk :in std_logic;
sync_reset: in std_logic;
D :in std_logic
);
end RisingEdge_DFlipFlop_AsyncResetLow;
architecture Behavioral of RisingEdge_DFlipFlop_AsyncResetLow is
begin
process(Clk,sync_reset)
begin
if(sync_reset='0') then
Q <= '0';
elsif(rising_edge(Clk)) then
Q <= D;
end if;
end process;
end Behavioral;
entity FallingEdge_DFlipFlop is
port(
Q : out std_logic;
Clk :in std_logic;
D :in std_logic
);
end FallingEdge_DFlipFlop;
architecture Behavioral of FallingEdge_DFlipFlop is
begin
process(Clk)
begin
if(falling_edge(Clk)) then
Q <= D;
end if;
end process;
end Behavioral;
entity FallingEdge_DFlipFlop_SyncReset is
port(
Q : out std_logic;
Clk :in std_logic;
sync_reset: in std_logic;
D :in std_logic
);
end FallingEdge_DFlipFlop_SyncReset;
architecture Behavioral of FallingEdge_DFlipFlop_SyncReset is
begin
process(Clk)
begin
if(falling_edge(Clk)) then
if(sync_reset='1') then
Q <= '0';
else
Q <= D;
end if;
end if;
end process;
end Behavioral;
VHDL code for Falling Edge D Flip-Flop with
Asynchronous Reset High Level:
entity FallingEdge_DFlipFlop_AsyncResetHigh is
port(
Q : out std_logic;
Clk :in std_logic;
sync_reset: in std_logic;
D :in std_logic
);
end FallingEdge_DFlipFlop_AsyncResetHigh;
architecture Behavioral of FallingEdge_DFlipFlop_AsyncResetHigh is
begin
process(Clk,sync_reset)
begin
if(sync_reset='1') then
Q <= '0';
elsif(falling_edge(Clk)) then
Q <= D;
end if;
end process;
end Behavioral;
entity PWM_Generator is
port (
clk: in std_logic; -- 100MHz clock input
DUTY_INCREASE: in std_logic; -- button to increase duty cycle by 10%
DUTY_DECREASE: in std_logic; -- button to decrease duty cycle by 10%
PWM_OUT: out std_logic -- PWM signal out with frequency of 10MHz
);
end PWM_Generator;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
ENTITY tb_PWM_Genenrator IS
END tb_PWM_Genenrator;
COMPONENT PWM_Generator
PORT(
clk : IN std_logic;
DUTY_INCREASE : IN std_logic;
DUTY_DECREASE : IN std_logic;
PWM_OUT : OUT std_logic
);
END COMPONENT;
--Inputs
signal clk : std_logic := '0';
signal DUTY_INCREASE : std_logic := '0';
signal DUTY_DECREASE : std_logic := '0';
--Outputs
signal PWM_OUT : std_logic;
BEGIN
-- Stimulus process
stim_proc: process
begin
DUTY_INCREASE <= '0';
DUTY_DECREASE <= '0';
wait for clk_period*10;
DUTY_INCREASE <= '1';
wait for clk_period*10;
DUTY_INCREASE <= '0';
wait for clk_period*10;
DUTY_INCREASE <= '1';
wait for clk_period*10;
DUTY_INCREASE <= '0';
wait for clk_period*10;
DUTY_INCREASE <= '1';
wait for clk_period*10;
DUTY_INCREASE <= '0';
wait for clk_period*10;
DUTY_DECREASE <= '1';
wait for clk_period*10;
DUTY_DECREASE <= '0';
wait for clk_period*10;
DUTY_DECREASE <= '1';
wait for clk_period*10;
DUTY_DECREASE <= '0';
wait for clk_period*10;
DUTY_DECREASE <= '1';
wait for clk_period*10;
DUTY_DECREASE <= '0';
wait for clk_period*10;
-- insert stimulus here
wait;
end process;
END;
COMPONENT comparator_VHDL
PORT(
A : IN std_logic_vector(1 downto 0);
B : IN std_logic_vector(1 downto 0);
A_less_B : OUT std_logic;
A_equal_B : OUT std_logic;
A_greater_B : OUT std_logic
);
END COMPONENT;
--Inputs
signal A : std_logic_vector(1 downto 0) := (others => '0');
signal B : std_logic_vector(1 downto 0) := (others => '0');
--Outputs
signal A_less_B : std_logic;
signal A_equal_B : std_logic;
signal A_greater_B : std_logic;
BEGIN
-- Stimulus process
stim_proc: process
begin
-- create test cases for A_less_B
for i in 0 to 3 loop
A <= std_logic_vector(to_unsigned(i,2));
B <= std_logic_vector(to_unsigned(i+1,2));
wait for 20 ns;
end loop;
-- create test cases for A_greater_B
for i in 0 to 3 loop
A <= std_logic_vector(to_unsigned(i+1,2));
B <= std_logic_vector(to_unsigned(i,2));
wait for 20 ns;
end loop;
-- create test cases for A_equal_B
for i in 0 to 3 loop
A <= std_logic_vector(to_unsigned(i,2));
B <= std_logic_vector(to_unsigned(i,2));
wait for 20 ns;
end loop;
wait;
end process;
END;
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity mux_4to1 is
port(
A,B,C,D : in STD_LOGIC;
S0,S1: in STD_LOGIC;
Z: out STD_LOGIC
);
end mux_4to1;
else
Z <= D;
end if;
end process;
end bhv;
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
ENTITY tb_mux IS
END tb_mux;
COMPONENT mux_4to1
PORT(
A : IN std_logic;
B : IN std_logic;
C : IN std_logic;
D : IN std_logic;
S0 : IN std_logic;
S1 : IN std_logic;
Z : OUT std_logic
);
END COMPONENT;
– Inputs
signal A : std_logic := '0';
signal B : std_logic := '0';
signal C : std_logic := '0';
signal D : std_logic := '0';
signal S0 : std_logic := '0';
signal S1 : std_logic := '0';
--Outputs
signal Z : std_logic;
BEGIN
– Stimulus process
stim_proc: process
begin
– hold reset state for 100 ns.
wait for 100 ns;
A <= '1';
B <= '0';
C <= '1';
D <= '0';
end process;
END;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity mux2_1 is
port(A,B : in STD_LOGIC;
S: in STD_LOGIC;
Z: out STD_LOGIC);
end mux2_1;
begin
process (A,B,S) is
begin
if (S ='0') then
Z <= A;
else
Z <= B;
end if;
end process;
end Behavioral;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity mux4_1 is
port(
A,B,C,D : in STD_LOGIC;
S0,S1: in STD_LOGIC;
Z: out STD_LOGIC
);
end mux4_1;
begin
m1: mux2_1 port map(A,B,S0,temp1);
m2: mux2_1 port map(C,D,S0,temp2);
m3: mux2_1 port map(temp1,temp2,S1,Z);
end Behavioral;
Library IEEE;
USE IEEE.Std_logic_1164.all;
end Architecture;
Multiplexer
Multiplexer (MUX) select one input from the multiple inputs and forwarded to output line
through selection line. It consist of 2 power n input and 1 output. The input data lines are
controlled by n selection lines.
For Example, if n = 2 then the mux will be of 4 to 1 mux with 4 input, 2 selection line and 1
output as shown below.
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity mux_4to1 is
port(
A,B,C,D : in STD_LOGIC;
S0,S1: in STD_LOGIC;
Z: out STD_LOGIC
);
end mux_4to1;
else
Z <= D;
end if;
end process;
end bhv;
ENTITY tb_mux IS
END tb_mux;
COMPONENT mux_4to1
PORT(
A : IN std_logic;
B : IN std_logic;
C : IN std_logic;
D : IN std_logic;
S0 : IN std_logic;
S1 : IN std_logic;
Z : OUT std_logic
);
END COMPONENT;
– Inputs
signal A : std_logic := '0';
signal B : std_logic := '0';
signal C : std_logic := '0';
signal D : std_logic := '0';
signal S0 : std_logic := '0';
signal S1 : std_logic := '0';
--Outputs
signal Z : std_logic;
BEGIN
– Instantiate the Unit Under Test (UUT)
uut: mux_4to1 PORT MAP (
A => A,
B => B,
C => C,
D => D,
S0 => S0,
S1 => S1,
Z => Z
);
– Stimulus process
stim_proc: process
begin
– hold reset state for 100 ns.
wait for 100 ns;
A <= '1';
B <= '0';
C <= '1';
D <= '0';
end process;
END;
entity mux2_1 is
port(A,B : in STD_LOGIC;
S: in STD_LOGIC;
Z: out STD_LOGIC);
end mux2_1;
begin
process (A,B,S) is
begin
if (S ='0') then
Z <= A;
else
Z <= B;
end if;
end process;
end Behavioral;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity mux4_1 is
port(
A,B,C,D : in STD_LOGIC;
S0,S1: in STD_LOGIC;
Z: out STD_LOGIC
);
end mux4_1;
begin
m1: mux2_1 port map(A,B,S0,temp1);
m2: mux2_1 port map(C,D,S0,temp2);
m3: mux2_1 port map(temp1,temp2,S1,Z);
end Behavioral;
Table of Contents
DeMultiplexer
1 to 4 Demux
Truth table for Demux 1 to 4
1 to 4 Demux design using Logic Gates
VHDL Code for 1 to 4 Demux
VHDL Testbench Code for 1 to 4 Demux
Testbench waveform for 1 to 4 Demux
DeMultiplexer
Demultiplexer (DEMUX) select one output from the multiple output line and fetch the single
input through selection line. It consist of 1 input and 2 power n output. The output data lines are
controlled by n selection lines. For Example, if n = 2 then the demux will be of 1 to 4 mux with
1 input, 2 selection line and 4 output as shown below. Also VHDL Code for 1 to 4
Demux described below.
1 to 4 Demux
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity demux_1to4 is
port(
F : in STD_LOGIC;
S0,S1: in STD_LOGIC;
A,B,C,D: out STD_LOGIC
);
end demux_1to4;
else
D <= F;
end if;
end process;
end bhv;
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
ENTITY tb_demux IS
END tb_demux;
COMPONENT demux_1to4
PORT(
F : IN std_logic;
S0 : IN std_logic;
S1 : IN std_logic;
A : OUT std_logic;
B : OUT std_logic;
C : OUT std_logic;
D : OUT std_logic
);
END COMPONENT;
--Inputs
signal F : std_logic := '0';
signal S0 : std_logic := '0';
signal S1 : std_logic := '0';
--Outputs
signal A : std_logic;
signal B : std_logic;
signal C : std_logic;
signal D : std_logic;
– No clocks detected in port list. Replace <clock> below with
– appropriate port name
BEGIN
– Stimulus process
stim_proc: process
begin
– hold reset state for 100 ns.
wait for 100 ns;
F <= '1';
wait;
end process;
END;