2012-09-21 7 views
2

지난 주 동안 나는 내 VHDL 안에 캐스팅 오류를 해결하기 위해 애 쓰고있었습니다. 내 코드는 아래에 첨부되어 있습니다.VHDL 캐스팅 오류

if 문을 올바르게 평가하지만 새 값을 min 또는 max 변수에 할당하지 않습니다. 나는 주석을 달고 기능의 다른 측면을 테스트하면서 이것을 알고있다.

FYI. t_battery_data 타입은 아래 기능에서 비교할 전압 인 의 배열을 포함합니다.

왜 이런 식으로 수행하는지 잘 모르겠습니다. 온라인 검색에서 찾을 수있는 것은 모두 내가 수행 한 ieee.numeric_std 라이브러리입니다.

아직도 엉망진창입니다. 어떤 제안이라도 대단히 감사하겠습니다. 감사!

function cell_delta_voltage_counts(
    bat_data: t_battery_data 
) return integer is 

    constant POS_INFINITY: integer:= 2 ** 16 - 1; 
    constant NEG_INFINITY: integer:= 0; 

    variable min: integer range 0 to 2 ** 16 - 1:= POS_INFINITY-5; 
    variable max: integer range 0 to 2 ** 16 - 1:= NEG_INFINITY; 

begin 

    for i in 0 to NUM_CELLS-1 loop 

     if (to_integer(unsigned(bat_data.cell_readings(i).voltage)) < min) then 
      min := to_integer(unsigned(bat_data.cell_readings(i).voltage)); 
     end if; 

     if (to_integer(unsigned(bat_data.cell_readings(i).voltage)) > max) then 
      max := to_integer(unsigned(bat_data.cell_readings(i).voltage)); 
     end if; 

    end loop; 

    return max - min; 

end function cell_delta_voltage_counts; 

답변

1

나는 기능을 많이 사용하지 않지만, IIRC 당신이 호출 사이의 상태 '기억'에 대한 최소 및 최대를 기대한다면, 당신은 함수 외부를 선언하고 불순한로 함수를 선언해야합니다

variable min: integer range 0 to 2 ** 16 - 1:= POS_INFINITY-5; 
variable max: integer range 0 to 2 ** 16 - 1:= NEG_INFINITY; 

impure function cell_delta_voltage_counts(
... 
1

여기서 아무 것도 볼 수 없습니다. 나는 당신의 코드를 시험해 보았고 그것은 Modelsim DE 10.1c에서 나를 위해 작동한다. 어떤 시뮬레이터를 사용하고 있습니까? 당신이 그것을 게시 그대로 함수를 사용

library ieee; 
use ieee.std_logic_1164.all; 
use ieee.numeric_std.all; 

entity test is 
end entity; 

architecture sim of test is 

    constant NUM_CELLS : integer := 2; 

    type t_reading is record 
     voltage : std_logic_vector(15 downto 0); 
    end record t_reading; 

    type t_readings is array(natural range <>) of t_reading; 

    type t_battery_data is record 
     cell_readings : t_readings(0 to NUM_CELLS-1); 
    end record t_battery_data; 

    function cell_delta_voltage_counts(
    (...) 
    end function cell_delta_voltage_counts; 
begin 

    process 
     variable v_battery_data : t_battery_data; 
     variable v_result : integer := 0; 
    begin 
     v_battery_data.cell_readings(0).voltage := x"000a"; 
     v_battery_data.cell_readings(1).voltage := x"001a"; 

     v_result := cell_delta_voltage_counts(v_battery_data); 
     report "result: " & integer'image(v_result); 

     wait; 
    end process; 

end architecture; 

:

여기에 함수를하려고 할 때 내가 사용하는 코드입니다. 시뮬레이션 결과는 예상대로 "결과 : 16"입니다.