diff options
| -rw-r--r-- | chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala | 6 | ||||
| -rw-r--r-- | src/test/scala/chiselTests/AsTypeOfTester.scala | 47 | ||||
| -rw-r--r-- | src/test/scala/chiselTests/ReinterpretCast.scala | 67 |
3 files changed, 49 insertions, 71 deletions
diff --git a/chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala b/chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala index c359a119..1689e6ad 100644 --- a/chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala +++ b/chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala @@ -102,10 +102,10 @@ abstract class EnumType(private val factory: EnumFactory, selfAnnotating: Boolea this.factory == that.asInstanceOf[EnumType].factory } - // This isn't actually used anywhere (and it would throw an exception anyway). But it has to be defined since we - // inherit it from Data. private[core] override def connectFromBits(that: Bits)(implicit sourceInfo: SourceInfo, - compileOptions: CompileOptions): Unit = ??? + compileOptions: CompileOptions): Unit = { + this := factory.apply(that.asUInt) + } final def === (that: EnumType): Bool = macro SourceInfoTransform.thatArg final def =/= (that: EnumType): Bool = macro SourceInfoTransform.thatArg diff --git a/src/test/scala/chiselTests/AsTypeOfTester.scala b/src/test/scala/chiselTests/AsTypeOfTester.scala index 3fe186b4..c78ddb17 100644 --- a/src/test/scala/chiselTests/AsTypeOfTester.scala +++ b/src/test/scala/chiselTests/AsTypeOfTester.scala @@ -5,7 +5,7 @@ package chiselTests import org.scalatest._ import chisel3._ -import chisel3.experimental.{DataMirror, FixedPoint} +import chisel3.experimental.{DataMirror, FixedPoint, ChiselEnum} import chisel3.testers.BasicTester import chisel3.util._ @@ -55,6 +55,47 @@ class ResetAsTypeOfBoolTester extends BasicTester { stop() } +class AsChiselEnumTester extends BasicTester { + object MyEnum extends ChiselEnum { + val foo, bar = Value + val fizz = Value(2.U) + } + class MyBundle extends Bundle { + val a = Bool() + val b = Bool() + } + + // To + assert(2.U.asTypeOf(MyEnum()) === MyEnum.fizz) + assert(VecInit(2.U.asBools).asTypeOf(MyEnum()) === MyEnum.fizz) + assert(2.U.asTypeOf(new MyBundle).asTypeOf(MyEnum()) === MyEnum.fizz) + + // From + assert(MyEnum.foo.asUInt === 0.U) + val vec = MyEnum.bar.asTypeOf(Vec(2, Bool())) + assert(vec(0) === 1.U) + assert(vec(1) === 0.U) + val bun = MyEnum.fizz.asTypeOf(new MyBundle) + assert(bun.b === 0.U) + assert(bun.a === 1.U) + + // In aggregate + class OtherBundle extends Bundle { + val enum = MyEnum() + val foo = Bool() + } + val wire = Wire(new OtherBundle) + wire.enum := MyEnum.fizz + wire.foo := true.B + + assert(wire.asUInt === 5.U) + val other = 5.U.asTypeOf(new OtherBundle) + assert(other.enum === MyEnum.fizz) + assert(other.foo === true.B) + + stop() +} + class AsTypeOfSpec extends ChiselFlatSpec { behavior of "asTypeOf" @@ -73,4 +114,8 @@ class AsTypeOfSpec extends ChiselFlatSpec { it should "work for casting implicit Reset to Bool" in { assertTesterPasses{ new ResetAsTypeOfBoolTester } } + + it should "work for casting to and from ChiselEnums" in { + assertTesterPasses(new AsChiselEnumTester) + } } diff --git a/src/test/scala/chiselTests/ReinterpretCast.scala b/src/test/scala/chiselTests/ReinterpretCast.scala deleted file mode 100644 index 61c351ab..00000000 --- a/src/test/scala/chiselTests/ReinterpretCast.scala +++ /dev/null @@ -1,67 +0,0 @@ -// See LICENSE for license details. - -package chiselTests - -import org.scalatest._ - -import chisel3._ -import chisel3.experimental.{DataMirror, FixedPoint} -import chisel3.testers.BasicTester -import chisel3.util._ - -class AsBundleTester extends BasicTester { - class MultiTypeBundle extends Bundle { - val u = UInt(4.W) - val s = SInt(4.W) - val fp = FixedPoint(4.W, 3.BP) - } - - val rawData = ((4 << 8) + (15 << 4) + (12 << 0)).U - val bunFromBits = rawData.asTypeOf(new MultiTypeBundle) - - assert(bunFromBits.u === 4.U) - assert(bunFromBits.s === -1.S) - assert(bunFromBits.fp === FixedPoint.fromDouble(-0.5, 4.W, 3.BP)) - - stop() -} - -class AsVecTester extends BasicTester { - val rawData = ((15 << 12) + (0 << 8) + (1 << 4) + (2 << 0)).U - val vec = rawData.asTypeOf(Vec(4, SInt(4.W))) - - assert(vec(0) === 2.S) - assert(vec(1) === 1.S) - assert(vec(2) === 0.S) - assert(vec(3) === -1.S) - - stop() -} - -class AsBitsTruncationTester extends BasicTester { - val truncate = (64 + 3).U.asTypeOf(UInt(3.W)) - val expand = 1.U.asTypeOf(UInt(3.W)) - - assert( DataMirror.widthOf(truncate).get == 3 ) - assert( truncate === 3.U ) - assert( DataMirror.widthOf(expand).get == 3 ) - assert( expand === 1.U ) - - stop() -} - -class AsBitsSpec extends ChiselFlatSpec { - behavior of "fromBits" - - it should "work with Bundles containing Bits Types" in { - assertTesterPasses{ new AsBundleTester } - } - - it should "work with Vecs containing Bits Types" in { - assertTesterPasses{ new AsVecTester } - } - - it should "expand and truncate UInts of different width" in { - assertTesterPasses{ new AsBitsTruncationTester } - } -} |
