From 698aaffb0b3d25cc1d6ce0f56535dcc985a9ee7b Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Thu, 14 Jul 2011 07:39:11 -0400 Subject: [PATCH] Fix missing leading zeros in %0d, bug367 --- Changes | 2 ++ include/verilated.cpp | 21 +++++++++++++++++++-- src/V3Number.cpp | 6 +++++- test_regress/t/t_display.pl | 5 +++-- test_regress/t/t_display.v | 8 +++++--- test_regress/t/t_display_noopt.pl | 5 +++-- 6 files changed, 37 insertions(+), 10 deletions(-) diff --git a/Changes b/Changes index 8f39ef14f..5a73aa281 100644 --- a/Changes +++ b/Changes @@ -11,6 +11,8 @@ indicates the contributor was also the author of the fix; Thanks! *** Support $fopen and I/O with integer instead of `verilator_file_descriptor. +**** Fix $display missing leading zeros in %0d, bug367. [Alex Solomatnikov] + **** Use 'vluint64_t' for SystemC instead of (same sized) 'uint64' for MSVC++. * Verilator 3.813 2011/06/28 diff --git a/include/verilated.cpp b/include/verilated.cpp index d85b5598e..1893e56e6 100644 --- a/include/verilated.cpp +++ b/include/verilated.cpp @@ -284,12 +284,14 @@ void _vl_vsformat(string& output, const char* formatp, va_list ap) { // Note also assumes variables < 64 are not wide, this assumption is // sometimes not true in low-level routines written here in verilated.cpp static VL_THREAD char tmp[VL_VALUE_STRING_MAX_WIDTH]; + const char* pctp = NULL; // Most recent %##.##g format bool inPct = false; bool widthSet = false; int width = 0; const char* pos = formatp; for (; *pos; ++pos) { if (!inPct && pos[0]=='%') { + pctp = pos; inPct = true; widthSet = false; width = 0; @@ -311,6 +313,9 @@ void _vl_vsformat(string& output, const char* formatp, va_list ap) { widthSet = true; width = width*10 + (fmt - '0'); break; + case '.': + inPct = true; // Get more digits + break; case '%': output += '%'; break; @@ -357,14 +362,26 @@ void _vl_vsformat(string& output, const char* formatp, va_list ap) { case 'd': { // Signed decimal int digits=sprintf(tmp,"%" VL_PRI64 "d",(vlsint64_t)(VL_EXTENDS_QQ(lbits,lbits,ld))); int needmore = width-digits; - if (needmore>0) output.append(needmore,' '); // Pre-pad spaces + if (needmore>0) { + if (pctp && pctp[0] && pctp[1]=='0') { //%0 + output.append(needmore,'0'); // Pre-pad zero + } else { + output.append(needmore,' '); // Pre-pad spaces + } + } output += tmp; break; } case 'u': { // Unsigned decimal int digits=sprintf(tmp,"%" VL_PRI64 "u",ld); int needmore = width-digits; - if (needmore>0) output.append(needmore,' '); // Pre-pad spaces + if (needmore>0) { + if (pctp && pctp[0] && pctp[1]=='0') { //%0 + output.append(needmore,'0'); // Pre-pad zero + } else { + output.append(needmore,' '); // Pre-pad spaces + } + } output += tmp; break; } diff --git a/src/V3Number.cpp b/src/V3Number.cpp index 3ae923851..ab2d97a8f 100644 --- a/src/V3Number.cpp +++ b/src/V3Number.cpp @@ -472,7 +472,11 @@ string V3Number::displayed(const string& vformat) const { str = cvtToStr(toUQuad()); } int intfmtsize = atoi(fmtsize.c_str()); - while ((int)(str.length()) < intfmtsize) str = " "+str; + bool zeropad = fmtsize.length()>0 && fmtsize[0]=='0'; + while ((int)(str.length()) < intfmtsize) { + if (zeropad) str = "0"+str; + else str = " "+str; + } return str; } default: diff --git a/test_regress/t/t_display.pl b/test_regress/t/t_display.pl index c8da91857..b24191fcb 100755 --- a/test_regress/t/t_display.pl +++ b/test_regress/t/t_display.pl @@ -19,8 +19,9 @@ execute ( [0] In top.v.sub2 [0] In top.v.sub2.subblock2 [0] Back \ Quote " -[0] %X=0c %D=12 %0X=c %0O=14 %B=001100 -[0] %x=0c %d=12 %0x=c %0o=14 %b=001100 +[0] %X=0c %0X=c %0O=14 %B=001100 +[0] %x=0c %0x=c %0o=14 %b=001100 +[0] %D=12 %d=12 %01d=12 %06d=000012 %6d= 12 [0] %x=00abbbbcccc %0x=abbbbcccc %o=00527356746314 %b=00000101010111011101110111100110011001100 [0] %x=00abc1234567812345678 %0x=abc1234567812345678 %o=012570110642547402215053170 %b=000001010101111000001001000110100010101100111100000010010001101000101011001111000 diff --git a/test_regress/t/t_display.v b/test_regress/t/t_display.v index 145c5fb6a..585262538 100644 --- a/test_regress/t/t_display.v +++ b/test_regress/t/t_display.v @@ -22,9 +22,11 @@ module t; $display("[%0t] Back \\ Quote \"", $time); // Old bug when \" last on the line. // Display formatting - $display("[%0t] %%X=%X %%D=%D %%0X=%0X %%0O=%0O %%B=%B", $time, - quad[5:0], quad[5:0], quad[5:0], quad[5:0], quad[5:0]); - $display("[%0t] %%x=%x %%d=%d %%0x=%0x %%0o=%0o %%b=%b", $time, + $display("[%0t] %%X=%X %%0X=%0X %%0O=%0O %%B=%B", $time, + quad[5:0], quad[5:0], quad[5:0], quad[5:0]); + $display("[%0t] %%x=%x %%0x=%0x %%0o=%0o %%b=%b", $time, + quad[5:0], quad[5:0], quad[5:0], quad[5:0]); + $display("[%0t] %%D=%D %%d=%d %%01d=%01d %%06d=%06d %%6d=%6d", $time, quad[5:0], quad[5:0], quad[5:0], quad[5:0], quad[5:0]); $display("[%0t] %%x=%x %%0x=%0x %%o=%o %%b=%b", $time, quad, quad, quad, quad); diff --git a/test_regress/t/t_display_noopt.pl b/test_regress/t/t_display_noopt.pl index 3bbecb87d..792702f91 100755 --- a/test_regress/t/t_display_noopt.pl +++ b/test_regress/t/t_display_noopt.pl @@ -22,8 +22,9 @@ execute ( [0] In top.v.sub2 [0] In top.v.sub2.subblock2 [0] Back \ Quote " -[0] %X=0c %D=12 %0X=c %0O=14 %B=001100 -[0] %x=0c %d=12 %0x=c %0o=14 %b=001100 +[0] %X=0c %0X=c %0O=14 %B=001100 +[0] %x=0c %0x=c %0o=14 %b=001100 +[0] %D=12 %d=12 %01d=12 %06d=000012 %6d= 12 [0] %x=00abbbbcccc %0x=abbbbcccc %o=00527356746314 %b=00000101010111011101110111100110011001100 [0] %x=00abc1234567812345678 %0x=abc1234567812345678 %o=012570110642547402215053170 %b=000001010101111000001001000110100010101100111100000010010001101000101011001111000