Search This Blog

Sunday, 13 May 2018

Get Familiar with System Task in Verilog

There are special Tasks and Function in Verilog language which are used to generate input and output during simulation process. These special Tasks and Functions are always starts with $ sign, followed by Task/Function specifier. Synthesis tools ignore these system tasks and functions.

These System Tasks are classified as below
  • Display Task
    • $display, $write, $monitor, $strobe
  • File I/O Task
    • $fopen, $fclose, $fdisplay, $fstrobe, $fmonitor
  • Timescale Task
    • $time, $stime, $realtime
  • Simulation Control Task
    • $reset, $finish, $stop
  • Display Tasks
  • $display : This system task is used to display string, expression or values of variables during simulation process. It is similar to "printf" in C language. There is by default new line character at the end of the string when you use $display system task. Numbers can be displayed in hexadecimal(%h), octal(%o), decimal(%d) or binary(%b) format.
    $display ("Hi, Verilog System Task");
    Output : Hi, Verilog System Task
    
    $display ($time); // current simulation time
    Output : 450
    
    count = 4'b11;
    $display ("The count = %b", count);
    Output : The count = 0010
    
  • $write : This system task is similar to $display system task. There is no new line character at the end of the string when we use $write task.
    count = 4'b11;
    $display ("Hi, Verilog System Task");
    $display ($time); // current simulation time
    $display ("The count = %b", count);
    Output : Hi, Verilog System Task 450 The count = 0010
    
  • $monitor : This system task is exactly same as $display, but it continuously monitor variables and it executes whenever any change happens in variables. Only one $monitor is active at one time. $monitoron and $monitoroff is used to enable or disable monitoring.
    initial
    begin
    $monitor ("$time, in1=%b, in2=%b, sel=%b, mux=%b", in1,in2,sel,mux);
    end
    
    initial
    begin
    in1=0; in2=0; sel=0;
    #10 sel=1;
    #10 in1=1;
    #10 sel=0; in2=1;
    #10 sel=1; in1=0;
    #50 $finish;
    end
    
    Output
    #0 in1= 0, in2=0, sel=0, mux=0
    #10 in1= 0, in2=0, sel=1, mux=0
    #20 in1= 1, in2=0, sel=1, mux=1
    #30 in1= 1, in2=1, sel=0, mux=1
    #40 in1= 0, in2=1, sel=1, mux=0
    
  • $strobe : This system task is also used to display. This system task displays simulation data at the end of current simulation time. This is the main difference between $strobe and $monitor. See Example.
  • File I/O Tasks : This system tasks are useful to operate files using verilog language. $fopen is used to open output file and it gives file a handle. This handle is called as File Descriptor. $fclose is used to close particular open file. $fdisplay and $fwrite are used to display content of opened file. The difference between these two system tasks are same as $diplay and $write. $fmonitor and $fstrobe are used to monitor variables and if any changes happen in those variable, these two system task write to opened file. The difference between $fmonitor and $fstrobe is same as $monitor and $strobe.
module writetask(); 
integer fd1,fd2,number,pointer; 
initial 
begin 
$display("value of fd1 before opening the file %b " , fd1); 
$display("value of fd2 before opening the file %b " , fd2); 
fd1 = $fopen("xyz.txt");
fd2 = $fopen("pqr.txt"); 
$display("value of fd1 after opening the file %b " , fd1); 
$display("value of fd2 after opening the file %b " , fd2); 
repeat(7) 
begin 
pointer = $random; 
number = $random % 10; 
$fwriteo(fd1, " Number is ", number); $fwriteh(fd2, " Pointer is ", pointer); 
end 
$fclose(fd1); 
$fclose(fd2); 
end 
endmodule

Output
value of fd1 before opening the file xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
value of fd2 before opening the file xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
value of fd1 after opening the file 00000000000000000000000000000010 
value of fd2 after opening the file 00000000000000000000000000000100
in file pqr.txt 
Pointer is 12163424 Pointer is 8ab4d609 Pointer is 06bacb0d Pointer is b2123465 Pointer is acb3e301 Pointer is 3bbca176 Pointer is 76d457ed 
In file xyz.txt 
Number is 37777111767 Number is 37456777767 Number is 00acb000007 Number is 37acb777774 Number is 00000000011 Number is 00012500007 Number is 00000000002

  • Timescale Tasks : $time, $stime and $realtime are used to display current simulation time. $realtime displays time in real numbers. $stime displays current time in 32-bit integer value. $time displays current simulation time in 64-bit integer value.

$display(Current simulation time ,$time);

Output
Current simulation time 50

  • Simulation Control Tasks : These tasks are used to control the flow of simulation. $reset is used to reset time to it's initial 0. $finish is used to end the simulation process and exits to the simulator back to the operating system. $stop is used to halt the simulation process and allow user to do some work on simulation process and again simulation process can continue after completing user work.

No comments:

Post a Comment