diff options
| author | Jim Lawson | 2015-05-11 13:02:03 -0700 |
|---|---|---|
| committer | Jim Lawson | 2015-07-24 15:50:53 -0700 |
| commit | 2ae50411cbc5e2cd5fdc9ca4069b9c5f64919bc4 (patch) | |
| tree | a656e44d86a68a7c53b159fe6c74d328a126126d /src/test/scala/ChiselTests/Stack.scala | |
| parent | b208bfb5691c7b5921dd47d0b599726872acd1cd (diff) | |
Incorporate chisel3-tests; update Makefile.
Diffstat (limited to 'src/test/scala/ChiselTests/Stack.scala')
| -rw-r--r-- | src/test/scala/ChiselTests/Stack.scala | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/test/scala/ChiselTests/Stack.scala b/src/test/scala/ChiselTests/Stack.scala new file mode 100644 index 00000000..afcbc8bc --- /dev/null +++ b/src/test/scala/ChiselTests/Stack.scala @@ -0,0 +1,62 @@ +package ChiselTests +import scala.collection.mutable.{Stack => ScalaStack} +import Chisel._ + +class Stack(val depth: Int) extends Module { + val io = new Bundle { + val push = Bool(INPUT) + val pop = Bool(INPUT) + val en = Bool(INPUT) + val dataIn = UInt(INPUT, 32) + val dataOut = UInt(OUTPUT, 32) + } + + val stack_mem = Mem(UInt(width = 32), depth) + val sp = Reg(init = UInt(0, width = log2Up(depth+1))) + val out = Reg(init = UInt(0, width = 32)) + + when (io.en) { + when(io.push && (sp < UInt(depth))) { + stack_mem(sp) := io.dataIn + sp := sp +% UInt(1) + } .elsewhen(io.pop && (sp > UInt(0))) { + sp := sp -% UInt(1) + } + when (sp > UInt(0)) { + out := stack_mem(sp -% UInt(1)) + } + } + io.dataOut := out +} + +class StackTester(c: Stack) extends Tester(c) { + var nxtDataOut = 0 + var dataOut = 0 + val stack = new ScalaStack[Int]() + + for (t <- 0 until 16) { + val enable = rnd.nextInt(2) + val push = rnd.nextInt(2) + val pop = rnd.nextInt(2) + val dataIn = rnd.nextInt(256) + + if (enable == 1) { + dataOut = nxtDataOut + if (push == 1 && stack.length < c.depth) { + stack.push(dataIn) + } else if (pop == 1 && stack.length > 0) { + stack.pop() + } + if (stack.length > 0) { + nxtDataOut = stack.top + } + } + + poke(c.io.pop, pop) + poke(c.io.push, push) + poke(c.io.en, enable) + poke(c.io.dataIn, dataIn) + step(1) + expect(c.io.dataOut, dataOut) + } +} |
