forked from The-OpenROAD-Project/OpenSTA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sim.cc
1291 lines (1217 loc) · 35.4 KB
/
Sim.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
// OpenSTA, Static Timing Analyzer
// Copyright (c) 2024, Parallax Software, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 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, see <https://www.gnu.org/licenses/>.
#include "Sim.hh"
#include "StaConfig.hh" // CUDD
#include "Error.hh"
#include "Mutex.hh"
#include "Debug.hh"
#include "Report.hh"
#include "Stats.hh"
#include "FuncExpr.hh"
#include "TimingRole.hh"
#include "TimingArc.hh"
#include "Liberty.hh"
#include "PortDirection.hh"
#include "Sequential.hh"
#include "Network.hh"
#include "Sdc.hh"
#include "Graph.hh"
#if CUDD
// https://davidkebo.com/cudd
#include "cudd.h"
#else
#define Cudd_Init(ignore1, ignore2, ignore3, ignore4, ignore5) nullptr
#define Cudd_Quit(ignore1)
#endif
namespace sta {
static LogicValue
logicNot(LogicValue value);
static const Pin *
findDrvrPin(const Pin *pin,
Network *network);
Sim::Sim(StaState *sta) :
StaState(sta),
observer_(nullptr),
valid_(false),
incremental_(false),
const_func_pins_(network_),
const_func_pins_valid_(false),
invalid_insts_(network_),
invalid_drvr_pins_(network_),
invalid_load_pins_(network_),
instances_with_const_pins_(network_),
instances_to_annotate_(network_),
bdd_(sta)
{
}
Sim::~Sim()
{
delete observer_;
}
#if CUDD
TimingSense
Sim::functionSense(const FuncExpr *expr,
const Pin *input_pin,
const Instance *inst)
{
debugPrint(debug_, "sim", 4, "find sense pin %s %s",
network_->pathName(input_pin),
expr->asString());
bool increasing, decreasing;
{
UniqueLock lock(bdd_lock_);
DdNode *bdd = funcBddSim(expr, inst);
DdManager *cudd_mgr = bdd_.cuddMgr();
LibertyPort *input_port = network_->libertyPort(input_pin);
DdNode *input_node = bdd_.ensureNode(input_port);
unsigned int input_index = Cudd_NodeReadIndex(input_node);
increasing = (Cudd_Increasing(cudd_mgr, bdd, input_index)
== Cudd_ReadOne(cudd_mgr));
decreasing = (Cudd_Decreasing(cudd_mgr, bdd, input_index)
== Cudd_ReadOne(cudd_mgr));
Cudd_RecursiveDeref(cudd_mgr, bdd);
bdd_.clearVarMap();
}
TimingSense sense;
if (increasing && decreasing)
sense = TimingSense::none;
else if (increasing)
sense = TimingSense::positive_unate;
else if (decreasing)
sense = TimingSense::negative_unate;
else
sense = TimingSense::non_unate;
debugPrint(debug_, "sim", 4, " %s", timingSenseString(sense));
return sense;
}
LogicValue
Sim::evalExpr(const FuncExpr *expr,
const Instance *inst)
{
UniqueLock lock(bdd_lock_);
DdNode *bdd = funcBddSim(expr, inst);
LogicValue value = LogicValue::unknown;
DdManager *cudd_mgr = bdd_.cuddMgr();
if (bdd == Cudd_ReadLogicZero(cudd_mgr))
value = LogicValue::zero;
else if (bdd == Cudd_ReadOne(cudd_mgr))
value = LogicValue::one;
if (bdd) {
Cudd_RecursiveDeref(bdd_.cuddMgr(), bdd);
bdd_.clearVarMap();
}
return value;
}
// BDD with instance pin values substituted.
DdNode *
Sim::funcBddSim(const FuncExpr *expr,
const Instance *inst)
{
DdNode *bdd = bdd_.funcBdd(expr);
DdManager *cudd_mgr = bdd_.cuddMgr();
InstancePinIterator *pin_iter = network_->pinIterator(inst);
while (pin_iter->hasNext()) {
const Pin *pin = pin_iter->next();
const LibertyPort *port = network_->libertyPort(pin);
DdNode *port_node = bdd_.findNode(port);
if (port_node) {
LogicValue value = logicValue(pin);
int var_index = Cudd_NodeReadIndex(port_node);
//printf("%s %d %c\n", port->name(), var_index, logicValueString(value));
switch (value) {
case LogicValue::zero:
bdd = Cudd_bddCompose(cudd_mgr, bdd, Cudd_ReadLogicZero(cudd_mgr), var_index);
Cudd_Ref(bdd);
break;
case LogicValue::one:
bdd = Cudd_bddCompose(cudd_mgr, bdd, Cudd_ReadOne(cudd_mgr), var_index);
Cudd_Ref(bdd);
break;
default:
break;
}
}
}
return bdd;
}
#else
// No CUDD.
static LogicValue
logicOr(LogicValue value1,
LogicValue value2)
{
static LogicValue logic_or[5][5] =
{{LogicValue::zero, LogicValue::one, LogicValue::unknown, LogicValue::unknown, LogicValue::unknown},
{LogicValue::one, LogicValue::one, LogicValue::one, LogicValue::one, LogicValue::one},
{LogicValue::unknown,LogicValue::one, LogicValue::unknown, LogicValue::unknown, LogicValue::unknown},
{LogicValue::unknown,LogicValue::one, LogicValue::unknown, LogicValue::unknown, LogicValue::unknown},
{LogicValue::unknown,LogicValue::one, LogicValue::unknown, LogicValue::unknown, LogicValue::unknown}};
return logic_or[int(value1)][int(value2)];
}
static LogicValue
logicAnd(LogicValue value1,
LogicValue value2)
{
static LogicValue logic_and[5][5] =
{{LogicValue::zero,LogicValue::zero, LogicValue::zero, LogicValue::zero, LogicValue::zero},
{LogicValue::zero,LogicValue::one, LogicValue::unknown,LogicValue::unknown, LogicValue::unknown},
{LogicValue::zero,LogicValue::unknown,LogicValue::unknown,LogicValue::unknown, LogicValue::unknown},
{LogicValue::zero,LogicValue::unknown,LogicValue::unknown,LogicValue::unknown, LogicValue::unknown},
{LogicValue::zero,LogicValue::unknown,LogicValue::unknown,LogicValue::unknown, LogicValue::unknown}};
return logic_and[int(value1)][int(value2)];
}
static LogicValue
logicXor(LogicValue value1,
LogicValue value2)
{
static LogicValue logic_xor[5][5]=
{{LogicValue::zero, LogicValue::one, LogicValue::unknown,LogicValue::unknown, LogicValue::unknown},
{LogicValue::one, LogicValue::zero, LogicValue::unknown,LogicValue::unknown, LogicValue::unknown},
{LogicValue::unknown,LogicValue::unknown,LogicValue::unknown,LogicValue::unknown, LogicValue::unknown},
{LogicValue::unknown,LogicValue::unknown,LogicValue::unknown,LogicValue::unknown, LogicValue::unknown},
{LogicValue::unknown,LogicValue::unknown,LogicValue::unknown,LogicValue::unknown, LogicValue::unknown}};
return logic_xor[int(value1)][int(value2)];
}
static TimingSense
senseNot(TimingSense sense)
{
static TimingSense sense_not[5] = {TimingSense::negative_unate,
TimingSense::positive_unate,
TimingSense::non_unate,
TimingSense::none,
TimingSense::unknown};
return sense_not[int(sense)];
}
static TimingSense
senseAndOr(TimingSense sense1,
TimingSense sense2)
{
static TimingSense sense_and_or[5][5] =
{{TimingSense::positive_unate, TimingSense::non_unate,
TimingSense::non_unate, TimingSense::positive_unate, TimingSense::unknown},
{TimingSense::non_unate, TimingSense::negative_unate,
TimingSense::non_unate, TimingSense::negative_unate, TimingSense::unknown},
{TimingSense::non_unate, TimingSense::non_unate, TimingSense::non_unate,
TimingSense::non_unate, TimingSense::unknown},
{TimingSense::positive_unate, TimingSense::negative_unate,
TimingSense::non_unate, TimingSense::none, TimingSense::unknown},
{TimingSense::unknown, TimingSense::unknown,
TimingSense::unknown, TimingSense::non_unate, TimingSense::unknown}};
return sense_and_or[int(sense1)][int(sense2)];
}
static TimingSense
senseXor(TimingSense sense1,
TimingSense sense2)
{
static TimingSense xor_sense[5][5] =
{{TimingSense::non_unate, TimingSense::non_unate,
TimingSense::non_unate, TimingSense::non_unate, TimingSense::unknown},
{TimingSense::non_unate, TimingSense::non_unate,
TimingSense::non_unate, TimingSense::non_unate, TimingSense::unknown},
{TimingSense::non_unate, TimingSense::non_unate,
TimingSense::non_unate, TimingSense::non_unate, TimingSense::unknown},
{TimingSense::non_unate, TimingSense::non_unate,
TimingSense::non_unate, TimingSense::none, TimingSense::unknown},
{TimingSense::unknown, TimingSense::unknown,
TimingSense::unknown, TimingSense::unknown, TimingSense::unknown}};
return xor_sense[int(sense1)][int(sense2)];
}
TimingSense
Sim::functionSense(const FuncExpr *expr,
const Pin *input_pin,
const Instance *inst)
{
TimingSense sense = TimingSense::none;
LogicValue value = LogicValue::unknown;
functionSense(expr, input_pin, inst, sense, value);
return sense;
}
void
Sim::functionSense(const FuncExpr *expr,
const Pin *input_pin,
const Instance *inst,
// return values
TimingSense &sense,
LogicValue &value) const
{
switch (expr->op()) {
case FuncExpr::op_port: {
Pin *pin = network_->findPin(inst, expr->port());
if (pin) {
if (pin == input_pin)
sense = TimingSense::positive_unate;
else
sense = TimingSense::none;
value = logicValue(pin);
}
else {
sense = TimingSense::none;
value = LogicValue::unknown;
}
break;
}
case FuncExpr::op_not: {
TimingSense sense1;
LogicValue value1;
functionSense(expr->left(), input_pin, inst, sense1, value1);
if (value1 == LogicValue::zero) {
sense = TimingSense::none;
value = LogicValue::one;
}
else if (value1 == LogicValue::one) {
sense = TimingSense::none;
value = LogicValue::zero;
}
else {
sense = senseNot(sense1);
value = LogicValue::unknown;
}
break;
}
case FuncExpr::op_or: {
TimingSense sense1, sense2;
LogicValue value1, value2;
functionSense(expr->left(), input_pin, inst, sense1, value1);
functionSense(expr->right(), input_pin, inst, sense2, value2);
if (value1 == LogicValue::one || value2 == LogicValue::one) {
sense = TimingSense::none;
value = LogicValue::one;
}
else if (value1 == LogicValue::zero) {
sense = sense2;
value = value2;
}
else if (value2 == LogicValue::zero) {
sense = sense1;
value = value1;
}
else {
sense = senseAndOr(sense1, sense2);
value = LogicValue::unknown;
}
break;
}
case FuncExpr::op_and: {
TimingSense sense1, sense2;
LogicValue value1, value2;
functionSense(expr->left(), input_pin, inst, sense1, value1);
functionSense(expr->right(), input_pin, inst, sense2, value2);
if (value1 == LogicValue::zero || value2 == LogicValue::zero) {
sense = TimingSense::none;
value = LogicValue::zero;
}
else if (value1 == LogicValue::one) {
sense = sense2;
value = value2;
}
else if (value2 == LogicValue::one) {
sense = sense1;
value = value1;
}
else {
sense = senseAndOr(sense1, sense2);
value = LogicValue::unknown;
}
break;
}
case FuncExpr::op_xor: {
TimingSense sense1, sense2;
LogicValue value1, value2;
functionSense(expr->left(), input_pin, inst, sense1, value1);
functionSense(expr->right(), input_pin, inst, sense2, value2);
if ((value1 == LogicValue::zero && value2 == LogicValue::zero)
|| (value1 == LogicValue::one && value2 == LogicValue::one)) {
sense = TimingSense::none;
value = LogicValue::zero;
}
else if ((value1 == LogicValue::zero && value2 == LogicValue::one)
|| (value1 == LogicValue::one && value2 == LogicValue::zero)) {
sense = TimingSense::none;
value = LogicValue::one;
}
else if (value1 == LogicValue::zero) {
sense = sense2;
value = value2;
}
else if (value1 == LogicValue::one) {
sense = senseNot(sense2);
value = logicNot(value2);
}
else if (value2 == LogicValue::zero) {
sense = sense1;
value = value1;
}
else if (value2 == LogicValue::one) {
sense = senseNot(sense1);
value = logicNot(value1);
}
else {
sense = senseXor(sense1, sense2);
value = logicXor(value1, value2);
}
break;
}
case FuncExpr::op_one:
sense = TimingSense::none;
value = LogicValue::one;
break;
case FuncExpr::op_zero:
sense = TimingSense::none;
value = LogicValue::zero;
break;
}
}
LogicValue
Sim::evalExpr(const FuncExpr *expr,
const Instance *inst)
{
switch (expr->op()) {
case FuncExpr::op_port: {
LibertyPort *port = expr->port();
if (port) {
Pin *pin = network_->findPin(inst, port->name());
if (pin)
return logicValue(pin);
}
// Internal ports don't have instance pins.
return LogicValue::unknown;
}
case FuncExpr::op_not:
return logicNot(evalExpr(expr->left(), inst));
case FuncExpr::op_or:
return logicOr(evalExpr(expr->left(),inst),
evalExpr(expr->right(),inst));
case FuncExpr::op_and:
return logicAnd(evalExpr(expr->left(),inst),
evalExpr(expr->right(),inst));
case FuncExpr::op_xor:
return logicXor(evalExpr(expr->left(),inst),
evalExpr(expr->right(),inst));
case FuncExpr::op_one:
return LogicValue::one;
case FuncExpr::op_zero:
return LogicValue::zero;
}
// Prevent warnings from lame compilers.
return LogicValue::zero;
}
#endif // CUDD
static LogicValue
logicNot(LogicValue value)
{
static LogicValue logic_not[5] = {LogicValue::one, LogicValue::zero,
LogicValue::unknown, LogicValue::unknown,
LogicValue::unknown};
return logic_not[int(value)];
}
void
Sim::clear()
{
valid_ = false;
incremental_ = false;
const_func_pins_.clear();
const_func_pins_valid_ = false;
instances_with_const_pins_.clear();
instances_to_annotate_.clear();
invalid_insts_.clear();
invalid_drvr_pins_.clear();
invalid_load_pins_.clear();
}
void
Sim::setObserver(SimObserver *observer)
{
delete observer_;
observer_ = observer;
}
void
Sim::ensureConstantsPropagated()
{
if (!valid_) {
Stats stats(debug_, report_);
ensureConstantFuncPins();
instances_to_annotate_.clear();
if (incremental_) {
seedInvalidConstants();
propagateToInvalidLoads();
propagateFromInvalidDrvrsToLoads();
}
else {
clearSimValues();
seedConstants();
}
invalid_insts_.clear();
propagateConstants(false);
annotateGraphEdges();
valid_ = true;
incremental_ = true;
stats.report("Propagate constants");
}
}
void
Sim::findLogicConstants()
{
clear();
ensureConstantFuncPins();
setConstFuncPins();
enqueueConstantPinInputs();
propagateConstants(true);
valid_ = true;
}
void
Sim::seedInvalidConstants()
{
for (const Instance *inst : invalid_insts_)
eval_queue_.push(inst);
}
void
Sim::propagateToInvalidLoads()
{
for (const Pin *load_pin : invalid_load_pins_) {
const Net *net = network_->net(load_pin);
if (net && network_->isGround(net))
setPinValue(load_pin, LogicValue::zero);
else if (net && network_->isPower(net))
setPinValue(load_pin, LogicValue::one);
else {
const Pin *drvr_pin = findDrvrPin(load_pin, network_);
if (drvr_pin)
propagateDrvrToLoad(drvr_pin, load_pin);
}
}
invalid_load_pins_.clear();
}
void
Sim::propagateFromInvalidDrvrsToLoads()
{
for (const Pin *drvr_pin : invalid_drvr_pins_) {
LogicValue value = const_func_pins_.hasKey(drvr_pin)
? pinConstFuncValue(drvr_pin)
: logicValue(drvr_pin);
PinConnectedPinIterator *load_iter=network_->connectedPinIterator(drvr_pin);
while (load_iter->hasNext()) {
const Pin *load_pin = load_iter->next();
if (load_pin != drvr_pin
&& network_->isLoad(load_pin))
setPinValue(load_pin, value);
}
delete load_iter;
}
invalid_drvr_pins_.clear();
}
void
Sim::propagateDrvrToLoad(const Pin *drvr_pin,
const Pin *load_pin)
{
LogicValue value = logicValue(drvr_pin);
setPinValue(load_pin, value);
}
void
Sim::constantsInvalid()
{
valid_ = false;
incremental_ = false;
}
void
Sim::ensureConstantFuncPins()
{
if (!const_func_pins_valid_) {
LeafInstanceIterator *inst_iter = network_->leafInstanceIterator();
while (inst_iter->hasNext()) {
Instance *inst = inst_iter->next();
InstancePinIterator *pin_iter = network_->pinIterator(inst);
while (pin_iter->hasNext()) {
const Pin *pin = pin_iter->next();
recordConstPinFunc(pin);
}
delete pin_iter;
}
delete inst_iter;
const_func_pins_valid_ = true;
}
}
void
Sim::recordConstPinFunc(const Pin *pin)
{
LibertyPort *port = network_->libertyPort(pin);
if (port) {
FuncExpr *expr = port->function();
if (expr
// Tristate outputs do not force the output to be constant.
&& port->tristateEnable() == nullptr
&& (expr->op() == FuncExpr::op_zero
|| expr->op() == FuncExpr::op_one))
const_func_pins_.insert(pin);
}
}
void
Sim::deleteInstanceBefore(const Instance *inst)
{
instances_with_const_pins_.erase(inst);
invalid_insts_.erase(inst);
}
void
Sim::makePinAfter(const Pin *pin)
{
// Incrementally update const_func_pins_.
recordConstPinFunc(pin);
}
void
Sim::deletePinBefore(const Pin *pin)
{
// Incrementally update const_func_pins_.
const_func_pins_.erase(pin);
invalid_load_pins_.erase(pin);
invalid_drvr_pins_.erase(pin);
invalid_insts_.insert(network_->instance(pin));
}
void
Sim::connectPinAfter(const Pin *pin)
{
if (incremental_) {
recordConstPinFunc(pin);
if (network_->isLoad(pin))
invalid_load_pins_.insert(pin);
if (network_->isDriver(pin))
invalid_drvr_pins_.insert(pin);
valid_ = false;
}
}
void
Sim::disconnectPinBefore(const Pin *pin)
{
if (incremental_) {
if (network_->isLoad(pin)) {
invalid_load_pins_.insert(pin);
removePropagatedValue(pin);
}
if (network_->isDriver(pin))
invalid_drvr_pins_.insert(pin);
}
}
void
Sim::pinSetFuncAfter(const Pin *pin)
{
if (incremental_) {
Instance *inst = network_->instance(pin);
if (instances_with_const_pins_.hasKey(inst))
invalid_insts_.insert(inst);
valid_ = false;
}
// Incrementally update const_func_pins_.
const_func_pins_.erase(pin);
recordConstPinFunc(pin);
}
void
Sim::seedConstants()
{
// Propagate constants from inputs tied hi/low in the network.
enqueueConstantPinInputs();
// Propagate set_LogicValue::zero, set_LogicValue::one, set_logic_dc constants.
setConstraintConstPins(sdc_->logicValues());
// Propagate set_case_analysis constants.
setConstraintConstPins(sdc_->caseLogicValues());
// Propagate 0/1 constant functions.
setConstFuncPins();
}
void
Sim::propagateConstants(bool thru_sequentials)
{
while (!eval_queue_.empty()) {
const Instance *inst = eval_queue_.front();
eval_queue_.pop();
evalInstance(inst, thru_sequentials);
}
}
void
Sim::setConstraintConstPins(LogicValueMap &value_map)
{
for (auto pin_value : value_map) {
const Pin *pin = pin_value.first;
LogicValue value = pin_value.second;
debugPrint(debug_, "sim", 2, "case pin %s = %c",
network_->pathName(pin),
logicValueString(value));
if (network_->isHierarchical(pin)) {
// Set the logic value on pins inside the instance of a hierarchical pin.
bool pin_is_output = network_->direction(pin)->isAnyOutput();
PinConnectedPinIterator *pin_iter=network_->connectedPinIterator(pin);
while (pin_iter->hasNext()) {
const Pin *pin1 = pin_iter->next();
if (network_->isLeaf(pin1)
&& network_->direction(pin1)->isAnyInput()
&& ((pin_is_output && !network_->isInside(pin1, pin))
|| (!pin_is_output && network_->isInside(pin1, pin))))
setPinValue(pin1, value);
}
delete pin_iter;
}
else
setPinValue(pin, value);
}
}
// Propagate constants from outputs with constant functions
// (tie high and tie low cell instances).
void
Sim::setConstFuncPins()
{
for (const Pin *pin : const_func_pins_) {
LogicValue value = pinConstFuncValue(pin);
setPinValue(pin, value);
debugPrint(debug_, "sim", 2, "func pin %s = %c",
network_->pathName(pin),
logicValueString(value));
}
}
LogicValue
Sim::pinConstFuncValue(const Pin *pin)
{
LibertyPort *port = network_->libertyPort(pin);
if (port) {
FuncExpr *expr = port->function();
if (expr->op() == FuncExpr::op_zero)
return LogicValue::zero;
else if (expr->op() == FuncExpr::op_one)
return LogicValue::one;
}
return LogicValue::unknown;
}
void
Sim::enqueueConstantPinInputs()
{
ConstantPinIterator *const_iter = network_->constantPinIterator();
while (const_iter->hasNext()) {
LogicValue value;
const Pin *pin;
const_iter->next(pin, value);
debugPrint(debug_, "sim", 2, "network constant pin %s = %c",
network_->pathName(pin),
logicValueString(value));
setPinValue(pin, value);
}
delete const_iter;
}
void
Sim::removePropagatedValue(const Pin *pin)
{
Instance *inst = network_->instance(pin);
if (instances_with_const_pins_.hasKey(inst)) {
invalid_insts_.insert(inst);
valid_ = false;
LogicValue constraint_value;
bool exists;
sdc_->caseLogicValue(pin, constraint_value, exists);
if (!exists) {
sdc_->logicValue(pin, constraint_value, exists);
if (!exists) {
debugPrint(debug_, "sim", 2, "pin %s remove prop constant",
network_->pathName(pin));
Vertex *vertex, *bidirect_drvr_vertex;
graph_->pinVertices(pin, vertex, bidirect_drvr_vertex);
if (vertex)
setSimValue(vertex, LogicValue::unknown);
if (bidirect_drvr_vertex)
setSimValue(bidirect_drvr_vertex, LogicValue::unknown);
}
}
}
}
void
Sim::setPinValue(const Pin *pin,
LogicValue value)
{
LogicValue constraint_value;
bool exists;
sdc_->caseLogicValue(pin, constraint_value, exists);
if (!exists)
sdc_->logicValue(pin, constraint_value, exists);
if (exists
&& value != constraint_value) {
if (value != LogicValue::unknown)
report_->warn(1521, "propagated logic value %c differs from constraint value of %c on pin %s.",
logicValueString(value),
logicValueString(constraint_value),
sdc_network_->pathName(pin));
}
else {
debugPrint(debug_, "sim", 3, "pin %s = %c",
network_->pathName(pin),
logicValueString(value));
Vertex *vertex, *bidirect_drvr_vertex;
graph_->pinVertices(pin, vertex, bidirect_drvr_vertex);
// Set vertex constant flags.
if (vertex)
setSimValue(vertex, value);
if (bidirect_drvr_vertex)
setSimValue(bidirect_drvr_vertex, value);
Instance *inst = network_->instance(pin);
if (logicValueZeroOne(value))
instances_with_const_pins_.insert(inst);
instances_to_annotate_.insert(inst);
if (network_->isLeaf(inst)
&& network_->direction(pin)->isAnyInput()) {
if (eval_queue_.empty()
|| (eval_queue_.back() != inst))
eval_queue_.push(inst);
}
else if (network_->isDriver(pin)) {
// Enqueue instances with input pins connected to net.
PinConnectedPinIterator *pin_iter=network_->connectedPinIterator(pin);
while (pin_iter->hasNext()) {
const Pin *pin1 = pin_iter->next();
if (pin1 != pin
&& network_->isLoad(pin1))
setPinValue(pin1, value);
}
delete pin_iter;
}
}
}
void
Sim::evalInstance(const Instance *inst,
bool thru_sequentials)
{
debugPrint(debug_, "sim", 2, "eval %s", network_->pathName(inst));
InstancePinIterator *pin_iter = network_->pinIterator(inst);
while (pin_iter->hasNext()) {
Pin *pin = pin_iter->next();
LibertyPort *port = network_->libertyPort(pin);
if (port) {
PortDirection *dir = port->direction();
if (dir->isAnyOutput()) {
LogicValue value = LogicValue::unknown;
FuncExpr *expr = port->function();
LibertyCell *cell = port->libertyCell();
if (expr) {
FuncExpr *tri_en_expr = port->tristateEnable();
if (tri_en_expr) {
if (evalExpr(tri_en_expr, inst) == LogicValue::one) {
value = evalExpr(expr, inst);
debugPrint(debug_, "sim", 2, " %s tri_en=1 %s = %c",
port->name(),
expr->asString(),
logicValueString(value));
}
}
else {
LibertyPort *expr_port = expr->port();
Sequential *sequential = (thru_sequentials && expr_port)
? cell->outputPortSequential(expr_port)
: nullptr;
if (sequential) {
value = evalExpr(sequential->data(), inst);
if (expr_port == sequential->outputInv())
value = logicNot(value);
debugPrint(debug_, "sim", 2, " %s seq %s = %c",
port->name(),
expr->asString(),
logicValueString(value));
}
else {
value = evalExpr(expr, inst);
debugPrint(debug_, "sim", 2, " %s %s = %c",
port->name(),
expr->asString(),
logicValueString(value));
}
}
}
else if (port->isClockGateOut()) {
value = clockGateOutValue(inst);
debugPrint(debug_, "sim", 2, " %s gated_clk = %c",
port->name(),
logicValueString(value));
}
if (value != logicValue(pin))
setPinValue(pin, value);
}
}
}
delete pin_iter;
}
LogicValue
Sim::clockGateOutValue(const Instance *inst)
{
LibertyCell *cell = network_->libertyCell(inst);
LibertyCellPortIterator port_iter(cell);
while (port_iter.hasNext()) {
LibertyPort *port = port_iter.next();
if (port->isClockGateClock()
|| port->isClockGateEnable()) {
Pin *gclk_pin = network_->findPin(inst, port);
if (gclk_pin) {
Vertex *gclk_vertex = graph_->pinLoadVertex(gclk_pin);
if (gclk_vertex->simValue() == LogicValue::zero)
return LogicValue::zero;
}
}
}
return LogicValue::unknown;
}
void
Sim::setSimValue(Vertex *vertex,
LogicValue value)
{
if (value != vertex->simValue()) {
vertex->setSimValue(value);
if (observer_)
observer_->valueChangeAfter(vertex);
}
}
TimingSense
Sim::functionSense(const Instance *inst,
const Pin *from_pin,
const Pin *to_pin)
{
if (logicZeroOne(from_pin))
return TimingSense::none;
else {
LibertyPort *from_port = network_->libertyPort(from_pin);
LibertyPort *to_port = network_->libertyPort(to_pin);
if (to_port) {
const FuncExpr *func = to_port->function();
if (func) {
PortDirection *to_dir = to_port->direction();
if (to_dir->isAnyTristate()) {
FuncExpr *tri_func = to_port->tristateEnable();
if (tri_func) {
if (func->hasPort(from_port)) {
// from_pin is an input to the to_pin function.
LogicValue tri_enable = evalExpr(tri_func, inst);
if (tri_enable == LogicValue::zero)
// Tristate is disabled.
return TimingSense::none;
else
return functionSense(func, from_pin, inst);
}
}
else {
// Missing tristate enable function.
if (func->hasPort(from_port))
// from_pin is an input to the to_pin function.
return functionSense(func, from_pin, inst);
}
}
else {
if (func->hasPort(from_port))
// from_pin is an input to the to_pin function.
return functionSense(func, from_pin, inst);
}
}
}
return TimingSense::unknown;
}
}
LogicValue
Sim::logicValue(const Pin *pin) const
{
Vertex *vertex = graph_->pinLoadVertex(pin);
if (vertex)
return vertex->simValue();
else {
if (network_->isHierarchical(pin)) {
const Pin *drvr_pin = findDrvrPin(pin, network_);
if (drvr_pin)
return logicValue(drvr_pin);
}
return LogicValue::unknown;
}
}
static const Pin *
findDrvrPin(const Pin *pin,
Network *network)
{
PinSet *drvrs = network->drivers(pin);
if (drvrs) {
PinSet::Iterator drvr_iter(drvrs);
if (drvr_iter.hasNext())
return drvr_iter.next();
}
return nullptr;