过去几天我一直很难过,我一直在努力解决 VHDL 中的投射错误。我的代码附在下面。
它将正确评估if语句,但不会将新值分配给min或max变量。我知道这一点,因为我评论并测试了函数的不同方面。
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;
答案 0 :(得分:1)
我没有使用很多函数,但如果你希望min和max能够“记住”它们在调用之间的状态,那么你需要在函数之外声明它们并将函数声明为不纯函数:
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 :(得分: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”,如预期的那样。