Search code examples
vhdl

VHDL initialize signed of variable length to maximum value


I've got the following function that returns the value variable when it can be represented with the length given as a parameter. If value is out of limits, it should return the maximum possible value or the maximum negative value depending on the sign of value.

function truncate (
  value   : in signed;
  length  : in integer)
  return signed is
constant max_positive_value  : signed(length-1 downto 0) := ((length-1) => '0', others => '1');
constant max_negative_value  : signed(length-1 downto 0) := ((length-1) => '1', others => '0');
variable return_value        : signed(length-1 downto 0) := (others => '0');
begin
  if (value >= max_positive_value) then
    return_value := max_positive_value;
  elsif (value <= max_negative_value) then
    return_value := max_negative_value;
  else
    return_value := resize(value, length);
  end if;
  return return_value;
end;

The problem is the initialization of the max_positive_value and max_negative_value. GHDL complains about not static choice exclude others choice. How can I initialize the limit values if the length is variable? I'm using VHDL 93.


Solution

  • I found another way to do it that Modelsim accepts without a warning but length needs to be smaller than 32:

        constant max_positive_value  : signed(length-1 downto 0) := to_signed(2**(length-1)-1, length);
        constant max_negative_value  : signed(length-1 downto 0) := to_signed(-2**(length-1), length);