用 VHDL 中的定点表示乘法

Multiplication with Fixed point representation in VHDL

对于定点运算,我用 0000 0010101010100110 表示 0.166 并乘以它。为此,我在 VHDL 中编写了如下代码。输出在 y 中分配,它有符号 41 位。对于有符号乘法 A(a1,b1)*A(a2,b2)=A(a1+a2+1,b1+b2)。然而在模拟过程中它给出了一个错误

      Target Size 41 and source size 40 for array dimension 0 does not match. 

代码:

 entity file1 is
    Port ( y : out signed(40 downto 0));
 end file1;

 architecture Behavioral of file1 is

 signal a : signed(19 downto 0) := "00000010101010100110";
 signal b : signed(19 downto 0) := "00000010101010100110";

 begin
    y<= (a*b);    ----error
 end Behavioral;

19+1位乘以19+1位的结果是39+1位,而你的端口是40+1位长。例如,让我们乘以 19 位的最大可能值:0x7FFFF * 0x7FFFF = 0x3FFFF00001 - 因此无符号结果为 39 位(19 + 19 + 进位),符号为 +1 位。

所以你应该 "normalize" 将结果扩展到 1 位,这应该等于结果的符号 (bit#40 = bit#39) 或者只选择 40 位端口作为输出:

Port ( y : out signed(39 downto 0))

如果你真的需要冗余的第41位:

begin
   y(39 downto 0) <= (a*b)
   y(40) <= y(39)
end Behavioral;

或者只使用 resize 签名函数:How to convert 8 bits to 16 bits in VHDL?