diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/scala/chisel3/package.scala | 6 | ||||
| -rw-r--r-- | src/test/scala/chiselTests/FixedPointSpec.scala | 41 |
2 files changed, 47 insertions, 0 deletions
diff --git a/src/main/scala/chisel3/package.scala b/src/main/scala/chisel3/package.scala index d0808980..e0364868 100644 --- a/src/main/scala/chisel3/package.scala +++ b/src/main/scala/chisel3/package.scala @@ -37,6 +37,8 @@ package object chisel3 { // scalastyle:ignore package.object.name val UInt = chisel3.core.UInt type SInt = chisel3.core.SInt val SInt = chisel3.core.SInt + type FixedPoint = chisel3.core.FixedPoint + val FixedPoint = chisel3.core.FixedPoint type Bool = chisel3.core.Bool val Bool = chisel3.core.Bool val Mux = chisel3.core.Mux @@ -150,6 +152,10 @@ package object chisel3 { // scalastyle:ignore package.object.name def B: Bool = Bool(x) // scalastyle:ignore method.name } + implicit class fromDoubleToLiteral(val x: Double) extends AnyVal { + def F(binaryPoint: Int): FixedPoint = FixedPoint.fromDouble(x, binaryPoint = binaryPoint) + } + 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/FixedPointSpec.scala b/src/test/scala/chiselTests/FixedPointSpec.scala new file mode 100644 index 00000000..69015e42 --- /dev/null +++ b/src/test/scala/chiselTests/FixedPointSpec.scala @@ -0,0 +1,41 @@ +// See LICENSE for license details. + +package chiselTests + +import chisel3._ +import chisel3.testers.BasicTester +import org.scalatest._ + +//scalastyle:off magic.number +class FixedPointSpec extends FlatSpec with Matchers { + behavior of "fixed point utilities" + + they should "allow conversion between doubles and the bigints needed to represent them" in { + val initialDouble = 0.125 + val bigInt = FixedPoint.toBigInt(initialDouble, 4) + val finalDouble = FixedPoint.toDouble(bigInt, 4) + + initialDouble should be(finalDouble) + } +} + +class SBP extends Module { + val io = IO(new Bundle { + val in = Input(FixedPoint(6, 2)) + val out = Output(FixedPoint(4, 0)) + }) + io.out := io.in.setBinaryPoint(0) +} +class SBPTester extends BasicTester { + val dut = Module(new SBP) + dut.io.in := FixedPoint.fromDouble(3.75, binaryPoint = 2) + + assert(dut.io.out === FixedPoint.fromDouble(3.0, binaryPoint = 0)) + + stop() +} +class SBPSpec extends ChiselPropSpec { + property("should allow set binary point") { + assertTesterPasses { new SBPTester } + } +} |
