VHDL coding tips and tricks: VHDL: 1 to 4 Demultiplexer(DEMUX) Using Case statements with Testbench

Saturday, March 27, 2010

VHDL: 1 to 4 Demultiplexer(DEMUX) Using Case statements with Testbench

   I want to share the VHDL code for a 1 : 4 DEMUX (Demultiplexer) implemented using case statements. The entity port has one 1-bit input and one 2-bit select input. The outputs are four 1-bit wires.

1 to 4 demux in vhdl


VHDL Code for 1:4 DEMUX:


--library declarations.
library ieee;
use ieee.std_logic_1164.all;

entity demux1_4 is
port(bitin : in std_logic; --1 bit input wire 
    sel : in std_logic_vector(1 downto 0);  --select input
    out0 : out std_logic; --output wire 2
    out1 : out std_logic; --output wire 2
    out2 : out std_logic; --output wire 3
    out3 : out std_logic --output wire 4
    );
end demux1_4;

architecture Behavioral of demux1_4 is

begin

--make sure to include all signals in the process sensitivity list which affects the output.
--The below process contains the behavioral description of 1:4 DEMUX.
process(bitin,sel)
begin
case sel is
    when "00" => out0 <= bitin; out1 <= '0'; out2 <= '0'; out3 <='0';
    when "01" => out1 <= bitin; out0 <= '0'; out2 <= '0'; out3 <='0';
    when "10" => out2 <= bitin; out0 <= '0'; out1 <= '0'; out3 <='0';
    when others => out3 <= bitin;  out0 <= '0'; out1 <= '0'; out2 <='0';
end case;
end process;

end Behavioral;

The process above is a combinatorial process. Every time any of the signals in the process sensitivity list is changed, the process is 'invoked' and the logic inside is as if 'executed'. This explanation is just for beginners to get an inkling on what is going on. But don't take it too seriously, as you will need to refine this understanding as you get to know more of how VHDL works.

Testbench code for 1:4 DEMUX:


--library declarations
library ieee;
use ieee.std_logic_1164.all;

--testbench entity is always empty. no ports to be declared here.
entity testbench is
end testbench;

architecture behavior of testbench is

--internal signals
signal out0,out1,out2,out3,bitin :  std_logic := '0';
signal sel :  std_logic_vector(1 downto 0) := "00";

begin

--entity instantiation with named association style
demux_4_1 : entity work.demux1_4 
    port map(bitin => bitin,
        sel => sel,
        out0 => out0,
        out1 => out1,
        out2 => out2,
        out3 => out3);

stimulus : process
begin
    --set the input line
    bitin <= '1';
    --wait for 2 ns after changing 'select' input each time,
    --so that we can see the change in simulation waveform
    sel <="00"; 
    wait for 2 ns; 
    sel <="01";
    wait for 2 ns;
    sel <="10";
    wait for 2 ns;
    sel <="11";
    wait for 2 ns;
    --more input combinations can be given here.
    wait;
end process stimulus;

end;

Simulation Waveform:


The codes were simulated in Modelsim. This is a screenshot of the waveform.

simulation waveform of 1:4 demux in vhdl using modelsim

RTL Schematic:


The code was successfully synthesized in Xilinx ISE. The RTL schematic of the design is shown below.


rtl schematic of 1:4 demux in vhdl using xilinx ise
 
Note :- Use RTL Viewer to get a closer look on how your design is actually implemented in hardware.

Note2 :- You might have seen my blog article, why you should use signed/unsigned data types instead of std_logic_vector(SLV). But here the sel input is declared as std_logic_vector. You might be wondering, why?

This was a deliberate decision. The idea is not to completely abandon SLV, but use it only for signals of general purpose, where you don't need to perform any logical or arithmetical operations on them.

2 comments:

  1. why did't u use componant to call the demux code ?

    ReplyDelete
  2. ----when "01" => out1 <= bitin; out0 <= '0'; out2 <= '0'; out3 <='0';----
    Please check this line above

    ReplyDelete