Delay unit is specified using 'timescale, which is declared as `timescale time_unit base / precision base
--time_unit is the amount of time a delay of #1 represents. The time unit must be 1 10 or 100
--base is the time base for each unit, ranging from seconds to femtoseconds, and must be: s ms us ns ps or fs
--precision and base represent how many decimal points of precision to use relative to the time units.
For example : `timescale 1 ns / 100 ps means
time values to be read as ns and to be rounded to the nearest 100 ps.
If timescale is omitted, there is a default time scale.
The following examples demonstrate how the time scale and time precision effect $stime, #delay and toggling in waveform.
EXAMPLE: `timescale 10ns/10ns
module tim();
reg i;
initial begin i=0;
#7.7212;
i=1;
$display("STATEMENT 1 :: time is ",$stime);
#7.123;
$finish;
end
endmodule module try;
time delay_time = 7.721;
initialbegin $display("STATEMENT 2 :: delay for %0t",delay_time );
end endmodule
RESULTS:
STATEMENT 1 :: time is 8 STATEMENT 2 :: delay for 8
reg i toggled at 80 in waveform debugger
EXAMPLE: `timescale 10ns/1ns
module tim();
reg i;
initial begin i=0;
#7.7212;
i=1;
$display("STATEMENT 1 :: time is ",$stime);
#7.123;
$finish;
end
endmodule
module try;
time delay_time = 7.721;
initialbegin $display("STATEMENT 2 :: delay for %0t",delay_time );
end endmodule RESULTS:
STATEMENT 1 :: time is 8 STATEMENT 2 :: delay for 80
reg i toggled at 77 waveform debugger
EXAMPLE: `timescale 10ns/1ps
module tim();
reg i;
initial begin i=0;
#7.7212;
i=1;
$display("STATEMENT 1 :: time is ",$stime);
#7.123;
$finish;
end
endmodule module try;
time delay_time = 7.721;
initialbegin $display("STATEMENT 2 :: delay for %0t",delay_time );
end endmodule
RESULTS:
STATEMENT 1 :: time is 8 STATEMENT 2 :: delay for 80000
reg i toggled at 77.212 in waveform debugger
In the timescale statement, the first value is the time unit and the second is the precision for the simulation. So with the time unit, when the simulator displays a value, you just have to multiply the value by this time unit to get the real time. With a 10ns time unit, when a delay of #7.7212, that means that it is 77.212ns delay.
Now the second one (time precision) specify with which precision time values are rounded. Asking for a 77.212ns delay is possible only with a 1ps precision. That's what you can see with reg i. It toggles at 77ns with a 1ns precision and 77.212 with a 1ps precision.
For the STATEMENT 1, $stime returns an integer scaled to timesale unit, that's why results are always 8 which is 7.7212 rounded up to 8.
Now for STATEMENT 2, the way %t is printed depends on $timeformat. It seems that in this case, 7.7212 is first rounded to an integer => 8 and then printed according to your time precision.
Im not sure of this topic. If some one finds mistake in my understanding, please mail me at gopi@testbench.in
Each module can have separate time scale. The smallest time_precision argument of all the timescale compiler directives in the design determines the precision of the time unit of the simulation.
Lets take an example. There are two modules. Module_1 is instance od Module_2. Module_1 has timescale of 1 ns/1ps. Module_2 has time scale of 1ns / 10ps. The smallest resolution is 1ps. This is taken as simulator resolution but each module evaluates according to its precision mentioned.
Lets take another example. There are two modules. Module_1 is instance of Module_2. Module_1 does not have any time scale. Module_2 is having time scale of 1ns/100 ps. As there is no time scale for Module_1 ,the simulator takes precision and time unit of 100 ps i.e `timescale 100 ps/100 ps.
$Time Vs $Realtime
$time round offs the time to nearby integer where as $realtime does not. So when you are using real valued delays, then use $realtime instead of $time , else there may be a misunderstanding during debugging.
System Task Printtimescale
The $printtimescale system task displays the time unit and precision for a particular module. When no argument is specified, $printtimescale displays the time unit and precision of the module that is the current scope. When an argument is specified, $printtimescale displays the time unit and precision of the module passed to it.
EXAMPLE: `timescale 1 ms / 1 us
module a_dat;
initial $printtimescale(b_dat.c1);
endmodule
The $timeformat system task performs the following two functions:
1)It specifies how the %t format specification reports time information for the $write, $display,$strobe, $monitor, $fwrite, $fdisplay, $fstrobe, and $fmonitor group of system tasks.
2)It specifies the time unit for delays entered interactively.
The units number argument shall be an integer in the range from 0 to -15. This argument represents the time
unit as shown in table
Unit number Time unit Unit number Time unit
0 1 s -8 10 ns
-1 100 ms -9 1 ns
-2 10 ms -10 100 ps
-3 1 ms -11 10 ps
-4 100 us -12 1 ps
-5 10 us -13 100 fs
-6 1 us -14 10 fs
-7 100 ns -15 1 fs
Syntax : $timeformat(time unit, precision number, suffix string, and minimum field width);