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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
|
// See LICENSE for license details.
package firrtlTests
import java.io._
import org.scalatest._
import org.scalatest.prop._
import firrtl._
import firrtl.annotations._
import firrtl.ir.Circuit
import firrtl.passes._
import firrtl.transforms.VerilogRename
import firrtl.Parser.IgnoreInfo
import FirrtlCheckers._
import firrtl.transforms.CombineCats
class DoPrimVerilog extends FirrtlFlatSpec {
"Xorr" should "emit correctly" in {
val compiler = new VerilogCompiler
val input =
"""circuit Xorr :
| module Xorr :
| input a: UInt<4>
| output b: UInt<1>
| b <= xorr(a)""".stripMargin
val check =
"""module Xorr(
| input [3:0] a,
| output b
|);
| assign b = ^a;
|endmodule
|""".stripMargin.split("\n") map normalized
executeTest(input, check, compiler)
}
"Andr" should "emit correctly" in {
val compiler = new VerilogCompiler
val input =
"""circuit Andr :
| module Andr :
| input a: UInt<4>
| output b: UInt<1>
| b <= andr(a)""".stripMargin
val check =
"""module Andr(
| input [3:0] a,
| output b
|);
| assign b = &a;
|endmodule
|""".stripMargin.split("\n") map normalized
executeTest(input, check, compiler)
}
"Orr" should "emit correctly" in {
val compiler = new VerilogCompiler
val input =
"""circuit Orr :
| module Orr :
| input a: UInt<4>
| output b: UInt<1>
| b <= orr(a)""".stripMargin
val check =
"""module Orr(
| input [3:0] a,
| output b
|);
| assign b = |a;
|endmodule
|""".stripMargin.split("\n") map normalized
executeTest(input, check, compiler)
}
"Not" should "emit correctly" in {
val compiler = new VerilogCompiler
val input =
"""circuit Not :
| module Not :
| input a: UInt<1>
| output b: UInt<1>
| b <= not(a)""".stripMargin
val check =
"""module Not(
| input a,
| output b
|);
| assign b = ~a;
|endmodule
|""".stripMargin.split("\n") map normalized
executeTest(input, check, compiler)
}
"inline Not" should "emit correctly" in {
val compiler = new VerilogCompiler
val input =
"""circuit InlineNot :
| module InlineNot :
| input a: UInt<1>
| input b: UInt<1>
| input c: UInt<4>
| output d: UInt<1>
| output e: UInt<1>
| output f: UInt<1>
| output g: UInt<1>
| d <= and(a, not(b))
| e <= or(a, not(b))
| f <= not(not(not(bits(c, 2, 2))))
| g <= mux(not(bits(c, 2, 2)), a, b)""".stripMargin
val check =
"""module InlineNot(
| input a,
| input b,
| input [3:0] c,
| output d,
| output e,
| output f,
| output g
|);
| wire _GEN_2;
| wire _GEN_4;
| assign d = a & ~b;
| assign e = a | ~b;
| assign _GEN_2 = c[2];
| assign _GEN_4 = _GEN_2;
| assign f = ~_GEN_4;
| assign g = _GEN_2 ? b : a;
|endmodule
|""".stripMargin.split("\n") map normalized
executeTest(input, check, compiler)
}
"Rem" should "emit correctly" in {
val compiler = new VerilogCompiler
val input =
"""circuit Test :
| module Test :
| input in : UInt<8>
| output out : UInt<1>
| out <= rem(in, UInt<1>("h1"))
|""".stripMargin
val check =
"""module Test(
| input [7:0] in,
| output out
|);
| wire [7:0] _GEN_0;
| assign out = _GEN_0[0];
| assign _GEN_0 = in % 8'h1;
|endmodule
|""".stripMargin.split("\n") map normalized
executeTest(input, check, compiler)
}
"nested cats" should "emit correctly" in {
val compiler = new MinimumVerilogCompiler
val input =
"""circuit Test :
| module Test :
| input in1 : UInt<1>
| input in2 : UInt<2>
| input in3 : UInt<3>
| input in4 : UInt<4>
| output out : UInt<10>
| out <= cat(in4, cat(in3, cat(in2, in1)))
|""".stripMargin
val check =
"""module Test(
| input in1,
| input [1:0] in2,
| input [2:0] in3,
| input [3:0] in4,
| output [9:0] out
|);
| wire [5:0] _GEN_1;
| assign out = {in4,_GEN_1};
| assign _GEN_1 = {in3,in2,in1};
|endmodule
|""".stripMargin.split("\n") map normalized
val finalState = compiler.compileAndEmit(CircuitState(parse(input), ChirrtlForm), Seq(new CombineCats()))
val lines = finalState.getEmittedCircuit.value split "\n" map normalized
for (e <- check) {
lines should contain (e)
}
}
}
class VerilogEmitterSpec extends FirrtlFlatSpec {
private def compile(input: String): CircuitState =
(new VerilogCompiler).compileAndEmit(CircuitState(parse(input), ChirrtlForm), List.empty)
private def compileBody(body: String): CircuitState = {
val str = """
|circuit Test :
| module Test :
|""".stripMargin + body.split("\n").mkString(" ", "\n ", "")
compile(str)
}
"Ports" should "emit with widths aligned and names aligned" in {
val compiler = new VerilogCompiler
val input =
"""circuit Test :
| module Test :
| input a : UInt<25000>
| output b : UInt
| input c : UInt<32>
| output d : UInt
| input e : UInt<1>
| input f : Analog<32>
| b <= a
| d <= add(c, e)
|""".stripMargin
val check = Seq(
" input [24999:0] a,",
" output [24999:0] b,",
" input [31:0] c,",
" output [32:0] d,",
" input e,",
" inout [31:0] f"
)
// We don't use executeTest because we care about the spacing in the result
val writer = new java.io.StringWriter
compiler.compile(CircuitState(parse(input), ChirrtlForm), writer)
val lines = writer.toString.split("\n")
for (c <- check) {
lines should contain (c)
}
}
"The Verilog Emitter" should "support Modules with no ports" in {
val compiler = new VerilogCompiler
val input =
"""circuit Test :
| module Test :
| wire x : UInt<32>
| x <= UInt(0)
""".stripMargin
compiler.compile(CircuitState(parse(input), ChirrtlForm), new java.io.StringWriter)
}
"AsClock" should "emit correctly" in {
val compiler = new VerilogCompiler
val input =
"""circuit Test :
| module Test :
| input in : UInt<1>
| output out : Clock
| out <= asClock(in)
|""".stripMargin
val check =
"""module Test(
| input in,
| output out
|);
| assign out = in;
|endmodule
|""".stripMargin.split("\n") map normalized
executeTest(input, check, compiler)
}
"The Verilog Emitter" should "support pads with width <= the width of the argument" in {
// We do just a few passes instead of using the VerilogCompiler to ensure that the pad actually
// reaches the VerilogEmitter and isn't removed by an optimization transform
val passes = Seq(
ToWorkingIR,
ResolveKinds,
InferTypes
)
def input(n: Int) =
s"""circuit Test :
| module Test :
| input in : UInt<8>
| output out : UInt<8>
| out <= pad(in, $n)
|""".stripMargin
for (w <- Seq(6, 8)) {
val circuit = passes.foldLeft(parse(input(w))) { case (c, p) => p.run(c) }
val state = CircuitState(circuit, LowForm, Seq(EmitCircuitAnnotation(classOf[VerilogEmitter])))
val emitter = new VerilogEmitter
val result = emitter.execute(state)
result should containLine ("assign out = in;")
}
}
"The verilog emitter" should "offer support for generating bindable forms of modules" in {
val emitter = new VerilogEmitter
val input =
"""circuit Test :
| module Test :
| input a : UInt<25000>
| output b : UInt
| input c : UInt<32>
| output d : UInt
| input e : UInt<1>
| input f : Analog<32>
| b <= a
| d <= add(c, e)
|""".stripMargin
val check =
"""
|module BindsToTest(
| input [24999:0] a,
| output [24999:0] b,
| input [31:0] c,
| output [32:0] d,
| input e,
| inout [31:0] f
|);
|
|$readmemh("file", memory);
|
|endmodule""".stripMargin.split("\n")
// We don't use executeTest because we care about the spacing in the result
val writer = new java.io.StringWriter
val initialState = CircuitState(parse(input), ChirrtlForm)
val compiler = new LowFirrtlCompiler()
val state = compiler.compile(initialState, Seq.empty)
val moduleMap = state.circuit.modules.map(m => m.name -> m).toMap
val module = state.circuit.modules.filter(module => module.name == "Test").collectFirst { case m: firrtl.ir.Module => m }.get
val renderer = emitter.getRenderer(module, moduleMap)(writer)
renderer.emitVerilogBind("BindsToTest",
"""
|$readmemh("file", memory);
|
|""".stripMargin)
val lines = writer.toString.split("\n")
val outString = writer.toString
// This confirms that the module io's were emitted
for (c <- check) {
lines should contain (c)
}
}
"Initial Blocks" should "be guarded by ifndef SYNTHESIS" in {
val input =
"""circuit Test :
| module Test :
| input clock : Clock
| input reset : AsyncReset
| input in : UInt<8>
| output out : UInt<8>
| reg r : UInt<8>, clock with : (reset => (reset, UInt(0)))
| r <= in
| out <= r
""".stripMargin
val state = CircuitState(parse(input), ChirrtlForm)
val result = (new VerilogCompiler).compileAndEmit(state, List())
result should containLines ("`ifndef SYNTHESIS", "initial begin")
result should containLines ("end // initial", "`endif // SYNTHESIS")
}
"Verilog name conflicts" should "be resolved" in {
val input =
"""|circuit parameter:
| module parameter:
| input always: UInt<1>
| output always$: UInt<1>
| inst assign of endmodule
| node always_ = not(always)
| node always__ = and(always_, assign.fork)
| always$ <= always__
| module endmodule:
| output fork: UInt<1>
| node const = add(UInt<4>("h1"), UInt<3>("h2"))
| fork <= const
|""".stripMargin
val check_firrtl =
"""|circuit parameter_:
| module parameter_:
| input always___: UInt<1>
| output always$: UInt<1>
| inst assign_ of endmodule_
| node always_ = not(always___)
| node always__ = and(always_, assign_.fork_)
| always$ <= always__
| module endmodule_:
| output fork_: UInt<1>
| node const_ = add(UInt<4>("h1"), UInt<3>("h2"))
| fork_ <= const_
|""".stripMargin
val state = CircuitState(parse(input), UnknownForm, Seq.empty, None)
val output = Seq( ToWorkingIR, ResolveKinds, InferTypes, new VerilogRename )
.foldLeft(state){ case (c, tx) => tx.runTransform(c) }
Seq( CheckHighForm )
.foldLeft(output.circuit){ case (c, tx) => tx.run(c) }
output.circuit.serialize should be (parse(check_firrtl).serialize)
}
behavior of "Register Updates"
they should "emit using 'else if' constructs" in {
val input =
s"""|circuit Foo:
| module Foo:
| input clock: Clock
| input sel: UInt<2>
| input in_0: UInt<1>
| input in_1: UInt<1>
| input in_2: UInt<1>
| input in_3: UInt<1>
| output out: UInt<1>
| reg tmp: UInt<1>, clock
| node _GEN_0 = mux(eq(sel, UInt<2>(2)), in_2, in_3)
| node _GEN_1 = mux(eq(sel, UInt<2>(1)), in_1, _GEN_0)
| tmp <= mux(eq(sel, UInt<2>(0)), in_0, _GEN_1)
| out <= tmp
|""".stripMargin
val circuit = Seq(ToWorkingIR, ResolveKinds, InferTypes).foldLeft(parse(input)) { case (c, p) => p.run(c) }
val state = CircuitState(circuit, LowForm, Seq(EmitCircuitAnnotation(classOf[VerilogEmitter])))
val result = (new VerilogEmitter).execute(state)
result should containLine ("if (sel == 2'h0) begin")
result should containLine ("end else if (sel == 2'h1) begin" )
result should containLine ("end else if (sel == 2'h2) begin")
result should containLine ("end else begin")
}
they should "ignore self assignments in false conditions" in {
val input =
s"""|circuit Foo:
| module Foo:
| input clock: Clock
| input sel: UInt<1>
| input in: UInt<1>
| output out: UInt<1>
| reg tmp: UInt<1>, clock
| tmp <= mux(eq(sel, UInt<1>(0)), in, tmp)
| out <= tmp
|""".stripMargin
val circuit = Seq(ToWorkingIR, ResolveKinds, InferTypes).foldLeft(parse(input)) { case (c, p) => p.run(c) }
val state = CircuitState(circuit, LowForm, Seq(EmitCircuitAnnotation(classOf[VerilogEmitter])))
val result = (new VerilogEmitter).execute(state)
result should not (containLine ("tmp <= tmp"))
}
they should "ignore self assignments in true conditions and invert condition" in {
val input =
s"""|circuit Foo:
| module Foo:
| input clock: Clock
| input sel: UInt<1>
| input in: UInt<1>
| output out: UInt<1>
| reg tmp: UInt<1>, clock
| tmp <= mux(eq(sel, UInt<1>(0)), tmp, in)
| out <= tmp
|""".stripMargin
val circuit = Seq(ToWorkingIR, ResolveKinds, InferTypes).foldLeft(parse(input)) { case (c, p) => p.run(c) }
val state = CircuitState(circuit, LowForm, Seq(EmitCircuitAnnotation(classOf[VerilogEmitter])))
val result = (new VerilogEmitter).execute(state)
result should containLine ("if (!(sel == 1'h0)) begin")
result should not (containLine ("tmp <= tmp"))
}
they should "ignore self assignments in both true and false conditions" in {
val input =
s"""|circuit Foo:
| module Foo:
| input clock: Clock
| input sel: UInt<1>
| input in: UInt<1>
| output out: UInt<1>
| reg tmp: UInt<1>, clock
| tmp <= mux(eq(sel, UInt<1>(0)), tmp, tmp)
| out <= tmp
|""".stripMargin
val circuit = Seq(ToWorkingIR, ResolveKinds, InferTypes).foldLeft(parse(input)) { case (c, p) => p.run(c) }
val state = CircuitState(circuit, LowForm, Seq(EmitCircuitAnnotation(classOf[VerilogEmitter])))
val result = (new VerilogEmitter).execute(state)
result should not (containLine ("tmp <= tmp"))
result should not (containLine ("always @(posedge clock) begin"))
}
they should "properly indent muxes in either the true or false condition" in {
val input =
s"""|circuit Foo:
| module Foo:
| input clock: Clock
| input reset: UInt<1>
| input sel: UInt<3>
| input in_0: UInt<1>
| input in_1: UInt<1>
| input in_2: UInt<1>
| input in_3: UInt<1>
| input in_4: UInt<1>
| input in_5: UInt<1>
| input in_6: UInt<1>
| input in_7: UInt<1>
| input in_8: UInt<1>
| input in_9: UInt<1>
| input in_10: UInt<1>
| input in_11: UInt<1>
| output out: UInt<1>
| reg tmp: UInt<0>, clock
| node m7 = mux(eq(sel, UInt<3>(7)), in_8, in_9)
| node m6 = mux(eq(sel, UInt<3>(6)), in_6, in_7)
| node m5 = mux(eq(sel, UInt<3>(5)), in_4, in_5)
| node m4 = mux(eq(sel, UInt<3>(4)), in_2, in_3)
| node m3 = mux(eq(sel, UInt<3>(3)), in_0, in_1)
| node m2 = mux(eq(sel, UInt<3>(2)), m6, m7)
| node m1 = mux(eq(sel, UInt<3>(1)), m4, m5)
| node m0 = mux(eq(sel, UInt<3>(0)), m2, m3)
| tmp <= mux(reset, m0, m1)
| out <= tmp
|""".stripMargin
val circuit = Seq(ToWorkingIR, ResolveKinds, InferTypes).foldLeft(parse(input)) { case (c, p) => p.run(c) }
val state = CircuitState(circuit, LowForm, Seq(EmitCircuitAnnotation(classOf[VerilogEmitter])))
val result = (new VerilogEmitter).execute(state)
/* The Verilog string is used to check for no whitespace between "else" and "if". */
val verilogString = result.getEmittedCircuit.value
result should containLine ("if (sel == 3'h0) begin")
verilogString should include ("end else if (sel == 3'h1) begin")
result should containLine ("if (sel == 3'h2) begin")
verilogString should include ("end else if (sel == 3'h3) begin")
result should containLine ("if (sel == 3'h4) begin")
verilogString should include ("end else if (sel == 3'h5) begin")
result should containLine ("if (sel == 3'h6) begin")
verilogString should include ("end else if (sel == 3'h7) begin")
result should containLine ("tmp <= in_0;")
result should containLine ("tmp <= in_1;")
result should containLine ("tmp <= in_2;")
result should containLine ("tmp <= in_3;")
result should containLine ("tmp <= in_4;")
result should containLine ("tmp <= in_5;")
result should containLine ("tmp <= in_6;")
result should containLine ("tmp <= in_7;")
result should containLine ("tmp <= in_8;")
result should containLine ("tmp <= in_9;")
}
"SInt addition" should "have casts" in {
val compiler = new VerilogCompiler
val result = compileBody(
"""input x : SInt<4>
|input y : SInt<4>
|output z : SInt
|z <= add(x, y)
|""".stripMargin
)
result should containLine("assign z = $signed(x) + $signed(y);")
}
it should "NOT cast SInt literals" in {
val compiler = new VerilogCompiler
val result = compileBody(
"""input x : SInt<4>
|output z : SInt
|z <= add(x, SInt(-1))
|""".stripMargin
)
result should containLine("assign z = $signed(x) + -4'sh1;")
}
it should "inline asSInt casts" in {
val compiler = new VerilogCompiler
val result = compileBody(
"""input x : UInt<4>
|input y : UInt<4>
|output z : SInt
|node _T_1 = asSInt(x)
|z <= add(_T_1, asSInt(y))
|""".stripMargin
)
result should containLine("assign z = $signed(x) + $signed(y);")
}
"Verilog Emitter" should "drop asUInt casts on Clocks" in {
val compiler = new VerilogCompiler
val result = compileBody(
"""input x : Clock
|input y : Clock
|output z : UInt<1>
|node _T_1 = asUInt(x)
|z <= eq(_T_1, asUInt(y))
|""".stripMargin
)
result should containLine("assign z = x == y;")
}
it should "drop asClock casts on UInts" in {
val compiler = new VerilogCompiler
val result = compileBody(
"""input x : UInt<1>
|input y : UInt<1>
|output z : Clock
|node _T_1 = eq(x, y)
|z <= asClock(_T_1)
|""".stripMargin
)
result should containLine("assign z = x == y;")
}
it should "drop asUInt casts on AsyncResets" in {
val compiler = new VerilogCompiler
val result = compileBody(
"""input x : AsyncReset
|input y : AsyncReset
|output z : UInt<1>
|node _T_1 = asUInt(x)
|z <= eq(_T_1, asUInt(y))
|""".stripMargin
)
result should containLine("assign z = x == y;")
}
it should "drop asAsyncReset casts on UInts" in {
val compiler = new VerilogCompiler
val result = compileBody(
"""input x : UInt<1>
|input y : UInt<1>
|output z : AsyncReset
|node _T_1 = eq(x, y)
|z <= asAsyncReset(_T_1)
|""".stripMargin
)
result should containLine("assign z = x == y;")
}
}
class VerilogDescriptionEmitterSpec extends FirrtlFlatSpec {
"Port descriptions" should "emit aligned comments on the line above" in {
val compiler = new VerilogCompiler
val input =
"""circuit Test :
| module Test :
| input a : UInt<1>
| input b : UInt<1>
| output c : UInt<1>
| c <= add(a, b)
|""".stripMargin
val check = Seq(
""" /* multi
| * line
| */
| input a,""".stripMargin,
""" // single line
| input b,""".stripMargin
)
// We don't use executeTest because we care about the spacing in the result
val modName = ModuleName("Test", CircuitName("Test"))
val annos = Seq(
DescriptionAnnotation(ComponentName("a", modName), "multi\nline"),
DescriptionAnnotation(ComponentName("b", modName), "single line"))
val finalState = compiler.compileAndEmit(CircuitState(parse(input), ChirrtlForm, annos), Seq.empty)
val output = finalState.getEmittedCircuit.value
for (c <- check) {
assert(output.contains(c))
}
}
"Declaration descriptions" should "emit aligned comments on the line above" in {
val compiler = new VerilogCompiler
val input =
"""circuit Test :
| module Test :
| input clock : Clock
| input a : UInt<1>
| input b : UInt<1>
| output c : UInt<1>
|
| wire d : UInt<1>
| d <= add(a, b)
|
| reg e : UInt<1>, clock
| e <= or(a, b)
|
| node f = and(a, b)
| c <= add(d, add(e, f))
|""".stripMargin
val check = Seq(
""" /* multi
| * line
| */
| wire d;""".stripMargin,
""" /* multi
| * line
| */
| reg e;""".stripMargin,
""" // single line
| wire f;""".stripMargin
)
// We don't use executeTest because we care about the spacing in the result
val modName = ModuleName("Test", CircuitName("Test"))
val annos = Seq(
DescriptionAnnotation(ComponentName("d", modName), "multi\nline"),
DescriptionAnnotation(ComponentName("e", modName), "multi\nline"),
DescriptionAnnotation(ComponentName("f", modName), "single line"))
val finalState = compiler.compileAndEmit(CircuitState(parse(input), ChirrtlForm, annos), Seq.empty)
val output = finalState.getEmittedCircuit.value
for (c <- check) {
assert(output.contains(c))
}
}
"Module descriptions" should "emit aligned comments on the line above" in {
val compiler = new VerilogCompiler
val input =
"""circuit Test :
| module Test :
| input clock : Clock
| input a : UInt<1>
| input b : UInt<1>
| output c : UInt<1>
|
| wire d : UInt<1>
| d <= add(a, b)
|
| reg e : UInt<1>, clock
| e <= or(a, b)
|
| node f = and(a, b)
| c <= add(d, add(e, f))
|""".stripMargin
val check = Seq(
"""/* multi
| * line
| */
|module Test(""".stripMargin
)
// We don't use executeTest because we care about the spacing in the result
val modName = ModuleName("Test", CircuitName("Test"))
val annos = Seq(DescriptionAnnotation(modName, "multi\nline"))
val finalState = compiler.compileAndEmit(CircuitState(parse(input), ChirrtlForm, annos), Seq.empty)
val output = finalState.getEmittedCircuit.value
for (c <- check) {
assert(output.contains(c))
}
}
"Multiple descriptions" should "be combined" in {
val compiler = new VerilogCompiler
val input =
"""circuit Test :
| module Test :
| input a : UInt<1>
| input b : UInt<1>
| output c : UInt<1>
|
| wire d : UInt<1>
| d <= add(a, b)
|
| c <= add(a, d)
|""".stripMargin
val check = Seq(
"""/* line1
| *
| * line2
| */
|module Test(""".stripMargin,
""" /* line3
| *
| * line4
| */
| input a,""".stripMargin,
""" /* line5
| *
| * line6
| */
| wire d;""".stripMargin
)
// We don't use executeTest because we care about the spacing in the result
val modName = ModuleName("Test", CircuitName("Test"))
val annos = Seq(
DescriptionAnnotation(modName, "line1"),
DescriptionAnnotation(modName, "line2"),
DescriptionAnnotation(ComponentName("a", modName), "line3"),
DescriptionAnnotation(ComponentName("a", modName), "line4"),
DescriptionAnnotation(ComponentName("d", modName), "line5"),
DescriptionAnnotation(ComponentName("d", modName), "line6")
)
val writer = new java.io.StringWriter
val finalState = compiler.compileAndEmit(CircuitState(parse(input), ChirrtlForm, annos), Seq.empty)
val output = finalState.getEmittedCircuit.value
for (c <- check) {
assert(output.contains(c))
}
}
}
|