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

package chiselTests

import chisel3._
import chisel3.stage.ChiselStage
import chisel3.util.{switch, is}

class SwitchSpec extends ChiselFlatSpec with Utils {
  "switch" should "require literal conditions" in {
    a [java.lang.IllegalArgumentException] should be thrownBy extractCause[IllegalArgumentException] {
      ChiselStage.elaborate(new Module {
        val io = IO(new Bundle {})
        val state = RegInit(0.U)
        val wire = WireDefault(0.U)
        switch (state) {
          is (wire) { state := 1.U }
        }
      })
    }
  }
  it should "require mutually exclusive conditions" in {
    a [java.lang.IllegalArgumentException] should be thrownBy extractCause[IllegalArgumentException] {
      ChiselStage.elaborate(new Module {
        val io = IO(new Bundle {})
        val state = RegInit(0.U)
        switch (state) {
          is (0.U) { state := 1.U }
          is (1.U) { state := 2.U }
          is (0.U) { state := 3.U }
        }
      })
    }
  }
  it should "provide useful source locators" in {
    val chirrtl = ChiselStage.emitChirrtl(new Module {
      val io = IO(new Bundle {
        val in = Input(UInt(2.W))
        val out = Output(UInt(2.W))
      })

      io.out := 0.U
      switch (io.in) {
        is (0.U) { io.out := 3.U }
        is (1.U) { io.out := 0.U }
        is (2.U) { io.out := 1.U }
        is (3.U) { io.out := 3.U }
      }
    })

    chirrtl should not include "Conditional.scala"
  }
}