如何在 VHDL 中做矢量积
How to do a vector product in VHDL
我将如何在 VHDL 中进行矢量乘积?
例如我有 2 个这样定义的向量:
type array_4_8bit is array (0 to 3) of std_logic_vector(7 downto 0);
signal Array_Last4Vals : array_4_8bit;
signal Multiplier : array_4_8bit;
Array_Last4Vals <= [5, 6, 7, 8]; -- will replace these numbers with the corresponding binary numbers
Multiplier <= [1, 2, 3, 4]; -- will replace these numbers with the corresponding binary numbers
product <= Multiplier*Array_Last4Vals;
然后我希望此示例中的产品为 [5, 12, 21, 32]。
我该怎么做?
VHDL 允许为自定义类型重新定义中缀运算符,例如 *
,因此您可以声明一个执行向量乘法的函数,例如:
function "*"(a, b : array_4_8bit) return array_4_8bit is
variable result_v : array_4_8bit;
begin
for idx in result_v'range loop
result_v(idx) := std_logic_vector(unsigned(a(idx)) * unsigned(b(idx)));
end loop;
return result_v;
end function;
上面假设向量元素是无符号值,并且每次乘法的结果被截断为 8 位。
对于输入数据的生成,您还可以考虑这样做:
type array_4_integer is array (0 to 3) of integer;
function to_array_4_8bit(v : array_4_integer) return array_4_8bit is
variable result_v : array_4_8bit;
begin
for idx in result_v'range loop
result_v(idx) := std_logic_vector(to_unsigned(v(idx), 8));
end loop;
return result_v;
end function;
Array_Last4Vals <= to_array_4_8bit((5, 6, 7, 8));
Multiplier <= to_array_4_8bit((1, 2, 3, 4));
我将如何在 VHDL 中进行矢量乘积?
例如我有 2 个这样定义的向量:
type array_4_8bit is array (0 to 3) of std_logic_vector(7 downto 0);
signal Array_Last4Vals : array_4_8bit;
signal Multiplier : array_4_8bit;
Array_Last4Vals <= [5, 6, 7, 8]; -- will replace these numbers with the corresponding binary numbers
Multiplier <= [1, 2, 3, 4]; -- will replace these numbers with the corresponding binary numbers
product <= Multiplier*Array_Last4Vals;
然后我希望此示例中的产品为 [5, 12, 21, 32]。
我该怎么做?
VHDL 允许为自定义类型重新定义中缀运算符,例如 *
,因此您可以声明一个执行向量乘法的函数,例如:
function "*"(a, b : array_4_8bit) return array_4_8bit is
variable result_v : array_4_8bit;
begin
for idx in result_v'range loop
result_v(idx) := std_logic_vector(unsigned(a(idx)) * unsigned(b(idx)));
end loop;
return result_v;
end function;
上面假设向量元素是无符号值,并且每次乘法的结果被截断为 8 位。
对于输入数据的生成,您还可以考虑这样做:
type array_4_integer is array (0 to 3) of integer;
function to_array_4_8bit(v : array_4_integer) return array_4_8bit is
variable result_v : array_4_8bit;
begin
for idx in result_v'range loop
result_v(idx) := std_logic_vector(to_unsigned(v(idx), 8));
end loop;
return result_v;
end function;
Array_Last4Vals <= to_array_4_8bit((5, 6, 7, 8));
Multiplier <= to_array_4_8bit((1, 2, 3, 4));