summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests
diff options
context:
space:
mode:
authorRichard Lin2017-02-15 14:19:36 -0800
committerGitHub2017-02-15 14:19:36 -0800
commit375e2b6a0a456c55298d82837d28986de6211ebc (patch)
tree781f4fc3d22dab0bc73a2fdec48920a20f5e7e42 /src/test/scala/chiselTests
parent41bee3f347e743e328ee520a48109cb542f3b245 (diff)
Implement asTypeOf, refactor internal APIs (#450)
Diffstat (limited to 'src/test/scala/chiselTests')
-rw-r--r--src/test/scala/chiselTests/ReinterpretCast.scala68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/test/scala/chiselTests/ReinterpretCast.scala b/src/test/scala/chiselTests/ReinterpretCast.scala
new file mode 100644
index 00000000..d4aecbe1
--- /dev/null
+++ b/src/test/scala/chiselTests/ReinterpretCast.scala
@@ -0,0 +1,68 @@
+// See LICENSE for license details.
+
+package chiselTests
+
+import org.scalatest._
+
+import chisel3._
+import chisel3.experimental.FixedPoint
+import chisel3.testers.BasicTester
+import chisel3.util._
+import chisel3.core.DataMirror
+
+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, width=4, binaryPoint=3))
+
+ 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 }
+ }
+}