summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorgrebe2017-01-13 09:51:18 -0800
committerChick Markley2017-01-13 09:51:18 -0800
commitf19653fbe9d2e1b49c49c31ddb98a758c390ba94 (patch)
treecd295142ab5a0040c4beb5606fc1c2cdc2901e28 /src
parent3215df07a97b58babb1deb3fab0928198b1daad2 (diff)
Make fromBits work with types other than UInt (#424)
* Make fromBits work with types other than UInt * Oops, left in a println * Add test for truncation/expansion * Fix stuff that broke when FixedPoint fromBits PR was merged. * Use .BP shorthand added in previous PR
Diffstat (limited to 'src')
-rw-r--r--src/test/scala/chiselTests/FixedPointSpec.scala4
-rw-r--r--src/test/scala/chiselTests/FromBitsTester.scala67
2 files changed, 71 insertions, 0 deletions
diff --git a/src/test/scala/chiselTests/FixedPointSpec.scala b/src/test/scala/chiselTests/FixedPointSpec.scala
index a08de073..4e6af33b 100644
--- a/src/test/scala/chiselTests/FixedPointSpec.scala
+++ b/src/test/scala/chiselTests/FixedPointSpec.scala
@@ -22,15 +22,19 @@ class FixedPointLiteralSpec extends FlatSpec with Matchers {
class FixedPointFromBitsTester extends BasicTester {
val uint = 3.U(4.W)
val sint = -3.S
+ val fp = FixedPoint.fromDouble(3.0, width = 4, binaryPoint = 0)
val fp_tpe = FixedPoint(4.W, 1.BP)
val uint_result = FixedPoint.fromDouble(1.5, width = 4, binaryPoint = 1)
val sint_result = FixedPoint.fromDouble(-1.5, width = 4, binaryPoint = 1)
+ val fp_result = FixedPoint.fromDouble(1.5, width = 4, binaryPoint = 1)
val uint2fp = fp_tpe.fromBits(uint)
val sint2fp = fp_tpe.fromBits(sint)
+ val fp2fp = fp_tpe.fromBits(fp)
assert(uint2fp === uint_result)
assert(sint2fp === sint_result)
+ assert(fp2fp === fp_result)
stop()
}
diff --git a/src/test/scala/chiselTests/FromBitsTester.scala b/src/test/scala/chiselTests/FromBitsTester.scala
new file mode 100644
index 00000000..5809b386
--- /dev/null
+++ b/src/test/scala/chiselTests/FromBitsTester.scala
@@ -0,0 +1,67 @@
+// See LICENSE for license details.
+
+package chiselTests
+
+import org.scalatest._
+
+import chisel3._
+import chisel3.testers.BasicTester
+import chisel3.util._
+import chisel3.core.DataMirror
+
+class FromBitsBundleTester extends BasicTester {
+ class MultiTypeBundle extends Bundle {
+ val u = UInt(4.W)
+ val s = SInt(4.W)
+ val fp = FixedPoint(4.W, 3.BP)
+ }
+
+ val bun = new MultiTypeBundle
+
+ val bunFromBits = bun.fromBits( ((4 << 8) + (15 << 4) + (12 << 0)).U )
+
+ assert(bunFromBits.u === 4.U)
+ assert(bunFromBits.s === -1.S)
+ assert(bunFromBits.fp === FixedPoint.fromDouble(-0.5, width=4, binaryPoint=3))
+
+ stop()
+}
+
+class FromBitsVecTester extends BasicTester {
+ val vec = Vec(4, SInt(4.W)).fromBits( ((15 << 12) + (0 << 8) + (1 << 4) + (2 << 0)).U )
+
+ assert(vec(0) === 2.S)
+ assert(vec(1) === 1.S)
+ assert(vec(2) === 0.S)
+ assert(vec(3) === -1.S)
+
+ stop()
+}
+
+class FromBitsTruncationTester extends BasicTester {
+ val truncate = UInt(3.W).fromBits( (64 + 3).U )
+ val expand = UInt(3.W).fromBits( 1.U )
+
+ assert( DataMirror.widthOf(truncate).get == 3 )
+ assert( truncate === 3.U )
+ assert( DataMirror.widthOf(expand).get == 3 )
+ assert( expand === 1.U )
+
+ stop()
+}
+
+class FromBitsSpec extends ChiselFlatSpec {
+ behavior of "fromBits"
+
+ it should "work with Bundles containing Bits Types" in {
+ assertTesterPasses{ new FromBitsBundleTester }
+ }
+
+ it should "work with Vecs containing Bits Types" in {
+ assertTesterPasses{ new FromBitsVecTester }
+ }
+
+ it should "expand and truncate UInts of different width" in {
+ assertTesterPasses{ new FromBitsTruncationTester }
+ }
+}