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

package chiselTests

import chisel3._
import chisel3.testers.BasicTester
import chisel3.stage.ChiselStage
import chisel3.util._

class LastAssignTester() extends BasicTester {
  val countOnClockCycles = true.B
  val (cnt, wrap) = Counter(countOnClockCycles, 2)

  val test = Wire(UInt(4.W))
  assert(test === 7.U) // allow read references before assign references

  test := 13.U
  assert(test === 7.U) // output value should be position-independent

  test := 7.U
  assert(test === 7.U) // this obviously should work

  when(cnt === 1.U) {
    stop()
  }
}

class MultiAssignSpec extends ChiselFlatSpec {
  "The last assignment" should "be used when multiple assignments happen" in {
    assertTesterPasses { new LastAssignTester }
  }
}

class IllegalAssignSpec extends ChiselFlatSpec with Utils {
  "Reassignments to literals" should "be disallowed" in {
    intercept[chisel3.internal.ChiselException] {
      extractCause[ChiselException] {
        ChiselStage.elaborate {
          new BasicTester {
            15.U := 7.U
          }
        }
      }
    }
  }

  "Reassignments to ops" should "be disallowed" in {
    intercept[chisel3.internal.ChiselException] {
      extractCause[ChiselException] {
        ChiselStage.elaborate {
          new BasicTester {
            (15.U + 1.U) := 7.U
          }
        }
      }
    }
  }

  "Reassignments to bit slices" should "be disallowed" in {
    intercept[chisel3.internal.ChiselException] {
      extractCause[ChiselException] {
        ChiselStage.elaborate {
          new BasicTester {
            (15.U)(1, 0) := 7.U
          }
        }
      }
    }
  }

  "Bulk-connecting two read-only nodes" should "be disallowed" in {
    intercept[chisel3.internal.ChiselException] {
      extractCause[ChiselException] {
        ChiselStage.elaborate {
          new BasicTester {
            (15.U + 1.U) <> 7.U
          }
        }
      }
    }
  }
}