file handle

F

FP

Guest
I have written a code in VHDL which will read input from 1 file,
process it and write output in a different file. I have used 2 file
handles, 1 for read and write each.

procedure get
uses file handle for read
end procedure get;

architecture
begin
open file
get command
close file
end architecture

My question is, should file handle be declared as variable or signal.
Is it 100% ok to use it as a signal.
I have been using different functions within the procedure which
modify the file handle. I dont think it should be defined as a signal.

If defined as a variable then, I am running into problems of opening
and closing the file. I dont want to open and close the file every
time i call the procedure.

Also, I am calling the get procedure more than once.

Any suggestions?
 
On Fri, 11 Apr 2008 10:55:58 -0700 (PDT), FP wrote:


My question is, should file handle be declared as variable or signal.
Is it 100% ok to use it as a signal.
I have been using different functions within the procedure which
modify the file handle. I dont think it should be defined as a signal.

If defined as a variable then, I am running into problems of opening
and closing the file. I dont want to open and close the file every
time i call the procedure.

Also, I am calling the get procedure more than once.
A FILE object in VHDL is neither a variable nor a signal.

As you've discovered, files generally will be closed when
the file object goes out of scope - for example, if you
declare it in a procedure, then the file will close when
the procedure exits. So you probably want to declare the
file some place where its lifetime will be the life of
the simulation. The declarative region of a process is
usually a smart place to put it; you probably don't want
more than one process messing with any given file,
except possibly INPUT or OUTPUT. However, you can declare
the file in the declarative region of your architecture
if you really want it to be accessible to any process.

Don't forget you can close any file under program
control any time you like, using file_close(F), so you
lose little by giving the file object static lifetime.

Files can be passed as parameters to procedures, so it's
easy to make procedures that read or write a file.
Such procedures can go in a package if you wish. The
STD.TEXTIO package is a good example - think about the
READLINE and WRITELINE procedures.

You can even declare a file in a package if you wish -
that's how INPUT and OUTPUT are declared. That's
potentially useful because it means that procedures
in the package can be written with no file parameter
at all - handy for things like writing to a debug log
that is sure to be global to the simulation.

Here's a simple example that may help to get you started.

use std.textio.all;
package INT_SCRIBBLER is
procedure SCRIBBLE(file F: text; N: integer);
end package;
package body INT_SCRIBBLER is
procedure SCRIBBLE(file F: text; N: integer) is
variable L: line;
begin
write(L, string'("*** my number is "));
write(L, N);
write(L, string'(" ***"));
writeline(F, L);
end;
end;

use std.textio.all;
use work.INT_SCRIBBLER.all;
entity TEST_INT_SCRIBBLER is end;
architecture A of TEST_INT_SCRIBBLER is
begin
process
file INTFILE: text;
variable X: integer;
begin
file_open(INTFILE, "junk.txt", write_mode);
X := 5;
while X < 20 loop
SCRIBBLE(INTFILE, X);
X := X + 3;
end loop;
file_close(INTFILE);
wait;
end process;
end;

Hope this helps
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan.bromley@MYCOMPANY.com
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
 
On Apr 11, 2:45 pm, Jonathan Bromley <jonathan.brom...@MYCOMPANY.com>
wrote:
On Fri, 11 Apr 2008 10:55:58 -0700 (PDT), FP wrote:
My question is, should file handle be declared as variable or signal.
Is it 100% ok to use it  as a signal.
I have been using different functions within the procedure which
modify the file handle. I dont think it should be defined as a signal.

If defined as a variable then, I am running into problems of opening
and closing the file.  I dont want to open and close the file every
time i call the procedure.

Also, I am calling the get procedure more than once.

A FILE object in VHDL is neither a variable nor a signal.

As you've discovered, files generally will be closed when
the file object goes out of scope - for example, if you
declare it in a procedure, then the file will close when
the procedure exits.  So you probably want to declare the
file some place where its lifetime will be the life of
the simulation.  The declarative region of a process is
usually a smart place to put it; you probably don't want
more than one process messing with any given file,
except possibly INPUT or OUTPUT.  However, you can declare
the file in the declarative region of your architecture
if you really want it to be accessible to any process.

Don't forget you can close any file under program
control any time you like, using file_close(F), so you
lose little by giving the file object static lifetime.

Files can be passed as parameters to procedures, so it's
easy to make procedures that read or write a file.
Such procedures can go in a package if you wish.  The
STD.TEXTIO package is a good example - think about the
READLINE and WRITELINE procedures.

You can even declare a file in a package if you wish -
that's how INPUT and OUTPUT are declared.  That's
potentially useful because it means that procedures
in the package can be written with no file parameter
at all - handy for things like writing to a debug log
that is sure to be global to the simulation.

Here's a simple example that may help to get you started.

  use std.textio.all;
  package INT_SCRIBBLER is
    procedure SCRIBBLE(file F: text; N: integer);
  end package;
  package body INT_SCRIBBLER is
    procedure SCRIBBLE(file F: text; N: integer) is
      variable L: line;
    begin
      write(L, string'("*** my number is "));
      write(L, N);
      write(L, string'(" ***"));
      writeline(F, L);
    end;
  end;

  use std.textio.all;
  use work.INT_SCRIBBLER.all;
  entity TEST_INT_SCRIBBLER is end;
  architecture A of TEST_INT_SCRIBBLER is
  begin
    process
      file INTFILE: text;
      variable X: integer;
    begin
      file_open(INTFILE, "junk.txt", write_mode);
      X := 5;
      while X < 20 loop
        SCRIBBLE(INTFILE, X);
        X := X + 3;
      end loop;
      file_close(INTFILE);
      wait;
    end process;
  end;

Hope this helps
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan.brom...@MYCOMPANY.comhttp://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
Thank you Jonathan for such a nice expalination. I have solved my
problem as per your suggestion.
I opened the file in the process and assigned the value to the
procedure through one of the paramteres and returned the updated value
back to the process by making the file handle of type inout. It works
great.
 

Welcome to EDABoard.com

Sponsor

Back
Top