2013-11-10 122 views
1

我正在實施Booth算法,用於將VHDL中的兩個數字(無符號2的補碼形式)相乘。不幸的是,我對VHDL很窮,無法弄清楚我的錯在哪裏。

的問題:通過模擬步進,我注意到,當我已經分配的「1011」的值Y,信號MULT了「0UUU」。我不明白爲什麼會發生。這裏是我的代碼:VHDL信號分配導致未初始化狀態

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

-- x, y are the n bit numbers to be multiplied. 
-- The Algorithm : 
-- U = 0, prev_bit = 0; 
-- for i = 1 to n do 
-- if start of a string of 1's in Y then U = U - X 
-- if end of a string of 1's in Y then U = U + X 
-- Arithmetic right shift UV 
-- Circular right shift Y and copy Y(0) to prev_bit 

entity booth is 
    generic(N : natural := 4); 
    port(
    x, y : in std_logic_vector(N-1 downto 0); 
    result : out std_logic_vector(2*N-1 downto 0); 
    clk : in std_logic 
); 
end booth; 

architecture booth_arch of booth is 
    --temp is equivalent to UV where UV is the result. 
    signal temp : std_logic_vector(2*N-1 downto 0) := (others => '0'); 
    --prev_bit to compare for starting and ending of strings of 1's. 
    signal prev_bit : std_logic := '0'; 
    signal mult : std_logic_vector(N-1 downto 0); 

    begin 
    process(x, y) 
     begin 
     mult <= y; 
     prev_bit <= '0'; 
     for i in 0 to N-1 loop 
      if(mult(0) = '1' and prev_bit = '0') then --start of a string of 1's 
      temp(2*N-1 downto N) <= std_logic_vector(unsigned(temp(2*N-1 downto N)) + unsigned(not(x)) + 1); 
      elsif(mult(0) = '0' and prev_bit = '1') then --end of a string of 1's 
      temp(2*N-1 downto N) <= std_logic_vector(unsigned(temp(2*N-1 downto N)) + unsigned(x)); 
      end if;  
     prev_bit <= mult(0); 
     mult(N-2 downto 0) <= mult(N-1 downto 1); --circular right shift y. 
     mult(N-1) <= prev_bit; 
     temp(2*N-2 downto 0) <= temp(2*N-1 downto 1); --arithmetic right shift temp. 
     end loop; 
     result <= temp; 
    end process;  
end booth_arch; 

P.S:clk信號在這裏是多餘的。我還沒有用過它。

回答

1

如果您的端口和內部信號是無符號的,請將其聲明爲unsigned,作爲開始。至少你正在使用正確的numeric_std庫。使用強類型系統,而不是與它戰鬥!

然後,您可能需要在每次乘法開始時初始化Temp(如您已經對Mult, Prev_Bit所做的那樣),而不是在模擬開始時進行一次初始化。目前,Temp似乎可能包含先前乘法的陳舊值(例如UUUU * UUUU)。

第三你能告訴我們你所分配到Y,但我們還不知道你是分配給X這可能仍然是UUUU所有我知道的。

編寫一個最小的VHDL測試平臺並將其添加到問題中將是得到進一步幫助的好方法 - 或者更可能的是,自己發現問題的真正原因!

1

除了Brian的評論:你正在讀寫信號mult在同一個組合過程中。除非你的真的知道你在做什麼,你不應該這樣做。綜合之後,你會得到與你的模擬器不相符的東西。

此外,您應該知道,您分配給mult(在您的流程的第一行)的值在流程完成並開始新的迭代之後纔會顯示。在VHDL中 - 我們說新的值在一個增量循環後可用。