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
|
package ChiselTests
import Chisel._
object Counter {
def wrapAround(n: UInt, max: UInt) =
Mux(n > max, UInt(0), n)
def apply(max: UInt, en: Bool, amt: UInt): UInt = {
val x = Reg(init=UInt(0, max.getWidth))
when (en) { x := wrapAround(x +% amt, max) }
x
}
}
class Counter extends Module {
val io = new Bundle {
val inc = Bool(INPUT)
val amt = UInt(INPUT, 4)
val tot = UInt(OUTPUT, 8)
}
io.tot := Counter(UInt(255), io.inc, io.amt)
}
class CounterTester(c: Counter) extends Tester(c) {
val maxInt = 16
var curCnt = 0
def intWrapAround(n: Int, max: Int) =
if(n > max) 0 else n
// let it spin for a bit
for (i <- 0 until 5) {
step(1)
}
for (i <- 0 until 10) {
val inc = rnd.nextBoolean()
val amt = rnd.nextInt(maxInt)
poke(c.io.inc, if (inc) 1 else 0)
poke(c.io.amt, amt)
step(1)
curCnt = if(inc) intWrapAround(curCnt + amt, 255) else curCnt
expect(c.io.tot, curCnt)
}
}
|