string declaration

Discussion in 'VHDL' started by Yann Sarrazin, Sep 9, 2003.

  1. 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
     
    Yann Sarrazin, Sep 9, 2003
    #1
    1. Advertising

  2. "Yann Sarrazin" <> wrote in message
    news:...

    > 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:
    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.
     
    Jonathan Bromley, Sep 9, 2003
    #2
    1. Advertising

  3. Yann Sarrazin

    FE Guest

    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" <> wrote in message
    news:...
    > 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
    >
    >
     
    FE, Sep 9, 2003
    #3
  4. 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" <> wrote in message
    >news:...
    >
    >
    >
    >>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:
    >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.
    >
    >
    >
    >
    >
     
    Yann Sarrazin, Sep 10, 2003
    #4
  5. Yann Sarrazin

    Thomas B.

    Joined:
    May 7, 2010
    Messages:
    1
    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
     
    Thomas B., May 7, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Noah
    Replies:
    5
    Views:
    961
  2. Alex Vinokur
    Replies:
    4
    Views:
    595
    Jonathan Turkanis
    Apr 5, 2004
  3. Ovidesvideo
    Replies:
    4
    Views:
    518
    Andrey Tarasevich
    Dec 10, 2004
  4. kelvSYC
    Replies:
    6
    Views:
    7,235
    Richard Herring
    May 17, 2005
  5. Replies:
    4
    Views:
    1,082
    Richard Tobin
    Dec 12, 2006
Loading...

Share This Page