2013-11-20 16 views
1

這是一個VHDL代碼序列1-2-3-4-6-7計數,但似乎計數形式1-7 的代碼似乎有一個邏輯錯誤的地方。請幫助我想實現在一個VHDL計數器1-2-3-4-6,但計數遞增形式1-7。

library ieee; 
use ieee.std_logic_1164.all; 
use ieee.std_logic_unsigned.all; 

entity newtest is 
port(C, CLR : in std_logic; 
Q : out std_logic_vector(2 downto 0)); 
end newtest; 
architecture archi of newtest is 
signal tmp: std_logic_vector(2 downto 0); 
begin 
process (C, CLR) 
begin 
if (CLR='1') then 
tmp <= "000"; 
elsif (C'event and C='1') then 
if (tmp="100") then 
tmp <= tmp + 1; 
end if; 
tmp <= tmp + 1; 
end if; 
end process; 
Q <= tmp; 
end archi; 
+0

信號賦值語義的基本誤解的附加項的輸入。 http://stackoverflow.com/questions/13954193/is-process-in-vhdl-reentrant/13956532#13956532應該犯的錯誤明確。 '如果(TMP = 「100」),那麼 TMP <= TMP + 2;'可能工作 –

回答

1

在兩個順序信號分配給tmp的過程中,不會按時間分隔,將會發生後面的分配。信號具有當前值和未來值。信號分配直到當前模擬週期後纔會更新。在時鐘C'event和C ='1'的下一個模擬週期中,您已經更新了未來值,然後才能將其分配給當前值。

下面以numeric_std包代替Synopsys公司std_logic_unsigned包而不改變TMP的類型,因此,類型轉換和從無符號的。我只是不想轉移我的ieee庫來包含不符合標準的東西。您可以使用std_logic_unsigned並刪除類型轉換。如果分配給Q(Q < = std_logic_vector(tmp);),或者如果可能的話使Q和tmp都無符號,那麼你也可以聲明信號tmp爲unsigned(2 downto 0)

library ieee; 
use ieee.std_logic_1164.all; 
--use ieee.std_logic_unsigned.all; 
use ieee.numeric_std.all; 

entity newtest is 
    port(C, CLR : in std_logic; 
     Q :  out std_logic_vector(2 downto 0)); 
end newtest; 
architecture archi of newtest is 
    signal tmp: std_logic_vector(2 downto 0); 
begin 
    process (C, CLR) 
    begin 
     if (CLR='1') then 
      tmp <= "000"; 
     elsif (C'event and C='1') then 
      if (tmp="100") then 
       tmp <= std_logic_vector (unsigned (tmp) + 2); 
      else 
       tmp <= std_logic_vector (unsigned (tmp) + 1); 
      end if; 
     end if; 
    end process; 
    Q <= tmp; 
end archi; 

現在只有一次賦值給tmp,它應該從「100」到「110」。有人必然會指出tmp可能是一個無符號而不是std_logic_vector,或者tmp可能是一個整數而不是兩者之一。

至於合成硬件由2添加增量需要用於TMP輸入的最右邊的兩個比特

+0

感謝。這工作:) – Thejdeep