On 8/26/2013 3:46 PM, Dave R wrote:
> On Monday, August 26, 2013 10:24:31 AM UTC-7, unfrostedpoptart
> wrote:
>> Hi all.
>>
>>
>>
>> This is one of those things I've never thought about after many
>> years of using Verilog and now I need it can't can't figure out how
>> to do it. I just want a way for my code to know the current
>> timescale so it can interpret $time correctly. The only thing
>> close I've found is $printtimescale, but that's just a print
>> statement and I need to capture it's output into variables. I've
>> googled and searched the LRM and can't find anything.
>>
>
> In SystemVerilog, you can write (timevar = $time/1ns) or better
> (timevar $realtime/1ns) and you will get your time back in ns. If you
> need to use timvar as part of a delay expression, make sure you scale
> it back, i.e. #(timevar*1ns).
As written does this actually work for modules with a time unit greater
than 1ns? I understand that the 1ns gets scaled to the local time unit
and truncated using the local time precision, but I would assume that
the 1ns time literal in a 10ns/10ns context would round to zero not the
0.1 you need to make this work universally. I think for this scheme to
work you need to use "1s" or a scaling time literal that has a value
that is as big as the largest time precision, preferably use $realtime
to get the best local time value and a real variable to hold the result.
Of course using "1s" breaks if you have a module with a 10s or 100s time
precision and if you use a larger scaling value you will have problems
with 1fs precisions because of the limited precision in a real value. So
this scheme has some minor problems, but should work for most cases.
Going back to Verilog-A for inspiration it has a time function $abstime
which returns the simulation time in seconds. It's trivial to implement
$abstime as a scaled version of $realtime using the VPI. Though I'm not
sure this is 100% correct since $abstime may not round the value using
the local precision while $realtime is required to do this. I would need
to do some testing to see exactly what it does since the Verilog-A
standard is not very descriptive.
Cary