4-to-1 Multiplexer (if-else Statement)

 The 4-to-1 Multiplexer is used to select between multiple input lines based on select line. The below code is written using if-else statement (sequential statement).

VHDL Code:

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity Mux41 is
    Port ( i0,i1,i2,i3,s0,s1 : in  STD_LOGIC;
           Y : out  STD_LOGIC);
end Mux41;

architecture Mux41 of Mux41 is
begin
process(i0,i1,i2,i3,s0,s1)
begin
if(s1='0' and s0='0') then
Y <= i0;
elsif(s1='0' and s0='1') then
Y <= i1;
elsif(s1='1' and s0='0') then
Y <= i2;
elsif(s1='1' and s0='1') then
Y <= i3;
else 
Y <= 'Z';
end if;
end process;
end Mux41;



Testbench:

LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
 
ENTITY mux41_tb IS
END mux41_tb;
 
ARCHITECTURE behavior OF mux41_tb IS 

    COMPONENT Mux41
    PORT(
         i0 : IN  std_logic;
         i1 : IN  std_logic;
         i2 : IN  std_logic;
         i3 : IN  std_logic;
         s0 : IN  std_logic;
         s1 : IN  std_logic;
         Y : OUT  std_logic
        );
    END COMPONENT;
    --Inputs
   signal i0 : std_logic := '0';
   signal i1 : std_logic := '0';
   signal i2 : std_logic := '0';
   signal i3 : std_logic := '0';
   signal s0 : std_logic := '0';
   signal s1 : std_logic := '0';
  --Outputs
   signal Y : std_logic;
  
BEGIN
 
-- Instantiate the Unit Under Test (UUT)
   uut: Mux41 PORT MAP (
          i0 => i0,
          i1 => i1,
          i2 => i2,
          i3 => i3,
          s0 => s0,
          s1 => s1,
          Y => Y
        );

   -- Stimulus process
   stim_proc: process
   begin
    i0<='0'; i1<='1'; i2<='1'; i3<='0';
s1<='0'; s0<='0'; wait for 10 ns;
s1<='0'; s0<='1'; wait for 10 ns;
s1<='1'; s0<='0'; wait for 10 ns;
s1<='1'; s0<='1'; wait for 10 ns;
   end process;

END;

Output:



The above code is tested on Xilinx ISE Design Suite 14.7 Webpack.
It is intended only for educational purpose.

Comments

Popular posts from this blog

3-to-8 line Decoder VHDL Code (with-select-when)

Sequence Detector (Mealy Machine-1011) VHDL Code

8-TO-1 Multiplexer VHDL Code (Structural Style)