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
|
// SPDX-License-Identifier: Apache-2.0
package chiselTests
import chisel3._
import chisel3.stage.ChiselStage
import chisel3.util._
import chisel3.testers.{BasicTester, TesterDriver}
import chisel3.experimental.{attach, Analog, BaseModule}
// IO for Modules that just connect bus to out
class AnalogReaderIO extends Bundle {
val bus = Analog(32.W)
val out = Output(UInt(32.W))
}
// IO for Modules that drive bus from in (there should be only 1)
class AnalogWriterIO extends Bundle {
val bus = Analog(32.W)
val in = Input(UInt(32.W))
}
trait AnalogReader {
def out: UInt
def bus: Analog
}
class AnalogReaderBlackBox extends BlackBox with AnalogReader {
val io = IO(new AnalogReaderIO)
def out = io.out
def bus = io.bus
}
class AnalogReaderWrapper extends Module with AnalogReader {
val io = IO(new AnalogReaderIO)
def out = io.out
def bus = io.bus
val mod = Module(new AnalogReaderBlackBox)
io <> mod.io
}
class AnalogWriterBlackBox extends BlackBox {
val io = IO(new AnalogWriterIO)
}
// Connects two Analog ports
class AnalogConnector extends Module {
val io = IO(new Bundle {
val bus1 = Analog(32.W)
val bus2 = Analog(32.W)
})
io.bus1 <> io.bus2
}
class VecAnalogReaderWrapper extends RawModule with AnalogReader {
val vecbus = IO(Vec(1, Analog(32.W)))
val out = IO(Output(UInt(32.W)))
val mod = Module(new AnalogReaderBlackBox)
def bus = vecbus(0)
mod.io.bus <> bus
out := mod.io.out
}
class VecBundleAnalogReaderWrapper extends RawModule with AnalogReader {
val vecBunBus = IO(
Vec(
1,
new Bundle {
val analog = Analog(32.W)
}
)
)
def bus = vecBunBus(0).analog
val out = IO(Output(UInt(32.W)))
val mod = Module(new AnalogReaderBlackBox)
mod.io.bus <> bus
out := mod.io.out
}
// Parent class for tests connecing up AnalogReaders and AnalogWriters
abstract class AnalogTester extends BasicTester {
final val BusValue = "hdeadbeef".U
final val (cycle, done) = Counter(true.B, 2)
when(done) { stop() }
final val writer = Module(new AnalogWriterBlackBox)
writer.io.in := BusValue
final def check(reader: BaseModule with AnalogReader): Unit =
assert(reader.out === BusValue)
}
class AnalogSpec extends ChiselFlatSpec with Utils {
behavior.of("Analog")
it should "NOT be bindable to registers" in {
a[ChiselException] should be thrownBy extractCause[ChiselException] {
ChiselStage.elaborate {
new Module {
val io = IO(new Bundle {})
val reg = Reg(Analog(32.W))
}
}
}
}
it should "NOT be bindable to a direction" in {
a[ChiselException] should be thrownBy extractCause[ChiselException] {
ChiselStage.elaborate {
new Module {
val io = IO(new Bundle {
val a = Input(Analog(32.W))
})
}
}
}
a[ChiselException] should be thrownBy extractCause[ChiselException] {
ChiselStage.elaborate {
new Module {
val io = IO(new Bundle {
val a = Output(Analog(32.W))
})
}
}
}
}
it should "be flippable" in {
ChiselStage.elaborate {
new Module {
val io = IO(new Bundle {
val a = Flipped(Analog(32.W))
})
}
}
}
// There is no binding on the type of a memory
// Should this be an error?
ignore should "NOT be a legal type for Mem" in {
a[ChiselException] should be thrownBy extractCause[ChiselException] {
ChiselStage.elaborate {
new Module {
val io = IO(new Bundle {})
val mem = Mem(16, Analog(32.W))
}
}
}
}
it should "NOT be bindable to Mem ports" in {
a[ChiselException] should be thrownBy extractCause[ChiselException] {
ChiselStage.elaborate {
new Module {
val io = IO(new Bundle {})
val mem = Mem(16, Analog(32.W))
val port = mem(5.U)
}
}
}
}
// TODO This should probably be caught in Chisel
// Also note this relies on executing Firrtl from Chisel directly
it should "NOT be connectable to UInts" in {
a[Exception] should be thrownBy {
runTester {
new BasicTester {
val uint = WireDefault(0.U(32.W))
val sint = Wire(Analog(32.W))
sint := uint
}
}
}
}
it should "work with 2 blackboxes bulk connected" in {
assertTesterPasses(
new AnalogTester {
val mod = Module(new AnalogReaderBlackBox)
mod.io.bus <> writer.io.bus
check(mod)
},
Seq("/chisel3/AnalogBlackBox.v"),
TesterDriver.verilatorOnly
)
}
it should "error if any bulk connected more than once" in {
a[ChiselException] should be thrownBy extractCause[ChiselException] {
ChiselStage.elaborate(new Module {
val io = IO(new Bundle {})
val wires = List.fill(3)(Wire(Analog(32.W)))
wires(0) <> wires(1)
wires(0) <> wires(2)
})
}
a[ChiselException] should be thrownBy extractCause[ChiselException] {
ChiselStage.elaborate(new Module {
val io = IO(new Bundle {})
val wires = List.fill(2)(Wire(Analog(32.W)))
wires(0) <> DontCare
wires(0) <> wires(1)
})
}
}
it should "allow DontCare connection" in {
ChiselStage.elaborate(new Module {
val io = IO(new Bundle {
val a = Analog(1.W)
})
io.a := DontCare
})
ChiselStage.elaborate(new Module {
val io = IO(new Bundle {
val a = Analog(1.W)
})
io.a <> DontCare
})
}
it should "work in bidirectional Aggregate wires" in {
class MyBundle extends Bundle {
val x = Input(UInt(8.W))
val y = Analog(8.W)
}
ChiselStage.elaborate(new Module {
val io = IO(new Bundle {
val a = new MyBundle
})
val w = Wire(new MyBundle)
w <> io.a
})
ChiselStage.elaborate(new Module {
val io = IO(new Bundle {
val a = Vec(1, new MyBundle)
})
val w = Wire(Vec(1, new MyBundle))
w <> io.a
})
}
it should "work with 3 blackboxes attached" in {
assertTesterPasses(
new AnalogTester {
val mods = Seq.fill(2)(Module(new AnalogReaderBlackBox))
attach(writer.io.bus, mods(0).io.bus, mods(1).io.bus)
mods.foreach(check(_))
},
Seq("/chisel3/AnalogBlackBox.v"),
TesterDriver.verilatorOnly
)
}
it should "work with 3 blackboxes separately attached via a wire" in {
assertTesterPasses(
new AnalogTester {
val mods = Seq.fill(2)(Module(new AnalogReaderBlackBox))
val busWire = Wire(Analog(32.W))
attach(busWire, writer.io.bus)
attach(busWire, mods(0).io.bus)
attach(mods(1).io.bus, busWire)
mods.foreach(check(_))
},
Seq("/chisel3/AnalogBlackBox.v"),
TesterDriver.verilatorOnly
)
}
// This does not currently work in Verilator unless Firrtl does constant prop and dead code
// elimination on these wires
ignore should "work with intermediate wires attached to each other" in {
assertTesterPasses(
new AnalogTester {
val mod = Module(new AnalogReaderBlackBox)
val busWire = Seq.fill(2)(Wire(Analog(32.W)))
attach(busWire(0), writer.io.bus)
attach(busWire(1), mod.io.bus)
attach(busWire(0), busWire(1))
check(mod)
},
Seq("/chisel3/AnalogBlackBox.v"),
TesterDriver.verilatorOnly
)
}
it should "work with blackboxes at different levels of the module hierarchy" in {
assertTesterPasses(
new AnalogTester {
val mods = Seq(Module(new AnalogReaderBlackBox), Module(new AnalogReaderWrapper))
val busWire = Wire(writer.io.bus.cloneType)
attach(writer.io.bus, mods(0).bus, mods(1).bus)
mods.foreach(check(_))
},
Seq("/chisel3/AnalogBlackBox.v"),
TesterDriver.verilatorOnly
)
}
// This does not currently work in Verilator, but does work in VCS
ignore should "support two analog ports in the same module" in {
assertTesterPasses(
new AnalogTester {
val reader = Module(new AnalogReaderBlackBox)
val connector = Module(new AnalogConnector)
connector.io.bus1 <> writer.io.bus
reader.io.bus <> connector.io.bus2
check(reader)
},
Seq("/chisel3/AnalogBlackBox.v"),
TesterDriver.verilatorOnly
)
}
it should "NOT support conditional connection of analog types" in {
a[ChiselException] should be thrownBy {
assertTesterPasses(
new AnalogTester {
val mod = Module(new AnalogReaderBlackBox)
when(cycle > 3.U) {
mod.io.bus <> writer.io.bus
}
check(mod)
},
Seq("/chisel3/AnalogBlackBox.v")
)
}
}
it should "work with Vecs of Analog" in {
assertTesterPasses(
new AnalogTester {
val mod = Module(new VecAnalogReaderWrapper)
mod.bus <> writer.io.bus
check(mod)
},
Seq("/chisel3/AnalogBlackBox.v"),
TesterDriver.verilatorOnly
)
}
it should "work with Vecs of Bundles of Analog" in {
assertTesterPasses(
new AnalogTester {
val mod = Module(new VecBundleAnalogReaderWrapper)
mod.bus <> writer.io.bus
check(mod)
},
Seq("/chisel3/AnalogBlackBox.v"),
TesterDriver.verilatorOnly
)
}
}
|