Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Passing file pointers around ?

742 views
Skip to first unread message

Frazer Worley, #909980 DAD

unread,
Apr 25, 1995, 3:00:00 AM4/25/95
to
Is there a way to pass file pointer to functions/procedures ?

I want to pass a file to a procedure which returns the next vector
in that file - this would get around the problem of not being able
to reopen files.

Textio in VHDL really is such a joke. Is fixing it the #1 priority
of the committee ? And if not why not ? Seems to me that they should
get their existing language in order before screwing it up even further
by adding my misconcieved extensions.

Sorry - but I'm a little fed up with VHDL :-)

Thanks in advance,

Frazer

Andrew Hana

unread,
Apr 26, 1995, 3:00:00 AM4/26/95
to
Frazer Worley, #909980 DAD (fra...@dseg.ti.com) wrote:
: Is there a way to pass file pointer to functions/procedures ?

: Thanks in advance,

: Frazer
:


The following works with Voyager 1.4 and 2.0. The code comes straight out of a test
harness for an ASIC that I worked on. Hence, the otherwise useless paramaters
"model_name" and "file_name". Just concentrate on the essential elements of the code.

1) The following procedure is defined.

procedure get_a_value( variable fp : IN TEXT;
constant model_name : IN string;
constant file_name : IN string;
constant bits_to_return : IN integer;
variable data : OUT std_logic_vector
) is

variable line_from_file : string(1 to MAX_LINE_LENGTH) := (OTHERS => ' ');
variable number_of_chars : natural;

begin
line_from_file(1) := '-';

-----------------------------------------------------
-- Read a line from the file
-- Ignore lines which start with a '#' '-' or a space
-----------------------------------------------------
while (line_from_file(1) = '-') OR (line_from_file(1) = '#') OR (line_from_file(1) = ' ') loop

-----------------------------------------------------------
-- Ensure that there is at least one more line to read from
-----------------------------------------------------------
assert NOT endfile(fp)
report model_name & ": Cannot read any more lines from file `" & file_name & "'"
severity failure;

read(fp, line_from_file, number_of_chars);

end loop;

---------------------------------------------------------------
-- Convert the HEX value to std_logic_vector
-- And return only "bits_to_return"
---------------------------------------------------------------
-- The function "fromhex" is left as an exercise for the reader
---------------------------------------------------------------
data := fromhex( line_from_file(1 to number_of_chars) )(bits_to_return-1 downto 0);

end get_a_value;


2) Next a process is used to extract data from the file.

do_something_with_the_data : process

constant FILE_NAME : string(1 to 9) := "some_file";
file src_data : TEXT is in FILE_NAME";
variable value_from_file : std_logic_vector(7 downto 0);

begin

wait until rising_edge(clock);

get_a_value(src_data, "this_process_name", FILE_NAME, 8, value_from_file)

----------------------------------------------------
-- Do something interesting with "value_from_file --
----------------------------------------------------

end process do_something_with_the_data;

3) The thing to note about this method is that the file is opened by the process.
Since a process never dies, so the file is opened once and once only at time 0
(at elaboration time in fact). Hence the file will be read sequentially and
can never be reopened.

4) If it is necessary to be able to re-open the file so that you can read from
the start again, it is necessary to put the "file" declaration in the procedure.
That is, the line
file src_data : TEXT is in FILE_NAME";
goes into the procedure. Obviously, references to the file_ptr in the paramater
list of the file are to be removed.
Once the procedure terminates, so the internal data structure that maintains the
file pointer is destroyed. Re-invoking the procedure at a later date will re-open
the file for reading at the top of the file.

5) It might be possible for you to declare a procedure/function that opens a file for
reading (by having the file declaration in it) and simply returning the file_ptr
to the calling process. I don't know if the simulator will allow you to use a
file_ptr on a file that it thinks it has closed down. I haven't experimented
and I wish you luck. Let me know how you get on and with which simulator you used.


Andrew Hana

-----------------------------------------------------------------------------------
-- These are my comments only and do not reflect the opinions of Hewlett Packard --
-----------------------------------------------------------------------------------

PS

Charles F. Shelor

unread,
Apr 26, 1995, 3:00:00 AM4/26/95
to fra...@dseg.ti.com
fra...@dseg.ti.com (Frazer Worley, #909980 DAD) wrote:
>Is there a way to pass file pointer to functions/procedures ?
>
No/Yes, everytime you call the READLINE or WRITELINE procedure you are
passing the file pointer. Extending this to your own code is simple.
(The No is for functions since the file is an access type and would be
of mode INOUT if it was to be modifed.)

Look at the definition of std.textio package to see examples of how
to declare and pass file arguments.

>
>Textio in VHDL really is such a joke. Is fixing it the #1 priority
>of the committee ?

Maybe not #1 but it was a high priority. File I/O received the most
number of changes in -1993 update. It is also (the only?) area that
is not backwards compatible to -1987.

> And if not why not ? Seems to me that they should
>get their existing language in order before screwing it up even further
>by adding my misconcieved extensions.

Where does a misconceived extension start and a language correction end?
VHDL-1993 was mostly language cleanup and very few extensions, just what
you asked for!

>
>Sorry - but I'm a little fed up with VHDL :-)

Have you considered taking VHDL training? The language is large and complex,
but every feature has a purpose. The language was designed with specific goals
and is carefully reviewed. The committee really wants CONSTRUCTIVE input.
If you have strong feelings about the language you can join the committee.
It is NOT a closed group, or an invitation only group. Contact Paul Menchini
He'll be glad to sign you up and let you work on the next version.


Charles F. Shelor cfsh...@acm.org
SHELOR ENGINEERING VHDL Training, Consulting, Models
3308 Hollow Creek Rd (817) 467-9367
Arlington, TX 76017-5346

PS I am not a member of the language committee at this time. I believe they
have done a superb job in maintaining the language consistancy while trying to
incorporate features that will improve the usefulness and lifetime of VHDL.
Paul and the other members of DASC do not receive enough thanks for the effort
they expend making VHDL what it is. Thank you DASC!


Andrew Rushton

unread,
May 3, 1995, 3:00:00 AM5/3/95
to
On Wed, 26 Apr 1995, Andrew Hana wrote:

> The following works with Voyager 1.4 and 2.0. The code comes straight out of a test
> harness for an ASIC that I worked on. Hence, the otherwise useless paramaters
> "model_name" and "file_name". Just concentrate on the essential elements of the code.

> procedure get_a_value( variable fp : IN TEXT;


> constant model_name : IN string;
> constant file_name : IN string;
> constant bits_to_return : IN integer;
> variable data : OUT std_logic_vector
> ) is
>
> variable line_from_file : string(1 to MAX_LINE_LENGTH) := (OTHERS => ' ');

<snip>
> read(fp, line_from_file, number_of_chars);

Er, somethings wrong here. This is *not* TextIO! I think you are using some
Voyager-specific READ procedure here. Anyone trying to reproduce this in their
own simulator will get nowhere!

In standard VHDL, you have to READLINE a LINE from a FILE and then READ data
from the LINE. For example:

PROCESS
FILE data : TEXT IS IN "test.dat";
VARIABLE data_line : LINE;
VARIABLE var_a, var_b, var_c : BIT;
BEGIN
WHILE NOT ENDFILE(data) LOOP
READLINE (data, data_line);
READ (data_line, var_a);
READ (data_line, var_b);
READ (data_line, var_c);
...
END LOOP;
WAIT;
END PROCESS;

READ is overloaded for types bit, bit_vector, integer, time, boolean, character
and string. Furthermore, there is a common NON-standard package called
std_logic_textio which extends TextIO to the types std_ulogic (and tjherefore
std_logic), std_logic_vector and std_ulogic_vector. It is fairly easy to extend
TextIO further to other types.

Of course, all this could be done in a subprogram too. Its easy really.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- Andy Rushton #8-) email a...@transeda.demon.co.uk --
-- TransEDA Limited tel. +44-1703-255118 --
-- UK fax. +44-1703-270278 --
-- assert opinion report "mine, not TransEDA's" severity note; --
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --


Andrew Hana

unread,
May 4, 1995, 3:00:00 AM5/4/95
to
Andrew Rushton (a...@transeda.demon.co.uk) wrote:
: On Wed, 26 Apr 1995, Andrew Hana wrote:

: > The following works with Voyager 1.4 and 2.0. The code comes straight out of a test


: > harness for an ASIC that I worked on. Hence, the otherwise useless paramaters
: > "model_name" and "file_name". Just concentrate on the essential elements of the code.

: > procedure get_a_value( variable fp : IN TEXT;


: > constant model_name : IN string;
: > constant file_name : IN string;
: > constant bits_to_return : IN integer;
: > variable data : OUT std_logic_vector
: > ) is
: >
: > variable line_from_file : string(1 to MAX_LINE_LENGTH) := (OTHERS => ' ');

: <snip>
: > read(fp, line_from_file, number_of_chars);

: Er, somethings wrong here. This is *not* TextIO! I think you are using some
: Voyager-specific READ procedure here. Anyone trying to reproduce this in their
: own simulator will get nowhere!

Could you explain this further. From my understanding:

1) "type TEXT is file of STRING;" is defined in the TEXTIO package

2) Section 3.4.1 (page 3-16) of 1076-87 indicates that given a type declaration
of the form "type FT is file of TM" (in the example `1' above, FT => TEXT, and
TM => STRING) then the following functions are implicitely declared
(That is, the simulator automatically creates the following functions for use
by the user)

procedure READ(F:in FT; VALUE:out TM);
procedure WRITE(F:out FT; VALUE:in TM);
function ENDIFLE(F:in FT) return BOOLEAN;
procedure READ(F:in FT; VALUE:out TM; LENGTH:out NATURAL);

3) All I am using is the implicitely decalared functions as defined by the LRM.
There are no VOYAGER-specific READ functions being used.

0 new messages