From a1e8ae80888b6b4d7bac057c6486b3442334cdc6 Mon Sep 17 00:00:00 2001 From: Jack Koenig Date: Mon, 5 Dec 2016 11:28:04 -0800 Subject: Fix literal width (#389) Fixes #388--- .../src/main/scala/chisel3/core/package.scala | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) (limited to 'chiselFrontend/src/main') 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) { -- cgit v1.2.3