我正在使用VHDL创建通用2的补码乘法器。我正在遵循Baugh Wooley算法,并且正在使用数组结构的乘法器方法来执行计算。我遇到的问题是,当我将5位乘以4位数字时,它会正确计算前3个乘积位,而不是其余位。我的代码适用于4位乘4位乘法。
Simulation Array Structure Algorithm
我将问题缩小到数组结构似乎无法正确生成SUM位的事实。我认为这是由于generate语句中的索引问题,但由于我的隧道视野,我无法找到错误。
--Main MULT entity--
LIBRARY IEEE;
use IEEE.std_logic_1164.all;
entity MULT is
generic(X_len:positive:=4;Y_len:positive:=4);
port(X:in std_logic_vector(X_len-1 downto 0);
Y: in std_logic_vector(Y_len-1 downto 0);
P: out std_logic_vector(X_len+Y_len-1 downto 0));
end;
architecture behav of MULT is
type twoD_array is array(X_len downto 0, Y_len+X_len-2 downto 0) of std_logic;
signal XY: twoD_array;
signal carry: twoD_array;
signal S: twoD_array;
--Declaring HA/FA components--
component HA is
port(a, b : in std_logic;
s, c_out : out std_logic);
end component;
component FA is
port(a, b, c_in : in std_logic;
s, c_out : out std_logic);
end component;
component AND_2 is
port(A,B:in std_logic;
C:out std_logic);
end component;
component NAND_2 is
port(A,B:in std_logic;
C:out std_logic);
end component;
-----------------------------
begin
--Generating Partial Products (XY)--
genPPouter: for r in 0 to X_len-1 generate
genPPinner: for c in 0 to Y_len-1 generate
notlastindex: if (r/=X_len-1) and (c/=Y_len-1) generate
PProw: AND_2 port map(X(r),Y(c),XY(r,c+r));
end generate;
lastindex: if (r/=X_len-1) and (c=Y_len-1) generate
PPlastin: NAND_2 port map(X(r),Y(c),XY(r,c+r));
end generate;
lastrownotlastin: if (r=X_len-1) and (c/=Y_len-1) generate
PPlastrwo: NAND_2 port map(X(r),Y(c),XY(r,c+r));
end generate;
lastrowlastin: if (r=X_len-1) and (c=Y_len-1) generate
PPlastrowlastin: AND_2 port map(X(r),Y(c),XY(r,c+r));
end generate;
end generate;
end generate;
P(0)<=XY(0,0);
--Generating first row components--
genHAorFA: for a in 1 to Y_len-1 generate
firstrwoHAXneqY: if (X_len/=Y_len) and ((a/=X_len-1) and (a/=Y_len-1)) generate
HAgen: HA port map(XY(0,a),XY(1,a),s(1,a),carry(1,a));
end generate;
firstrowFAXneqY: if (X_len/=Y_len) and ((a=X_len-1) and (a=Y_len-1)) generate
FAgen: FA port map(XY(0,a),XY(1,a),'1',s(1,a),carry(1,a));
end generate;
firstrowHAXeqY: if (X_len=Y_len) generate
HAgen2: HA port map(XY(0,a),XY(1,a),s(1,a),carry(1,a));
end generate;
end generate;
P(1)<=s(1,1);
--Generating FAs--
genFAsouter: for r in 2 to X_len-1 generate
genFAsinner: for c in 2 to Y_len generate
genFAnotlastindex: if (c/=Y_len) generate
genFA: FA port map(XY(r,c+r-2),s(r-1,c+r-2),carry(r-1,c-1+r-2),s(r,c+r-2),carry(r,c+r-2));
end generate;
genFAlastindex: if (c=Y_len) generate
genFA2: FA port map(XY(r,c+r-2),XY(r-1,c+r-2),carry(r-1,c-1+r-2),s(r,c+r-2),carry(r,c+r-2));
end generate;
end generate;
P(r)<=s(r,r);
end generate;
--Generating RCA--
genRCAX: for c in X_len to Y_len+X_len-2 generate
genFirstHA: if (X_len/=Y_len) and (c=X_len) generate
genHA: HA port map(s(X_len-1,c),carry(X_len-1,c-1),s(X_len,c),carry(X_len,c));
P(c)<=s(X_len,c);
end generate;
genfirstFAifXeqY: if (X_len=Y_len) and (c=X_len) generate
genfirstFA: FA port map(s(X_len-1,c),carry(X_len-1,c-1),'1',s(X_len,c),carry(X_len,c));
P(c)<=s(X_len,c);
end generate;
genIntermediateFA: if (c/=X_len) and (c/=X_len+Y_len-2) generate
genIntFA: FA port map(s(X_len-1,c),carry(X_len-1,c-1),carry(X_len,c-1),s(X_len,c),carry(X_len,c));
P(c)<=s(X_len,c);
end generate;
genLastFA: if (c=X_len+Y_len-2) generate
genlastFA: FA port map(XY(X_len-1,c),carry(X_len-1,c-1),carry(X_len,c-1),s(X_len,c),carry(X_len,c));
P(c)<=s(X_len,c);
end generate;
end generate;
--Generate HA to add 1 at X_len+Y_len-1--
genfinalHA: HA port map(carry(X_len,Y_len+X_len-2),'1',P(X_len+Y_len-1),carry(0,0));
end behav;
我应该在波形上得到实际的2的补码结果,但是我只生成前3个乘积位。