我正在實施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信號在這裏是多餘的。我還沒有用過它。