The Designer's Guide Community
Forum
Welcome, Guest. Please Login or Register. Please follow the Forum guidelines.
Jul 18th, 2024, 12:32am
Pages: 1
Send Topic Print
verilogA: how to print out number with parametric precision? (Read 7551 times)
Irvin73
New Member
*
Offline



Posts: 6
Israel
verilogA: how to print out number with parametric precision?
Mar 06th, 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.
Back to top
 
 
View Profile   IP Logged
Geoffrey_Coram
Senior Fellow
******
Offline



Posts: 1999
Massachusetts, USA
Re: verilogA: how to print out number with parametric precision?
Reply #1 - 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);

?
Back to top
 
 

If at first you do succeed, STOP, raise your standards, and stop wasting your time.
View Profile WWW   IP Logged
Irvin73
New Member
*
Offline



Posts: 6
Israel
Re: verilogA: how to print out number with parametric precision?
Reply #2 - 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 Smiley

Back to top
 
 
View Profile   IP Logged
Pages: 1
Send Topic Print
Copyright 2002-2024 Designer’s Guide Consulting, Inc. Designer’s Guide® is a registered trademark of Designer’s Guide Consulting, Inc. All rights reserved. Send comments or questions to editor@designers-guide.org. Consider submitting a paper or model.