如何在 vhdl 中找到两个向量的点积?

How to find dot product of two vectors in vhdl?

我是 VHDL 新手。我正在尝试为 Xilinx FPGA 上的向量点积或标量积设计通用代码。假设我们有一个向量两个向量

V1=[1,4,5,1] and V2=[3,6,9,1].

我们可以使用

找到它
V1.V2=(1x3)+(4x6)+(5x9)+(1x1)=73
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.numeric_std.ALL;

entity dot_product is
    Port ( vector_x : in STD_LOGIC_VECTOR (3 downto 0);
           vector_y : in STD_LOGIC_VECTOR (3 downto 0);
           r0 : out STD_LOGIC_VECTOR (3 downto 0);
           r1 : out STD_LOGIC_VECTOR (3 downto 0);
           r2 : out STD_LOGIC_VECTOR (3 downto 0);
           result : out STD_LOGIC_VECTOR (7 downto 0));
end dot_product;

architecture Behavioral of dot_product is

begin
r0 <= std_logic_vector(signed(vector_x(0)* vector_y(0)));
r1 <= std_logic_vector(signed(vector_x(1)* vector_y(1)));
r2 <= std_logic_vector(signed(vector_x(2)* vector_y(2)));
r3 <= std_logic_vector(signed(vector_x(3)* vector_y(3)));
result<=r0+r1+r2+r3;

end Behavioral;

我们如何在 VHDL 中找到它的点积,稍后我可以根据需要更改矢量大小。请帮忙。谢谢:)

这些是一些注释:

  • vector_x : in STD_LOGIC_VECTOR (3 downto 0);

这不是数学中的向量。它是一个位 0 和 1 的数组。所以你可以用它来表示从 0 (0000) 到 15 (1111) 的数字。

如果你想要一个向量,考虑声明一个 std_logic_vector.

的数组

type t_vector is array (integer range <>) of std_logic_vector(3 downto 0);

考虑阅读此内容:https://www.nandland.com/vhdl/examples/example-array-type-vhdl.html

然后将向量声明为类型为 t_vector 的数组。

之后就可以在一个进程中使用for循环来做乘法和加法了。 考虑阅读以下内容:http://habeebq.github.io/writing-a-2x2-matrix-multiplier-in-vhdl.html

要获得通用数组大小​​,请考虑使用不受约束的数组或通用数组。

entity dot_product is
    generic (width : positive := 8);
    port (vector_x : in t_vector (width downto 0);
          vector_y : in t_vector (width downto 0);
            result : out STD_LOGIC_VECTOR (7 downto 0));
end dot_product ;