diff options
| author | chick | 2016-11-09 16:23:52 -0800 |
|---|---|---|
| committer | ducky | 2016-11-21 12:48:10 -0800 |
| commit | 22406a589c4a3f8de42a9f5c988201f474c11282 (patch) | |
| tree | 35f8bdcfe288ed5b9fd051172afe342f3686f1e9 | |
| parent | d46b9acd557d2fe6ffe27f43ee72cd9b2a22f65d (diff) | |
simple test that range interpolator works with UInt factory method
| -rw-r--r-- | chiselFrontend/src/main/scala/chisel3/core/Bits.scala | 10 | ||||
| -rw-r--r-- | src/main/scala/chisel3/package.scala | 7 | ||||
| -rw-r--r-- | src/test/scala/chiselTests/RangeMacroTest.scala | 9 | ||||
| -rw-r--r-- | src/test/scala/chiselTests/RangeSpec.scala | 17 |
4 files changed, 33 insertions, 10 deletions
diff --git a/chiselFrontend/src/main/scala/chisel3/core/Bits.scala b/chiselFrontend/src/main/scala/chisel3/core/Bits.scala index 83733089..82b60a4c 100644 --- a/chiselFrontend/src/main/scala/chisel3/core/Bits.scala +++ b/chiselFrontend/src/main/scala/chisel3/core/Bits.scala @@ -563,10 +563,14 @@ private[core] sealed trait UIntFactory { result.binding = LitBinding() result } - /** Create a UInt with the specified range */ + /** Create a UInt with the specified range */ def apply(range: Range): UInt = { width(range.getWidth) } + /** Create a UInt with the specified range */ + def apply(range: (NumericBound[Int], NumericBound[Int])): UInt = { + apply(KnownUIntRange(range._1, range._2)) + } /** Create a UInt with a specified width - compatibility with Chisel2. */ // NOTE: This resolves UInt(width = 32) @@ -736,6 +740,10 @@ object SInt { def apply(range: Range): SInt = { width(range.getWidth) } + /** Create a SInt with the specified range */ + def apply(range: (NumericBound[Int], NumericBound[Int])): SInt = { + apply(KnownSIntRange(range._1, range._2)) + } def Lit(value: BigInt): SInt = Lit(value, Width()) def Lit(value: BigInt, width: Int): SInt = Lit(value, Width(width)) diff --git a/src/main/scala/chisel3/package.scala b/src/main/scala/chisel3/package.scala index 3cdda971..436534e1 100644 --- a/src/main/scala/chisel3/package.scala +++ b/src/main/scala/chisel3/package.scala @@ -1,3 +1,4 @@ + // See LICENSE for license details. package object chisel3 { // scalastyle:ignore package.object.name @@ -11,6 +12,8 @@ package object chisel3 { // scalastyle:ignore package.object.name import chisel3.util._ import chisel3.internal.firrtl.Port + import chisel3.internal.firrtl.NumericBound + type Direction = chisel3.core.Direction val Input = chisel3.core.Input val Output = chisel3.core.Output @@ -156,6 +159,10 @@ package object chisel3 { // scalastyle:ignore package.object.name def F(binaryPoint: Int): FixedPoint = FixedPoint.fromDouble(x, binaryPoint = binaryPoint) } + implicit class ChiselRange(val sc: StringContext) extends AnyVal { + def range(args: Any*): (NumericBound[Int], NumericBound[Int]) = macro chisel3.internal.RangeTransform.apply + } + implicit class fromUIntToBitPatComparable(val x: UInt) extends AnyVal { final def === (that: BitPat): Bool = macro SourceInfoTransform.thatArg @deprecated("Use '=/=', which avoids potential precedence problems", "chisel3") diff --git a/src/test/scala/chiselTests/RangeMacroTest.scala b/src/test/scala/chiselTests/RangeMacroTest.scala index 88a0730f..cafff1d2 100644 --- a/src/test/scala/chiselTests/RangeMacroTest.scala +++ b/src/test/scala/chiselTests/RangeMacroTest.scala @@ -8,15 +8,6 @@ import org.scalatest._ import org.scalatest.prop._ import chisel3.testers.BasicTester -package object rangeMacroTest { - -implicit class ChiselRange(val sc: StringContext) extends AnyVal { - def range(args: Any*): Unit = macro chisel3.internal.RangeTransform.apply -} - -} - -import rangeMacroTest._ /** Comprehensive test of static range parsing functionality. * Note: negative (failure) conditions can't be tested because they will fail at compile time, diff --git a/src/test/scala/chiselTests/RangeSpec.scala b/src/test/scala/chiselTests/RangeSpec.scala new file mode 100644 index 00000000..edaff8aa --- /dev/null +++ b/src/test/scala/chiselTests/RangeSpec.scala @@ -0,0 +1,17 @@ +// See LICENSE for license details. + +package chiselTests + +import chisel3._ +import org.scalatest.{Matchers, FreeSpec} + +class RangeSpec extends FreeSpec with Matchers { + "Ranges can be specified for UInt, SInt, and FixedPoint" - { + "to specify a UInt" in { + val x = UInt(range"[0, 7)") + x.getWidth should be (3) + + println(range"[4,32)") + } + } +} |
