string declaration

Y

Yann Sarrazin

Hi all

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

My question is : What is the correct range for toto if t1 and t2 are
not the same length ,is there any tricks to set toto with a correct range.
May be its not possible !

Thanks yann
 
J

Jonathan Bromley

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.
 
F

FE

Hi Yann,
You should use a string pointer (line) defined in std.textio.

library std;
use std.textio.all;

process(A)
constant t1 : string :="once upon a time";
constant t2 : string :="the end";
variable toto : line;
begin
if A=0 then
write(toto, t1);
else
write(toto, t2);
end if;

-- toto is the string pointer
-- toto.all is the string

deallocate(toto); -- important to free the memory
end process;

regards
fe
 
Y

Yann Sarrazin

Cool......

That is what I was looking for
You reply is quiet clear

Thanks to show me how to use WRITE procedure , I didn't know "add things
to the end"

I like the Complicated and pretty solutions.. this give me more
flexibility in my testbench

Well done
You have always good reply on this newsgroup
 
Joined
May 7, 2010
Messages
1
Reaction score
0
DEALLOCATE and 'LENGTH doesn't work

Good morning

I'm from Switzerland but I will try to write in proper english.

I read your post about the LINE and how to find out the length of it.
But the attribute 'LEGNTH doesn't work, however i use the STD-library.
And DEALLOCATE to clear the space doesn't work either!
What's the matter, can anybody help?

Thanks
Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top