-
Notifications
You must be signed in to change notification settings - Fork 649
Expand file tree
/
Copy pathDecoder.scala
More file actions
60 lines (51 loc) · 1.79 KB
/
Decoder.scala
File metadata and controls
60 lines (51 loc) · 1.79 KB
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
52
53
54
55
56
57
58
59
60
// SPDX-License-Identifier: Apache-2.0
package chiselTests
import chisel3._
import chisel3.simulator.scalatest.ChiselSim
import chisel3.simulator.stimulus.RunUntilFinished
import chisel3.util.{BitPat, Counter}
import org.scalacheck.{Arbitrary, Gen}
import org.scalatest.propspec.AnyPropSpec
class Decoder(bitpats: List[String]) extends Module {
val io = IO(new Bundle {
val inst = Input(UInt(32.W))
val matched = Output(Bool())
})
io.matched := VecInit(bitpats.map(BitPat(_) === io.inst).reduce(_ || _))
}
class DecoderTester(pairs: List[(String, String)]) extends Module {
val (insts, bitpats) = pairs.unzip
val (cnt, wrap) = Counter(true.B, pairs.size)
val dut = Module(new Decoder(bitpats))
dut.io.inst := VecInit(insts.map(_.asUInt))(cnt)
when(!dut.io.matched) {
assert(cnt === 0.U)
stop()
}
when(wrap) {
stop()
}
}
class DecoderSpec extends AnyPropSpec with PropertyUtils with ChiselSim {
// 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 "?")
// The following randomly throws in white space and underscores which are legal and ignored.
val bpp = bp.map { a =>
if (rnd.nextBoolean()) {
a.toString
} else {
a.toString + (if (rnd.nextBoolean()) "_" else " ")
}
}.mkString
("b" + bs, "b" + bpp)
}
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)]) =>
simulate { new DecoderTester(pairs) }(RunUntilFinished(pairs.size + 1))
}
}
}