From 5e9be183f98d32164332fa0548fe80686f50c851 Mon Sep 17 00:00:00 2001 From: Henry Cook Date: Fri, 14 Aug 2015 00:53:57 -0700 Subject: added MulLookup and Tbl tests --- src/test/scala/chiselTests/ChiselSpec.scala | 15 +++++++-- src/test/scala/chiselTests/MulLookup.scala | 38 +++++++++++++++++++++++ src/test/scala/chiselTests/Tbl.scala | 47 +++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 src/test/scala/chiselTests/MulLookup.scala create mode 100644 src/test/scala/chiselTests/Tbl.scala (limited to 'src/test/scala/chiselTests') diff --git a/src/test/scala/chiselTests/ChiselSpec.scala b/src/test/scala/chiselTests/ChiselSpec.scala index 1f1b5399..3485b398 100644 --- a/src/test/scala/chiselTests/ChiselSpec.scala +++ b/src/test/scala/chiselTests/ChiselSpec.scala @@ -40,10 +40,21 @@ class ChiselPropSpec extends PropSpec with PropertyChecks { def popCount(n: Long) = n.toBinaryString.count(_=='1') - val smallPosInts = Gen.choose(1, 8) + val smallPosInts = Gen.choose(1, 7) val safeUIntWidth = Gen.choose(1, 31) val safeUInts = Gen.choose(0, (1 << 30)) val vecSizes = Gen.choose(0, 4) - def enSequence(n: Int) = Gen.containerOfN[List,Boolean](n,Gen.oneOf(true,false)) val binaryString = for(i <- Arbitrary.arbitrary[Int]) yield "b" + i.toBinaryString + def enSequence(n: Int) = Gen.containerOfN[List,Boolean](n,Gen.oneOf(true,false)) + + def safeUIntN(n: Int) = for { + w <- smallPosInts + i <- Gen.containerOfN[List,Int](n, Gen.choose(0, (1 << w) - 1)) + } yield (w, i) + + def safeUIntPairN(n: Int) = for { + w <- smallPosInts + i <- Gen.containerOfN[List,Int](n, Gen.choose(0, (1 << w) - 1)) + j <- Gen.containerOfN[List,Int](n, Gen.choose(0, (1 << w) - 1)) + } yield (w, i zip j) } diff --git a/src/test/scala/chiselTests/MulLookup.scala b/src/test/scala/chiselTests/MulLookup.scala new file mode 100644 index 00000000..1fe77a93 --- /dev/null +++ b/src/test/scala/chiselTests/MulLookup.scala @@ -0,0 +1,38 @@ +package chiselTests + +import Chisel._ +import org.scalatest._ +import org.scalatest.prop._ +import Chisel.testers.BasicTester + +class MulLookup(val w: Int) extends Module { + val io = new Bundle { + val x = UInt(INPUT, w) + val y = UInt(INPUT, w) + val z = UInt(OUTPUT, 2 * w) + } + val tbl = Vec( + for { + i <- 0 until 1 << w + j <- 0 until 1 << w + } yield UInt(i * j, 2 * w) + ) + io.z := tbl(((io.x << w) | io.y)) +} + +class MulLookupSpec extends ChiselPropSpec { + + class MulLookupTester(w: Int, x: Int, y: Int) extends BasicTester { + val dut = Module(new MulLookup(w)) + dut.io.x := UInt(x) + dut.io.y := UInt(y) + io.done := Bool(true) + io.error := dut.io.z != UInt(x * y) + } + + property("Mul lookup table should return the correct result") { + forAll(smallPosInts, smallPosInts) { (x: Int, y: Int) => + assert(execute{ new MulLookupTester(3, x, y) }) + } + } +} diff --git a/src/test/scala/chiselTests/Tbl.scala b/src/test/scala/chiselTests/Tbl.scala new file mode 100644 index 00000000..4add985e --- /dev/null +++ b/src/test/scala/chiselTests/Tbl.scala @@ -0,0 +1,47 @@ +package chiselTests + +import Chisel._ +import org.scalatest._ +import org.scalatest.prop._ +import Chisel.testers.BasicTester + +class Tbl(w: Int, n: Int) extends Module { + val io = new Bundle { + val wi = UInt(INPUT, log2Ceil(w)) + val ri = UInt(INPUT, log2Ceil(w)) + val we = Bool(INPUT) + val d = UInt(INPUT, w) + val o = UInt(OUTPUT, w) + } + val m = Mem(UInt(width = w), n) + io.o := m(io.ri) + when (io.we) { + m(io.wi) := io.d + when(io.ri === io.wi) { io.o := io.d } + } +} + +class TblSpec extends ChiselPropSpec { + + class TblTester(w: Int, n: Int, idxs: List[Int], values: List[Int]) extends BasicTester { + val (cnt, wrap) = Counter(Bool(true), idxs.size) + val dut = Module(new Tbl(w, n)) + val vvalues = Vec(values.map(UInt(_))) + val vidxs = Vec(idxs.map(UInt(_))) + val prev_idx = vidxs(cnt - UInt(1)) + val prev_value = vvalues(cnt - UInt(1)) + dut.io.wi := vidxs(cnt) + dut.io.ri := prev_idx + dut.io.we := Bool(true) //TODO enSequence + dut.io.d := vvalues(cnt) + when(cnt > UInt(0) && dut.io.o != prev_value) { io.done := Bool(true); io.error := prev_idx } + when(wrap) { io.done := Bool(true) } + } + + property("All table reads should return the previous write") { + forAll(safeUIntPairN(8)) { case(w: Int, pairs: List[(Int, Int)]) => + val (idxs, values) = pairs.unzip + assert(execute{ new TblTester(w, 1 << w, idxs, values) }) + } + } +} -- cgit v1.2.3