0

I am having hard time working with systemverilog data type time.

Lets say there are two packages coming from third party.

package p1;
  import p2:*;
  class d;
    time c;
    function print();
      $display("t1 is %t, t2 is %t\n", c.t1, c.t2);
      c = t2 - t1;
      $display("c is %t\n", c);
    endfunction  
endpackage : p1

package p2;
  class c;
    time t1;
    time t2;
  endclass : c
endpackage : p2

If t2 is 100ns, t1 is 10ns, c prints out 900000. Can someone tell me what might have happened with the code above? Is there a way to check the time granularity in both the packages?

user1978273
  • 484
  • 10
  • 24

2 Answers2

2

First of all, there is nothing special about the time data type—it's just a 64-bit unsigned integer. What matters is the timescale/unit where you make assignments to t1 and t2 using $time

Unfortunately there's no way to know what the timescale is from another design unit is unless you establish a mutually agreed upon time unit. You can do that by dividing all times by that mutual unit.

realtime t1;

t1 = $realtime/1ns;

It's best to use $realtime to get any fractional time steps.

dave_59
  • 39,096
  • 3
  • 24
  • 63
0

From this example. You can use the $printtimescale system function to display the default timescale or the timescale of a given module instance.

You could log this and do checks in your external batch processing, e.g. via a Makefile.

Or see my other answer, here, which captures the timescale into a variable then uses it to measure frequency and time.

    `timescale 1ns/1ps  
      
    module tb;  
      des m_des();  
      alu m_alu();  
      
      initial begin  
        $printtimescale(tb);  
        $printtimescale(tb.m_alu);  
        $printtimescale(tb.m_des);  
      end  
    endmodule  
      
    module alu;  
      
    endmodule  
      
    `timescale 1ns/10ps  
      
    module des;  
      
    endmodule  
Jay M
  • 3,736
  • 1
  • 24
  • 33