Variable Input file length

Discussion in 'VHDL' started by Ted, Dec 6, 2006.

  1. Ted

    Ted Guest

    I have a test bench in which the input stimulus file length is unknown.
    I would like to read the file in to my test bench and loop through
    each element of the stimulus every clock. The only problem is the the
    upper limit of the loop is unknown. I created the following to find
    the file length (number of elements) and use that length as the upper
    limit of a loop used to read the file into an array which is based off
    of that length. Here is the code I created (shortened for posting) but
    it doesn't work in Model Sim:

    ..
    ..
    ..
    signal ind : integer range 1 to 16777216;

    --
    ************************************************************************************
    -- Determine length of input file
    --
    ************************************************************************************
    filelength : process

    variable index : integer := 0;
    variable linein : line;
    variable test : integer;
    file f2 : text open READ_MODE is "adc.txt";

    begin

    while not (endfile(f2)) loop
    readline(f2,linein);
    read(linein,test);
    index := index + 1;
    end loop;

    ind <= index;
    wait;

    end process;
    --
    ************************************************************************************
    -- stimuli process
    --
    ************************************************************************************
    stim_proc : process

    file f2 : text open READ_MODE is "adc.txt";

    type arradc is array(0 to ind) of integer;

    variable lineout2 : line;
    variable adc : arradc;
    variable pixel : integer := 0;
    variable lower : integer := 0;
    variable upper : integer := 3430;
    variable linein2 : line;

    begin

    start_read <= '0';

    --
    **********************************************************************************
    -- read data file
    --
    **********************************************************************************
    i := adc'high;
    for i in 0 to adc'high loop
    readline(f2,linein2);
    read(linein2,adc(i));
    end loop;
    ..
    ..
    ..
    ..
    ..

    I thought it would be pretty straight forward to use the signal "ind"
    to set the size of the array type, arradc, and create a variable, adc,
    to read the file elements into of type arradc. I keep getting the
    following message ...

    # ** Fatal: (vsim-3734) Index value 2 is out of range 0 to 1 (null
    array).
    Makes sense because that is the low limit of the range of the signal
    ind.

    Basically, what I want to do is determine how long the file is and use
    that number to create a type that I can use to create a variable to
    read the stimulus into...

    Any ideas on how I can do this? Thanks.

    Ted
    Ted, Dec 6, 2006
    #1
    1. Advertising

  2. Ted wrote:

    > I have a test bench in which the input stimulus file length is
    > unknown.
    > I would like to read the file in to my test bench and loop through
    > each element of the stimulus every clock. The only problem is the
    > the
    > upper limit of the loop is unknown. I created the following to find
    > the file length (number of elements) and use that length as the
    > upper limit of a loop used to read the file into an array which is
    > based off of that length.


    You cannot do that. The upper and lower limits in an array (type)
    declaration must be static. The value of a signal is not static.

    If you absolutely must store the contents of the file and have that
    accessible in your testbench, you must create a linked list. It
    consist of dynamically allocated objects, by using access types and
    the NEW operator. See for example:
    http://www.emba.uvm.edu/~jswift/uvm_class/notes/access.html

    Another way would just to create an array that is large enough to hold
    the largest expected file. But that is rightout ugly.

    But why reading in the file first and then use the contents? If you
    just need a new vector each clock, why not read a line each clock and
    use that vector?

    Yet another approach: don't use a file at all. Declare a constant
    array with your vectors.

    --
    Paul.
    www.aimcom.nl
    email address: switch x and s
    Paul Uiterlinden, Dec 6, 2006
    #2
    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. Mitchua
    Replies:
    5
    Views:
    2,712
    Eric J. Roode
    Jul 17, 2003
  2. =?Utf-8?B?SG96aQ==?=
    Replies:
    1
    Views:
    6,932
    Ken Cox [Microsoft MVP]
    Jun 2, 2004
  3. Sam
    Replies:
    3
    Views:
    14,074
    Karl Seguin
    Feb 17, 2005
  4. =?Utf-8?B?Sm9obl9I?=
    Replies:
    1
    Views:
    2,758
    =?Utf-8?B?Sm9obl9I?=
    Jan 19, 2006
  5. vaysagekv
    Replies:
    1
    Views:
    351
    Stefan Ram
    Sep 9, 2012
Loading...

Share This Page