blob: ee892fc534a0672f05b089c6f8138e28d7b0232c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
// See LICENSE for license details.
package chiselTests
import org.scalatest._
import org.scalatest.prop._
import org.scalacheck._
import chisel3._
import chisel3.testers.BasicTester
import chisel3.util._
class Decoder(bitpats: List[String]) extends Module {
val io = IO(new Bundle {
val inst = Input(UInt.width(32))
val matched = Output(Bool())
})
io.matched := Vec(bitpats.map(BitPat(_) === io.inst)).reduce(_||_)
}
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.matched) {
assert(cnt === UInt.Lit(0))
stop()
}
when(wrap) {
stop()
}
}
class DecoderSpec extends ChiselPropSpec {
// 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)
}
private def nPairs(n: Int) = Gen.containerOfN[List, (String,String)](n,bitpatPair)
property("BitPat wildcards should be usable in decoding") {
forAll(nPairs(4)){ (pairs: List[(String, String)]) =>
assertTesterPasses{ new DecoderTester(pairs) }
}
}
}
|