summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/BundleLiteralSpec.scala
blob: 2a6c53d5a1d475215470469c99bbaaa5d49ea60f (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// See LICENSE for license details.

package chiselTests

import chisel3._
import chisel3.testers.BasicTester
import chisel3.experimental.RawModule
import chisel3.experimental.BundleLiterals._
import chisel3.core.BundleLiteralException

class BundleLiteralSpec extends ChiselFlatSpec {
  class MyBundle extends Bundle {
    val a = UInt(8.W)
    val b = Bool()
  }

  "bundle literals" should "work in RTL" in {
    val outsideBundleLit = (new MyBundle).Lit(_.a -> 42.U, _.b -> true.B)
    assertTesterPasses{ new BasicTester{
      // TODO: add direct bundle compare operations, when that feature is added
      chisel3.assert(outsideBundleLit.a === 42.U)
      chisel3.assert(outsideBundleLit.b === true.B)

      val bundleLit = (new MyBundle).Lit(_.a -> 42.U, _.b -> true.B)
      chisel3.assert(bundleLit.a === 42.U)
      chisel3.assert(bundleLit.b === true.B)

      chisel3.assert(bundleLit.a === outsideBundleLit.a)
      chisel3.assert(bundleLit.b === outsideBundleLit.b)

      val bundleWire = Wire(new MyBundle)
      bundleWire := outsideBundleLit

      chisel3.assert(bundleWire.a === 42.U)
      chisel3.assert(bundleWire.b === true.B)

      stop()
    } }
  }

  "partial bundle literals" should "work in RTL" in {
    assertTesterPasses{ new BasicTester{
      val bundleLit = (new MyBundle).Lit(_.a -> 42.U)
      chisel3.assert(bundleLit.a === 42.U)

      val bundleWire = Wire(new MyBundle)
      bundleWire := bundleLit

      chisel3.assert(bundleWire.a === 42.U)

      stop()
    } }
  }

  class MyOuterBundle extends Bundle {
    val a = new MyBundle
    val b = new Bundle {
      val c = Bool()
      val d = UInt(8.W)
    }
  }

  "contained bundles" should "work" in {
    assertTesterPasses{ new BasicTester{
      // Specify the inner Bundle value as a Bundle literal
      val explicitBundleLit = (new MyOuterBundle).Lit(
        _.a -> (new MyBundle).Lit(_.a -> 42.U, _.b -> true.B)
      )
      chisel3.assert(explicitBundleLit.a.a === 42.U)
      chisel3.assert(explicitBundleLit.a.b === true.B)

      // Specify the inner Bundle fields directly
      val expandedBundleLit = (new MyOuterBundle).Lit(
        _.a.a -> 42.U, _.a.b -> true.B,
        _.b.c -> false.B, _.b.d -> 255.U
      )
      chisel3.assert(expandedBundleLit.a.a === 42.U)
      chisel3.assert(expandedBundleLit.a.b === true.B)
      chisel3.assert(expandedBundleLit.b.c === false.B)
      chisel3.assert(expandedBundleLit.b.d === 255.U)

      // Anonymously contruct the inner Bundle literal
      // A bit of weird syntax that depends on implementation details of the Bundle literal constructor
      val childBundleLit = (new MyOuterBundle).Lit(
        b => b.b -> b.b.Lit(_.c -> false.B, _.d -> 255.U)
      )
      chisel3.assert(childBundleLit.b.c === false.B)
      chisel3.assert(childBundleLit.b.d === 255.U)

      stop()
    } }
  }

  "Bundle literals" should "assign" in {
    assertTesterPasses{ new BasicTester{
      val bundleWire = Wire(Output(new MyBundle))
      val bundleLit = (new MyBundle).Lit(_.a -> 42.U, _.b -> true.B)
      bundleWire := bundleLit

      chisel3.assert(bundleWire.a === 42.U)
      chisel3.assert(bundleWire.b === true.B)
      stop()
    } }
  }

  "partially initialized Bundle literals" should "assign" in {
    assertTesterPasses{ new BasicTester{
      val bundleWire = Wire(Output(new MyBundle))
      val bundleLit = (new MyBundle).Lit(_.a -> 42.U)
      bundleWire := bundleLit

      chisel3.assert(bundleWire.a === 42.U)
      stop()
    } }
  }

  "bundle literals with bad field specifiers" should "fail" in {
    val exc = intercept[BundleLiteralException] { elaborate { new RawModule {
      val bundle = new MyBundle
      bundle.Lit(x => bundle.a -> 0.U)  // DONT DO THIS, this gets past a syntax error to exercise the failure
    }}}
    exc.getMessage should include ("not a field")
  }

  "bundle literals with duplicate fields" should "fail" in {
    val exc = intercept[BundleLiteralException] { elaborate { new RawModule {
      (new MyBundle).Lit(_.a -> 0.U, _.a -> 0.U)
    }}}
    exc.getMessage should include ("duplicate")
    exc.getMessage should include (".a")
  }

  "bundle literals with non-literal values" should "fail" in {
    val exc = intercept[BundleLiteralException] { elaborate { new RawModule {
      (new MyBundle).Lit(_.a -> UInt())
    }}}
    exc.getMessage should include ("non-literal value")
    exc.getMessage should include (".a")
  }

  "bundle literals with non-type-equivalent element fields" should "fail" in {
    val exc = intercept[BundleLiteralException] { elaborate { new RawModule {
      (new MyBundle).Lit(_.a -> true.B)
    }}}
    exc.getMessage should include ("non-type-equivalent value")
    exc.getMessage should include (".a")
  }

  "bundle literals with non-type-equivalent sub-bundles" should "fail" in {
    val exc = intercept[BundleLiteralException] { elaborate { new RawModule {
      (new MyOuterBundle).Lit(_.b -> (new MyBundle).Lit(_.a -> 0.U))
    }}}
    exc.getMessage should include ("non-type-equivalent value")
    exc.getMessage should include (".b")
  }
}