Skip to content

Instantly share code, notes, and snippets.

@kimushu
Last active December 12, 2015 12:09
Show Gist options
  • Save kimushu/4770004 to your computer and use it in GitHub Desktop.
Save kimushu/4770004 to your computer and use it in GitHub Desktop.
Parametrized Priority Encoder ((2**W) to W-Line)
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use ieee.math_real.all;
entity priority_encoder is
generic (
W: integer := 3
);
port (
input : in std_logic_vector(2**W-1 downto 0);
output : out std_logic_vector(W-1 downto 0);
eout : out std_ulogic
);
end priority_encoder;
architecture logic of priority_encoder is
procedure PRI_ENC (
signal input : in std_logic_vector;
signal output : out std_logic_vector;
signal eout : out std_logic
) is
constant W : integer := integer(ceil(log2(real(input'length))));
variable highest : std_logic_vector(W-1 downto 0) := (others => '0');
variable no_ones : std_logic := '1';
begin
for i in input'reverse_range loop
if (input(i) = '1') then
highest := std_logic_vector(to_unsigned(i, W));
no_ones := '0';
end if;
end loop;
output <= highest;
eout <= no_ones;
end procedure;
begin
PRI_ENC(input, output, eout);
end logic;
-- vim:sts=2 sw=2 et:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment