summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/IllegalRefSpec.scala
blob: 219df5af6537293b5e230a2d3657f4a6b8586728 (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
// SPDX-License-Identifier: Apache-2.0

package chiselTests

import chisel3._
import chisel3.stage.ChiselStage

object IllegalRefSpec {
  class IllegalRefInner extends RawModule {
    val io = IO(new Bundle {
      val i = Input(Bool())
      val o = Output(Bool())
    })
    val x = io.i & io.i
    io.o := io.i
  }

  class IllegalRefOuter(useConnect: Boolean) extends RawModule {
    val io = IO(new Bundle {
      val a = Input(Bool())
      val b = Input(Bool())
      val out = Output(Bool())
    })

    val inst = Module(new IllegalRefInner)
    io.out := inst.io.o
    inst.io.i := io.a
    val x = WireInit(io.b)
    if (useConnect) {
      val z = WireInit(inst.x) // oops
    } else {
      val z = inst.x & inst.x // oops
    }
  }

  class CrossWhenConnect(useConnect: Boolean) extends RawModule {
    val io = IO(new Bundle {
      val i = Input(Bool())
      val o = Output(Bool())
    })
    private var tmp: Option[Bool] = None
    when(io.i) {
      val x = io.i & io.i
      tmp = Some(x)
    }
    if (useConnect) {
      io.o := tmp.get
    } else {
      val z = tmp.get & tmp.get
      io.o := io.i
    }
  }
}

class IllegalRefSpec extends ChiselFlatSpec with Utils {
  import IllegalRefSpec._

  val variants = Map("a connect" -> true, "an op" -> false)

  variants.foreach {
    case (k, v) =>
      s"Illegal cross-module references in ${k}" should "fail" in {
        a[ChiselException] should be thrownBy extractCause[ChiselException] {
          ChiselStage.elaborate { new IllegalRefOuter(v) }
        }
      }

      s"Using a signal that has escaped its enclosing when scope in ${k}" should "fail" in {
        a[ChiselException] should be thrownBy extractCause[ChiselException] {
          ChiselStage.elaborate { new CrossWhenConnect(v) }
        }
      }
  }
}