forked from freechipsproject/chisel-testers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AdvTester.scala
236 lines (204 loc) · 7.7 KB
/
AdvTester.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// SPDX-License-Identifier: Apache-2.0
package chisel3.iotesters
/* Written by Stephen Twigg, Eric Love */
import chisel3._
import chisel3.util._
import scala.collection.mutable.ArrayBuffer
import java.io.{PrintWriter, StringWriter}
// Provides a template to define advanced tester transactions
trait AdvTests extends PeekPokeTests {
def cycles: Long
def wire_poke[T <: Element: Pokeable](port: T, target: BigInt): Unit
def reg_poke[T <: Element: Pokeable](port: T, target: BigInt): Unit
def takestep(work: => Unit = {}): Unit
def takesteps(n: Int)(work: =>Unit = {}): Unit
def until(pred: =>Boolean, maxCycles: Long = 0L)(work: =>Unit): Boolean
def eventually(pred: =>Boolean, maxCycles: Long = 0L): Boolean
def do_until(work: =>Unit)(pred: =>Boolean, maxCycles: Long = 0L): Boolean
}
abstract class AdvTester[+T <: Module](dut: T,
base: Int = 16,
logFile: Option[java.io.File] = chiselMain.context.logFile)
extends PeekPokeTester(dut, base, logFile) {
val defaultMaxCycles = 1024L
var _cycles = 0L
def cycles = _cycles
override def incTime(n: Int) {
_cycles += n
super.incTime(n)
}
// List of scala objects that need to be processed along with the test benches, like sinks and sources
val preprocessors = new ArrayBuffer[Processable]()
val postprocessors = new ArrayBuffer[Processable]()
// pre v post refers to when user-customized update code ('work') is processed
// e.g. sinks are in the preprocessing list and sources in the postprocessing list
// this allows the testbench to respond to a request within one cycle
// This section of code lets testers easily emulate have registers right before dut inputs
// This testing style conforms with the general ASPIRE testbench style
// Also, to ensure difference enforced, poke 'deprecated' and replaced with wire_poke
def wire_poke[T <: Element: Pokeable](port: T, target: BigInt) = super.poke(port, target)
override def poke[T <: Element: Pokeable](port: T, target: BigInt) {
require(false, "poke hidden for AdvTester, use wire_poke or reg_poke")
}
private val registered_bits_updates = new scala.collection.mutable.HashMap[Element,BigInt]()
private def do_registered_updates() = {
registered_bits_updates.foreach{case (key, value) => key match {
case Pokeable(p) => wire_poke(p, value)
}}
registered_bits_updates.clear
}
def reg_poke[T <: Element: Pokeable](port: T, target: BigInt) { registered_bits_updates(port) = target }
// This function replaces step in the advanced tester and makes sure all tester features are clocked in the appropriate order
def takestep(work: => Unit = {}): Unit = {
try {
step(1)
do_registered_updates()
preprocessors.foreach(_.process()) // e.g. sinks
work
postprocessors.foreach(_.process())
} catch {
case e: Throwable =>
fail
val sw = new StringWriter
val pw = new PrintWriter(sw)
e.printStackTrace(pw)
logger.info(pw.toString)
assert(finish, "test fail")
}
}
def takesteps(n: Int)(work: =>Unit = {}): Unit = {
require(n > 0, "Number of steps taken must be positive integer.")
(0 until n).foreach(_ => takestep(work))
}
// Functions to step depending on predicates
def until(pred: =>Boolean, maxCycles: Long = defaultMaxCycles)(work: =>Unit): Boolean = {
var timeout_cycles = 0L
while(!pred && (timeout_cycles < maxCycles)) {
takestep(work)
timeout_cycles += 1
}
assert(timeout_cycles < maxCycles,
"until timed out after %d cycles".format(timeout_cycles))
pred
}
def eventually(pred: =>Boolean, maxCycles: Long = defaultMaxCycles) = {until(pred, maxCycles){}}
def do_until(work: =>Unit)(pred: =>Boolean, maxCycles: Long = defaultMaxCycles): Boolean = {
takestep(work)
until(pred, maxCycles){work}
}
def assert(expr: Boolean, errMsg:String = "") = {
if (!expr && !errMsg.isEmpty) fail
expr
}
class IrrevocableSink[T <: Data, R]( socket: ReadyValidIO[T], cvt: T=>R,
max_count: Option[Int] = None ) extends Processable
{
socket match {
case s: IrrevocableIO[T] =>
case s: DecoupledIO[T] => {
logger.warning("Potentially unsafe conversion of DecoupledIO output to IrrevocableIO")
}
case _ => {
logger.warning("Potentially unsafe conversion of ReadyValidIO output to IrrevocableIO")
}
}
val outputs = new scala.collection.mutable.Queue[R]()
private var amReady = true
private def isValid = peek(socket.valid) == 1
def process() = {
// Handle this cycle
if(isValid && amReady) {
outputs.enqueue(cvt(socket.bits))
}
// Decide what to do next cycle and post onto register
amReady = max_count match { case None => true case Some(p) => outputs.length <= p }
reg_poke(socket.ready, amReady)
}
// Initialize
wire_poke(socket.ready, 1)
preprocessors += this
}
object IrrevocableSink {
def apply[T<:Element: Pokeable](socket: ReadyValidIO[T]) =
new IrrevocableSink(socket, (socket_bits: T) => peek(socket_bits))
}
class DecoupledSink[T <: Data, R]( socket: ReadyValidIO[T], cvt: T=>R,
max_count: Option[Int] = None ) extends IrrevocableSink(socket,cvt,max_count)
{
logger.warning("DecoupledSink is deprecated. Use IrrevocableSink")
}
object DecoupledSink {
def apply[T<:Element: Pokeable](socket: ReadyValidIO[T]) =
new DecoupledSink(socket, (socket_bits: T) => peek(socket_bits))
}
class ValidSink[T <: Data, R]( socket: ValidIO[T], cvt: T=>R ) extends Processable
{
val outputs = new scala.collection.mutable.Queue[R]()
private def isValid = peek(socket.valid) == 1
def process() = {
if(isValid) {
outputs.enqueue(cvt(socket.bits))
}
}
// Initialize
preprocessors += this
}
object ValidSink {
def apply[T<:Element: Pokeable](socket: ValidIO[T]) =
new ValidSink(socket, (socket_bits: T) => peek(socket_bits))
}
class DecoupledSource[T <: Data, R]( socket: DecoupledIO[T], post: (T,R)=>Unit ) extends Processable
{
val inputs = new scala.collection.mutable.Queue[R]()
private var amValid = false
private var justFired = false
private def isReady = (peek(socket.ready) == 1)
def isIdle = !amValid && inputs.isEmpty && !justFired
def process() = {
justFired = false
if(isReady && amValid) { // Fired last cycle
amValid = false
justFired = true
}
if(!amValid && !inputs.isEmpty) {
amValid = true
post(socket.bits, inputs.dequeue())
}
reg_poke(socket.valid, amValid)
}
// Initialize
wire_poke(socket.valid, 0)
postprocessors += this
}
object DecoupledSource {
def apply[T<:Element: Pokeable](socket: DecoupledIO[T]) =
new DecoupledSource(socket, (socket_bits: T, in: BigInt) => reg_poke(socket_bits, in))
}
class ValidSource[T <: Data, R]( socket: ValidIO[T], post: (T,R)=>Unit ) extends Processable
{
val inputs = new scala.collection.mutable.Queue[R]()
private var amValid = false
private var justFired = false
def isIdle = inputs.isEmpty && !amValid
def process() = {
// Always advance the input
justFired = (amValid==true)
amValid = false
if(!inputs.isEmpty) {
amValid = true
post(socket.bits, inputs.dequeue())
}
reg_poke(socket.valid, amValid)
}
// Initialize
wire_poke(socket.valid, 0)
postprocessors += this
}
object ValidSource {
def apply[T<:Element: Pokeable](socket: ValidIO[T]) =
new ValidSource(socket, (socket_bits: T, in: BigInt) => reg_poke(socket_bits, in))
}
}
trait Processable {
def process(): Unit
}