2015-01-26 19 views
0

我遇到了一個問題,我不明白爲什麼... 例如,我做出這樣的聲明:VHDL,在使用兩個變量的問題來算

variable compteur1,compteur2 : natural range 0 to 15; 
process(H) 
begin 
    if(rising_edge(H)) then 
     compteur1 := compteur1 + 1; 
     if (compteur1 = 9) then 
      compteur2 := compteur2 + 1; 
     end if; 
    end if; 
end process; 

爲什麼只有compteur1正常計數從0到15,而compteur2只運行一次?

+0

看起來很可疑,就好像第一行有一個錯字(*除了*一般的電腦的奇怪拼寫)。確保重新輸入的代碼中沒有其他錯誤,呃? – 2015-01-26 23:44:16

+0

@NathanTuggy - 我相信compteur是法國人的反擊。問題是隻有拼寫錯誤。凱文本來可以抓住他們。 – user1155120 2015-01-27 00:01:59

+0

@DavidKoontz當然,但爲什麼有人用中文用戶名使用法語變量名? – 2015-01-27 00:04:31

回答

0

因爲你有一個聲明,說當computer1 = 9,然後使計算機2的價值提前。這隻發生一次。

+0

但是compter1在0到15之間變化,到達15時會變成0? – 2015-01-26 23:22:08

+0

看看你的陳述。當computer1達到值'9'時,你只增加computer2。 – 2015-01-26 23:28:13

+1

@邢碧舟No. VHDL整數不會自動翻轉。如果你模擬這個,你應該會得到一個錯誤,並且當它是15時嘗試增加'compter1'。然而合成器會忽略這個錯誤,並且允許無聲翻轉,因爲沒有實際的方法來自動處理硬件中的錯誤條件。如果你想要滾動,那麼當達到15或者使用'compter1:=(compter1 + 1)mod 16'時,你必須將'compter1'設置爲0。 – 2015-01-26 23:28:17

1

有幾件事情錯了,側翻凱文提到,還有一些錯別字的名字:

此:

package compt is 
    shared variable compteur1,compteur2 : natural range 0 to 15; 
end package; 

library ieee; 
use ieee.std_logic_1164.all; 
use work.compt.all; 

entity foo is 
end entity; 

architecture fum of foo is 
    signal H: std_logic := '0'; 
begin 

CLOCK: 
    process 
    begin 
     wait for 10 ns; 
     H <= not H; 
     if Now > 600 ns then 
      wait; 
     end if; 
    end process; 

UNLABELLED: 
    process(H) 
    begin 
     if(rising_edge(H)) then 
      compteur1 := compteur1 + 1; 
      report "compteur1 = " & natural'image(compteur1); 
      if (compteur1 = 9) then 
       compteur2 := compteur2 + 1; 
       report "compteur2 = " & natural'image(compteur2); 
      end if; 
     end if; 
    end process; 
end architecture; 

,因爲你增加compteur1超越它必將使運行時錯誤:

compteur.vhdl:31:13:@10ns:(report note): compteur1 = 1
compteur.vhdl:31:13:@30ns:(report note): compteur1 = 2
compteur.vhdl:31:13:@50ns:(report note): compteur1 = 3
compteur.vhdl:31:13:@70ns:(report note): compteur1 = 4
compteur.vhdl:31:13:@90ns:(report note): compteur1 = 5
compteur.vhdl:31:13:@110ns:(report note): compteur1 = 6
compteur.vhdl:31:13:@130ns:(report note): compteur1 = 7
compteur.vhdl:31:13:@150ns:(report note): compteur1 = 8
compteur.vhdl:31:13:@170ns:(report note): compteur1 = 9
compteur.vhdl:34:17:@170ns:(report note): compteur2 = 1
compteur.vhdl:31:13:@190ns:(report note): compteur1 = 10
compteur.vhdl:31:13:@210ns:(report note): compteur1 = 11
compteur.vhdl:31:13:@230ns:(report note): compteur1 = 12
compteur.vhdl:31:13:@250ns:(report note): compteur1 = 13
compteur.vhdl:31:13:@270ns:(report note): compteur1 = 14
compteur.vhdl:31:13:@290ns:(report note): compteur1 = 15
./foo:error: bound check failure at compteur.vhdl:30
./foo:error: simulation failed
ghdl: compilation error

因爲VHDL沒有模塊化整數類型和您使用的共享變量,您可以測試,你增加:

UNLABELLED: 
    process(H) 
    begin 
     if(rising_edge(H)) then 
      if compteur1 = 15 then 
       compteur1 := 0; 
      else 
       compteur1 := compteur1 + 1; 
      end if; 
      report "compteur1 = " & natural'image(compteur1); 
      if (compteur1 = 9) then 
       compteur2 := compteur2 + 1; 
       report "compteur2 = " & natural'image(compteur2); 
      end if; 
     end if; 
    end process; 

這會給你compteur2倍數的增量:

compteur.vhdl:35:13:@10ns:(report note): compteur1 = 1
compteur.vhdl:35:13:@30ns:(report note): compteur1 = 2
compteur.vhdl:35:13:@50ns:(report note): compteur1 = 3
compteur.vhdl:35:13:@70ns:(report note): compteur1 = 4
compteur.vhdl:35:13:@90ns:(report note): compteur1 = 5
compteur.vhdl:35:13:@110ns:(report note): compteur1 = 6
compteur.vhdl:35:13:@130ns:(report note): compteur1 = 7
compteur.vhdl:35:13:@150ns:(report note): compteur1 = 8
compteur.vhdl:35:13:@170ns:(report note): compteur1 = 9
compteur.vhdl:38:17:@170ns:(report note): compteur2 = 1
compteur.vhdl:35:13:@190ns:(report note): compteur1 = 10
compteur.vhdl:35:13:@210ns:(report note): compteur1 = 11
compteur.vhdl:35:13:@230ns:(report note): compteur1 = 12
compteur.vhdl:35:13:@250ns:(report note): compteur1 = 13
compteur.vhdl:35:13:@270ns:(report note): compteur1 = 14
compteur.vhdl:35:13:@290ns:(report note): compteur1 = 15
compteur.vhdl:35:13:@310ns:(report note): compteur1 = 0
compteur.vhdl:35:13:@330ns:(report note): compteur1 = 1
compteur.vhdl:35:13:@350ns:(report note): compteur1 = 2
compteur.vhdl:35:13:@370ns:(report note): compteur1 = 3
compteur.vhdl:35:13:@390ns:(report note): compteur1 = 4
compteur.vhdl:35:13:@410ns:(report note): compteur1 = 5
compteur.vhdl:35:13:@430ns:(report note): compteur1 = 6
compteur.vhdl:35:13:@450ns:(report note): compteur1 = 7
compteur.vhdl:35:13:@470ns:(report note): compteur1 = 8
compteur.vhdl:35:13:@490ns:(report note): compteur1 = 9
compteur.vhdl:38:17:@490ns:(report note): compteur2 = 2
compteur.vhdl:35:13:@510ns:(report note): compteur1 = 10
compteur.vhdl:35:13:@530ns:(report note): compteur1 = 11
compteur.vhdl:35:13:@550ns:(report note): compteur1 = 12
compteur.vhdl:35:13:@570ns:(report note): compteur1 = 13
compteur.vhdl:35:13:@590ns:(report note): compteur1 = 14
compteur.vhdl:35:13:@610ns:(report note): compteur1 = 15

這正常退出(見時鐘進程if語句)。

看着時間標記,你還會看到compteur1在同一邊緣上使用變量導致的compteur2增量變爲9。

當使用遞增之前,你可能會考慮估計變量:

UNLABELLED: 
    process(H) 
    begin 
     if(rising_edge(H)) then 
      if (compteur1 = 9) then 
       compteur2 := compteur2 + 1; 
       report "compteur2 = " & natural'image(compteur2); 
      end if; 
      if compteur1 = 15 then 
       compteur1 := 0; 
      else 
       compteur1 := compteur1 + 1; 
      end if; 
      report "compteur1 = " & natural'image(compteur1); 
     end if; 
    end process; 

這將是如果你使用的信號的順序一致:

compteur.vhdl:39:13:@490ns:(report note): compteur1 = 9
compteur.vhdl:32:17:@510ns:(report note): compteur2 = 2
compteur.vhdl:39:13:@510ns:(report note): compteur1 = 10
compteur.vhdl:39:13:@530ns:(report note): compteur1 = 11

下一個時鐘邊沿遞增compteur2。

正如QuantumRipple指出

如果你簡單地回答一個問題,而不是提供完整的解決方案,你可能會錯過一些東西。在這種情況下,側翻在comptuer2的影響,這是可以治癒的,他表示做同樣的事情吧:

process(H) 
    begin 
     if rising_edge(H) then 
      if compteur1 = 9 then 
       if compteur2 = 15 then 
        compteur2 := 0; 
       else 
        compteur2 := compteur2 + 1; 
       end if; 
       report "compteur2 = " & natural'image(compteur2); 
      end if; 
      if compteur1 = 15 then 
       compteur1 := 0; 
      else 
       compteur1 := compteur1 + 1; 
      end if; 
      report "compteur1 = " & natural'image(compteur1); 
     end if; 
    end process; 

有我們能夠立刻在問題的原代碼介意至少一個其他特點。信號分配中不涉及計數器變量,其中表示不會由綜合產生硬件。

+0

而爲了讓這個更強大,你可以添加一個明確的側翻compteur2爲好。只有將它添加到compteur1纔會延遲,而不是解決問題。 – QuantumRipple 2015-01-28 21:08:15