Priority encoders


priority encoders are used to encode the position of the active input with the highest priority


truth table for an 4-2 priority encoder: a3 is the highest priority input


library IEEE;

use IEEE.std_logic_1164.all;

use IEEE.numeric_std.all;

entity prenc_4to2 is

    port(a: in unsigned(3 downto 0); v: out std_logic; x out unsigned (1 downto 0));

end prenc_4to2;

architecture process_prenc_1 of encod_4to2 is

begin

process(a)

variable int_a: integer range 0 to 15;

begin

    int_a := to_integer(a); -- from IEEE.numeric_std

    v <='1';

    case int_a is

      when 8 to 15 => x <= "11";

      when 4 to 7 =>  x <= "10";

      when 2 to 3 =>  x <= "01";

      when 1 =>  x <= "00";

      when others => v <='0'; x <="XX";

    end case;

end process;

end process_prenc_1;

architecture process_prenc_2 of encod_4to2 is

begin

process(a)

begin

    x <= "XX"; v <= '0';

    for i in 3 downto 0 loop

      if(a(i)='1') then

        x <= to_unsigned(i,2);

        -- from IEEE.numeric_std

        v <= '1';

      exit;

      end if;

    end loop;

end process;

end process_prenc_2;