VHDL整数到字符串

VHDL integer to string

假设您想将整数转换为 VHDL 中的字符串,以便在 VGA 显示器上显示。你不能使用 ieee 2008 标准,因为你必须使用 xilinx ISE 14.7。我有以下用于将整数类型转换为字符串类型的代码,但在 while 循环和 for 循环中出现 "Non-static loop limit exceeded" 错误:

-- convert integer to string using specified base
-- (adapted from Steve Vogwell's posting in comp.lang.vhdl)

function str(int: integer; base: integer) return string is

variable temp:      string(1 to 10);
variable num:       integer;
variable abs_int:   integer;
variable len:       integer := 1;
variable power:     integer := 1;

begin

-- bug fix for negative numbers
abs_int := abs(int);

num     := abs_int;

while num >= base loop                     -- Determine how many
  len := len + 1;                          -- characters required
  num := num / base;                       -- to represent the
end loop ;                                 -- number.

for i in len downto 1 loop                 -- Convert the number to
  temp(i) := chr(abs_int/power mod base);  -- a string starting
  power := power * base;                   -- with the right hand
end loop ;                                 -- side.

-- return result and add sign if required
if int < 0 then
   return '-'& temp(1 to len);
 else
   return temp(1 to len);
end if;

end str;

我 'solved' 将错误变形为这个怪物:

-- convert integer to string using specified base
-- (adapted from Steve Vogwell's posting in comp.lang.vhdl)

function str(int: integer; base: integer) return string is

    variable temp:      string(1 to 9);
    variable num:       integer;
    variable abs_int:   integer;
    variable len:       integer := 1;
    variable power:     integer := 1;

    begin

    -- bug fix for negative numbers
    abs_int := abs(int);

    num     := abs_int;

    for i in 0 to 100 loop
        if (num >= base) then                   -- Determine how many
          len := len + 1;                       -- characters required
          num := num / base;                    -- to represent the
        else                                    -- number.
            exit;
        end if;
    end loop ;                                 

    for i in 9 downto 1 loop                 -- Convert the number to
        if (i <= len) then
            temp(i) := chr(abs_int/power mod base);  -- a string starting
            power := power * base;                   -- with the right hand
        else
            exit;
        end if;
    end loop ;                                 -- side.

    -- return result and add sign if required
    if int < 0 then
       return '-'& temp(1 to len);
     else
       return temp(1 to len);
    end if;

end str;

整数转字符串有无障碍的方法吗?

如果 I 是一个整数,integer'image(I) 是它作为字符串的表示。

我从各个方面进行了研究,最后发现我必须制作一个巨大的箱子块才能让它工作。现在我终于可以显示快速变化的变量,这对调试非常有帮助。不幸的是,解决方案必须如此麻烦。

(我已经有一个 ROM 用于显示结果字符串发送到的文本。)

function int_to_str(int : integer) return string is
    variable a : natural := 0;
    variable r : string(1 to 11);

begin
    a := abs (int);

    case a is
        when 0    => r := "0          ";
        when 1    => r := "1          ";
        when 2    => r := "2          ";
        when 3    => r := "3          ";
        .
        .
        .
        when 1000 => r := "1000       ";

        when others => r := "???????????";
    end case;

    if (int < 0) then
        r := '-' & r(1 to 10);
    end if;

    return r;
end int_to_str;