# Clock precision setting

**URL:** <https://verificationacademy.com/forums/t/clock-precision-setting/38771>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, clock\
**Created:** [March 2, 2021, 6:04am UTC](https://verificationacademy.com/forums/t/clock-precision-setting/38771 "2021-03-02T06:04:24Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mitu\_Raj](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/mitu_raj/32/1966_2.png) [@Mitu\_Raj](https://verificationacademy.com/forums/u/Mitu_Raj)\
**Post date:** [March 2, 2021, 6:04am UTC](https://verificationacademy.com/forums/t/clock-precision-setting/38771/1 "2021-03-02T06:04:24Z")

</div>

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.

```verilog
`timescale 1ns/100ps

localparam T = 39.600

logic clk = 1'b0 ;

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

```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [March 2, 2021, 9:45pm UTC](https://verificationacademy.com/forums/t/clock-precision-setting/38771/2 "2021-03-02T21:45:11Z")

</div>

_In reply to [Mitu Raj](https://verificationacademy.com/forums/t/clock-precision-setting/38771):_

Works for me. Please show a complete example.

```verilog
`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

```

```auto
# run -all
# 39.6
# 79.2
# 118.8
# 158.4
# 198
# 237.6
# 277.2
# 316.8
# 356.4
# 396

```

---

<div class="post-metadata">

**Author:** ![Mitu\_Raj](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/mitu_raj/32/1966_2.png) [@Mitu\_Raj](https://verificationacademy.com/forums/u/Mitu_Raj)\
**Post date:** [March 3, 2021, 4:08am UTC](https://verificationacademy.com/forums/t/clock-precision-setting/38771/3 "2021-03-03T04:08:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bachan21](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/bachan21/32/1208_2.png) [@bachan21](https://verificationacademy.com/forums/u/bachan21)\
**Post date:** [March 3, 2021, 6:30am UTC](https://verificationacademy.com/forums/t/clock-precision-setting/38771/4 "2021-03-03T06:30:25Z")

</div>

_In reply to [Mitu Raj](https://verificationacademy.com/forums/t/clock-precision-setting/38771/3):_

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.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [March 3, 2021, 5:00pm UTC](https://verificationacademy.com/forums/t/clock-precision-setting/38771/5 "2021-03-03T17:00:44Z")

</div>

_In reply to [bachan21](https://verificationacademy.com/forums/t/clock-precision-setting/38771/4):_  
That was not the problem. It was a tool setting that overrode the compiler directive.

---

<div class="post-metadata">

**Author:** ![bachan21](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/bachan21/32/1208_2.png) [@bachan21](https://verificationacademy.com/forums/u/bachan21)\
**Post date:** [March 5, 2021, 7:32am UTC](https://verificationacademy.com/forums/t/clock-precision-setting/38771/6 "2021-03-05T07:32:29Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/clock-precision-setting/38771/5):_

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

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [March 5, 2021, 8:28am UTC](https://verificationacademy.com/forums/t/clock-precision-setting/38771/7 "2021-03-05T08:28:54Z")

</div>

_In reply to [bachan21](https://verificationacademy.com/forums/t/clock-precision-setting/38771/6):_

$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.

---

<div class="post-metadata">

**Author:** ![bachan21](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/bachan21/32/1208_2.png) [@bachan21](https://verificationacademy.com/forums/u/bachan21)\
**Post date:** [March 5, 2021, 9:05am UTC](https://verificationacademy.com/forums/t/clock-precision-setting/38771/8 "2021-03-05T09:05:46Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/clock-precision-setting/38771/7):_

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

```

This code will return 40, right?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [March 5, 2021, 5:40pm UTC](https://verificationacademy.com/forums/t/clock-precision-setting/38771/9 "2021-03-05T17:40:19Z")

</div>

_In reply to [bachan21](https://verificationacademy.com/forums/t/clock-precision-setting/38771/8):_

This is something you could easily answer by trying yourself.

---

<div class="post-metadata">

**Author:** ![bachan21](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/bachan21/32/1208_2.png) [@bachan21](https://verificationacademy.com/forums/u/bachan21)\
**Post date:** [March 6, 2021, 7:29am UTC](https://verificationacademy.com/forums/t/clock-precision-setting/38771/10 "2021-03-06T07:29:35Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/clock-precision-setting/38771/9):_

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