VHDL:对变量使用WHEN-ELSE语句

VHDL:对变量使用WHEN-ELSE语句,vhdl,modelsim,Vhdl,Modelsim,问题 我在一个包中编写了一个函数,用于转换测试台的一些值。我想检查输出是否超过最大值,如果超过,我想将其设置为该最大值。我感到疲倦的是: -- vec_in: 0...1023, returns -14...23.5 dB function conv_dac602_scale ( vec_in : std_logic_vector) return real is variable val_in, dB : real := 0.0; constant low

问题
我在一个包中编写了一个函数,用于转换测试台的一些值。我想检查输出是否超过最大值,如果超过,我想将其设置为该最大值。我感到疲倦的是:

  -- vec_in: 0...1023, returns -14...23.5 dB
  function conv_dac602_scale (
    vec_in : std_logic_vector)
    return real is
    variable val_in, dB : real := 0.0;
    constant lower : real := -14.0;
    constant upper : real := 23.5;
  begin  -- function conv_dac602_scale
    val_in := real(to_integer(unsigned(vec_in)));
    dB := (lower+(val_in*((upper-lower)/1024.0)));
    return dB when dB <= upper else upper;  -- this is the important line! (129)
  end function conv_dac602_scale;
然后我尝试先将其分配给变量r:

  ...
    r := dB when dB <= upper else upper;  -- this is the important line! (129)
    return r;
  end function conv_dac602_scale;
。。。

r:=dB when dBThe
target我会重新考虑更改您调用return的方式,因为您在
时使用的
语法似乎不正确

明确你想做什么:

return dB when dB <= upper else upper;

在VHDL-2008中,允许在顺序进程中进行并发分配。问题可能是ModelSim 10.0b不支持这个构造(我手头没有10.0b,但10.1编译得很好)。我不知道。我必须查找它,在某些情况下使用它听起来很简单:)@fru1tbat我必须用aldec尝试一下。我应该澄清一下,它是根据您首先分配给变量的地方所做的调整编译的。你仍然不能做“返回…何时…其他”。它怎么什么都不做?它本质上是“如果值大于限制,则返回限制”,这在我看来是完全合理的(除了与“return”一起使用它的问题)。它不会返回
上限,因为他生成return语句的方式。对不起,我将您的语句解释为质疑这个想法,而不是语法。从概念上讲,这是完全正确的。不用担心。我将在答复中澄清这一点。谢谢
return dB when dB <= upper else upper;
if ( dB <= upper) then
     return dB;
else
     return upper;
end if;