blob: 1849ddf84f98a973206f929db6c8c801cfe3a81b (
plain)
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
|
// SPDX-License-Identifier: Apache-2.0
package chiselTests
import chisel3._
import chisel3.stage.ChiselStage
import chisel3.testers.BasicTester
import chisel3.util._
class FailingAssertTester() extends BasicTester {
assert(false.B)
// Wait to come out of reset
val (_, done) = Counter(!reset.asBool, 4)
when(done) {
stop()
}
}
class SucceedingAssertTester() extends BasicTester {
assert(true.B)
// Wait to come out of reset
val (_, done) = Counter(!reset.asBool, 4)
when(done) {
stop()
}
}
class PipelinedResetModule extends Module {
val io = IO(new Bundle {})
val a = RegInit(0xbeef.U)
val b = RegInit(0xbeef.U)
assert(a === b)
}
// This relies on reset being asserted for 3 or more cycles
class PipelinedResetTester extends BasicTester {
val module = Module(new PipelinedResetModule)
module.reset := RegNext(RegNext(RegNext(reset)))
val (_, done) = Counter(!reset.asBool, 4)
when(done) {
stop()
}
}
class ModuloAssertTester extends BasicTester {
assert((4.U % 2.U) === 0.U)
stop()
}
class FormattedAssertTester extends BasicTester {
val foobar = Wire(UInt(32.W))
foobar := 123.U
assert(foobar === 123.U, "Error! Wire foobar =/= %x! This is 100%% wrong.\n", foobar)
stop()
}
class BadUnescapedPercentAssertTester extends BasicTester {
assert(1.U === 1.U, "I'm 110% sure this is an invalid message")
stop()
}
class AssertSpec extends ChiselFlatSpec with Utils {
"A failing assertion" should "fail the testbench" in {
assert(!runTester { new FailingAssertTester })
}
"A succeeding assertion" should "not fail the testbench" in {
assertTesterPasses { new SucceedingAssertTester }
}
"An assertion" should "not assert until we come out of reset" in {
assertTesterPasses { new PipelinedResetTester }
}
"Assertions" should "allow the modulo operator % in the message" in {
assertTesterPasses { new ModuloAssertTester }
}
they should "allow printf-style format strings with arguments" in {
assertTesterPasses { new FormattedAssertTester }
}
they should "not allow unescaped % in the message" in {
a[java.util.UnknownFormatConversionException] should be thrownBy {
extractCause[java.util.UnknownFormatConversionException] {
ChiselStage.elaborate { new BadUnescapedPercentAssertTester }
}
}
}
}
|