0
0
VHDLprogramming~10 mins

Priority encoder in VHDL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Priority encoder
Input bits
Check highest priority bit
Yes
Output index of that bit
No
Check next bit
No
Output default (e.g., zero)
The priority encoder checks input bits from highest to lowest priority and outputs the index of the first bit set to 1.
Execution Sample
VHDL
library ieee;
use ieee.std_logic_1164.all;

entity priority_encoder is
  port(input : in std_logic_vector(3 downto 0);
       output : out std_logic_vector(1 downto 0));
end entity;

architecture behavior of priority_encoder is
begin
  process(input) is
  begin
    if input(3) = '1' then
      output <= "11";
    elsif input(2) = '1' then
      output <= "10";
    elsif input(1) = '1' then
      output <= "01";
    elsif input(0) = '1' then
      output <= "00";
    else
      output <= "00"; -- default
    end if;
  end process;
end behavior;
This VHDL code implements a 4-bit priority encoder that outputs the index of the highest priority input bit set to 1.
Execution Table
StepInput bitsCheck bitConditionOutput assignedReason
1input = "0101"input(3)'0' = '1'? NoNoneHighest bit not set
2input = "0101"input(2)'1' = '1'? Yesoutput <= "10"Second highest bit set
3input = "0101"Stop checking-Output fixed at "10"Priority found, stop
4input = "0000"input(3)'0' = '1'? NoNoneNo bits set
5input = "0000"input(2)'0' = '1'? NoNoneNo bits set
6input = "0000"input(1)'0' = '1'? NoNoneNo bits set
7input = "0000"input(0)'0' = '1'? Nooutput <= "00"Default output
8input = "1000"input(3)'1' = '1'? Yesoutput <= "11"Highest bit set
9input = "1000"Stop checking-Output fixed at "11"Priority found, stop
💡 Stops checking once highest priority '1' bit is found or outputs default if none set.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 7After Step 8Final
input"0101""0101""0101""0101""0000""1000""1000"
output"00""00""10""10""00""00""11"
Key Moments - 3 Insights
Why does the encoder stop checking bits after finding the first '1'?
Because the priority encoder outputs the index of the highest priority bit set to '1' and ignores lower bits, as shown in execution_table rows 2-3 and 8-9.
What happens if no input bits are set to '1'?
The encoder outputs a default value ("00"), as shown in execution_table rows 4-7, ensuring a defined output even when no bits are active.
Why is the output "10" when input is "0101"?
Because input(2) is the highest priority bit set to '1' in "0101", so output is set to binary 2 ("10"), as shown in execution_table row 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 2 when input is "0101"?
A"11"
B"01"
C"10"
D"00"
💡 Hint
Check the 'Output assigned' column at step 2 in the execution_table.
At which step does the encoder output the default value "00"?
AStep 3
BStep 7
CStep 9
DStep 1
💡 Hint
Look for the step where no bits are set and output is assigned default in execution_table.
If input changes to "1001", what would be the output according to the priority encoder logic?
A"11"
B"00"
C"01"
D"10"
💡 Hint
Highest priority bit set is input(3), output is binary 3 ("11"), see similar case in step 8.
Concept Snapshot
Priority encoder checks input bits from highest to lowest.
Outputs binary index of first '1' bit found.
Stops checking after first '1'.
Outputs default if no bits set.
Common in hardware to reduce signals.
Full Transcript
A priority encoder in VHDL checks input bits starting from the highest priority bit down to the lowest. It outputs the binary index of the first bit set to '1'. If no bits are set, it outputs a default value, usually zero. The process stops checking bits once it finds the highest priority '1' bit. This behavior is shown in the execution table where inputs like "0101" result in output "10" because the second highest bit is set. When input is all zeros, the output defaults to "00". This logic helps hardware circuits decide which input has priority quickly.