2013-03-14 189 views
0

如何將std_logic矢量,bit_vector或任何其他矢量轉換爲字符串?vhdl:將矢量轉換爲字符串

Signal a,b   : UNSIGNED(7 DOWNTO 0); 
SIGNAL x,y,z : BIT_VECTOR(7 DOWNTO 0); 

... 

report "value: " & BIT_VECTOR'Image(x) severity note; 
report "and this one: " & to_string(a) severity note; 

這不起作用,所以如何將矢量轉換爲字符串?

回答

3

正如你已經發現,在「圖像屬性只申報標量類型,不是數組或記錄:通常的做法是創建一個自己的測試實用程序庫包括在一個包to_stringimage功能開始設計,並在整個過程中使用它。

將這些庫進行標準化是完全可能的,你可能會發現很多潛在的「測試工具」包,但沒有一個真正適合成爲標準。

這就是說,你可能會發現下面的包是一個有用的起點。

它封裝了一些自定義數據類型,並對它們進行操作。沒有泛型,但是由於超載,您可以像使用通用函數一樣使用該包。 (你會注意到函數體是不完整的!)擴展它並添加類型很容易,大多數情況下都是粘貼&;它使主設計保持了很多混亂。

將類型declns和(僅限testbench)函數分離爲兩個單獨的包可能會更好; TypesTypes_Test_Utils。然後在整個設計中使用類型,而測試實用程序僅暴露於測試平臺。

library IEEE; 
use IEEE.numeric_std.all; 

package Types is 
    subtype SmallNum is UNSIGNED(7 DOWNTO 0); 
    subtype BiggerNum is UNSIGNED(19 DOWNTO 0); 
    subtype Bits is BIT_VECTOR(7 DOWNTO 0); 

    -- and operations on these types 
    -- Simulate generic procedures using overloading 

    function to_string(N : Unsigned) return String; 
    function to_string(N : Bits) return String; 

    procedure eq_checker (name : string; sig,should : SmallNum; at : time); 
    procedure eq_checker (name : string; sig,should : Bits; at : time); 

end Types; 

package body Types is 

function to_string(N : Unsigned) return String is 
variable temp : string(1 to (N'length + 3)/4) := (others => 'x'); 
begin 
    -- not finished! 
    return temp; 
end to_string; 

function to_string(N : Bits) return String is 
begin 
    return "hello"; 
end to_string; 

procedure eq_checker(name : string; sig,should : SmallNum; at : time) is 
begin 
    if (at = now) then 
    if sig = should then 
     report to_string(sig) & "has same value" severity note; 
    else 
     report to_string(sig) & "has not same value as " & to_string(should) severity note; 
    end if; 
    end if; 
end procedure eq_checker; 

procedure eq_checker(name : string; sig,should : Bits; at : time) is 
begin 
    null; 
end procedure eq_checker; 

end Types; 

和一個簡單的測試吧...

use Work.Types.all; 

    ENTITY tester IS 
    END tester; 

    ARCHITECTURE behavior OF tester IS 

    Signal a,b  : SmallNum := X"AA"; 
    Signal c  : BiggerNum := X"ABCDE"; 
    SIGNAL x,y  : Bits := X"BB"; 

    BEGIN 

    process(a,x) is 
    begin 
    report "value: " & to_string(X) severity note; 
    report "and this one: " & to_string(a) severity note; 
    report "this one too: " & to_string(c) severity note; 
    end process; 

    END; 
0
package package_x is 
    subtype any_type is UNSIGNED(7 DOWNTO 0); 

... 
end package_x; 

package body package_x is 

    procedure someprocedure (signal sig: in any_type) is 

    VARIABLE li : line; 
    file output : text open write_mode is "output"; 

    begin 
    write(li, std_logic_vector(sig)); 
    writeline(output, li); 
    end; 
end package_x; 
+0

+1開始使用亞型...... – 2013-03-14 12:05:57

+1

的任擇議定書要求的轉換串。另一方面是寫/寫。看到布賴恩的帖子轉換爲字符串 – 2015-10-06 14:58:06

+1

好吧,一個'行'只是被定義爲'訪問'類型'字符串'。所以,可以將其轉換爲函數並返回'li.all'。 – PlayDough 2016-03-17 21:48:55

1
function slv_to_string (a: std_logic_vector) return string is 
    variable b : string (a'length-1 downto 1) := (others => NUL); 
begin 
     for i in a'length-1 downto 1 loop 
     b(i) := std_logic'image(a((i-1)))(2); 
     end loop; 
    return b; 
end function; 

:)

3

的VHDL-2008標準定義爲to_stringstd_logic_vectorstd_ulogic_vector,以及各種其他類型。使用VHDL-2008模式可能是最容易的(現在大多數模擬器都支持2008)。

4

這裏是一個解決方案,其中std_logic_vector類型變量的範圍內不會對返回值的影響:

function to_string (a: std_logic_vector) return string is 
variable b : string (1 to a'length) := (others => NUL); 
variable stri : integer := 1; 
begin 
    for i in a'range loop 
     b(stri) := std_logic'image(a((i)))(2); 
    stri := stri+1; 
    end loop; 
return b; 
end function;