-
Notifications
You must be signed in to change notification settings - Fork 70
/
DecoupledDriver.scala
107 lines (93 loc) · 2.15 KB
/
DecoupledDriver.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// SPDX-License-Identifier: Apache-2.0
package chiseltest
import chisel3._
import chisel3.util._
// implicit class, cannot maintain state
class DecoupledDriver[T <: Data](x: ReadyValidIO[T]) {
// Source (enqueue) functions
//
def initSource(): this.type = {
x.valid.poke(false.B)
this
}
def enqueueNow(data: T): Unit = {
x.bits.poke(data)
x.valid.poke(true)
fork
.withRegion(Monitor) {
x.ready.expect(true.B)
}
.joinAndStep()
x.valid.poke(false)
}
def enqueue(data: T): Unit = {
x.bits.poke(data)
x.valid.poke(true)
fork
.withRegion(Monitor) {
while (!x.ready.peekBoolean()) {
step(1)
}
}
.joinAndStep()
x.valid.poke(false)
}
def enqueueSeq(data: Seq[T]): Unit = {
for (elt <- data) {
enqueue(elt)
}
}
// Sink (dequeue) functions
//
def initSink(): this.type = {
x.ready.poke(false.B)
this
}
// NOTE: this doesn't happen in the Monitor phase, unlike public functions
def waitForValid(): Unit = {
while (!x.valid.peekBoolean()) {
step(1)
}
}
def expectDequeue(data: T): Unit = {
x.ready.poke(true)
fork
.withRegion(Monitor) {
waitForValid()
x.valid.expect(true.B)
x.bits.expect(data)
}
.joinAndStep()
x.ready.poke(false)
}
def expectDequeueNow(data: T): Unit = {
x.ready.poke(true)
fork
.withRegion(Monitor) {
x.valid.expect(true.B)
x.bits.expect(data)
}
.joinAndStep()
x.ready.poke(false)
}
def expectDequeueSeq(data: Seq[T]): Unit = {
for (elt <- data) {
expectDequeue(elt)
}
}
def expectPeek(data: T): Unit = {
fork.withRegion(Monitor) {
x.valid.expect(true.B)
x.bits.expect(data)
}
}
def expectInvalid(): Unit = {
fork.withRegion(Monitor) {
x.valid.expect(false.B)
}
}
@deprecated("You no longer need to set the clock explicitly.", since = "6.0.x")
def setSourceClock(clock: Clock): this.type = this
@deprecated("You no longer need to set the clock explicitly.", since = "6.0.x")
def setSinkClock(clock: Clock): this.type = this
}