summaryrefslogtreecommitdiff
path: root/src/test/scala/ChiselTests/Counter.scala
diff options
context:
space:
mode:
authorJim Lawson2015-05-11 13:02:03 -0700
committerJim Lawson2015-07-24 15:50:53 -0700
commit2ae50411cbc5e2cd5fdc9ca4069b9c5f64919bc4 (patch)
treea656e44d86a68a7c53b159fe6c74d328a126126d /src/test/scala/ChiselTests/Counter.scala
parentb208bfb5691c7b5921dd47d0b599726872acd1cd (diff)
Incorporate chisel3-tests; update Makefile.
Diffstat (limited to 'src/test/scala/ChiselTests/Counter.scala')
-rw-r--r--src/test/scala/ChiselTests/Counter.scala44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/test/scala/ChiselTests/Counter.scala b/src/test/scala/ChiselTests/Counter.scala
new file mode 100644
index 00000000..cab61c53
--- /dev/null
+++ b/src/test/scala/ChiselTests/Counter.scala
@@ -0,0 +1,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)
+ }
+}