2013-01-01 144 views
5

短的路你能告訴我,如果有更好的方式來寫VHDL優先編碼器比只使用if-else語句?我的意思是,我有這個代碼,它的工作原理,但它可以寫得更短?開關是一個std_logic_vector。寫VHDL優先編碼器

感謝您的幫助!

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 

回答

8

在一個過程中,您可以使用循環。

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; 

注意,如果多個開關被設置時,所述可變分配多次。然而,隨着循環以遞增順序執行,最後(最高)的分配獲勝。

順便提及這個過程中,像您是組合,但我刪除從靈敏度列表不需要的信號。讓它同步會更好;

shifting : PROCESS(clk) 
begin 
    if rising_edge(clk) then 
     for i in ... 
    end if; 
end process;