@@ -632,8 +632,11 @@ abstract class LogicValue implements Comparable<LogicValue> {
632
632
/// - [chunkSize] = default: `61'h2_9ebc_5f06_5bf7`
633
633
/// - [chunkSize] = 10: `61'h29e_bc5f065bf7`
634
634
///
635
- /// Leading 0s are omitted in the output string:
635
+ /// [leadingZeros] defaults to false, so leading 0s are omitted in
636
+ /// the output string:
636
637
/// - `25'h1`
638
+ /// otherwise if [leadingZeros] is set to true then the output string is:
639
+ /// - `25'h000_0001`
637
640
///
638
641
/// When a [LogicValue] has 'x' or 'z' bits, then the radix characters those
639
642
/// bits overlap will be expanded into binary form with '<' '>' bracketing
@@ -648,7 +651,10 @@ abstract class LogicValue implements Comparable<LogicValue> {
648
651
/// - `9'bz_zzzz_zzzz = 9'hZZZ`
649
652
///
650
653
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 = '_' }) {
652
658
if (radixStringChars.contains (sepChar)) {
653
659
throw LogicValueConversionException ('separation character invalid' );
654
660
}
@@ -661,22 +667,33 @@ abstract class LogicValue implements Comparable<LogicValue> {
661
667
_ => throw LogicValueConversionException ('Unsupported radix: $radix ' )
662
668
};
663
669
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);
672
682
} 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
+ }
674
690
}
675
691
} else {
676
692
final span = (math.log (radix) / math.log (2 )).ceil ();
677
693
final extendedStr =
678
694
LogicValue .of (this , width: span * (width / span).ceil ());
679
695
final buf = StringBuffer ();
696
+ var haveLeadingZeros = true ;
680
697
for (var i = (extendedStr.width ~ / span) - 1 ; i >= 0 ; i-- ) {
681
698
final binaryChunk = extendedStr.slice ((i + 1 ) * span - 1 , i * span);
682
699
var chunkString = binaryChunk.toString (includeWidth: false );
@@ -695,10 +712,17 @@ abstract class LogicValue implements Comparable<LogicValue> {
695
712
else
696
713
binaryChunk.toBigInt ().toUnsigned (span).toRadixString (radix)
697
714
].first;
715
+ if (s != '0' ) {
716
+ haveLeadingZeros = false ;
717
+ }
718
+ if ((s == '0' ) & ! leadingZeros & haveLeadingZeros) {
719
+ continue ;
720
+ }
698
721
buf.write (_reverse (s));
699
722
}
700
723
reversedStr = _reverse (buf.toString ());
701
724
}
725
+
702
726
final spaceString = _reverse (reversedStr
703
727
.replaceAllMapped (
704
728
RegExp ('((>(.){$chunkSize }<)|([a-zA-Z0-9])){$chunkSize }' ),
@@ -739,10 +763,10 @@ abstract class LogicValue implements Comparable<LogicValue> {
739
763
/// If the LogicValue width is not encoded as round number of radix
740
764
/// characters, the leading character must be small enough to be encoded
741
765
/// 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
746
770
static LogicValue ofRadixString (String valueString, {String sepChar = '_' }) {
747
771
if (radixStringChars.contains (sepChar)) {
748
772
throw LogicValueConstructionException ('separation character invalid' );
0 commit comments