summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/Assert.scala
blob: efc2e1e790e5180452771357e3bad97791730c7d (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
// See LICENSE for license details.

package chiselTests

import org.scalatest._
import chisel3._
import chisel3.testers.BasicTester
import chisel3.util._

class FailingAssertTester() extends BasicTester {
  assert(Bool(false))
  // Wait to come out of reset
  val (_, done) = Counter(!reset, 4)
  when (done) {
    stop()
  }
}

class SucceedingAssertTester() extends BasicTester {
  assert(Bool(true))
  // Wait to come out of reset
  val (_, done) = Counter(!reset, 4)
  when (done) {
    stop()
  }
}

class PipelinedResetModule extends Module {
  val io = IO(new Bundle { })
  val a = Reg(init = UInt(0xbeef))
  val b = Reg(init = UInt(0xbeef))
  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 := Reg(next = Reg(next = Reg(next = reset)))

  val (_, done) = Counter(!reset, 4)
  when (done) {
    stop()
  }
}

class AssertSpec extends ChiselFlatSpec {
  "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 }
  }
}