system tasks for file output & strobing 1. introduction there are tasks and functions that are...
TRANSCRIPT
Introduction
There are tasks and functions that are used to generate input and output during simulation. Their names begin with a dollar sign ($). The synthesis tools parse and ignore system functions, and hence can be included even in synthesizable models.
We will discuss system tasks for file output & strobing in this PPT.
2
File Output
Output from Verilog normally goes to the standard output and the file verilog.log.
It is possible to redirect the output of Verilog to a chosen file.
3
Opening a File - 1
A file can be opened with the system task $fopen.
Usage: $fopen("<name_of_file>” ) ;Usage: <file_handle> =
$fopen("<name_of_file>");
4
Opening a File - 2
• The task $fopen returns a 32-bit value called a multichannel descriptor. Only one bit is set in a multichannel descriptor.
• The standard output has a multichannel descriptor with the least significant bit (bit 0) set. Standard output is also called
channel 0. The standard output is always open.• Each successive call to $fopen opens a new channel
and returns a 32-bit descriptor with bit 1 set, bit 2 set, and so on, up to bit 31 set.
• The channel number corresponds to the individual bit set in the multichannel descriptor. Next slide illustrates the use of file descriptors.
5
Opening a File - 3
//Multichannel descriptor
integer handle1, handle2, handle3; //integers are 32-bit values
//standard output is open; descriptor = 32'h0000_0001 (bit 0 set)
initial
begin
handle1 = $fopen("file1.out"); //handle1=32'h0000_0002 (bit 1 set)
handle2 = $fopen("file2.out"); //handle2=32'h0000_0004 (bit 2 set)
handle3 = $fopen("file3.out"); //handle3=32'h0000_0008 (bit 3 set)
end
The advantage of multichannel descriptors is that it is possible to selectively write to multiple files at the same time.
6
Writing to Files - 1
The system tasks $fdisplay, $fmonitor, $fwrite, and $fstrobe are used to write to files. Note that these tasks are similar in syntax to regular system tasks $display, $monitor, etc., but they provide the additional capability of writing to files.
Usage:
$fdisplay(<file_descriptor>, pl, p2,..., pn);$fmonitor(<file_descriptor>, pl, p2,..., pn);
pl, p2, ... , pn can be variables, signal names, or quoted strings.
7
Writing to Files - 2
A file_descriptor is a multichannel descriptor that can be a file handle or a bitwise combination of file handles.
Verilog will write the output to all files that have a 1 associated with them in the file descriptor.
We will use the file descriptors defined in Slide-6 and illustrate the use of the $fdisplay and $fmonitor tasks.
8
Writing to Files - 3
//All handles defined in Slide-6
//Writing to files
integer desc1, desc2, desc3; //three file descriptors
initial
begin
desc1 = handle1 | 1; //bitwise or; desc1 = 32'h0000_0003
$fdisplay(desc1, "Display 1") ; //write to files file1.out & std.out
desc2 = handle2 | handle1; //desc2 = 32'h0000_0006
$fdisplay(desc2, "Display 2") ; //write to files file2.out & file1.out
desc3 = handle3 ; //desc3 = 32'h0000_0008
$fdisplay(desc3, "Display 3") ; //write to file file3.out only
end
9
Closing Files
Files can be closed with the system task $fclose.
Usage: $fclose(<file_handle>);
//Closing Files$fclose(handle1) ;
A file cannot be written to once it is closed. The corresponding bit in the multichannel descriptor is set to 0. The next $fopen call can reuse the bit.
10
Strobing
Strobing is done with the system task keyword $strobe.
This task is very similar to the $display task except for a slight difference. If many other statements are executed in the same time unit as the $display task, the order in which the statements and the $display task are executed is nondeterministic.
If $strobe is used, it is always executed after all other assignment statements in the same time unit have executed.
Thus, $strobe provides a synchronization mechanism to ensure that data is displayed only after all other assignment statements, which change the data in that time step, have executed.
11
Strobing - Example
//Strobing
always @(posedge clock)
begina = b;c = d;
end
always @(posedge clock)$strobe("Displaying a=%b, c=%b", a, c); //display values at
posedge
The values at positive edge of clock will be displayed only after statements a =b and c =d execute. If $display was used, $display might execute before statements a =b and c =d, thus displaying different values.
12