5.FileIo

Note: Samples below assumes Veritak Project Extension "Enable [%u] unit=byte" is enabled.

Here is a example using FileIo in Verilog-2001 and multi-dimmentional array.
This program converts RGB to Black &White with shades ,or without shades(binary).


No conversion ( after_abc1.bmp)




Converted to Black&White with shades (after_abc2.bmp)




Converted to Binary. (after_abc3.bmp)

//Refered C source is found per following URL
//http://www.fit.ac.jp/elec/7_online/lu/sample/bmp_image_proc.cpp

`define Y_SIZE 2048     //maximum size of y dots
`define X_SIZE 2048  //maximum size of x dots

`define HIGH   255      //maxinum Strength
`define LOW      0      //minimum Strengh
`define LEVEL  256      //

module bmp_test;
parameter read_filename="3port.bmp";//input file name
parameter write_filename1="after_abc1.bmp";//output file just after read
parameter write_filename2="after_abc2.bmp";//ouput file, Black and White with strength
parameter write_filename3="after_abc3.bmp";//output file Black and White with binary
parameter [7:0] INTENSITY=100;//0-255

integer  biCompression,       
      biSizeImage,
      biXPelsPerMeter,
      biYPelsPerMeter,
      biClrUsed, 
      biClrImportant;

reg [15:0]  bfType;
integer bfSize;
reg [15:0]   bfReserved1,      bfReserved2;
integer  bfOffBits;
integer  biSize,   biWidth, biHeight;
reg [15:0] biPlanes,    biBitCount;


reg [7:0] image_in [0:`Y_SIZE][0:`X_SIZE][0:2]; //input color matrix
reg [7:0] image_out [0:`Y_SIZE][0:`X_SIZE][0:2];        //output color matrix
reg [7:0] image_bw [0:`Y_SIZE][0:`X_SIZE];              //shades matrix


//********************************************
//   Read 24Bit bitmap file      *
//********************************************


task readBMP(input [128*8:1] read_filename);
        integer fp;
        integer  i, j, k;
        reg [7:0] byte;
        begin   
                // Open File
                fp = $fopen(read_filename, "rb") ;//must be binary read mode
                if (!fp) begin
                        $display("readBmp: Open error!\n");
                        $finish;
                end
                $display("input file : %s\n", read_filename);

                // Read Header Informations
                $fread(bfType,  fp);
                $fread(bfSize,  fp);
                $fread(bfReserved1, fp);
                $fread(bfReserved2, fp);
                $fread(bfOffBits,  fp);

                $fread(biSize,  fp);
                $fread(biWidth, fp);
                if (biWidth%4) begin
                        $display("Sorry, biWidth%4 must be zero in this program. Found =%d",biWidth);
                        $finish;
                end
                $fread(biHeight, fp);
                $fread(biPlanes, fp);
                $fread(biBitCount,  fp);
                if (biBitCount !=24) begin
                        $display("Sorry, biBitCount must be 24 in this program. Found=%d",biBitCount);
                        $finish;
                end
                $fread(biCompression,  fp);
                $fread(biSizeImage,  fp);
                $fread(biXPelsPerMeter, fp);
                $fread(biYPelsPerMeter, fp);
                $fread(biClrUsed,  fp);
                $fread(biClrImportant,  fp);

        // Read RGB Data
                for (i=0; i<  biHeight; i=i+1) begin
                        for (j=0; j<  biWidth; j=j+1) begin
                                for (k=0; k<3; k=k+1) begin
                                        $fread(byte,fp);
                                        image_in[biHeight-i][j][2-k]=byte;
                                end
                        end
                end
                $display("Current POS=%d",$ftell(fp));
                $fclose(fp);
        end
endtask


//******************************************************
//   Output 24bits to bitmap file   *
//******************************************************

task writeBMP(input [128*8:1] write_filename,input O);
        integer fp;
        integer  i, j, k;
        
        begin
        // Open File
                fp = $fopen(write_filename, "wb");//must be binary read mode
                if (!fp) begin
                        $display("writeBmp: Open error!\n");
                        $finish;
                end
                $display("output file : %s\n", write_filename);

        // Write Header Informations 
                $fwrite(fp,"%u",bfType);
                $fwrite(fp,"%u",bfSize);
                $fwrite(fp,"%u",bfReserved1);
                $fwrite(fp,"%u",bfReserved2);
                $fwrite(fp,"%u",bfOffBits);

                $fwrite(fp,"%u",biSize);
                $fwrite(fp,"%u",biWidth);
                $fwrite(fp,"%u",biHeight);
                $fwrite(fp,"%u",biPlanes);
                $fwrite(fp,"%u",biBitCount);
                $fwrite(fp,"%u",biCompression);
                $fwrite(fp,"%u",biSizeImage);
                $fwrite(fp,"%u",biXPelsPerMeter);
                $fwrite(fp,"%u",biYPelsPerMeter);
                $fwrite(fp,"%u",biClrUsed);
                $fwrite(fp,"%u",biClrImportant);

        // Write Bitmap Data
                for (i=0; i< biHeight; i=i+1) begin
                        for (j=0; j< biWidth; j=j+1) begin
                                for (k=0; k<3; k=k+1)  begin
                                        if (O) $fwrite(fp,"%u",image_out[biHeight-i][j][2-k]);
                                      else $fwrite(fp,"%u",image_in[biHeight-i][j][2-k]);
                                end
                        end
                end
                $display("Current WPOS=%d",$ftell(fp));
                $fclose(fp);
        end
endtask



//**********************************************
// Convert RGB to 256 levels of Black & White *
//**********************************************

task BMPto256BW;
        integer y, x, a;
        begin
                for (y=0; y<biHeight; y=y+1) begin
                        for (x=0; x<biWidth; x=x+1) begin
                                a =$rtoi(0.3*image_in[y][x][0] + 0.59*image_in[y][x][1] + 0.11*image_in[y][x][2]);
                                if (a<`LOW) a = `LOW;
                                if (a>`HIGH) a = `HIGH;
                                image_bw[y][x] = a;
                        end
                end
        end
endtask



//****************************************
// Convert Black&While to 24bit bitmap *
//****************************************
task BWto24BMP;
        integer  y, x, a;
        begin
                for (y=0; y<biHeight; y=y+1) begin
                        for (x=0; x<biWidth; x=x+1) begin
                                a = image_bw[y][x];
                                image_out[y][x][0] = a;
                                image_out[y][x][1] = a;
                                image_out[y][x][2] = a;
                        end
                end
        end
        
endtask



//****************************************
// Make binary                     *
//****************************************
task toBinary( input [7:0] intensity);
        
        integer y, x;
        begin

                for (y=0; y<biHeight; y=y+1)begin
                        for (x=0; x<biWidth; x=x+1) begin
                                if(image_bw[y][x] >= intensity) image_bw[y][x]=`HIGH;
                                else image_bw[y][x] = `LOW;
                        end
                end
        end
endtask


initial begin

        
        //Operation1
        readBMP(read_filename);   // Read ,
        writeBMP(write_filename1,0);//and just write without any conversion

        //Operation2
        BMPto256BW; //Convert RGB to Black&White
        BWto24BMP; //
        writeBMP(write_filename2,1);//Output

        //Operation3
        toBinary(INTENSITY);
        BWto24BMP;
        writeBMP(write_filename3,1);//Output

end

endmodule


Another examples is text file operation.
Use $fscanf,$fdisplay,$fseek,$ftell to operate text file.

In this example;

//Sep.21.2005
//$fseek/$fscanf/$fdisplay Test
module large_file;
        parameter integer WORDS=1024*1024*8;//1024*1024*34 BYTES;
        parameter integer STR_LEN=32+2;//CR+LF
        integer i;
        integer fp;
        integer offset;
        reg [255:0] R2;
        
        initial begin
//Open
                fp=$fopen("large_file.txt","w+");//Open By Write & Read Mode
                if (!fp) begin
                        $display("File Can not be opend.!");
                        $finish;
                end
//Write
                $display("Writing Large File..");

                for (i=0; i<WORDS;i=i+1) begin
                        R2={i+7,i+6,i+5,i+4,i+3,i+2,i+1,i};
                        if (i%10000==1) $display("%d",i);
                        $fdisplay(fp,"%32x",R2[127:0]);
                end
                $display("Large Text File genrated Words=%d  Total %fMBytes",WORDS,(WORDS*STR_LEN)/1e6);
//Trival Check
                trival_check(fp);
//Random Check
                random_check(fp);
        end
        
        task disp_current_position(input integer fp);
                integer position;
                begin
                        position=$ftell(fp);
                        $display("Current Posion=%d",position);
                end
        endtask

        task trival_check(input integer fp);
                parameter OFFSET=10;
                begin
                        $display("Trival Check Starts...");
                        offset=$ftell(fp);
                        disp_current_position(fp);      
                
                        offset=offset-OFFSET*STR_LEN;
                        $fseek(fp, offset,0);
                        disp_current_position(fp);

                        for (i=0;i<OFFSET;i=i+1) begin
                                $fscanf(fp,"%h\n",R2);
                                $display("%h",R2);
                        end
                        $display("Trival Check Done.\n");
                end
        endtask

        task random_check(input integer fp);
                integer EOF;
                parameter integer No_of_Checks=1000_000;
                integer i,position,aligned_pos,word_pos;
                reg [127:0] word;
                begin
                        $display("Random Check.Starts.. It takes several minutes.");
                        $fseek(fp,0,2);//Go to EOF
                        EOF=$ftell(fp);//EOF position
                        disp_current_position(fp);      
                        for (i=0;i< No_of_Checks;i=i+1) begin
                                
                                position={$random} % EOF;//or $unsigned($random) % EOF
                                word_pos=(position/STR_LEN);
                                aligned_pos=word_pos*STR_LEN;

                                $fseek(fp,aligned_pos,0);//Goto aligned_pos
                                $fscanf(fp,"%h\n",R2);
                                word={word_pos+3,word_pos+2,word_pos+1,word_pos};
                                if (word !==R2[127:0] ) begin//Check result
                                        $display("Fails. Error detected");
                                        $stop;//assert(0);
                                end
                        end
                        $display("Random Test passed. Random %d seeks performed . Error Detected 0",No_of_Checks);
                end     
        endtask

endmodule