2013-03-14 185 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;