-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.scala
69 lines (58 loc) · 1.58 KB
/
Stack.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// See LICENSE.txt for license details.
package TutorialExamples
import Chisel._
import scala.collection.mutable.HashMap
import scala.collection.mutable.{Stack => ScalaStack}
import scala.util.Random
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(depth, UInt(width = 32))
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 StackTests(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)
}
}