VHDL整数到字符串

标签 vhdl

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

-- 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;

我通过将其变形为这种怪物来“解决”错误:

-- 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) 就是它作为字符串的表示。

关于VHDL整数到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35817471/

相关文章:

vhdl - VHDL 的 IEEE 库中的可合成定点/浮点

vhdl - 如何创建将单个信号映射到 std_logic_vector 的 1 位的端口映射?

compiler-errors - GHDL编译问题

vhdl - TimeQuest 中关于 VHDL 代码的“无报告路径”

VHDL : nested vs. 多个条件中的 If 语句

compiler-errors - 类型转换不能有聚合操作数(modelsim中的错误)

vhdl - vhdl 中的 index(9) 和 index(9 downto 9) 有什么区别?

使用别名的 VHDL 聚合分配

compilation - VHDL编译错误