Reading non-text files

T

Tricky

How easy is it to read files that are not text into VHDL test benches?
does it require writing of new libraries to handle different file
types?

Or is it simpler to just have the IO put into a text file if possible?
 
J

Jonathan Bromley

How easy is it to read files that are not text into VHDL test benches?

Not wildly difficult, but somewhat messy.
does it require writing of new libraries to handle different file
types?

In practice, no; you can treat just about any file as a
FILE OF CHARACTER and read the byte stream. The VHDL
standard does not mandate this behaviour, but it works
in all simulators I've ever met. Characters can be
easily converted to/from their 0-255 integer equivalent
using CHARACTER'POS and CHARACTER'VAL attribute-functions.
Or is it simpler to just have the IO put into a text file if possible?

Yes, much simpler. Use an external utility (C, Perl, Tcl, you choose)
to map your binary file formats to/from some VHDL-friendly text form.

~~~~~~~~~~~~~~~~~~~~~~

To get you started, here are my does-it-work-in-my-simulator
test programs for binary files:

------1. Write out a 256-byte binary file containing the byte
------ values 0 to 255 in ascending order.

entity binfile is end;

architecture b of binfile is
begin
process
type charfile is file of character;
file f: charfile;
begin
file_open(f, "junk.bin", write_mode);
for i in character loop
write(f, i);
end loop;
file_close(f);
wait;
end process;
end;

-----2. Read a binary file and display its contents byte-wise
----- on the console

use std.textio.all;

entity readfile is end;

architecture f of readfile is
type UnixFile is file of character;
file Src: UnixFile;
begin
ReadBytes : process
variable c: character; -- char read from the file
variable L: line; -- used for displaying output
begin
file_open(Src, "testfile", READ_MODE);
while not endfile(Src) loop
-- Get the next byte
read(Src, c);
-- See what we got: first as a character...
write(L, character'IMAGE(c), field=>5, justified=>LEFT);
-- and then as its ASCII code in decimal:
write(L, character'POS(c), field=>3, justified=>RIGHT);
writeline(OUTPUT, L);
end loop;
wait;
end process;
end;
--
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
(e-mail address removed)
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
 
K

KJ

Tricky said:
How easy is it to read files that are not text into VHDL test benches?
does it require writing of new libraries to handle different file
types?
See Jonathon's reply
Or is it simpler to just have the IO put into a text file if possible?
'Simpler' only the first time....once you notice yourself continually
creating 'text' versions of binary files for each new project that you work
on you'll wonder why you didn't just do the binary file I/O yourself right
from the git-go.

The biggest drawback to not using the native binary file in the first place
is that your simulation testbench now depends on other utilities to convert
to/from text files in order to verify proper behaviour.

KJ
 
D

Duane Clark

Tricky said:
How easy is it to read files that are not text into VHDL test benches?
does it require writing of new libraries to handle different file
types?

Or is it simpler to just have the IO put into a text file if possible?

The standard read/write commands in VHDL use integers, and I guess use
the endian order of the machine it is running on. I usually read non
text files into an integer variable, and do an immediate
conversion/unpacking, including byte swapping if needed. For integers
and bits, that is very simple; I don't see an advantage to having a
library. If I have a file with floating point data, I usually convert it
to integers with Matlab.
 
J

Jonathan Bromley

The standard read/write commands in VHDL use integers

The only way to get truly "standard" (simulator-independent,
system-independent) file I/O in VHDL is to use STD.TEXTIO,
which allows you to read and write line-structured plain-text
files in a truly tool- and system-independent manner.

Any other files are tool- and system-dependent. As Duane
points out, even integers (4 bytes???) suffer byte-ordering
issues. The use of FILE OF CHARACTER, as I suggested, is
tool-independent in practice; but even that comes with no
guarantees. FILE OF anything-else is a hostage to fortune;
speaking only for myself, I would avoid it like the plague.
--
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
(e-mail address removed)
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
 
M

Mike Treseler

Tricky said:
How easy is it to read files that are not text into VHDL test benches?

Here is an example:
http://home.comcast.net/~mike_treseler/char_file.vhd
Or is it simpler to just have the IO put into a text file if possible?

To test a synthesis entity I have to
convert the test data to vector types.
My testbench could do this, but
I prefer to write a script or editor
macro to convert the file to a vhdl constant array
that the testbench can use directly.

-- Mike Treseler
 
H

Huibert J. Lincklaen Arriens

Tricky said:
How easy is it to read files that are not text into VHDL test benches?
does it require writing of new libraries to handle different file
types?

Or is it simpler to just have the IO put into a text file if possible?

You can find a VHDL package for reading and writing binary files (bytes
and words of different sizes and endian order) that we use in a
laboratory exercise in one of our courses on
http://kobalt.et.tudelft.nl/~huib/

Huibert J. Lincklaen Arriens
Delft University of Technology
the Netherlands
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top