summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/Direction.scala
blob: 03755e83d79992c70a422ec8461a173999fbcf95 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// SPDX-License-Identifier: Apache-2.0

package chiselTests

import org.scalatest._
import chisel3._
import chisel3.stage.ChiselStage
import org.scalatest.matchers.should.Matchers

import scala.collection.immutable.SeqMap

class DirectionedBundle extends Bundle {
  val in = Input(UInt(32.W))
  val out = Output(UInt(32.W))
}

class DirectionHaver extends Module {
  val io = IO(new Bundle {
    val in = Input(UInt(32.W))
    val out = Output(UInt(32.W))
    val inBundle = Input(new DirectionedBundle) // should override elements
    val outBundle = Output(new DirectionedBundle) // should override elements
  })
}

class GoodDirection extends DirectionHaver {
  io.out := 0.U
  io.outBundle.in := 0.U
  io.outBundle.out := 0.U
}

class BadDirection extends DirectionHaver {
  io.in := 0.U
}

class BadSubDirection extends DirectionHaver {
  io.inBundle.out := 0.U
}

class TopDirectionOutput extends Module {
  val io = IO(Output(new DirectionedBundle))
  io.in := 42.U
  io.out := 117.U
}

class DirectionSpec extends ChiselPropSpec with Matchers with Utils {

  //TODO: In Chisel3 these are actually FIRRTL errors. Remove from tests?

  property("Outputs should be assignable") {
    ChiselStage.elaborate(new GoodDirection)
  }

  property("Inputs should not be assignable") {
    a[Exception] should be thrownBy extractCause[Exception] {
      ChiselStage.elaborate(new BadDirection)
    }
    a[Exception] should be thrownBy extractCause[Exception] {
      ChiselStage.elaborate(new BadSubDirection)
    }
  }

  property("Top-level forced outputs should be assignable") {
    ChiselStage.elaborate(new TopDirectionOutput)
  }

  property("Empty Vecs with directioned sample_element should not cause direction errors") {
    ChiselStage.elaborate(new Module {
      val io = IO(new Bundle {
        val foo = Input(UInt(8.W))
        val x = Vec(0, Output(UInt(8.W)))
      })
    })
    ChiselStage.elaborate(new Module {
      val io = IO(new Bundle {
        val foo = Input(UInt(8.W))
        val x = Flipped(Vec(0, Output(UInt(8.W))))
      })
    })
    ChiselStage.elaborate(new Module {
      val io = IO(new Bundle {
        val foo = Input(UInt(8.W))
        val x = Output(Vec(0, UInt(8.W)))
      })
    })
  }

  property("Empty Vecs with no direction on the sample_element *should* cause direction errors") {
    an[Exception] should be thrownBy extractCause[Exception] {
      ChiselStage.elaborate(new Module {
        val io = IO(new Bundle {
          val foo = Input(UInt(8.W))
          val x = Vec(0, UInt(8.W))
        })
      })
    }
  }

  property("Empty Bundles should not cause direction errors") {
    ChiselStage.elaborate(new Module {
      val io = IO(new Bundle {
        val foo = Input(UInt(8.W))
        val x = new Bundle {}
      })
    })
    ChiselStage.elaborate(new Module {
      val io = IO(new Bundle {
        val foo = Input(UInt(8.W))
        val x = Flipped(new Bundle {})
      })
    })
    ChiselStage.elaborate(new Module {
      val io = IO(new Bundle {
        val foo = Input(UInt(8.W))
        val x = new Bundle {
          val y = if (false) Some(Input(UInt(8.W))) else None
        }
      })
    })
  }

  property("Explicitly directioned but empty Bundles should cause direction errors") {
    an[Exception] should be thrownBy extractCause[Exception] {
      ChiselStage.elaborate(new Module {
        val io = IO(new Bundle {
          val foo = UInt(8.W)
          val x = Input(new Bundle {})
        })
      })
    }
  }

  import chisel3.experimental.{DataMirror, Direction}

  property("Directions should be preserved through cloning and binding of Bundles") {
    ChiselStage.elaborate(new Module {
      class MyBundle extends Bundle {
        val foo = Input(UInt(8.W))
        val bar = Output(UInt(8.W))
      }
      class MyOuterBundle extends Bundle {
        val fizz = new MyBundle
        val buzz = Flipped(new MyBundle)
      }
      val a = new MyOuterBundle
      val b = IO(a)
      val specifiedDirs = Seq(
        a.fizz.foo -> SpecifiedDirection.Input,
        a.fizz.bar -> SpecifiedDirection.Output,
        a.fizz -> SpecifiedDirection.Unspecified,
        a.buzz.foo -> SpecifiedDirection.Input,
        a.buzz.bar -> SpecifiedDirection.Output,
        a.buzz -> SpecifiedDirection.Flip
      )
      val actualDirs = Seq(
        b.fizz.foo -> Direction.Input,
        b.fizz.bar -> Direction.Output,
        b.fizz -> Direction.Bidirectional(Direction.Default),
        b.buzz.foo -> Direction.Output,
        b.buzz.bar -> Direction.Input,
        b.buzz -> Direction.Bidirectional(Direction.Flipped)
      )
      for ((data, dir) <- specifiedDirs) {
        DataMirror.specifiedDirectionOf(data) shouldBe (dir)
      }
      for ((data, dir) <- actualDirs) {
        DataMirror.directionOf(data) shouldBe (dir)
      }
    }.asInstanceOf[Module]) // The cast works around weird reflection behavior (bug?)
  }

  property("Directions should be preserved through cloning and binding of Vecs") {
    ChiselStage.elaborate(new Module {
      val a = Vec(1, Input(UInt(8.W)))
      val b = Vec(1, a)
      val c = Vec(1, Flipped(a))
      val io0 = IO(b)
      val io1 = IO(c)
      val specifiedDirs = Seq(
        a(0) -> SpecifiedDirection.Input,
        b(0)(0) -> SpecifiedDirection.Input,
        a -> SpecifiedDirection.Unspecified,
        b -> SpecifiedDirection.Unspecified,
        c(0) -> SpecifiedDirection.Flip,
        c(0)(0) -> SpecifiedDirection.Input,
        c -> SpecifiedDirection.Unspecified
      )
      val actualDirs = Seq(
        io0(0)(0) -> Direction.Input,
        io0(0) -> Direction.Input,
        io0 -> Direction.Input,
        io1(0)(0) -> Direction.Output,
        io1(0) -> Direction.Output,
        io1 -> Direction.Output
      )
      for ((data, dir) <- specifiedDirs) {
        DataMirror.specifiedDirectionOf(data) shouldBe (dir)
      }
      for ((data, dir) <- actualDirs) {
        DataMirror.directionOf(data) shouldBe (dir)
      }
    }.asInstanceOf[Module]) // The cast works around weird reflection behavior (bug?)
  }

  property("Using Vec and Flipped together should calculate directions properly") {
    class MyModule extends RawModule {
      class MyBundle extends Bundle {
        val a = Input(Bool())
        val b = Output(Bool())
      }

      val index = IO(Input(UInt(1.W)))

      // Check all permutations of Vec and Flipped.
      val regularVec = IO(Vec(2, new MyBundle))
      regularVec <> DontCare
      assert(DataMirror.directionOf(regularVec.head.a) == Direction.Input)
      assert(DataMirror.directionOf(regularVec.head.b) == Direction.Output)
      assert(DataMirror.directionOf(regularVec(index).a) == Direction.Input)
      assert(DataMirror.directionOf(regularVec(index).b) == Direction.Output)

      val vecFlipped = IO(Vec(2, Flipped(new MyBundle)))
      vecFlipped <> DontCare
      assert(DataMirror.directionOf(vecFlipped.head.a) == Direction.Output)
      assert(DataMirror.directionOf(vecFlipped.head.b) == Direction.Input)
      assert(DataMirror.directionOf(vecFlipped(index).a) == Direction.Output)
      assert(DataMirror.directionOf(vecFlipped(index).b) == Direction.Input)

      val flippedVec = IO(Flipped(Vec(2, new MyBundle)))
      flippedVec <> DontCare
      assert(DataMirror.directionOf(flippedVec.head.a) == Direction.Output)
      assert(DataMirror.directionOf(flippedVec.head.b) == Direction.Input)
      assert(DataMirror.directionOf(flippedVec(index).a) == Direction.Output)
      assert(DataMirror.directionOf(flippedVec(index).b) == Direction.Input)

      // Flipped(Vec(Flipped())) should be equal to non-flipped.
      val flippedVecFlipped = IO(Flipped(Vec(2, Flipped(new MyBundle))))
      flippedVecFlipped <> DontCare
      assert(DataMirror.directionOf(flippedVecFlipped.head.a) == Direction.Input)
      assert(DataMirror.directionOf(flippedVecFlipped.head.b) == Direction.Output)
      assert(DataMirror.directionOf(flippedVecFlipped(index).a) == Direction.Input)
      assert(DataMirror.directionOf(flippedVecFlipped(index).b) == Direction.Output)
    }

    val emitted: String = ChiselStage.emitChirrtl(new MyModule)
    val firrtl:  String = ChiselStage.convert(new MyModule).serialize

    // Check that emitted directions are correct.
    Seq(emitted, firrtl).foreach { o =>
      {
        // Chisel Emitter formats spacing a little differently than the
        // FIRRTL Emitter :-(
        val s = o.replace("{flip a", "{ flip a")
        assert(s.contains("output regularVec : { flip a : UInt<1>, b : UInt<1>}[2]"))
        assert(s.contains("input vecFlipped : { flip a : UInt<1>, b : UInt<1>}[2]"))
        assert(s.contains("input flippedVec : { flip a : UInt<1>, b : UInt<1>}[2]"))
        assert(s.contains("output flippedVecFlipped : { flip a : UInt<1>, b : UInt<1>}[2]"))
      }
    }
  }

  property("Vec with Input/Output should calculate directions properly") {
    class MyModule extends RawModule {
      class MyBundle extends Bundle {
        val a = Input(Bool())
        val b = Output(Bool())
      }

      val index = IO(Input(UInt(1.W)))

      val inputVec = IO(Vec(2, Input(new MyBundle)))
      inputVec <> DontCare
      assert(DataMirror.directionOf(inputVec.head.a) == Direction.Input)
      assert(DataMirror.directionOf(inputVec.head.b) == Direction.Input)
      assert(DataMirror.directionOf(inputVec(index).a) == Direction.Input)
      assert(DataMirror.directionOf(inputVec(index).b) == Direction.Input)

      val vecInput = IO(Input(Vec(2, new MyBundle)))
      vecInput <> DontCare
      assert(DataMirror.directionOf(vecInput.head.a) == Direction.Input)
      assert(DataMirror.directionOf(vecInput.head.b) == Direction.Input)
      assert(DataMirror.directionOf(vecInput(index).a) == Direction.Input)
      assert(DataMirror.directionOf(vecInput(index).b) == Direction.Input)

      val vecInputFlipped = IO(Input(Vec(2, Flipped(new MyBundle))))
      vecInputFlipped <> DontCare
      assert(DataMirror.directionOf(vecInputFlipped.head.a) == Direction.Input)
      assert(DataMirror.directionOf(vecInputFlipped.head.b) == Direction.Input)
      assert(DataMirror.directionOf(vecInputFlipped(index).a) == Direction.Input)
      assert(DataMirror.directionOf(vecInputFlipped(index).b) == Direction.Input)

      val outputVec = IO(Vec(2, Output(new MyBundle)))
      outputVec <> DontCare
      assert(DataMirror.directionOf(outputVec.head.a) == Direction.Output)
      assert(DataMirror.directionOf(outputVec.head.b) == Direction.Output)
      assert(DataMirror.directionOf(outputVec(index).a) == Direction.Output)
      assert(DataMirror.directionOf(outputVec(index).b) == Direction.Output)

      val vecOutput = IO(Output(Vec(2, new MyBundle)))
      vecOutput <> DontCare
      assert(DataMirror.directionOf(vecOutput.head.a) == Direction.Output)
      assert(DataMirror.directionOf(vecOutput.head.b) == Direction.Output)
      assert(DataMirror.directionOf(vecOutput(index).a) == Direction.Output)
      assert(DataMirror.directionOf(vecOutput(index).b) == Direction.Output)

      val vecOutputFlipped = IO(Output(Vec(2, Flipped(new MyBundle))))
      vecOutputFlipped <> DontCare
      assert(DataMirror.directionOf(vecOutputFlipped.head.a) == Direction.Output)
      assert(DataMirror.directionOf(vecOutputFlipped.head.b) == Direction.Output)
      assert(DataMirror.directionOf(vecOutputFlipped(index).a) == Direction.Output)
      assert(DataMirror.directionOf(vecOutputFlipped(index).b) == Direction.Output)
    }

    val emitted: String = ChiselStage.emitChirrtl(new MyModule)
    val firrtl:  String = ChiselStage.convert(new MyModule).serialize

    // Check that emitted directions are correct.
    Seq(emitted, firrtl).foreach { o =>
      {
        // Chisel Emitter formats spacing a little differently than the
        // FIRRTL Emitter :-(
        val s = o.replace("{a", "{ a")
        assert(s.contains("input inputVec : { a : UInt<1>, b : UInt<1>}[2]"))
        assert(s.contains("input vecInput : { a : UInt<1>, b : UInt<1>}[2]"))
        assert(s.contains("input vecInputFlipped : { a : UInt<1>, b : UInt<1>}[2]"))
        assert(s.contains("output outputVec : { a : UInt<1>, b : UInt<1>}[2]"))
        assert(s.contains("output vecOutput : { a : UInt<1>, b : UInt<1>}[2]"))
        assert(s.contains("output vecOutputFlipped : { a : UInt<1>, b : UInt<1>}[2]"))
      }
    }
  }
  property("Bugfix: marking Vec fields with mixed directionality as Output/Input clears inner directions") {
    class Decoupled extends Bundle {
      val bits = UInt(3.W)
      val valid = Bool()
      val ready = Flipped(Bool())
    }
    class Coercing extends Bundle {
      val source = Output(Vec(1, new Decoupled()))
      val sink = Input(Vec(1, new Decoupled()))
    }
    class MyModule extends RawModule {
      val io = IO(new Coercing())
      val source = IO(Output(Vec(1, new Decoupled())))
      val sink = IO(Input(Vec(1, new Decoupled())))
    }

    val emitted: String = ChiselStage.emitChirrtl(new MyModule)

    assert(
      emitted.contains(
        "output io : { source : { bits : UInt<3>, valid : UInt<1>, ready : UInt<1>}[1], flip sink : { bits : UInt<3>, valid : UInt<1>, ready : UInt<1>}[1]}"
      )
    )
    assert(
      emitted.contains(
        "output source : { bits : UInt<3>, valid : UInt<1>, ready : UInt<1>}[1]"
      )
    )
    assert(
      emitted.contains(
        "input sink : { bits : UInt<3>, valid : UInt<1>, ready : UInt<1>}[1]"
      )
    )
  }
  property("Bugfix: clearing all flips inside an opaque type") {

    class Decoupled extends Bundle {
      val bits = UInt(3.W)
      val valid = Bool()
      val ready = Flipped(Bool())
    }
    class MyOpaqueType extends Record {
      val k = new Decoupled()
      val elements = SeqMap("" -> k)
      override def opaqueType = elements.size == 1
      override def cloneType: this.type = (new MyOpaqueType).asInstanceOf[this.type]
    }
    class MyModule extends RawModule {
      val w = Wire(new MyOpaqueType())
    }

    val emitted: String = ChiselStage.emitChirrtl(new MyModule)

    assert(
      emitted.contains(
        "wire w : { bits : UInt<3>, valid : UInt<1>, flip ready : UInt<1>}"
      )
    )
  }
}