summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Rigge2019-05-01 19:55:21 -0700
committeredwardcwang2019-05-01 19:55:20 -0700
commit94d5b90493b42dd2c85d0d94ea707a69160d0536 (patch)
tree39c3fc0b6e05f5b05c5d826203c2e04092217fcd
parentc1ab9e7afd5072c11d879db913e1b553c7fe0dbe (diff)
Make asTypeOf work for bundles with zero-width fields. (#1079)
Closes #1075.
-rw-r--r--chiselFrontend/src/main/scala/chisel3/core/Aggregate.scala12
-rw-r--r--src/test/scala/chiselTests/AsTypeOfTester.scala22
2 files changed, 32 insertions, 2 deletions
diff --git a/chiselFrontend/src/main/scala/chisel3/core/Aggregate.scala b/chiselFrontend/src/main/scala/chisel3/core/Aggregate.scala
index 35f53013..c75974f0 100644
--- a/chiselFrontend/src/main/scala/chisel3/core/Aggregate.scala
+++ b/chiselFrontend/src/main/scala/chisel3/core/Aggregate.scala
@@ -66,8 +66,16 @@ sealed abstract class Aggregate extends Data {
var i = 0
val bits = WireDefault(UInt(this.width), that) // handles width padding
for (x <- flatten) {
- x.connectFromBits(bits(i + x.getWidth - 1, i))
- i += x.getWidth
+ val fieldWidth = x.getWidth
+ if (fieldWidth > 0) {
+ x.connectFromBits(bits(i + fieldWidth - 1, i))
+ i += fieldWidth
+ } else {
+ // There's a zero-width field in this bundle.
+ // Zero-width fields can't really be assigned to, but the frontend complains if there are uninitialized fields,
+ // so we assign it to DontCare. We can't use connectFromBits() on DontCare, so use := instead.
+ x := DontCare
+ }
}
}
}
diff --git a/src/test/scala/chiselTests/AsTypeOfTester.scala b/src/test/scala/chiselTests/AsTypeOfTester.scala
index c78ddb17..563e2b11 100644
--- a/src/test/scala/chiselTests/AsTypeOfTester.scala
+++ b/src/test/scala/chiselTests/AsTypeOfTester.scala
@@ -27,6 +27,24 @@ class AsTypeOfBundleTester extends BasicTester {
stop()
}
+class AsTypeOfBundleZeroWidthTester extends BasicTester {
+ class ZeroWidthBundle extends Bundle {
+ val a = UInt(0.W)
+ val b = UInt(1.W)
+ val c = UInt(0.W)
+ }
+
+ val bun = new ZeroWidthBundle
+
+ val bunAsTypeOf = 1.U.asTypeOf(bun)
+
+ assert(bunAsTypeOf.a === 0.U)
+ assert(bunAsTypeOf.b === 1.U)
+ assert(bunAsTypeOf.c === 0.U)
+
+ stop()
+}
+
class AsTypeOfVecTester extends BasicTester {
val vec = ((15 << 12) + (0 << 8) + (1 << 4) + (2 << 0)).U.asTypeOf(Vec(4, SInt(4.W)))
@@ -103,6 +121,10 @@ class AsTypeOfSpec extends ChiselFlatSpec {
assertTesterPasses{ new AsTypeOfBundleTester }
}
+ it should "work with Bundles that have fields of zero width" in {
+ assertTesterPasses{ new AsTypeOfBundleZeroWidthTester }
+ }
+
it should "work with Vecs containing Bits Types" in {
assertTesterPasses{ new AsTypeOfVecTester }
}