The Designer's Guide Community Forum
https://designers-guide.org/forum/YaBB.pl
Design Languages >> Verilog-AMS >> verilogA: how to print out number with parametric precision?
https://designers-guide.org/forum/YaBB.pl?num=1331033757

Message started by Irvin73 on Mar 6th, 2012, 3:35am

Title: verilogA: how to print out number with parametric precision?
Post by Irvin73 on Mar 6th, 2012, 3:35am

Hi all,
I want to print out some data from my verilogA code. Let's say for this task I'm using code like:


Code:
$strobe("Conversion value: %6.2f (LSB)",counter_value);


and get this response as expected: Conversion value: 700.00 (LSB)

But the question is, how can I print with parametric precision? I mean if I want change the number of digits after point according to parameter, there is a way to do it?

Following straight forward approach (create the pattern string first dependent on parameter, then strobe it) doesn't solve the problem:


Code:
parameter integer intPrecision = 2;
string strPattern;

strPattern= $sscanf("Conversion value: %%6.%df (LSB)",intPrecision);
$strobe(strPattern,counter_value);


I get output like: Conversion value: %6.2f (LSB)  700

It means $strobe function behaves in different way for these two cases for some reason. Any suggestions about possible solution?
Thanks in advance.

Title: Re: verilogA: how to print out number with parametric precision?
Post by Geoffrey_Coram on Mar 6th, 2012, 10:41am

I don't recall seeing support for variable field widths in Verilog, but since the simulator generally translates Verilog to C and compiles the C into object code, did you try something like

$strobe("Conversion value: %*.*f (LSB)", 6, 2, counter_value);

?

Title: Re: verilogA: how to print out number with parametric precision?
Post by Irvin73 on Mar 7th, 2012, 11:00am

Hi Geoffrey,
Thanks for your response. Unfortunately,  when trying your suggestion, I got the compilation error:

Encountered unexpected in percentage format command, '*'. Use a valid percentage format command and try again

Because for practical resasons the number of different values for precision digits is small (say 1,2,..6), I simply used the "case" statement workaround, like:


Code:
case(1)
   (intPrecision == 1) :  $strobe("Conversion value: %6.1f (LSB)",counter_value);
   (intPrecision == 2) :  $strobe("Conversion value: %6.2f (LSB)",counter_value);
   (intPrecision == 3) :  $strobe("Conversion value: %6.3f (LSB)",counter_value);
   (intPrecision == 4) :  $strobe("Conversion value: %6.4f (LSB)",counter_value);
   default                  $strobe("Conversion value: %6.1f (LSB)",counter_value);
endcase

Of course it is so nasty (especially if you have a lot of $strobe or $fwrite statements!), but it works.
If anyone will share some magic trick to solve it in clean way, it will be good news. Meanwhile I continue with this workaround.

Thanks again for your response :)


The Designer's Guide Community Forum » Powered by YaBB 2.2.2!
YaBB © 2000-2008. All Rights Reserved.