From 2ae50411cbc5e2cd5fdc9ca4069b9c5f64919bc4 Mon Sep 17 00:00:00 2001 From: Jim Lawson Date: Mon, 11 May 2015 13:02:03 -0700 Subject: Incorporate chisel3-tests; update Makefile. --- src/test/scala/ChiselTests/Counter.scala | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/test/scala/ChiselTests/Counter.scala (limited to 'src/test/scala/ChiselTests/Counter.scala') 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) + } +} -- cgit v1.2.3