2013-03-14 97 views
1

下面是代碼:VHDL代碼作用奇怪(小的代碼,其中,可變保持其值並且犯規重置)

entity main is 
port(input:in unsigned(99 downto 0); 
clk:in std_logic; 
output:out unsigned(99 downto 0) 
); 
end main; 

architecture Behavioral of main is 

begin 

process(clk) 

variable x:unsigned(99 downto 0):=X"27c8a94a6fb72a00000000000"; 
begin 
if(clk'event and clk='1') then 

x:=(x*input);// this line is a problem!! 

output<=x; 
x:=X"27c8a94a6fb72a00000000000";// i have to rest x manually :S 

end if; 
end process; 
end Behavioral; 

第一個問題是X雖然一個變量,並應重置每個時間進程運行它不'並保存它的價值!我已經在模擬中一步一步地看到它。 第二個問題是雖然輸入固定到這個實體,並且不會因每個時鐘進程而改變x或者是x *輸入(正確答案)或者0(錯誤答案) 到時候我的時鐘停止滴答停止在0答案,if我減少時鐘一點它可能會停止在正確的答案(這是輸入* x) 我的問題是爲什麼輸入* x不固定,如果輸入和x是固定的,第二個是爲什麼x不重置時再次調用過程從其敏感度列表中)。 這裏是測試代碼:


LIBRARY ieee; 
USE ieee.std_logic_1164.ALL; 
use IEEE.numeric_std.all; 
-- Uncomment the following library declaration if using 
-- arithmetic functions with Signed or Unsigned values 
--USE ieee.numeric_std.ALL; 

ENTITY test IS 
END test; 

ARCHITECTURE behavior OF test IS 

    -- Component Declaration for the Unit Under Test (UUT) 

    COMPONENT main 
    PORT(
     input : IN unsigned(99 downto 0); 
     clk : IN std_logic; 
     output : OUT unsigned(99 downto 0) 
     ); 
    END COMPONENT; 


    --Inputs 
    signal input : unsigned(99 downto 0) := (others => '0'); 
    signal clk : std_logic := '0'; 

    --Outputs 
    signal output : unsigned(99 downto 0); 

    -- Clock period definitions 
    constant clk_period : time := 10 ns; 

BEGIN 

    -- Instantiate the Unit Under Test (UUT) 
    uut: main PORT MAP (
      input => input, 
      clk => clk, 
      output => output 
     ); 

    -- Clock process definitions 
    clk_process :process 
    begin 
     clk <= '0'; 
     wait for clk_period/2; 
     clk <= '1'; 
     wait for clk_period/2; 
    end process; 


    -- Stimulus process 
    stim_proc: process 
    begin   
     -- hold reset state for 100 ns. 
     wait for 100 ns; 
input<=X"000000000000000000000000F"; 
--input<="000000000001"; 
     wait for clk_period*10; 

     -- insert stimulus here 

     wait; 
    end process; 

END; 

///

注意,在實驗開始的過程中沒有插入輸入值多次運行,在這種情況下,我不關心結果,我在插入輸入時發生此問題。

回答

0

的問題是,需要將這一引線長度100的2個的無符號值到200的結果(見 numeric_std包!)。你可以解決你的問題如下:

entity main is 
port(
input:in unsigned(99 downto 0); 
clk:in std_logic; 
output:out unsigned(199 downto 0) 
); 
end main; 

architecture Behavioral of main is 

begin 
process(clk) 
    variable x:unsigned(99 downto 0); 
    variable x_res:unsigned(199 downto 0); 
begin 
    if(clk'event and clk='1') then 
     x:=(input); 
     x_res:=(x*x); 
     output<=x_res; 
    end if; 
end process; 
end Behavioral; 
1

要理解的第一件事是你永遠不要調用一個過程!

一個進程只啓動一次 - 所以變量只被初始化一次。

它運行完成並休眠 - 下一個時鐘事件將其喚醒,並在每個時鐘週期從頭到尾執行。

這可能有助於將一個進程想象成一個帶有事件循環的C主程序;它會一直睡到事件發生,並且你聲明的變量是一個全局變量(或「靜態」變量 - 它保持它的最後一個值)。如果你想要C函數或過程的效果(咳嗽,void函數),寫一個VHDL函數或過程;如果需要C函數或過程的效果(咳嗽,void函數)每次在流程中調用它們時,將重新初始化這些變量中的局部變量。

所以經過1個時鐘週期你看x * input,你看到的x * input * input * input ... * input最低顯著100位幾個時鐘週期之後,由於乘法字的增長,最終將全部爲0。

如果所有你想做的事由X的初始值乘以每一個新的輸入,那麼你將是更好的聲明X恆定的,並且簡單地寫爲:

process(clk) 
constant x:unsigned(99 downto 0):=X"27c8a94a6fb72a00000000000"; 
begin 
    if rising_edge(clk) then 
     output <= x * input; 
    end if; 
end process; 
+0

可悲的是並非如此,因爲我在模擬器中一步一步的簡歷看成爲「E1」(corrwct答案)然後sudeenly就變成0?E1 * E1不爲零時,我不爲什麼發生這種情況,我通過inializing其重置每個運行x值我改變碼本: 過程(CLK) 變量x:無符號(99 DOWNTO 0); 開始 如果(clk'event並且clk = '1'),那麼 X:=(輸入); x:=(x * x); output <= x; end if; 結束過程; 結束行爲; – 2013-03-14 16:17:17

+0

您發佈的代碼中沒有「cv」,因此我無法理解此評論。進一步的「000F」*「xxxxxxx_x000_0000_0000」不可能等於「e1」。我的答案中有一個錯誤(現在正在編輯),但它不會影響我推薦的前進方向。 – 2013-03-14 16:28:46