3
votes

Imagine you wanted to convert integers to strings in VHDL to display on a VGA monitor. You can't use ieee 2008 standard though because you have to use xilinx ISE 14.7. I have the following code for converting an integer type to a string type but I get "Non-static loop limit exceeded" errors for the while loop and in the for loop:

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

I 'solved' the errors by morphing it to be this monstrosity:

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

Is there a non-retarded way to convert integers to strings?

2
It sounds as if you are trying to use the 'image() function and the string type (in its unconstraint form) for synthesis. This won't work. A little soft-processor seems most appropriate for a task like this (e.g. xilinx picoblaze). It can be done in FPGA logic too, but it won't be the best use of resources ever.andrsmllr
I think you'll need a lookup table. But why do you need a string for VGA? Monitors display pixels only. Characters are mostlikely displayed by graphic buffer manipulations.Paebbels
I can easily display strings on the screen so I went to the next logical step of converting ints to strings so that I could display them too. Both, the function above, and integer'image() don't work for variable ints. What is the common, simple solution to convert ints that are variables to strings?John

2 Answers

7
votes

If I is an integer, integer'image(I) is its representation as a string.

0
votes

I went at it from every direction and finally found that I had to make a giant case block to get it to work. Now I can finally display rapidly changing variables that are really helpful for debugging. It's unfortunate that the solution had to be so cumbersome.

(I already have a ROM for displaying text that the resulting string is sent to.)

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;