Verilog Coding Tips and Tricks: File Reading and Writing(line by line) in Verilog - Part 2

Sunday, November 5, 2017

File Reading and Writing(line by line) in Verilog - Part 2

File reading and writing is a very useful thing to know in Verilog. The possibility to read test input values from files, and write output values for later verification makes testbench codes easy to write and understand.

There are few ways to read or write files in Verilog. I have already explained one method in my last post, File Reading and Writing in Verilog - Part 1. The method described in this new post will help you to read the contents of a file line by line, instead of reading everything together. This is helpful when the size of file is too big.

In this post, we will learn:
  • How to read hexadecimal/binary/decimal values from a file using fopen and fscanf function.
  • How to write a file with hexadecimal/binary/decimal values using fopen and fdisplay.

Verilog code for File Read and Write:

`timescale 1ns / 1ps

module tb();

    reg [7:0] A; //register declaration for storing each line of file.
    integer outfile0,outfile1,outfile2,outfile3; //file descriptors

initial begin
    
    //The $fopen function opens a file and returns a multi-channel descriptor 
    //in the format of an unsized integer. 
    outfile0=$fopen("A_hex.txt","r");   //"r" means reading and "w" means writing
    outfile1=$fopen("A_write_dec.txt","w");
    outfile2=$fopen("A_write_bin.txt","w");
    outfile3=$fopen("A_write_hex.txt","w");
    
    //read the contents of the file A_hex.txt as hexadecimal values into register "A".
    while (! $feof(outfile0)) begin //read until an "end of file" is reached.
        $fscanf(outfile0,"%h\n",A); //scan each line and get the value as an hexadecimal
    //Write the read value into text files.
        $fdisplay(outfile1,"%d",A); //write as decimal
        $fdisplay(outfile2,"%b",A); //write as binary
        $fdisplay(outfile3,"%h",A); //write as hexadecimal
        #10;
    end 
    //once reading and writing is finished, close all the files.
    $fclose(outfile0);
    $fclose(outfile1);
    $fclose(outfile2);
    $fclose(outfile3);
    //wait and then stop the simulation.
    #100;
    $stop;
end    
      
endmodule

The above code is for reading a hexadecimal file and writing hex, binary and decimal numbers to a file. But with small changes we can make it read binary or decimal numbers too.

For Reading Binary numbers from the file,

Replace the line outfile0=$fopen("A_hex.txt","r");
with    outfile0=$fopen("A_bin.txt","r");     to change the name of the file.
Replace the line $fscanf(outfile0,"%h\n",A);
with    $fscanf(outfile0,"%b\n",A);     to change the type of data read from the file.


Similarly for Reading Decimal numbers from the file, 

Replace the line outfile0=$fopen("A_hex.txt","r");
with    outfile0=$fopen("A_dec.txt","r");     to change the name of the file.
Replace the line $fscanf(outfile0,"%h\n",A);
with    $fscanf(outfile0,"%d\n",A);     to change the type of data read from the file.


 That's all! The output files should look the same in both the cases.

A screenshot of input files and output files is given below:


The input files can be downloaded from here,



The code was tested using Xilinx ISE 14.6 tool.

6 comments:

  1. Replies
    1. Give more information please. It did work for me in Xilinx ISE.

      Delete
  2. Can you please tell me how to read a bmp file pixel by pixel in vhdl xilinx 14.7

    ReplyDelete
  3. You can also refer to the video https://youtu.be/uUZceAfnVNk for a great understanding of #verilog. This tutorial covers registers, unwanted latches & operator synthesis and helps you master these fundamental concepts.Check out the series of free tutorials by Mr. P R Sivakumar(CEO, Maven Silicon) on basic and advanced concepts of Front End VLSI. His amazing explanations and easy to understand content make these videos a great tool for you to update and upgrade your VLSI skills.

    ReplyDelete
  4. Hi, Nice blog indeed.
    $fscanf(outfile0,"%h\n",A); what is the use of /n here.
    Even if I am removing it, I am not seeing any difference.

    ReplyDelete