假设您想在VHDL中将整数转换为字符串以在VGA监视器上显示。但由于您必须使用Xilinx ISE 14.7,因此无法使用IEEE 2008标准。我有以下代码可将整数类型转换为字符串类型,但是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;
有没有一种不错的方法来将整数转换为字符串?
'image()
函数和string
类型(在其无约束形式下)进行合成。这是行不通的。对于这样的任务,一个小型软处理器似乎是最合适的选择(例如Xilinx Picoblaze)。它也可以在FPGA逻辑中完成,但这不会是最佳的资源利用方式。 - andrsmllr