Lib4U

‎"Behind every stack of books there is a flood of knowledge."

FILE HANDLING

 

vcount_tb

The system tasks and functions for file-based operations are divided into three categories:

 Functions and tasks that open and close files
 Tasks that output values into files
 Tasks that output values into variables
 Tasks and functions that read values from files and load into variables or memories

Fopen And Fclose 

$fopen and $fclose

The function $fopen opens the file specified as the filename argument and returns either a 32 bit multi channel descriptor, or a 32 bit file descriptor, determined by the absence or presence of the type argument. Filename is a character string, or a reg containing a character string that names the file to be opened. 
The multi channel descriptor mcd is a 32 bit reg in which a single bit is set indicating which file is opened. The least significant bit (bit 0) of a mcd always refers to the standard output. Output is directed to two or more files opened with multi channel descriptors by bitwise oring together their mcds and writing to the resultant value. The most significant bit (bit 32) of a multi channel descriptor is reserved, and shall always be cleared, limiting an implementation to at most 31 files opened for output via multi channel descriptors. The file descriptor fd is a 32 bit value. The most significant bit (bit 32) of a fd is reserved, and shall always be set; this allows implementations of the file input and output functions to determine how the file was opened. The remaining bits hold a small number indicating what file is opened.

EXAMPLE
// file open close example
module fopenclose();
integer mcd,number;
initial
begin
mcd = $fopen(“xyz.txt”); // opening the file
repeat(7)
begin
number = $random ;
$fdisplay(mcd, ” Number is “, number);
end
$fclose(mcd); // closing the file
end
endmodule

After simulating the above code, file name called “xyz.txt” will be opened in the same directory. In above example you show that file is getting open and closing, so according to that there will be change in value of mcd.

EXAMPLE
// Display mcd value before and after the opening the file.
module fopenclose();
integer mcd,number;
initial
begin
$display(“value of mcd before opening the file %b “ , mcd);
mcd = $fopen(“xyz.txt”); // opening the file
$display(“value of mcd after opening the file %b “ , mcd);
repeat(7)
begin
number = $random ;
$fdisplay(mcd, ” Number is “, number);
end
$fclose(mcd); // closing the file
end
endmodule
RESULT

value of mcd before opening the file xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
value of mcd after opening the file 00000000000000000000000000000010

Then how can we check that file is closed or not??
In above example its clear that mcd value is changed as file is opened, if we dont close the file than it will be remain in stack. But how will we come to
know that file is closed?? That will come after following examples.

Fdisplay 

$fdisplay, $fdisplayb, $fdisplayo, $fdisplayh

$display has its own counterparts. Those are $fdisplay, $fdisplayb, $fdisplayo, $fdisplayh. Instead of writing on screen they are writing on the specific file with is pointed by the mcd. $fdisplay write in decimal format, $fdisplay in binary, $fdisplay in octal and $fdisplayh in hex format. so no need to put %d-b-o-h.

EXAMPLE
// file open close example with all $fdisplay

module fopenclose();
integer mcd,number;
initial
begin
mcd = $fopen(“temp.txt”); // mcd = multi_channel_descriptor
repeat(7)
begin
number = $random;
$fdisplay(mcd, “Number is “, number);
end
$fclose(mcd);
end
endmodule
RESULT

Number is 303379748
Number is -1064739199
Number is -2071669239
Number is -1309649309
Number is 112818957
Number is 1189058957
Number is -1295874971

EXAMPLE $displayb
module fopenclose();
integer mcd,number;
initial
begin
mcd = $fopen(“temp.txt”); // mcd = multi_channel_descriptor
repeat(7)
begin
number = $random;
$fdisplayb(mcd, “Number is “, number);
end
$fclose(mcd);
end
endmodule
RESULT

Number is 00010010000101010011010100100100
Number is 11000000100010010101111010000001
Number is 10000100100001001101011000001001
Number is 10110001111100000101011001100011
Number is 00000110101110010111101100001101
Number is 01000110110111111001100110001101
Number is 10110010110000101000010001100101

EXAMPLE c. $displayo
module fopenclose();
integer mcd,number;
initial
begin
mcd = $fopen(“temp.txt”); // mcd = multi_channel_descriptor
repeat(7)
begin
number = $random;
$fdisplayo(mcd, “Number is “, number);
end
$fclose(mcd);
end
endmodule
RESULT

Number is 02205232444
Number is 30042257201
Number is 20441153011
Number is 26174053143
Number is 00656275415
Number is 10667714615
Number is 26260502145

EXAMPLE. $displayh
module fopenclose();
integer mcd,number;
initial
begin
mcd = $fopen(“temp.txt”); // mcd = multi_channel_descriptor
repeat(7)
begin
number = $random;
$fdisplayh(mcd, “Number is “, number);
end
$fclose(mcd);
end
endmodule
RESULT

Number is 12153524
Number is c0895e81
Number is 8484d609
Number is b1f05663
Number is 06b97b0d
Number is 46df998d
Number is b2c28465

In below example we will see that how we will come to know that file is closed or not?? so even after closing the file I will try to write in that file, for that it should give error.

EXAMPLE
module fopenclose();
integer mcd,number;
initial
begin
$display(“value of mcd before opening the file %b “ , mcd);
mcd = $fopen(“xyz.txt”);
$display(“value of mcd after opening the file %b “ , mcd);
repeat(7)
begin
number = $random ;
$fdisplay(mcd, ” Number is “, number);
end
$fclose(mcd);
$fdisplay(“value of mcd after closing the file %b “,
mcd);
end
endmodule
RESULT

Error during elaboration.

Fmonitor 

$fmonitor, $fmonitorb, $fmonitoro, $fmonitorh, $fstrobe, $fstrobeb,$fstrobeo, $fstrobeh

Like $display; $monitor and $strobe also have counterparts. They also write in decimal, binary, octal and hexadecimal.

EXAMPLE
// file open close example with $fmonitor
module monitortask();
integer mcd,number;
initial
begin
#0;
mcd = $fopen(“abc.txt”);
$monitoron;
repeat(7)
begin
#1 number = $random ;
end
$monitoroff;
$fclose(mcd);
end

initial
$fmonitorh(mcd, ” Number is “, number);
endmodule
RESULT

Number is 12153524
Number is c0895e81
Number is 8484d609
Number is b1f05663
Number is 06b97b0d
Number is 46df998d

Due to initial-initial race condition we have to put the #0 delay in first initial block and $monitoron-$monitoroff system task, otherwise it is not able to
cache the updated value of integer “number” because “number” is updated in active(1st) event while monitor in system task(3rd) event in the event queue.

Fwrite 

$fwrite, $fwriteb, $fwriteo, $fwriteh

Like $display; $write also have counterparts. They also write in decimal,binary, octal and hexadecimal.

EXAMPLE
// file open close example with $fwrite
module writetask();
integer mcd1,mcd2,number,pointer;
initial
begin
$display(“value of mcd1 before opening the file %b “ , mcd1);
$display(“value of mcd2 before opening the file %b “ , mcd2);
mcd1 = $fopen(“xyz.txt”);
mcd2 = $fopen(“pqr.txt”);
$display(“value of mcd1 after opening the file %b “ , mcd1);
$display(“value of mcd2 after opening the file %b “ , mcd2);
repeat(7)
begin
pointer = $random;
number = $random % 10;
$fwriteo(mcd1, ” Number is “, number);
$fwriteh(mcd2, ” Pointer is “, pointer);
end
$fclose(mcd1);
$fclose(mcd2);
end
endmodule

One of the reasons behind writing this example is to show how the integers are getting different value as per the number of files are opened.

RESULT

value of mcd1 before opening the file xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
value of mcd2 before opening the file xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
value of mcd1 after opening the file 00000000000000000000000000000010
value of mcd2 after opening the file 00000000000000000000000000000100
in file pqr.txt
Pointer is 12153524 Pointer is 8484d609 Pointer is 06b97b0d
Pointer is b2c28465 Pointer is 00f3e301 Pointer is 3b23f176
Pointer is 76d457ed

In file xyz.txt

Number is 37777777767 Number is 37777777767 Number is
00000000007 Number is 37777777774 Number is 00000000011 Number
is 00000000007 Number is 00000000002

Mcd 

Simultaneously writing same data to two different file. This example shows how to set up multi channel descriptors. In this example, two different channels are opened using the $fopen function. The two multi channel descriptors that are returned by the function are then combined in a bit-wise or operation and assigned to the integer variable “broadcast”. The “broadcast” variable can then be used as the first parameter in a file output task to direct output to all two channels at once.

EXAMPLE
module writetask();
integer mcd1,mcd2,broadcast,number;
initial
begin
mcd1 = $fopen(“lsbbit1.txt”);
mcd2 = $fopen(“lsbbit2.txt”);
broadcast = mcd1 |mcd2 ;
repeat(7)
begin
number = $random;
$fdisplayh(broadcast,” Number is “, number);
end
$fclose(mcd1);
$fclose(mcd2);
end
endmodule
RESULT
In lsbbit1.txt

Number is 12153524
Number is c0895e81
Number is 8484d609
Number is b1f05663
Number is 06b97b0d
Number is 46df998d
Number is b2c28465

In lsbbit2.txt

Number is 12153524
Number is c0895e81
Number is 8484d609
Number is b1f05663
Number is 06b97b0d
Number is 46df998d
Number is b2c28465

To create a descriptor that directs output to the standard output that is monitor screen as well as both the files, the “broadcast” variable is a bit-wise
logical or with the constant 1, which effectively writes to both files as well as monitor screen.

EXAMPLE
module writetask();
integer mcd1,mcd2,broadcast,number;
initial
begin
mcd1 = $fopen(“lsbbit1.txt”);
mcd2 = $fopen(“lsbbit2.txt”);
broadcast = 1 | mcd1 | mcd2 ;
repeat(7)
begin
number = $random;
$fdisplayh(broadcast,” Number is “, number);
end
$fclose(mcd1);
$fclose(mcd2);
end
endmodule
endmodule
RESULT

Number is 12153524
Number is c0895e81
Number is 8484d609
Number is b1f05663
Number is 06b97b0d
Number is 46df998d
Number is b2c28465

Formating Data To String 

The $swrite family of tasks are based on the $fwrite family of tasks, and accept the same type of arguments as the tasks upon which they are based, with one exception: The first parameter to $swrite shall be a reg variable to which the resulting string shall be written, instead of a variable specifying the file to which to write the resulting string. 

The system task $sformat is similar to the system task $swrite, with a one major difference. Unlike the display and write family of output system tasks, $sformat always interprets its second argument, and only its second argument as a format string. This format argument can be a static string, such as “data is %d” , or can be a reg variable whose content is interpreted as the format string. No other arguments are interpreted as format strings. $sformat supports all the format specifies supported by $display. 

EXAMPLE: 
$sformat(string, “Formatted %d %x”, a, b);

Source:

http://www.testbench.in/TB_26_FILE_HANDLING.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Information

This entry was posted on January 10, 2013 by in Electronic & Computer Engineering, IC Design & Fabrication, Science & Technology and tagged .
Virtual Fashion Technology

Virtual Fashion Education

toitocuaanhem

"chúng tôi chỉ là tôi tớ của anh em, vì Đức Kitô" (2Cr 4,5b)

VentureBeat

News About Tech, Money and Innovation

digitalerr0r

Modern art using the GPU

Theme Showcase

Find the perfect theme for your blog.

lsuvietnam

Learn to Learn

Gocomay's Blog

Con tằm đến thác vẫn còn vương tơ

Toán cho Vật lý

Khoa Vật lý, Đại học Sư phạm Tp.HCM - ĐT :(08)-38352020 - 109

Maths 4 Physics & more...

Blog Toán Cao Cấp (M4Ps)

Bucket List Publications

Indulge- Travel, Adventure, & New Experiences

Lib4U

‎"Behind every stack of books there is a flood of knowledge."

The WordPress.com Blog

The latest news on WordPress.com and the WordPress community.

%d bloggers like this: