VHDL整数字符串

问题描述:

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

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

是否有一个非延迟的方式来将整数转换为字符串?

+1

听起来好像你正在尝试使用用于合成的''image()'函数和'string'类型(以其无限制形式)。这不起作用。一个小小的软处理器似乎最适合这样的任务(例如xilinx picoblaze)。它也可以在FPGA逻辑中完成,但它不会是有史以来最好的资源利用方式。 – damage

+0

我认为你需要一个查找表。但为什么你需要一个VGA字符串?监视器仅显示像素。字符很可能通过图形缓冲区操作显示。 – Paebbels

+0

我可以很容易地在屏幕上显示的字符串,所以我去了整数转换为字符串,这样我也能显示他们的下一个合乎逻辑的步骤。上述函数和integer'image()都不适用于可变整数。将变量ints转换为字符串的常见简单方法是什么? – John

我去它从各个方向,最后发现,我不得不做出一个巨大的情况下块得到它的工作。现在,我可以最终显示快速变化的变量,这对调试非常有帮助。这是不幸的是,解决方案必须如此迟钝,但..

(我已经有用于显示文本结果字符串被发送到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; 

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

+0

没错。虽然使用integer'image(i)时,我得到“表达式不是一个常量”错误。我只是试图将int(即快速变化)转换为显示在显示器上的字符串。 – John

+0

在你的“怪物”两个返回表达式不要么返回定长字符串表达式。 – user1155120

+0

这并不是说该字符串不是一个固定长度,它似乎是抱怨传递的整数中不是恒定的。只要我把它从整数'图像(1234)改为整数'图像(some_var),它抱怨。 – John