-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemorySearch.scala
44 lines (41 loc) · 1.15 KB
/
MemorySearch.scala
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
// See LICENSE.txt for license details.
package TutorialExamples
import Chisel._
class MemorySearch extends Module {
val io = new Bundle {
val target = UInt(INPUT, 4)
val en = Bool(INPUT)
val done = Bool(OUTPUT)
val address = UInt(OUTPUT, 3)
}
val index = Reg(init = UInt(0, width = 3))
val elts = Vec(UInt(0), UInt(4), UInt(15), UInt(14),
UInt(2), UInt(5), UInt(13))
val elt = elts(index)
val over = !io.en && ((elt === io.target) || (index === UInt(7)))
when (io.en) {
index := UInt(0)
} .elsewhen (!over) {
index := index + UInt(1)
}
io.done := over
io.address := index
}
class MemorySearchTests(c: MemorySearch) extends Tester(c) {
val list = c.elts.map(int(_))
val n = 8
val maxT = n * (list.length + 3)
for (k <- 0 until n) {
val target = rnd.nextInt(16)
poke(c.io.en, 1)
poke(c.io.target, target)
step(1)
poke(c.io.en, 0)
do {
step(1)
} while (peek(c.io.done) == 0 && t < maxT)
val addr = peek(c.io.address).toInt
expect(addr == list.length || list(addr) == target,
"LOOKING FOR " + target + " FOUND " + addr)
}
}