-
Notifications
You must be signed in to change notification settings - Fork 541
/
Copy pathlogic_lpm.c
2498 lines (2410 loc) · 85.6 KB
/
logic_lpm.c
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) 2011-2024 Cary R. ([email protected])
*
* 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 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 <stdlib.h>
# include <string.h>
# include "config.h"
# include "vlog95_priv.h"
/*
* Data type used to signify if a $signed or $unsigned should be emitted.
*/
typedef enum lpm_sign_e {
NO_SIGN = 0,
NEED_SIGNED = 1,
NEED_UNSIGNED = 2
} lpm_sign_t;
static ivl_signal_t nexus_is_signal(ivl_scope_t scope, ivl_nexus_t nex,
int*msb, int*lsb, unsigned*array_word,
ivl_net_const_t*accept_const);
/*
* Look to see if the nexus driver is signed.
*/
static int nexus_driver_is_signed(ivl_scope_t scope, ivl_nexus_t nex)
{
int is_signed = 0;
unsigned sign_set = 0;
unsigned idx, count = ivl_nexus_ptrs(nex);
for (idx = 0; idx < count; idx += 1) {
ivl_nexus_ptr_t nex_ptr = ivl_nexus_ptr(nex, idx);
ivl_lpm_t t_lpm = ivl_nexus_ptr_lpm(nex_ptr);
ivl_net_const_t t_net_const = ivl_nexus_ptr_con(nex_ptr);
ivl_net_logic_t t_nlogic = ivl_nexus_ptr_log(nex_ptr);
ivl_signal_t t_sig = ivl_nexus_ptr_sig(nex_ptr);
ivl_drive_t cur_drive1 = ivl_nexus_ptr_drive1(nex_ptr);
ivl_drive_t cur_drive0 = ivl_nexus_ptr_drive0(nex_ptr);
if ((cur_drive1 == IVL_DR_HiZ) &&
(cur_drive0 == IVL_DR_HiZ)) continue;
/* Only one driver can set the sign information. */
assert( ! sign_set);
if (t_lpm) {
sign_set = 1;
is_signed = ivl_lpm_signed(t_lpm);
}
if (t_net_const) {
sign_set = 1;
is_signed = ivl_const_signed(t_net_const);
}
if (t_nlogic) {
sign_set = 1;
/* A BUFZ is used to drive a local signal so look for the
* local signal to get the sign information. */
if (ivl_logic_type(t_nlogic) == IVL_LO_BUFZ) {
unsigned array_word = 0;
int msb = 0, lsb = 0;
ivl_signal_t sig;
assert(ivl_logic_pins(t_nlogic) == 2);
sig = nexus_is_signal(scope,
ivl_logic_pin(t_nlogic, 0),
&msb, &lsb, &array_word, 0);
assert(sig);
is_signed = ivl_signal_signed(sig);
}
/* The rest of the logic type are always unsigned. */
}
if (t_sig) {
sign_set = 1;
is_signed = ivl_signal_signed(t_sig);
}
}
return is_signed;
}
static unsigned get_nexus_width(ivl_nexus_t nex)
{
unsigned idx, count = ivl_nexus_ptrs(nex);
for (idx = 0; idx < count; idx += 1) {
ivl_nexus_ptr_t nex_ptr = ivl_nexus_ptr(nex, idx);
ivl_signal_t sig = ivl_nexus_ptr_sig(nex_ptr);
if (sig) return ivl_signal_width(sig);
}
assert(0);
return 0;
}
static lpm_sign_t lpm_get_sign_type(ivl_lpm_t lpm,
unsigned can_skip_unsigned)
{
lpm_sign_t rtn = NO_SIGN;
int opr_sign = 0;
int lpm_sign = ivl_lpm_signed(lpm);
switch (ivl_lpm_type(lpm)) {
case IVL_LPM_SIGN_EXT:
opr_sign = nexus_driver_is_signed(ivl_lpm_scope(lpm),
ivl_lpm_data(lpm, 0));
break;
default:
opr_sign = lpm_sign;
break;
}
/* Check to see if a $signed() or $unsigned() is needed. */
if (lpm_sign && ! opr_sign) rtn = NEED_SIGNED;
if (! lpm_sign && opr_sign && ! can_skip_unsigned) rtn = NEED_UNSIGNED;
return rtn;
}
static unsigned emit_drive(ivl_drive_t drive)
{
switch (drive) {
case IVL_DR_HiZ:
fprintf(vlog_out, "highz");
break;
case IVL_DR_WEAK:
fprintf(vlog_out, "weak");
break;
case IVL_DR_PULL:
fprintf(vlog_out, "pull");
break;
case IVL_DR_STRONG:
fprintf(vlog_out, "strong");
break;
case IVL_DR_SUPPLY:
fprintf(vlog_out, "supply");
break;
default:
return 1;
break;
}
return 0;
}
/*
* If the strength type is 2 then emit both strengths. If it is 1 then only
* emit the 1 strength (pullup) and if it is 0 only emit the 0 strength
* (pulldown).
*/
static void emit_strength(ivl_drive_t drive1, ivl_drive_t drive0,
unsigned strength_type, const char *type,
const char *file, unsigned lineno)
{
assert(strength_type <= 2);
if ((strength_type == 2) &&
((drive1 != IVL_DR_STRONG) || (drive0 != IVL_DR_STRONG))) {
fprintf(vlog_out, " (");
if (emit_drive(drive1)) {
fprintf(vlog_out, "<invalid>");
fprintf(stderr, "%s:%u: vlog95 error: Unsupported %s "
"1 drive (%d)\n", file, lineno,
type, (int)drive1);
vlog_errors += 1;
}
fprintf(vlog_out, "1, ");
if (emit_drive(drive0)) {
fprintf(vlog_out, "<invalid>");
fprintf(stderr, "%s:%u: vlog95 error: Unsupported %s "
"0 drive (%d)\n", file, lineno,
type, (int)drive0);
vlog_errors += 1;
}
fprintf(vlog_out, "0)");
} else if ((strength_type == 1) && (drive1 != IVL_DR_PULL)) {
fprintf(vlog_out, " (");
if (emit_drive(drive1)) {
fprintf(vlog_out, "<invalid>");
fprintf(stderr, "%s:%u: vlog95 error: Unsupported %s "
"1 drive (%d)\n", file, lineno,
type, (int)drive1);
vlog_errors += 1;
}
fprintf(vlog_out, "1)");
} else if ((strength_type == 0) && (drive0 != IVL_DR_PULL)) {
fprintf(vlog_out, " (");
if (emit_drive(drive0)) {
fprintf(vlog_out, "<invalid>");
fprintf(stderr, "%s:%u: vlog95 error: Unsupported %s "
"0 drive (%d)\n", file, lineno,
type, (int)drive0);
vlog_errors += 1;
}
fprintf(vlog_out, "0)");
}
}
static void emit_gate_strength(ivl_net_logic_t nlogic, unsigned strength_type)
{
emit_strength(ivl_logic_drive1(nlogic), ivl_logic_drive0(nlogic),
strength_type,
"gate", ivl_logic_file(nlogic), ivl_logic_lineno(nlogic));
}
static void emit_part_selector(int msb, int lsb)
{
if (msb != lsb)
fprintf(vlog_out, "[%d:%d]", msb, lsb);
else
fprintf(vlog_out, "[%d]", lsb);
}
/*
* Look for a single driver behind an LPM that passes strength information
* and get the real drive information from it.
*/
static void get_unique_lpm_drive(ivl_lpm_t lpm, ivl_drive_t *drive1,
ivl_drive_t *drive0)
{
ivl_nexus_t nex = ivl_lpm_data(lpm, 0);
unsigned idx, count = ivl_nexus_ptrs(nex);
unsigned have_driver = 0;
for (idx = 0; idx < count; idx += 1) {
ivl_nexus_ptr_t nex_ptr = ivl_nexus_ptr(nex, idx);
ivl_drive_t cur_drive1 = ivl_nexus_ptr_drive1(nex_ptr);
ivl_drive_t cur_drive0 = ivl_nexus_ptr_drive0(nex_ptr);
if ((cur_drive1 == IVL_DR_HiZ) &&
(cur_drive0 == IVL_DR_HiZ)) continue;
assert(! have_driver);
*drive1 = cur_drive1;
*drive0 = cur_drive0;
have_driver = 1;
}
/* This should never happen. */
if (! have_driver) {
fprintf(stderr, "%s:%u: vlog95 error: Unable to find drive "
"information for strength transparent LPM.\n",
ivl_lpm_file(lpm), ivl_lpm_lineno(lpm));
vlog_errors += 1;
}
}
static void emit_lpm_strength(ivl_lpm_t lpm)
{
ivl_lpm_type_t type = ivl_lpm_type(lpm);
ivl_drive_t drive1 = IVL_DR_STRONG;
ivl_drive_t drive0 = IVL_DR_STRONG;
/* This LPM object passes strength information so we need to look
* for the strength information at the real driver. */
if (type == IVL_LPM_PART_PV) {
get_unique_lpm_drive(lpm, &drive1, &drive0);
} else {
drive1 = ivl_lpm_drive1(lpm);
drive0 = ivl_lpm_drive0(lpm);
}
emit_strength(drive1, drive0, 2, "LPM",
ivl_lpm_file(lpm), ivl_lpm_lineno(lpm));
}
static void emit_delay(ivl_scope_t scope, ivl_expr_t rise, ivl_expr_t fall,
ivl_expr_t decay, unsigned dly_count)
{
assert((dly_count >= 2) && (dly_count <= 3));
/* No delays. */
if (! rise) {
assert(! fall);
assert(! decay);
return;
}
/* If all three delays match then we only have a single delay. */
if ((rise == fall) && (rise == decay)) {
fprintf(vlog_out, " #(");
emit_scaled_delayx(scope, rise, 0);
fprintf(vlog_out, ")");
return;
}
/* If we have a gate that only supports two delays then print them. */
if (dly_count == 2) {
fprintf(vlog_out, " #(");
emit_scaled_delayx(scope, rise, 0);
fprintf(vlog_out, ", ");
emit_scaled_delayx(scope, fall, 0);
fprintf(vlog_out, ")");
return;
}
/* What's left is a gate that supports three delays. */
fprintf(vlog_out, " #(");
emit_scaled_delayx(scope, rise, 0);
fprintf(vlog_out, ", ");
emit_scaled_delayx(scope, fall, 0);
if (decay) {
fprintf(vlog_out, ", ");
emit_scaled_delayx(scope, decay, 0);
}
fprintf(vlog_out, ")");
}
static void emit_driver_delay(ivl_scope_t scope, ivl_nexus_t nex)
{
ivl_expr_t rise = 0;
ivl_expr_t fall = 0;
ivl_expr_t decay = 0;
unsigned idx, count = ivl_nexus_ptrs(nex);
for (idx = 0; idx < count; idx += 1) {
ivl_nexus_ptr_t nex_ptr = ivl_nexus_ptr(nex, idx);
ivl_lpm_t t_lpm = ivl_nexus_ptr_lpm(nex_ptr);
ivl_net_const_t t_net_const = ivl_nexus_ptr_con(nex_ptr);
ivl_net_logic_t t_nlogic = ivl_nexus_ptr_log(nex_ptr);
ivl_drive_t cur_drive1 = ivl_nexus_ptr_drive1(nex_ptr);
ivl_drive_t cur_drive0 = ivl_nexus_ptr_drive0(nex_ptr);
if ((cur_drive1 == IVL_DR_HiZ) &&
(cur_drive0 == IVL_DR_HiZ)) continue;
/* Only one driver can set the delay. */
assert( ! rise);
if (t_lpm) {
rise = ivl_lpm_delay(t_lpm, 0);
fall = ivl_lpm_delay(t_lpm, 1);
decay = ivl_lpm_delay(t_lpm, 2);
}
if (t_net_const) {
rise = ivl_const_delay(t_net_const, 0);
fall = ivl_const_delay(t_net_const, 1);
decay = ivl_const_delay(t_net_const, 2);
}
if (t_nlogic) {
rise = ivl_logic_delay(t_nlogic, 0);
fall = ivl_logic_delay(t_nlogic, 1);
decay = ivl_logic_delay(t_nlogic, 2);
}
}
emit_delay(scope, rise, fall, decay, 3);
}
static unsigned is_local_nexus(const ivl_scope_t scope, ivl_nexus_t nex)
{
unsigned idx, count = ivl_nexus_ptrs(nex);
unsigned is_local = 1;
unsigned has_output_driver = 0;
for (idx = 0; idx < count; idx += 1) {
ivl_nexus_ptr_t nex_ptr = ivl_nexus_ptr(nex, idx);
ivl_signal_t sig = ivl_nexus_ptr_sig(nex_ptr);
if (! sig) continue;
/* Check to see if there is an output or inout port
* driving into the local scope. */
if ((scope == ivl_scope_parent(ivl_signal_scope(sig))) &&
((ivl_signal_port(sig) == IVL_SIP_OUTPUT) ||
(ivl_signal_port(sig) == IVL_SIP_INOUT))) {
has_output_driver = 1;
continue;
}
if (scope != ivl_signal_scope(sig)) continue;
if ((ivl_nexus_ptr_drive1(nex_ptr) != IVL_DR_HiZ) ||
(ivl_nexus_ptr_drive0(nex_ptr) != IVL_DR_HiZ)) continue;
if (ivl_signal_local(sig)) {
is_local = 1;
} else {
is_local = 0;
break;
}
}
/* We return is_local=true only if there is not an output or inout
* driving into this scope. This is needed since some module outputs
* are combined with a concatenation and some inouts are connected
* with a tran_VP. */
return is_local && !has_output_driver;
}
/*
* This returns the nexus of the LPM if it is not a local signal.
*/
static ivl_nexus_t get_lpm_output(ivl_scope_t scope, ivl_lpm_t lpm)
{
ivl_nexus_t output = 0;
switch (ivl_lpm_type(lpm)) {
case IVL_LPM_ABS:
case IVL_LPM_ADD:
case IVL_LPM_ARRAY:
case IVL_LPM_CAST_INT:
case IVL_LPM_CAST_INT2:
case IVL_LPM_CAST_REAL:
case IVL_LPM_CMP_EEQ:
case IVL_LPM_CMP_EQ:
case IVL_LPM_CMP_EQX:
case IVL_LPM_CMP_EQZ:
case IVL_LPM_CMP_GE:
case IVL_LPM_CMP_GT:
case IVL_LPM_CMP_NE:
case IVL_LPM_CMP_NEE:
case IVL_LPM_CMP_WEQ:
case IVL_LPM_CMP_WNE:
case IVL_LPM_CONCAT:
case IVL_LPM_CONCATZ:
case IVL_LPM_DIVIDE:
case IVL_LPM_FF:
case IVL_LPM_LATCH:
case IVL_LPM_MOD:
case IVL_LPM_MULT:
case IVL_LPM_MUX:
case IVL_LPM_PART_PV:
case IVL_LPM_PART_VP:
case IVL_LPM_POW:
case IVL_LPM_RE_AND:
case IVL_LPM_RE_NAND:
case IVL_LPM_RE_NOR:
case IVL_LPM_RE_OR:
case IVL_LPM_RE_XOR:
case IVL_LPM_RE_XNOR:
case IVL_LPM_REPEAT:
case IVL_LPM_SFUNC:
case IVL_LPM_SHIFTL:
case IVL_LPM_SHIFTR:
case IVL_LPM_SIGN_EXT:
case IVL_LPM_SUB:
case IVL_LPM_SUBSTITUTE:
case IVL_LPM_UFUNC:
/* If the output of this LPM is a local signal then something
* else will request that this be emitted. */
output = ivl_lpm_q(lpm);
if (is_local_nexus(scope, output)) return 0;
break;
default:
// HERE: Can this be a simple assert at some point in time?
fprintf(vlog_out, "<unknown>");
fprintf(stderr, "%s:%u: vlog95 error: Unknown LPM type (%d).\n",
ivl_lpm_file(lpm), ivl_lpm_lineno(lpm),
(int)ivl_lpm_type(lpm));
vlog_errors += 1;
return 0;
}
return output;
}
static void emit_logic_as_ca(ivl_scope_t scope, ivl_net_logic_t nlogic);
static void emit_lpm_as_ca(ivl_scope_t scope, ivl_lpm_t lpm,
unsigned sign_extend);
/* For an undriven port look for the local signal to get the nexus name. */
static void emit_nexus_port_signal(ivl_scope_t scope, ivl_nexus_t nex)
{
unsigned idx, count = ivl_nexus_ptrs(nex);
ivl_signal_t sig = 0;
for (idx = 0; idx < count; idx += 1) {
ivl_nexus_ptr_t nex_ptr = ivl_nexus_ptr(nex, idx);
ivl_signal_t t_sig = ivl_nexus_ptr_sig(nex_ptr);
if ((ivl_nexus_ptr_drive1(nex_ptr) != IVL_DR_HiZ) ||
(ivl_nexus_ptr_drive0(nex_ptr) != IVL_DR_HiZ)) assert(0);
if (t_sig) {
if (scope != ivl_signal_scope(t_sig)) continue;
assert(! sig);
sig = t_sig;
}
}
/* There will not be a signal for an empty port. */
if (sig) emit_nexus_as_ca(scope, ivl_signal_nex(sig, 0), 0, 0);
else fprintf(vlog_out, "/* Empty */");
}
static ivl_signal_t find_local_signal(const ivl_scope_t scope, ivl_nexus_t nex,
unsigned *word)
{
unsigned idx, count = ivl_nexus_ptrs(nex);
ivl_signal_t sig = 0;
*word = 0;
for (idx = 0; idx < count; idx += 1) {
ivl_nexus_ptr_t nex_ptr = ivl_nexus_ptr(nex, idx);
ivl_signal_t t_sig = ivl_nexus_ptr_sig(nex_ptr);
if (!t_sig) continue;
if (ivl_signal_local(t_sig) &&
(ivl_signal_port(t_sig) != IVL_SIP_INPUT)) continue;
if (ivl_signal_scope(t_sig) != scope) continue;
assert(! sig);
sig = t_sig;
*word = ivl_nexus_ptr_pin(nex_ptr);
}
return sig;
}
/*
* Emit the input port driving expression.
*/
void emit_nexus_port_driver_as_ca(ivl_scope_t scope, ivl_nexus_t nex)
{
unsigned idx, count = ivl_nexus_ptrs(nex);
ivl_lpm_t lpm = 0;
ivl_net_const_t net_const = 0;
ivl_net_logic_t nlogic = 0;
ivl_signal_t sig = 0;
unsigned word = 0;
/* Look for the nexus driver. */
for (idx = 0; idx < count; idx += 1) {
ivl_nexus_ptr_t nex_ptr = ivl_nexus_ptr(nex, idx);
ivl_lpm_t t_lpm = ivl_nexus_ptr_lpm(nex_ptr);
ivl_net_const_t t_net_const = ivl_nexus_ptr_con(nex_ptr);
ivl_net_logic_t t_nlogic = ivl_nexus_ptr_log(nex_ptr);
ivl_signal_t t_sig = ivl_nexus_ptr_sig(nex_ptr);
if ((ivl_nexus_ptr_drive1(nex_ptr) == IVL_DR_HiZ) &&
(ivl_nexus_ptr_drive0(nex_ptr) == IVL_DR_HiZ)) continue;
if (t_lpm) {
assert(! lpm);
lpm = t_lpm;
}
if (t_net_const) {
assert(! net_const);
net_const = t_net_const;
}
if (t_nlogic) {
assert(! nlogic);
nlogic = t_nlogic;
}
if (t_sig) {
assert(! sig);
sig = t_sig;
word = ivl_nexus_ptr_pin(nex_ptr);
}
}
/* An LPM is driving the nexus. */
if (lpm) {
assert(! net_const);
assert(! nlogic);
assert(! sig);
/* If there is a signal in this scope that is also driven by
* the LPM then use the signal instead. */
sig = find_local_signal(scope, ivl_lpm_q(lpm), &word);
if (sig) emit_nexus_as_ca(scope, ivl_signal_nex(sig, word), 0, 0);
else emit_lpm_as_ca(scope, lpm, 0);
/* A constant is driving the nexus. */
} else if (net_const) {
assert( !nlogic);
assert(! sig);
/* If there is a signal in this scope that is also driven by
* the constant then use the signal instead. */
sig = find_local_signal(scope, ivl_const_nex(net_const), &word);
if (sig) emit_nexus_as_ca(scope, ivl_signal_nex(sig, word), 0, 0);
else emit_const_nexus(scope, net_const);
/* A logic gate is driving the nexus. */
} else if (nlogic) {
assert(! sig);
/* If there is a signal in this scope that is also driven by
* the logic then use the signal instead. */
sig = find_local_signal(scope, ivl_logic_pin(nlogic, 0), &word);
if (sig) emit_nexus_as_ca(scope, ivl_signal_nex(sig, word), 0, 0);
else emit_logic_as_ca(scope, nlogic);
/* A signal is driving the nexus. */
} else if (sig) {
emit_nexus_as_ca(scope, ivl_signal_nex(sig, word), 0, 0);
/* If there is no driver then look for a single signal that is
* driven by this nexus that has the correct scope. This is needed
* to translate top level ports. */
} else {
emit_nexus_port_signal(scope, nex);
}
}
void emit_nexus_as_ca(ivl_scope_t scope, ivl_nexus_t nex, unsigned allow_UD,
unsigned sign_extend)
{
/* If there is no nexus then there is nothing to print. */
if (! nex) return;
/* A local nexus only has a single driver. */
if (is_local_nexus(scope, nex)) {
unsigned idx, count = ivl_nexus_ptrs(nex);
unsigned must_be_sig = 0;
unsigned out_of_scope_drive = 0;
ivl_lpm_t lpm = 0;
ivl_net_const_t net_const = 0;
ivl_net_logic_t nlogic = 0;
ivl_signal_t sig = 0;
unsigned word = 0;
for (idx = 0; idx < count; idx += 1) {
ivl_nexus_ptr_t nex_ptr = ivl_nexus_ptr(nex, idx);
ivl_lpm_t t_lpm = ivl_nexus_ptr_lpm(nex_ptr);
ivl_net_const_t t_net_const = ivl_nexus_ptr_con(nex_ptr);
ivl_net_logic_t t_nlogic = ivl_nexus_ptr_log(nex_ptr);
ivl_signal_t t_sig = ivl_nexus_ptr_sig(nex_ptr);
if ((ivl_nexus_ptr_drive1(nex_ptr) == IVL_DR_HiZ) &&
(ivl_nexus_ptr_drive0(nex_ptr) == IVL_DR_HiZ)) {
/* If we only have a single input then we want
* the nexus this signal is driven by. */
if (count == 1) {
must_be_sig = 1;
} else continue;
}
if (t_lpm) {
assert(! lpm);
lpm = t_lpm;
}
if (t_net_const) {
if (scope != ivl_const_scope(t_net_const)) {
// HERE: Need to verify that this is not a parameter
out_of_scope_drive = 1;
}
assert(! net_const);
net_const = t_net_const;
}
if (t_nlogic) {
assert(! nlogic);
nlogic = t_nlogic;
}
if (t_sig) {
assert(! sig);
sig = t_sig;
word = ivl_nexus_ptr_pin(nex_ptr);
}
}
if (lpm) {
assert(! net_const);
assert(! nlogic);
assert(! sig);
assert(! must_be_sig);
// HERE: I think we need special input code like the following.
#if 0
/* If there is a signal in this scope that is also driven by
* the LPM then use the signal instead. */
sig = find_local_signal(scope, ivl_lpm_q(lpm), &word);
if (sig) emit_nexus_as_ca(scope, ivl_signal_nex(sig, word), 0, 0);
else emit_lpm_as_ca(scope, lpm, sign_extend);
#endif
emit_lpm_as_ca(scope, lpm, sign_extend);
} else if (net_const) {
assert( !nlogic);
assert(! sig);
assert(! must_be_sig);
if (out_of_scope_drive) {
// HERE: An out of scope const drive that is not a parameter is really a
// port so look for and emit the local signal name (nexus_is_signal
// may work). The is_local_nexus code also needs to be changed to
// not emit the port expressions as a CA. Make sure this works
// correctly if the parameter is passed as a port argument.
// For now report this as missing.
fprintf(vlog_out, "<missing>");
} else emit_const_nexus(scope, net_const);
} else if (nlogic) {
assert(! sig);
assert(! must_be_sig);
emit_logic_as_ca(scope, nlogic);
} else if (sig) {
// HERE: should these be allow_UD?
if (must_be_sig) {
emit_nexus_as_ca(ivl_signal_scope(sig),
ivl_signal_nex(sig, word),
0, 0);
} else emit_name_of_nexus(scope, nex, 0);
// HERE: The assert causes pr1703959 to fail.
// } else assert(0);
} else {
fprintf(stderr, "?:?: vlog95 error: Could not emit "
"nexus as a CA.\n");
vlog_errors += 1;
fprintf(vlog_out, "<missing>");
}
} else {
emit_name_of_nexus(scope, nex, allow_UD);
}
}
static void emit_pull_const(char value, unsigned width)
{
unsigned idx;
fprintf(vlog_out, "%u'b", width);
for (idx = 0; idx < width; idx += 1) {
fprintf(vlog_out, "%c", value);
}
}
static void emit_logic_as_ca(ivl_scope_t scope, ivl_net_logic_t nlogic)
{
unsigned inputs = ivl_logic_pins(nlogic) - 1;
switch (ivl_logic_type(nlogic)) {
case IVL_LO_AND:
assert(inputs == 2);
fprintf(vlog_out, "(");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 1), 0, 0);
fprintf(vlog_out, " & ");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 2), 0, 0);
fprintf(vlog_out, ")");
break;
case IVL_LO_BUF:
case IVL_LO_BUFT:
case IVL_LO_BUFZ:
assert(inputs == 1);
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 1), 0, 0);
break;
case IVL_LO_EQUIV:
assert(inputs == 2);
fprintf(vlog_out, "(");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 1), 0, 0);
fprintf(vlog_out, " ~^ ");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 2), 0, 0);
fprintf(vlog_out, ")");
break;
case IVL_LO_IMPL:
assert(inputs == 2);
fprintf(vlog_out, "(~");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 1), 0, 0);
fprintf(vlog_out, " | ");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 2), 0, 0);
fprintf(vlog_out, ")");
break;
case IVL_LO_NAND:
assert(inputs == 2);
fprintf(vlog_out, "~(");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 1), 0, 0);
fprintf(vlog_out, " & ");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 2), 0, 0);
fprintf(vlog_out, ")");
break;
case IVL_LO_NOR:
assert(inputs == 2);
fprintf(vlog_out, "~(");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 1), 0, 0);
fprintf(vlog_out, " | ");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 2), 0, 0);
fprintf(vlog_out, ")");
break;
case IVL_LO_NOT:
assert(inputs == 1);
fprintf(vlog_out, "(~ ");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 1), 0, 0);
fprintf(vlog_out, ")");
break;
case IVL_LO_OR:
assert(inputs == 2);
fprintf(vlog_out, "(");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 1), 0, 0);
fprintf(vlog_out, " | ");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 2), 0, 0);
fprintf(vlog_out, ")");
break;
case IVL_LO_XNOR:
assert(inputs == 2);
fprintf(vlog_out, "(");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 1), 0, 0);
fprintf(vlog_out, " ~^ ");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 2), 0, 0);
fprintf(vlog_out, ")");
break;
case IVL_LO_XOR:
assert(inputs == 2);
fprintf(vlog_out, "(");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 1), 0, 0);
fprintf(vlog_out, " ^ ");
emit_nexus_as_ca(scope, ivl_logic_pin(nlogic, 2), 0, 0);
fprintf(vlog_out, ")");
break;
/* A pull up/down at this point has been turned into an assignment
* with strength so just emit the appropriate constant. */
case IVL_LO_PULLDOWN:
emit_pull_const('0', ivl_logic_width(nlogic));
break;
case IVL_LO_PULLUP:
emit_pull_const('1', ivl_logic_width(nlogic));
break;
default:
fprintf(vlog_out, "<unknown>");
fprintf(stderr, "%s:%u: vlog95 error: Unknown CA logic type "
"(%d).\n",
ivl_logic_file(nlogic), ivl_logic_lineno(nlogic),
(int)ivl_logic_type(nlogic));
vlog_errors += 1;
}
}
static void emit_lpm_array(ivl_scope_t scope, ivl_lpm_t lpm)
{
ivl_signal_t sig = ivl_lpm_array(lpm);
emit_scope_module_path(scope, ivl_signal_scope(sig));
emit_id(ivl_signal_basename(sig));
fprintf(vlog_out, "[");
// HERE: Need to remove the scale to match array base instead of adding it back.
emit_nexus_as_ca(scope, ivl_lpm_select(lpm), 0, 0);
fprintf(vlog_out, " + %d]", ivl_signal_array_base(sig));
}
static void emit_lpm_concat(ivl_scope_t scope, ivl_lpm_t lpm)
{
unsigned idx, count= ivl_lpm_size(lpm);
ivl_nexus_t nex;
fprintf(vlog_out, "{");
// HERE: Need to check for a zero repeat that was dropped from the concat.
/* Check to see if this is a repeat. */
nex = ivl_lpm_data(lpm, 0);
for (idx = 1; idx < count; idx += 1) {
if (nex != ivl_lpm_data(lpm, idx)) break;
}
/* If all the nexus match then we have a repeat. */
if ((idx == count) && (count > 1)) {
fprintf(vlog_out, "%u{", count);
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0), 0, 0);
fprintf(vlog_out, "}");
/* Icarus uses a concat to combine the output from multiple devices
* into a single vector, because of this we need to also look for
* the nexus driver outside the scope. emit_nexus_as_ca( , , 1, ) */
} else {
for (idx = count-1; idx > 0; idx -= 1) {
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, idx), 1, 0);
fprintf(vlog_out, ", ");
}
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0), 1, 0);
}
fprintf(vlog_out, "}");
}
/*
* Look for an output signal in the nexus that is driving into this scope.
*/
static ivl_signal_t find_output_signal(const ivl_scope_t scope, ivl_nexus_t nex,
unsigned*array_word)
{
unsigned idx, count = ivl_nexus_ptrs(nex);
(void)array_word; /* Parameter is not used. */
for (idx = 0; idx < count; idx += 1) {
ivl_nexus_ptr_t nex_ptr = ivl_nexus_ptr(nex, idx);
ivl_signal_t t_sig = ivl_nexus_ptr_sig(nex_ptr);
if (! t_sig) continue;
/* The signal must not be a driver. */
if ((ivl_nexus_ptr_drive1(nex_ptr) != IVL_DR_HiZ) ||
(ivl_nexus_ptr_drive0(nex_ptr) != IVL_DR_HiZ)) continue;
/* The signal must be an output. */
if (ivl_signal_port(t_sig) != IVL_SIP_OUTPUT) continue;
/* The signal must be driving into this scope. */
if (ivl_scope_parent(ivl_signal_scope(t_sig)) == scope) {
return t_sig;
}
}
return 0;
}
static ivl_signal_t nexus_is_signal(ivl_scope_t scope, ivl_nexus_t nex,
int*msb, int*lsb, unsigned*array_word,
ivl_net_const_t*accept_const)
{
unsigned idx, count = ivl_nexus_ptrs(nex);
ivl_lpm_t lpm = 0;
ivl_net_const_t net_const = 0;
ivl_net_logic_t nlogic = 0;
ivl_signal_t sig;
/* Look for a signal in the local scope first. */
sig = find_local_signal(scope, nex, array_word);
if (sig) {
get_sig_msb_lsb(sig, msb, lsb);
return sig;
}
/* Now look for an output signal driving into the local scope. */
sig = find_output_signal(scope, nex, array_word);
if (sig) {
get_sig_msb_lsb(sig, msb, lsb);
return sig;
}
/* Now scan the nexus looking for a driver. */
for (idx = 0; idx < count; idx += 1) {
ivl_nexus_ptr_t nex_ptr = ivl_nexus_ptr(nex, idx);
ivl_lpm_t t_lpm = ivl_nexus_ptr_lpm(nex_ptr);
ivl_net_const_t t_net_const = ivl_nexus_ptr_con(nex_ptr);
ivl_net_logic_t t_nlogic = ivl_nexus_ptr_log(nex_ptr);
ivl_signal_t t_sig = ivl_nexus_ptr_sig(nex_ptr);
if ((ivl_nexus_ptr_drive1(nex_ptr) == IVL_DR_HiZ) &&
(ivl_nexus_ptr_drive0(nex_ptr) == IVL_DR_HiZ)) continue;
if (t_lpm) {
assert(! lpm);
/* The real signal could be hidden behind a select. */
if (ivl_lpm_type(t_lpm) == IVL_LPM_PART_VP) {
t_sig = nexus_is_signal(scope, ivl_lpm_data(t_lpm, 0),
msb, lsb, array_word, 0);
}
if (t_sig) {
if (*msb >= *lsb) {
*lsb += ivl_lpm_base(t_lpm);
*msb = *lsb + (int)ivl_lpm_width(t_lpm) - 1;
} else {
*lsb -= ivl_lpm_base(t_lpm);
*msb = *lsb - (int)ivl_lpm_width(t_lpm) + 1;
}
} else lpm = t_lpm;
}
if (t_net_const) {
assert(! net_const);
net_const = t_net_const;
}
if (t_nlogic) {
assert(! nlogic);
/* The real signal could be hidden behind a BUFZ gate. */
if (ivl_logic_type(t_nlogic) == IVL_LO_BUFZ) {
assert(ivl_logic_pins(t_nlogic) == 2);
t_sig = nexus_is_signal(scope,
ivl_logic_pin(t_nlogic, 1),
msb, lsb, array_word, 0);
} else nlogic = t_nlogic;
}
if (t_sig) {
assert(! sig);
sig = t_sig;
/* This could be an array word so save the word. */
*array_word = ivl_nexus_ptr_pin(nex_ptr);
}
}
if (sig) return sig;
if (accept_const && net_const) {
*lsb = 0;
*msb = ivl_const_width(net_const) - 1;
*accept_const = net_const;
}
return 0;
}
static void emit_part_name(ivl_scope_t scope, ivl_signal_t sig,
unsigned array_word)
{
emit_scope_call_path(scope, ivl_signal_scope(sig));
emit_id(ivl_signal_basename(sig));
if (ivl_signal_dimensions(sig)) {
int array_idx = (int) array_word + ivl_signal_array_base(sig);
fprintf(vlog_out, "[%d]", array_idx);
}
}
static void emit_lpm_part_select(ivl_scope_t scope, ivl_lpm_t lpm,
unsigned sign_extend)
{
unsigned width = ivl_lpm_width(lpm);
unsigned array_word = 0;
int base = ivl_lpm_base(lpm);
int msb = 0, lsb = 0;
ivl_signal_t sig = nexus_is_signal(scope, ivl_lpm_data(lpm, 0),
&msb, &lsb, &array_word, 0);
if (sign_extend && !allow_signed) {
fprintf(stderr, "%s:%u: vlog95 error: >>> operator is not "
"supported.\n",
ivl_lpm_file(lpm), ivl_lpm_lineno(lpm));
vlog_errors += 1;
}
// HERE: variable parameter select needs to be rebuilt.
if (! sig) {
/* Check if the compiler used a select for a shift. */
assert(base >= 0);
if (base) fprintf(vlog_out, "(");
emit_nexus_as_ca(scope, ivl_lpm_data(lpm, 0), 0, 0);
if (base) {
fprintf(vlog_out, " ");
if (sign_extend) fprintf(vlog_out, ">");
fprintf(vlog_out, ">> %d)", base);
}
return;
}
if (sign_extend) {
fprintf(vlog_out, "(");
assert(base != lsb);
// HERE: This looks wrong.
if (msb >= lsb) base += lsb;
else base = lsb - base;
emit_part_name(scope, sig, array_word);
fprintf(vlog_out, " >>> %d)", base);
return;
}
if (width == 1) {
emit_part_name(scope, sig, array_word);
ivl_nexus_t sel = ivl_lpm_data(lpm, 1);
if (sel) {
fprintf(vlog_out, "[");
if ((msb >= lsb) && (lsb == 0)) {
emit_nexus_as_ca(scope, sel, 0, 0);
// HERE: Need to scale the select nexus.
} else {
fprintf(stderr, "%s:%u: vlog95 sorry: Non-zero based "
"variable part selects are not "
"supported.\n", ivl_lpm_file(lpm),
ivl_lpm_lineno(lpm));
vlog_errors += 1;
fprintf(vlog_out, "<missing>");
}
} else {
/* Skip a select of the entire bit. */
if ((msb == lsb) && (base == 0)) return;
fprintf(vlog_out, "[");
if (msb >= lsb) base += lsb;
else base = lsb - base;
fprintf(vlog_out, "%d", base);
}
fprintf(vlog_out, "]");
} else {