diff options
| author | Henry Cook | 2015-08-13 23:32:14 -0700 |
|---|---|---|
| committer | Henry Cook | 2015-08-13 23:32:14 -0700 |
| commit | caf993c89ba07a65d05b0e17d109a60f96330136 (patch) | |
| tree | 9decc9c40d6b39aecf38c6fcbb9a09dabb0c21b7 /src/test/scala/chiselTests/Decoder.scala | |
| parent | 31c5c92fa59cbd9aec00851fd722b9a6317179ef (diff) | |
add decoder test
Diffstat (limited to 'src/test/scala/chiselTests/Decoder.scala')
| -rw-r--r-- | src/test/scala/chiselTests/Decoder.scala | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/test/scala/chiselTests/Decoder.scala b/src/test/scala/chiselTests/Decoder.scala new file mode 100644 index 00000000..cc463600 --- /dev/null +++ b/src/test/scala/chiselTests/Decoder.scala @@ -0,0 +1,41 @@ +package chiselTests +import Chisel._ +import org.scalatest._ +import org.scalatest.prop._ +import org.scalacheck._ +import Chisel.testers.BasicTester + +class Decoder(bitpats: List[String]) extends Module { + val io = new Bundle { + val inst = UInt(INPUT, 32) + val match_idx = UInt(OUTPUT, 5) + } + io.match_idx := Vec(bitpats.map(BitPat(_) === io.inst)).indexWhere{i: Bool => i} +} + +class DecoderSpec extends ChiselPropSpec { + + class DecoderTester(pairs: List[(String, String)]) extends BasicTester { + val (insts, bitpats) = pairs.unzip + val (cnt, wrap) = Counter(Bool(true), pairs.size) + val dut = Module(new Decoder(bitpats)) + dut.io.inst := Vec(insts.map(UInt(_)))(cnt) + when(dut.io.match_idx != cnt) { io.done := Bool(true); io.error := cnt } + when(wrap) { io.done := Bool(true) } + } + + // Use a single Int to make both a specific instruction and a BitPat that will match it + val bitpatPair = for(seed <- Arbitrary.arbitrary[Int]) yield { + val rnd = new scala.util.Random(seed) + val bs = seed.toBinaryString + val bp = bs.map(if(rnd.nextBoolean) _ else "?").mkString + ("b"+bs, "b"+bp) + } + def nPairs(n: Int) = Gen.containerOfN[List, (String,String)](n,bitpatPair) + + property("BitPat wildcards should be usable in decoding") { + forAll(nPairs(16)){ (pairs: List[(String, String)]) => + assert(execute{ new DecoderTester(pairs) }) + } + } +} |
