Saturday, 5 October 2013

VHDL File I/O - File read write code example

     Here we present the VHDL File I/O syntax and examples.

     This is very useful in handling file i/o for processing signals, images into VHDL codes.

VHDL ElecDude.com


VHDL File operations:

  • File open
  • File read/write
  • File close
Libraries: 
The file i/o procedures and commands are under IEEE -> STD_LOGIC_TEXTIO
library IEEE;
use IEEE.STD_LOGIC_TEXTIO.ALL;

File open syntax:


file <FILEPOINTER> : text is in "<FILENAME>";
File Read Syntax:

procedure READLINE(file F: TEXT; L: out LINE);
procedure READ(L: inout LINE; value: out bit);
-- line serves as a buffer for the data to be written/read  into/from  a file

    variable <inline>: line;

    variable <dataread> : real;

    readline(<infileptr>, <inline>);
    read(<inline>, <dataread>);
Then this real value can be converted into required datatype.

File Write Syntax:

procedure WRITELINE(file F: TEXT; L: inout LINE);
procedure WRITE(L: inout LINE; value: out bit);

    variable <inline>: line;

    variable <datas> : real;
    variable <dataint> : integer;

   write(<inline>, <datas>);   --writes real
  
writeline(<infileptr>, <inline>);

   write(<inline>, <dataint>);  -- writes integer
  
writeline(<infileptr>, <inline>);

For checking current line as end-of-file:


endfile(<fileptr>);
   
For writing string & bitvector:

WRITE (buf, string’("hello"));
WRITELINE(fileptr,buf);
WRITE (buf, bit_vector’("010111"));
WRITELINE(fileptr,buf);

Example for file read & write as a PROCESS:

din:STD_LOGIC_VECTOR(7 downto 0);
d1,intt,linenumber: integer;
dreal:real;

reading : process

    file infile : text is in "file1.txt"; --declare input file 1987
    variable inline: line; --line number declaration
    variable dataread1 : real;
    begin
        wait until clk = '1' and clk'event;
            if (not (endfile(infile)) then
                    readline(infile, inline);
                    read(inline, dataread1);
                    d1 <=integer(dataread1);
            else   
                endoffile <='1'; --set signal to tell end of file read file is reached.
        end if;
    end process reading;

writing : process
    file outfile : text is out "outimgvhdl.txt";
    variable buff_out : line; --line number declaration
    begin
        wait until clk = '1' and clk'event;
        if(endoffile='0') then --if the file end is not reached.
            write(buff_out, intt);             --writes integer
            writeline(outfile, buff_out);


            write(buff_out, din);             --writes STD_LV
            writeline(outfile, buff_out);

            write(buff_out, dreal);             --writes real data (float)
            writeline(outfile, buff_out);
          end if;   
            linenumber <= linenumber + 1;
        else
            null;
        end if;
    end process writing;



SANTHOSH
 
  If you have queries about this article,
Plz let us know by your comments,
It helps us to do much more better



 
Reactions:

3 comments:

Post a Comment

Search Here...