forked from steveicarus/iverilog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelaborate.cc
7724 lines (6622 loc) · 236 KB
/
elaborate.cc
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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1998-2024 Stephen Williams ([email protected])
* Copyright CERN 2013 / Stephen Williams ([email protected])
*
* This source code is free software; you can redistribute it
* and/or modify it in source code form under the terms of the GNU
* General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
# include "config.h"
/*
* Elaboration takes as input a complete parse tree and the name of a
* root module, and generates as output the elaborated design. This
* elaborated design is presented as a Module, which does not
* reference any other modules. It is entirely self contained.
*/
# include <algorithm>
# include <typeinfo>
# include <climits>
# include <cstdlib>
# include <cstring>
# include <iostream>
# include <sstream>
# include <list>
# include "pform.h"
# include "PClass.h"
# include "PEvent.h"
# include "PGenerate.h"
# include "PPackage.h"
# include "PScope.h"
# include "PSpec.h"
# include "PTimingCheck.h"
# include "netlist.h"
# include "netenum.h"
# include "netvector.h"
# include "netdarray.h"
# include "netparray.h"
# include "netscalar.h"
# include "netclass.h"
# include "netmisc.h"
# include "util.h"
# include "parse_api.h"
# include "compiler.h"
# include "ivl_assert.h"
# include "map_named_args.h"
using namespace std;
// Implemented in elab_scope.cc
extern void set_scope_timescale(Design*des, NetScope*scope, PScope*pscope);
void PGate::elaborate(Design*, NetScope*) const
{
cerr << "internal error: what kind of gate? " <<
typeid(*this).name() << endl;
}
unsigned PGate::calculate_array_size_(Design*des, NetScope*scope,
long&high, long&low) const
{
if (ranges_ && ranges_->size() > 1) {
if (gn_system_verilog()) {
cerr << get_fileline() << ": sorry: Multi-dimensional"
<< " arrays of instances are not yet supported." << endl;
} else {
cerr << get_fileline() << ": error: Multi-dimensional"
<< " arrays of instances require SystemVerilog." << endl;
}
des->errors += 1;
return 0;
}
unsigned size = 1;
high = 0;
low = 0;
if (ranges_) {
if (!evaluate_range(des, scope, this, ranges_->front(), high, low))
return 0;
if (high > low)
size = high - low + 1;
else
size = low - high + 1;
if (debug_elaborate) {
cerr << get_fileline() << ": debug: PGate: Make array "
<< "[" << high << ":" << low << "]" << " of "
<< size << " instances for " << get_name() << endl;
}
}
return size;
}
/*
* Elaborate the continuous assign. (This is *not* the procedural
* assign.) Elaborate the lvalue and rvalue, and do the assignment.
*/
void PGAssign::elaborate(Design*des, NetScope*scope) const
{
ivl_assert(*this, scope);
NetExpr* rise_time, *fall_time, *decay_time;
eval_delays(des, scope, rise_time, fall_time, decay_time, true);
ivl_drive_t drive0 = strength0();
ivl_drive_t drive1 = strength1();
ivl_assert(*this, pin(0));
ivl_assert(*this, pin(1));
/* Elaborate the l-value. */
NetNet*lval = pin(0)->elaborate_lnet(des, scope);
if (lval == 0) {
return;
}
// If this turns out to be an assignment to an unpacked array,
// then handle that special case elsewhere.
if (lval->pin_count() > 1) {
elaborate_unpacked_array_(des, scope, lval);
return;
}
ivl_assert(*this, lval->pin_count() == 1);
if (debug_elaborate) {
cerr << get_fileline() << ": PGAssign::elaborate: elaborated l-value"
<< " width=" << lval->vector_width()
<< ", pin_count=" << lval->pin_count() << endl;
}
NetExpr*rval_expr = elaborate_rval_expr(des, scope, lval->net_type(), pin(1));
if (rval_expr == 0) {
cerr << get_fileline() << ": error: Unable to elaborate r-value: "
<< *pin(1) << endl;
des->errors += 1;
return;
}
NetNet*rval = rval_expr->synthesize(des, scope, rval_expr);
if (rval == 0) {
cerr << get_fileline() << ": internal error: "
<< "Failed to synthesize expression: " << *rval_expr << endl;
des->errors += 1;
return;
}
if (debug_elaborate) {
cerr << get_fileline() << ": debug: PGAssign: elaborated r-value"
<< " width="<< rval->vector_width()
<< ", type="<< rval->data_type()
<< ", expr=" << *rval_expr << endl;
}
ivl_assert(*this, lval && rval);
ivl_assert(*this, rval->pin_count() == 1);
// Detect the case that the rvalue-expression is a simple
// expression. In this case, we will need to create a driver
// (later) to carry strengths.
bool need_driver_flag = false;
if (dynamic_cast<NetESignal*>(rval_expr) ||!rval->is_linked())
need_driver_flag = true;
// expression elaboration should have caused the rval width to
// match the l-value by now.
if (rval->vector_width() < lval->vector_width()) {
cerr << get_fileline() << ": internal error: "
<< "lval-rval width mismatch: "
<< "rval->vector_width()==" << rval->vector_width()
<< ", lval->vector_width()==" << lval->vector_width() << endl;
}
ivl_assert(*this, rval->vector_width() >= lval->vector_width());
/* If the r-value insists on being larger than the l-value,
use a part select to chop it down down to size. */
if (lval->vector_width() < rval->vector_width()) {
NetPartSelect*tmp = new NetPartSelect(rval, 0,lval->vector_width(),
NetPartSelect::VP);
des->add_node(tmp);
tmp->set_line(*this);
netvector_t*osig_vec = new netvector_t(rval->data_type(),
lval->vector_width()-1,0);
NetNet*osig = new NetNet(scope, scope->local_symbol(),
NetNet::TRI, osig_vec);
osig->set_line(*this);
osig->local_flag(true);
connect(osig->pin(0), tmp->pin(0));
rval = osig;
need_driver_flag = false;
}
/* When we are given a non-default strength value and if the drive
* source is a bit, part, indexed select or a concatenation we need
* to add a driver (BUFZ) to convey the strength information. */
if ((drive0 != IVL_DR_STRONG || drive1 != IVL_DR_STRONG) &&
((dynamic_cast<NetESelect*>(rval_expr)) ||
(dynamic_cast<NetEConcat*>(rval_expr)))) {
need_driver_flag = true;
}
if (need_driver_flag) {
NetBUFZ*driver = new NetBUFZ(scope, scope->local_symbol(),
rval->vector_width(), false);
driver->set_line(*this);
des->add_node(driver);
connect(rval->pin(0), driver->pin(1));
netvector_t*tmp_vec = new netvector_t(rval->data_type(),
rval->vector_width()-1,0);
NetNet*tmp = new NetNet(scope, scope->local_symbol(),
NetNet::WIRE, tmp_vec);
tmp->set_line(*this);
tmp->local_flag(true);
connect(driver->pin(0), tmp->pin(0));
rval = tmp;
}
/* Set the drive and delays for the r-val. */
if (drive0 != IVL_DR_STRONG || drive1 != IVL_DR_STRONG)
rval->pin(0).drivers_drive(drive0, drive1);
if (rise_time || fall_time || decay_time)
rval->pin(0).drivers_delays(rise_time, fall_time, decay_time);
connect(lval->pin(0), rval->pin(0));
if (lval->local_flag())
delete lval;
}
NetNet *elaborate_unpacked_array(Design *des, NetScope *scope, const LineInfo &loc,
const NetNet *lval, PExpr *expr)
{
NetNet *expr_net;
PEIdent* ident = dynamic_cast<PEIdent*> (expr);
if (!ident) {
if (dynamic_cast<PEConcat*> (expr)) {
cout << loc.get_fileline() << ": sorry: Continuous assignment"
<< " of array concatenation is not yet supported."
<< endl;
des->errors++;
return nullptr;
} else if (dynamic_cast<PEAssignPattern*> (expr)) {
auto net_expr = elaborate_rval_expr(des, scope, lval->array_type(), expr);
expr_net = net_expr->synthesize(des, scope, net_expr);
} else {
cout << loc.get_fileline() << ": error: Can not assign"
<< " non-array expression `" << *expr << "` to array."
<< endl;
des->errors++;
return nullptr;
}
} else {
expr_net = ident->elaborate_unpacked_net(des, scope);
}
if (!expr_net)
return nullptr;
auto const &lval_dims = lval->unpacked_dims();
auto const &expr_dims = expr_net->unpacked_dims();
if (expr_dims.empty()) {
cerr << loc.get_fileline() << ": error: Can not assign"
<< " non-array identifier `" << *expr << "` to array."
<< endl;
des->errors++;
return nullptr;
}
if (!netrange_equivalent(lval_dims, expr_dims)) {
cerr << loc.get_fileline() << ": error: Unpacked dimensions"
<< " are not compatible in array assignment." << endl;
des->errors++;
return nullptr;
}
if (!lval->net_type()->type_equivalent(expr_net->net_type())) {
cerr << loc.get_fileline() << ": error: Element types are not"
<< " compatible in array assignment." << endl;
des->errors++;
return nullptr;
}
return expr_net;
}
void PGAssign::elaborate_unpacked_array_(Design*des, NetScope*scope, NetNet*lval) const
{
NetNet *rval_net = elaborate_unpacked_array(des, scope, *this, lval, pin(1));
if (rval_net)
assign_unpacked_with_bufz(des, scope, lval, lval, rval_net);
}
void PGBuiltin::calculate_gate_and_lval_count_(unsigned&gate_count,
unsigned&lval_count) const
{
switch (type()) {
case BUF:
case NOT:
if (pin_count() > 2) gate_count = pin_count() - 1;
else gate_count = 1;
lval_count = gate_count;
break;
case PULLDOWN:
case PULLUP:
gate_count = pin_count();
lval_count = gate_count;
break;
case TRAN:
case RTRAN:
case TRANIF0:
case TRANIF1:
case RTRANIF0:
case RTRANIF1:
gate_count = 1;
lval_count = 2;
break;
default:
gate_count = 1;
lval_count = 1;
break;
}
}
NetNode* PGBuiltin::create_gate_for_output_(Design*des, NetScope*scope,
perm_string inst_name,
unsigned instance_width) const
{
NetNode*gate = 0;
switch (type()) {
case AND:
if (pin_count() < 2) {
cerr << get_fileline() << ": error: the AND "
"primitive must have an input." << endl;
des->errors += 1;
} else {
gate = new NetLogic(scope, inst_name, pin_count(),
NetLogic::AND, instance_width);
}
break;
case BUF:
if (pin_count() < 2) {
cerr << get_fileline() << ": error: the BUF "
"primitive must have an input." << endl;
des->errors += 1;
} else {
gate = new NetLogic(scope, inst_name, 2,
NetLogic::BUF, instance_width);
}
break;
case BUFIF0:
if (pin_count() != 3) {
cerr << get_fileline() << ": error: the BUFIF0 "
"primitive must have three arguments." << endl;
des->errors += 1;
} else {
gate = new NetLogic(scope, inst_name, pin_count(),
NetLogic::BUFIF0, instance_width);
}
break;
case BUFIF1:
if (pin_count() != 3) {
cerr << get_fileline() << ": error: the BUFIF1 "
"primitive must have three arguments." << endl;
des->errors += 1;
} else {
gate = new NetLogic(scope, inst_name, pin_count(),
NetLogic::BUFIF1, instance_width);
}
break;
case CMOS:
if (pin_count() != 4) {
cerr << get_fileline() << ": error: the CMOS "
"primitive must have four arguments." << endl;
des->errors += 1;
} else {
gate = new NetLogic(scope, inst_name, pin_count(),
NetLogic::CMOS, instance_width);
}
break;
case NAND:
if (pin_count() < 2) {
cerr << get_fileline() << ": error: the NAND "
"primitive must have an input." << endl;
des->errors += 1;
} else {
gate = new NetLogic(scope, inst_name, pin_count(),
NetLogic::NAND, instance_width);
}
break;
case NMOS:
if (pin_count() != 3) {
cerr << get_fileline() << ": error: the NMOS "
"primitive must have three arguments." << endl;
des->errors += 1;
} else {
gate = new NetLogic(scope, inst_name, pin_count(),
NetLogic::NMOS, instance_width);
}
break;
case NOR:
if (pin_count() < 2) {
cerr << get_fileline() << ": error: the NOR "
"primitive must have an input." << endl;
des->errors += 1;
} else {
gate = new NetLogic(scope, inst_name, pin_count(),
NetLogic::NOR, instance_width);
}
break;
case NOT:
if (pin_count() < 2) {
cerr << get_fileline() << ": error: the NOT "
"primitive must have an input." << endl;
des->errors += 1;
} else {
gate = new NetLogic(scope, inst_name, 2,
NetLogic::NOT, instance_width);
}
break;
case NOTIF0:
if (pin_count() != 3) {
cerr << get_fileline() << ": error: the NOTIF0 "
"primitive must have three arguments." << endl;
des->errors += 1;
} else {
gate = new NetLogic(scope, inst_name, pin_count(),
NetLogic::NOTIF0, instance_width);
}
break;
case NOTIF1:
if (pin_count() != 3) {
cerr << get_fileline() << ": error: the NOTIF1 "
"primitive must have three arguments." << endl;
des->errors += 1;
} else {
gate = new NetLogic(scope, inst_name, pin_count(),
NetLogic::NOTIF1, instance_width);
}
break;
case OR:
if (pin_count() < 2) {
cerr << get_fileline() << ": error: the OR "
"primitive must have an input." << endl;
des->errors += 1;
} else {
gate = new NetLogic(scope, inst_name, pin_count(),
NetLogic::OR, instance_width);
}
break;
case RCMOS:
if (pin_count() != 4) {
cerr << get_fileline() << ": error: the RCMOS "
"primitive must have four arguments." << endl;
des->errors += 1;
} else {
gate = new NetLogic(scope, inst_name, pin_count(),
NetLogic::RCMOS, instance_width);
}
break;
case RNMOS:
if (pin_count() != 3) {
cerr << get_fileline() << ": error: the RNMOS "
"primitive must have three arguments." << endl;
des->errors += 1;
} else {
gate = new NetLogic(scope, inst_name, pin_count(),
NetLogic::RNMOS, instance_width);
}
break;
case RPMOS:
if (pin_count() != 3) {
cerr << get_fileline() << ": error: the RPMOS "
"primitive must have three arguments." << endl;
des->errors += 1;
} else {
gate = new NetLogic(scope, inst_name, pin_count(),
NetLogic::RPMOS, instance_width);
}
break;
case PMOS:
if (pin_count() != 3) {
cerr << get_fileline() << ": error: the PMOS "
"primitive must have three arguments." << endl;
des->errors += 1;
} else {
gate = new NetLogic(scope, inst_name, pin_count(),
NetLogic::PMOS, instance_width);
}
break;
case PULLDOWN:
gate = new NetLogic(scope, inst_name, 1,
NetLogic::PULLDOWN, instance_width);
break;
case PULLUP:
gate = new NetLogic(scope, inst_name, 1,
NetLogic::PULLUP, instance_width);
break;
case XNOR:
if (pin_count() < 2) {
cerr << get_fileline() << ": error: the XNOR "
"primitive must have an input." << endl;
des->errors += 1;
} else {
gate = new NetLogic(scope, inst_name, pin_count(),
NetLogic::XNOR, instance_width);
}
break;
case XOR:
if (pin_count() < 2) {
cerr << get_fileline() << ": error: the XOR "
"primitive must have an input." << endl;
des->errors += 1;
} else {
gate = new NetLogic(scope, inst_name, pin_count(),
NetLogic::XOR, instance_width);
}
break;
case TRAN:
if (pin_count() != 2) {
cerr << get_fileline() << ": error: Pin count for "
<< "tran device." << endl;
des->errors += 1;
} else {
gate = new NetTran(scope, inst_name, IVL_SW_TRAN,
instance_width);
}
break;
case RTRAN:
if (pin_count() != 2) {
cerr << get_fileline() << ": error: Pin count for "
<< "rtran device." << endl;
des->errors += 1;
} else {
gate = new NetTran(scope, inst_name, IVL_SW_RTRAN,
instance_width);
}
break;
case TRANIF0:
if (pin_count() != 3) {
cerr << get_fileline() << ": error: Pin count for "
<< "tranif0 device." << endl;
des->errors += 1;
} else {
gate = new NetTran(scope, inst_name, IVL_SW_TRANIF0,
instance_width);
}
break;
case RTRANIF0:
if (pin_count() != 3) {
cerr << get_fileline() << ": error: Pin count for "
<< "rtranif0 device." << endl;
des->errors += 1;
} else {
gate = new NetTran(scope, inst_name, IVL_SW_RTRANIF0,
instance_width);
}
break;
case TRANIF1:
if (pin_count() != 3) {
cerr << get_fileline() << ": error: Pin count for "
<< "tranif1 device." << endl;
des->errors += 1;
} else {
gate = new NetTran(scope, inst_name, IVL_SW_TRANIF1,
instance_width);
}
break;
case RTRANIF1:
if (pin_count() != 3) {
cerr << get_fileline() << ": error: Pin count for "
<< "rtranif1 device." << endl;
des->errors += 1;
} else {
gate = new NetTran(scope, inst_name, IVL_SW_RTRANIF1,
instance_width);
}
break;
default:
cerr << get_fileline() << ": internal error: unhandled "
"gate type." << endl;
des->errors += 1;
break;
}
return gate;
}
bool PGBuiltin::check_delay_count(Design*des) const
{
switch (type()) {
case AND:
case NAND:
case OR:
case NOR:
case XOR:
case XNOR:
case BUF:
case NOT:
if (delay_count() > 2) {
cerr << get_fileline() << ": error: More than two delays "
<< "given to a " << gate_name() << " gate." << endl;
des->errors += 1;
return true;
}
break;
case BUFIF0:
case NOTIF0:
case BUFIF1:
case NOTIF1:
if (delay_count() > 3) {
cerr << get_fileline() << ": error: More than three delays "
<< "given to a " << gate_name() << " gate." << endl;
des->errors += 1;
return true;
}
break;
case NMOS:
case RNMOS:
case PMOS:
case RPMOS:
case CMOS:
case RCMOS:
if (delay_count() > 3) {
cerr << get_fileline() << ": error: More than three delays "
<< "given to a " << gate_name() << " switch." << endl;
des->errors += 1;
return true;
}
break;
case TRAN:
case RTRAN:
if (delay_count() != 0) {
cerr << get_fileline() << ": error: A " << gate_name()
<< " switch does not take any delays." << endl;
des->errors += 1;
return true;
}
break;
case TRANIF0:
case TRANIF1:
if (delay_count() > 2) {
cerr << get_fileline() << ": error: More than two delays "
<< "given to a " << gate_name() << " switch." << endl;
des->errors += 1;
return true;
}
break;
case RTRANIF0:
case RTRANIF1:
if (delay_count() > 2) {
cerr << get_fileline() << ": error: More than two delays "
<< "given to an " << gate_name() << " switch." << endl;
des->errors += 1;
return true;
}
break;
case PULLUP:
case PULLDOWN:
if (delay_count() != 0) {
cerr << get_fileline() << ": error: A " << gate_name()
<< " source does not take any delays." << endl;
des->errors += 1;
return true;
}
break;
default:
cerr << get_fileline() << ": internal error: unhandled "
"gate type." << endl;
des->errors += 1;
return true;
break;
}
return false;
}
/*
* Elaborate a Builtin gate. These normally get translated into
* NetLogic nodes that reflect the particular logic function.
*/
void PGBuiltin::elaborate(Design*des, NetScope*scope) const
{
unsigned instance_width = 1;
perm_string name = get_name();
if (name == "") name = scope->local_symbol();
/* Calculate the array bounds and instance count for the gate,
as described in the Verilog source. If there is none, then
the count is 1, and high==low==0. */
long low=0, high=0;
unsigned array_count = calculate_array_size_(des, scope, high, low);
if (array_count == 0) return;
unsigned gate_count = 0, lval_count = 0;
calculate_gate_and_lval_count_(gate_count, lval_count);
/* Now we have a gate count. Elaborate the lval (output or
bi-directional) expressions only. We do it early so that
we can see if we can make wide gates instead of an array
of gates. */
vector<NetNet*>lval_sigs (lval_count);
for (unsigned idx = 0 ; idx < lval_count ; idx += 1) {
if (pin(idx) == 0) {
cerr << get_fileline() << ": error: Logic gate port "
"expressions are not optional." << endl;
des->errors += 1;
return;
}
if (lval_count > gate_count)
lval_sigs[idx] = pin(idx)->elaborate_bi_net(des, scope);
else
lval_sigs[idx] = pin(idx)->elaborate_lnet(des, scope);
// The only way this should return zero is if an error
// happened, so for that case just return.
if (lval_sigs[idx] == 0) return;
// For now, assume all the outputs are the same width.
ivl_assert(*this, idx == 0 || lval_sigs[idx]->vector_width() == lval_sigs[0]->vector_width());
}
/* Detect the special case that the l-value width exactly
matches the gate count. In this case, we will make a single
gate that has the desired vector width.
NOTE: This assumes that all the outputs have the same
width. For gates with 1 output, this is trivially true. */
if (lval_sigs[0]->vector_width() == array_count) {
instance_width = array_count;
array_count = 1;
if (debug_elaborate && instance_width != 1)
cerr << get_fileline() << ": debug: PGBuiltin: "
"Collapsed gate array into single wide "
"(" << instance_width << ") instance." << endl;
}
/* Calculate the gate delays from the delay expressions
given in the source. For logic gates, the decay time
is meaningless because it can never go to high
impedance. However, the bufif devices can generate
'bz output, so we will pretend that anything can.
If only one delay value expression is given (i.e., #5
nand(foo,...)) then rise, fall and decay times are
all the same value. If two values are given, rise and
fall times are use, and the decay time is the minimum
of the rise and fall times. Finally, if all three
values are given, they are taken as specified. */
if (check_delay_count(des)) return;
NetExpr* rise_time, *fall_time, *decay_time;
eval_delays(des, scope, rise_time, fall_time, decay_time, true);
struct attrib_list_t*attrib_list;
unsigned attrib_list_n = 0;
attrib_list = evaluate_attributes(attributes, attrib_list_n,
des, scope);
/* Allocate all the netlist nodes for the gates. */
vector<NetNode*>cur (array_count*gate_count);
/* Now make as many gates as the bit count dictates. Give each
a unique name, and set the delay times. */
for (unsigned idx = 0 ; idx < array_count*gate_count ; idx += 1) {
unsigned array_idx = idx/gate_count;
unsigned gate_idx = idx%gate_count;
ostringstream tmp;
unsigned index = (low < high)? (low+array_idx) : (low-array_idx);
tmp << name << "<" << index << "." << gate_idx << ">";
perm_string inm = lex_strings.make(tmp.str());
cur[idx] = create_gate_for_output_(des, scope, inm, instance_width);
if (cur[idx] == 0)
return;
for (unsigned adx = 0 ; adx < attrib_list_n ; adx += 1)
cur[idx]->attribute(attrib_list[adx].key,
attrib_list[adx].val);
/* Set the delays and drive strength for all built in gates. */
cur[idx]->rise_time(rise_time);
cur[idx]->fall_time(fall_time);
cur[idx]->decay_time(decay_time);
cur[idx]->pin(0).drive0(strength0());
cur[idx]->pin(0).drive1(strength1());
cur[idx]->set_line(*this);
des->add_node(cur[idx]);
}
delete[]attrib_list;
/* The gates have all been allocated, this loop runs through
the parameters and attaches the ports of the objects. */
for (unsigned idx = 0 ; idx < pin_count() ; idx += 1) {
PExpr*ex = pin(idx);
if (ex == 0) {
cerr << get_fileline() << ": error: Logic gate port "
"expressions are not optional." << endl;
des->errors += 1;
return;
}
NetNet*sig = 0;
if (idx < lval_count) {
sig = lval_sigs[idx];
} else {
// If this is an array, the port expression is required
// to be the exact width required (this will be checked
// later). But if this is a single instance, consensus
// is that we just take the LSB of the port expression.
NetExpr*tmp = elab_and_eval(des, scope, ex, is_array() ? -1 : 1);
if (tmp == 0)
continue;
if (!is_array() && tmp->expr_width() != 1)
tmp = new NetESelect(tmp, make_const_0(1), 1,
IVL_SEL_IDX_UP);
sig = tmp->synthesize(des, scope, tmp);
delete tmp;
}
if (sig == 0)
continue;
ivl_assert(*this, sig);
if (array_count == 1) {
/* Handle the case where there is one gate that
carries the whole vector width. */
if (1 == sig->vector_width() && instance_width != 1) {
ivl_assert(*this, sig->vector_width() == 1);
NetReplicate*rep
= new NetReplicate(scope,
scope->local_symbol(),
instance_width,
instance_width);
rep->set_line(*this);
des->add_node(rep);
connect(rep->pin(1), sig->pin(0));
netvector_t*osig_vec = new netvector_t(IVL_VT_LOGIC,
instance_width-1,0);
sig = new NetNet(scope, scope->local_symbol(),
NetNet::WIRE, osig_vec);
sig->set_line(*this);
sig->local_flag(true);
connect(rep->pin(0), sig->pin(0));
}
if (instance_width != sig->vector_width()) {
cerr << get_fileline() << ": error: "
<< "Expression width " << sig->vector_width()
<< " does not match width " << instance_width
<< " of logic gate array port " << idx+1
<< "." << endl;
des->errors += 1;
}
// There is only 1 instance, but there may be
// multiple outputs to that gate. That would
// potentially mean multiple actual gates.
// Although in Verilog proper a multiple
// output gate has only 1 input, this conditional
// handles gates with N outputs and M inputs.
if (idx < gate_count) {
connect(cur[idx]->pin(0), sig->pin(0));
} else {
for (unsigned dev = 0 ; dev < gate_count; dev += 1)
connect(cur[dev]->pin(idx-gate_count+1), sig->pin(0));
}
} else if (sig->vector_width() == 1) {
/* Handle the case where a single bit is connected
repetitively to all the instances. If idx is an
output port, connect it to all array_count
devices that have outputs at this
position. Otherwise, idx is an input to all
array_count*gate_count devices. */
if (idx < gate_count) {
for (unsigned gdx = 0 ; gdx < array_count ; gdx += 1) {
unsigned dev = gdx*gate_count;
connect(cur[dev+idx]->pin(0), sig->pin(0));
}
} else {
unsigned use_idx = idx - gate_count + 1;
for (unsigned gdx = 0 ; gdx < cur.size() ; gdx += 1)
connect(cur[gdx]->pin(use_idx), sig->pin(0));
}
} else if (sig->vector_width() == array_count) {
/* Bi-directional switches should get collapsed into
a single wide instance, so should never reach this
point. Check this is so, as the following code
doesn't handle bi-directional connections. */
ivl_assert(*this, lval_count == gate_count);
/* Handle the general case that each bit of the
value is connected to a different instance. In
this case, the output is handled slightly
different from the inputs. */
if (idx < gate_count) {
NetConcat*cc = new NetConcat(scope,
scope->local_symbol(),
sig->vector_width(),
array_count);
cc->set_line(*this);
des->add_node(cc);
/* Connect the concat to the signal. */
connect(cc->pin(0), sig->pin(0));
/* Connect the outputs of the gates to the concat. */
for (unsigned gdx = 0 ; gdx < array_count; gdx += 1) {
unsigned dev = gdx*gate_count;
connect(cur[dev+idx]->pin(0), cc->pin(gdx+1));
netvector_t*tmp2_vec = new netvector_t(IVL_VT_LOGIC);
NetNet*tmp2 = new NetNet(scope,
scope->local_symbol(),
NetNet::WIRE, tmp2_vec);
tmp2->set_line(*this);