首页 > 解决方案 > 如何在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 中找到它的点积,然后我可以根据我的要求更改矢量大小。请帮忙。谢谢:)

标签: vhdlproductxilinx

解决方案


这些是一些注释:

  • vector_x : 在 STD_LOGIC_VECTOR (3 down to 0);

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

如果您想要一个向量,请考虑声明一个 std_logic_vector 数组。

类型 t_vector 是 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 ;

推荐阅读