diff options
| -rw-r--r-- | chiselFrontend/src/main/scala/chisel3/core/Aggregate.scala | 7 | ||||
| -rw-r--r-- | chiselFrontend/src/main/scala/chisel3/core/Bits.scala | 6 | ||||
| -rw-r--r-- | chiselFrontend/src/main/scala/chisel3/core/Data.scala | 12 | ||||
| -rw-r--r-- | src/main/scala/chisel3/util/BitPat.scala | 2 | ||||
| -rw-r--r-- | src/main/scala/chisel3/util/Bitwise.scala | 2 | ||||
| -rw-r--r-- | src/test/scala/chiselTests/Reg.scala | 11 |
6 files changed, 25 insertions, 15 deletions
diff --git a/chiselFrontend/src/main/scala/chisel3/core/Aggregate.scala b/chiselFrontend/src/main/scala/chisel3/core/Aggregate.scala index 459a3b67..12ae7e6b 100644 --- a/chiselFrontend/src/main/scala/chisel3/core/Aggregate.scala +++ b/chiselFrontend/src/main/scala/chisel3/core/Aggregate.scala @@ -15,8 +15,8 @@ import chisel3.internal.sourceinfo.{SourceInfo, DeprecatedSourceInfo, VecTransfo * of) other Data objects. */ sealed abstract class Aggregate extends Data { - private[chisel3] def cloneTypeWidth(width: Width): this.type = cloneType - private[chisel3] def width: Width = flatten.map(_.width).reduce(_ + _) + private[core] def cloneTypeWidth(width: Width): this.type = cloneType + private[core] def width: Width = flatten.map(_.width).reduce(_ + _) } object Vec { @@ -388,5 +388,6 @@ class Bundle extends Aggregate { } private[core] object Bundle { - val keywords = List("flip", "asInput", "asOutput", "cloneType", "toBits", "chiselCloneType") + val keywords = List("flip", "asInput", "asOutput", "cloneType", "toBits", + "widthOption", "chiselCloneType") } diff --git a/chiselFrontend/src/main/scala/chisel3/core/Bits.scala b/chiselFrontend/src/main/scala/chisel3/core/Bits.scala index 7a3962a0..c2621251 100644 --- a/chiselFrontend/src/main/scala/chisel3/core/Bits.scala +++ b/chiselFrontend/src/main/scala/chisel3/core/Bits.scala @@ -385,7 +385,7 @@ abstract trait Num[T <: Data] { sealed class UInt private[core] (width: Width, lit: Option[ULit] = None) extends Bits(width, lit) with Num[UInt] { - private[chisel3] override def cloneTypeWidth(w: Width): this.type = + private[core] override def cloneTypeWidth(w: Width): this.type = new UInt(w).asInstanceOf[this.type] private[chisel3] def toType = s"UInt$width" @@ -577,7 +577,7 @@ object UInt extends UIntFactory sealed class SInt private (width: Width, lit: Option[SLit] = None) extends Bits(width, lit) with Num[SInt] { - private[chisel3] override def cloneTypeWidth(w: Width): this.type = + private[core] override def cloneTypeWidth(w: Width): this.type = new SInt(w).asInstanceOf[this.type] private[chisel3] def toType = s"SInt$width" @@ -721,7 +721,7 @@ object SInt { /** A data type for booleans, defined as a single bit indicating true or false. */ sealed class Bool(lit: Option[ULit] = None) extends UInt(Width(1), lit) { - private[chisel3] override def cloneTypeWidth(w: Width): this.type = { + private[core] override def cloneTypeWidth(w: Width): this.type = { require(!w.known || w.get == 1) new Bool().asInstanceOf[this.type] } diff --git a/chiselFrontend/src/main/scala/chisel3/core/Data.scala b/chiselFrontend/src/main/scala/chisel3/core/Data.scala index 9115d1ba..bf68773f 100644 --- a/chiselFrontend/src/main/scala/chisel3/core/Data.scala +++ b/chiselFrontend/src/main/scala/chisel3/core/Data.scala @@ -117,8 +117,9 @@ abstract class Data extends HasId { } private[chisel3] def lref: Node = Node(this) private[chisel3] def ref: Arg = if (isLit) litArg.get else lref - private[chisel3] def cloneTypeWidth(width: Width): this.type + private[core] def cloneTypeWidth(width: Width): this.type private[chisel3] def toType: String + private[core] def width: Width def cloneType: this.type def chiselCloneType: this.type = { @@ -136,8 +137,13 @@ abstract class Data extends HasId { def litValue(): BigInt = litArg.get.num def isLit(): Boolean = litArg.isDefined - private[core] def width: Width + /** Returns the width, in bits, if currently known. + * @throws java.util.NoSuchElementException if the width is not known. */ final def getWidth: Int = width.get + /** Returns whether the width is currently known. */ + final def isWidthKnown: Boolean = width.known + /** Returns Some(width) if the width is known, else None. */ + final def widthOption: Option[Int] = if (isWidthKnown) Some(getWidth) else None // While this being in the Data API doesn't really make sense (should be in // Aggregate, right?) this is because of an implementation limitation: @@ -241,7 +247,7 @@ object Clock { sealed class Clock extends Element(Width(1)) { def cloneType: this.type = Clock().asInstanceOf[this.type] private[chisel3] override def flatten: IndexedSeq[Bits] = IndexedSeq() - private[chisel3] def cloneTypeWidth(width: Width): this.type = cloneType + private[core] def cloneTypeWidth(width: Width): this.type = cloneType private[chisel3] def toType = "Clock" override def connect (that: Data)(implicit sourceInfo: SourceInfo): Unit = that match { diff --git a/src/main/scala/chisel3/util/BitPat.scala b/src/main/scala/chisel3/util/BitPat.scala index 5b37bd1b..e79b882b 100644 --- a/src/main/scala/chisel3/util/BitPat.scala +++ b/src/main/scala/chisel3/util/BitPat.scala @@ -68,7 +68,7 @@ object BitPat { */ def apply(x: UInt): BitPat = { require(x.isLit) - val len = if (x.widthKnown) x.getWidth else 0 + val len = if (x.isWidthKnown) x.getWidth else 0 apply("b" + x.litValue.toString(2).reverse.padTo(len, "0").reverse.mkString) } } diff --git a/src/main/scala/chisel3/util/Bitwise.scala b/src/main/scala/chisel3/util/Bitwise.scala index 2743e59f..1d48ec0a 100644 --- a/src/main/scala/chisel3/util/Bitwise.scala +++ b/src/main/scala/chisel3/util/Bitwise.scala @@ -29,7 +29,7 @@ object Fill { n match { case 0 => UInt.width(0) case 1 => x - case _ if x.widthKnown && x.getWidth == 1 => + case _ if x.isWidthKnown && x.getWidth == 1 => Mux(x.toBool, UInt((BigInt(1) << n) - 1, n), UInt(0, n)) case _ if n > 1 => val p2 = Array.ofDim[UInt](log2Up(n + 1)) diff --git a/src/test/scala/chiselTests/Reg.scala b/src/test/scala/chiselTests/Reg.scala index b66d7cb4..a9086223 100644 --- a/src/test/scala/chiselTests/Reg.scala +++ b/src/test/scala/chiselTests/Reg.scala @@ -16,7 +16,7 @@ class RegSpec extends ChiselFlatSpec { "A Reg" should "be of the same type and width as outType, if specified" in { class RegOutTypeWidthTester extends BasicTester { - val reg = Reg(t=UInt.width(2), next=Wire(UInt.width(3)), init=UInt(20)) + val reg = Reg(t=UInt(width=2), next=Wire(UInt(width=3)), init=UInt(20)) reg.getWidth should be (2) } elaborate{ new RegOutTypeWidthTester } @@ -24,11 +24,14 @@ class RegSpec extends ChiselFlatSpec { "A Reg" should "be of unknown width if outType is not specified and width is not forced" in { class RegUnknownWidthTester extends BasicTester { - val reg1 = Reg(next=Wire(UInt.width(3)), init=20.U) + val reg1 = Reg(next=Wire(UInt(width=3)), init=UInt(20)) + reg1.isWidthKnown should be (false) DataMirror.widthOf(reg1).known should be (false) - val reg2 = Reg(init=20.U) + val reg2 = Reg(init=UInt(20)) + reg2.isWidthKnown should be (false) DataMirror.widthOf(reg2).known should be (false) - val reg3 = Reg(next=Wire(UInt.width(3)), init=5.U) + val reg3 = Reg(next=Wire(UInt(width=3)), init=UInt(5)) + reg3.isWidthKnown should be (false) DataMirror.widthOf(reg3).known should be (false) } elaborate { new RegUnknownWidthTester } |
