I want to declare a string as variable in my testbench
----------------------------------------------------
constant t1:="once upon a time";
constant t2:="the end"
variable toto:string(......); --I need to define a range for toto
--and now in my process , I declare:
if A=0 then
toto: = t1;
else
toto:=t2;
end if;
There's a simple, messy answer and a complicated, pretty answer...
Simple and messy
~~~~~~~~~~~~~~~~
Make 'toto' plenty wide enough. Then you can store each string
in the appropriate slice of it.
variable toto: string(1 to 80) := (others => ' ');
-- plenty wide enough, and full of spaces
...
if A=0 then
toto(t1'range) := t1;
else
toto(t2'range) := t2;
end if;
This is quite horrible, though, because you have no easy way to
find out how many characters of 'toto' are in use at any given
time.
Complicated and pretty
~~~~~~~~~~~~~~~~~~~~~~
Make 'toto' a variable of type LINE, from package STD.TEXTIO.
Then you can copy any string into it using the WRITE procedure:
variable toto: LINE; -- initialised to empty!
...
if A=0 then
WRITE(toto, t1);
else
WRITE(toto, t2);
end if;
Don't forget that the WRITE procedure adds things to the end
of the string stored in 'toto', so that it grows ever longer.
To reset it back to "empty", simply:
DEALLOCATE(toto);
Which leads us to an attractive procedure that has the effect
of copying any string into a LINE variable, having first
cleared that variable:
procedure put(L: inout LINE; S: in STRING) is
begin
DEALLOCATE(L);
WRITE(L, S);
end;
Now, of course, you have the problem of how to make use of
the string contents of the line variable. Given a variable
L of type LINE, you can...
* get hold of the whole string contents:
report "Message is " & L.ALL;
* take a slice of it, just like a string:
variable c: CHARACTER;
...
c := L(1);
* find its length:
if L'LENGTH > 50 then ...
* write it to a file (such as the console):
WRITELINE(OUTPUT, L); -- also clears L to empty
Hope this helps
--
Jonathan Bromley, Consultant
DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * Perl * Tcl/Tk * Verification * Project Services
Doulos Ltd. Church Hatch, 22 Market Place, Ringwood, Hampshire, BH24 1AW, UK
Tel: +44 (0)1425 471223 mail: (e-mail address removed)
Fax: +44 (0)1425 471573 Web:
http://www.doulos.com
The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.