VHDL - Lab Solution
VHDL - Lab Solution
Solution
www.vlsitraining.com
--Inputs
SIGNAL A ,B ,CIN : std_logic := '0';
--Outputs
SIGNAL S ,COUT : std_logic;
--Intermediate signal
SIGNAL ABC : std_logic_vector(2 DOWNTO 0):="000";
BEGIN
-- Instantiate the Unit Under Test (UUT)
uut: FULL_ADDER PORT MAP
(A => A, B => B, CIN => CIN, S => S, COUT => COUT );
tb : PROCESS
BEGIN
A <= ABC(0); B <= ABC(1); CIN <=ABC(2);
wait for 5 ns;
ABC <= ABC + 1;
END PROCESS;
END;
www.vlsitraining.com
begin
L1: for I in SUM'RANGE generate
L2: if I=0 generate
FA1: FULL_ADDER port map (IN1(0),IN2(0),CIN,SUM(0),C(0)); --component instantiation
end generate;
--for unit adder
L3: if I>0 generate
FA2: FULL_ADDER port map (IN1(I),IN2(I),C(I-1),SUM(I),C(I)); --component instantiation
end generate;
end generate;
COUT <= C(C'HIGH);
end STRUCTURE;
www.vlsitraining.com
);
tb : PROCESS
BEGIN
A<= "0010";
wait for 5 ns;
A<= "1111";
wait for 5 ns;
A<= "1111";
wait for 5 ns;
A<= "1111";
wait for 5 ns;
A<= "1111";
wait ;
END PROCESS;
B<= "0100";
CIN<='1';
B<= "0001";
CIN<='0';
B<= "0000";
CIN<='1';
B<= "0001";
CIN<='1';
B<= "0100";
CIN<='0';
END;
www.vlsitraining.com
www.vlsitraining.com
www.vlsitraining.com
www.vlsitraining.com
www.vlsitraining.com
www.vlsitraining.com
ENTITY t_comparator_vhd IS
END t_comparator_vhd;
ARCHITECTURE behavior OF t_comparator_vhd IS
-- Component Declaration for the Unit Under Test (UUT)
COMPONENT comparator
PORT(reset : IN std_logic;
A : IN std_logic_vector(3 downto 0);
B : IN std_logic_vector(3 downto 0);
E , G ,L : OUT std_logic);
END COMPONENT;
--Inputs
SIGNAL reset : std_logic := '0';
SIGNAL A : std_logic_vector(3 downto 0) := (others=>'0');
SIGNAL B : std_logic_vector(3 downto 0) := (others=>'0');
--Outputs
SIGNAL E ,G, L : std_logic;
BEGIN
-- Instantiate the Unit Under Test (UUT)
uut: comparator PORT MAP(reset => reset,
A => A,
B => B,
E => E,
G => G,
L => L );
tb : PROCESS
BEGIN
wait for 5 ns;
A <= A + 2;
B <= B + 3;
wait for 5 ns;
A <= A + 6;
B <= B + 1;
wait for 5 ns;
A <= A + 1;
B <= B + 2;
wait for 5 ns;
END PROCESS;
END;
www.vlsitraining.com
www.vlsitraining.com
ENTITY t_SRlatch_vhd IS
END t_SRlatch_vhd;
ARCHITECTURE behavior OF t_SRlatch_vhd IS
-- Component Declaration for the Unit Under Test (UUT)
COMPONENT SRlatch
Port ( S,R,Preset,Clear,Enable : in std_logic;
Q : inout std_logic;
Qn : out std_logic);
END COMPONENT;
--Inputs
SIGNAL S , R , Preset ,Clear , Enable : std_logic := '0';
--BiDirs
SIGNAL Q : std_logic;
--Outputs
SIGNAL Qn : std_logic;
BEGIN
-- Instantiate the Unit Under Test (UUT)
uut: SRlatch PORT MAP( S => S,R => R, Preset => Preset, Clear => Clear, Enable => Enable,
Q => Q, Qn => Qn);
tb : PROCESS
BEGIN
wait for 5 ns;
S <='0'; R <='0';
Preset <='0';
Clear <='1'; Enable <='1';
wait for 10 ns;
S <='0'; R <='0';
Preset <='1'; Clear <='0'; Enable <='1';
wait for 10 ns;
S <='0'; R <='1';
Preset <='0'; Clear <='0'; Enable <='1';
wait for 10 ns;
S <='1'; R <='0';
Preset <='0'; Clear <='0'; Enable <='0';
wait for 10 ns;
S <='1'; R <='0';
Preset <='0'; Clear <='0'; Enable <='1';
wait for 10 ns;
S <='0'; R <='1';
Preset <='0'; Clear <='0'; Enable <='1';
wait for 10 ns;
S <='0'; R <='1';
Preset <='0'; Clear <='0'; Enable <='0';
wait; -- will wait forever
END PROCESS;
END;
www.vlsitraining.com
www.vlsitraining.com
www.vlsitraining.com
www.vlsitraining.com
www.vlsitraining.com
www.vlsitraining.com
www.vlsitraining.com
www.vlsitraining.com
ENTITY t_seq_counter_vhd IS
END t_seq_counter_vhd;
ARCHITECTURE behavior OF t_seq_counter_vhd IS
-- Component Declaration for the Unit Under Test (UUT)
COMPONENT seq_counter
PORT( clk ,preset ,clear : IN std_logic;
A ,B ,C : INOUT std_logic );
END COMPONENT;
--Inputs
SIGNAL clk,preset ,clear : std_logic := '0';
--BiDirs
SIGNAL A,B,C : std_logic;
BEGIN
-- Instantiate the Unit Under Test (UUT)
uut: seq_counter PORT MAP( clk => clk,
A => A,
tb1 : PROCESS
BEGIN
wait for 5 ns;
clk <= not(clk);
END PROCESS;
tb2 : PROCESS
BEGIN
Preset <='0'; Clear <='1';
wait for 10 ns;
Preset <='1'; Clear <='0';
wait for 10 ns;
Preset <='0'; Clear <='0';
wait;
END PROCESS;
END;
www.vlsitraining.com
0/0
1/0
0/0
B
1/0
C
0/0
0/0
1/0
1/0
www.vlsitraining.com
0/1
www.vlsitraining.com
www.vlsitraining.com
-- Coding style:
---------------------------------------------------------------------------------library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY mealy_odd_parity_1p IS
PORT( clk, reset: IN std_logic;
x: IN std_logic;
y: OUT std_logic);
END mealy_odd_parity_1p;
ARCHITECTURE mealy_1p OF mealy_odd_parity_1p IS
TYPE state_type IS (even, odd);
SIGNAL current_state: state_type;
BEGIN
PROCESS (clk, reset, current_state, x)
BEGIN
IF reset = '1' THEN
current_state <= even;
ELSIF (clk='1' and clk'event) THEN
CASE current_state IS
WHEN even =>
IF x = '1' THEN
current_state <= odd;
y <= '1';
ELSE
current_state <= even;
y <= '0';
END IF;
WHEN odd =>
IF x = '1' THEN
current_state <= even;
y <= '0';
ELSE
current_state <= odd;
y <= '1';
END IF;
END CASE;
END IF;
END PROCESS ;
END ARCHITECTURE mealy_1p;
www.vlsitraining.com
www.vlsitraining.com
-- Coding style:
---------------------------------------------------------------------------------library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY mealy_odd_parity_3p IS
PORT( clk, reset: IN std_logic;
x: IN std_logic;
y: OUT std_logic);
END ENTITY mealy_odd_parity_3p;
ARCHITECTURE mealy_3p OF mealy_odd_parity_3p IS
TYPE state_type IS (even, odd);
SIGNAL current_state: state_type;
SIGNAL next_state: state_type;
BEGIN
CP: PROCESS (clk, reset)
BEGIN
IF reset = '1' THEN
current_state <= even;
ELSIF rising_edge (clk) THEN
current_state <= next_state;
END IF;
END PROCESS ;
NSL: PROCESS (current_state, x)
BEGIN
CASE current_state IS
WHEN even =>
IF x = '1' THEN
next_state <= odd;
ELSE
next_state <= even;
END IF;
WHEN odd =>
IF x = '1' THEN
next_state <= even;
ELSE
next_state <= odd;
END IF;
END CASE;
END PROCESS ;
www.vlsitraining.com
www.vlsitraining.com
-- Coding style:
.
---------------------------------------------------------------------------------library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY odd_parity_1p IS
PORT( clk, reset: IN std_logic;
x: IN std_logic;
y: OUT std_logic);
END ENTITY odd_parity_1p;
ARCHITECTURE moore_1p OF odd_parity_1p IS
TYPE state_type IS (even, odd);
SIGNAL current_state: state_type;
BEGIN
PROCESS (clk, reset, current_state)
BEGIN
IF reset = '1' THEN
current_state <= even;
ELSIF rising_edge (clk) THEN
CASE current_state IS
WHEN even =>
y <= '0';
IF x = '1' THEN
current_state <= odd;
ELSE
current_state <= even;
END IF;
WHEN odd =>
y <= '1';
IF x = '1' THEN
current_state <= even;
ELSE
current_state <= odd;
END IF;
END CASE;
END IF;
END PROCESS ;
END ARCHITECTURE moore_1p;
www.vlsitraining.com
-- Coding style:
.
---------------------------------------------------------------------------------library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY odd_parity_2p IS
PORT( clk, reset: IN std_logic;
x: IN std_logic;
y: OUT std_logic);
END ENTITY odd_parity_2p;
ARCHITECTURE moore_2p OF odd_parity_2p IS
TYPE state_type IS (even, odd);
SIGNAL current_state: state_type;
SIGNAL next_state: state_type;
BEGIN
CP: PROCESS (clk, reset)
BEGIN
IF reset = '1' THEN
current_state <= even;
ELSIF rising_edge (clk) THEN
current_state <= next_state;
END IF;
END PROCESS ;
NSP: PROCESS (current_state, x)
BEGIN
CASE current_state IS
WHEN even =>
y <= '0';
IF x = '1' THEN
next_state <= odd;
ELSE
next_state <= even;
END IF;
WHEN odd =>
y <= '1';
IF x = '1' THEN
next_state <= even;
ELSE
next_state <= odd;
END IF;
END CASE;
END PROCESS ;
www.vlsitraining.com
-- Coding style:
.
---------------------------------------------------------------------------------library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY odd_parity_3p IS
PORT( clk, reset: IN std_logic;
x: IN std_logic;
y: OUT std_logic);
END ENTITY odd_parity_3p;
ARCHITECTURE moore_3p OF odd_parity_3p IS
TYPE state_type IS (even, odd);
SIGNAL current_state: state_type;
SIGNAL next_state: state_type;
BEGIN
CP: PROCESS (clk, reset)
BEGIN
IF reset = '1' THEN
current_state <= even;
ELSIF rising_edge (clk) THEN
current_state <= next_state;
END IF;
END PROCESS ;
NSP: PROCESS (current_state, x)
BEGIN
CASE current_state IS
WHEN even =>
IF x = '1' THEN
next_state <= odd;
ELSE
next_state <= even;
END IF;
WHEN odd =>
IF x = '1' THEN
next_state <= even;
ELSE
next_state <= odd;
END IF;
END CASE;
END PROCESS ;
www.vlsitraining.com
www.vlsitraining.com