2013-03-14 3 views

Répondre

3

Comme vous l'avez découvert, l'attribut « image est seulement déclarée pour les types scalaires, pas de tableaux ou d'enregistrements: l'approche habituelle est de créer sa propre bibliothèque d'utilitaires de test, y compris to_string ou image fonctions dans un package au début d'un design, et l'utiliser tout au long.

Il serait parfaitement possible de standardiser une bibliothèque de ces derniers, et vous trouverez probablement de nombreux paquets "test utilitaire" potentiels, mais aucun n'a vraiment assez bien réussi pour mériter de devenir un standard. Cela étant dit, vous pourriez trouver le paquet suivant un point de départ utile.

Il encapsule un couple de types de données personnalisés avec des opérations sur eux. Pas de génériques, mais grâce à la surcharge, vous pouvez utiliser le paquet comme si ses fonctions étaient génériques. (Vous remarquerez que les corps de la fonction sont incomplets!) L'extension et l'ajout de types est facile à couper & pâte pour la plupart; et il garde beaucoup de fouillis hors de la conception principale.

Il est peut-être préférable de séparer le type declns et les fonctions (testbench uniquement) en deux paquets distincts; Types et Types_Test_Utils. Puis Types est utilisé tout au long de la conception, tandis que les utilitaires de test sont uniquement exposés à la base de test.

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; 

Et un testeur simple pour elle ...

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 pour commencer à utiliser les sous-types ... –

+1

L'OP a demandé une conversion chaîne. Ceci d'un autre côté est écriture/écriture. Voir la publication de Brian pour une conversion en chaîne –

+1

Eh bien, une 'line' est simplement définie comme un type' access' à 'string'. Donc, on pourrait convertir cela en une fonction et renvoyer 'li.all' aussi bien. – PlayDough

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

La norme VHDL-2008 définit to_string pour std_logic_vector et std_ulogic_vector, ainsi qu'une variété de autres types. Il pourrait être plus facile d'utiliser le mode VHDL-2008 (la plupart des simulateurs supportent actuellement 2008).

4

Voici une solution où la plage de la variable de type std_logic_vector n'a pas d'impact sur la valeur de retour:

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