3

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?

3
  • 1
    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. Commented Mar 6, 2016 at 0:13
  • 1
    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. Commented Mar 6, 2016 at 0:28
  • 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? Commented Mar 6, 2016 at 0:57

2 Answers 2

7

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

Sign up to request clarification or add additional context in comments.

4 Comments

That's right. I get "Expression is not a constant" errors though when using integer'image(i). I'm simply trying to convert an int (that is rapidly changing) to a string which is displayed on a monitor.
The two return expressions in your 'monstrosity' don't return constant length string expressions either.
It's not that the string isn't a constant length, it seems to be complaining that the integer being passed in isn't constant. As soon as I changed it from integer'image(1234) to integer'image(some_var) it complained.
The parameter in 'image() does not need to be a constant. But from reading the other comments, I see what you're trying to do, so it's no surprise it has to be a constant for synthesis.
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.