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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
// SPDX-License-Identifier: Apache-2.0
package firrtlTests
import firrtl._
import firrtl.annotations._
import firrtl.testutils.FirrtlCheckers.{containLine, containLines}
import firrtl.testutils.FirrtlFlatSpec
import firrtlTests.execution._
class MemInitSpec extends FirrtlFlatSpec {
def compile(circuit: String, annos: AnnotationSeq): CircuitState = {
(new VerilogCompiler).compileAndEmit(CircuitState(parse(circuit), ChirrtlForm, annos))
}
def basicTest(tpe: String = "UInt<32>"): String =
s"""
|circuit MemTest:
| module MemTest:
| input clock : Clock
| input rAddr : UInt<5>
| input rEnable : UInt<1>
| input wAddr : UInt<5>
| input wData : $tpe
| input wEnable : UInt<1>
| output rData : $tpe
|
| mem m:
| data-type => $tpe
| depth => 32
| reader => r
| writer => w
| read-latency => 1
| write-latency => 1
| read-under-write => new
|
| m.r.clk <= clock
| m.r.addr <= rAddr
| m.r.en <= rEnable
| rData <= m.r.data
|
| m.w.clk <= clock
| m.w.addr <= wAddr
| m.w.en <= wEnable
| m.w.data <= wData
| m.w.mask is invalid
|
|""".stripMargin
val mRef = CircuitTarget("MemTest").module("MemTest").ref("m")
"NoAnnotation" should "create a randomized initialization" in {
val annos = Seq()
val result = compile(basicTest(), annos)
result should containLine(" m[initvar] = _RAND_0[31:0];")
}
"MemoryRandomInitAnnotation" should "create a randomized initialization" in {
val annos = Seq(MemoryRandomInitAnnotation(mRef))
val result = compile(basicTest(), annos)
result should containLine(" m[initvar] = _RAND_0[31:0];")
}
behavior.of("MemoryScalarInitAnnotation")
it should "create an initialization with all zeros" in {
val annos = Seq(MemoryScalarInitAnnotation(mRef, 0))
val result = compile(basicTest(), annos)
result should containLine(" m[initvar] = 0;")
}
Seq(1, 3, 30, 400, 12345).foreach { value =>
it should
s"create an initialization with all values set to $value" in {
val annos = Seq(MemoryScalarInitAnnotation(mRef, value))
val result = compile(basicTest(), annos)
result should containLine(s" m[initvar] = $value;")
}
}
it should "fail for a negative value" in {
assertThrows[EmitterException] {
compile(basicTest(), Seq(MemoryScalarInitAnnotation(mRef, -1)))
}
}
it should "fail for a value that is too large" in {
assertThrows[EmitterException] {
compile(basicTest(), Seq(MemoryScalarInitAnnotation(mRef, BigInt(1) << 32)))
}
}
behavior.of("MemoryArrayInitAnnotation")
it should "initialize all addresses" in {
val values = Seq.tabulate(32)(ii => 2 * ii + 5).map(BigInt(_))
val annos = Seq(MemoryArrayInitAnnotation(mRef, values))
val result = compile(basicTest(), annos)
values.zipWithIndex.foreach {
case (value, addr) =>
result should containLine(s" m[$addr] = $value;")
}
}
it should "fail for a negative value" in {
assertThrows[EmitterException] {
val values = Seq.tabulate(32)(_ => BigInt(-1))
compile(basicTest(), Seq(MemoryArrayInitAnnotation(mRef, values)))
}
}
it should "fail for a value that is too large" in {
assertThrows[EmitterException] {
val values = Seq.tabulate(32)(_ => BigInt(1) << 32)
compile(basicTest(), Seq(MemoryArrayInitAnnotation(mRef, values)))
}
}
it should "fail if the number of values is too small" in {
assertThrows[EmitterException] {
val values = Seq.tabulate(31)(_ => BigInt(1))
compile(basicTest(), Seq(MemoryArrayInitAnnotation(mRef, values)))
}
}
it should "fail if the number of values is too large" in {
assertThrows[EmitterException] {
val values = Seq.tabulate(33)(_ => BigInt(1))
compile(basicTest(), Seq(MemoryArrayInitAnnotation(mRef, values)))
}
}
it should "fail on Memory with Vector type" in {
val caught = intercept[Exception] {
val annos = Seq(MemoryScalarInitAnnotation(mRef, 0))
compile(basicTest("UInt<32>[2]"), annos)
}
assert(caught.getMessage.endsWith("Cannot initialize memory m of non ground type UInt<32>[2]"))
}
it should "fail on Memory with Bundle type" in {
val caught = intercept[Exception] {
val annos = Seq(MemoryScalarInitAnnotation(mRef, 0))
compile(basicTest("{real: SInt<10>, imag: SInt<10>}"), annos)
}
assert(
caught.getMessage.endsWith("Cannot initialize memory m of non ground type { real : SInt<10>, imag : SInt<10>}")
)
}
private def jsonAnno(name: String, suffix: String): String =
s"""[{"class": "firrtl.annotations.$name", "target": "~MemTest|MemTest>m"$suffix}]"""
behavior.of("MemoryInit load from JSON")
it should "work with MemoryRandomInitAnnotation" in {
val json = jsonAnno("MemoryRandomInitAnnotation", "")
val annos = JsonProtocol.deserialize(json)
assert(annos == Seq(MemoryRandomInitAnnotation(mRef)))
}
it should "work with MemoryScalarInitAnnotation" in {
val json = jsonAnno("MemoryScalarInitAnnotation", """, "value": 1234567890""")
val annos = JsonProtocol.deserialize(json)
assert(annos == Seq(MemoryScalarInitAnnotation(mRef, 1234567890)))
}
it should "work with MemoryArrayInitAnnotation" in {
val json = jsonAnno("MemoryArrayInitAnnotation", """, "values": [10000000000, 20000000000]""")
val annos = JsonProtocol.deserialize(json)
val largeSeq = Seq(BigInt("10000000000"), BigInt("20000000000"))
assert(annos == Seq(MemoryArrayInitAnnotation(mRef, largeSeq)))
}
behavior.of("MemoryFileInlineAnnotation")
it should "emit $readmemh for text.hex" in {
val annos = Seq(MemoryFileInlineAnnotation(mRef, filename = "text.hex"))
val result = compile(basicTest(), annos)
result should containLine("""$readmemh("text.hex", """ + mRef.name + """);""")
}
it should "emit $readmemb for text.bin" in {
val annos = Seq(MemoryFileInlineAnnotation(mRef, filename = "text.bin", hexOrBinary = MemoryLoadFileType.Binary))
val result = compile(basicTest(), annos)
result should containLine("""$readmemb("text.bin", """ + mRef.name + """);""")
}
it should "fail with blank filename" in {
assertThrows[Exception] {
compile(basicTest(), Seq(MemoryFileInlineAnnotation(mRef, filename = "")))
}
}
behavior.of("MemorySynthesisInitialization")
it should "emit readmem in `ifndef SYNTHESIS` block by default" in {
val annos = Seq(
MemoryFileInlineAnnotation(mRef, filename = "text.hex", hexOrBinary = MemoryLoadFileType.Hex)
)
val result = compile(basicTest(), annos)
result should containLines(
"""`endif // RANDOMIZE""",
"""$readmemh("text.hex", """ + mRef.name + """);""",
"""end // initial"""
)
}
it should "emit readmem outside `ifndef SYNTHESIS` block with MemorySynthInit annotation" in {
val annos = Seq(
MemoryFileInlineAnnotation(mRef, filename = "text.hex", hexOrBinary = MemoryLoadFileType.Hex)
) ++ Seq(MemorySynthInit)
val result = compile(basicTest(), annos)
result should containLines(
"""`endif // SYNTHESIS""",
"""initial begin""",
"""$readmemh("text.hex", """ + mRef.name + """);""",
"""end"""
)
}
it should "emit MemoryScalarInit outside `ifndef SYNTHESIS` block with MemorySynthInit annotation" in {
val annos = Seq(MemoryScalarInitAnnotation(mRef, 0), MemorySynthInit)
val result = compile(basicTest(), annos)
result should containLines(
"""`endif // SYNTHESIS""",
"""initial begin""",
""" for (initvar = 0; initvar < 32; initvar = initvar+1)""",
""" m[initvar] = 0;""",
"""end"""
)
}
it should "always emit MemoryRandomInit inside `ifndef SYNTHESIS` block even with MemorySynthInit annotation" in {
// randomization should never make it to synthesis!
val annos = Seq(MemoryRandomInitAnnotation(mRef), MemorySynthInit)
val result = compile(basicTest(), annos)
result shouldNot containLines(
"""`endif // SYNTHESIS""",
"""initial begin""",
""" for (initvar = 0; initvar < 32; initvar = initvar+1)""",
""" m[initvar] = _RAND_0[31:0];""",
"""end"""
)
result should containLines(
"""`ifdef RANDOMIZE_MEM_INIT""",
"""_RAND_0 = {1{`RANDOM}};""",
""" for (initvar = 0; initvar < 32; initvar = initvar+1)""",
""" m[initvar] = _RAND_0[31:0];"""
)
}
it should "emit MemoryArrayInit outside `ifndef SYNTHESIS` block with MemorySynthInit annotation" in {
val values = Seq.tabulate(32)(ii => 2 * ii + 5).map(BigInt(_))
val annos = Seq(MemoryArrayInitAnnotation(mRef, values), MemorySynthInit)
val result = compile(basicTest(), annos)
val expInit = values.zipWithIndex.map { case (value, addr) => s" m[$addr] = $value;" }
result should containLines(
((Seq("""`endif // SYNTHESIS""", """initial begin""") ++
expInit) :+
"""end"""): _*
)
}
it should "emit readmem inside `ifndef SYNTHESIS` block with MemoryNoSynthInit annotation" in {
val annos = Seq(
MemoryFileInlineAnnotation(mRef, filename = "text.hex", hexOrBinary = MemoryLoadFileType.Hex)
) ++ Seq(MemoryNoSynthInit)
val result = compile(basicTest(), annos)
result should containLines(
"""`endif // RANDOMIZE""",
"""$readmemh("text.hex", """ + mRef.name + """);""",
"""end // initial"""
)
}
/** Firrtl which contains a child memory module instantiated twice
* If deduplication occurs, the firrtl for the child module should appear only
* once
* Any non-local memory annotations bound to the 'm' memories in each child instance
* should properly deduplicate in order for them to be emitted in the Verilog
*/
def dedupTest =
s"""
|circuit Top:
| module Child:
| input clock : Clock
| input rAddr : UInt<5>
| input rEnable : UInt<1>
| input wAddr : UInt<5>
| input wData : UInt<8>
| input wEnable : UInt<1>
| output rData : UInt<8>
|
| mem m:
| data-type => UInt<8>
| depth => 32
| reader => r
| writer => w
| read-latency => 1
| write-latency => 1
| read-under-write => new
|
| m.r.clk <= clock
| m.r.addr <= rAddr
| m.r.en <= rEnable
| rData <= m.r.data
|
| m.w.clk <= clock
| m.w.addr <= wAddr
| m.w.en <= wEnable
| m.w.data <= wData
| m.w.mask is invalid
|
| module Top:
| input clock : Clock
| input rAddr : UInt<5>
| input rEnable : UInt<1>
| input wAddr : UInt<5>
| input wData : UInt<8>
| input wEnable : UInt<1>
| output rData : UInt<8>[2]
|
| inst c1 of Child
| c1.clock <= clock
| c1.rAddr <= rAddr
| c1.rEnable <= rEnable
| c1.wAddr <= wAddr
| c1.wData <= wData
| c1.wEnable <= wEnable
|
| inst c2 of Child
| c2.clock <= clock
| c2.rAddr <= rAddr
| c2.rEnable <= rEnable
| c2.wAddr <= wAddr
| c2.wData <= wData
| c2.wEnable <= wEnable
|
| rData[0] <= c1.rData
| rData[1] <= c2.rData
|""".stripMargin
// Absolute references to the memory objects in each child module
val child1MRef = CircuitTarget("Top").module("Top").instOf("c1", "Child").ref("m")
val child2MRef = CircuitTarget("Top").module("Top").instOf("c2", "Child").ref("m")
// Final deduplicated reference
val dedupedRef = CircuitTarget("Top").module("Child").ref("m")
behavior.of("MemoryInitDeduplication")
it should "allow MemoryRandomInitAnnotation to randomize memory in single deduped module" in {
val annos = Seq(
MemoryRandomInitAnnotation(child1MRef),
MemoryRandomInitAnnotation(child2MRef)
)
val result = compile(dedupTest, annos)
result should containLine(" m[initvar] = _RAND_0[7:0];")
}
it should "allow MemoryScalarInitAnnotation to initialize memory to 0 in deduped module" in {
val annos = Seq(
MemoryScalarInitAnnotation(child1MRef, value = 0),
MemoryScalarInitAnnotation(child2MRef, value = 0)
)
val result = compile(dedupTest, annos)
result should containLine(" m[initvar] = 0;")
}
it should "allow MemoryArrayInitAnnotation to initialize memory with array of values in deduped module" in {
val values = Seq.tabulate(32)(ii => 2 * ii + 5).map(BigInt(_))
val annos = Seq(
MemoryArrayInitAnnotation(child1MRef, values),
MemoryArrayInitAnnotation(child2MRef, values)
)
val result = compile(dedupTest, annos)
values.zipWithIndex.foreach {
case (value, addr) =>
result should containLine(s" m[$addr] = $value;")
}
}
it should "allow MemoryFileInlineAnnotation to emit $readmemh in deduped module" in {
val annos = Seq(
MemoryFileInlineAnnotation(child1MRef, filename = "text.hex"),
MemoryFileInlineAnnotation(child2MRef, filename = "text.hex")
)
val result = compile(dedupTest, annos)
result should containLine("""$readmemh("text.hex", """ + dedupedRef.name + """);""")
}
it should "fail dedup if not all instances have the MemoryFileInlineAnnotation" in {
val annos = Seq(
MemoryFileInlineAnnotation(child1MRef, filename = "text.hex")
)
assertThrows[FirrtlUserException] {
compile(dedupTest, annos)
}
}
it should "fail dedup if instances have different MemoryFileInlineAnnotation filenames" in {
val annos = Seq(
MemoryFileInlineAnnotation(child1MRef, filename = "text.hex"),
MemoryFileInlineAnnotation(child2MRef, filename = "text.bin")
)
assertThrows[FirrtlUserException] {
compile(dedupTest, annos)
}
}
}
abstract class MemInitExecutionSpec(values: Seq[Int], init: ReferenceTarget => Annotation)
extends SimpleExecutionTest
with VerilogExecution {
override val body: String =
s"""
|mem m:
| data-type => UInt<32>
| depth => ${values.length}
| reader => r
| read-latency => 1
| write-latency => 1
| read-under-write => new
|m.r.clk <= clock
|m.r.en <= UInt<1>(1)
|""".stripMargin
val mRef = CircuitTarget("dut").module("dut").ref("m")
override val customAnnotations: AnnotationSeq = Seq(init(mRef))
override def commands: Seq[SimpleTestCommand] = (Seq(-1) ++ values).zipWithIndex.map {
case (value, addr) =>
if (value == -1) { Seq(Poke("m.r.addr", addr)) }
else if (addr >= values.length) { Seq(Expect("m.r.data", value)) }
else { Seq(Poke("m.r.addr", addr), Expect("m.r.data", value)) }
}.flatMap(_ ++ Seq(Step(1)))
}
class MemScalarInit0ExecutionSpec
extends MemInitExecutionSpec(
Seq.tabulate(31)(_ => 0),
r => MemoryScalarInitAnnotation(r, 0)
) {}
class MemScalarInit17ExecutionSpec
extends MemInitExecutionSpec(
Seq.tabulate(31)(_ => 17),
r => MemoryScalarInitAnnotation(r, 17)
) {}
class MemArrayInitExecutionSpec
extends MemInitExecutionSpec(
Seq.tabulate(31)(ii => ii * 5 + 7),
r => MemoryArrayInitAnnotation(r, Seq.tabulate(31)(ii => ii * 5 + 7).map(BigInt(_)))
) {}
|