2017-07-06 16 views
0

我剛剛寫了一個VHDL代碼,但它無法正常工作。我想用D觸發器編寫一個不穩定的計數器,計數如下:0 –> 13 –> 5 –> 7 –> 12 –> 6 –> 3 –> 15 –> 10 -> 0。 它應具有以下屬性:不穩定的計數器代碼通過VHDL

  1. 它應該是結構
  2. 四個輸出端(3下降到0)
  3. 它應該有一個異步復位
  4. 它應該有一個使能
  5. 它應該有一個活躍的高時鐘。

我在下面寫了我的代碼,但是我知道它不好。任何人都可以幫助我嗎?

library IEEE ;  
use IEEE.std_logic_1164.all ;   
-- and_gate 
entity and_gate is     
    port(Input1, Input2:in bit; 
    Output:out bit); 
end and_gate; 
architecture behavioral of and_gate is 
    begin 
    Output <= (Input1 and Input2) ; 
end behavioral; 
-- or_gate 
entity or_gate is     
    port(Input1, Input2:in bit; 
    Output:out bit); 
end or_gate;               
architecture behavioral of or_gate is 
    begin 
    Output <= (Input1 or Input2); 
end behavioral; 
-- not_gate 
entity not_gate is     
    port(Input : in bit; Output : out bit); 
end not_gate; 
architecture behavioral of not_gate is  
begin 
    Output <= not(Input); 
end behavioral; 
-- D-FF 
entity DFF is 
    port(CLK,RESET,EN,DIN :in bit; 
    COUNT :out bit);  
    end DFF; 
architecture behavioral of DFF is 
begin 
    LSEQ :process(RESET,CLK) 
    begin 
     if(RESET ='0')then 
      COUNT <='0'; 
     elsif(CLK'event and CLK ='1')then 
      if(EN ='1')then 
       COUNT <=DIN; 
     end if; 
    end if; 
end process; 
end behavioral; 
-- counter 
entity counter is 
    port (CLK,RESET,EN: in bit; 
     COUNT : out bit_vector(3 downto 0)); 
end counter; 

architecture counter_structural of counter is 

component and_gate         
    port(Input1,Input2 : in bit; Output: out bit); 
end component; 

component or_gate         
    port(Input1,Input2 : in bit; Output : out bit); 
end component; 

component not_gate         
    port(Input : in bit; Output : out bit); 
end component; 

component DFF 
    port(CLK,RESET,EN,DIN :in bit; COUNT :out bit);  
end component; 

signal A,B,C,D,ai,bi,ci,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15 : bit;       

begin 
    Gate1: not_gate port map (A,ai);     
    Gate2: not_gate port map (B,bi); 
    Gate3: and_gate port map (ai,bi,a1); 
    Gate4: and_gate port map (C,D,a2); 
    Gate5: or_gate port map (a1,a2,a3);  
    Gate6: DFF port map (CLK,RESET,EN,a3,COUNT(0)); 
    Gate7: and_gate port map (ai,B,a4); 
    Gate8: not_gate port map (c,ci);     
    Gate9: and_gate port map (ai,D,a5); 
    Gate10: or_gate port map (a4,ci,a6); 
    Gate11: or_gate port map (a5,a6,a7); 
    Gate12: DFF port map (CLK,RESET,EN,a7,COUNT(1)); 
    Gate13: and_gate port map (A,B,a8);    
    Gate14: and_gate port map (C,D,a9); 
    Gate15: or_gate port map (a8,a9,a10); 
    Gate16: DFF port map (CLK,RESET,EN,a10,COUNT(2)); 
    Gate17: and_gate port map (ai,B,a11); 
    Gate18: and_gate port map (ci,D,a12); 
    Gate19: and_gate port map (ai,D,a13); 
    Gate20: or_gate port map (a11,a12,a14); 
    Gate21: or_gate port map (a13,a14,a15); 
    Gate22: DFF port map (CLK,RESET,EN,a15,COUNT(3)); 
end counter_structural ; 
+0

[最小,完整和可驗證示例]的一部分(https://stackoverflow.com/help/mcve)已完成 - 重現問題和驗證所需的所有信息 - 「不起作用」不是問題陳述。什麼是不正確的? (它在做什麼?)你有測試臺嗎?計數順序是否需要? – user1155120

+0

請包括這個結構的原理圖。基於這段代碼,它看起來像是一堆連接着我的門。 – JHBonarius

回答

0

爲此,您可以使用LFSR(線性反饋移位寄存器)。 如果您選擇正確的參數並糾正第一個值,您會得到一個僞隨機生成器。 好的一點是,網上提供了正確的參數和第一個值。 (谷歌應該足夠快地提供給你)

0

使用環形計數器。環形計數器只是n個寄存器,其輸入和輸出鏈接在一個環中。最終寄存器的輸出也可以用作計數器的輸出。您想要輸出的值可以用作寄存器的初始值和復位值。確保寄存器的初始值與你想輸出的相反,因爲環形計數器將在寄存器n上輸出初始值,然後輸出n-1,n-2等。

在這種情況下,你可以使用:

register value 
0  0 
1  10 
2  15 
3  3 
4  6 
5  12 
6  7 
7  5 
8  13 
9  0 

編輯:使用環形計數器的另一個好處是重用組件。結構環計數器只需要一個組件,一個寄存器。這將阻止你的代碼被所有組件聲明臃腫,就像它現在一樣。