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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
|
// SPDX-License-Identifier: Apache-2.0
package chisel3
import scala.reflect.macros.blackbox.Context
import scala.language.experimental.macros
import chisel3.internal._
import chisel3.internal.Builder.pushCommand
import chisel3.internal.firrtl._
import chisel3.internal.sourceinfo.SourceInfo
import scala.reflect.macros.blackbox
/** Scaladoc information for internal verification statement macros
* that are used in objects assert, assume and cover.
*
* @groupdesc VerifPrintMacros
*
* <p>
* '''These internal methods are not part of the public-facing API!'''
* </p>
* <br>
*
* @groupprio VerifPrintMacros 1001
*/
trait VerifPrintMacrosDoc
object assert extends VerifPrintMacrosDoc {
/** Checks for a condition to be valid in the circuit at rising clock edge
* when not in reset. If the condition evaluates to false, the circuit
* simulation stops with an error.
*
* @param cond condition, assertion fires (simulation fails) when false
* @param message optional format string to print when the assertion fires
* @param data optional bits to print in the message formatting
*
* @note See [[printf.apply(fmt:String* printf]] for format string documentation
* @note currently cannot be used in core Chisel / libraries because macro
* defs need to be compiled first and the SBT project is not set up to do
* that
*/
// Macros currently can't take default arguments, so we need two functions to emulate defaults.
def apply(
cond: Bool,
message: String,
data: Bits*
)(
implicit sourceInfo: SourceInfo,
compileOptions: CompileOptions
): Assert = macro _applyMacroWithInterpolatorCheck
/** Checks for a condition to be valid in the circuit at all times. If the
* condition evaluates to false, the circuit simulation stops with an error.
*
* Does not fire when in reset (defined as the current implicit reset, e.g. as set by
* the enclosing `withReset` or Module.reset.
*
* May be called outside of a Module (like defined in a function), so
* functions using assert make the standard Module assumptions (single clock
* and single reset).
*
* @param cond condition, assertion fires (simulation fails) on a rising clock edge when false and reset is not asserted
* @param message optional chisel Printable type message
*
* @note See [[printf.apply(fmt:Printable)]] for documentation on printf using Printables
* @note currently cannot be used in core Chisel / libraries because macro
* defs need to be compiled first and the SBT project is not set up to do
* that
*/
def apply(
cond: Bool,
message: Printable
)(
implicit sourceInfo: SourceInfo,
compileOptions: CompileOptions
): Assert = macro _applyMacroWithPrintableMessage
def apply(cond: Bool)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Assert =
macro _applyMacroWithNoMessage
import VerificationStatement._
/** @group VerifPrintMacros */
def _applyMacroWithInterpolatorCheck(
c: blackbox.Context
)(cond: c.Tree,
message: c.Tree,
data: c.Tree*
)(sourceInfo: c.Tree,
compileOptions: c.Tree
): c.Tree = {
import c.universe._
message match {
case q"scala.StringContext.apply(..$_).s(..$_)" =>
c.warning(
c.enclosingPosition,
"The s-interpolator prints the Scala .toString of Data objects rather than the value " +
"of the hardware wire during simulation. Use the cf-interpolator instead. If you want " +
"an elaboration time check, call assert with a Boolean condition."
)
case _ =>
}
val apply_impl_do = symbolOf[this.type].asClass.module.info.member(TermName("_applyWithSourceLinePrintable"))
q"$apply_impl_do($cond, ${getLine(c)}, _root_.scala.Some(_root_.chisel3.Printable.pack($message, ..$data)))($sourceInfo, $compileOptions)"
}
/** An elaboration-time assertion. Calls the built-in Scala assert function. */
def apply(cond: Boolean, message: => String): Unit = Predef.assert(cond, message)
/** An elaboration-time assertion. Calls the built-in Scala assert function. */
def apply(cond: Boolean): Unit = Predef.assert(cond, "")
/** Named class for assertions. */
final class Assert private[chisel3] () extends VerificationStatement
/** @group VerifPrintMacros */
@deprecated(
"This method has been deprecated in favor of _applyMacroWithStringMessage. Please use the same.",
"Chisel 3.5"
)
def _applyMacroWithMessage(
c: blackbox.Context
)(cond: c.Tree,
message: c.Tree,
data: c.Tree*
)(sourceInfo: c.Tree,
compileOptions: c.Tree
): c.Tree = {
import c.universe._
val apply_impl_do = symbolOf[this.type].asClass.module.info.member(TermName("_applyWithSourceLinePrintable"))
q"$apply_impl_do($cond, ${getLine(c)},_root_.scala.Some(_root_.chisel3.Printable.pack($message,..$data)))($sourceInfo, $compileOptions)"
}
/** @group VerifPrintMacros */
def _applyMacroWithStringMessage(
c: blackbox.Context
)(cond: c.Tree,
message: c.Tree,
data: c.Tree*
)(sourceInfo: c.Tree,
compileOptions: c.Tree
): c.Tree = {
import c.universe._
val apply_impl_do = symbolOf[this.type].asClass.module.info.member(TermName("_applyWithSourceLinePrintable"))
q"$apply_impl_do($cond, ${getLine(c)},_root_.scala.Some(_root_.chisel3.Printable.pack($message,..$data)))($sourceInfo, $compileOptions)"
}
/** @group VerifPrintMacros */
def _applyMacroWithPrintableMessage(
c: blackbox.Context
)(cond: c.Tree,
message: c.Tree
)(sourceInfo: c.Tree,
compileOptions: c.Tree
): c.Tree = {
import c.universe._
val apply_impl_do = symbolOf[this.type].asClass.module.info.member(TermName("_applyWithSourceLinePrintable"))
q"$apply_impl_do($cond, ${getLine(c)}, _root_.scala.Some($message))($sourceInfo, $compileOptions)"
}
/** @group VerifPrintMacros */
def _applyMacroWithNoMessage(
c: blackbox.Context
)(cond: c.Tree
)(sourceInfo: c.Tree,
compileOptions: c.Tree
): c.Tree = {
import c.universe._
val apply_impl_do = symbolOf[this.type].asClass.module.info.member(TermName("_applyWithSourceLinePrintable"))
q"$apply_impl_do($cond, ${getLine(c)}, _root_.scala.None)($sourceInfo, $compileOptions)"
}
/** This will be removed in Chisel 3.6 in favor of the Printable version
*
* @group VerifPrintMacros
*/
def _applyWithSourceLine(
cond: Bool,
line: SourceLineInfo,
message: Option[String],
data: Bits*
)(
implicit sourceInfo: SourceInfo,
compileOptions: CompileOptions
): Assert = {
val id = new Assert()
when(!Module.reset.asBool()) {
failureMessage("Assertion", line, cond, message.map(Printable.pack(_, data: _*)))
Builder.pushCommand(Verification(id, Formal.Assert, sourceInfo, Module.clock.ref, cond.ref, ""))
}
id
}
/** @group VerifPrintMacros */
def _applyWithSourceLinePrintable(
cond: Bool,
line: SourceLineInfo,
message: Option[Printable]
)(
implicit sourceInfo: SourceInfo,
compileOptions: CompileOptions
): Assert = {
val id = new Assert()
message.foreach(Printable.checkScope(_))
when(!Module.reset.asBool()) {
failureMessage("Assertion", line, cond, message)
Builder.pushCommand(Verification(id, Formal.Assert, sourceInfo, Module.clock.ref, cond.ref, ""))
}
id
}
}
object assume extends VerifPrintMacrosDoc {
/** Assumes a condition to be valid in the circuit at all times.
* Acts like an assertion in simulation and imposes a declarative
* assumption on the state explored by formal tools.
*
* Does not fire when in reset (defined as the encapsulating Module's
* reset). If your definition of reset is not the encapsulating Module's
* reset, you will need to gate this externally.
*
* May be called outside of a Module (like defined in a function), so
* functions using assert make the standard Module assumptions (single clock
* and single reset).
*
* @param cond condition, assertion fires (simulation fails) when false
* @param message optional format string to print when the assertion fires
* @param data optional bits to print in the message formatting
*
* @note See [[printf.apply(fmt:String* printf]] for format string documentation
*/
// Macros currently can't take default arguments, so we need two functions to emulate defaults.
def apply(
cond: Bool,
message: String,
data: Bits*
)(
implicit sourceInfo: SourceInfo,
compileOptions: CompileOptions
): Assume = macro _applyMacroWithInterpolatorCheck
/** Assumes a condition to be valid in the circuit at all times.
* Acts like an assertion in simulation and imposes a declarative
* assumption on the state explored by formal tools.
*
* Does not fire when in reset (defined as the encapsulating Module's
* reset). If your definition of reset is not the encapsulating Module's
* reset, you will need to gate this externally.
*
* May be called outside of a Module (like defined in a function), so
* functions using assert make the standard Module assumptions (single clock
* and single reset).
*
* @param cond condition, assertion fires (simulation fails) when false
* @param message optional Printable type message when the assertion fires
*
* @note See [[printf.apply(fmt:Printable]] for documentation on printf using Printables
*/
def apply(
cond: Bool,
message: Printable
)(
implicit sourceInfo: SourceInfo,
compileOptions: CompileOptions
): Assume = macro _applyMacroWithPrintableMessage
def apply(cond: Bool)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Assume =
macro _applyMacroWithNoMessage
/** An elaboration-time assumption. Calls the built-in Scala assume function. */
def apply(cond: Boolean, message: => String): Unit = Predef.assume(cond, message)
/** An elaboration-time assumption. Calls the built-in Scala assume function. */
def apply(cond: Boolean): Unit = Predef.assume(cond, "")
/** Named class for assumptions. */
final class Assume private[chisel3] () extends VerificationStatement
import VerificationStatement._
/** @group VerifPrintMacros */
def _applyMacroWithInterpolatorCheck(
c: blackbox.Context
)(cond: c.Tree,
message: c.Tree,
data: c.Tree*
)(sourceInfo: c.Tree,
compileOptions: c.Tree
): c.Tree = {
import c.universe._
message match {
case q"scala.StringContext.apply(..$_).s(..$_)" =>
c.warning(
c.enclosingPosition,
"The s-interpolator prints the Scala .toString of Data objects rather than the value " +
"of the hardware wire during simulation. Use the cf-interpolator instead. If you want " +
"an elaboration time check, call assert with a Boolean condition."
)
case _ =>
}
val apply_impl_do = symbolOf[this.type].asClass.module.info.member(TermName("_applyWithSourceLinePrintable"))
q"$apply_impl_do($cond, ${getLine(c)}, _root_.scala.Some(_root_.chisel3.Printable.pack($message, ..$data)))($sourceInfo, $compileOptions)"
}
/** @group VerifPrintMacros */
@deprecated(
"This method has been deprecated in favor of _applyMacroWithStringMessage. Please use the same.",
"Chisel 3.5"
)
def _applyMacroWithMessage(
c: blackbox.Context
)(cond: c.Tree,
message: c.Tree,
data: c.Tree*
)(sourceInfo: c.Tree,
compileOptions: c.Tree
): c.Tree = {
import c.universe._
val apply_impl_do = symbolOf[this.type].asClass.module.info.member(TermName("_applyWithSourceLinePrintable"))
q"$apply_impl_do($cond, ${getLine(c)}, _root_.scala.Some(_root_.chisel3.Printable.pack($message, ..$data)))($sourceInfo, $compileOptions)"
}
/** @group VerifPrintMacros */
def _applyMacroWithStringMessage(
c: blackbox.Context
)(cond: c.Tree,
message: c.Tree,
data: c.Tree*
)(sourceInfo: c.Tree,
compileOptions: c.Tree
): c.Tree = {
import c.universe._
val apply_impl_do = symbolOf[this.type].asClass.module.info.member(TermName("_applyWithSourceLinePrintable"))
q"$apply_impl_do($cond, ${getLine(c)}, _root_.scala.Some(_root_.chisel3.Printable.pack($message, ..$data)))($sourceInfo, $compileOptions)"
}
/** @group VerifPrintMacros */
def _applyMacroWithPrintableMessage(
c: blackbox.Context
)(cond: c.Tree,
message: c.Tree
)(sourceInfo: c.Tree,
compileOptions: c.Tree
): c.Tree = {
import c.universe._
val apply_impl_do = symbolOf[this.type].asClass.module.info.member(TermName("_applyWithSourceLinePrintable"))
q"$apply_impl_do($cond, ${getLine(c)}, _root_.scala.Some($message))($sourceInfo, $compileOptions)"
}
/** @group VerifPrintMacros */
def _applyMacroWithNoMessage(
c: blackbox.Context
)(cond: c.Tree
)(sourceInfo: c.Tree,
compileOptions: c.Tree
): c.Tree = {
import c.universe._
val apply_impl_do = symbolOf[this.type].asClass.module.info.member(TermName("_applyWithSourceLinePrintable"))
q"$apply_impl_do($cond, ${getLine(c)}, _root_.scala.None)($sourceInfo, $compileOptions)"
}
/** This will be removed in Chisel 3.6 in favor of the Printable version
*
* @group VerifPrintMacros
*/
def _applyWithSourceLine(
cond: Bool,
line: SourceLineInfo,
message: Option[String],
data: Bits*
)(
implicit sourceInfo: SourceInfo,
compileOptions: CompileOptions
): Assume = {
val id = new Assume()
when(!Module.reset.asBool()) {
failureMessage("Assumption", line, cond, message.map(Printable.pack(_, data: _*)))
Builder.pushCommand(Verification(id, Formal.Assume, sourceInfo, Module.clock.ref, cond.ref, ""))
}
id
}
/** @group VerifPrintMacros */
def _applyWithSourceLinePrintable(
cond: Bool,
line: SourceLineInfo,
message: Option[Printable]
)(
implicit sourceInfo: SourceInfo,
compileOptions: CompileOptions
): Assume = {
val id = new Assume()
message.foreach(Printable.checkScope(_))
when(!Module.reset.asBool()) {
failureMessage("Assumption", line, cond, message)
Builder.pushCommand(Verification(id, Formal.Assume, sourceInfo, Module.clock.ref, cond.ref, ""))
}
id
}
}
object cover extends VerifPrintMacrosDoc {
/** Declares a condition to be covered.
* At ever clock event, a counter is incremented iff the condition is active
* and reset is inactive.
*
* Does not fire when in reset (defined as the encapsulating Module's
* reset). If your definition of reset is not the encapsulating Module's
* reset, you will need to gate this externally.
*
* May be called outside of a Module (like defined in a function), so
* functions using assert make the standard Module assumptions (single clock
* and single reset).
*
* @param cond condition that will be sampled on every clock tick
* @param message a string describing the cover event
*/
// Macros currently can't take default arguments, so we need two functions to emulate defaults.
def apply(cond: Bool, message: String)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Cover =
macro _applyMacroWithMessage
def apply(cond: Bool)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Cover =
macro _applyMacroWithNoMessage
/** Named class for cover statements. */
final class Cover private[chisel3] () extends VerificationStatement
import VerificationStatement._
/** @group VerifPrintMacros */
def _applyMacroWithNoMessage(
c: blackbox.Context
)(cond: c.Tree
)(sourceInfo: c.Tree,
compileOptions: c.Tree
): c.Tree = {
import c.universe._
val apply_impl_do = symbolOf[this.type].asClass.module.info.member(TermName("_applyWithSourceLine"))
q"$apply_impl_do($cond, ${getLine(c)}, _root_.scala.None)($sourceInfo, $compileOptions)"
}
/** @group VerifPrintMacros */
def _applyMacroWithMessage(
c: blackbox.Context
)(cond: c.Tree,
message: c.Tree
)(sourceInfo: c.Tree,
compileOptions: c.Tree
): c.Tree = {
import c.universe._
val apply_impl_do = symbolOf[this.type].asClass.module.info.member(TermName("_applyWithSourceLine"))
q"$apply_impl_do($cond, ${getLine(c)}, _root_.scala.Some($message))($sourceInfo, $compileOptions)"
}
/** @group VerifPrintMacros */
def _applyWithSourceLine(
cond: Bool,
line: SourceLineInfo,
message: Option[String]
)(
implicit sourceInfo: SourceInfo,
compileOptions: CompileOptions
): Cover = {
val id = new Cover()
when(!Module.reset.asBool()) {
Builder.pushCommand(Verification(id, Formal.Cover, sourceInfo, Module.clock.ref, cond.ref, ""))
}
id
}
}
object stop {
/** Terminate execution, indicating success.
*
* @param message a string describing why the simulation was stopped
*/
def apply(message: String = "")(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Stop = {
val stp = new Stop()
when(!Module.reset.asBool) {
pushCommand(Stop(stp, sourceInfo, Builder.forcedClock.ref, 0))
}
stp
}
/** Terminate execution with a failure code. */
@deprecated(
"Non-zero return codes are not well supported. Please use assert(false.B) if you want to indicate a failure.",
"Chisel 3.5"
)
def apply(code: Int)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Stop = {
val stp = new Stop()
when(!Module.reset.asBool) {
pushCommand(Stop(stp, sourceInfo, Builder.forcedClock.ref, code))
}
stp
}
/** Named class for [[stop]]s. */
final class Stop private[chisel3] () extends VerificationStatement
}
/** Base class for all verification statements: Assert, Assume, Cover, Stop and Printf. */
abstract class VerificationStatement extends NamedComponent {
_parent.foreach(_.addId(this))
}
/** Helper functions for common functionality required by stop, assert, assume or cover */
private object VerificationStatement {
type SourceLineInfo = (String, Int, String)
def getLine(c: blackbox.Context): SourceLineInfo = {
val p = c.enclosingPosition
(p.source.file.name, p.line, p.lineContent.trim)
}
def failureMessage(
kind: String,
lineInfo: SourceLineInfo,
cond: Bool,
message: Option[Printable]
)(
implicit sourceInfo: SourceInfo,
compileOptions: CompileOptions
): Unit = {
val (filename, line, content) = lineInfo
val lineMsg = s"$filename:$line $content".replaceAll("%", "%%")
val fmt = message match {
case Some(msg) =>
p"$kind failed: $msg\n at $lineMsg\n"
case None => p"$kind failed\n at $lineMsg\n"
}
when(!cond) {
printf.printfWithoutReset(fmt)
}
}
}
|