TextIO Tutorial

T

Tricky

Ok, I'll definitively thank this group in my thesis! Your idea seems
to work, but there is still a little problem:

the name are of different length! If i do like this:
-----------------------------------------------
Architecture...
Begin
process
    VARIABLE filename : String (23 downto 1);
-----------------------------------------------
it works until it find a filename of lenght different from 23.

I tried to do something like this:
VARIABLE filename : String;
or
VARIABLE filename : String (natural range<>);
but I always get an error message.
Have any suggestion?

Thanks

Dek

I assume you are reading the string from a line? well, a line is
actually a pointer to a string.

So, assuming you just have 1 filename per line, you can do something
like:

readline(namefile, inline);

FILE_OPEN(f, inline.all, READ_MODE);
DEALLOCATE(inline); --drops the pointer

You could do the same if its not just 1 file name per line, but the
parsing would get more complicated. But the method above means you can
have filenames as long as you want.
 
D

Dek

I assume you are reading the string from a line? well, a line is
actually a pointer to a string.

So, assuming you just have 1 filename per line, you can do something
like:

readline(namefile, inline);

FILE_OPEN(f, inline.all, READ_MODE);
DEALLOCATE(inline);         --drops the pointer

You could do the same if its not just 1 file name per line, but the
parsing would get more complicated. But the method above means you can
have filenames as long as you want.- Nascondi testo citato

- Mostra testo citato -

Sorry but I didn't get what you mean; When I use the deallocate
command don't I lose all informations pointed by infile? If I
understood right the deallocate command serves to free memory, so if I
write :

DEALLOCATE(inline);

I will free the memory block used to store the contents of infile; how
can I find the same contents in a string whose lenght is not
constrained?

Another question: is there a command to know the lenght of a line?
Because I could do so:

READFILE (infile, inline);
int := LENGHT (inline); -- int already defined as an integer

than i could continue with an IF clause. Unfortunately LENGHT is not
the right keyword.

Thanks again
 
D

Dek

I assume you are reading the string from a line? well, a line is
actually a pointer to a string.

So, assuming you just have 1 filename per line, you can do something
like:

readline(namefile, inline);

FILE_OPEN(f, inline.all, READ_MODE);
DEALLOCATE(inline);         --drops the pointer

You could do the same if its not just 1 file name per line, but the
parsing would get more complicated. But the method above means you can
have filenames as long as you want.- Nascondi testo citato

- Mostra testo citato -


Sorry but I didn't get what you mean; When I use the deallocate
command don't I lose all informations pointed by inline? If I
understood right the deallocate command serves to free memory, so if
I
write :

DEALLOCATE(inline);


I will free the memory block used to store the contents of infile;
how
can I find the same contents in a string whose lenght is not
constrained?


Another question: is there a command to know the lenght of a line?
Because I could do so:


READFILE (infile, inline);
int := LENGHT (inline); -- int already defined as an integer


than i could continue with an IF clause. Unfortunately LENGHT is not
the right keyword.


Thanks again
 
D

Dek

Sorry but I didn't get what you mean; When I use the deallocate
command don't I lose all informations pointed by inline? If I
understood right the deallocate command serves to free memory, so if
I
write :

DEALLOCATE(inline);

I will free the memory block used to store the contents of infile;
how
can I find the same contents in a string whose lenght is not
constrained?

Another question: is there a command to know the lenght of a line?
Because I could do so:

READFILE (infile, inline);
int := LENGHT (inline);  -- int already defined as an integer

than i could continue with an IF clause. Unfortunately LENGHT is not
the right keyword.

Thanks again- Nascondi testo citato

- Mostra testo citato -


Ok found the right command:

int := inline'Length;


Bye!
 
T

Tricky

Sorry but I didn't get what you mean; When I use the deallocate
command don't I lose all informations pointed by inline? If I
understood right the deallocate command serves to free memory, so if
I
write :

DEALLOCATE(inline);

I will free the memory block used to store the contents of infile;
how
can I find the same contents in a string whose lenght is not
constrained?

the READLINE function just copies the next line from the file into
"inline". if you call DEALLOCATE(inline) you are just removing the
pointer to the copy of the line, the file itself is unnaffected and
the next call to readline will correctly read off the next line from
the file.

the previous example still stands. I was only using DEALLOCATE because
I wasnt actually reading anything from the line, I was dereferencing
the pointer with the inline.all call. if you dont deallocate it
without reading all the data off it, the next readline call will just
append the next line onto the end of whatever is left (and in my
example, the whole line would be left)

so the following example is still good, assuming you dont have
anything else on the line other than the filename:

readline(namefile, inline);

FILE_OPEN(f, inline.all, READ_MODE);
DEALLOCATE(inline); --drops the pointer

the good thing about this method is you dont need to declare a string
to read the data off "inline" and restrict it's length, you can just
use the line itself, as the "line" type is just a pointer to a string.
 
D

Dek

the READLINE function just copies the next line from the file into
"inline". if you call DEALLOCATE(inline) you are just removing the
pointer to the copy of the line, the file itself is unnaffected and
the next call to readline will correctly read off the next line from
the file.

the previous example still stands. I was only using DEALLOCATE because
I wasnt actually reading anything from the line, I was dereferencing
the pointer with the inline.all call. if you dont deallocate it
without reading all the data off it, the next readline call will just
append the next line onto the end of whatever is left (and in my
example, the whole line would be left)

so the following example is still good, assuming you dont have
anything else on the line other than the filename:

readline(namefile, inline);

FILE_OPEN(f, inline.all, READ_MODE);
DEALLOCATE(inline);         --drops the pointer

the good thing about this method is you dont need to declare a string
to read the data off "inline" and restrict it's length, you can just
use the line itself, as the "line" type is just a pointer to a string.- Nascondi testo citato

- Mostra testo citato -


Ok, I think I should explane better what I have to do; my purpose is
to write something like this:

----------------------------------------------------------------------------------------------------------------

READLINE ( filein, buf1); --filein: file where names
are stored; buf1 e buf2 defined as a line;
READ (buf1, filename); --filename define as a
string
WRITE ( buf2, string'("My command 1"));
Write (buf2, filename);
WRITE ( buf2, string'("My command 2"));
WRITELINE (fileout, buf2);

----------------------------------------------------------------------------------------------------------------

Now, I tried to write something like this:

------

readline ( namefile, L);
FILE_OPEN(namefile, L.all, READ_MODE);
DEALLOCATE(L); --drops the pointer
write (buf, L);
writeline (outfile, buf);

--------

but it doesn't work; It would be really nice to don't be forced
declaring string lenght, but I didn't understood yet how to use
properly the deallocate command.

Thanks

Bye
 
M

Mike Treseler

Dek said:
but it doesn't work; It would be really nice to don't be forced
declaring string length, but I didn't understood yet how to use
properly the deallocate command.

Consider using the REPORT command for text, and get on
with the testbench.

TextIO has already stolen two weeks of your time.

-- Mike Treseler
 
J

JimLewis

Dek,
Here is something I have used. Make sure that FileName length (20
here)
is big enough to allow you to get the file name. This assumes that
there is no white space at the end of the line. Note though I used
this in a toy program. I only read files for testbenches when I have
a big data set to read in, like a video image or something of that
sort.

use std.textio.all;

TestProc : process
File TestFile : Text ;
variable FileInLine : Line ;
variable FileName : string (1 to 20 ) ;
variable NameLength : natural ;
begin
Write( OUTPUT, "File Name to Read: " & LF);
Read ( INPUT, FileName, NameLength) ;
if (FileName(NameLength) = LF) then
NameLength := NameLength - 1 ;
end if ;
file_open(TestFile, FileName(1 to NameLength), READ_MODE);


Best,
Jim
SynthWorks VHDL Training
 
J

JimLewis

This one handles spaces on the same line as the name, but is much more
tedious

Main : Process

begin
write(WriteBuf, String'("Enter File Name to Read: ")) ;
writeline(OUTPUT, WriteBuf) ;
Readline (INPUT, ReadBuf) ;
i := 1 ;
loop
Read(ReadBuf, InputString(i), Valid) ;
exit when not Valid ;
i := i + 1 ;
end loop ;
if i = 1 then wait; end if ; -- add error handling here
file_open(TestFile, InputString(1 to i-1), READ_MODE) ;


Cheers,
Jim
SynthWorks VHDL Training
 
J

JimLewis

This needs a small modification for your code:
This one handles spaces on the same line as the name, but is much more
tedious

Main : Process

begin
  write(WriteBuf, String'("Enter File Name to Read: ")) ;
  writeline(OUTPUT, WriteBuf) ;
  Readline (INPUT, ReadBuf) ;
  i := 1 ;
  loop
    Read(ReadBuf, InputString(i), Valid) ;

    exit when not Valid or InputString(i) = ' ' or InputString(i) =
HT ;
    i := i + 1 ;
  end loop ;
  if i = 1 then wait; end if ;   -- add error handling here
  file_open(TestFile, InputString(1 to i-1), READ_MODE) ;
Cheers,
Jim
SynthWorks VHDL Training
 
D

Dek

This needs a small modification for your code:




      exit when not Valid or InputString(i) = ' ' or InputString(i) =
HT ;


Cheers,
Jim
SynthWorks VHDL Training


Thanks all, it seems a good idea, but finally I used Tcl, it's quite
simple

Bye

Dek
 
M

Mike Treseler

Dek said:
Thanks all, it seems a good idea, but finally I used Tcl, it's quite
simple

Glad you saw the light.
tcl is much better at text than vhdl,
as is python, bash script, emacs-lisp, perl, ...

-- Mike Treseler
 
D

Dek

Glad you saw the light.
tcl is much better at text than vhdl,
as is python, bash script, emacs-lisp, perl, ...

           -- Mike Treseler


Hemm... actually I used Tcl just as a macro to be used with modelsim
(as HT-Lab suggested); in my VHDL TextIO is still present, but since I
use the files name as generics, they don't have to be constrained
anymore.

Maybe, as you say, I did better if I used just Tcl or Python or so on,
but I want to take confidence with VHDL for further applications, and
I had data from an actual detector already stored in .txt files.
Anyway I think it's nice to know how TextIO works because, even if
it's not very powerful, it gives you the chance to understeand how an
hardware model would work on data already stored; and I don't think
this is a so rare application.
 
M

Mike Treseler

Hemm... actually I used Tcl just as a macro to be used with modelsim
(as HT-Lab suggested); in my VHDL TextIO is still present, but since I
use the files name as generics, they don't have to be constrained
anymore.

Sorry that I misinterpreted your posting.
Congratulations for conquering textio.
Maybe, as you say, I did better if I used just Tcl or Python or so on,
but I want to take confidence with VHDL for further applications, and
I had data from an actual detector already stored in .txt files.

I prefer to convert existing text files to vhdl packages
using a scripting language, since I am using one anyway
to run modelsim. This isn't better, just different.

-- Mike Treseler
 
Joined
Mar 23, 2010
Messages
1
Reaction score
0
Hi all,
I'm new in vhdl and I want to read from a file which contain numbers
separated by POINT not by White space ( as the textio procedure works by default ), it's like this
44.123.25.254
44.123.25.253
44.123.25.250

I wanna to compare such line withe an other variable like this

entity start is

generic( File_string : string := "C:\IP\IP.txt" );

port(
in : in std_logic_vector(32 downto 0)
);

end entity

archtecture arch of start is

signal L :std_logic_vector(32 downto 0);
begin
P : process


FILE inFile : TEXT IS IN File_string;

VARIABLE inLine : LINE;

begin
****
****
****




Can you tell me how can I do to compare the Input value with such line
with READLINE
Are we obligated to peass by READ after READLINE or a simple READLINE it's enought?
thx..
 

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,995
Messages
2,570,235
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top