Reading non-text files

Discussion in 'VHDL' started by Tricky, Sep 3, 2007.

  1. Tricky

    Tricky Guest

    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?
     
    Tricky, Sep 3, 2007
    #1
    1. Advertising

  2. On Mon, 03 Sep 2007 08:44:34 -0700, Tricky wrote:

    >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

    http://www.MYCOMPANY.com

    The contents of this message may contain personal views which
    are not the views of Doulos Ltd., unless specifically stated.
     
    Jonathan Bromley, Sep 3, 2007
    #2
    1. Advertising

  3. Tricky

    KJ Guest

    "Tricky" <> wrote in message
    news:...
    > 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
     
    KJ, Sep 3, 2007
    #3
  4. Tricky

    Duane Clark Guest

    Tricky wrote:
    > 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.
     
    Duane Clark, Sep 3, 2007
    #4
  5. On Mon, 03 Sep 2007 20:26:04 GMT, Duane Clark wrote:


    >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

    http://www.MYCOMPANY.com

    The contents of this message may contain personal views which
    are not the views of Doulos Ltd., unless specifically stated.
     
    Jonathan Bromley, Sep 3, 2007
    #5
  6. Tricky wrote:
    > 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
     
    Mike Treseler, Sep 4, 2007
    #6
  7. Tricky wrote:
    > 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
     
    Huibert J. Lincklaen Arriens, Sep 11, 2007
    #7
    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. Darrel
    Replies:
    3
    Views:
    698
    Kevin Spencer
    Nov 11, 2004
  2. crazyprakash
    Replies:
    4
    Views:
    3,431
    adrian
    Oct 30, 2005
  3. Replies:
    4
    Views:
    998
    M.E.Farmer
    Feb 13, 2005
  4. Replies:
    0
    Views:
    806
  5. Replies:
    1
    Views:
    313
    Victor Bazarov
    Sep 27, 2007
Loading...

Share This Page