我正在尝试用VHDL写两个包函数,平均有两个和四个8位std_logic_vector输入信号。当我实现以下avgtwo代码时,我得到的答案比模拟中的预期大2倍。这就像返回操作'sum(sum'high downto 1)'没有取总和的高8位...我错过了什么?
library IEEE;
use IEEE.STD_LOGIC_1164.all;
use IEEE.STD_LOGIC_UNSIGNED.all;
use IEEE.NUMERIC_STD.ALL;
package my_package is
function avgtwo(v1, v2 : std_logic_vector) return std_logic_vector;
function avgfour(v1, v2, v3, v4 : std_logic_vector) return std_logic_vector;
end my_package;
package body my_package is
function avgtwo
(
v1, v2 : std_logic_vector
)
return std_logic_vector is
variable sum : std_logic_vector(v1'high+1 downto 0) := (others => '0');
begin
sum := std_logic_vector(unsigned(v1)) + std_logic_vector(unsigned(v2));
return sum(sum'high downto 1);
end function avgtwo;
function avgfour
(
v1, v2, v3, v4 : std_logic_vector
)
return std_logic_vector is
variable sum : std_logic_vector(v1'high+2 downto 0) := (others => '0');
begin
sum := std_logic_vector(unsigned(v1)) + std_logic_vector(unsigned(v2)) + std_logic_vector(unsigned(v3)) + std_logic_vector(unsigned(v4));
return sum(sum'high downto 2);
end function avgfour;
end my_package;
答案 0 :(得分:0)
我弄清楚我做错了什么。您必须确保添加两个与接收变量大小相同的std_logic_vector。所以我编辑了总和行如下:
在'avgtwo'功能中:
sum := std_logic_vector(unsigned('0' & v1)) + std_logic_vector(unsigned('0' & v2));
类似于avgfour函数 - 但使用“00”而不是“0”。