-
Notifications
You must be signed in to change notification settings - Fork 651
Expand file tree
/
Copy pathExtModule.scala
More file actions
304 lines (252 loc) · 8.69 KB
/
ExtModule.scala
File metadata and controls
304 lines (252 loc) · 8.69 KB
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
// SPDX-License-Identifier: Apache-2.0
package chiselTests
import chisel3._
import chisel3.reflect.DataMirror
import chisel3.simulator.scalatest.ChiselSim
import chisel3.simulator.stimulus.RunUntilFinished
import chisel3.testing.scalatest.FileCheck
import circt.stage.ChiselStage
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
// Avoid collisions with regular BlackBox tests by putting ExtModule blackboxes
// in their own scope.
package extmoduletests {
class BlackBoxInverter extends ExtModule {
val in = IO(Input(Bool()))
val out = IO(Output(Bool()))
addResource("/chisel3/BlackBoxInverter.v")
}
class BlackBoxPassthrough extends ExtModule {
val in = IO(Input(Bool()))
val out = IO(Output(Bool()))
addResource("/chisel3/BlackBoxPassthrough.v")
}
}
class ExtModuleTester extends Module {
val blackBoxPos = Module(new extmoduletests.BlackBoxInverter)
val blackBoxNeg = Module(new extmoduletests.BlackBoxInverter)
blackBoxPos.in := 1.U
blackBoxNeg.in := 0.U
assert(blackBoxNeg.out === 1.U)
assert(blackBoxPos.out === 0.U)
stop()
}
/** Instantiate multiple BlackBoxes with similar interfaces but different
* functionality. Used to detect failures in BlackBox naming and module
* deduplication.
*/
class MultiExtModuleTester extends Module {
val blackBoxInvPos = Module(new extmoduletests.BlackBoxInverter)
val blackBoxInvNeg = Module(new extmoduletests.BlackBoxInverter)
val blackBoxPassPos = Module(new extmoduletests.BlackBoxPassthrough)
val blackBoxPassNeg = Module(new extmoduletests.BlackBoxPassthrough)
blackBoxInvPos.in := 1.U
blackBoxInvNeg.in := 0.U
blackBoxPassPos.in := 1.U
blackBoxPassNeg.in := 0.U
assert(blackBoxInvNeg.out === 1.U)
assert(blackBoxInvPos.out === 0.U)
assert(blackBoxPassNeg.out === 0.U)
assert(blackBoxPassPos.out === 1.U)
stop()
}
class ExtModuleWithSuggestName extends ExtModule {
val in = IO(Input(UInt(8.W)))
in.suggestName("foo")
val out = IO(Output(UInt(8.W)))
}
class ExtModuleWithSuggestNameTester extends Module {
val in = IO(Input(UInt(8.W)))
val out = IO(Output(UInt(8.W)))
val inst = Module(new ExtModuleWithSuggestName)
inst.in := in
out := inst.out
}
class SimpleIOBundle extends Bundle {
val in = Input(UInt(8.W))
val out = Output(UInt(8.W))
}
class ExtModuleWithFlatIO extends ExtModule {
val badIO = FlatIO(new SimpleIOBundle)
}
class ExtModuleWithFlatIOTester extends Module {
val io = IO(new SimpleIOBundle)
val inst = Module(new ExtModuleWithFlatIO)
io <> inst.badIO
}
class ExtModuleInvalidatedTester extends Module {
val in = IO(Input(UInt(8.W)))
val out = IO(Output(UInt(8.W)))
val inst = Module(new ExtModule {
val in = IO(Input(UInt(8.W)))
val out = IO(Output(UInt(8.W)))
})
inst.in := in
out := inst.out
}
class ExtModuleSpec extends AnyFlatSpec with Matchers with ChiselSim with FileCheck {
"A ExtModule inverter" should "work" in {
simulate(new ExtModuleTester)(RunUntilFinished(3))
}
"Multiple ExtModules" should "work" in {
simulate(new MultiExtModuleTester)(RunUntilFinished(3))
}
"DataMirror.modulePorts" should "work with ExtModule" in {
ChiselStage.emitCHIRRTL(new Module {
val io = IO(new Bundle {})
val m = Module(new extmoduletests.BlackBoxPassthrough)
assert(DataMirror.modulePorts(m) == Seq("in" -> m.in, "out" -> m.out))
})
}
behavior.of("ExtModule")
it should "work with .suggestName (aka it should not require reflection for naming)" in {
val chirrtl = ChiselStage.emitCHIRRTL(new ExtModuleWithSuggestNameTester)
chirrtl should include("input foo : UInt<8>")
chirrtl should include("connect inst.foo, in")
}
it should "work with FlatIO" in {
val chirrtl = ChiselStage.emitCHIRRTL(new ExtModuleWithFlatIOTester)
chirrtl should include("connect io.out, inst.out")
chirrtl should include("connect inst.in, io.in")
chirrtl shouldNot include("badIO")
}
it should "not have invalidated ports in a chisel3._ context" in {
val chirrtl = ChiselStage.emitCHIRRTL(new ExtModuleInvalidatedTester)
chirrtl shouldNot include("invalidater inst.in")
chirrtl shouldNot include("invalidate inst.out")
}
it should "sort the verilog output of their param map by param key" in {
class ParameterizedBlackBox(m: Map[String, Param]) extends ExtModule(m) {
val io = IO(new Bundle {
val out = Output(Clock())
val in = Input(Clock())
})
}
class Top(m: Map[String, Param]) extends Module {
val io = IO(new Bundle {})
val pbb = Module(new ParameterizedBlackBox(m))
pbb.io.in := clock
}
val sixteenParams = ('a' until 'p').map { key => key.toString -> IntParam(1) }
def splitAndStrip(verilog: String): Array[String] = verilog.split("\n").map(_.dropWhile(_.isWhitespace))
getVerilogString(new Top(Map())) should include("ParameterizedBlackBox pbb")
getVerilogString(new Top(Map("a" -> IntParam(1)))) should include(".a(1)")
// check that both param orders are the same
(splitAndStrip(getVerilogString(new Top(Map("a" -> IntParam(1), "b" -> IntParam(1))))) should contain).allOf(
".a(1),",
".b(1)"
)
(splitAndStrip(getVerilogString(new Top(Map("b" -> IntParam(1), "a" -> IntParam(1))))) should contain).allOf(
".a(1),",
".b(1)"
)
// check that both param orders are the same, note that verilog output does a newline when more params are present
(splitAndStrip(getVerilogString(new Top(sixteenParams.toMap))) should contain).allOf(
".a(1),",
".b(1),",
".c(1),",
".d(1),",
".e(1),",
".f(1),",
".g(1),",
".h(1),",
".i(1),",
".j(1),",
".k(1),",
".l(1),",
".m(1),",
".n(1),",
".o(1)"
)
(splitAndStrip(getVerilogString(new Top(sixteenParams.reverse.toMap))) should contain).allOf(
".a(1),",
".b(1),",
".c(1),",
".d(1),",
".e(1),",
".f(1),",
".g(1),",
".h(1),",
".i(1),",
".j(1),",
".k(1),",
".l(1),",
".m(1),",
".n(1),",
".o(1)"
)
}
it should "emit FIRRTL knownlayer syntax if they have known layers" in {
object A extends layer.Layer(layer.LayerConfig.Extract())
// No known layers
class Bar extends ExtModule(knownLayers = Seq.empty)
// Single known layer, built-in
class Baz extends ExtModule(knownLayers = Seq(layers.Verification))
// Multiple known layers
class Qux extends ExtModule(knownLayers = Seq(layers.Verification, layers.Verification.Assert))
// Single known layer, user-defined and should be added to the circuit
class Quz extends ExtModule(knownLayers = Seq(A))
class Foo extends Module {
private val bar = Module(new Bar)
private val baz = Module(new Baz)
private val qux = Module(new Qux)
private val quz = Module(new Quz)
}
info("emitted CHIRRTL looks correct")
ChiselStage
.emitCHIRRTL(new Foo)
.fileCheck()(
"""|CHECK: layer A,
|CHECK: extmodule Bar :
|CHECK: extmodule Baz knownlayer Verification :
|CHECK: extmodule Qux knownlayer Verification, Verification.Assert :
|CHECK: extmodule Quz knownlayer A :
|""".stripMargin
)
info("CIRCT compilation doesn't error")
ChiselStage.emitSystemVerilog(new Foo)
}
it should "allow updates to knownLayers via adding layer-colored probe ports or via addLayer" in {
class Bar extends ExtModule {
val a = IO(Output(probe.Probe(Bool(), layers.Verification)))
}
object A extends layer.Layer(layer.LayerConfig.Extract())
class Baz extends ExtModule(knownLayers = Seq(A)) {
val a = IO(Output(probe.Probe(Bool(), layers.Verification)))
}
class Qux extends ExtModule {
layer.addLayer(A)
}
class Foo extends Module {
private val bar = Module(new Bar)
private val baz = Module(new Baz)
private val qux = Module(new Qux)
}
ChiselStage
.emitCHIRRTL(new Foo)
.fileCheck()(
"""|CHECK: layer Verification,
|CHECK: extmodule Bar knownlayer Verification :
|CHECK: extmodule Baz knownlayer A, Verification :
|CHECK: extmodule Qux knownlayer A :
|""".stripMargin
)
}
it should "have source locator information on ports" in {
class Bar extends ExtModule {
val a = IO(Output(Bool()))
}
class Foo extends Module {
val a = IO(Output(Bool()))
private val bar = Module(new Bar)
a :<= bar.a
}
ChiselStage
.emitCHIRRTL(new Foo)
.fileCheck()(
"""|CHECK: extmodule Bar :
|CHECK: output a : UInt<1> @[{{.+}}ExtModule.scala {{[0-9]+}}:{{[0-9]+}}]
|""".stripMargin
)
}
}