@@ -632,8 +632,11 @@ abstract class LogicValue implements Comparable<LogicValue> {
632632 /// - [chunkSize] = default: `61'h2_9ebc_5f06_5bf7`
633633 /// - [chunkSize] = 10: `61'h29e_bc5f065bf7`
634634 ///
635- /// Leading 0s are omitted in the output string:
635+ /// [leadingZeros] defaults to false, so leading 0s are omitted in
636+ /// the output string:
636637 /// - `25'h1`
638+ /// otherwise if [leadingZeros] is set to true then the output string is:
639+ /// - `25'h000_0001`
637640 ///
638641 /// When a [LogicValue] has 'x' or 'z' bits, then the radix characters those
639642 /// bits overlap will be expanded into binary form with '<' '>' bracketing
@@ -648,7 +651,10 @@ abstract class LogicValue implements Comparable<LogicValue> {
648651 /// - `9'bz_zzzz_zzzz = 9'hZZZ`
649652 ///
650653 String toRadixString (
651- {int radix = 2 , int chunkSize = 4 , String sepChar = '_' }) {
654+ {int radix = 2 ,
655+ int chunkSize = 4 ,
656+ bool leadingZeros = false ,
657+ String sepChar = '_' }) {
652658 if (radixStringChars.contains (sepChar)) {
653659 throw LogicValueConversionException ('separation character invalid' );
654660 }
@@ -661,22 +667,33 @@ abstract class LogicValue implements Comparable<LogicValue> {
661667 _ => throw LogicValueConversionException ('Unsupported radix: $radix ' )
662668 };
663669 final String reversedStr;
664- if (isValid) {
665- final radixString =
666- toBigInt ().toUnsigned (width).toRadixString (radix).toUpperCase ();
667- reversedStr = _reverse (radixString);
668- } else if (radix == 10 ) {
669- final span = (width * math.log (2 ) / math.log (radix)).floor ();
670- if (toRadixString ().contains (RegExp ('[xX]' ))) {
671- reversedStr = 'X' * span;
670+ if (radix == 10 ) {
671+ if (isValid) {
672+ var radixString =
673+ toBigInt ().toUnsigned (width).toRadixString (radix).toUpperCase ();
674+ if (leadingZeros) {
675+ final span =
676+ math.max (1 , (width * math.log (2 ) / math.log (radix)).floor ());
677+ for (var i = radixString.length; i < (width / span).ceil (); i++ ) {
678+ radixString = '0$radixString ' ;
679+ }
680+ }
681+ reversedStr = _reverse (radixString);
672682 } else {
673- reversedStr = 'Z' * span;
683+ final span =
684+ math.max (1 , (width * math.log (2 ) / math.log (radix)).floor ());
685+ if (toRadixString ().contains (RegExp ('[xX]' ))) {
686+ reversedStr = 'X' * span;
687+ } else {
688+ reversedStr = 'Z' * span;
689+ }
674690 }
675691 } else {
676692 final span = (math.log (radix) / math.log (2 )).ceil ();
677693 final extendedStr =
678694 LogicValue .of (this , width: span * (width / span).ceil ());
679695 final buf = StringBuffer ();
696+ var haveLeadingZeros = true ;
680697 for (var i = (extendedStr.width ~ / span) - 1 ; i >= 0 ; i-- ) {
681698 final binaryChunk = extendedStr.slice ((i + 1 ) * span - 1 , i * span);
682699 var chunkString = binaryChunk.toString (includeWidth: false );
@@ -695,10 +712,17 @@ abstract class LogicValue implements Comparable<LogicValue> {
695712 else
696713 binaryChunk.toBigInt ().toUnsigned (span).toRadixString (radix)
697714 ].first;
715+ if (s != '0' ) {
716+ haveLeadingZeros = false ;
717+ }
718+ if ((s == '0' ) & ! leadingZeros & haveLeadingZeros) {
719+ continue ;
720+ }
698721 buf.write (_reverse (s));
699722 }
700723 reversedStr = _reverse (buf.toString ());
701724 }
725+
702726 final spaceString = _reverse (reversedStr
703727 .replaceAllMapped (
704728 RegExp ('((>(.){$chunkSize }<)|([a-zA-Z0-9])){$chunkSize }' ),
@@ -739,10 +763,10 @@ abstract class LogicValue implements Comparable<LogicValue> {
739763 /// If the LogicValue width is not encoded as round number of radix
740764 /// characters, the leading character must be small enough to be encoded
741765 /// in the remaining width:
742- /// - 9'h1AA
743- /// - 10'h2AA
744- /// - 11'h4AA
745- /// - 12'hAAA
766+ /// - 9'h1aa
767+ /// - 10'h2aa
768+ /// - 11'h4aa
769+ /// - 12'haa
746770 static LogicValue ofRadixString (String valueString, {String sepChar = '_' }) {
747771 if (radixStringChars.contains (sepChar)) {
748772 throw LogicValueConstructionException ('separation character invalid' );
0 commit comments