Clock precision setting

I want to generate a 39.6 ns period clock from my test bench.

The following code generates a 40 ns clock instead. Can anyone tell me what is the problem? How to fix this precision problem? I use Modelsim simulator.

`timescale 1ns/100ps

localparam T = 39.600

logic clk = 1'b0 ;

always #(T/2) clk = ~ clk ;

In reply to Mitu Raj:

Works for me. Please show a complete example.

`timescale 1ns/100ps
module top();
  localparam T = 39.600;
  logic clk = 1'b1 ;
  always #(T/2) clk = ~ clk ;
  initial begin
    repeat (10) @(posedge clk) $display($realtime);
    $finish;
  end
endmodule
# run -all
# 39.6
# 79.2
# 118.8
# 158.4
# 198
# 237.6
# 277.2
# 316.8
# 356.4
# 396

I guess it was a tool-related problem then. I found ModelSim has its own simulation resolution setting (ns, ps, fs etc) which overrides the code’s simulation precision statement. I set it to 100 ps, and it works fine now. Thanks Dave.

In reply to Mitu Raj:

Hello Midu Raj,
I assume you are taking $time for printing the clock time.
You can observe in Mr. Dave’s code that he is using $realtime for printing clock time purpose.
When you are using a clock with decimal points and $time for printing the clock value, the function will strip off the decimal part of clock value and round it as per the decimal value.
So do make sure that you are not using $time for display purpose.

So when ever a clock is involved, do use “$realtime” for display purpose. Follow this as a guideline.

In reply to bachan21:
That was not the problem. It was a tool setting that overrode the compiler directive.

In reply to dave_59:

Okay Dave.
Then what is the behavioral difference between $time and $realtime?

In reply to bachan21:

$time returns an integer, $realtime returns a real. That could have been one possible issue with the original incomplete piece of code, but it did not show any use of either function. If they were just looking at waveforms, that would not have mattered.

In reply to dave_59:

`timescale 1ns/1ns
time t =39.6;
$display(t);

This code will return 40, right?

In reply to bachan21:

This is something you could easily answer by trying yourself.

In reply to dave_59:

Hi dave,
Executed the code. It’s showing 40.