Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9061429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:28:57+00:00 2026-06-16T15:28:57+00:00

Could you tell me if there is a better way to write a priority

  • 0

Could you tell me if there is a better way to write a priority encoder in VHDL than just using if-else statements? I mean, I have got this code and it works, but could it be written shorter? Switch is an std_logic_vector.

Thanks for your help!

shifting : PROCESS(CLK_IN, new_length_ready, switch)

    VARIABLE highest_switch :      INTEGER RANGE 0 TO 15 := 15;

BEGIN

    -- priority encoder, changes the length of the snake track 
    -- ( i.e., number of leds for the snake to move )

    IF switch(15) = '1' THEN
        highest_switch := 15;
    ELSIF switch(14) = '1' THEN
        highest_switch := 14;
    ELSIF switch(13) = '1' THEN
        highest_switch := 13;
    ELSIF switch(12) = '1' THEN
        highest_switch := 12;
    ELSIF switch(11) = '1' THEN
        highest_switch := 11;
    ELSIF switch(10) = '1' THEN
        highest_switch := 10;
    ELSIF switch(9) = '1' THEN
        highest_switch := 9;
    ELSIF switch(8) = '1' THEN
        highest_switch := 8;
    ELSIF switch(7) = '1' THEN
        highest_switch := 7;
    ELSIF switch(6) = '1' THEN
        highest_switch := 6;
    ELSIF switch(5) = '1' THEN
        highest_switch := 5;
    ELSIF switch(4) = '1' THEN
        highest_switch := 4;
    ELSIF switch(3) = '1' THEN
        highest_switch := 3;
    ELSIF switch(2) = '1' THEN
        highest_switch := 2;
    ELSIF switch(1) = '1' THEN
        highest_switch := 1;
    ELSIF switch(0) = '1' THEN
        highest_switch := 0;
    ELSE
        highest_switch := 15;
    END IF
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:28:59+00:00Added an answer on June 16, 2026 at 3:28 pm

    Within a process, you can use a loop.

    type Switches is range 0 to 15;
    
    shifting : PROCESS(switch)
       VARIABLE highest_switch : Switches := 15;
    begin
       for i in Switches loop
          if switch(i) = '1' then 
             highest_switch := i;
          end if;
       end loop
    ...
    end process;
    

    Notice that if several switches are set, the variable is assigned multiple times. However as the loop is executed in increasing order, the last (highest) assignment wins.

    Incidentally this process, like yours is combinational, but I removed unnecessary signals from the sensitivity list. It would be better to make it synchronous;

    shifting : PROCESS(clk)
    begin
       if rising_edge(clk) then
          for i in ...
       end if;
    end process;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering is there a way in PHP that you could tell where
Is there a method that could tell for a string like foo bar Foo
Is there a command-line tool that could tell me if Gzip is on? What
Ok guys, could you tell me is there a certain bug in current code:
I was wondering if someone could tell me the best way to call a
I was wondering if there was any way through php or javascript I could
was wondering if someone out there could tell me how to display a variable
Ok so i was wondering if anyone could tell me where i am going
I was wondering if anyone could tell me about the i++ operator in C#.
I was wondering if anyone could tell me if what I'm trying to accomplish

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.