Encoders

 

encoders are used to encode discrete data into a coded form


truth table for an 4-2 binary encoder



 
library IEEE; 

use IEEE.std_logic_1164.all;

use IEEE.numeric_std.all;

entity encod_4to2 is

    port(a3,a2,a1,a0: in std_logic;x1,x0: out std_logic);
end encod_4to2; 
 
architecture data_flow_1 of encod_4to2 is 

signal tmp_in: unsigned(3 downto 0);

signal tmp_out: unsigned(1 downto 0);

begin

    tmp_in <= a3 & a2 & a1 & a0;

    -- conditional signal assignment

    tmp_out <= "00" when tmp_in="0001" else

        "01" when tmp_in="0010" else

        "10" when tmp_in="0100" else

        "11" when tmp_in="1000" else

        "XX"; -- default value

    x1 <= tmp_out(1);

    x0<= tmp_out(0);

end data_flow_1;
 
architecture data_flow_2 of encod_4to2 is 

signal tmp_in: unsigned(3 downto 0);

signal tmp_out: unsigned(1 downto 0);

begin

    tmp_in <= a3 & a2 & a1 & a0;

    with tmp_in select 

    tmp_out <= "00" when "0001" else

        "01" when "0010" else

        "10" when "0100" else

        "11" when "1000" else

        "XX"; -- default value

    x1 <= tmp_out(1);

    x0 <= tmp_out(0);

end data_flow_2;
 
architecture process_enc of encod_4to2 is 

begin

process(a3,a2,a1,a0)

variable tmp_in: unsigned(3 downto 0);

variable tmp_out: unsigned(1 downto 0);

begin

    tmp_in := a3 & a2 & a1 & a0;

    case tmp_in is 

      when "0001" => tmp_out := "00";

      when "0010" => tmp_out := "01";

      when "0100" => tmp_out := "10";

      when "1000" => tmp_out := "11";

      when others => tmp_out := "XX";

    end case;

    x1 <= tmp_out(1);

    x0 <= tmp_out(0);

    end process;

end process_enc;