2011-08-10 5 views
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; 

답변

0

내가 잘못한 것을 알았습니다. 수신 변수와 동일한 크기의 두 std_logic_vectors를 추가해야합니다.

'avgtwo'기능에서

: 그래서 다음과 같이 요약 라인을 편집 avgfour 기능에 대한 유사

sum := std_logic_vector(unsigned('0' & v1)) + std_logic_vector(unsigned('0' & v2)); 

를 - 대신 '0'의 "00"을 사용합니다.