VHDL Code
VHDL Code
VHDL Code
case statement
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity encoder is
port(
);
end encoder;
begin
process(a)
begin
case a is
when others => b <= "ZZ"; end case; end process; end bhv;
Usin if else statement
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity encoder1 is
port(
a : in STD_LOGIC_VECTOR(3 downto 0);
b : out STD_LOGIC_VECTOR(1 downto 0)
);
end encoder1;
process(a)
begin
if (a="1000") then
b <= "00";
elsif (a="0100") then
b <= "01";
elsif (a="0010") then
b <= "10";
elsif (a="0001") then
b <= "11";
else
b <= "ZZ";
end if;
end process;
end bhv;
use IEEE.STD_LOGIC_1164.all;
entity encoder2 is
port(
a : in STD_LOGIC_VECTOR(3 downto 0);
b : out STD_LOGIC_VECTOR(1 downto 0)
);
end encoder2;
end bhv;
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity decoder is
port(
a : in STD_LOGIC_VECTOR(1 downto 0);
b : out STD_LOGIC_VECTOR(3 downto 0)
);
end decoder;
architecture bhv of decoder is
begin
process(a)
begin
case a is
when "00" => b <= "0001";
when "01" => b <= "0010";
when "10" => b <= "0100";
when "11" => b <= "1000";
end case;
end process;
end bhv;
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity decoder1 is
port(
a : in STD_LOGIC_VECTOR(1 downto 0);
b : out STD_LOGIC_VECTOR(3 downto 0)
);
end decoder1;
process(a)
begin
if (a="00") then
b <= "0001";
elsif (a="01") then
b <= "0010";
elsif (a="10") then
b <= "0100";
else
b <= "1000";
end if;
end process;
end bhv;
Using Logic gates
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity decoder2 is
port(
a : in STD_LOGIC_VECTOR(1 downto 0);
b : out STD_LOGIC_VECTOR(3 downto 0)
);
end decoder2;
end bhv;
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;
use IEEE.std_logic_unsigned.all;
---------------------------------- Entity Declarations -------------------------
entity andgate is
Port( A : in std_logic;
B : in std_logic;
Y : out std_logic
);
end andgate;
NOT Gate
NAND Gate
begin
Y= A nand B ;
end Behavioral;
NOR Gate
XOR Gate
XNOR Gate
Multiplexer (MUX) select one input from the multiple inputs and forwarded to
output line through selection line. It consists 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.
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;
begin
process (A,B,C,D,S0,S1) is
begin
Z <= A;
Z <= B;
Z <= C;
else
Z <= D;
end if;
end process;
end bhv;
VHDL TestBench Code for 4 to 1 Multiplexer
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 Z : std_logic;
BEGIN
A => A,
B => B,
C => C,
D => D,
S0 => S0,
S1 => S1,
Z => Z
);
-- Stimulus process
stim_proc: process
begin
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
end mux4_1;
component mux2_1
S: in STD_LOGIC;
Z: out STD_LOGIC);
end component;
begin
end Behavioral;
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.
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity demux_1to4 is
port(
F : in STD_LOGIC;
S0,S1: in STD_LOGIC;
);
end demux_1to4;
begin
process (F,S0,S1) is
begin
A <= F;
B <= F;
C <= F;
else
D <= F;
end if;
end process;
end bhv;
VHDL Testbench Code for 1 to 4 Demux
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
--Outputs
signal A : std_logic;
signal B : std_logic;
signal C : std_logic;
signal D : std_logic;
BEGIN
F => F,
S0 => S0,
S1 => S1,
A => A,
B => B,
C => C,
D => D
);
-- Stimulus process
stim_proc: process
begin
F <= '1';
--*============================
-- This is the AND gate
library ieee;
use ieee.std_logic_1164.all;
--
entity andGate is
port( A, B : in std_logic;
F : out std_logic);
end andGate;
--
architecture func of andGate is
begin
F <= A and B;
end func;
--*============================
--interconnecting wires
signal C1, C2, C3, C4, C5, C6: std_logic;--full adder carry
signal FA1, FA2, FA3 : std_logic;--inner full adder sums
signal andOut1, andOut2, orOut : std_logic;
begin
end struct;
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity adder_4bit is
port(
a : in STD_LOGIC_VECTOR(3 downto 0);
b : in STD_LOGIC_VECTOR(3 downto 0);
carry : out STD_LOGIC;
sum : out STD_LOGIC_VECTOR(3 downto 0)
);
end adder_4bit;
architecture adder_4bit_arc of adder_4bit is
Component fa is
port (a : in STD_LOGIC;
b : in STD_LOGIC;
c : in STD_LOGIC;
sum : out STD_LOGIC;
carry : out STD_LOGIC
);
end component;
begin
end adder_4bit_arc;
entity fa is
port (a : in STD_LOGIC;
b : in STD_LOGIC;
c : in STD_LOGIC;
sum : out STD_LOGIC;
carry : out STD_LOGIC
);
end fa;
architecture fa_arc of fa is
begin
end fa_arc;
VHDL code for 4-bit binary comparator
entity VHDL_Binary_Comparator is
port (
inp-A,inp-B : in std_logic_vector(3 downto 0);
greater, equal, smaller : out std_logic
);
end VHDL_Binary_Comparator ;
ALU’s comprise the combinational logic that implements logic operations such as
AND, OR, NOT gate and arithmetic operations, such as Adder, Subtractor.
entity alu is
Port ( inp_a : in signed(3 downto 0);
inp_b : in signed(3 downto 0);
sel : in STD_LOGIC_VECTOR (2 downto 0);
out_alu : out signed(3 downto 0));
end alu;
end process;
end Behavioral;
library ieee;
use ieee.std_logic_1164.all;
entity srl is
port(r,s:in bit; q,qbar:buffer bit);
end srl;
architecture virat of srl is
signal s1,r1:bit;
begin
q<= s nand qbar;
qbar<= r nand q;
end virat;
Waveforms
VHDL Code for a D Latch
library ieee;
use ieee.std_logic_1164.all;
entity Dl is
port(d:in bit; q,qbar:buffer bit);
end Dl;
architecture virat of Dl is
signal s1,r1:bit;
begin
q<= d nand qbar;
qbar<= d nand q;
end virat;
Waveforms
VHDL Code for an SR Flip Flop
library ieee;
use ieee.std_logic_1164.all;
entity srflip is
port(r,s,clk:in bit; q,qbar:buffer bit);
end srflip;
architecture virat of srflip is
signal s1,r1:bit;
begin
s1<=s nand clk;
r1<=r nand clk;
q<= s1 nand qbar;
qbar<= r1 nand q;
end virat;
VHDL code for a JK Flip Flop
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity jk is
port(
j : in STD_LOGIC;
k : in STD_LOGIC;
clk : in STD_LOGIC;
reset : in STD_LOGIC;
q : out STD_LOGIC;
qb : out STD_LOGIC
);
end jk;
architecture virat of jk is
begin
jkff : process (j,k,clk,reset) is
variable m : std_logic := '0';
begin
if (reset = '1') then
m : = '0';
elsif (rising_edge (clk)) then
if (j/ = k) then
m : = j;
elsif (j = '1' and k = '1') then
m : = not m;
end if;
end if;
q <= m;
qb <= not m;
end process jkff;
end virat;
VHDL Code for a D Flip Flop
Library ieee;
use ieee.std_logic_1164.all;
entity dflip is
port(d,clk:in bit; q,qbar:buffer bit);
end dflip;
architecture virat of dflip is
signal d1,d2:bit;
begin
d1<=d nand clk;
d2<=(not d) nand clk;
q<= d1 nand qbar;
qbar<= d2 nand q;
end virat;
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity Toggle_flip_flop is
port(
t : in STD_LOGIC;
clk : in STD_LOGIC;
reset : in STD_LOGIC;
dout : out STD_LOGIC
);
end Toggle_flip_flop;
architecture virat of Toggle_flip_flop is
begin
tff : process (t,clk,reset) is
variable m : std_logic : = '0';
begin
if (reset = '1') then
m : = '0';
elsif (rising_edge (clk)) then
if (t = '1') then
m : = not m;
end if;
end if;
dout < = m;
end process tff;
end virat;
library IEEE;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity counter is
port(Clock, CLR : in std_logic;
Q : out std_logic_vector(3 downto 0)
);
end counter;
architecture virat of counter is
signal tmp: std_logic_vector(3 downto 0);
begin
process (Clock, CLR)
begin
if (CLR = '1') then
tmp < = "0000";
elsif (Clock'event and Clock = '1') then
mp <= tmp + 1;
end if;
end process;
Q <= tmp;
end virat;
Waveforms
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity dcounter is
port(Clock, CLR : in std_logic;
Q : out std_logic_vector(3 downto 0));
end dcounter;
architecture virat of dcounter is
signal tmp: std_logic_vector(3 downto 0);
begin
process (Clock, CLR)
begin
if (CLR = '1') then
tmp <= "1111";
elsif (Clock'event and Clock = '1') then
tmp <= tmp - 1;
end if;
end process;
Q <= tmp;
end virat;
Waveforms
Synchronous Reset :
process(clk)
begin
if(rising_edge(clk)) then
if(reset = '0') then --reset is checked only at the rising edge of clock.
o <= i;
else
o <= '0';
end if;
end if;
end process;
The code is synthesised to the following block in FPGA: (truth table of FDR flip flop
is also given)
In the schematic diagram FDR is a single D-type flip-flop with data (D) and
synchronous reset (R) inputs and data output (Q). The synchronous reset (R) input,
when High, overrides all other inputs and resets the Q output Low on the 0 to 1
clock (C) transition. The data on the D input is loaded into the flip-flop when R is
Low during the 0 to1 clock transition.If you analyze the above code you can see
that the value of reset changes the signal 'o' only at the rising edge of the clock.This
method has the following advantages:
1)The reset applied to all the flip-flops are fully synchronized with clock and
always meet the reset recovery time.
2)In some cases, synchronous reset will synthesis to smaller flip-flops.
1)If the reset applied is for a small duration then the clock edge may not be able
to capture the reset signal.Thus if you are synchronous resets make sure that
your reset signal stays active for enough time so that it get captured by the clock.
2)Also the change in reset doesn't immediately reflect in the associated signals.
Asynchronous reset:
process(clk,reset)
begin
if(reset = '0') then --change in reset get immediately reflected on signal 'o'.
if(rising_edge(clk)) then
o <= i;
end if;
else
o <= '0';
end if;
end process;
The code is synthesised to the following in FPGA. (the truth table of the
particular flip-flop is also given)
In the schematic FDC is a single D-type flip-flop with data (D) and asynchronous
clear (CLR) inputs and data output (Q). The asynchronous CLR, when High,
overrides all other inputs and sets the Q output Low. The data on the D input is
loaded into the flip-flop when CLR is Low on the 0 to 1 clock transition.If you
analyse the code you can see that when the reset goes high , immediately signal 'o'
will become '0'.It doesn't wait for clock change. Now let us look at
the advantages of this method:
Note :- As you can see both the methods have their own advantages and
disadvantages.And selecting one of the method depends upon your design
requirement.