2012-09-21 42 views
2

我很難過去幾天我一直在試圖解決我的VHDL中的鑄造錯誤。我的代碼附在下面。VHDL Casting Error

它將正確評估if語句,但不會將新值分配給最小或最大變量。我知道這一點,因爲我評論和測試了函數的不同方面。

FYI。類型t_battery_data包含一個std_logic_vectors(15 downto 0)的數組,這是我在下面的函數中比較的電壓。

我不知道爲什麼這樣做。關於我在網上搜索時可以找到的所有內容包括我已經完成的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」。