Hi, I am trying to read time values with their time-unit from a csv file. What I am doing is that:
time tx, ty;
$sscanf(splits[0], "%t", tx);
$sscanf(splits[1], "%t", ty);
In the file splits[0] = 100ns,
and splits[1] = 100us
The problem I face is that, it always reads it as 100us even when it is 100ms because us is default time unit. That’s why it is always assigning as us. Is there a proper way of assigning it to a time variable with its time-unit.
csv file looks like this:
timeX, 100ns
timeY, 100us
timeZ, 100ms
Thank you for your help.
The %t format of $sscanf only reads a real number and scales it according to $timeformat and the local time scale. You need to read the time value separately from the time unit literal.
Here’s an example you can play with
`timescale 10ns/10ns
module top;
real tx;
string s;
initial begin
$timeformat(-9,2,"ns",10);
$sscanf("123ps", "%t%s", tx,s);
$display(tx,, s);
$timeformat(-12,2,"ps",10);
$sscanf("123ps", "%t%s", tx,s);
$display(tx,, s);
end
endmodule
Thank you for your answer. After I read the time information, I need to wait in my testbench for that time duration.
For example:
initial begin
#(tx);
.......
#(ty);
......
$stop;
end
I can of course compare them in an if-else block whether it is ns, us, etc and then wait accordingly. But this is not convenient. Is there a better solution for my problem?
No need for a if-else block. Use an associative array as a lookup table
module top;
timeunit 1ns/1ps;
real tx,ty;
int code;
const real scale[string] = '{"fs":1fs, "ps":1ps, "ns":1ns, "us":1us, "ms":1ms, "s":1s};
string s;
initial begin
code = $sscanf("123ps", "%g%s", tx,s);
tx *= scale[s]; // equivalent to tx = tx * scale[s];
#(tx);
$display(tx,, "ns",, $realtime );
code = $sscanf("123ms", "%g%s", ty,s);
ty *= scale[s];
#(ty);
$display(ty,, "ns",, $realtime);
end
endmodule
2 Likes
I appreciate your help. That is a very elegant solution for my case.