Escolar Documentos
Profissional Documentos
Cultura Documentos
MTDS ASSIGNMENT-2
NIRMAL SURESH
B120029EC
S7 ECE
TEST BENCH:
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
USE ieee.numeric_std.ALL;
ENTITY tbmem648 IS
END tbmem648;
ARCHITECTURE behavior OF tbmem648 IS
-- Component Declaration for the Unit Under Test (UUT)
COMPONENT MEM648
PORT(
clk : IN std_logic;
en : IN std_logic;
rddat : IN std_logic;
wtdat : IN std_logic;
add : IN integer;
indata : IN std_logic_vector(63 downto 0);
outdata : OUT std_logic_vector(63 downto 0)
);
END COMPONENT;
--Inputs
signal clk : std_logic := '0';
signal en : std_logic := '0';
signal rddat : std_logic := '0';
signal wtdat : std_logic := '0';
signal add : integer := 0;
signal indata : std_logic_vector(63 downto 0) := (others => '0');
--Outputs
signal outdata : std_logic_vector(63 downto 0);
-- Clock period definitions
constant clk_period : time := 10 ns;
BEGIN
-- Instantiate the Unit Under Test (UUT)
uut: MEM648 PORT MAP (
clk => clk,
en => en,
rddat => rddat,
wtdat => wtdat,
add => add,
indata => indata,
outdata => outdata
);
-- Clock process definition
clk_process :process
begin
clk <= '0';
wait for clk_period/2;
clk <= '1';
wait for clk_period/2;
end process;
-- Stimulus process
stim_proc: process
begin
en<='1';
rddat<='0';
wtdat<='0';
add<=0;
indata<=(indata'range =>'0');
wait for 20 ns;
wtdat<='1';
add<=6;
indata<= indata + "1011001010111100";
wait for 20 ns;
add<=25;
indata<= indata + "0011010111000011";
wait for 20 ns;
add<=56;
indata<= indata + "1100110011001100";
wait for 20 ns;
add<=63;
indata<= indata + "0101010110101010";
wait for 20 ns;
indata<=(others => '0');
wtdat<='0';
rddat<='1';
add<=6;
wait for 20 ns;
add<=25;
wait for 20 ns;
add<=56;
wait for 20 ns;
add<= 63;
wait for 20 ns;
wtdat<='0';
rddat<='0';
add<=0;
wait;
end process;
END;
q : out std_logic );
end component;
for all : cntr use entity counter.cntr(Beh);
begin
temp(0) <= clk;
G1 : for k in 0 to 15 generate
count1 : cntr port map ('1', temp(k) , rst,temp(k+1));
o <= not temp(16 downto 1);
end generate;
end Behavioral;
TEST BENCH:
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
ENTITY tbcntr IS
END tbcntr;
ARCHITECTURE behavior OF tbcntr IS
-- Component Declaration for the Unit Under Test (UUT)
COMPONENT BITCTR16
PORT(
clk : IN std_logic;
rst : IN std_logic;
o : OUT std_logic_vector(0 to 15)
);
END COMPONENT;
--Inputs
signal clk : std_logic := '0';
signal rst : std_logic := '0';
--Outputs
signal o : std_logic_vector(0 to 15);
-- Clock period definitions
constant clk_period : time := 50 ns;
BEGIN
VII Write a number of functions and store in a package .Use these functions from a
different design library (find applications to use the functions)
The functions are
a) Conversion from bit vector to integer.
b) Counting the number of zeros (and number of ones) in a 16 bit word.
c) Finding the biggest of 10 integers
library IEEE;
use IEEE.STD_LOGIC_1164.all;
package fns is
TYPE arr IS ARRAY(9 DOWNTO 0) OF INTEGER;
function bittodec ( bit1 : bit_vector ) return integer;
--function dectobit ( dec : in integer) return bit_vector;
function noofzs(bit2 : bit_vector) return integer;
function noofos(bit3 : bit_vector) return integer;
function larger( arr1: arr) return integer;
end fns;
package body fns is
function bittodec ( bit1 : bit_vector) return integer is
variable res : integer:=0;
begin
for i in bit1'range loop
if bit1(i) = '1' then
res := res +(2**i);
end if;
end loop;
return res;
end bittodec;
function dectobit ( signal dec : integer) return bit_vector is
variable temp ,cnt,res1,remn : integer := 0;
temp:= dec ;
cnt:=1;
remn:= 0;
while dec>0 loop
remn := dec mod 2;
res1:= res1 + (cnt*remn);
cnt:=cnt*10;
dec:=dec/2;
end loop;
return res1;
end dectobit;
function noofzs(bit2 : bit_vector) return integer is
variable res2:integer;
begin
for i in bit2' range loop
if bit2(i) = '0' then
res2:=res2+1;
end if;
end loop;
return res2;
end noofzs;
function noofos(bit3: bit_vector) return integer is
variable res3:integer;
begin
for i in bit3' range loop
if bit3(i) = '1' then
res3:=res3+1;
end if;
end loop;
return res3;
end noofos;
function larger(arr1: arr) return integer is
variable res4: integer := 0;
begin
res4:=arr1(0);
for i in arr1'range loop
if arr1(i) > res4 then
res4:=arr1(i);
end if;
end loop;
return res4;
end larger;
end fns;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
library packagefn;
use packagefn.fns.all;
use IEEE.STD_LOGIC_unsigned.all;
entity pack is
Port ( a : in bit_vector(15 downto 0):="1111111111111110";
b : in bit_vector(7 downto 0):="00000011";
c : in integer:=10;
d : in arr:=(1,3,6,8,9,8,6,4,2,0));
end pack;
architecture Behavioral of pack is
signal bint,large,n,m : integer;
begin
bint <= bittodec(b);
n<=noofzs(a);
m<=noofos(a);
large<=larger(d);
end Behavioral;
VIII Design the following as state machines and write VHDL code.
a) Up/down counter counting from 0 to 5.
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity stateupdwn is
Port ( clk : in STD_LOGIC;
rst : in STD_LOGIC;
inp : in STD_LOGIC;
o : out STD_LOGIC_VECTOR(2 DOWNTO 0));
end stateupdwn;
architecture Behavioral of stateupdwn is
TYPE state is (s0, s1, s2 ,s3, s4, s5);
signal pstat : state := s0;
begin
process(clk)
begin
if rst ='1' then
pstat <= s0;
o <="000";
elsif clk'event and clk ='1' then
case pstat is
when s0 => o <= "000";
if inp = '1' then
pstat <= s1;
else
pstat <= s5;
end if;
when s1 => o <= "001" ;
if inp = '1' then
pstat <= s2;
else
pstat <= s0;
end if;
end case;
end if;
end process;
end Behavioral;
TEST BENCH:
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
ENTITY tbstate IS
END tbstate;
ARCHITECTURE behavior OF tbstate IS
-- Component Declaration for the Unit Under Test (UUT)
COMPONENT stateupdwn
PORT(
clk : IN std_logic;
rst : IN std_logic;
inp : IN std_logic;
o : OUT std_logic_vector(2 downto 0)
);
END COMPONENT;
--Inputs
signal clk : std_logic := '0';
signal rst : std_logic := '0';
signal inp : std_logic := '0';
--Outputs
signal o : std_logic_vector(2 downto 0);
-- Clock period definitions
constant clk_period : time := 10 ns;
BEGIN
-- Instantiate the Unit Under Test (UUT)
uut: stateupdwn PORT MAP (
clk => clk,
-- Stimulus process
stim_proc: process
begin
rst<= '1';
wait for 10 ns;
rst<= '0';
wait for 50 ns;
inp<='1';
wait for 50 ns;
inp<='1';
wait for 50 ns;
inp<='1';
wait for 50 ns;
inp<='0';
wait for 50 ns;
inp<='0';
wait for 50 ns;
inp<='1';
wait;
end process;
END;
case pst is
when s0 => --if ((ready = '1')and(clk'event and clk ='1'))then
-- wait until (clk'event and clk ='1');
s(0) <= a(0) xor b(0);
temp(0) <= a(0)and b(0);
nst<= s1;
--end if;
when s1 => s(1) <= a(1) xor b(1) xor temp(0);
temp(1) <= (a(1)and b(1)) xor( a(1) xor b(1) xor
temp(0)) ;
nst<= s2;
when s2 => s(2) <= a(2) xor b(2) xor temp(1);
temp(2) <= (a(2)and b(2)) or( a(2) xor b(2) xor
temp(1)) ;
nst<= s3;
when s3 => s(3) <= a(3) xor b(3) xor temp(2);
temp(3) <= (a(3)and b(3)) or( a(3) xor b(3) xor
temp(2)) ;
nst<= s0;
end case;
end process;
cout<= temp(3);
end archi;
TEST BENCH:
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
ENTITY tbserial IS
END tbserial;
ARCHITECTURE behavior OF tbserial IS
-- Component Declaration for the Unit Under Test (UUT)
COMPONENT serl
PORT(
a : IN std_logic_vector(3 downto 0);
--Inputs
signal a : std_logic_vector(3 downto 0) := (others => '0');
signal b : std_logic_vector(3 downto 0) := (others => '0');
signal rst : std_logic := '0';
signal clk : std_logic := '0';
signal ready : std_logic := '0';
--Outputs
signal s : std_logic_vector(3 downto 0):= (others => '0');
signal cout : std_logic:='0';
-- Clock period definitions
constant clk_period : time := 50 ns;
BEGIN
-- Instantiate the Unit Under Test (UUT)
uut: serl PORT MAP (
a => a,
b => b,
s => s,
cout => cout,
rst => rst,
clk => clk,
ready => ready
);
-- Clock process definitions
clk_process :process
begin
clk <= '0';
wait for clk_period/2;
clk <= '1';
wait for clk_period/2;
end process;
-- Stimulus proces
stim_proc: process
begin
rst<='1';
wait for 50 ns;
rst<= '0';
ready<='1';
a<="0001"; b<="0101";
wait for 50ns;
--ready<='0';
wait;
end process;
END
TEST BENCH:
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
ENTITY tbseq IS
END tbseq;
ARCHITECTURE behavior OF tbseq IS
-- Component Declaration for the Unit Under Test (UUT)
COMPONENT seq
PORT(
clk : IN std_logic;
rst : IN std_logic;
x : IN std_logic;
z : OUT std_logic
);
END COMPONENT;
--Inputs
signal clk : std_logic := '0';
signal rst : std_logic := '0';
signal x : std_logic := '0';
--Outputs
signal z : std_logic;
-- Clock period definitions
constant clk_period : time := 10 ps;
BEGIN
-- Instantiate the Unit Under Test (UUT)
uut: seq PORT MAP (
clk => clk,
rst => rst,
x => x,
z => z
);
-- Clock process definitions
clk_process :process
begin
clk <= '0';
wait for clk_period/2;
clk <= '1';
wait for clk_period/2;
end process;
-- Stimulus process
stim_proc: process
begin
z<='0';
wait for 2*clk_period;
wait for 0.005 ns;
x<='0';
wait for clk_period;
x<='1';
wait for clk_period;
x<='0';
wait for clk_period;
x<='1';
wait for clk_period;
x<='1';
wait for clk_period;
x<='0';
wait for clk_period;
x<='1';
wait for clk_period;
x<='0';
wait for clk_period;
x<='1';
wait for clk_period;
x<='1';
wait for clk_period;
x<='0';
wait for clk_period;
x<='1';
wait for clk_period;
x<='0';
wait for clk_period;
x<='0';
wait for clk_period;
x<='0';
wait for clk_period;
x<='1';
wait for clk_period;
wait;
end process;
END;