Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > VHDL > string declaration

Reply
Thread Tools

string declaration

 
 
Yann Sarrazin
Guest
Posts: n/a
 
      09-09-2003
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


 
Reply With Quote
 
 
 
 
Jonathan Bromley
Guest
Posts: n/a
 
      09-09-2003
"Yann Sarrazin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

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


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: http://www.velocityreviews.com/forums/(E-Mail 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.



 
Reply With Quote
 
 
 
 
FE
Guest
Posts: n/a
 
      09-09-2003
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

"Yann Sarrazin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Yann Sarrazin
Guest
Posts: n/a
 
      09-10-2003
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





Jonathan Bromley wrote:

>"Yann Sarrazin" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>
>
>
>>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.
>>
>>

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



 
Reply With Quote
 
Thomas B. Thomas B. is offline
Junior Member
Join Date: May 2010
Posts: 1
 
      05-07-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a static function declaration conflict with a non-static declaration? nospam_timur@tabi.org C Programming 4 12-12-2006 10:26 PM
maxplusII error: a deferred constant declaration without a full declaration is not supported Noah VHDL 5 04-07-2006 02:34 PM
"virtual outside class declaration" and "declaration does not declare anything" kelvSYC C++ 6 05-17-2005 08:58 AM
Function declaration in class declaration Ovidesvideo C++ 4 12-10-2004 06:36 PM
Intel C++ 8.0 : declaration hides declaration Alex Vinokur C++ 4 04-05-2004 09:49 PM



Advertisments