diff options
| -rw-r--r-- | chiselFrontend/src/main/scala/chisel3/core/package.scala | 18 | ||||
| -rw-r--r-- | src/test/scala/chiselTests/WidthSpec.scala | 17 |
2 files changed, 23 insertions, 12 deletions
diff --git a/chiselFrontend/src/main/scala/chisel3/core/package.scala b/chiselFrontend/src/main/scala/chisel3/core/package.scala index 1d5817ae..87dca1f3 100644 --- a/chiselFrontend/src/main/scala/chisel3/core/package.scala +++ b/chiselFrontend/src/main/scala/chisel3/core/package.scala @@ -55,14 +55,17 @@ package chisel3 { implicit class fromStringToLiteral(val str: String) { /** String to UInt parse, recommended style for constants. */ - def U(): UInt = UInt.Lit(parse(str), parsedWidth(str)) // scalastyle:ignore method.name + def U(): UInt = str.asUInt() // scalastyle:ignore method.name /** String to UInt parse with specified width, recommended style for constants. */ - def U(width: Width): UInt = UInt.Lit(parse(str), width) // scalastyle:ignore method.name + def U(width: Width): UInt = str.asUInt(width) // scalastyle:ignore method.name /** String to UInt parse, recommended style for variables. */ - def asUInt(): UInt = UInt.Lit(parse(str), parsedWidth(str)) + def asUInt(): UInt = { + val bigInt = parse(str) + UInt.Lit(bigInt, Width(bigInt.bitLength max 1)) + } /** String to UInt parse with specified width, recommended style for variables. */ def asUInt(width: Width): UInt = UInt.Lit(parse(str), width) @@ -78,15 +81,6 @@ package chisel3 { } BigInt(num.filterNot(_ == '_'), radix) } - - protected def parsedWidth(n: String) = - if (n(0) == 'b') { - Width(n.length-1) - } else if (n(0) == 'h') { - Width((n.length-1) * 4) - } else { - Width() - } } implicit class fromBooleanToLiteral(val boolean: Boolean) { diff --git a/src/test/scala/chiselTests/WidthSpec.scala b/src/test/scala/chiselTests/WidthSpec.scala new file mode 100644 index 00000000..9a5b1860 --- /dev/null +++ b/src/test/scala/chiselTests/WidthSpec.scala @@ -0,0 +1,17 @@ +// See LICENSE for license details. + +package chiselTests + +import chisel3._ + +class WidthSpec extends ChiselFlatSpec { + "Literals without specified widths" should "get the minimum legal width" in { + "hdeadbeef".U.getWidth should be (32) + "h_dead_beef".U.getWidth should be (32) + "h0a".U.getWidth should be (4) + "h1a".U.getWidth should be (5) + "h0".U.getWidth should be (1) + 1.U.getWidth should be (1) + 1.S.getWidth should be (2) + } +} |
