summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/experimental/ModuleDataProductSpec.scala
blob: 789865179304220e7b6d7221ad09cdc969665663 (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
// See LICENSE for license details.

package chiselTests.experimental

import chisel3._
import chisel3.experimental.{BaseModule, ExtModule}
import chisel3.experimental.dataview.DataProduct
import chiselTests.ChiselFlatSpec

object ModuleDataProductSpec {
  class MyBundle extends Bundle {
    val foo = UInt(8.W)
    val bar = UInt(8.W)
  }
  trait MyIntf extends BaseModule {
    val in = IO(Input(new MyBundle))
    val out = IO(Output(new MyBundle))
  }
  class Passthrough extends RawModule {
    val in = IO(Input(UInt(8.W)))
    val out = IO(Output(UInt(8.W)))
    out := in
  }
  class MyUserModule extends Module with MyIntf {
    val inst = Module(new Passthrough)
    inst.in := in.foo
    val r = RegNext(in)
    out := r
  }

  class MyExtModule extends ExtModule with MyIntf
  class MyExtModuleWrapper extends RawModule with MyIntf {
    val inst = Module(new MyExtModule)
    inst.in := in
    out := inst.out
  }
}

class ModuleDataProductSpec extends ChiselFlatSpec {
  import ModuleDataProductSpec._

  behavior of "DataProduct"

  it should "work for UserModules (recursively)" in {
    val m = elaborateAndGetModule(new MyUserModule)
    val expected = Seq(
      m.clock -> "m.clock",
      m.reset -> "m.reset",
      m.in -> "m.in",
      m.in.foo -> "m.in.foo",
      m.in.bar -> "m.in.bar",
      m.out -> "m.out",
      m.out.foo -> "m.out.foo",
      m.out.bar -> "m.out.bar",
      m.r -> "m.r",
      m.r.foo -> "m.r.foo",
      m.r.bar -> "m.r.bar",
      m.inst.in -> "m.inst.in",
      m.inst.out -> "m.inst.out"
    )

    val impl = implicitly[DataProduct[MyUserModule]]
    val set = impl.dataSet(m)
    for ((d, _) <- expected) {
      set(d) should be (true)
    }
    val it = impl.dataIterator(m, "m")
    it.toList should contain theSameElementsAs (expected)
  }

  it should "work for (wrapped) ExtModules" in {
    val m = elaborateAndGetModule(new MyExtModuleWrapper).inst
    val expected = Seq(
      m.in -> "m.in",
      m.in.foo -> "m.in.foo",
      m.in.bar -> "m.in.bar",
      m.out -> "m.out",
      m.out.foo -> "m.out.foo",
      m.out.bar -> "m.out.bar"
    )

    val impl = implicitly[DataProduct[MyExtModule]]
    val set = impl.dataSet(m)
    for ((d, _) <- expected) {
      set(d) should be (true)
    }
    val it = impl.dataIterator(m, "m")
    it.toList should contain theSameElementsAs (expected)
  }

}