summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/Module.scala
blob: 13dbe1e945840d3a2afc0da4e2978221ae491681 (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
// SPDX-License-Identifier: Apache-2.0

package chiselTests

import chisel3._
import chisel3.experimental.DataMirror
import chisel3.stage.{ChiselGeneratorAnnotation, ChiselStage, NoRunFirrtlCompilerAnnotation}
import firrtl.annotations.NoTargetAnnotation
import firrtl.options.Unserializable

import scala.io.Source
import scala.annotation.nowarn

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

class PlusOne extends Module {
  val io = IO(new SimpleIO)
  io.out := io.in + 1.asUInt
}

class ModuleVec(val n: Int) extends Module {
  val io = IO(new Bundle {
    val ins = Input(Vec(n, UInt(32.W)))
    val outs = Output(Vec(n, UInt(32.W)))
  })
  val pluses = VecInit(Seq.fill(n) { Module(new PlusOne).io })
  for (i <- 0 until n) {
    pluses(i).in := io.ins(i)
    io.outs(i) := pluses(i).out
  }
}

class ModuleWire extends Module {
  val io = IO(new SimpleIO)
  val inc = Wire(chiselTypeOf(Module(new PlusOne).io))
  inc.in := io.in
  io.out := inc.out
}

class ModuleWhen extends Module {
  val io = IO(new Bundle {
    val s = new SimpleIO
    val en = Output(Bool())
  })
  when(io.en) {
    val inc = Module(new PlusOne).io
    inc.in := io.s.in
    io.s.out := inc.out
  }.otherwise { io.s.out := io.s.in }
}

class ModuleForgetWrapper extends Module {
  val io = IO(new SimpleIO)
  val inst = new PlusOne
}

class ModuleDoubleWrap extends Module {
  val io = IO(new SimpleIO)
  val inst = Module(Module(new PlusOne))
}

class ModuleRewrap extends Module {
  val io = IO(new SimpleIO)
  val inst = Module(new PlusOne)
  val inst2 = Module(inst)
}

class ModuleWrapper(gen: => Module) extends Module {
  val io = IO(new Bundle {})
  val child = Module(gen)
  override val desiredName = s"${child.desiredName}Wrapper"
}

class NullModuleWrapper extends Module {
  val io = IO(new Bundle {})
  override lazy val desiredName = s"${child.desiredName}Wrapper"
  println(s"My name is ${name}")
  val child = Module(new ModuleWire)
}

class ModuleSpec extends ChiselPropSpec with Utils {

  property("ModuleVec should elaborate") {
    ChiselStage.elaborate { new ModuleVec(2) }
  }

  ignore("ModuleVecTester should return the correct result") {}

  property("ModuleWire should elaborate") {
    ChiselStage.elaborate { new ModuleWire }
  }

  ignore("ModuleWireTester should return the correct result") {}

  property("ModuleWhen should elaborate") {
    ChiselStage.elaborate { new ModuleWhen }
  }

  ignore("ModuleWhenTester should return the correct result") {}

  property("Forgetting a Module() wrapper should result in an error") {
    (the[ChiselException] thrownBy extractCause[ChiselException] {
      ChiselStage.elaborate { new ModuleForgetWrapper }
    }).getMessage should include("attempted to instantiate a Module without wrapping it")
  }

  property("Double wrapping a Module should result in an error") {
    (the[ChiselException] thrownBy extractCause[ChiselException] {
      ChiselStage.elaborate { new ModuleDoubleWrap }
    }).getMessage should include("Called Module() twice without instantiating a Module")
  }

  property("Rewrapping an already instantiated Module should result in an error") {
    (the[ChiselException] thrownBy extractCause[ChiselException] {
      ChiselStage.elaborate { new ModuleRewrap }
    }).getMessage should include("This is probably due to rewrapping a Module instance")
  }

  property("object Module.clock should return a reference to the currently in scope clock") {
    ChiselStage.elaborate(new Module {
      val io = IO(new Bundle {
        val clock2 = Input(Clock())
      })
      assert(Module.clock eq this.clock)
      withClock(io.clock2) { assert(Module.clock eq io.clock2) }
    })
  }
  property("object Module.reset should return a reference to the currently in scope reset") {
    ChiselStage.elaborate(new Module {
      val io = IO(new Bundle {
        val reset2 = Input(Bool())
      })
      assert(Module.reset eq this.reset)
      withReset(io.reset2) { assert(Module.reset eq io.reset2) }
    })
  }
  property("object Module.currentModule should return an Option reference to the current Module") {
    def checkModule(mod: Module): Boolean = Module.currentModule.map(_ eq mod).getOrElse(false)
    ChiselStage.elaborate(new Module {
      val io = IO(new Bundle {})
      assert(Module.currentModule.get eq this)
      assert(checkModule(this))
    })
  }

  property("object chisel3.util.experimental.getAnnotations should return current annotations.") {
    case class DummyAnnotation() extends NoTargetAnnotation with Unserializable
    (new ChiselStage).transform(
      Seq(
        ChiselGeneratorAnnotation(() =>
          new RawModule {
            assert(chisel3.util.experimental.getAnnotations().contains(DummyAnnotation()))
          }
        ),
        DummyAnnotation(),
        NoRunFirrtlCompilerAnnotation
      )
    )
  }

  property("DataMirror.modulePorts should work") {
    ChiselStage.elaborate(new Module {
      val io = IO(new Bundle {})
      val m = Module(new chisel3.Module {
        val a = IO(UInt(8.W))
        val b = IO(Bool())
      })
      assert(DataMirror.modulePorts(m) == Seq("clock" -> m.clock, "reset" -> m.reset, "a" -> m.a, "b" -> m.b))
    })
  }

  property("DataMirror.modulePorts should replace deprecated <module>.getPorts") {
    class MyModule extends Module {
      val io = IO(new Bundle {
        val in = Input(UInt(8.W))
        val out = Output(Vec(2, UInt(8.W)))
      })
      val extra = IO(Input(UInt(8.W)))
      val delay = RegNext(io.in)
      io.out(0) := delay
      io.out(1) := delay + extra
    }
    var mod: MyModule = null
    ChiselStage.elaborate {
      mod = new MyModule
      mod
    }
    // Note that this is just top-level ports, Aggregates are not flattened
    (DataMirror.modulePorts(mod) should contain).theSameElementsInOrderAs(
      Seq(
        "clock" -> mod.clock,
        "reset" -> mod.reset,
        "io" -> mod.io,
        "extra" -> mod.extra
      )
    )
    // Delete this when the deprecated API is deleted
    // Note this also uses deprecated Port
    import chisel3.internal.firrtl.Port
    import SpecifiedDirection.{Input => IN, Unspecified}
    (mod.getPorts should contain).theSameElementsInOrderAs(
      Seq(
        Port(mod.clock, IN),
        Port(mod.reset, IN),
        Port(mod.io, Unspecified),
        Port(mod.extra, IN)
      )
    ): @nowarn // delete when Port and getPorts become private
  }

  property("DataMirror.fullModulePorts should return all ports including children of Aggregates") {
    class MyModule extends Module {
      val io = IO(new Bundle {
        val in = Input(UInt(8.W))
        val out = Output(Vec(2, UInt(8.W)))
      })
      val extra = IO(Input(UInt(8.W)))
      val delay = RegNext(io.in)
      io.out(0) := delay
      io.out(1) := delay + extra
    }
    var mod: MyModule = null
    ChiselStage.elaborate {
      mod = new MyModule
      mod
    }
    val expected = Seq(
      "clock" -> mod.clock,
      "reset" -> mod.reset,
      "io" -> mod.io,
      "io_out" -> mod.io.out,
      "io_out_0" -> mod.io.out(0),
      "io_out_1" -> mod.io.out(1),
      "io_in" -> mod.io.in,
      "extra" -> mod.extra
    )
    (DataMirror.fullModulePorts(mod) should contain).theSameElementsInOrderAs(expected)
  }

  property("A desiredName parameterized by a submodule should work") {
    ChiselStage.elaborate(new ModuleWrapper(new ModuleWire)).name should be("ModuleWireWrapper")
  }
  property("A name generating a null pointer exception should provide a good error message") {
    (the[ChiselException] thrownBy extractCause[ChiselException](
      ChiselStage.elaborate(new NullModuleWrapper)
    )).getMessage should include("desiredName of chiselTests.NullModuleWrapper is null")
  }
  property("The name of a module in a function should be sane") {
    def foo = {
      class Foo1 extends RawModule {
        assert(name == "Foo1")
      }
      new Foo1
    }
    ChiselStage.elaborate(foo)
  }
  property("The name of an anonymous module should include '_Anon'") {
    trait Foo { this: RawModule =>
      assert(name.contains("_Anon"))
    }
    ChiselStage.elaborate(new RawModule with Foo)
  }

  property("getVerilogString(new PlusOne() should produce a valid Verilog string") {
    val s = getVerilogString(new PlusOne())
    assert(s.contains("assign io_out = io_in + 32'h1"))
  }

  property("emitVerilog((new PlusOne()..) shall produce a valid Verilog file in a subfolder") {
    emitVerilog(new PlusOne(), Array("--target-dir", "generated"))
    val s = Source.fromFile("generated/PlusOne.v").mkString("")
    assert(s.contains("assign io_out = io_in + 32'h1"))
  }
}