Pasted test.sv/test2.sv content below;
(1) for 20ns delay, I am getting print “20000” against “1 ps / 1 ps” (makes sense, 20ns = 20000ps);
But, output is “20000000” (20 X 1000 X 1000) for “1 ns / 1 ps” timescale (as revealed by $printtimescale) – why is that?
TimeScale of $unit is 1 ns / 1 ps
20000000
TimeScale of test is 1 ps / 1 ps
20000
TimeScale of $unit is 1 ns / 1 ps
20000000
(2) If I uncomment “$timeformat()” calls in both printA and printB, this is what I get – so $timeformat won’t override `timescale, correct?
TimeScale of $unit is 1 ns / 1 ps
20000.000 ns
TimeScale of test is 1 ps / 1 ps
20.000 ns
TimeScale of $unit is 1 ns / 1 ps
20000.000 ns
test.sv -->
`timescale 1ps/1ps
typedef A;
class A;
time t1;
function void printA;
$printtimescale;
//$timeformat( -9, 3, " ns", 0 );
$display("%0t",t1);
endfunction
endclass
module test;
`include "test2.SystemVerilog";
A a1=new();
B b1=new();
initial begin
#20ns;
a1.t1=$realtime;
b1.t1=$realtime;
$display("----------------------\n");
a1.printA();
$display("----------------------\n");
b1.printB();
$display("----------------------\n");
b1.printA();
$display("----------------------\n");
end
endmodule
test2.sv -->
`timescale 1ns/1ps
class B extends A;
function void printB;
$printtimescale;
//$timeformat( -9, 3, " ns", 0 );
$display("%0t",t1);
endfunction
endclass
The
`timescale compiler directive has no affect on code written in $unit. The $unit time scale relies on tool defaults or the timeunit construct. There can only be one time scale in a particular $unit, module, or any other design unit.
The first timescale sets the time scale and precision of the test block it has no effect on class A. The time scale and precision of class A is set by tool defaults. You will get different results on different tools unless you use the timeunit directive or put everything in a package. The timeunit construct was designed to replace timescale (See 3.14.2.3 Precedence of timeunit, timeprecision, and timescale). The second timescale has no effect class B. It can only affect other design units that follow.
$realtime return 20000.0 because the time is 20ns and the current timescale is 1ps. That value gets converted to an 64-bit integer to be stored in t1. The fact that 20000 represents 20000ps is lost—it’s just a plain integer.
Without $timeformat, SystemVerilog scales the %t argument from what ever the current time scale is to the global precision (ps). For printA(), your tool has chosen a time scale of 1ns. So 20000 becomes 20000ns and gets scaled to 20000000 ps. With $timeforat, the %t argument gets scaled from the current time scale (ns) to the specified time scale -9 (ns). So 20000.00ns get printed.
In printB(), the times scale used to write t1 and read with $display are the same, the you get the expected output of 20000 ps or 20.0ns.