0
평균 2 ~ 4 개의 8 비트 std_logic_vector 입력 신호를 갖는 VHDL에서 두 개의 패키지 함수를 작성하려고합니다. 다음 avgtwo 코드를 구현할 때 시뮬레이션 결과에서 예상보다 2 배 큰 응답을 얻습니다. 그것은 반환 작업 '합계 (sum'high downto 1)'합계의 상위 8 비트를 복용하지 않습니다 ... 무엇을 놓친 거지?함수의 std_logic_vector 변수 평균을 계산하십시오.
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;