summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/Vec.scala
blob: 4a871890f835109ddaab810893cce5a3d4326870 (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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// SPDX-License-Identifier: Apache-2.0

package chiselTests

import org.scalacheck._

import chisel3._
import chisel3.stage.ChiselStage
import chisel3.testers.{BasicTester, TesterDriver}
import chisel3.util._
import org.scalacheck.Shrink
import scala.annotation.tailrec

class LitTesterMod(vecSize: Int) extends Module {
  val io = IO(new Bundle {
    val out = Output(Vec(vecSize, UInt()))
  })
  io.out := VecInit(Seq.fill(vecSize) { 0.U })
}

class RegTesterMod(vecSize: Int) extends Module {
  val io = IO(new Bundle {
    val in = Input(Vec(vecSize, UInt()))
    val out = Output(Vec(vecSize, UInt()))
  })
  val vecReg = RegNext(io.in, VecInit(Seq.fill(vecSize) { 0.U }))
  io.out := vecReg
}

class IOTesterMod(vecSize: Int) extends Module {
  val io = IO(new Bundle {
    val in = Input(Vec(vecSize, UInt()))
    val out = Output(Vec(vecSize, UInt()))
  })
  io.out := io.in
}

class OneBitUnitRegVec extends Module {
  val io = IO(new Bundle {
    val out = Output(UInt(1.W))
  })
  val oneBitUnitRegVec = Reg(Vec(1, UInt(1.W)))
  oneBitUnitRegVec(0) := 1.U(1.W)
  io.out := oneBitUnitRegVec(0)
}

class LitTester(w: Int, values: List[Int]) extends BasicTester {
  val dut = Module(new LitTesterMod(values.length))
  for (a <- dut.io.out)
    assert(a === 0.U)
  stop()
}

class RegTester(w: Int, values: List[Int]) extends BasicTester {
  val v = VecInit(values.map(_.U(w.W)))
  val dut = Module(new RegTesterMod(values.length))
  val doneReg = RegInit(false.B)
  dut.io.in := v
  when(doneReg) {
    for ((a, b) <- dut.io.out.zip(values))
      assert(a === b.U)
    stop()
  }.otherwise {
    doneReg := true.B
    for (a <- dut.io.out)
      assert(a === 0.U)
  }
}

class IOTester(w: Int, values: List[Int]) extends BasicTester {
  val v = VecInit(values.map(_.U(w.W))) // Does this need a Wire? No. It's a Vec of Lits and hence synthesizeable.
  val dut = Module(new IOTesterMod(values.length))
  dut.io.in := v
  for ((a, b) <- dut.io.out.zip(values)) {
    assert(a === b.U)
  }
  stop()
}

class IOTesterModFill(vecSize: Int) extends Module {
  // This should generate a BindingException when we attempt to wire up the Vec.fill elements
  //  since they're pure types and hence unsynthesizeable.
  val io = IO(new Bundle {
    val in = Input(VecInit(Seq.fill(vecSize) { UInt() }))
    val out = Output(VecInit(Seq.fill(vecSize) { UInt() }))
  })
  io.out := io.in
}

class ValueTester(w: Int, values: List[Int]) extends BasicTester {
  val v = VecInit(values.map(_.asUInt(w.W)))
  for ((a, b) <- v.zip(values)) {
    assert(a === b.asUInt)
  }
  stop()
}

class TabulateTester(n: Int) extends BasicTester {
  val v = VecInit(Range(0, n).map(i => (i * 2).asUInt))
  val x = VecInit(Array.tabulate(n) { i => (i * 2).asUInt })
  val u = VecInit.tabulate(n)(i => (i * 2).asUInt)

  assert(v.asUInt() === x.asUInt())
  assert(v.asUInt() === u.asUInt())
  assert(x.asUInt() === u.asUInt())

  stop()
}

class FillTester(n: Int, value: Int) extends BasicTester {
  val x = VecInit(Array.fill(n)(value.U))
  val u = VecInit.fill(n)(value.U)

  assert(x.asUInt() === u.asUInt(), s"Expected Vec to be filled like $x, instead VecInit.fill created $u")
  stop()
}

object VecMultiDimTester {

  @tailrec
  private def assert2DIsCorrect(n: Int, arr: Vec[Vec[UInt]], compArr: Seq[Seq[Int]]): Unit = {
    val compareRow = arr(n).zip(compArr(n))
    compareRow.foreach(x => assert(x._1 === x._2.U))
    if (n != 0) assert2DIsCorrect(n - 1, arr, compArr)
  }

  @tailrec
  private def assert3DIsCorrect(n: Int, m: Int, arr: Vec[Vec[Vec[UInt]]], compArr: Seq[Seq[Seq[Int]]]): Unit = {
    assert2DIsCorrect(m - 1, arr(n), compArr(n))
    if (n != 0) assert3DIsCorrect(n - 1, m, arr, compArr)
  }

  class TabulateTester2D(n: Int, m: Int) extends BasicTester {
    def gen(x:        Int, y: Int): UInt = (x + y).asUInt
    def genCompVec(x: Int, y: Int): Int = x + y
    val vec = VecInit.tabulate(n, m) { gen }
    val compArr = Seq.tabulate(n, m) { genCompVec }

    assert2DIsCorrect(n - 1, vec, compArr)
    stop()
  }

  class TabulateTester3D(n: Int, m: Int, p: Int) extends BasicTester {
    def gen(x:        Int, y: Int, z: Int): UInt = (x + y + z).asUInt
    def genCompVec(x: Int, y: Int, z: Int): Int = x + y + z
    val vec = VecInit.tabulate(n, m, p) { gen }
    val compArr = Seq.tabulate(n, m, p) { genCompVec }

    assert3DIsCorrect(n - 1, m, vec, compArr)
    stop()
  }

  class Fill2DTester(n: Int, m: Int, value: Int) extends BasicTester {
    val u = VecInit.fill(n, m)(value.U)
    val compareArr = Seq.fill(n, m)(value)

    assert2DIsCorrect(n - 1, u, compareArr)
    stop()
  }

  class Fill3DTester(n: Int, m: Int, p: Int, value: Int) extends BasicTester {
    val u = VecInit.fill(n, m, p)(value.U)
    val compareArr = Seq.fill(n, m, p)(value)

    assert3DIsCorrect(n - 1, m, u, compareArr)
    stop()
  }

  class BidirectionalTester2DFill(n: Int, m: Int) extends BasicTester {
    val mod = Module(new PassthroughModule)
    val vec2D = VecInit.fill(n, m)(mod.io)
    for {
      vec1D <- vec2D
      module <- vec1D
    } yield {
      module <> Module(new PassthroughModuleTester).io
    }
    stop()
  }

  class BidirectionalTester3DFill(n: Int, m: Int, p: Int) extends BasicTester {
    val mod = Module(new PassthroughModule)
    val vec3D = VecInit.fill(n, m, p)(mod.io)

    for {
      vec2D <- vec3D
      vec1D <- vec2D
      module <- vec1D
    } yield {
      module <> (Module(new PassthroughModuleTester).io)
    }
    stop()
  }

  class TabulateModuleTester(value: Int) extends Module {
    val io = IO(Flipped(new PassthroughModuleIO))
    // This drives the input of a PassthroughModule
    io.in := value.U
  }

  class BidirectionalTester2DTabulate(n: Int, m: Int) extends BasicTester {
    val vec2D = VecInit.tabulate(n, m) { (x, y) => Module(new TabulateModuleTester(x + y + 1)).io }

    for {
      x <- 0 until n
      y <- 0 until m
    } yield {
      val value = x + y + 1
      val receiveMod = Module(new PassthroughModule).io
      vec2D(x)(y) <> receiveMod
      assert(receiveMod.out === value.U)
    }
    stop()
  }

  class BidirectionalTester3DTabulate(n: Int, m: Int, p: Int) extends BasicTester {
    val vec3D = VecInit.tabulate(n, m, p) { (x, y, z) => Module(new TabulateModuleTester(x + y + z + 1)).io }

    for {
      x <- 0 until n
      y <- 0 until m
      z <- 0 until p
    } yield {
      val value = x + y + z + 1
      val receiveMod = Module(new PassthroughModule).io
      vec3D(x)(y)(z) <> receiveMod
      assert(receiveMod.out === value.U)
    }
    stop()
  }
}

class IterateTester(start: Int, len: Int)(f: UInt => UInt) extends BasicTester {
  val controlVec = VecInit(Seq.iterate(start.U, len)(f))
  val testVec = VecInit.iterate(start.U, len)(f)
  assert(
    controlVec.asUInt() === testVec.asUInt(),
    s"Expected Vec to be filled like $controlVec, instead creaeted $testVec\n"
  )
  stop()
}

class ShiftRegisterTester(n: Int) extends BasicTester {
  val (cnt, wrap) = Counter(true.B, n * 2)
  val shifter = Reg(Vec(n, UInt((log2Ceil(n).max(1)).W)))
  (shifter, shifter.drop(1)).zipped.foreach(_ := _)
  shifter(n - 1) := cnt
  when(cnt >= n.asUInt) {
    val expected = cnt - n.asUInt
    assert(shifter(0) === expected)
  }
  when(wrap) {
    stop()
  }
}

class HugeVecTester(n: Int) extends BasicTester {
  require(n > 0)
  val myVec = Wire(Vec(n, UInt()))
  myVec.foreach { x =>
    x := 123.U
    assert(x === 123.U)
  }
  stop()
}

class OneBitUnitRegVecTester extends BasicTester {
  val dut = Module(new OneBitUnitRegVec)
  assert(dut.io.out === 1.U)
  stop()
}

class ZeroEntryVecTester extends BasicTester {
  require(Vec(0, Bool()).getWidth == 0)

  val bundleWithZeroEntryVec = new Bundle {
    val foo = Bool()
    val bar = Vec(0, Bool())
  }
  require(0.U.asTypeOf(bundleWithZeroEntryVec).getWidth == 1)
  require(bundleWithZeroEntryVec.asUInt.getWidth == 1)

  val m = Module(new Module {
    val io = IO(Output(bundleWithZeroEntryVec))
    io.foo := false.B
  })
  WireDefault(m.io.bar)

  stop()
}

class PassthroughModuleTester extends Module {
  val io = IO(Flipped(new PassthroughModuleIO))
  // This drives the input of a PassthroughModule
  io.in := 123.U
  assert(io.out === 123.U)
}

class ModuleIODynamicIndexTester(n: Int) extends BasicTester {
  val duts = VecInit.fill(n)(Module(new PassthroughModule).io)
  val tester = Module(new PassthroughModuleTester)

  val (cycle, done) = Counter(true.B, n)
  for ((m, i) <- duts.zipWithIndex) {
    when(cycle =/= i.U) {
      m.in := 0.U // default
      assert(m.out === 0.U)
    }.otherwise {
      m.in := DontCare
    }
  }
  // only connect one dut per cycle
  duts(cycle) <> tester.io
  assert(duts(cycle).out === 123.U)

  when(done) { stop() }
}

class ReduceTreeTester() extends BasicTester {
  class FooIO[T <: Data](n: Int, private val gen: T) extends Bundle {
    val in = Flipped(Vec(n, new DecoupledIO(gen)))
    val out = new DecoupledIO(gen)
  }

  class Foo[T <: Data](n: Int, private val gen: T) extends Module {
    val io = IO(new FooIO(n, gen))

    def foo(a: DecoupledIO[T], b: DecoupledIO[T]) = {
      a.ready := true.B
      b.ready := true.B
      val out = Wire(new DecoupledIO(gen))

      out.valid := true.B

      val regSel = RegInit(false.B)
      out.bits := Mux(regSel, a.bits, b.bits)
      out.ready := a.ready
      out
    }

    io.out <> io.in.reduceTree(foo)
  }

  val dut = Module(new Foo(5, UInt(5.W)))
  dut.io := DontCare
  stop()
}

class VecSpec extends ChiselPropSpec with Utils {
  // Disable shrinking on error.
  implicit val noShrinkListVal = Shrink[List[Int]](_ => Stream.empty)
  implicit val noShrinkInt = Shrink[Int](_ => Stream.empty)

  property("Vecs should be assignable") {
    forAll(safeUIntN(8)) {
      case (w: Int, v: List[Int]) =>
        assertTesterPasses { new ValueTester(w, v) }
    }
  }

  property("Vecs should be passed through vec IO") {
    forAll(safeUIntN(8)) {
      case (w: Int, v: List[Int]) =>
        assertTesterPasses { new IOTester(w, v) }
    }
  }

  property("Vec.fill with a pure type should generate an exception") {
    // We don't really need a sequence of random widths here, since any should throw an exception.
    forAll(safeUIntWidth) {
      case (w: Int) =>
        an[BindingException] should be thrownBy extractCause[BindingException] {
          ChiselStage.elaborate(new IOTesterModFill(w))
        }
    }
  }

  property("A Reg of a Vec should operate correctly") {
    forAll(safeUIntN(8)) {
      case (w: Int, v: List[Int]) =>
        assertTesterPasses { new RegTester(w, v) }
    }
  }

  property("A Vec of lit should operate correctly") {
    forAll(safeUIntN(8)) {
      case (w: Int, v: List[Int]) =>
        assertTesterPasses { new LitTester(w, v) }
    }
  }

  property("VecInit should tabulate correctly") {
    forAll(smallPosInts) { (n: Int) => assertTesterPasses { new TabulateTester(n) } }
  }

  property("VecInit should tabulate 2D vec correctly") {
    forAll(smallPosInts, smallPosInts) { (n: Int, m: Int) =>
      assertTesterPasses { new VecMultiDimTester.TabulateTester2D(n, m) }
    }
  }

  property("VecInit should tabulate 3D vec correctly") {
    forAll(smallPosInts, smallPosInts, smallPosInts) { (n: Int, m: Int, p: Int) =>
      assertTesterPasses { new VecMultiDimTester.TabulateTester3D(n, m, p) }
    }
  }

  property("VecInit should fill correctly") {
    forAll(smallPosInts, Gen.choose(0, 50)) { (n: Int, value: Int) => assertTesterPasses { new FillTester(n, value) } }
  }

  property("VecInit should fill 2D vec correctly") {
    forAll(smallPosInts, smallPosInts, Gen.choose(0, 50)) { (n: Int, m: Int, value: Int) =>
      assertTesterPasses { new VecMultiDimTester.Fill2DTester(n, m, value) }
    }
  }

  property("VecInit should fill 3D vec correctly") {
    forAll(smallPosInts, smallPosInts, smallPosInts, Gen.choose(0, 50)) { (n: Int, m: Int, p: Int, value: Int) =>
      assertTesterPasses { new VecMultiDimTester.Fill3DTester(n, m, p, value) }
    }
  }

  property("VecInit should support 2D fill bidirectional wire connection") {
    forAll(smallPosInts, smallPosInts) { (n: Int, m: Int) =>
      assertTesterPasses { new VecMultiDimTester.BidirectionalTester2DFill(n, m) }
    }
  }

  property("VecInit should support 3D fill bidirectional wire connection") {
    forAll(smallPosInts, smallPosInts, smallPosInts) { (n: Int, m: Int, p: Int) =>
      assertTesterPasses { new VecMultiDimTester.BidirectionalTester3DFill(n, m, p) }
    }
  }

  property("VecInit should support 2D tabulate bidirectional wire connection") {
    forAll(smallPosInts, smallPosInts) { (n: Int, m: Int) =>
      assertTesterPasses { new VecMultiDimTester.BidirectionalTester2DTabulate(n, m) }
    }
  }

  property("VecInit should support 3D tabulate bidirectional wire connection") {
    forAll(smallPosInts, smallPosInts, smallPosInts) { (n: Int, m: Int, p: Int) =>
      assertTesterPasses { new VecMultiDimTester.BidirectionalTester3DTabulate(n, m, p) }
    }
  }

  property("VecInit should iterate correctly") {
    forAll(Gen.choose(1, 10), smallPosInts) { (start: Int, len: Int) =>
      assertTesterPasses { new IterateTester(start, len)(x => x + 50.U) }
    }
  }

  property("Regs of vecs should be usable as shift registers") {
    forAll(smallPosInts) { (n: Int) => assertTesterPasses { new ShiftRegisterTester(n) } }
  }

  property("Infering widths on huge Vecs should not cause a stack overflow") {
    assertTesterPasses(new HugeVecTester(10000), annotations = TesterDriver.verilatorOnly)
  }

  property("A Reg of a Vec of a single 1 bit element should compile and work") {
    assertTesterPasses { new OneBitUnitRegVecTester }
  }

  property("A Vec with zero entries should compile and have zero width") {
    assertTesterPasses { new ZeroEntryVecTester }
  }

  property("Dynamic indexing of a Vec of Module IOs should work") {
    assertTesterPasses { new ModuleIODynamicIndexTester(4) }
  }

  property("It should be possible to bulk connect a Vec and a Seq") {
    ChiselStage.elaborate(new Module {
      val io = IO(new Bundle {
        val out = Output(Vec(4, UInt(8.W)))
      })
      val seq = Seq.fill(4)(0.U)
      io.out <> seq
    })
  }

  property("Bulk connecting a Vec and Seq of different sizes should report a ChiselException") {
    a[ChiselException] should be thrownBy extractCause[ChiselException] {
      ChiselStage.elaborate(new Module {
        val io = IO(new Bundle {
          val out = Output(Vec(4, UInt(8.W)))
        })
        val seq = Seq.fill(5)(0.U)
        io.out <> seq
      })
    }
  }

  property("It should be possible to initialize a Vec with DontCare") {
    ChiselStage.elaborate(new Module {
      val io = IO(new Bundle {
        val out = Output(Vec(4, UInt(8.W)))
      })
      io.out := VecInit(Seq(4.U, 5.U, DontCare, 2.U))
    })
  }

  property("Indexing a Chisel type Vec by a hardware type should give a sane error message") {
    a[ExpectedHardwareException] should be thrownBy extractCause[ChiselException] {
      ChiselStage.elaborate {
        new Module {
          val io = IO(new Bundle {})
          val foo = Vec(2, Bool())
          foo(0.U) := false.B
        }
      }
    }
  }

  property("reduceTree should preserve input/output type") {
    assertTesterPasses { new ReduceTreeTester() }
  }

  property("Vecs of empty Bundles and empty Records should work") {
    class MyModule(gen: Record) extends Module {
      val idx = IO(Input(UInt(2.W)))
      val in = IO(Input(gen))
      val out = IO(Output(gen))

      val reg = RegInit(0.U.asTypeOf(Vec(4, gen)))
      reg(idx) := in
      out := reg(idx)
    }
    class EmptyBundle extends Bundle
    class EmptyRecord extends Record {
      val elements = collection.immutable.ListMap.empty
      override def cloneType = (new EmptyRecord).asInstanceOf[this.type]
    }
    for (gen <- List(new EmptyBundle, new EmptyRecord)) {
      val chirrtl = ChiselStage.emitChirrtl(new MyModule(gen))
      chirrtl should include("input in : { }")
      chirrtl should include("reg reg : { }[4]")
    }
  }
}