forked from steveicarus/iverilog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynth2.cc
2290 lines (1944 loc) · 76.4 KB
/
synth2.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) 2002-2022 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"
# include "functor.h"
# include "netlist.h"
# include "netvector.h"
# include "netmisc.h"
# include "compiler.h"
# include "ivl_assert.h"
using namespace std;
/* General notes on enables and bitmasks.
*
* When synthesising an asynchronous process that contains conditional
* statements (if/case statements), we need to determine the conditions
* that cause each nexus driven by that process to be updated. If a
* nexus is not updated under all circumstances, we must infer a latch.
* To this end, we generate an enable signal for each output nexus. As
* we walk the statement tree for the process, for each substatement we
* pass the enable signals generated so far into the synth_async method,
* and on return from the synth_async method, the enable signals will be
* updated to reflect any conditions introduced by that substatement.
* Once we have synthesised all the statements for that process, if an
* enable signal is not tied high, we must infer a latch for that nexus.
*
* When synthesising a synchronous process, we use the synth_async method
* to synthesise the combinatorial inputs to the D pins of the flip-flops
* we infer for that process. In this case the enable signal can be used
* as a clock enable for the flip-flop. This saves us explicitly feeding
* back the flip-flop output to undriven inputs of any synthesised muxes.
*
* The strategy described above is not sufficient when not all bits in
* a nexus are treated identically (i.e. different conditional clauses
* drive differing parts of the same vector). To handle this properly,
* we would (potentially) need to generate a separate enable signal for
* each bit in the vector. This would be a lot of work, particularly if
* we wanted to eliminate duplicates. For now, the strategy employed is
* to maintain a bitmask for each output nexus that identifies which bits
* in the nexus are unconditionally driven (driven by every clause). When
* we finish synthesising an asynchronous process, if the bitmask is not
* all ones, we must infer a latch. This currently results in an error,
* because to safely synthesise such a latch we would need the bit-level
* gate enables. When we finish synthesising a synchronous process, if
* the bitmask is not all ones, we explicitly feed the flip-flop outputs
* back to undriven inputs of any synthesised muxes to ensure undriven
* parts of the vector retain their previous state when the flip-flop is
* clocked.
*
* The enable signals are passed as links to the current output nexus
* for each signal. If an enable signal is not linked, this is treated
* as if the signal was tied low.
*
* The bitmasks are passed as bool vectors. 'true' indicates a bit is
* unconditionally driven. An empty vector (size = 0) indicates that
* the current substatement doesn't drive any bits in the nexus.
*/
static void qualify_enable(Design*des, NetScope*scope, NetNet*qualifier,
bool active_state, NetLogic::TYPE gate_type,
Link&enable_i, Link&enable_o)
{
if (enable_i.is_linked(scope->tie_lo())) {
connect(enable_o, scope->tie_lo());
return;
}
if (active_state == false) {
NetLogic*gate = new NetLogic(scope, scope->local_symbol(),
2, NetLogic::NOT, 1);
des->add_node(gate);
connect(gate->pin(1), qualifier->pin(0));
NetNet*sig = new NetNet(scope, scope->local_symbol(), NetNet::WIRE,
&netvector_t::scalar_logic);
sig->local_flag(true);
connect(sig->pin(0), gate->pin(0));
qualifier = sig;
}
if (enable_i.is_linked(scope->tie_hi())) {
connect(enable_o, qualifier->pin(0));
return;
}
NetLogic*gate = new NetLogic(scope, scope->local_symbol(),
3, gate_type, 1);
des->add_node(gate);
connect(gate->pin(1), qualifier->pin(0));
connect(gate->pin(2), enable_i);
connect(enable_o, gate->pin(0));
NetNet*sig = new NetNet(scope, scope->local_symbol(), NetNet::WIRE,
&netvector_t::scalar_logic);
sig->local_flag(true);
connect(sig->pin(0), gate->pin(0));
}
static void multiplex_enables(Design*des, NetScope*scope, NetNet*select,
Link&enable_1, Link&enable_0, Link&enable_o)
{
if (!enable_1.is_linked() &&
!enable_0.is_linked() )
return;
if ( enable_1.is_linked(scope->tie_hi()) &&
enable_0.is_linked(scope->tie_hi()) ) {
connect(enable_o, scope->tie_hi());
return;
}
if (enable_1.is_linked(scope->tie_lo()) || !enable_1.is_linked()) {
qualify_enable(des, scope, select, false, NetLogic::AND,
enable_0, enable_o);
return;
}
if (enable_0.is_linked(scope->tie_lo()) || !enable_0.is_linked()) {
qualify_enable(des, scope, select, true, NetLogic::AND,
enable_1, enable_o);
return;
}
if (enable_1.is_linked(scope->tie_hi())) {
qualify_enable(des, scope, select, true, NetLogic::OR,
enable_0, enable_o);
return;
}
if (enable_0.is_linked(scope->tie_hi())) {
qualify_enable(des, scope, select, false, NetLogic::OR,
enable_1, enable_o);
return;
}
NetMux*mux = new NetMux(scope, scope->local_symbol(), 1, 2, 1);
des->add_node(mux);
connect(mux->pin_Sel(), select->pin(0));
connect(mux->pin_Data(1), enable_1);
connect(mux->pin_Data(0), enable_0);
connect(enable_o, mux->pin_Result());
NetNet*sig = new NetNet(scope, scope->local_symbol(), NetNet::WIRE,
&netvector_t::scalar_logic);
sig->local_flag(true);
connect(sig->pin(0), mux->pin_Result());
}
static void merge_sequential_enables(Design*des, NetScope*scope,
Link&top_enable, Link&sub_enable)
{
if (!sub_enable.is_linked())
return;
if (top_enable.is_linked(scope->tie_hi()))
return;
if (sub_enable.is_linked(scope->tie_hi()))
top_enable.unlink();
if (top_enable.is_linked()) {
NetLogic*gate = new NetLogic(scope, scope->local_symbol(),
3, NetLogic::OR, 1);
des->add_node(gate);
connect(gate->pin(1), sub_enable);
connect(gate->pin(2), top_enable);
top_enable.unlink();
connect(top_enable, gate->pin(0));
NetNet*sig = new NetNet(scope, scope->local_symbol(), NetNet::WIRE,
&netvector_t::scalar_logic);
sig->local_flag(true);
connect(sig->pin(0), gate->pin(0));
} else {
connect(top_enable, sub_enable);
}
}
static void merge_sequential_masks(NetProc::mask_t&top_mask, const NetProc::mask_t&sub_mask)
{
if (sub_mask.size() == 0)
return;
if (top_mask.size() == 0) {
top_mask = sub_mask;
return;
}
assert(top_mask.size() == sub_mask.size());
for (unsigned idx = 0 ; idx < top_mask.size() ; idx += 1) {
if (sub_mask[idx] == true)
top_mask[idx] = true;
}
}
static void merge_parallel_masks(NetProc::mask_t&top_mask, const NetProc::mask_t&sub_mask)
{
if (sub_mask.size() == 0)
return;
if (top_mask.size() == 0) {
top_mask = sub_mask;
return;
}
assert(top_mask.size() == sub_mask.size());
for (unsigned idx = 0 ; idx < top_mask.size() ; idx += 1) {
if (sub_mask[idx] == false)
top_mask[idx] = false;
}
}
static bool all_bits_driven(const NetProc::mask_t&mask)
{
if (mask.size() == 0)
return false;
for (unsigned idx = 0 ; idx < mask.size() ; idx += 1) {
if (mask[idx] == false)
return false;
}
return true;
}
bool NetProcTop::tie_off_floating_inputs_(Design*des,
NexusSet&nex_map, NetBus&nex_in,
const vector<NetProc::mask_t>&bitmasks,
bool is_ff_input)
{
bool flag = true;
for (unsigned idx = 0 ; idx < nex_in.pin_count() ; idx += 1) {
if (nex_in.pin(idx).nexus()->has_floating_input()) {
if (all_bits_driven(bitmasks[idx])) {
// If all bits are unconditionally driven, we can
// use the enable signal to prevent the flip-flop/
// latch from updating when an undriven mux input
// is selected, so we can just tie off the input.
unsigned width = nex_map[idx].wid;
NetLogic*gate = new NetLogic(scope(), scope()->local_symbol(),
1, NetLogic::PULLDOWN, width);
des->add_node(gate);
connect(nex_in.pin(idx), gate->pin(0));
if (nex_in.pin(idx).nexus()->pick_any_net())
continue;
ivl_variable_type_t data_type = IVL_VT_LOGIC;
netvector_t*tmp_vec = new netvector_t(data_type, width-1,0);
NetNet*sig = new NetNet(scope(), scope()->local_symbol(),
NetNet::WIRE, tmp_vec);
sig->local_flag(true);
connect(sig->pin(0), gate->pin(0));
} else if (is_ff_input) {
// For a flip-flop, we can feed back the output
// to ensure undriven bits hold their last value.
connect(nex_in.pin(idx), nex_map[idx].lnk);
} else {
// This infers a latch, but without generating
// gate enable signals at the bit-level, we
// can't safely latch the undriven bits (we
// shouldn't generate combinatorial loops).
cerr << get_fileline() << ": warning: A latch "
<< "has been inferred for some bits of '"
<< nex_map[idx].lnk.nexus()->pick_any_net()->name()
<< "'." << endl;
cerr << get_fileline() << ": sorry: Bit-level "
"latch gate enables are not currently "
"supported in synthesis." << endl;
des->errors += 1;
flag = false;
}
}
}
return flag;
}
bool NetProc::synth_async(Design*, NetScope*, NexusSet&, NetBus&, NetBus&, vector<mask_t>&)
{
return false;
}
/*
* Async synthesis of assignments is done by synthesizing the rvalue
* expression, then connecting the l-value directly to the output of
* the r-value.
*
* The nex_map is the O-set for the statement, and lists the positions
* of the outputs as the caller wants results linked up. The nex_out,
* however, is the set of nexa that are to actually get linked to the
* r-value.
*/
bool NetAssignBase::synth_async(Design*des, NetScope*scope,
NexusSet&nex_map, NetBus&nex_out,
NetBus&enables, vector<mask_t>&bitmasks)
{
if (dynamic_cast<NetCAssign*>(this) || dynamic_cast<NetDeassign*>(this) ||
dynamic_cast<NetForce*>(this) || dynamic_cast<NetRelease*>(this)) {
cerr << get_fileline() << ": sorry: Procedural continuous "
"assignment is not currently supported in synthesis."
<< endl;
des->errors += 1;
return false;
}
/* If the lval is a concatenation, synthesise each part
separately. */
if (lval_->more ) {
/* Temporarily set the lval_ and rval_ fields for each
part in turn and recurse. Restore them when done. */
NetAssign_*full_lval = lval_;
NetExpr*full_rval = rval_;
unsigned offset = 0;
bool flag = true;
while (lval_) {
unsigned width = lval_->lwidth();
NetEConst*base = new NetEConst(verinum(offset));
base->set_line(*this);
rval_ = new NetESelect(full_rval->dup_expr(), base, width);
rval_->set_line(*this);
eval_expr(rval_, width);
NetAssign_*more = lval_->more;
lval_->more = 0;
if (!synth_async(des, scope, nex_map, nex_out, enables, bitmasks))
flag = false;
lval_ = lval_->more = more;
offset += width;
}
lval_ = full_lval;
rval_ = full_rval;
return flag;
}
assert(rval_);
NetNet*rsig = rval_->synthesize(des, scope, rval_);
assert(rsig);
if (lval_->word() && ! dynamic_cast<NetEConst*>(lval_->word())) {
cerr << get_fileline() << ": sorry: Assignment to variable "
"location in memory is not currently supported in "
"synthesis." << endl;
des->errors += 1;
return false;
}
NetNet*lsig = lval_->sig();
if (!lsig) {
cerr << get_fileline() << ": error: "
"NetAssignBase::synth_async on unsupported lval ";
dump_lval(cerr);
cerr << endl;
des->errors += 1;
return false;
}
if (debug_synth2) {
cerr << get_fileline() << ": NetAssignBase::synth_async: "
<< "l-value signal is " << lsig->vector_width() << " bits, "
<< "r-value signal is " << rsig->vector_width() << " bits." << endl;
cerr << get_fileline() << ": NetAssignBase::synth_async: "
<< "lval_->lwidth()=" << lval_->lwidth() << endl;
cerr << get_fileline() << ": NetAssignBase::synth_async: "
<< "lsig = " << scope_path(scope) << "." << lsig->name() << endl;
if (const NetExpr*base = lval_->get_base()) {
cerr << get_fileline() << ": NetAssignBase::synth_async: "
<< "base_=" << *base << endl;
}
cerr << get_fileline() << ": NetAssignBase::synth_async: "
<< "nex_map.size()==" << nex_map.size()
<< ", nex_out.pin_count()==" << nex_out.pin_count() << endl;
}
unsigned ptr = 0;
if (nex_out.pin_count() > 1) {
NexusSet tmp_set;
nex_output(tmp_set);
ivl_assert(*this, tmp_set.size() == 1);
ptr = nex_map.find_nexus(tmp_set[0]);
ivl_assert(*this, nex_out.pin_count() > ptr);
ivl_assert(*this, enables.pin_count() > ptr);
ivl_assert(*this, bitmasks.size() > ptr);
} else {
ivl_assert(*this, nex_out.pin_count() == 1);
ivl_assert(*this, enables.pin_count() == 1);
ivl_assert(*this, bitmasks.size() == 1);
}
unsigned lval_width = lval_->lwidth();
unsigned lsig_width = lsig->vector_width();
ivl_assert(*this, nex_map[ptr].wid == lsig_width);
// Here we note if the l-value is actually a bit/part
// select. If so, generate a NetPartSelect to perform the select.
bool is_part_select = lval_width != lsig_width;
long base_off = 0;
if (is_part_select && !scope->loop_index_tmp.empty()) {
// If we are within a NetForLoop, there may be an index
// value. That is collected from the scope member
// loop_index_tmp, and the evaluate_function method
// knows how to apply it.
ivl_assert(*this, !scope->loop_index_tmp.empty());
ivl_assert(*this, lval_width < lsig_width);
// Evaluate the index expression to a constant.
const NetExpr*base_expr_raw = lval_->get_base();
ivl_assert(*this, base_expr_raw);
NetExpr*base_expr = base_expr_raw->evaluate_function(*this, scope->loop_index_tmp);
if (! eval_as_long(base_off, base_expr)) {
ivl_assert(*this, 0);
}
ivl_assert(*this, base_off >= 0);
ivl_variable_type_t tmp_data_type = rsig->data_type();
netvector_t*tmp_type = new netvector_t(tmp_data_type, lsig_width-1,0);
NetNet*tmp = new NetNet(scope, scope->local_symbol(),
NetNet::WIRE, tmp_type);
tmp->local_flag(true);
tmp->set_line(*this);
NetPartSelect*ps = new NetPartSelect(tmp, base_off, lval_width, NetPartSelect::PV);
ps->set_line(*this);
des->add_node(ps);
connect(ps->pin(0), rsig->pin(0));
rsig = tmp;
} else if (is_part_select) {
// In this case, there is no loop_index_tmp, so we are
// not within a NetForLoop. Generate a NetSubstitute
// object to handle the bit/part-select in the l-value.
ivl_assert(*this, scope->loop_index_tmp.empty());
ivl_assert(*this, lval_width < lsig_width);
const NetExpr*base_expr_raw = lval_->get_base();
ivl_assert(*this, base_expr_raw);
NetExpr*base_expr = base_expr_raw->evaluate_function(*this, scope->loop_index_tmp);
if (! eval_as_long(base_off, base_expr)) {
cerr << get_fileline() << ": sorry: assignment to variable "
"bit location is not currently supported in "
"synthesis." << endl;
des->errors += 1;
return false;
}
ivl_assert(*this, base_off >= 0);
ivl_variable_type_t tmp_data_type = rsig->data_type();
netvector_t*tmp_type = new netvector_t(tmp_data_type, lsig_width-1,0);
NetNet*tmp = new NetNet(scope, scope->local_symbol(),
NetNet::WIRE, tmp_type);
tmp->local_flag(true);
tmp->set_line(*this);
NetNet*isig = nex_out.pin(ptr).nexus()->pick_any_net();
if (isig) {
if (debug_synth2) {
cerr << get_fileline() << ": NetAssignBase::synth_async: "
<< " Found an isig:" << endl;
nex_out.pin(ptr).dump_link(cerr, 8);
}
} else {
if (debug_synth2) {
cerr << get_fileline() << ": NetAssignBase::synth_async: "
<< " Found no isig, resorting to lsig." << endl;
}
isig = new NetNet(scope, scope->local_symbol(),
NetNet::WIRE, tmp_type);
isig->local_flag(true);
isig->set_line(*this);
connect(isig->pin(0), nex_out.pin(ptr));
}
ivl_assert(*this, isig);
NetSubstitute*ps = new NetSubstitute(isig, rsig, lsig_width, base_off);
ps->set_line(*this);
des->add_node(ps);
connect(ps->pin(0), tmp->pin(0));
rsig = tmp;
}
rsig = crop_to_width(des, rsig, lsig_width);
ivl_assert(*this, rsig->pin_count()==1);
nex_out.pin(ptr).unlink();
enables.pin(ptr).unlink();
connect(nex_out.pin(ptr), rsig->pin(0));
connect(enables.pin(ptr), scope->tie_hi());
mask_t&bitmask = bitmasks[ptr];
if (is_part_select) {
if (bitmask.size() == 0) {
bitmask = mask_t (lsig_width, false);
}
ivl_assert(*this, bitmask.size() == lsig_width);
for (unsigned idx = 0; idx < lval_width; idx += 1) {
bitmask[base_off + idx] = true;
}
} else if (bitmask.size() > 0) {
for (unsigned idx = 0; idx < bitmask.size(); idx += 1) {
bitmask[idx] = true;
}
} else {
bitmask = mask_t (lsig_width, true);
}
/* This lval_ represents a reg that is a WIRE in the
synthesized results. This function signals the destructor
to change the REG that this l-value refers to into a
WIRE. It is done then, at the last minute, so that pending
synthesis can continue to work with it as a REG. */
lval_->turn_sig_to_wire_on_release();
return true;
}
bool NetProc::synth_async_block_substatement_(Design*des, NetScope*scope,
NexusSet&nex_map,
NetBus&nex_out,
NetBus&enables,
vector<mask_t>&bitmasks,
NetProc*substmt)
{
ivl_assert(*this, nex_map.size() == nex_out.pin_count());
ivl_assert(*this, nex_map.size() == enables.pin_count());
ivl_assert(*this, nex_map.size() == bitmasks.size());
// Create a temporary map of the output only from this statement.
NexusSet tmp_map;
substmt->nex_output(tmp_map);
if (debug_synth2) {
cerr << get_fileline() << ": NetProc::synth_async_block_substatement_: "
<< "tmp_map.size()==" << tmp_map.size()
<< " for statement at " << substmt->get_fileline()
<< endl;
for (unsigned idx = 0 ; idx < nex_out.pin_count() ; idx += 1) {
cerr << get_fileline() << ": NetProc::synth_async_block_substatement_: "
<< "incoming nex_out[" << idx << "] dump link" << endl;
nex_out.pin(idx).dump_link(cerr, 8);
}
}
// Create temporary variables to collect the output from the synthesis.
NetBus tmp_out (scope, tmp_map.size());
NetBus tmp_ena (scope, tmp_map.size());
vector<mask_t> tmp_masks (tmp_map.size());
// Map (and move) the accumulated nex_out for this block
// to the version that we can pass to the next statement.
// We will move the result back later.
for (unsigned idx = 0 ; idx < tmp_out.pin_count() ; idx += 1) {
unsigned ptr = nex_map.find_nexus(tmp_map[idx]);
ivl_assert(*this, ptr < nex_out.pin_count());
connect(tmp_out.pin(idx), nex_out.pin(ptr));
nex_out.pin(ptr).unlink();
}
if (debug_synth2) {
for (unsigned idx = 0 ; idx < nex_map.size() ; idx += 1) {
cerr << get_fileline() << ": NetProc::synth_async_block_substatement_: nex_map[" << idx << "] dump link, base=" << nex_map[idx].base << ", wid=" << nex_map[idx].wid << endl;
nex_map[idx].lnk.dump_link(cerr, 8);
}
for (unsigned idx = 0 ; idx < tmp_map.size() ; idx += 1) {
cerr << get_fileline() << ": NetProc::synth_async_block_substatement_: tmp_map[" << idx << "] dump link, base=" << tmp_map[idx].base << ", wid=" << tmp_map[idx].wid << endl;
tmp_map[idx].lnk.dump_link(cerr, 8);
}
for (unsigned idx = 0 ; idx < tmp_out.pin_count() ; idx += 1) {
cerr << get_fileline() << ": NetProc::synth_async_block_substatement_: tmp_out[" << idx << "] dump link" << endl;
tmp_out.pin(idx).dump_link(cerr, 8);
}
}
bool flag = substmt->synth_async(des, scope, tmp_map, tmp_out, tmp_ena, tmp_masks);
if (debug_synth2) {
cerr << get_fileline() << ": NetProc::synth_async_block_substatement_: "
"substmt->synch_async(...) --> " << (flag? "true" : "false")
<< " for statement at " << substmt->get_fileline() << "." << endl;
}
if (!flag) return false;
// Now map the output from the substatement back to the
// outputs for this block.
for (unsigned idx = 0 ; idx < tmp_out.pin_count() ; idx += 1) {
unsigned ptr = nex_map.find_nexus(tmp_map[idx]);
ivl_assert(*this, ptr < nex_out.pin_count());
if (debug_synth2) {
cerr << get_fileline() << ": NetProc::synth_async_block_substatement_: "
<< "tmp_out.pin(" << idx << "):" << endl;
tmp_out.pin(idx).dump_link(cerr, 8);
}
connect(nex_out.pin(ptr), tmp_out.pin(idx));
merge_sequential_enables(des, scope, enables.pin(ptr), tmp_ena.pin(idx));
merge_sequential_masks(bitmasks[ptr], tmp_masks[idx]);
}
return true;
}
/*
* Sequential blocks are translated to asynchronous logic by
* translating each statement of the block, in order, into gates.
* The nex_out for the block is the union of the nex_out for all
* the substatements.
*/
bool NetBlock::synth_async(Design*des, NetScope*scope,
NexusSet&nex_map, NetBus&nex_out,
NetBus&enables, vector<mask_t>&bitmasks)
{
if (last_ == 0) {
return true;
}
bool flag = true;
NetProc*cur = last_;
do {
cur = cur->next_;
bool sub_flag = synth_async_block_substatement_(des, scope, nex_map, nex_out,
enables, bitmasks, cur);
flag = flag && sub_flag;
} while (cur != last_);
return flag;
}
/*
* This function is used to fix up a MUX selector to be no longer than
* it needs to be. The general idea is that if the selector needs to
* be only N bits, but is actually M bits, we translate it to this:
*
* osig = { |esig[M-1:N-1], esig[N-2:0] }
*
* This obviously implies that (N >= 2) and (M >= N). In the code
* below, N is sel_need, and M is sel_got (= esig->vector_width()).
*/
static NetNet* mux_selector_reduce_width(Design*des, NetScope*scope,
const LineInfo&loc,
NetNet*esig, unsigned sel_need)
{
const unsigned sel_got = esig->vector_width();
ivl_assert(*esig, sel_got >= sel_need);
// If the actual width matches the desired width (M==N) then
// osig is esig itself. We're done.
if (sel_got == sel_need)
return esig;
if (debug_synth2) {
cerr << loc.get_fileline() << ": mux_selector_reduce_width: "
<< "Reduce selector width=" << sel_got
<< " to " << sel_need << " bits." << endl;
}
ivl_assert(*esig, sel_need >= 2);
// This is the output signal, osig.
ivl_variable_type_t osig_data_type = IVL_VT_LOGIC;
netvector_t*osig_vec = new netvector_t(osig_data_type, sel_need-1, 0);
NetNet*osig = new NetNet(scope, scope->local_symbol(),
NetNet::TRI, osig_vec);
osig->local_flag(true);
osig->set_line(loc);
// Create the concat: osig = {...,...}
NetConcat*osig_cat = new NetConcat(scope, scope->local_symbol(),
sel_need, 2, !disable_concatz_generation);
osig_cat->set_line(loc);
des->add_node(osig_cat);
connect(osig_cat->pin(0), osig->pin(0));
// Create the part select esig[N-2:0]...
NetPartSelect*ps0 = new NetPartSelect(esig, 0, sel_need-1,
NetPartSelect::VP);
ps0->set_line(loc);
des->add_node(ps0);
connect(ps0->pin(1), esig->pin(0));
netvector_t*ps0_vec = new netvector_t(osig_data_type, sel_need-2, 0);
NetNet*ps0_sig = new NetNet(scope, scope->local_symbol(),
NetNet::TRI, ps0_vec);
ps0_sig->local_flag(true);
ps0_sig->set_line(loc);
connect(ps0_sig->pin(0), ps0->pin(0));
// osig = {..., esig[N-2:0]}
connect(osig_cat->pin(1), ps0_sig->pin(0));
// Create the part select esig[M-1:N-1]
NetPartSelect*ps1 = new NetPartSelect(esig, sel_need-1,
sel_got-sel_need+1,
NetPartSelect::VP);
ps1->set_line(loc);
des->add_node(ps1);
connect(ps1->pin(1), esig->pin(0));
netvector_t*ps1_vec = new netvector_t(osig_data_type, sel_got-sel_need, 0);
NetNet*ps1_sig = new NetNet(scope, scope->local_symbol(),
NetNet::TRI, ps1_vec);
ps1_sig->local_flag(true);
ps1_sig->set_line(loc);
connect(ps1_sig->pin(0), ps1->pin(0));
// Create the reduction OR: | esig[M-1:N-1]
NetUReduce*ered = new NetUReduce(scope, scope->local_symbol(),
NetUReduce::OR, sel_got-sel_need+1);
ered->set_line(loc);
des->add_node(ered);
connect(ered->pin(1), ps1_sig->pin(0));
NetNet*ered_sig = new NetNet(scope, scope->local_symbol(),
NetNet::TRI, &netvector_t::scalar_logic);
ered_sig->local_flag(true);
ered_sig->set_line(loc);
connect(ered->pin(0), ered_sig->pin(0));
// osig = { |esig[M-1:N-1], esig[N-2:0] }
connect(osig_cat->pin(2), ered_sig->pin(0));
return osig;
}
bool NetCase::synth_async(Design*des, NetScope*scope,
NexusSet&nex_map, NetBus&nex_out,
NetBus&enables, vector<mask_t>&bitmasks)
{
if (type()==NetCase::EQZ || type()==NetCase::EQX)
return synth_async_casez_(des, scope, nex_map, nex_out,
enables, bitmasks);
// Special case: If the case expression is constant, then this
// is a pattern where the guards are non-constant and tested
// against a constant case. Handle this as chained conditions
// instead.
if (dynamic_cast<NetEConst*> (expr_))
return synth_async_casez_(des, scope, nex_map, nex_out,
enables, bitmasks);
ivl_assert(*this, nex_map.size() == nex_out.pin_count());
ivl_assert(*this, nex_map.size() == enables.pin_count());
ivl_assert(*this, nex_map.size() == bitmasks.size());
if (debug_synth2) {
cerr << get_fileline() << ": NetCase::synth_async: "
<< "Selector expression: " << *expr_ << endl;
}
/* Synthesize the select expression. */
NetNet*esig = expr_->synthesize(des, scope, expr_);
unsigned sel_width = esig->vector_width();
ivl_assert(*this, sel_width > 0);
if (debug_synth2) {
cerr << get_fileline() << ": NetCase::synth_async: "
<< "selector width (sel_width) = " << sel_width << endl;
}
vector<unsigned> mux_width (nex_out.pin_count());
for (unsigned idx = 0 ; idx < nex_out.pin_count() ; idx += 1) {
mux_width[idx] = nex_map[idx].wid;
if (debug_synth2) {
cerr << get_fileline() << ": NetCase::synth_async: "
<< "idx=" << idx
<< ", mux_width[idx]=" << mux_width[idx] << endl;
}
}
// The incoming nex_out is taken as the input for this
// statement. Since there are collection of statements
// that start at this same point, we save all these
// inputs and reuse them for each statement. Unlink the
// nex_out now, so we can hook up the mux outputs.
NetBus statement_input (scope, nex_out.pin_count());
for (unsigned idx = 0 ; idx < nex_out.pin_count() ; idx += 1) {
connect(statement_input.pin(idx), nex_out.pin(idx));
nex_out.pin(idx).unlink();
if (debug_synth2) {
cerr << get_fileline() << ": NetCase::synth_async: "
<< "statement_input.pin(" << idx << "):" << endl;
statement_input.pin(idx).dump_link(cerr, 8);
}
}
/* Collect all the statements into a map of index to statement.
The guard expression it evaluated to be the index of the mux
value, and the statement is bound to that index. */
unsigned long max_guard_value = 0;
map<unsigned long,NetProc*>statement_map;
NetProc*default_statement = 0;
for (size_t item = 0 ; item < items_.size() ; item += 1) {
if (items_[item].guard == 0) {
default_statement = items_[item].statement;
continue;
}
NetEConst*ge = dynamic_cast<NetEConst*>(items_[item].guard);
if (ge == 0) {
cerr << items_[item].guard->get_fileline() << ": sorry: "
<< "variable case item expressions with a variable "
<< "case select expression are not supported in "
<< "synthesis. " << endl;
des->errors += 1;
return false;
}
ivl_assert(*this, ge);
verinum gval = ge->value();
unsigned long sel_idx = gval.as_ulong();
if (statement_map[sel_idx]) {
cerr << ge->get_fileline() << ": warning: duplicate case "
<< "value '" << sel_idx << "' detected. This case is "
<< "unreachable." << endl;
delete items_[item].statement;
items_[item].statement = 0;
continue;
}
if (sel_idx > max_guard_value)
max_guard_value = sel_idx;
if (items_[item].statement) {
statement_map[sel_idx] = items_[item].statement;
continue;
}
// Handle the special case of an empty statement.
statement_map[sel_idx] = this;
}
// The minimum selector width is the number of inputs that
// are selected, rounded up to the nearest power of 2.
unsigned sel_need = max(ceil(log2(max_guard_value + 1)), 1.0);
// If the sel_width can select more than just the explicit
// guard values, and there is a default statement, then adjust
// the sel_need to allow for the implicit selections.
if (default_statement && (sel_width > sel_need))
sel_need += 1;
// The mux size is always an exact power of 2.
if (sel_need >= 8*sizeof(unsigned)) {
cerr << get_fileline() << ": sorry: mux select width of "
<< sel_need << " bits is too large for synthesis." << endl;
des->errors += 1;
return false;
}
unsigned mux_size = 1U << sel_need;
if (debug_synth2) {
cerr << get_fileline() << ": NetCase::synth_async: "
<< "Adjusted mux_size is " << mux_size
<< " (max_guard_value=" << max_guard_value
<< ", sel_need=" << sel_need
<< ", sel_width=" << sel_width << ")." << endl;
}
if (sel_width > sel_need) {
if (debug_synth2) {
cerr << get_fileline() << ": NetCase::synth_async: "
<< "Selector is " << sel_width << " bits, "
<< "need only " << sel_need << " bits." << endl;
}
esig = mux_selector_reduce_width(des, scope, *this, esig, sel_need);
}
/* If there is a default clause, synthesize it once and we'll
link it in wherever it is needed. If there isn't, create
a dummy default to pass on the accumulated nex_out from
preceding statements. */
NetBus default_out (scope, nex_out.pin_count());
NetBus default_ena (scope, nex_out.pin_count());
vector<mask_t> default_masks (nex_out.pin_count());
for (unsigned idx = 0 ; idx < nex_out.pin_count() ; idx += 1) {
connect(default_out.pin(idx), statement_input.pin(idx));
connect(default_ena.pin(idx), scope->tie_lo());
}
if (default_statement) {
bool flag = synth_async_block_substatement_(des, scope, nex_map, default_out,
default_ena, default_masks,
default_statement);
if (!flag) return false;
if (debug_synth2) {
cerr << get_fileline() << ": NetCase::synth_async: "
<< "synthesize default clause at " << default_statement->get_fileline()
<< " is done." << endl;
}
}
vector<NetMux*> out_mux (nex_out.pin_count());
vector<NetMux*> ena_mux (nex_out.pin_count());
vector<bool> full_case (nex_out.pin_count());
for (size_t mdx = 0 ; mdx < nex_out.pin_count() ; mdx += 1) {
out_mux[mdx] = new NetMux(scope, scope->local_symbol(),
mux_width[mdx], mux_size, sel_need);
des->add_node(out_mux[mdx]);
// The select signal is already synthesized, and is
// common for every mux of this case statement. Simply
// hook it up.
connect(out_mux[mdx]->pin_Sel(), esig->pin(0));
// The outputs are in the nex_out, and connected to the
// mux Result pins.
connect(out_mux[mdx]->pin_Result(), nex_out.pin(mdx));
// Make sure the output is now connected to a net. If
// not, then create a fake one to carry the net-ness of
// the pin.
if (out_mux[mdx]->pin_Result().nexus()->pick_any_net() == 0) {
ivl_variable_type_t mux_data_type = IVL_VT_LOGIC;
netvector_t*tmp_vec = new netvector_t(mux_data_type, mux_width[mdx]-1,0);
NetNet*tmp = new NetNet(scope, scope->local_symbol(),
NetNet::WIRE, tmp_vec);
tmp->local_flag(true);
ivl_assert(*this, tmp->vector_width() != 0);
connect(out_mux[mdx]->pin_Result(), tmp->pin(0));
}
// Create a mux for the enables, but don't hook it up
// until we know we need it.
ena_mux[mdx] = new NetMux(scope, scope->local_symbol(),
1, mux_size, sel_need);
// Assume a full case to start with. We'll check this as
// we synthesise each clause.
full_case[mdx] = true;
}
for (unsigned idx = 0 ; idx < mux_size ; idx += 1) {
NetProc*stmt = statement_map[idx];
if (stmt==0) {
ivl_assert(*this, default_out.pin_count() == out_mux.size());
for (unsigned mdx = 0 ; mdx < nex_out.pin_count() ; mdx += 1) {
connect(out_mux[mdx]->pin_Data(idx), default_out.pin(mdx));
connect(ena_mux[mdx]->pin_Data(idx), default_ena.pin(mdx));
merge_parallel_masks(bitmasks[mdx], default_masks[mdx]);
if (!default_ena.pin(mdx).is_linked(scope->tie_hi()))
full_case[mdx] = false;
}
continue;
}
ivl_assert(*this, stmt);
if (stmt == this) {
// Handle the special case of an empty statement.
ivl_assert(*this, statement_input.pin_count() == out_mux.size());
for (unsigned mdx = 0 ; mdx < nex_out.pin_count() ; mdx += 1) {
connect(out_mux[mdx]->pin_Data(idx), statement_input.pin(mdx));
connect(ena_mux[mdx]->pin_Data(idx), scope->tie_lo());
bitmasks[mdx] = mask_t (mux_width[mdx], false);
full_case[mdx] = false;
}
continue;
}
NetBus tmp_out (scope, nex_out.pin_count());
NetBus tmp_ena (scope, nex_out.pin_count());
for (unsigned mdx = 0 ; mdx < nex_out.pin_count() ; mdx += 1) {
connect(tmp_out.pin(mdx), statement_input.pin(mdx));
connect(tmp_ena.pin(mdx), scope->tie_lo());
}
vector<mask_t> tmp_masks (nex_out.pin_count());
bool flag = synth_async_block_substatement_(des, scope, nex_map, tmp_out,
tmp_ena, tmp_masks, stmt);
if (!flag) return false;
for (size_t mdx = 0 ; mdx < nex_out.pin_count() ; mdx += 1) {
connect(out_mux[mdx]->pin_Data(idx), tmp_out.pin(mdx));
connect(ena_mux[mdx]->pin_Data(idx), tmp_ena.pin(mdx));