summaryrefslogtreecommitdiff
path: root/src/test/scala/cookbook/RegOfVec.scala
blob: ddb615c70269fac309d6f59da9f6888f4502bad8 (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
// SPDX-License-Identifier: Apache-2.0

package cookbook

import chisel3._

/* ### How do I create a Reg of type Vec?
 *
 * For information, please see the API documentation for Vec
 * (https://chisel.eecs.berkeley.edu/api/index.html#chisel3.Vec)
 */
class RegOfVec extends CookbookTester(2) {
  // Reg of Vec of 32-bit UInts without initialization
  val regOfVec = Reg(Vec(4, UInt(32.W)))
  regOfVec(0) := 123.U // a couple of assignments
  regOfVec(2) := regOfVec(0)

  // Reg of Vec of 32-bit UInts initialized to zero
  //   Note that Seq.fill constructs 4 32-bit UInt literals with the value 0
  //   Vec(...) then constructs a Wire of these literals
  //   The Reg is then initialized to the value of the Wire (which gives it the same type)
  val initRegOfVec = RegInit(VecInit(Seq.fill(4)(0.U(32.W))))

  // Simple test (cycle comes from superclass)
  when(cycle === 2.U) { assert(regOfVec(2) === 123.U) }
  for (elt <- initRegOfVec) { assert(elt === 0.U) }
}

class RegOfVecSpec extends CookbookSpec {
  "RegOfVec" should "work" in {
    assertTesterPasses { new RegOfVec }
  }
}