2016-03-05 64 views
0

想象一下,你想整數轉換爲字符串在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

回答

0

我去它從各個方向,最後發現,我不得不做出一個巨大的情況下塊得到它的工作。現在,我可以最終顯示快速變化的變量,這對調試非常有幫助。這是不幸的是,解決方案必須如此遲鈍,但..

(我已經有用於顯示文本結果字符串被髮送到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; 
2

如果I是整數,integer'image(I)是它作爲一個字符串表示。

+0

沒錯。雖然使用integer'image(i)時,我得到「表達式不是一個常量」錯誤。我只是試圖將int(即快速變化)轉換爲顯示在顯示器上的字符串。 – John

+0

在你的「怪物」兩個返回表達式不要麼返回定長字符串表達式。 – user1155120

+0

這並不是說該字符串不是一個固定長度,它似乎是抱怨傳遞的整數中不是恆定的。只要我把它從整數'圖像(1234)改爲整數'圖像(some_var),它抱怨。 – John