diff options
| author | Jack Koenig | 2017-08-15 14:44:59 -0700 |
|---|---|---|
| committer | Jack Koenig | 2017-08-15 18:59:25 -0700 |
| commit | 90e775b1228765ce7f345716fa215f72b97816a9 (patch) | |
| tree | 73be4079fb5820cbae7ae7b788f43e1e2537f2c4 /src | |
| parent | c87145bc61a729bb035428d527c5787c174c5256 (diff) | |
Make .dir give correct direction for Module io in compatibility
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/scala/chisel3/compatibility.scala | 23 | ||||
| -rw-r--r-- | src/test/scala/chiselTests/CompatibilitySpec.scala | 54 |
2 files changed, 50 insertions, 27 deletions
diff --git a/src/main/scala/chisel3/compatibility.scala b/src/main/scala/chisel3/compatibility.scala index 33e8e75c..e169fb8f 100644 --- a/src/main/scala/chisel3/compatibility.scala +++ b/src/main/scala/chisel3/compatibility.scala @@ -29,26 +29,17 @@ package object Chisel { // scalastyle:ignore package.object.name } implicit class AddDirMethodToData[T<:Data](val target: T) extends AnyVal { - import chisel3.core.{DataMirror, ActualDirection, SpecifiedDirection} + import chisel3.core.{DataMirror, ActualDirection, requireIsHardware} def dir: Direction = { - DataMirror.isSynthesizable(target) match { - case true => target match { - case e: Element => DataMirror.directionOf(e) match { - case ActualDirection.Unspecified => NODIR - case ActualDirection.Output => OUTPUT - case ActualDirection.Input => INPUT - case dir => throw new RuntimeException(s"Unexpected element direction '$dir'") - } + requireIsHardware(target) // This has the side effect of calling _autoWrapPorts + target match { + case e: Element => DataMirror.directionOf(e) match { + case ActualDirection.Output => OUTPUT + case ActualDirection.Input => INPUT case _ => NODIR } - case false => DataMirror.specifiedDirectionOf(target) match { // returns local direction only - case SpecifiedDirection.Unspecified => NODIR - case SpecifiedDirection.Input => INPUT - case SpecifiedDirection.Output => OUTPUT - case dir => throw new RuntimeException(s"Unexpected element direction '$dir'") - } + case _ => NODIR } - } } diff --git a/src/test/scala/chiselTests/CompatibilitySpec.scala b/src/test/scala/chiselTests/CompatibilitySpec.scala index abbc040a..52a93aed 100644 --- a/src/test/scala/chiselTests/CompatibilitySpec.scala +++ b/src/test/scala/chiselTests/CompatibilitySpec.scala @@ -11,17 +11,26 @@ class CompatibiltySpec extends ChiselFlatSpec with GeneratorDrivenPropertyChecks behavior of "Chisel compatibility layer" it should "accept direction arguments" in { - val directionArgument: Direction = Gen.oneOf(INPUT, OUTPUT, NODIR).sample.get - val b = Bool(directionArgument) - b shouldBe a [Bool] - b.getWidth shouldEqual 1 - b.dir shouldEqual(directionArgument) - // Choose a random width - val width = Gen.choose(1, 2048).sample.get - val u = UInt(directionArgument, width) - u shouldBe a [UInt] - u.getWidth shouldEqual width - u.dir shouldEqual(directionArgument) + elaborate(new Module { + // Choose a random direction + val directionArgument: Direction = Gen.oneOf(INPUT, OUTPUT, NODIR).sample.get + val expectedDirection = directionArgument match { + case NODIR => OUTPUT + case other => other + } + // Choose a random width + val width = Gen.choose(1, 2048).sample.get + val io = new Bundle { + val b = Bool(directionArgument) + val u = UInt(directionArgument, width) + } + io.b shouldBe a [Bool] + io.b.getWidth shouldEqual 1 + io.b.dir shouldEqual (expectedDirection) + io.u shouldBe a [UInt] + io.u.getWidth shouldEqual width + io.u.dir shouldEqual (expectedDirection) + }) } it should "accept single argument U/SInt factory methods" in { @@ -260,4 +269,27 @@ class CompatibiltySpec extends ChiselFlatSpec with GeneratorDrivenPropertyChecks io.out := RegEnable(io.in(0), true.B) }) } + + "Data.dir" should "give the correct direction for io" in { + import Chisel._ + elaborate(new Module { + val io = (new Bundle { + val foo = Bool(OUTPUT) + val bar = Bool().flip + }).flip + Chisel.assert(io.foo.dir == INPUT) + Chisel.assert(io.bar.dir == OUTPUT) + }) + } + + // Note: This is a regression (see https://github.com/freechipsproject/chisel3/issues/668) + it should "fail for Chisel types" in { + import Chisel._ + an [chisel3.core.Binding.ExpectedHardwareException] should be thrownBy { + elaborate(new Module { + val io = new Bundle { } + UInt(INPUT).dir + }) + } + } } |
