FPGA VHDL EVEN DETECTOR
--=============================
--Even detector
--=============================
library ieee;
use ieee.std_logic_1164.all;
-- entity declaration
entity even_detector is
port(
a: in std_logic_vector(2 downto 0);
even: out std_logic;
odd : out std_logic
);
end even_detector;
--=============================
--(sum of products design)
--=============================
architecture sop_arch of even_detector is
signal p1, p2, p3, p4 : std_logic;
begin
even <= (p1 or p2) or (p3 or p4) after 20 ns;
p1 <= (not a(2)) and (not a(1)) and (not a(0)) after 15 ns;
p2 <= (not a(2)) and a(1) and a(0) after 12 ns;
p3 <= a(2) and (not a(1)) and a(0) after 12 ns;
p4 <= a(2) and a(1) and (not a(0)) after 12 ns;
end sop_arch ;
--=============================
--xor gate design
--=============================
architecture xor_arch of even_detector is
signal odd1: std_logic;
begin
even <= not odd1;
odd <= a(2) xor a(1) xor a(0);
odd <= odd1;
end xor_arch;
--=============================
--structural design
--=============================
architecture str_arch of even_detector is
-- declaration for xor gate
component xor2
port(
i1, i2: in std_logic;
o1: out std_logic
);
end component;
-- declaration for invertor
component not1
port(
i1: in std_logic;
o1: out std_logic
);
end component;
signal sig1,sig2: std_logic;
begin
-- instantiation of the 1st xor instance
unit1: xor2
port map (i1 => a(0), i2 => a(1), o1 => sig1);
-- instantiation of the 2nd xor instance
unit2: xor2
port map (i1 => a(2), i2 => sig1, o1 => sig2);
-- instantiation of invertor
unit3: not1
port map (i1 => sig2, o1 => even);
end str_arch;
--=============================
-- behavioural design odd
--=============================
architecture beh1_arch of even_detector is
signal odd1: std_logic;
begin
-- invertor
even <= not odd1;
-- xor network for odd parity
process(a)
variable tmp: std_logic;
begin
tmp := '0';
for i in 2 downto 0 loop
tmp := tmp xor a(i);
end loop;
odd1 <= tmp;
end process;
odd <=odd1;
end beh1_arch;
--=============================
-- behavioural design even
--=============================
architecture beh2_arch of even_detector is
begin
process(a)
variable sum, r: integer;
begin
sum := 0;
for i in 2 downto 0 loop
if a(i)='1' then
sum := sum +1;
end if;
end loop ;
r := sum mod 2;
if (r=0) then
even <= '1';
else
even <='0';
end if;
end process;
end beh2_arch;
--=============================
-- Listing 2.4
--=============================
-- 2-input xor gate
library ieee;
use ieee.std_logic_1164.all;
entity xor2 is
port(
i1, i2: in std_logic;
o1: out std_logic
);
end xor2;
architecture beh_arch of xor2 is
begin
o1 <= i1 xor i2;
end beh_arch;
-- invertor
library ieee;
use ieee.std_logic_1164.all;
entity not1 is
port(
i1: in std_logic;
o1: out std_logic
);
end not1;
architecture beh_arch of not1 is
begin
o1 <= not i1;
end beh_arch;
--=============================
-- Listing 2.4
--=============================
-- 2-input xor gate
library ieee;
use ieee.std_logic_1164.all;
entity xor2 is
port(
i1, i2: in std_logic;
o1: out std_logic
);
end xor2;
architecture beh_arch of xor2 is
begin
o1 <= i1 xor i2;
end beh_arch;
-- invertor
library ieee;
use ieee.std_logic_1164.all;
entity not1 is
port(
i1: in std_logic;
o1: out std_logic
);
end not1;
architecture beh_arch of not1 is
begin
o1 <= not i1;
end beh_arch;
EVEN DETECTOR TESTBENCH
--=============================
-- even detector testbench
--=============================
library ieee;
use ieee.std_logic_1164.all;
entity even_detector_testbench is
end even_detector_testbench;
architecture tb_arch of even_detector_testbench is
component even_detector
port(
a: in std_logic_vector(2 downto 0);
even: out std_logic
);
end component;
signal test_in: std_logic_vector(2 downto 0);
signal test_out: std_logic;
begin
-- instantiate the circuit under test
uut: even_detector
port map( a=>test_in, even=>test_out);
-- test vector generator
process
begin
test_in <= "000";
wait for 200 ns;
test_in <= "001";
wait for 200 ns;
test_in <= "010";
wait for 200 ns;
test_in <= "011";
wait for 200 ns;
test_in <= "100";
wait for 200 ns;
test_in <= "101";
wait for 200 ns;
test_in <= "110";
wait for 200 ns;
test_in <= "111";
wait for 200 ns;
end process;
--verifier
process
variable error_status: boolean;
begin
wait on test_in;
wait for 100 ns;
if ((test_in="000" and test_out = '1') or
(test_in="001" and test_out = '0') or
(test_in="010" and test_out = '0') or
(test_in="011" and test_out = '1') or
(test_in="100" and test_out = '0') or
(test_in="101" and test_out = '1') or
(test_in="110" and test_out = '1') or
(test_in="111" and test_out = '0'))
then
error_status := false;
else
error_status := true;
end if;
-- error reporting
assert not error_status
report "test failed."
severity note;
end process;
end tb_arch;
TESTBENCH CONFIGURATION
--=============================
configuration demo_config of even_detector_testbench is
for tb_arch
for uut: even_detector
use entity work.even_detector(sop_arch);
end for;
end for;
end demo_config;
CONSTRAINTS
#16I/Os_1
NET "odd" LOC = "p113" ;
NET "even" LOC = "p126" ;
#8I/Os_2
NET "a[0]" LOC = "p94" ;
NET "a[1]" LOC = "p93" ;
NET "a[2]" LOC = "p92" ;
No comments:
Post a Comment