In reply to moustafaali:
I am running Code 1, and getting the correct output. What run commands are you using?
// Code 1
`timescale 1ns/1ps
module timing;
initial begin
$timeformat(-9, 3, "ns", 8);
#1.0ns $display("%t", $realtime); // 1.000ns
#2.0ns $display("%t", $realtime); // 3.000ns
#0.1ns $display("%t", $realtime); // 3.100ns
#41ps $display("%t", $realtime); // 3.141ns
end
endmodule
Output --
# run -all
# 1.000ns
# 3.000ns
# 3.100ns
# 3.141ns
# quit -f