2013-11-10 54 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中 - 我们说新的值在一个增量循环后可用。