2017-08-22 47 views
0

我想換班std_logic_vector如何移位std_logic_vector?

但是這個代碼是有錯誤:

architecture led_main of testing is 
    signal clk_cnt : std_logic_vector(64 DOWNTO 0) := (others => '0'); 
    signal led_buf : std_logic_vector(3 downto 0) := "0001"; 
begin 
    process(clk) 
    begin 
     if rising_edge(clk) then 
      clk_cnt <= clk_cnt + 1; 
      if clk_cnt >= 24999999 then 
       led_buf <= led_buf(0) & led_buf(3 downto 1); 
      end if; 
     end if; 
    end process; 

    ground <= '0'; 
end led_main; 

我覺得 「0001」, 「0010」, 「0100」 ......

+0

this co de led_buf <= led_buf(0)&led_buf(3 downto 1);更改led_buf <= led_buf srl 1; (error) –

+0

你想要移位或旋轉嗎?你的問題要求轉移,但你的代碼顯示旋轉。您是否想要向右或向左進行操作,因爲您的代碼位於右側,而您的文本顯示的是左側的示例! – Paebbels

+0

@YannVernier不,桶式移位器是移位器的一種實現形式,速度非常快,但也耗費資源。你可以延長桶轉換也做轉動,但是桶形轉換器描述了結構如何構建一個好的轉換器,而不是轉動。看到這個答案[n位桶式移位器](https://stackoverflow.com/questions/26551049/vhdl-n-bit-barrel-shifter)桶式移位器結構。 – Paebbels

回答

4

你的移位寄存器確定。實際上,它是一個旋轉器。

但是你的計數器是大的(65位),它不會在合適的時間內翻轉或重置爲零。您當前的設計等待25M週期,然後在每個週期從25M轉移到2 ** 64。

更重要的是,您正在使用非標準IEEE封裝對std_logic_vector執行算術運算(加法)。請使用包numeric_stdunsigned型號。

位,爲您的計數器所需要的數量可以用log2功能這樣獲得:

function log2ceil(arg : positive) return natural is 
    variable tmp : positive; 
    variable log : natural; 
begin 
    if arg = 1 then return 0; end if; 
    tmp := 1; 
    log := 0; 
    while arg > tmp loop 
     tmp := tmp * 2; 
     log := log + 1; 
    end loop; 
    return log; 
end function; 

來源:https://github.com/VLSI-EDA/PoC/blob/master/src/common/utils.vhdl

全部重寫代碼:

use IEEE.numeric_std.all; 

architecture led_main of testing is 
    constant CNT_MAX : positive := 25000000; 
    signal clk_cnt : unsigned(log2ceil(CNT_MAX) - 1 downto 0) := (others => '0'); 
    signal led_buf : std_logic_vector(3 downto 0)   := "0001"; 
begin 
    process(clk) 
    begin 
     if rising_edge(clk) then 
      clk_cnt <= clk_cnt + 1; 
      if clk_cnt = (CNT_MAX - 1) then 
       clk_cnt <= (others => '0'); 
       led_buf <= led_buf(0) & led_buf(3 downto 1); 
      end if; 
     end if; 
    end process; 
end led_main; 

` `