aboutsummaryrefslogtreecommitdiffstats
path: root/projects/XUPVVH/XUPVVH.ip_user_files/ipstatic/hdl/iomodule_v3_1_vh_rfs.vhd
blob: c45c529ac613863555bc1b2347301f99911f47ed (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
-------------------------------------------------------------------------------
-- iomodule_vote_pkg.vhd - Package specification
-------------------------------------------------------------------------------
--
-- (c) Copyright 2015-2016 Xilinx, Inc. All rights reserved.
--
-- This file contains confidential and proprietary information
-- of Xilinx, Inc. and is protected under U.S. and
-- international copyright and other intellectual property
-- laws.
--
-- DISCLAIMER
-- This disclaimer is not a license and does not grant any
-- rights to the materials distributed herewith. Except as
-- otherwise provided in a valid license issued to you by
-- Xilinx, and to the maximum extent permitted by applicable
-- law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND
-- WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES
-- AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
-- BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-
-- INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and
-- (2) Xilinx shall not be liable (whether in contract or tort,
-- including negligence, or under any other theory of
-- liability) for any loss or damage of any kind or nature
-- related to, arising under or in connection with these
-- materials, including for any direct, or any indirect,
-- special, incidental, or consequential loss or damage
-- (including loss of data, profits, goodwill, or any type of
-- loss or damage suffered as a result of any action brought
-- by a third party) even if such damage or loss was
-- reasonably foreseeable or Xilinx had been advised of the
-- possibility of the same.
--
-- CRITICAL APPLICATIONS
-- Xilinx products are not designed or intended to be fail-
-- safe, or for use in any application requiring fail-safe
-- performance, such as life-support or safety devices or
-- systems, Class III medical devices, nuclear facilities,
-- applications related to the deployment of airbags, or any
-- other applications that could lead to death, personal
-- injury, or severe property or environmental damage
-- (individually and collectively, "Critical
-- Applications"). Customer assumes the sole risk and
-- liability of any use of Xilinx products in Critical
-- Applications, subject only to applicable laws and
-- regulations governing limitations on product liability.
--
-- THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
-- PART OF THIS FILE AT ALL TIMES.
--
-------------------------------------------------------------------------------
-- Filename:        iomodule_vote_pkg.vhd
--
-- Description:
--
-- VHDL-Standard:   VHDL'93/02
-------------------------------------------------------------------------------
-- Structure:
--              iomodule_vote_pkg
--
-------------------------------------------------------------------------------
-- Author:          rolandp
--
-- History:
--   rolandp 2015-09-14    First Version
--
-------------------------------------------------------------------------------
-- Naming Conventions:
--      active low signals:                     "*_n"
--      clock signals:                          "clk", "clk_div#", "clk_#x"
--      reset signals:                          "rst", "rst_n"
--      generics:                               "C_*"
--      user defined types:                     "*_TYPE"
--      state machine next state:               "*_ns"
--      state machine current state:            "*_cs"
--      combinatorial signals:                  "*_com"
--      pipelined or register delay signals:    "*_d#"
--      counter signals:                        "*cnt*"
--      clock enable signals:                   "*_ce"
--      internal version of output port         "*_i"
--      device pins:                            "*_pin"
--      ports:                                  - Names begin with Uppercase
--      processes:                              "*_PROCESS"
--      component instantiations:               "<ENTITY_>I_<#|FUNC>
-------------------------------------------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;

package IOModule_Vote_Pkg is

  function vote (vote1, vote2, vote3 : std_logic_vector; tmr_disable : boolean) return std_logic_vector;
  function vote (vote1, vote2, vote3 : std_logic_vector; tmr_disable : boolean; tmr : integer) return std_logic_vector;

  function vote (vote1, vote2, vote3 : std_logic; tmr_disable : boolean) return std_logic;
  function vote (vote1, vote2, vote3 : std_logic; tmr_disable : boolean; tmr : integer) return std_logic;

  function vote (vote1 : boolean; vote2, vote3 : std_logic; tmr_disable : boolean) return boolean;
  function vote (vote1 : boolean; vote2, vote3 : std_logic; tmr_disable : boolean; tmr : integer) return boolean;

  constant C_MAX_FIT_SIZE       : natural := 32;
  constant C_MAX_PIT_SIZE       : natural := 32;
  constant C_MAX_GPO_SIZE       : natural := 32;
  constant C_UART_PROG_CNT_SIZE : natural := 20;

  -- FIT
  subtype  FIT_COUNT_Pos    is natural range C_MAX_FIT_SIZE - 1 downto 0;
  constant FIT_INTERRUPT_Pos : natural :=    FIT_COUNT_Pos'high + 1;
  constant FIT_TOGGLE_Pos    : natural :=    FIT_INTERRUPT_Pos + 1;

  constant C_FIT_VOTE_SIZE   : natural := FIT_TOGGLE_Pos  + 1;
  subtype  FIT1_Pos         is natural range C_FIT_VOTE_SIZE - 1 downto 0;
  subtype  FIT2_Pos         is natural range FIT1_Pos'high + C_FIT_VOTE_SIZE downto FIT1_Pos'high + 1;
  subtype  FIT3_Pos         is natural range FIT2_Pos'high + C_FIT_VOTE_SIZE downto FIT2_Pos'high + 1;
  subtype  FIT4_Pos         is natural range FIT3_Pos'high + C_FIT_VOTE_SIZE downto FIT3_Pos'high + 1;

  -- UART Baudrate Counter
  subtype  UART_BAUD_FIT_Pos  is natural range FIT4_Pos'high + C_MAX_FIT_SIZE + 2 downto FIT4_Pos'high + 1;
  subtype  UART_BAUD_REG_Pos  is natural range UART_BAUD_FIT_Pos'high + C_UART_PROG_CNT_SIZE downto
                                               UART_BAUD_FIT_Pos'high + 1;
  subtype  UART_BAUD_CNT_Pos  is natural range UART_BAUD_REG_Pos'high + C_UART_PROG_CNT_SIZE downto
                                               UART_BAUD_REG_Pos'high + 1;
  constant UART_BAUD_EN16_Pos  : natural :=    UART_BAUD_CNT_Pos'high + 1;

  -- UART Control
  constant PARITY_ERROR_Pos    : natural :=    0;
  constant FRAME_ERROR_Pos     : natural :=    PARITY_ERROR_Pos + 1;
  constant OVERRUN_ERROR_Pos   : natural :=    FRAME_ERROR_Pos + 1;
  constant ERROR_INTERRUPT_Pos : natural :=    OVERRUN_ERROR_Pos + 1;

  subtype  UART_CONTROL_Pos   is natural range UART_BAUD_EN16_Pos + 1 + ERROR_INTERRUPT_Pos downto
                                               UART_BAUD_EN16_Pos + 1;

  -- UART Transmit
  constant UART_TX_DIV16_Pos         : natural :=    0;
  constant UART_TX_DATA_ENABLE_Pos   : natural :=    UART_TX_DIV16_Pos + 1;
  constant UART_TX_START_Pos         : natural :=    UART_TX_DATA_ENABLE_Pos + 1;
  constant UART_TX_DATABITS_Pos      : natural :=    UART_TX_START_Pos + 1;
  subtype  UART_TX_MUX_SEL_Pos      is natural range UART_TX_DATABITS_Pos + 3 downto UART_TX_DATABITS_Pos + 1;
  constant UART_TX_DATA_IS_SENT_Pos  : natural :=    UART_TX_MUX_SEL_Pos'high + 1;
  constant UART_TX_SERIAL_DATA_Pos   : natural :=    UART_TX_DATA_IS_SENT_Pos + 1;
  constant UART_TX_Pos               : natural :=    UART_TX_SERIAL_DATA_Pos + 1;
  constant UART_TX_CALC_PARITY_Pos   : natural :=    UART_TX_Pos + 1;
  constant UART_TX_RUN_Pos           : natural :=    UART_TX_CALC_PARITY_Pos + 1;
  constant UART_TX_SEL_PARITY_Pos    : natural :=    UART_TX_RUN_Pos + 1;
  subtype  UART_TX_FIFO_DOUT_Pos    is natural range UART_TX_SEL_PARITY_Pos + 8 downto UART_TX_SEL_PARITY_Pos + 1;
  constant UART_TX_BUFFER_EMPTY_Pos  : natural :=    UART_TX_FIFO_DOUT_Pos'high + 1;

  subtype  UART_TRANSMIT_Pos        is natural range UART_CONTROL_Pos'high + 1 + UART_TX_BUFFER_EMPTY_Pos downto
                                                     UART_CONTROL_Pos'high + 1;

  -- UART Receive
  constant UART_RX_PREVIOUS_RX_Pos         : natural :=    0;
  constant UART_RX_START_EDGE_DETECTED_Pos : natural :=    UART_RX_PREVIOUS_RX_Pos + 1;
  constant UART_RX_RUNNING_Pos             : natural :=    UART_RX_START_EDGE_DETECTED_Pos + 1;
  constant UART_RX_RECYCLE_Pos             : natural :=    UART_RX_RUNNING_Pos + 1;
  constant UART_RX_STOP_BIT_POSITION_Pos   : natural :=    UART_RX_RECYCLE_Pos + 1;
  constant UART_RX_CALC_PARITY_Pos         : natural :=    UART_RX_STOP_BIT_POSITION_Pos + 1;
  constant UART_RX_MID_START_BIT_Pos       : natural :=    UART_RX_CALC_PARITY_Pos + 1;
  subtype  UART_RX_SERIAL_TO_PARALLEL_Pos is natural range UART_RX_MID_START_BIT_Pos + 9 downto
                                                           UART_RX_MID_START_BIT_Pos + 1;
  constant UART_RX_NEW_RX_DATA_WRITE_Pos   : natural :=    UART_RX_SERIAL_TO_PARALLEL_Pos'high + 1;
  constant UART_RX_DATA_EXISTS_Pos         : natural :=    UART_RX_NEW_RX_DATA_WRITE_Pos + 1;
  subtype  UART_RX_DATA_I_Pos             is natural range UART_RX_DATA_EXISTS_Pos + 8 downto
                                                           UART_RX_DATA_EXISTS_Pos + 1;
  subtype  UART_RX_DATA_Pos               is natural range UART_RX_DATA_I_Pos'high + 8 downto
                                                           UART_RX_DATA_I_Pos'high + 1;

  subtype  UART_RECEIVE_Pos               is natural range UART_TRANSMIT_Pos'high + 1 + UART_RX_DATA_Pos'high downto
                                                           UART_TRANSMIT_Pos'high + 1;

  -- GPO
  subtype  GPO1_Pos                       is natural range UART_RECEIVE_Pos'high + C_MAX_GPO_SIZE downto
                                                           UART_RECEIVE_Pos'high + 1;
  subtype  GPO2_Pos                       is natural range GPO1_Pos'high + C_MAX_GPO_SIZE downto GPO1_Pos'high + 1;
  subtype  GPO3_Pos                       is natural range GPO2_Pos'high + C_MAX_GPO_SIZE downto GPO2_Pos'high + 1;
  subtype  GPO4_Pos                       is natural range GPO3_Pos'high + C_MAX_GPO_SIZE downto GPO3_Pos'high + 1;

  -- PIT
  subtype  PIT_PRELOAD_VALUE_Pos          is natural range C_MAX_PIT_SIZE-1 downto 0;
  constant PIT_RELOAD_Pos                  : natural :=    PIT_PRELOAD_VALUE_Pos'high + 1;
  constant PIT_COUNT_EN_Pos                : natural :=    PIT_RELOAD_Pos + 1;
  constant PIT_COUNT_LOAD_N_Pos            : natural :=    PIT_COUNT_EN_Pos + 1;
  constant PIT_PRELOAD_WRITTEN_Pos         : natural :=    PIT_COUNT_LOAD_N_Pos + 1;
  subtype  PIT_COUNT_Pos                  is natural range PIT_PRELOAD_WRITTEN_Pos + C_MAX_PIT_SIZE downto
                                                           PIT_PRELOAD_WRITTEN_Pos + 1;
  constant PIT_INTERRUPT_I_Pos             : natural :=    PIT_COUNT_Pos'high + 1;
  constant PIT_TOGGLE_I_Pos                : natural :=    PIT_INTERRUPT_I_Pos + 1;
  constant PIT_VOTE_SIZE                   : natural :=    PIT_TOGGLE_I_Pos + 1;

  subtype  PIT1_Pos                       is natural range GPO4_Pos'high + PIT_VOTE_SIZE downto GPO4_Pos'high + 1;
  subtype  PIT2_Pos                       is natural range PIT1_Pos'high + PIT_VOTE_SIZE downto PIT1_Pos'high + 1;
  subtype  PIT3_Pos                       is natural range PIT2_Pos'high + PIT_VOTE_SIZE downto PIT2_Pos'high + 1;
  subtype  PIT4_Pos                       is natural range PIT3_Pos'high + PIT_VOTE_SIZE downto PIT3_Pos'high + 1;

  -- Interrupt
  subtype  IRQ_INTERRUPT_Pos              is natural range 31 downto 0;
  subtype  IRQ_INTR_PRESENT_Pos           is natural range IRQ_INTERRUPT_Pos'high + 32 downto
                                                           IRQ_INTERRUPT_Pos'high + 1;
  subtype  IRQ_CISR_Pos                   is natural range IRQ_INTR_PRESENT_Pos'high + 32 downto
                                                           IRQ_INTR_PRESENT_Pos'high + 1;
  subtype  IRQ_CIER_Pos                   is natural range IRQ_CISR_Pos'high + 32 downto IRQ_CISR_Pos'high + 1;
  subtype  IRQ_CIMR_Pos                   is natural range IRQ_CIER_Pos'high + 32 downto IRQ_CIER_Pos'high + 1;
  subtype  IRQ_CIVR_Pos                   is natural range IRQ_CIMR_Pos'high + 5 downto IRQ_CIMR_Pos'high + 1;
  subtype  IRQ_FAST_STATE_Pos             is natural range IRQ_CIVR_Pos'high + 2 downto IRQ_CIVR_Pos'high + 1;
  constant IRQ_INTC_IRQ_Pos                : natural :=    IRQ_FAST_STATE_Pos'high + 1;
  constant IRQ_DO_FAST_ACK_Pos             : natural :=    IRQ_INTC_IRQ_Pos + 1;
  constant IRQ_RST_CIPR_RD_Pos             : natural :=    IRQ_DO_FAST_ACK_Pos + 1;
  constant IRQ_VOTE_SIZE                   : natural :=    IRQ_RST_CIPR_RD_Pos + 1;


  subtype  IRQ_Pos                        is natural range PIT4_Pos'high + IRQ_VOTE_SIZE downto PIT4_Pos'high + 1;

  constant VOTE_SIZE : natural := IRQ_Pos'high + 1;

end package IOModule_Vote_Pkg;


-------------------------------------------------------------------------------
-- iomodule_vote_pkg_body.vhd - Package body
-------------------------------------------------------------------------------
--
-- (c) Copyright 2015-2016 Xilinx, Inc. All rights reserved.
--
-- This file contains confidential and proprietary information
-- of Xilinx, Inc. and is protected under U.S. and
-- international copyright and other intellectual property
-- laws.
--
-- DISCLAIMER
-- This disclaimer is not a license and does not grant any
-- rights to the materials distributed herewith. Except as
-- otherwise provided in a valid license issued to you by
-- Xilinx, and to the maximum extent permitted by applicable
-- law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND
-- WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES
-- AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
-- BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-
-- INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and
-- (2) Xilinx shall not be liable (whether in contract or tort,
-- including negligence, or under any other theory of
-- liability) for any loss or damage of any kind or nature
-- related to, arising under or in connection with these
-- materials, including for any direct, or any indirect,
-- special, incidental, or consequential loss or damage
-- (including loss of data, profits, goodwill, or any type of
-- loss or damage suffered as a result of any action brought
-- by a third party) even if such damage or loss was
-- reasonably foreseeable or Xilinx had been advised of the
-- possibility of the same.
--
-- CRITICAL APPLICATIONS
-- Xilinx products are not designed or intended to be fail-
-- safe, or for use in any application requiring fail-safe
-- performance, such as life-support or safety devices or
-- systems, Class III medical devices, nuclear facilities,
-- applications related to the deployment of airbags, or any
-- other applications that could lead to death, personal
-- injury, or severe property or environmental damage
-- (individually and collectively, "Critical
-- Applications"). Customer assumes the sole risk and
-- liability of any use of Xilinx products in Critical
-- Applications, subject only to applicable laws and
-- regulations governing limitations on product liability.
--
-- THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
-- PART OF THIS FILE AT ALL TIMES.
--
-------------------------------------------------------------------------------
-- Filename:        iomodule_vote_pkg_body.vhd
--
-- Description:
--
-- VHDL-Standard:   VHDL'93/02
-------------------------------------------------------------------------------
-- Structure:
--              iomodule_vote_pkg
--
-------------------------------------------------------------------------------
-- Author:          rolandp
--
-- History:
--   rolandp 2015-09-14    First Version
--
-------------------------------------------------------------------------------
-- Naming Conventions:
--      active low signals:                     "*_n"
--      clock signals:                          "clk", "clk_div#", "clk_#x"
--      reset signals:                          "rst", "rst_n"
--      generics:                               "C_*"
--      user defined types:                     "*_TYPE"
--      state machine next state:               "*_ns"
--      state machine current state:            "*_cs"
--      combinatorial signals:                  "*_com"
--      pipelined or register delay signals:    "*_d#"
--      counter signals:                        "*cnt*"
--      clock enable signals:                   "*_ce"
--      internal version of output port         "*_i"
--      device pins:                            "*_pin"
--      ports:                                  - Names begin with Uppercase
--      processes:                              "*_PROCESS"
--      component instantiations:               "<ENTITY_>I_<#|FUNC>
-------------------------------------------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;

package body IOModule_Vote_Pkg is

  function vote(vote1, vote2, vote3 : std_logic_vector) return std_logic_vector is
    variable I     : integer := 0;
    variable voted : std_logic_vector(1023 downto 0);
    variable vote1_l, vote2_l, vote3_l : std_logic_vector(1023 downto 0);
  begin
    vote1_l(vote1'length - 1 downto 0) := vote1;
    vote2_l(vote2'length - 1 downto 0) := vote2;
    vote3_l(vote3'length - 1 downto 0) := vote3;
    for I in vote1_l'range loop
      voted(I) := (vote1_l(I) and vote2_l(I)) or
                  (vote1_l(I) and vote3_l(I)) or
                  (vote2_l(I) and vote3_l(I));
    end loop;
    return voted(vote1'length - 1 downto 0);
  end function vote;

  function vote(vote1, vote2, vote3 : std_logic_vector; tmr_disable : boolean) return std_logic_vector is
  begin
    if tmr_disable then
      return vote1;
    else
      return vote(vote1, vote2, vote3);
    end if;
  end function vote;

  function vote(vote1, vote2, vote3 : std_logic_vector;
                tmr_disable         : boolean;
                tmr                 : integer) return std_logic_vector is
  begin
    if (tmr = 0 or tmr_disable) then
      return vote1;
    else
      return vote(vote1, vote2, vote3);
    end if;
  end function vote;

  function vote(vote1, vote2, vote3 : std_logic) return std_logic is
    variable voted : std_logic;
  begin
    voted := (vote1 and vote2) or
             (vote1 and vote3) or
             (vote2 and vote3);
    return voted;
  end function vote;

  function vote(vote1, vote2, vote3 : std_logic; tmr_disable : boolean) return std_logic is
  begin
    if tmr_disable then
      return vote1;
    else
      return vote(vote1, vote2, vote3);
    end if;
  end function vote;

  function vote (vote1, vote2, vote3 : std_logic; tmr_disable : boolean; tmr : integer) return std_logic is
  begin
    if (tmr = 0 or tmr_disable) then
       return vote1;
    else
      return vote(vote1, vote2, vote3);
    end if;
  end function vote;

  function vote(vote1 : boolean; vote2, vote3 : std_logic) return boolean is
    variable voted, vote2_b, vote3_b : boolean;
  begin
    if (vote2 = '1') then
      vote2_b := true;
    else
      vote2_b := false;
    end if;
    if (vote3 = '1') then
      vote3_b := true;
    else
      vote3_b := false;
    end if;
    voted := (vote1   and vote2_b) or
             (vote1   and vote3_b) or
             (vote2_b and vote3_b);
    return voted;
  end function vote;

  function vote(vote1 : boolean; vote2, vote3 : std_logic; tmr_disable : boolean) return boolean is
  begin
    if tmr_disable then
      return vote1;
    else
      return vote(vote1, vote2, vote3);
    end if;
  end function vote;

  function vote(vote1 : boolean; vote2, vote3 : std_logic; tmr_disable : boolean; tmr : integer) return boolean is
  begin
    if (tmr = 0 or tmr_disable) then
      return vote1;
    else
      return vote(vote1, vote2, vote3);
    end if;
  end function vote;

end package body IOModule_Vote_Pkg;


-------------------------------------------------------------------------------
-- iomodule_funcs.vhd - Entity and architecture
-------------------------------------------------------------------------------
--
-- (c) Copyright 2001-2016 Xilinx, Inc. All rights reserved.
--
-- This file contains confidential and proprietary information
-- of Xilinx, Inc. and is protected under U.S. and 
-- international copyright and other intellectual property
-- laws.
--
-- DISCLAIMER
-- This disclaimer is not a license and does not grant any
-- rights to the materials distributed herewith. Except as
-- otherwise provided in a valid license issued to you by
-- Xilinx, and to the maximum extent permitted by applicable
-- law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND
-- WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES
-- AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
-- BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-
-- INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and
-- (2) Xilinx shall not be liable (whether in contract or tort,
-- including negligence, or under any other theory of
-- liability) for any loss or damage of any kind or nature
-- related to, arising under or in connection with these
-- materials, including for any direct, or any indirect,
-- special, incidental, or consequential loss or damage
-- (including loss of data, profits, goodwill, or any type of
-- loss or damage suffered as a result of any action brought
-- by a third party) even if such damage or loss was
-- reasonably foreseeable or Xilinx had been advised of the
-- possibility of the same.
--
-- CRITICAL APPLICATIONS
-- Xilinx products are not designed or intended to be fail-
-- safe, or for use in any application requiring fail-safe
-- performance, such as life-support or safety devices or
-- systems, Class III medical devices, nuclear facilities,
-- applications related to the deployment of airbags, or any
-- other applications that could lead to death, personal
-- injury, or severe property or environmental damage
-- (individually and collectively, "Critical
-- Applications"). Customer assumes the sole risk and
-- liability of any use of Xilinx products in Critical
-- Applications, subject only to applicable laws and
-- regulations governing limitations on product liability.
--
-- THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
-- PART OF THIS FILE AT ALL TIMES.
--
-------------------------------------------------------------------------------
-- Filename:        iomodule_funcs.vhd
--
-- Description:     Support functions for iomodule
--
-- VHDL-Standard:   VHDL'93
-------------------------------------------------------------------------------
-- Structure:
--                  iomodule_funcs.vhd
--
-------------------------------------------------------------------------------
-- Author:          rolandp
-------------------------------------------------------------------------------
-- Naming Conventions:
--      active low signals:                     "*_n"
--      clock signals:                          "clk", "clk_div#", "clk_#x" 
--      reset signals:                          "rst", "rst_n" 
--      generics:                               "C_*" 
--      user defined types:                     "*_TYPE" 
--      state machine next state:               "*_ns" 
--      state machine current state:            "*_cs" 
--      combinatorial signals:                  "*_com" 
--      pipelined or register delay signals:    "*_d#" 
--      counter signals:                        "*cnt*"
--      clock enable signals:                   "*_ce"
--      internal version of output port         "*_i"
--      device pins:                            "*_pin" 
--      ports:                                  - Names begin with Uppercase 
--      processes:                              "*_PROCESS" 
--      component instantiations:               "<ENTITY_>I_<#|FUNC>
-------------------------------------------------------------------------------

library IEEE;
use IEEE.std_logic_1164.all;

package iomodule_funcs is

  type TARGET_FAMILY_TYPE is (
                              -- pragma xilinx_rtl_off
                              non_RTL,
                              -- pragma xilinx_rtl_on
                              RTL
                             );

  function String_To_Family (S : string; Select_RTL : boolean) return TARGET_FAMILY_TYPE;

end package iomodule_funcs;

library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;

package body iomodule_funcs is

  function LowerCase_Char(char : character) return character is
  begin
    -- If char is not an upper case letter then return char
    if char < 'A' or char > 'Z' then
      return char;
    end if;
    -- Otherwise map char to its corresponding lower case character and
    -- return that
    case char is
      when 'A'    => return 'a'; when 'B' => return 'b'; when 'C' => return 'c'; when 'D' => return 'd';
      when 'E'    => return 'e'; when 'F' => return 'f'; when 'G' => return 'g'; when 'H' => return 'h';
      when 'I'    => return 'i'; when 'J' => return 'j'; when 'K' => return 'k'; when 'L' => return 'l';
      when 'M'    => return 'm'; when 'N' => return 'n'; when 'O' => return 'o'; when 'P' => return 'p';
      when 'Q'    => return 'q'; when 'R' => return 'r'; when 'S' => return 's'; when 'T' => return 't';
      when 'U'    => return 'u'; when 'V' => return 'v'; when 'W' => return 'w'; when 'X' => return 'x';
      when 'Y'    => return 'y'; when 'Z' => return 'z';
      when others => return char;
    end case;
  end LowerCase_Char;

  function LowerCase_String (s : string) return string is
    variable res : string(s'range);
  begin  -- function LoweerCase_String
    for I in s'range loop
      res(I) := LowerCase_Char(s(I));
    end loop;  -- I
    return res;
  end function LowerCase_String;

  -- Returns true if case insensitive string comparison determines that
  -- str1 and str2 are equal
  function Equal_String( str1, str2 : string ) return boolean is
    constant len1 : integer := str1'length;
    constant len2 : integer := str2'length;
    variable equal : boolean := true;
  begin
    if not (len1=len2) then
      equal := false;
    else
      for i in str1'range loop
        if not (LowerCase_Char(str1(i)) = LowerCase_Char(str2(i))) then
          equal := false;
        end if;
      end loop;
    end if;

    return equal;
  end Equal_String;

  function String_To_Family (S : string; Select_RTL : boolean) return TARGET_FAMILY_TYPE is
  begin  -- function String_To_Family
    if ((Select_RTL) or Equal_String(S, "rtl")) then
      return RTL;
    else
      return non_RTL;
    end if;
  end function String_To_Family;

end package body iomodule_funcs;


-------------------------------------------------------------------------------
-- xilinx_primitives.vhd - Entity and architecture
-------------------------------------------------------------------------------
--
-- (c) Copyright 2016 Xilinx, Inc. All rights reserved.
--
-- This file contains confidential and proprietary information
-- of Xilinx, Inc. and is protected under U.S. and 
-- international copyright and other intellectual property
-- laws.
--
-- DISCLAIMER
-- This disclaimer is not a license and does not grant any
-- rights to the materials distributed herewith. Except as
-- otherwise provided in a valid license issued to you by
-- Xilinx, and to the maximum extent permitted by applicable
-- law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND
-- WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES
-- AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
-- BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-
-- INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and
-- (2) Xilinx shall not be liable (whether in contract or tort,
-- including negligence, or under any other theory of
-- liability) for any loss or damage of any kind or nature
-- related to, arising under or in connection with these
-- materials, including for any direct, or any indirect,
-- special, incidental, or consequential loss or damage
-- (including loss of data, profits, goodwill, or any type of
-- loss or damage suffered as a result of any action brought
-- by a third party) even if such damage or loss was
-- reasonably foreseeable or Xilinx had been advised of the
-- possibility of the same.
--
-- CRITICAL APPLICATIONS
-- Xilinx products are not designed or intended to be fail-
-- safe, or for use in any application requiring fail-safe
-- performance, such as life-support or safety devices or
-- systems, Class III medical devices, nuclear facilities,
-- applications related to the deployment of airbags, or any
-- other applications that could lead to death, personal
-- injury, or severe property or environmental damage
-- (individually and collectively, "Critical
-- Applications"). Customer assumes the sole risk and
-- liability of any use of Xilinx products in Critical
-- Applications, subject only to applicable laws and
-- regulations governing limitations on product liability.
--
-- THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
-- PART OF THIS FILE AT ALL TIMES.
--
-------------------------------------------------------------------------------
-- Filename:        xilinx_primitives.vhd
--
-- Description:     
--                  
-- VHDL-Standard:   VHDL'93
-------------------------------------------------------------------------------
-- Structure:   
--                  xilinx_primitives.vhd
--
-------------------------------------------------------------------------------
-- Author:          rolandp
--
-- History:
--   rolandp 2016-05-21    First Version
--
-------------------------------------------------------------------------------
-- Naming Conventions:
--      active low signals:                     "*_n"
--      clock signals:                          "clk", "clk_div#", "clk_#x" 
--      reset signals:                          "rst", "rst_n" 
--      generics:                               "C_*" 
--      user defined types:                     "*_TYPE" 
--      state machine next state:               "*_ns" 
--      state machine current state:            "*_cs" 
--      combinatorial signals:                  "*_com" 
--      pipelined or register delay signals:    "*_d#" 
--      counter signals:                        "*cnt*"
--      clock enable signals:                   "*_ce" 
--      internal version of output port         "*_i"
--      device pins:                            "*_pin" 
--      ports:                                  - Names begin with Uppercase 
--      processes:                              "*_PROCESS" 
--      component instantiations:               "<ENTITY_>I_<#|FUNC>
-------------------------------------------------------------------------------

-- XIL_Scan_Reset_Control
library IEEE;
use IEEE.std_logic_1164.all;

entity xil_scan_reset_control is
  port (
    Scan_En          : in  std_logic;
    Scan_Reset_Sel   : in  std_logic;
    Scan_Reset       : in  std_logic;
    Functional_Reset : in  std_logic;
    Reset            : out std_logic);
end entity xil_scan_reset_control;

architecture IMP of xil_scan_reset_control is

begin
  Reset <= '0'               when Scan_En = '1' else
            Functional_Reset when Scan_Reset_Sel = '0' else
            Scan_Reset;
end architecture IMP;

----- entity XIL_SRL16E -----
library IEEE;
use IEEE.std_logic_1164.all;
library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_funcs.all;

entity XIL_SRL16E is
  generic(
    C_TARGET    : TARGET_FAMILY_TYPE;
    C_USE_SRL16 : string;
    C_STATIC    : boolean    := false;
    INIT        : bit_vector := X"0000");
  port(
    Config_Reset : in  std_logic;
    Q            : out std_logic;
    A0           : in  std_logic;
    A1           : in  std_logic;
    A2           : in  std_logic;
    A3           : in  std_logic;
    CE           : in  std_logic;
    CLK          : in  std_logic;
    D            : in  std_logic);
end entity XIL_SRL16E;

library unisim;
use unisim.vcomponents.all;

library ieee;
use ieee.numeric_std.all;

architecture IMP of XIL_SRL16E is

begin  -- architecture IMP

  Use_unisim: if (C_USE_SRL16 /= "no" and C_TARGET /= RTL) generate
    XIL_SRL16E_I1: SRL16E
      generic map (
        INIT  => INIT)  -- [bit_vector]
      port map (
        Q   => Q,       -- [out std_logic]
        A0  => A0,      -- [in  std_logic]
        A1  => A1,      -- [in  std_logic]
        A2  => A2,      -- [in  std_logic]
        A3  => A3,      -- [in  std_logic]
        CE  => CE,      -- [in  std_logic]
        CLK => CLK,     -- [in  std_logic]
        D   => D);      -- [in std_logic]
  end generate Use_unisim;

  Use_RTL : if (C_USE_SRL16 = "no" or C_TARGET = RTL) generate
    signal shift_reg         : std_logic_vector(15 downto 0) := to_stdLogicVector(INIT);
    constant shift_reg_const : std_logic_vector(15 downto 0) := to_stdLogicVector(INIT);
    attribute shreg_extract : string;
    attribute shreg_extract of SHIFT_REG : signal is C_USE_SRL16;
  begin

    Static_Values: if (C_STATIC) generate
    begin
      Q <= shift_reg_const(to_integer(unsigned(to_stdLogicVector(A3 & A2 & A1 & A0))));
    end generate Static_Values;

    Dynamic_Values: if (not C_STATIC) generate
    begin
      Q <= shift_reg(to_integer(unsigned(to_stdLogicVector(A3 & A2 & A1 & A0))));

      process(CLK)
      begin
        if (rising_edge(CLK)) then
          if (Config_Reset = '1') then
            shift_reg <= shift_reg_const;
          else
            if CE = '1' then
              shift_reg <= shift_reg(14 downto 0) & D;
            end if;
          end if;
        end if;
      end process;
      
    end generate Dynamic_Values;

  end generate Use_RTL;

end architecture IMP;

----- entity XIL_SRLC16E -----
library IEEE;
use IEEE.std_logic_1164.all;
library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_funcs.all;

entity XIL_SRLC16E is
  generic(
    C_TARGET    : TARGET_FAMILY_TYPE;
    C_USE_SRL16 : string;
    INIT        : bit_vector := X"0000");
  port(
    Config_Reset : in  std_logic;
    Q            : out std_logic;
    Q15          : out std_logic;
    A0           : in  std_logic;
    A1           : in  std_logic;
    A2           : in  std_logic;
    A3           : in  std_logic;
    CE           : in  std_logic;
    CLK          : in  std_logic;
    D            : in  std_logic);
end entity XIL_SRLC16E;

library unisim;
use unisim.vcomponents.all;

library ieee;
use ieee.numeric_std.all;

architecture IMP of XIL_SRLC16E is

begin  -- architecture IMP

  Use_unisim: if (C_USE_SRL16 /= "no" and C_TARGET /= RTL) generate
    XIL_SRL16CE_I1: SRLC16E
      generic map (
        INIT  => INIT)  -- [bit_vector]
      port map (
        Q15 => Q15,       -- [out std_logic]
        Q   => Q,       -- [out std_logic]
        A0  => A0,      -- [in  std_logic]
        A1  => A1,      -- [in  std_logic]
        A2  => A2,      -- [in  std_logic]
        A3  => A3,      -- [in  std_logic]
        CE  => CE,      -- [in  std_logic]
        CLK => CLK,     -- [in  std_logic]
        D   => D);      -- [in std_logic]
  end generate Use_unisim;

  Use_RTL : if (C_USE_SRL16 = "no" or C_TARGET = RTL) generate
    signal shift_reg        : std_logic_vector(15 downto 0) := to_stdLogicVector(INIT);
    attribute shreg_extract : string;
    attribute shreg_extract of SHIFT_REG : signal is C_USE_SRL16;
  begin

    Q   <= shift_reg(to_integer(unsigned(to_stdLogicVector(A3 & A2 & A1 & A0))));
    Q15 <= shift_reg(15);

    process(CLK)
    begin
      if (rising_edge(CLK)) then
        if (Config_Reset = '1') then
          shift_reg <= (others => '0');
        else
          if CE = '1' then
            shift_reg <= shift_reg(14 downto 0) & D;
          end if;
        end if;
      end if;
    end process;

  end generate Use_RTL;

end architecture IMP;

----- entity MUXCY -----
library IEEE;
use IEEE.std_logic_1164.all;
library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_funcs.all;

entity MB_MUXCY is
  generic (
    C_TARGET : TARGET_FAMILY_TYPE
  );
  port (
    LO : out std_logic;
    CI : in  std_logic;
    DI : in  std_logic;
    S  : in  std_logic
  );
end entity MB_MUXCY;

library Unisim;
use Unisim.vcomponents.all;

architecture IMP of MB_MUXCY is
begin
  
  Using_RTL: if ( C_TARGET = RTL ) generate 
  begin
    LO <= DI when S = '0' else CI;
  end generate Using_RTL;
  
  Using_FPGA: if ( C_TARGET /= RTL ) generate 
  begin
    Native: MUXCY_L
      port map(
        LO => LO,
        CI => CI,
        DI => DI,
        S  => S
      );
  end generate Using_FPGA;
  
end architecture IMP;

----- entity XORCY -----
library IEEE;
use IEEE.std_logic_1164.all;
library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_funcs.all;

entity MB_XORCY is
  generic (
    C_TARGET : TARGET_FAMILY_TYPE
  );
  port (
    O  : out std_logic;
    CI : in  std_logic;
    LI : in  std_logic
  );
end entity MB_XORCY;

library Unisim;
use Unisim.vcomponents.all;

architecture IMP of MB_XORCY is
begin
  
  Using_RTL: if ( C_TARGET = RTL ) generate 
  begin
    O <= (CI xor LI);
  end generate Using_RTL;
  
  Using_FPGA: if ( C_TARGET /= RTL ) generate 
  begin
    Native: XORCY
      port map(
        O  => O,
        CI => CI,
        LI => LI
      );
  end generate Using_FPGA;
  
end architecture IMP;

----- entity MUXCY with XORCY -----
library IEEE;
use IEEE.std_logic_1164.all;
library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_funcs.all;

entity MB_MUXCY_XORCY is
  generic (
    C_TARGET : TARGET_FAMILY_TYPE
  );
  port (
    O  : out std_logic;
    LO : out std_logic;
    CI : in  std_logic;
    DI : in  std_logic;
    S  : in  std_logic
  );
end entity MB_MUXCY_XORCY;

library Unisim;
use Unisim.vcomponents.all;

architecture IMP of MB_MUXCY_XORCY is
begin
  
  Using_RTL: if ( C_TARGET = RTL ) generate 
  begin
    O <= (CI xor S);
    LO <= DI when S = '0' else CI;
  end generate Using_RTL;
  
  Using_FPGA: if ( C_TARGET /= RTL ) generate 
  begin
    Native_I1: MUXCY_L
      port map(
        LO => LO,
        CI => CI,
        DI => DI,
        S  => S
      );
    Native_I2: XORCY
      port map(
        O  => O,
        CI => CI,
        LI => S
      );
  end generate Using_FPGA;
  
end architecture IMP;

----- entity FDR -----
library IEEE;
use IEEE.std_logic_1164.all;
library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_funcs.all;

entity MB_FDR is
  generic (
    C_TARGET : TARGET_FAMILY_TYPE;
    INIT : bit := '0'
  );
  port(
    Q  : out std_logic;
    C  : in  std_logic;
    D  : in  std_logic;
    R  : in  std_logic
  );
end entity MB_FDR;

library Unisim;
use Unisim.vcomponents.all;

architecture IMP of MB_FDR is
begin
  
  Using_RTL: if ( C_TARGET = RTL ) generate 
    function To_StdLogic(A : in bit ) return std_logic is
    begin
      if( A = '1' ) then
        return '1';
      end if;
      return '0';
    end;

    signal q_o : std_logic := To_StdLogic(INIT);
  begin
    Q <=  q_o;
    process(C)
    begin
      if (rising_edge(C)) then
        if (R = '1') then
          q_o <= '0';
        else
          q_o <= D;
        end if;
      end if;
    end process;
  end generate Using_RTL;
  
  Using_FPGA: if ( C_TARGET /= RTL ) generate 
  begin
    Native: FDR
      generic map(
        INIT => INIT
      )
      port map(
        Q   => Q,
        C   => C,
        D   => D,
        R   => R
      );
  end generate Using_FPGA;
  
end architecture IMP;

----- entity FDRE -----
library IEEE;
use IEEE.std_logic_1164.all;
library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_funcs.all;

entity MB_FDRE is
  generic (
    C_TARGET : TARGET_FAMILY_TYPE;
    INIT : bit := '0'
  );
  port(
    Q  : out std_logic;
    C  : in  std_logic;
    CE : in  std_logic;
    D  : in  std_logic;
    R  : in  std_logic
  );
end entity MB_FDRE;

library Unisim;
use Unisim.vcomponents.all;

architecture IMP of MB_FDRE is
begin
  
  Using_RTL: if ( C_TARGET = RTL ) generate 
    function To_StdLogic(A : in bit ) return std_logic is
    begin
      if( A = '1' ) then
        return '1';
      end if;
      return '0';
    end;

    signal q_o : std_logic := To_StdLogic(INIT);
  begin
    Q <=  q_o;
    process(C)
    begin
      if (rising_edge(C)) then
        if (R = '1') then
          q_o <= '0';
        elsif (CE = '1') then
          q_o <= D;
        end if;
      end if;
    end process;
  end generate Using_RTL;
  
  Using_FPGA: if ( C_TARGET /= RTL ) generate 
  begin
    Native: FDRE
      generic map(
        INIT => INIT
      )
      port map(
        Q   => Q,
        C   => C,
        CE  => CE,
        D   => D,
        R   => R
      );
  end generate Using_FPGA;
  
end architecture IMP;

----- entity FDSE -----
library IEEE;
use IEEE.std_logic_1164.all;
library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_funcs.all;

entity MB_FDSE is
  generic (
    C_TARGET : TARGET_FAMILY_TYPE;
    INIT : bit := '1'
  );
  port(
    Q  : out std_logic;
    C  : in  std_logic;
    CE : in  std_logic;
    D  : in  std_logic;
    S  : in  std_logic
  );
end entity MB_FDSE;

library Unisim;
use Unisim.vcomponents.all;

architecture IMP of MB_FDSE is
begin
  
  Using_RTL: if ( C_TARGET = RTL ) generate 
    function To_StdLogic(A : in bit ) return std_logic is
    begin
      if( A = '1' ) then
        return '1';
      end if;
      return '0';
    end;

    signal q_o : std_logic := To_StdLogic(INIT);
  begin
    Q <=  q_o;
    process(C)
    begin
      if (rising_edge(C)) then
        if (S = '1') then
          q_o <= '1';
        elsif (CE = '1') then
          q_o <= D;
        end if;
      end if;
    end process;
  end generate Using_RTL;
  
  Using_FPGA: if ( C_TARGET /= RTL ) generate 
  begin
    Native: FDSE
      generic map(
        INIT => INIT
      )
      port map(
        Q   => Q,
        C   => C,
        CE  => CE,
        D   => D,
        S   => S
      );
  end generate Using_FPGA;
  
end architecture IMP;

----- entity MULT_AND -----
library IEEE;
use IEEE.std_logic_1164.all;
library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_funcs.all;

entity MB_MULT_AND is
  generic (
    C_TARGET : TARGET_FAMILY_TYPE;
    INIT : bit := '0'
  );
  port(
    LO : out std_logic;
    I0 : in  std_logic;
    I1 : in  std_logic
  );
end entity MB_MULT_AND;

library Unisim;
use Unisim.vcomponents.all;

architecture IMP of MB_MULT_AND is
begin
  
  Using_RTL: if ( C_TARGET = RTL ) generate 
  begin
    LO <= I0 and I1;
  end generate Using_RTL;
  
  Using_FPGA: if ( C_TARGET /= RTL ) generate 
  begin
    Native: MULT_AND
      port map(
        LO => LO,
        I0 => I0,
        I1 => I1
      );
  end generate Using_FPGA;
  
end architecture IMP;

----- entity LUT3 -----
library IEEE;
use IEEE.std_logic_1164.all;
use ieee.numeric_std.all;
library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_funcs.all;

entity MB_LUT3 is
  generic (
    C_TARGET : TARGET_FAMILY_TYPE;
    INIT     : bit_vector := X"00"
  );
  port (
    O  : out std_logic;
    I0 : in  std_logic;
    I1 : in  std_logic;
    I2 : in  std_logic
  );
end entity MB_LUT3;

library Unisim;
use Unisim.vcomponents.all;

architecture IMP of MB_LUT3 is
begin
  
  Using_RTL: if ( C_TARGET = RTL ) generate 
    constant INIT_reg : std_logic_vector(7 downto 0) := To_StdLogicVector(INIT);
  begin
    process (I0, I1, I2)
      variable I_reg    : std_logic_vector(2 downto 0);
      variable I0_v, I1_v, I2_v : std_logic;
    begin
      -- Filter unknowns
      if I0 = '0' then I0_v := '0'; else I0_v := '1'; end if;
      if I1 = '0' then I1_v := '0'; else I1_v := '1'; end if;
      if I2 = '0' then I2_v := '0'; else I2_v := '1'; end if;
      I_reg := TO_STDLOGICVECTOR( I2_v & I1_v & I0_v);
      O     <= INIT_reg(TO_INTEGER(unsigned(I_reg)));
    end process;
  end generate Using_RTL;
  
  Using_FPGA: if ( C_TARGET /= RTL ) generate 
  begin
    Native: LUT3
      generic map(
        INIT    => INIT
      )
      port map(
        O       => O,
        I0      => I0,
        I1      => I1,
        I2      => I2
      );
  end generate Using_FPGA;
  
end architecture IMP;

----- entity MUXF5 -----
library IEEE;
use IEEE.std_logic_1164.all;
library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_funcs.all;

entity MB_MUXF5 is
  generic (
    C_TARGET : TARGET_FAMILY_TYPE
  );
  port (
    O  : out std_logic;
    I0 : in  std_logic;
    I1 : in  std_logic;
    S  : in  std_logic
  );
end entity MB_MUXF5;

library Unisim;
use Unisim.vcomponents.all;

architecture IMP of MB_MUXF5 is
begin
  
  Using_RTL: if ( C_TARGET = RTL ) generate 
  begin
    O <= I0 when S = '0' else I1;
  end generate Using_RTL;
  
  Using_FPGA: if ( C_TARGET /= RTL ) generate 
  begin
    Native: MUXF5
      port map(
        O  => O,
        I0 => I0,
        I1 => I1,
        S  => S
      );
  end generate Using_FPGA;
  
end architecture IMP;

----- entity MUXF6 -----
library IEEE;
use IEEE.std_logic_1164.all;
library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_funcs.all;

entity MB_MUXF6 is
  generic (
    C_TARGET : TARGET_FAMILY_TYPE
  );
  port (
    O  : out std_logic;
    I0 : in  std_logic;
    I1 : in  std_logic;
    S  : in  std_logic
  );
end entity MB_MUXF6;

library Unisim;
use Unisim.vcomponents.all;

architecture IMP of MB_MUXF6 is
begin
  
  Using_RTL: if ( C_TARGET = RTL ) generate 
  begin
    O <= I0 when S = '0' else I1;
  end generate Using_RTL;
  
  Using_FPGA: if ( C_TARGET /= RTL ) generate 
  begin
    Native: MUXF6
      port map(
        O  => O,
        I0 => I0,
        I1 => I1,
        S  => S
      );
  end generate Using_FPGA;
  
end architecture IMP;




------------------------------------------------------------------------------
-- synchronizers.vhd - Entity and architecture
-------------------------------------------------------------------------------
--
-- (c) Copyright 2014,2017 Xilinx, Inc. All rights reserved.
--
-- This file contains confidential and proprietary information
-- of Xilinx, Inc. and is protected under U.S. and 
-- international copyright and other intellectual property
-- laws.
--
-- DISCLAIMER
-- This disclaimer is not a license and does not grant any
-- rights to the materials distributed herewith. Except as
-- otherwise provided in a valid license issued to you by
-- Xilinx, and to the maximum extent permitted by applicable
-- law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND
-- WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES
-- AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
-- BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-
-- INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and
-- (2) Xilinx shall not be liable (whether in contract or tort,
-- including negligence, or under any other theory of
-- liability) for any loss or damage of any kind or nature
-- related to, arising under or in connection with these
-- materials, including for any direct, or any indirect,
-- special, incidental, or consequential loss or damage
-- (including loss of data, profits, goodwill, or any type of
-- loss or damage suffered as a result of any action brought
-- by a third party) even if such damage or loss was
-- reasonably foreseeable or Xilinx had been advised of the
-- possibility of the same.
--
-- CRITICAL APPLICATIONS
-- Xilinx products are not designed or intended to be fail-
-- safe, or for use in any application requiring fail-safe
-- performance, such as life-support or safety devices or
-- systems, Class III medical devices, nuclear facilities,
-- applications related to the deployment of airbags, or any
-- other applications that could lead to death, personal
-- injury, or severe property or environmental damage
-- (individually and collectively, "Critical
-- Applications"). Customer assumes the sole risk and
-- liability of any use of Xilinx products in Critical
-- Applications, subject only to applicable laws and
-- regulations governing limitations on product liability.
--
-- THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
-- PART OF THIS FILE AT ALL TIMES.
--
-------------------------------------------------------------------------------
-- Filename:        synchronizers.vhd
--
-- Description:     
--
-- VHDL-Standard:   VHDL'93/02
-------------------------------------------------------------------------------
-- Structure:   
--              mb_sync_bit
--              mb_sync_vec
--                mb_sync_bit
--              mb_sync_reset
--
-------------------------------------------------------------------------------
-- Author:          rolandp
--
-- History:
--   rolandp 2014-09-01    First Version
--
-------------------------------------------------------------------------------
-- Naming Conventions:
--      active low signals:                     "*_n"
--      clock signals:                          "clk", "clk_div#", "clk_#x" 
--      reset signals:                          "rst", "rst_n" 
--      generics:                               "C_*" 
--      user defined types:                     "*_TYPE" 
--      state machine next state:               "*_ns" 
--      state machine current state:            "*_cs" 
--      combinatorial signals:                  "*_com" 
--      pipelined or register delay signals:    "*_d#" 
--      counter signals:                        "*cnt*"
--      clock enable signals:                   "*_ce" 
--      internal version of output port         "*_i"
--      device pins:                            "*_pin" 
--      ports:                                  - Names begin with Uppercase 
--      processes:                              "*_PROCESS" 
--      component instantiations:               "<ENTITY_>I_<#|FUNC>
-------------------------------------------------------------------------------

library IEEE;
use IEEE.std_logic_1164.all;

entity mb_sync_bit is
  generic(
    C_LEVELS            : natural   := 2;
    C_RESET_VALUE       : std_logic := '0';
    C_RESET_SYNCHRONOUS : boolean   := true;
    C_RESET_ACTIVE_HIGH : boolean   := true);
  port(
    Clk            : in  std_logic;
    Rst            : in  std_logic;
    Scan_En        : in  std_logic;
    Scan_Reset_Sel : in  std_logic;
    Scan_Reset     : in  std_logic;
    Raw            : in  std_logic;
    Synced         : out std_logic);
end mb_sync_bit;

library iomodule_v3_1_5;
use iomodule_v3_1_5.xil_scan_reset_control;

architecture IMP of mb_sync_bit is

  component xil_scan_reset_control is
  port (
    Scan_En          : in  std_logic;
    Scan_Reset_Sel   : in  std_logic;
    Scan_Reset       : in  std_logic;
    Functional_Reset : in  std_logic;
    Reset            : out std_logic);
  end component xil_scan_reset_control;
  
  -- Downgrade Synth 8-3332 warnings
  attribute DowngradeIPIdentifiedWarnings: string;
  attribute DowngradeIPIdentifiedWarnings of IMP : architecture is "yes";

begin

  -- Generate synchronizer DFFs
  Synchronize : if C_LEVELS > 1 generate
    signal reset : std_logic; 
    signal sync  : std_logic_vector(1 to C_LEVELS) := (others => C_RESET_VALUE);
    attribute ASYNC_REG : string;
    attribute ASYNC_REG of sync : signal is "TRUE";
  begin
    
    -- Internal reset always has active high polarity 
    reset <= Rst when C_RESET_ACTIVE_HIGH else
             not Rst;

    -- Synchronous reset
    use_sync_reset: if C_RESET_SYNCHRONOUS generate
    begin
      
      Sync_Rst_DFFs : process(Clk)
      begin
        if Clk'event and Clk = '1' then
          if reset = '1' then
            sync <= (sync'range  => C_RESET_VALUE); 
          else
            for I in C_LEVELS downto 2 loop
              sync(I) <= sync(I-1);
            end loop;
            sync(1) <= Raw;
          end if;
        end if;
      end process;

    end generate use_sync_reset;

    -- Asychronous reset
    use_async_reset: if not C_RESET_SYNCHRONOUS generate
      signal async_reset : std_logic;
    begin

      -- Make sure asynchronous reset can be controlled during scan test
      scan_reset_control_i: xil_scan_reset_control
        port map (
          Scan_En          => Scan_En,
          Scan_Reset_Sel   => Scan_Reset_Sel,
          Scan_Reset       => Scan_Reset,
          Functional_Reset => reset,
          Reset            => async_reset);
      
      Async_Rst_DFFs : process(Clk, async_reset)
      begin
        if async_reset = '1' then
          sync <= (sync'range => C_RESET_VALUE); 
        elsif Clk'event and Clk = '1' then
          for I in C_LEVELS downto 2 loop
            sync(I) <= sync(I-1);
          end loop;
          sync(1) <= Raw;
        end if;
      end process;

    end generate use_async_reset;

    Synced <= sync(C_LEVELS);

  end generate Synchronize;

  -- 1 synchronizer DFF
  Single_Synchronize : if C_LEVELS = 1 generate
    signal reset : std_logic;
    signal sync  : std_logic := C_RESET_VALUE;
  begin
    
    -- Internal reset always has active high polarity 
    reset <= Rst when C_RESET_ACTIVE_HIGH else
             not Rst;

    -- Synchronous reset
    use_sync_reset: if C_RESET_SYNCHRONOUS generate
    begin
      
      Sync_Rst_DFFs : process(Clk)
      begin
        if Clk'event and Clk = '1' then
          if reset = '1' then
            sync <= C_RESET_VALUE; 
          else
            sync <= Raw;
          end if;
        end if;
      end process;

    end generate use_sync_reset;

    -- Asychronous reset
    use_async_reset: if not C_RESET_SYNCHRONOUS generate
      signal async_reset : std_logic;
    begin

      -- Make sure asynchronous reset can be controlled from during scan test
      scan_reset_control_i: xil_scan_reset_control
      port map (
        Scan_En          => Scan_En,
        Scan_Reset_Sel   => Scan_Reset_Sel,
        Scan_Reset       => Scan_Reset,
        Functional_Reset => reset,
        Reset            => async_reset);
      
      Async_Rst_DFFs : process(Clk, async_reset)
      begin
        if async_reset = '1' then
          sync <= C_RESET_VALUE; 
        elsif Clk'event and Clk = '1' then
          sync <= Raw;
        end if;
      end process;

    end generate use_async_reset;

    Synced <= sync;
    
  end generate Single_Synchronize;

  -- No synchronizer DFFs, connect input to output directly
  No_Synchronize : if C_LEVELS = 0 generate
  begin
    Synced <= Raw;
  end generate No_Synchronize;
      
end architecture IMP;  -- mb_sync_bit


library iomodule_v3_1_5;
use iomodule_v3_1_5.mb_sync_bit;

library IEEE;
use IEEE.std_logic_1164.all;

entity mb_sync_vec is
  generic(
    C_LEVELS            : natural   := 2;
    C_RESET_VALUE       : std_logic := '0';
    C_RESET_SYNCHRONOUS : boolean   := true;
    C_RESET_ACTIVE_HIGH : boolean   := true;
    C_WIDTH             : natural);
  port(
    Clk            : in  std_logic;
    Rst            : in  std_logic := '0';
    Scan_En        : in  std_logic;
    Scan_Reset_Sel : in  std_logic := '0';
    Scan_Reset     : in  std_logic := '0';
    Raw            : in  std_logic_vector(0 to C_WIDTH-1);
    Synced         : out std_logic_vector(0 to C_WIDTH-1));
end mb_sync_vec;

architecture IMP of mb_sync_vec is

  component mb_sync_bit
  generic(
    C_LEVELS            : natural;
    C_RESET_VALUE       : std_logic;
    C_RESET_SYNCHRONOUS : boolean;
    C_RESET_ACTIVE_HIGH : boolean);
  port(
    Clk            : in  std_logic;
    Rst            : in  std_logic;
    Scan_En        : in  std_logic;
    Scan_Reset_Sel : in  std_logic;
    Scan_Reset     : in  std_logic;
    Raw            : in  std_logic;
    Synced         : out std_logic);
  end component;
  
begin

  sync_bits: for I in 0 to C_WIDTH-1 generate
  begin
    
    sync_bit : mb_sync_bit
      generic map(
        C_LEVELS             => C_LEVELS,
        C_RESET_VALUE        => C_RESET_VALUE,
        C_RESET_SYNCHRONOUS  => C_RESET_SYNCHRONOUS,
        C_RESET_ACTIVE_HIGH  => C_RESET_ACTIVE_HIGH)
      port map (
        Clk            => Clk,
        Rst            => Rst,
        Scan_En        => Scan_En,
        Scan_Reset_Sel => Scan_Reset_Sel,
        Scan_Reset     => Scan_Reset,
        Raw            => Raw(I),
        Synced         => Synced(I));
    
  end generate sync_bits;  

end architecture IMP;   -- mb_sync_vec


library IEEE;
use IEEE.std_logic_1164.all;

entity mb_sync_reset is
  generic(
    C_LEVELS            : natural := 2;
    C_RESET_VALUE       : std_logic := '1';
    C_RESET_ACTIVE_HIGH : boolean := true);
  port(
    Clk            : in  std_logic;
    Scan_En        : in  std_logic;
    Scan_Reset_Sel : in  std_logic;
    Scan_Reset     : in  std_logic;
    Raw            : in  std_logic;
    Synced         : out std_logic);
end mb_sync_reset;

library iomodule_v3_1_5;
use iomodule_v3_1_5.xil_scan_reset_control;

architecture IMP of mb_sync_reset is

  component xil_scan_reset_control is
  port (
    Scan_En          : in  std_logic;
    Scan_Reset_Sel   : in  std_logic;
    Scan_Reset       : in  std_logic;
    Functional_Reset : in  std_logic;
    Reset            : out std_logic);
  end component xil_scan_reset_control;

  signal preset       : std_logic;
  signal async_preset : std_logic;

  -- Downgrade Synth 8-3332 warnings
  attribute DowngradeIPIdentifiedWarnings: string;
  attribute DowngradeIPIdentifiedWarnings of IMP : architecture is "yes";

begin

  -- Internal preset always has active high polarity 
  preset <= Raw when C_RESET_ACTIVE_HIGH else
            not Raw;

  -- Make sure asynchronous preset can be controlled during scan test
  scan_reset_control_i: xil_scan_reset_control
    port map (
      Scan_En          => Scan_En,
      Scan_Reset_Sel   => Scan_Reset_Sel,
      Scan_Reset       => Scan_Reset,
      Functional_Reset => preset,
      Reset            => async_preset);

  -- Generate synchronizer DFFs
  Synchronize : if C_LEVELS > 1 generate
    signal sync : std_logic_vector(1 to C_LEVELS) := (others => C_RESET_VALUE);
    attribute ASYNC_REG : string;
    attribute ASYNC_REG of sync : signal is "TRUE";
  begin

    Reset_DFFs : process(Clk)
    begin
      if async_preset = '1' then
        sync <= (sync'range  => C_RESET_VALUE); 
      elsif Clk'event and Clk = '1' then
        for I in C_LEVELS downto 2 loop
          sync(I) <= sync(I-1);
        end loop;
        sync(1) <= Raw;
      end if;
    end process;

    Synced <= sync(C_LEVELS);
  end generate Synchronize;

  -- 1 synchronizer DFF
  Single_Synchronize : if C_LEVELS = 1 generate
    signal sync : std_logic := C_RESET_VALUE;
  begin

    Reset_DFFs : process(Clk)
    begin
      if async_preset = '1' then
        sync <= C_RESET_VALUE;
      elsif Clk'event and Clk = '1' then
        sync <= Raw;
      end if;
    end process;

    Synced <= sync;
  end generate Single_Synchronize;

  -- No synchronizer DFFs, connect input to output directly
  No_Synchronize : if C_LEVELS = 0 generate
  begin
    Synced <= Raw;
  end generate No_Synchronize;

end architecture IMP;  -- mb_sync_reset


-------------------------------------------------------------------------------
-- divide_part.vhd - Entity and architecture
-------------------------------------------------------------------------------
--
-- (c) Copyright 2011 Xilinx, Inc. All rights reserved.
--
-- This file contains confidential and proprietary information
-- of Xilinx, Inc. and is protected under U.S. and 
-- international copyright and other intellectual property
-- laws.
--
-- DISCLAIMER
-- This disclaimer is not a license and does not grant any
-- rights to the materials distributed herewith. Except as
-- otherwise provided in a valid license issued to you by
-- Xilinx, and to the maximum extent permitted by applicable
-- law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND
-- WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES
-- AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
-- BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-
-- INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and
-- (2) Xilinx shall not be liable (whether in contract or tort,
-- including negligence, or under any other theory of
-- liability) for any loss or damage of any kind or nature
-- related to, arising under or in connection with these
-- materials, including for any direct, or any indirect,
-- special, incidental, or consequential loss or damage
-- (including loss of data, profits, goodwill, or any type of
-- loss or damage suffered as a result of any action brought
-- by a third party) even if such damage or loss was
-- reasonably foreseeable or Xilinx had been advised of the
-- possibility of the same.
--
-- CRITICAL APPLICATIONS
-- Xilinx products are not designed or intended to be fail-
-- safe, or for use in any application requiring fail-safe
-- performance, such as life-support or safety devices or
-- systems, Class III medical devices, nuclear facilities,
-- applications related to the deployment of airbags, or any
-- other applications that could lead to death, personal
-- injury, or severe property or environmental damage
-- (individually and collectively, "Critical
-- Applications"). Customer assumes the sole risk and
-- liability of any use of Xilinx products in Critical
-- Applications, subject only to applicable laws and
-- regulations governing limitations on product liability.
--
-- THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
-- PART OF THIS FILE AT ALL TIMES.
--
-------------------------------------------------------------------------------
-- Filename:        divide_part.vhd
--
-- Description:     
--                  
-- VHDL-Standard:   VHDL'93
-------------------------------------------------------------------------------
-- Structure:   
--              divide_part.vhd
--
-------------------------------------------------------------------------------
-- Author:          goran
--
-- History:
--   goran  2007-12-19    First Version
--
-------------------------------------------------------------------------------
-- Naming Conventions:
--      active low signals:                     "*_n"
--      clock signals:                          "clk", "clk_div#", "clk_#x" 
--      reset signals:                          "rst", "rst_n" 
--      generics:                               "C_*" 
--      user defined types:                     "*_TYPE" 
--      state machine next state:               "*_ns" 
--      state machine current state:            "*_cs" 
--      combinatorial signals:                  "*_com" 
--      pipelined or register delay signals:    "*_d#" 
--      counter signals:                        "*cnt*"
--      clock enable signals:                   "*_ce" 
--      internal version of output port         "*_i"
--      device pins:                            "*_pin" 
--      ports:                                  - Names begin with Uppercase 
--      processes:                              "*_PROCESS" 
--      component instantiations:               "<ENTITY_>I_<#|FUNC>
-------------------------------------------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;
library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_funcs.all;

entity Divide_part is
  generic (
    C_TARGET    : TARGET_FAMILY_TYPE;
    C_USE_SRL16 : string;
    Ratio       : natural;
    First       : boolean := true
    );
  port (
    Config_Reset : in  std_logic;
    Clk          : in  std_logic;
    Rst          : in  std_logic;
    Clk_En       : in  std_logic;
    Clk_En_Out   : out std_logic
    );

end entity Divide_part;

library ieee;
use ieee.numeric_std.all;

architecture VHDL_RTL of Divide_part is
  
  component XIL_SRL16E is
    generic (
      C_TARGET    : TARGET_FAMILY_TYPE;
      C_USE_SRL16 : string;
      C_STATIC    : boolean := false;
      INIT        : bit_vector);
    port (
      Config_Reset : in  std_logic;
      Q            : out std_logic;
      A0           : in  std_logic;
      A1           : in  std_logic;
      A2           : in  std_logic;
      A3           : in  std_logic;
      CE           : in  std_logic;
      CLK          : in  std_logic;
      D            : in  std_logic);
  end component XIL_SRL16E;
  
  component XIL_SRLC16E is
    generic (
      C_TARGET    : TARGET_FAMILY_TYPE;
      C_USE_SRL16 : string;
      INIT        : bit_vector);
    port (
      Config_Reset : in  std_logic;
      Q            : out std_logic;
      Q15          : out std_logic;
      A0           : in  std_logic;
      A1           : in  std_logic;
      A2           : in  std_logic;
      A3           : in  std_logic;
      CE           : in  std_logic;
      CLK          : in  std_logic;
      D            : in  std_logic);
  end component XIL_SRLC16E;
  
  signal loop_Bit   : std_logic;
  signal loop_Bit_i : std_logic;

  -- Set clock enable during reset
  signal Clk_En_i   : std_logic;

  -- Previous cycle reset, used to determine when to shift in a 1
  signal Rst_d1     : std_logic := '0';

  -- This prevents an X from showing up in ModelSim
  -- by simulating the default value in the hardware
  signal Clk_En_Out_i : std_logic := '0';

  constant Nr_Of_SRL16      : natural                      := 1 + ((Ratio-1)/16);
  constant Last_SRL16_Ratio : natural                      := ((Ratio-1) mod 16);
  constant A                : std_logic_vector(3 downto 0) :=
    std_logic_vector(to_unsigned(Last_SRL16_Ratio, 4));

begin  -- architecture VHDL_RTL
  
  One_SRL16 : if (Nr_Of_SRL16 = 1) generate
  begin
    SRL16E_I : XIL_SRL16E
      generic map (
        C_TARGET    => C_TARGET,
        C_USE_SRL16 => C_USE_SRL16,     -- [string]
        INIT        => X"0001")         -- [bit_vector]
      port map (
        Config_Reset => Config_Reset,   -- [in  std_logic]
        CE           => Clk_En_i,       -- [in  std_logic]
        D            => loop_Bit_i,     -- [in  std_logic]
        Clk          => Clk,            -- [in  std_logic]
        A0           => A(0),           -- [in  std_logic]
        A1           => A(1),           -- [in  std_logic]
        A2           => A(2),           -- [in  std_logic]
        A3           => A(3),           -- [in  std_logic]
        Q            => loop_Bit);      -- [out std_logic]
  end generate One_SRL16;

  Two_SRL16 : if (Nr_Of_SRL16 = 2) generate

    signal shift   : std_logic;
    signal shift_i : std_logic;
    -- signal Emptys : std_logic_vector(0 to Nr_Of_SRL16);
  begin
    -- Shift in 0's during reset
    shift_i <= shift;

    -- The first SRLC16E
    SRLC16E_1 : XIL_SRLC16E
      generic map (
        C_TARGET    => C_TARGET,
        C_USE_SRL16 => C_USE_SRL16,     -- [string]
        INIT        => X"0001")         -- [bit_vector]
      port map (
        Config_Reset => Config_Reset,   -- [in  std_logic]
        CE           => Clk_En_i,       -- [in  std_logic]
        D            => loop_Bit_i,     -- [in  std_logic]
        Clk          => Clk,            -- [in  std_logic]
        A0           => '1',            -- [in  std_logic]
        A1           => '1',            -- [in  std_logic]
        A2           => '1',            -- [in  std_logic]
        A3           => '1',            -- [in  std_logic]
        Q15          => shift,          -- [out  std_logic]
        Q            => open);          -- [out std_logic]

    SRL16E_2 : XIL_SRL16E
      generic map (
        C_TARGET    => C_TARGET,
        C_USE_SRL16 => C_USE_SRL16,     -- [string]
        INIT        => X"0000")         -- [bit_vector]
      port map (
        Config_Reset => Config_Reset,   -- [in  std_logic]
        CE           => Clk_En_i,       -- [in  std_logic]
        D            => shift_i,        -- [in  std_logic]
        Clk          => Clk,            -- [in  std_logic]
        A0           => A(0),           -- [in  std_logic]
        A1           => A(1),           -- [in  std_logic]
        A2           => A(2),           -- [in  std_logic]
        A3           => A(3),           -- [in  std_logic]
        Q            => loop_Bit);      -- [out std_logic]
  end generate Two_SRL16;

  More_Than_Two : if (Nr_Of_SRL16 > 2) generate

    signal shifts   : std_logic_vector(1 to Nr_Of_SRL16-1);
    signal shifts_i : std_logic_vector(1 to Nr_Of_SRL16-1);
    -- signal Emptys : std_logic_vector(0 to Nr_Of_SRL16);
  begin

    -- Shift in 0's during reset
    Shifts_I_Rst : for I in 1 to Nr_Of_SRL16-1 generate
       shifts_i(I) <= shifts(I);
    end generate Shifts_I_Rst;

    -- The first SRLC16E
    SRLC16E_1 : XIL_SRLC16E
      generic map (
        C_TARGET    => C_TARGET,
        C_USE_SRL16 => C_USE_SRL16,     -- [string]
        INIT        => X"0001")         -- [bit_vector]
      port map (
        Config_Reset => Config_Reset,   -- [in  std_logic]
        CE           => Clk_En_i,       -- [in  std_logic]
        D            => loop_Bit_i,     -- [in  std_logic]
        Clk          => Clk,            -- [in  std_logic]
        A0           => '1',            -- [in  std_logic]
        A1           => '1',            -- [in  std_logic]
        A2           => '1',            -- [in  std_logic]
        A3           => '1',            -- [in  std_logic]
        Q15          => shifts(1),      -- [out  std_logic]
        Q            => open);          -- [out std_logic]

    The_Rest : for I in 1 to Nr_Of_SRL16-2 generate
    begin
      SRLC16E_I : XIL_SRLC16E
        generic map (
          C_TARGET    => C_TARGET,
          C_USE_SRL16 => C_USE_SRL16,    -- [string]
          INIT        => X"0000")        -- [bit_vector]
        port map (
          Config_Reset => Config_Reset,  -- [in  std_logic]
          CE           => Clk_En_i,      -- [in  std_logic]
          D            => shifts_i(I),   -- [in  std_logic]
          Clk          => Clk,           -- [in  std_logic]
          A0           => '1',           -- [in  std_logic]
          A1           => '1',           -- [in  std_logic]
          A2           => '1',           -- [in  std_logic]
          A3           => '1',           -- [in  std_logic]
          Q15          => shifts(I+1),   -- [out  std_logic]
          Q            => open);         -- [out std_logic]

    end generate The_Rest;

    -- The last SRL16
    SRL16E_n : XIL_SRL16E
      generic map (
        C_TARGET    => C_TARGET,
        C_USE_SRL16 => C_USE_SRL16,               -- [string]
        INIT        => X"0000")                   -- [bit_vector]
      port map (
        Config_Reset => Config_Reset,             -- [in  std_logic]
        CE           => Clk_En_i,                 -- [in  std_logic]
        D            => shifts_i(Nr_Of_SRL16-1),  -- [in  std_logic]
        Clk          => Clk,                      -- [in  std_logic]
        A0           => A(0),                     -- [in  std_logic]
        A1           => A(1),                     -- [in  std_logic]
        A2           => A(2),                     -- [in  std_logic]
        A3           => A(3),                     -- [in  std_logic]
        Q            => loop_Bit);                -- [out std_logic]

  end generate More_Than_Two;

  -- Store if the previous cycle was a reset
  Clk_Rst_D1 : process (Clk) is
  begin
    if Clk'event and Clk = '1' then   -- rising clock edge
      Rst_d1 <= Rst;
    end if;
  end process Clk_Rst_D1;

  -- Set clock enable during reset, Rst_d1 is necessary to load
  -- the 1 bit in from loop_bit_i
  clk_en_i   <= Clk_En;-- or Rst or Rst_d1;

  -- Loops around from previous interrupt, or inserts after reset
  -- The reset pulse must be at least 17 cycles
  loop_Bit_i <= loop_Bit; --(loop_Bit or Rst_d1);

  -- Same signal, but internal version has default value for
  -- simulation
  Clk_En_Out <= Clk_En_Out_i;

  -----------------------------------------------------------------------------
  -- If the SRL16 is the first in a series then the output is a clean single
  -- clock pulse
  -----------------------------------------------------------------------------
  Is_First : if (First) generate
    Clk_En_Out_i <= loop_Bit;
  end generate Is_First;


  -----------------------------------------------------------------------------
  -- If not the first the output has to be masked so that it produce a single
  -- clock pulse
  -----------------------------------------------------------------------------
  not_First : if (not First) generate
    signal Out1 : std_logic;
  begin

    Out1_DFF : process (Clk) is
    begin  -- process Out1_DFF
      if Clk'event and Clk = '1' then   -- rising clock edge
        Out1 <= loop_Bit;
      end if;
    end process Out1_DFF;

    Out2_DFF : process (Clk) is
    begin  -- process Out2_DFF
      if Clk'event and Clk = '1' then   -- rising clock edge
        if (Out1 = '1') then
          Clk_En_Out_i <= Clk_En;
        end if;
      end if;
    end process Out2_DFF;
    
  end generate not_First;
end architecture VHDL_RTL;


-------------------------------------------------------------------------------
-- fit_module.vhd - Entity and architecture
-------------------------------------------------------------------------------
--
-- (c) Copyright 2011-2012,2016 Xilinx, Inc. All rights reserved.
--
-- This file contains confidential and proprietary information
-- of Xilinx, Inc. and is protected under U.S. and
-- international copyright and other intellectual property
-- laws.
--
-- DISCLAIMER
-- This disclaimer is not a license and does not grant any
-- rights to the materials distributed herewith. Except as
-- otherwise provided in a valid license issued to you by
-- Xilinx, and to the maximum extent permitted by applicable
-- law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND
-- WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES
-- AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
-- BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-
-- INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and
-- (2) Xilinx shall not be liable (whether in contract or tort,
-- including negligence, or under any other theory of
-- liability) for any loss or damage of any kind or nature
-- related to, arising under or in connection with these
-- materials, including for any direct, or any indirect,
-- special, incidental, or consequential loss or damage
-- (including loss of data, profits, goodwill, or any type of
-- loss or damage suffered as a result of any action brought
-- by a third party) even if such damage or loss was
-- reasonably foreseeable or Xilinx had been advised of the
-- possibility of the same.
--
-- CRITICAL APPLICATIONS
-- Xilinx products are not designed or intended to be fail-
-- safe, or for use in any application requiring fail-safe
-- performance, such as life-support or safety devices or
-- systems, Class III medical devices, nuclear facilities,
-- applications related to the deployment of airbags, or any
-- other applications that could lead to death, personal
-- injury, or severe property or environmental damage
-- (individually and collectively, "Critical
-- Applications"). Customer assumes the sole risk and
-- liability of any use of Xilinx products in Critical
-- Applications, subject only to applicable laws and
-- regulations governing limitations on product liability.
--
-- THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
-- PART OF THIS FILE AT ALL TIMES.
--
-------------------------------------------------------------------------------
-- Filename:        fit_module.vhd
--
-- Description:
--
-- VHDL-Standard:   VHDL'93
-------------------------------------------------------------------------------
-- Structure:
--              fit_module.vhd
--
-------------------------------------------------------------------------------
-- Author:          goran
--
-- History:
--   goran  2007-12-19    First Version
-- stefana  2012-05-30    Fixed log2 function
--
-------------------------------------------------------------------------------
-- Naming Conventions:
--      active low signals:                     "*_n"
--      clock signals:                          "clk", "clk_div#", "clk_#x"
--      reset signals:                          "rst", "rst_n"
--      generics:                               "C_*"
--      user defined types:                     "*_TYPE"
--      state machine next state:               "*_ns"
--      state machine current state:            "*_cs"
--      combinatorial signals:                  "*_com"
--      pipelined or register delay signals:    "*_d#"
--      counter signals:                        "*cnt*"
--      clock enable signals:                   "*_ce"
--      internal version of output port         "*_i"
--      device pins:                            "*_pin"
--      ports:                                  - Names begin with Uppercase
--      processes:                              "*_PROCESS"
--      component instantiations:               "<ENTITY_>I_<#|FUNC>
-------------------------------------------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_funcs.all;

entity FIT_Module is
  generic (
    C_TARGET          : TARGET_FAMILY_TYPE;
    C_TMR             : integer := 0;
    C_USE_TMR_DISABLE : integer := 0;
    C_VOTE_SIZE       : integer := 0;
    C_USE_SRL16       : string;
    C_USE_FIT         : integer := 1;
    C_NO_CLOCKS       : integer := 6216;  -- The number of clocks between each interrupt
    C_INACCURACY      : integer := 5      -- The maximum inaccuracy of the number
                                          -- of clocks allowed in per thousands
    );
  port (
    Config_Reset : in  std_logic;
    Clk          : in  std_logic;
    Reset        : in  boolean;
    TMR_Disable  : in  std_logic;
    FromAVote    : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
    FromBVote    : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
    ToVote       : out std_logic_vector(C_VOTE_SIZE-1 downto 0);
    Toggle       : out std_logic;
    Interrupt    : out std_logic);
end entity FIT_Module;

library iomodule_v3_1_5;
use iomodule_v3_1_5.all;
use iomodule_v3_1_5.iomodule_vote_pkg.all;

architecture VHDL_RTL of FIT_Module is

  constant C_NO_CLOCKS_MIN : integer := 3;

  component Divide_Part is
    generic (
      C_TARGET    : TARGET_FAMILY_TYPE;
      C_USE_SRL16 : string;
      Ratio       : natural;
      First       : boolean);
    port (
      Config_Reset : in  std_logic;
      Clk          : in  std_logic;
      Rst          : in  std_logic;
      Clk_En       : in  std_logic;
      Clk_En_Out   : out std_logic);
  end component Divide_Part;

  component MB_MUXCY_XORCY is
    generic (
      C_TARGET : TARGET_FAMILY_TYPE);
    port (
      O  : out std_logic;
      LO : out std_logic;
      CI : in  std_logic;
      DI : in  std_logic;
      S  : in  std_logic);
  end component MB_MUXCY_XORCY;

  -- log2 function returns the number of bits required to encode x choices
  function log2(x : natural) return integer is
    variable i : integer := 0;
  begin
    if x = 0 then return 0;
    elsif x > 2**30 then return 31;
    else
      while 2**i < x loop
        i := i+1;
      end loop;
      return i;
    end if;
  end function log2;

  -----------------------------------------------------------------------------
  -- All supported architectures have the SRL16C primitive, we will actually
  -- looking for factorials upto 128 (upto 7 SRL16s in a chain)
  -- looking for any more is not efficient since 128 can be done with 7 LUTs in
  -- a normal counter
  -----------------------------------------------------------------------------
  constant MAX_DIV_FACTOR : natural := 128;

  subtype SRL16_DIV_TYPE is natural range 2 to MAX_DIV_FACTOR;
  type FACTORS_LIST_TYPE is array (natural range 1 to 15) of SRL16_DIV_TYPE;

  type FACTORS_TYPE is
  record
    Good_Divide   : boolean;
    Nr_Of_Factors : natural;
    Factor_List   : FACTORS_LIST_TYPE;
    Nr_Of_SRL16s  : natural;
  end record FACTORS_TYPE;

  -----------------------------------------------------------------------------
  -- Trying to divide R into integer values of values 2-16 until the end result
  -- is between 2-16.
  --
  -- This function returns a FACTORS_TYPE which contains:
  -- FACTOR_LIST   - List of factors
  -- Nr_Of_Factors - Number of factors / Number of divide_parts needed
  -- Nr_Of_SRL16s  - Number of SRL16s
  -- Good_Divide   - Whether the number could be factored
  -----------------------------------------------------------------------------
  function Get_Factors (R : natural) return FACTORS_TYPE is
    variable N      : natural := R;
    variable Result : FACTORS_TYPE;
    variable no     : natural := 1;
    variable Found  : boolean;
  begin  -- function Get_Factors
    -- Initialize values
    Result.Nr_Of_Factors := 0;
    Result.Nr_Of_SRL16s  := 0;
    Result.Factor_List   := (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2);

    -- Check if we can do it in one SRL16
    if (N < 16) then
      Result.FACTOR_LIST(1) := N;
      Result.Nr_Of_Factors  := 1;
      Result.Good_Divide    := true;
      Result.Nr_Of_SRL16s   := 1;
      return Result;
    end if;

    -- Each time through this loop it finds a factor
    -- The factor gets added to FACTOR_LIST(no)
    -- The Nr_Of_SRL16s is increased appropriately
    while N /= 1 loop
      Found := false;
      -- Trying first with a single SRL16 since it the most efficient implementation
      --
      -- Find largest factor from 16 down to 2, because no matter what value
      -- it is in this range, it will use a single SRL16
      for I in 16 downto 2 loop
        if ((N mod I = 0)) then         -- Found factor
          Result.FACTOR_LIST(no) := I;
          Result.Nr_Of_SRL16s    := Result.Nr_Of_SRL16s + 1;
          N                      := N / I;
          no                     := no + 1;
          Found                  := true;
          exit;
        end if;
      end loop;  -- I

      -- No factor from 2 to 16
      if (not(Found)) then
        -- Trying from 17 upto MAX_DIV_FACTOR to find if a chain of SRL16 can
        -- be used
        -- Find the smallest value for the smallest chain
        for I in 17 to MAX_DIV_FACTOR loop
          if ((N mod I = 0)) then       -- Found factor
            Result.FACTOR_LIST(no) := I;
            Result.Nr_Of_SRL16s    := Result.Nr_Of_SRL16s + (1 + ((I-1)/16));
            N                      := N / I;
            no                     := no + 1;
            Found                  := true;
            exit;
          end if;
        end loop;  -- I
      end if;

      -- No factor from 2 to MAX_DIV_FACTOR
      if (not(Found)) then
        -- Could not factor
        Result.Good_Divide := false;
        exit;
      end if;
    end loop;
    if (found) then
      Result.Good_Divide   := true;
      Result.Nr_Of_Factors := no-1;
    end if;
    return Result;
  end function Get_Factors;

  -----------------------------------------------------------------------------
  -- Trying to find a ratio that is within 1.5% of the asked ratio and that the
  -- ratio can be implemented with SRL16.
  -----------------------------------------------------------------------------
  function Find_Best_Factors (R : natural) return FACTORS_TYPE is
    constant Proc_Diff : natural := R*C_INACCURACY/1000;  -- Calculate the max difference
                                                          -- for the maximum inaccuracy
    variable Result    : FACTORS_TYPE;
  begin  -- function Find_Best_Factors
    Result := Get_Factors(R);

    if (Result.Good_Divide) then
      return Result;
    end if;

    -- This if statement gets rid of a warning if C_INACCURACY = 0
    if (Proc_Diff > 0) then
      for I in 1 to Proc_Diff loop
        Result := Get_Factors(R+I);
        if (Result.Good_Divide) then
          return Result;
        end if;
        Result := Get_Factors(R-I);
        if (Result.Good_Divide) then
          return Result;
        end if;
      end loop;  -- I
    end if;  -- Proc_Diff > 0

    Result.Good_Divide := false;
    return Result;
  end function Find_Best_Factors;

begin  -- architecture VHDL_RTL

  Implement_FIT : if (C_USE_FIT /= 0 and C_NO_CLOCKS >= C_NO_CLOCKS_MIN) generate

    constant Nr_Of_Bits     : natural := log2(C_NO_CLOCKS-1);
    constant Divide_Factors : FACTORS_TYPE := Find_Best_Factors(C_NO_CLOCKS);
    signal Interrupt_i : std_logic := '0';
    signal rst_i       : std_logic;
    signal toggle_i    : std_logic;

  begin

    -----------------------------------------------------------------------------
    -- handle the reset
    -----------------------------------------------------------------------------
    rst_i <= '1' when Reset else '0';

    -----------------------------------------------------------------------------
    -- A clean and good ratio was found that was within the 1.5% limit, so
    -- implement the fit_timer division using SRL16s but only if the number of
    -- SRL16 is less than what is needed for a standard down-counter
    --
    -- ex. the value 127*127 = 16129 can be split into two SRL16 chain where each
    -- chain is 8 SRL16 => a total of 16 SRL16. But a 14-bit counter can count to
    -- 16129 and it will only consume 14 LUTs so it's more area efficient
    -----------------------------------------------------------------------------
    Using_SRL16s : if (Divide_Factors.Good_Divide) and
                      (Divide_Factors.Nr_Of_SRL16s <= Nr_Of_Bits) and (C_TMR = 0) generate

      signal Clk_En_I : std_logic_vector(0 to Divide_Factors.Nr_Of_Factors);

    begin
      Clk_En_I(0) <= '1';

      SRL16s : for I in 1 to Divide_Factors.Nr_Of_Factors generate
      begin

        Divide_I : Divide_Part
          generic map (
            C_TARGET    => C_TARGET,
            C_USE_SRL16 => C_USE_SRL16,                    -- [string]
            Ratio       => Divide_Factors.FACTOR_LIST(I),  -- [natural range 2 to 16]
            First       => (I = 1))                        -- [boolean]
          port map (
            Config_Reset => Config_Reset,                  -- [in  std_logic]
            Clk          => Clk,                           -- [in  std_logic]
            Rst          => rst_i,                         -- [in  std_logic]
            Clk_En       => Clk_En_I(I-1),                 -- [in  std_logic]
            Clk_En_Out   => Clk_En_I(I));                  -- [out std_logic]

      end generate SRL16s;

      Interrupt_i <= Clk_En_I(Divide_Factors.Nr_Of_Factors);

      ToVote <= (others => '0');
    end generate Using_SRL16s;

    -----------------------------------------------------------------------------
    -- Couldn't find a good ratio within the 1.5% limit so implement the fit_timer
    -- generation using a standard counter or
    -- the number of SRL16 is greater than the number of LUTS a standard down-counter needs
    -----------------------------------------------------------------------------
    Using_Counter : if (not Divide_Factors.Good_Divide) or
                       (Divide_Factors.Nr_Of_SRL16s > Nr_Of_Bits) or (C_TMR /= 0) generate

      constant New_Value : std_logic_vector(0 to Nr_Of_Bits-1) :=
        std_logic_vector(to_unsigned(natural(C_NO_CLOCKS-2), Nr_Of_Bits));

      signal Cnt         : std_logic_vector(0 to Nr_Of_Bits-1);
      signal New_Cnt     : std_logic_vector(0 to Nr_Of_Bits-1);
      signal Carry       : std_logic_vector(0 to Nr_Of_Bits);
      signal Count       : std_logic_vector(0 to Nr_Of_Bits-1) := New_Value;
      signal rst_cnt     : std_logic                           := '0';

    begin

      -- Reset the counter
      rst_cnt <= Interrupt_i or rst_i;

      Carry(Nr_Of_Bits) <= '0';         -- Always subtracting

      All_Bits : for I in Nr_Of_Bits-1 downto 0 generate
      begin
        -- New_Cnt counts up
        -- New_Cnt(I) <= not(Count(I)) when Interrupt_i = '0' else New_Value(I);
        New_Cnt(I) <= not(Count(I));

        MUXCY_XORCY_L_I1 : MB_MUXCY_XORCY
          generic map (
            C_TARGET => C_TARGET)
          port map (
            DI => Count(I),             -- [in  std_logic S = 0]
            CI => Carry(I+1),           -- [in  std_logic S = 1]
            S  => New_Cnt(I),           -- [in  std_logic (Select)]
            LO => Carry(I),             -- [out std_logic]
            O  => Cnt(I));              -- [out std_logic]

      end generate All_Bits;

      TMR_No : if (C_TMR = 0) generate
      begin
        -- Count goes from all 1's during interrupt_i
        -- then C_NO_CLOCKS-1 down to 0 between interrupts
        Counter : process (Clk) is
        begin  -- process Counter
          if Clk'event and Clk = '1' then  -- rising clock edge
            if rst_cnt = '1' then
              Count       <= New_Value;
              Interrupt_i <= '0';
            else
              Count       <= Cnt;
              Interrupt_i <= not Carry(0);
            end if;
          end if;
        end process Counter;

        ToVote(FIT_COUNT_Pos)     <= (others => '0');
        ToVote(FIT_INTERRUPT_Pos) <= '0';

      end generate TMR_No;

      TMR_Yes : if (C_TMR /= 0) generate
        signal tmr_disable_b : boolean;
        signal Interrupt_i_d : std_logic;
        signal Count_d       : std_logic_vector(0 to Nr_Of_Bits-1);
      begin

        tmr_disable_b <= TMR_Disable = '1' and C_USE_TMR_DISABLE = 1;

        Counter_Logic : process (rst_cnt, Cnt, Carry) is
        begin
          if rst_cnt = '1' then
            Count_d       <= New_Value;
            Interrupt_i_d <= '0';
          else
            Count_d       <= Cnt;
            Interrupt_i_d <= not Carry(0);
          end if;
        end process Counter_Logic;

        ToVote(FIT_COUNT_Pos'low + Count_d'length - 1 downto FIT_COUNT_Pos'low) <= Count_d;
        ToVote(FIT_COUNT_Pos'high downto FIT_COUNT_POs'low + Count_d'length )   <= (others => '0');

        ToVote(FIT_INTERRUPT_Pos) <= Interrupt_i_d;

        Counter_DFF : process (Clk) is
        begin
          if Clk'event and Clk = '1' then  -- rising clock edge
            Count       <= vote(Count_d,
                                FromAVote(FIT_COUNT_Pos'low + Count_d'length - 1 downto FIT_COUNT_Pos'low),
                                FromBVote(FIT_COUNT_Pos'low + Count_d'length - 1 downto FIT_COUNT_Pos'low),
                                tmr_disable_b);
            Interrupt_i <= vote(Interrupt_i_d,FromAVote(FIT_INTERRUPT_Pos),FromBVote(FIT_INTERRUPT_Pos),
                                tmr_disable_b);
          end if;
        end process Counter_DFF;

      end generate TMR_Yes;

    end generate Using_Counter;

    Interrupt <= Interrupt_i;

    TMR_No_Toggle : if (C_TMR = 0) generate
    begin

      Toggle_Handler : process (Clk) is
      begin
        if Clk'event and Clk = '1' then
          if Reset then
            toggle_i <= '0';
          elsif Interrupt_i = '1' then
            toggle_i <= not toggle_i;
          end if;
        end if;
      end process Toggle_Handler;

      ToVote(FIT_TOGGLE_Pos) <= '0';

    end generate TMR_No_Toggle;

    TMR_Yes_Toggle : if (C_TMR /= 0) generate
      signal tmr_disable_b  : boolean;
      signal toggle_i_d     : std_logic;
    begin

      tmr_disable_b <= TMR_Disable = '1' and C_USE_TMR_DISABLE = 1;

      Toggle_Logic : process (Reset, toggle_i, Interrupt_i) is
      begin
        if Reset then
          toggle_i_d <= '0';
        elsif Interrupt_i = '1' then
          toggle_i_d <= not toggle_i;
        else
          toggle_i_d <= toggle_i;
        end if;
      end process Toggle_Logic;

      Toggle_DFF : process (Clk) is
      begin
        if Clk'event and Clk = '1' then
          toggle_i <= vote(toggle_i_d,FromAVote(FIT_TOGGLE_Pos),FromBVote(FIT_TOGGLE_Pos),tmr_disable_b);
        end if;
      end process Toggle_DFF;

      ToVote(FIT_TOGGLE_Pos) <= toggle_i_d;

    end generate TMR_Yes_Toggle;

    Toggle <= toggle_i;

  end generate Implement_FIT;

  Nothing : if (C_USE_FIT = 0 or C_NO_CLOCKS < C_NO_CLOCKS_MIN) generate
  begin
    Interrupt <= '0';
    Toggle    <= '0';
    ToVote    <= (others => '0');
  end generate Nothing;

end architecture VHDL_RTL;


-------------------------------------------------------------------------------
-- gpi_module.vhd - Entity and architecture
-------------------------------------------------------------------------------
--
-- (c) Copyright 2011-2013 Xilinx, Inc. All rights reserved.
--
-- This file contains confidential and proprietary information
-- of Xilinx, Inc. and is protected under U.S. and 
-- international copyright and other intellectual property
-- laws.
--
-- DISCLAIMER
-- This disclaimer is not a license and does not grant any
-- rights to the materials distributed herewith. Except as
-- otherwise provided in a valid license issued to you by
-- Xilinx, and to the maximum extent permitted by applicable
-- law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND
-- WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES
-- AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
-- BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-
-- INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and
-- (2) Xilinx shall not be liable (whether in contract or tort,
-- including negligence, or under any other theory of
-- liability) for any loss or damage of any kind or nature
-- related to, arising under or in connection with these
-- materials, including for any direct, or any indirect,
-- special, incidental, or consequential loss or damage
-- (including loss of data, profits, goodwill, or any type of
-- loss or damage suffered as a result of any action brought
-- by a third party) even if such damage or loss was
-- reasonably foreseeable or Xilinx had been advised of the
-- possibility of the same.
--
-- CRITICAL APPLICATIONS
-- Xilinx products are not designed or intended to be fail-
-- safe, or for use in any application requiring fail-safe
-- performance, such as life-support or safety devices or
-- systems, Class III medical devices, nuclear facilities,
-- applications related to the deployment of airbags, or any
-- other applications that could lead to death, personal
-- injury, or severe property or environmental damage
-- (individually and collectively, "Critical
-- Applications"). Customer assumes the sole risk and
-- liability of any use of Xilinx products in Critical
-- Applications, subject only to applicable laws and
-- regulations governing limitations on product liability.
--
-- THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
-- PART OF THIS FILE AT ALL TIMES.
--
-------------------------------------------------------------------------------
-- Filename:        gpi_module.vhd
--
-- Description:     
--                  
-- VHDL-Standard:   VHDL'93/02
-------------------------------------------------------------------------------
-- Structure:   
--              gpi_module.vhd
--
-------------------------------------------------------------------------------
-- Author:          goran
--
-- History:
--   goran   2007-12-19    First Version
--   stefana 2012-03-20    Added interrupt
--
-------------------------------------------------------------------------------
-- Naming Conventions:
--      active low signals:                     "*_n"
--      clock signals:                          "clk", "clk_div#", "clk_#x" 
--      reset signals:                          "rst", "rst_n" 
--      generics:                               "C_*" 
--      user defined types:                     "*_TYPE" 
--      state machine next state:               "*_ns" 
--      state machine current state:            "*_cs" 
--      combinatorial signals:                  "*_com" 
--      pipelined or register delay signals:    "*_d#" 
--      counter signals:                        "*cnt*"
--      clock enable signals:                   "*_ce" 
--      internal version of output port         "*_i"
--      device pins:                            "*_pin" 
--      ports:                                  - Names begin with Uppercase 
--      processes:                              "*_PROCESS" 
--      component instantiations:               "<ENTITY_>I_<#|FUNC>
-------------------------------------------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;

entity GPI_Module is
  generic (
    C_USE_GPI       : integer;
    C_GPI_SIZE      : integer;
    C_GPI_INTERRUPT : integer);
  port (
    Clk           : in  std_logic;
    Reset         : in  boolean;
    Config_Reset  : in  std_logic;
    GPI_Read      : in  std_logic;
    GPI           : in  std_logic_vector(C_GPI_SIZE-1 downto 0);
    GPI_In        : out std_logic_vector(C_GPI_SIZE-1 downto 0);
    GPI_Interrupt : out std_logic
    );
end entity GPI_Module;

architecture IMP of GPI_Module is

begin  -- architecture IMP

  Using_GPI : if (C_USE_GPI /= 0) generate
    signal GPI_Sampled : std_logic_vector(C_GPI_SIZE-1 downto 0);
  begin

    ----------------------------------------------------------------------------------------------
    -- Hold GPI_In signal in constant reset until we want to read the values.
    -- This allows us to just ORing all IO registers which we want to read
    ----------------------------------------------------------------------------------------------
    GPI_Sampling : process (Clk) is
    begin  -- process GPI_Sampling
      if Clk'event and Clk = '1' then  -- rising clock edge
        if (GPI_Read = '0' or Config_Reset = '1') then       -- synchronous reset (active high)
          GPI_In <= (others => '0');
        else
          GPI_In <= GPI;
        end if;
        if Config_Reset = '1' then
          GPI_Sampled <= (others => '0');
        else
          GPI_Sampled <= GPI;
        end if;
      end if;
    end process GPI_Sampling;

    ----------------------------------------------------------------------------------------------
    -- Generate interupt pulse whenever input differs from sampled value according to setting
    ----------------------------------------------------------------------------------------------
    Use_GPI_Interrupt : if (C_GPI_INTERRUPT /= 0) generate
      constant Zero : std_logic_vector(C_GPI_SIZE-1 downto 0) := (others => '0');
    begin

      GPI_Interrupt_DFF : process (Clk) is
      begin  -- process GPI_Interrupt_DFF
        if Clk'event and Clk = '1' then  -- rising clock edge
          if Reset then
            GPI_Interrupt <= '0';
          elsif (C_GPI_INTERRUPT = 1 and (GPI_Sampled /= GPI))              or    -- Both
                (C_GPI_INTERRUPT = 2 and (not GPI_Sampled and GPI) /= Zero) or    -- Rising
                (C_GPI_INTERRUPT = 3 and (GPI_Sampled and not GPI) /= Zero) then  -- Falling
            GPI_Interrupt <= '1';
          else
            GPI_Interrupt <= '0';
          end if;
        end if;
      end process GPI_Interrupt_DFF;

    end generate Use_GPI_Interrupt;

    No_GPI_Interrupt : if (C_GPI_INTERRUPT = 0) generate
    begin
      GPI_Interrupt <= '0';
    end generate No_GPI_Interrupt;

  end generate Using_GPI;

  No_GPI : if (C_USE_GPI = 0) generate
  begin
    GPI_In        <= (others => '0');
    GPI_Interrupt <= '0';
  end generate No_GPI;
  
end architecture IMP;


-------------------------------------------------------------------------------
-- gpo_module.vhd - Entity and architecture
-------------------------------------------------------------------------------
--
-- (c) Copyright 2011,2016 Xilinx, Inc. All rights reserved.
--
-- This file contains confidential and proprietary information
-- of Xilinx, Inc. and is protected under U.S. and 
-- international copyright and other intellectual property
-- laws.
--
-- DISCLAIMER
-- This disclaimer is not a license and does not grant any
-- rights to the materials distributed herewith. Except as
-- otherwise provided in a valid license issued to you by
-- Xilinx, and to the maximum extent permitted by applicable
-- law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND
-- WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES
-- AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
-- BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-
-- INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and
-- (2) Xilinx shall not be liable (whether in contract or tort,
-- including negligence, or under any other theory of
-- liability) for any loss or damage of any kind or nature
-- related to, arising under or in connection with these
-- materials, including for any direct, or any indirect,
-- special, incidental, or consequential loss or damage
-- (including loss of data, profits, goodwill, or any type of
-- loss or damage suffered as a result of any action brought
-- by a third party) even if such damage or loss was
-- reasonably foreseeable or Xilinx had been advised of the
-- possibility of the same.
--
-- CRITICAL APPLICATIONS
-- Xilinx products are not designed or intended to be fail-
-- safe, or for use in any application requiring fail-safe
-- performance, such as life-support or safety devices or
-- systems, Class III medical devices, nuclear facilities,
-- applications related to the deployment of airbags, or any
-- other applications that could lead to death, personal
-- injury, or severe property or environmental damage
-- (individually and collectively, "Critical
-- Applications"). Customer assumes the sole risk and
-- liability of any use of Xilinx products in Critical
-- Applications, subject only to applicable laws and
-- regulations governing limitations on product liability.
--
-- THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
-- PART OF THIS FILE AT ALL TIMES.
--
-------------------------------------------------------------------------------
-- Filename:        gpo_module.vhd
--
-- Description:     
--                  
-- VHDL-Standard:   VHDL'93/02
-------------------------------------------------------------------------------
-- Structure:   
--              gpo_module.vhd
--
-------------------------------------------------------------------------------
-- Author:          goran
--
-- History:
--   goran  2007-12-19    First Version
--
-------------------------------------------------------------------------------
-- Naming Conventions:
--      active low signals:                     "*_n"
--      clock signals:                          "clk", "clk_div#", "clk_#x" 
--      reset signals:                          "rst", "rst_n" 
--      generics:                               "C_*" 
--      user defined types:                     "*_TYPE" 
--      state machine next state:               "*_ns" 
--      state machine current state:            "*_cs" 
--      combinatorial signals:                  "*_com" 
--      pipelined or register delay signals:    "*_d#" 
--      counter signals:                        "*cnt*"
--      clock enable signals:                   "*_ce" 
--      internal version of output port         "*_i"
--      device pins:                            "*_pin" 
--      ports:                                  - Names begin with Uppercase 
--      processes:                              "*_PROCESS" 
--      component instantiations:               "<ENTITY_>I_<#|FUNC>
-------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;

entity GPO_Module is
  
  generic (
    C_TMR             : integer := 0;
    C_USE_TMR_DISABLE : integer := 0;
    C_VOTE_SIZE       : integer := 0;
    C_USE_GPO         : integer := 1;
    C_GPO_SIZE        : integer range 1 to 32 := 32;
    C_GPO_INIT        : std_logic_vector(31 downto 0) := (others => '0'));
  port (
    Clk         : in  std_logic;
    Reset       : in  boolean;
    GPO_Write   : in  std_logic;
    Write_Data  : in  std_logic_vector(31 downto 0);
    TMR_Disable : in  std_logic;
    FromAVote   : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
    FromBVote   : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
    ToVote      : out std_logic_vector(C_VOTE_SIZE-1 downto 0);
    GPO         : out std_logic_vector(C_GPO_SIZE-1 downto 0));    
end entity GPO_Module;

library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_vote_pkg.all;

architecture IMP of GPO_Module is

  signal gpo_io_i : std_logic_vector(C_GPO_SIZE-1 downto 0);
  
begin  -- architecture IMP

  TMR_No : if (C_TMR = 0 and C_USE_GPO /= 0) generate
  begin

      GPO_DFF : process (Clk) is
      begin  -- process GPO_DFF
        if Clk'event and Clk = '1' then  -- rising clock edge
          if Reset then                  -- synchronous reset (active high)
            gpo_io_i <= C_GPO_INIT(gpo_io_i'range);
          elsif (GPO_Write = '1') then
            gpo_io_i <= Write_Data(gpo_io_i'range);
          end if;
        end if;
      end process GPO_DFF;
      
    ToVote <= (others => '0');

  end generate TMR_No;

  TMR_Yes : if (C_TMR /= 0 and C_USE_GPO /= 0) generate
    signal tmr_disable_b : boolean;
    signal gpo_io_i_d    : std_logic_vector(C_GPO_SIZE-1 downto 0);
  begin
    
    tmr_disable_b <= TMR_Disable = '1' and C_USE_TMR_DISABLE = 1;

     GPO_logic : process (Reset, GPO_Write, Write_Data, gpo_io_i) is
    begin 
      if Reset then
        gpo_io_i_d <= C_GPO_INIT(gpo_io_i'range);
      elsif (GPO_Write = '1') then
        gpo_io_i_d <= Write_Data(gpo_io_i'range);
      else
        gpo_io_i_d <= gpo_io_i;
      end if;
    end process GPO_Logic;

    ToVote(gpo_io_i_d'range)                          <= gpo_io_i_d;
    ToVote(C_MAX_GPO_SIZE-1 downto gpo_io_i_d'high+1) <= (others => '0');
    
    GPO_DFF : process (Clk) is
    begin
      if Clk'event and Clk = '1' then
        gpo_io_i <= vote(gpo_io_i_d, FromAVote(gpo_io_i_d'range), FromBVote(gpo_io_i_d'range),tmr_disable_b); 
      end if;
    end process GPO_DFF;

  end generate TMR_Yes;
  
  Empty : if (C_USE_GPO = 0) generate
  begin
    gpo_io_i <= (others => '0');
    ToVote   <= (others => '0');
  end generate Empty;

  GPO <= gpo_io_i;
end architecture IMP;


-------------------------------------------------------------------------------
-- intr_ctrl.vhd - Entity and architecture
-------------------------------------------------------------------------------
--
-- (c) Copyright 2011-2012,2016,2018 Xilinx, Inc. All rights reserved.
--
-- This file contains confidential and proprietary information
-- of Xilinx, Inc. and is protected under U.S. and
-- international copyright and other intellectual property
-- laws.
--
-- DISCLAIMER
-- This disclaimer is not a license and does not grant any
-- rights to the materials distributed herewith. Except as
-- otherwise provided in a valid license issued to you by
-- Xilinx, and to the maximum extent permitted by applicable
-- law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND
-- WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES
-- AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
-- BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-
-- INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and
-- (2) Xilinx shall not be liable (whether in contract or tort,
-- including negligence, or under any other theory of
-- liability) for any loss or damage of any kind or nature
-- related to, arising under or in connection with these
-- materials, including for any direct, or any indirect,
-- special, incidental, or consequential loss or damage
-- (including loss of data, profits, goodwill, or any type of
-- loss or damage suffered as a result of any action brought
-- by a third party) even if such damage or loss was
-- reasonably foreseeable or Xilinx had been advised of the
-- possibility of the same.
--
-- CRITICAL APPLICATIONS
-- Xilinx products are not designed or intended to be fail-
-- safe, or for use in any application requiring fail-safe
-- performance, such as life-support or safety devices or
-- systems, Class III medical devices, nuclear facilities,
-- applications related to the deployment of airbags, or any
-- other applications that could lead to death, personal
-- injury, or severe property or environmental damage
-- (individually and collectively, "Critical
-- Applications"). Customer assumes the sole risk and
-- liability of any use of Xilinx products in Critical
-- Applications, subject only to applicable laws and
-- regulations governing limitations on product liability.
--
-- THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
-- PART OF THIS FILE AT ALL TIMES.
--
-------------------------------------------------------------------------------
-- Filename:        intr_ctrl.vhd
--
-- Description:
--
-- VHDL-Standard:   VHDL'93/02
-------------------------------------------------------------------------------
-- Structure:
--              intr_ctrl.vhd
--
-------------------------------------------------------------------------------
-- Author:          goran
--
-- History:
--   goran  2008-01-08    First Version
--   stefan 2011-12-28    Added Fast Interrupt
--
-------------------------------------------------------------------------------
-- Naming Conventions:
--      active low signals:                     "*_n"
--      clock signals:                          "clk", "clk_div#", "clk_#x"
--      reset signals:                          "rst", "rst_n"
--      generics:                               "C_*"
--      user defined types:                     "*_TYPE"
--      state machine next state:               "*_ns"
--      state machine current state:            "*_cs"
--      combinatorial signals:                  "*_com"
--      pipelined or register delay signals:    "*_d#"
--      counter signals:                        "*cnt*"
--      clock enable signals:                   "*_ce"
--      internal version of output port         "*_i"
--      device pins:                            "*_pin"
--      ports:                                  - Names begin with Uppercase
--      processes:                              "*_PROCESS"
--      component instantiations:               "<ENTITY_>I_<#|FUNC>
-------------------------------------------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_funcs.all;

entity intr_ctrl is

  generic (
    C_TARGET            : TARGET_FAMILY_TYPE;
    C_TMR               : integer := 0;
    C_USE_TMR_DISABLE   : integer := 0;
    C_VOTE_SIZE         : integer := 0;
    C_USE_COMB_MUX      : integer := 0;
    C_ADDR_WIDTH        : integer range 32 to 64 := 32;
    C_INTC_ENABLED      : std_logic_vector(31 downto 0);
    C_INTC_LEVEL_EDGE   : std_logic_vector(31 downto 0);
    C_INTC_POSITIVE     : std_logic_vector(31 downto 0);
    C_INTC_ASYNC_INTR   : std_logic_vector(31 downto 0);
    C_INTC_HAS_FAST     : integer range 0 to 1  := 0;
    C_INTC_ADDR_WIDTH   : integer range 5 to 64 := 32;
    C_INTC_NUM_SYNC_FF  : integer range 0 to 7  := 2;
    C_INTC_BASE_VECTORS : std_logic_vector(63 downto 0);
    C_USE_LUTRAM        : string);
  port (
    Clk               : in  std_logic;
    Reset             : in  boolean;
    TMR_Disable       : in  std_logic;
    FromAVote         : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
    FromBVote         : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
    ToVote            : out std_logic_vector(C_VOTE_SIZE-1 downto 0);
    INTR              : in  std_logic_vector(31 downto 0);
    INTR_ACK          : in  std_logic_vector(1 downto 0);
    INTR_ADDR         : out std_logic_vector(C_ADDR_WIDTH-1 downto 0);
    INTC_WRITE_CIAR   : in  std_logic;
    INTC_WRITE_CIER   : in  std_logic;
    INTC_WRITE_CIMR   : in  std_logic;
    INTC_WRITE_CIVAR  : in  std_logic;
    INTC_WRITE_CIVEAR : in  std_logic;
    INTC_CIVAR_ADDR   : in  std_logic_vector(4 downto 0);
    Write_Data        : in  std_logic_vector(31 downto 0);
    INTC_READ_CISR    : in  std_logic;
    INTC_READ_CIPR    : in  std_logic;
    INTC_IRQ          : out std_logic;
    INTC_CISR         : out std_logic_vector(31 downto 0);
    INTC_CIPR         : out std_logic_vector(31 downto 0));
end entity intr_ctrl;

library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_vote_pkg.all;
use iomodule_v3_1_5.mb_sync_bit;

architecture IMP of intr_ctrl is

  component MB_FDR is
    generic (
      C_TARGET : TARGET_FAMILY_TYPE;
      INIT : bit := '0');
    port(
      Q  : out std_logic;
      C  : in  std_logic;
      D  : in  std_logic;
      R  : in  std_logic);
  end component MB_FDR;

  component mb_sync_bit is
  generic(
    C_LEVELS            : natural   := 2;
    C_RESET_VALUE       : std_logic := '0';
    C_RESET_SYNCHRONOUS : boolean   := true;
    C_RESET_ACTIVE_HIGH : boolean   := true);
  port(
    Clk            : in  std_logic;
    Rst            : in  std_logic;
    Scan_En        : in  std_logic;
    Scan_Reset_Sel : in  std_logic;
    Scan_Reset     : in  std_logic;
    Raw            : in  std_logic;
    Synced         : out std_logic);
  end component mb_sync_bit;

  constant C_ENABLED_NONE : boolean := (C_INTC_ENABLED = (31 downto 0  => '0'));
  constant C_ENABLED_MSH  : boolean := (C_INTC_ENABLED(31 downto 16) /= X"0000");
  constant C_CIVAR_WIDTH  : integer := Boolean'Pos(C_ENABLED_MSH) + 4;
  constant C_CIVAR_SIZE   : integer := 2 ** C_CIVAR_WIDTH;

  constant C_BASE_VECTORS : std_logic_vector(63 downto 0) :=
    (C_INTC_BASE_VECTORS and X"FFFFFFFFFFFFFF80") or X"0000000000000010";
  constant C_DEFAULT_ADDR : std_logic_vector(C_ADDR_WIDTH - 1 downto 0) :=
    C_BASE_VECTORS(C_ADDR_WIDTH - 1 downto 0);

  constant USE_LUTRAM : boolean := C_USE_LUTRAM = "yes";

  signal interrupt    : std_logic_vector(31 downto 0);
  signal intr_present : std_logic_vector(31 downto 0);
  signal cisr         : std_logic_vector(31 downto 0);
  signal cier         : std_logic_vector(31 downto 0);
  signal cipr         : std_logic_vector(31 downto 0);
  signal rst_cipr_rd  : std_logic;
  signal rst_cipr_rd_voted : std_logic;
  signal civr         : std_logic_vector(4 downto 0);
  signal fast_ack     : std_logic_vector(31 downto 0);
  signal cimr         : std_logic_vector(31 downto 0);

  signal tmr_disable_b : boolean;

begin

  tmr_disable_b <= TMR_Disable = '1' and C_USE_TMR_DISABLE = 1 and C_TMR = 1;

  All_INTR_Bits : for I in 31 downto 0 generate
  begin

    Using_Intr : if (C_INTC_ENABLED(I) = '1') generate
    begin

      -- Clean the interrupt signals
      -- All internal sources are considered clean and only external needs to be registred once
      Ext_Intr : if (I > 15) generate
        constant C_RESET_VALUE : std_logic :=  not C_INTC_POSITIVE(I);
        signal synced_intr : std_logic;
      begin

        -- Synchronize the interrupt signals
        Async_Gen : if C_INTC_ASYNC_INTR(I) = '1' generate
          signal reset_std : std_logic;
        begin

          reset_std <= '1' when reset else '0';
          
          sync_bit_i : mb_sync_bit
          generic map(
            C_LEVELS            => C_INTC_NUM_SYNC_FF,
            C_RESET_VALUE       => C_RESET_VALUE,
            C_RESET_SYNCHRONOUS => true,
            C_RESET_ACTIVE_HIGH => true)
          port map(
            Clk            => Clk,
            Rst            => reset_std,
            Scan_En        => '0',
            Scan_Reset_Sel => '0',
            Scan_Reset     => '0',
            Raw            => INTR(I),
            Synced         => synced_intr);
          
        end generate Async_Gen;

        Sync_Gen: if C_INTC_ASYNC_INTR(I) = '0' generate
        begin
          synced_intr <= INTR(i);
        end generate Sync_Gen;

        Clean_TMR_No : if (C_TMR = 0) generate
        begin

          Clean_Signal : process (Clk) is
          begin  -- process Clean_Signal
            if Clk'event and Clk = '1' then  -- rising clock edge
              if Reset then                  -- synchronous reset (active high)
                interrupt(I) <= not C_INTC_POSITIVE(I);
              else
                interrupt(I) <= synced_intr;
              end if;
            end if;
          end process Clean_Signal;

          ToVote(IRQ_INTERRUPT_Pos'low + I) <= '0';

        end generate Clean_TMR_No;

        Clean_TMR_Yes : if (C_TMR /= 0) generate
          signal interrupt_d : std_logic;
        begin

          Clean_Signal : process (Reset,synced_intr) is
          begin
            if Reset then
              interrupt_d <= not C_INTC_POSITIVE(I);
            else
              interrupt_d <= synced_intr;
            end if;
          end process Clean_Signal;

          ToVote(IRQ_INTERRUPT_Pos'low + I) <= interrupt_d;

          Clean_Signal_DFF : process (Clk) is
          begin
            if Clk'event and Clk = '1' then
              interrupt(I) <= vote(interrupt_d,
                                   FromAVote(IRQ_INTERRUPT_Pos'low+I),
                                   FromBVote(IRQ_INTERRUPT_Pos'low+I), tmr_disable_b);
            end if;
          end process Clean_Signal_DFF;

        end generate Clean_TMR_Yes;

        -- Detect External Interrupt
        Level : if (C_INTC_LEVEL_EDGE(I) = '0') generate
        begin
          intr_present(I) <= interrupt(I) xnor C_INTC_POSITIVE(I);
        end generate Level;

        Edge_TMR_No : if (C_TMR = 0) generate
        begin

          Edge : if (C_INTC_LEVEL_EDGE(I) = '1') generate
          begin
            Reg_INTR : process (Clk) is
              variable s1 : std_logic;
            begin  -- process Reg_INTR
              if Clk'event and Clk = '1' then  -- rising clock edge
                if Reset then                  -- synchronous reset (active high)
                  intr_present(I) <= '0';
                  s1              := not C_INTC_POSITIVE(I);
                else
                  intr_present(I) <= '0';
                  if (C_INTC_POSITIVE(I) = '0') and (s1 = '1') and (interrupt(I) = '0') then
                    intr_present(I) <= '1';
                  end if;
                  if (C_INTC_POSITIVE(I) = '1') and (s1 = '0') and (interrupt(I) = '1') then
                    intr_present(I) <= '1';
                  end if;
                  s1 := interrupt(I);
                end if;
              end if;
            end process Reg_INTR;
          end generate Edge;

          ToVote(IRQ_INTR_PRESENT_Pos'low + I) <= '0';

        end generate Edge_TMR_No;

        Edge_TMR_Yes : if (C_TMR /= 0) generate
        begin

          Edge : if (C_INTC_LEVEL_EDGE(I) = '1') generate
            signal intr_present_d : std_logic;
            signal s1   : std_logic;
          begin

            Reg_INTR_Logic : process (Reset, s1, interrupt) is
            begin
              if Reset then
                intr_present_d <= '0';
              else
                if ((C_INTC_POSITIVE(I) = '0') and (s1 = '1') and (interrupt(I) = '0')) or
                   ((C_INTC_POSITIVE(I) = '1') and (s1 = '0') and (interrupt(I) = '1')) then
                  intr_present_d <= '1';
                else
                  intr_present_d <= '0';
                end if;
              end if;
            end process Reg_INTR_Logic;

            ToVote(IRQ_INTR_PRESENT_Pos'low + I) <= intr_present_d;

            Reg_INTR_DFF : process (Clk) is
            begin  -- process Reg_INTR
              if Clk'event and Clk = '1' then  -- rising clock edge
                if (Reset) then
                  s1 <= not C_INTC_POSITIVE(I);
                else
                  s1 <= interrupt(I);   -- no need to vote s1 as it follows interrupt which is voted
                end if;
                intr_present(I) <= vote(intr_present_d,
                                        FromAVote(IRQ_INTR_PRESENT_Pos'low+I),
                                        FromBVote(IRQ_INTR_PRESENT_Pos'low+I), tmr_disable_b);
              end if;
            end process Reg_INTR_DFF;

          end generate Edge;

          Level : if (C_INTC_LEVEL_EDGE(I) = '0') generate
          begin
            ToVote(IRQ_INTR_PRESENT_Pos'low + I) <= '0';
          end generate Level;

        end generate Edge_TMR_Yes;

      end generate Ext_Intr;

      Internal_Intr : if (I < 16) generate
      begin
        -- Internal source is always one-clock long and active high, no need to detect an edge
        intr_present(I)                       <= INTR(I);
        interrupt(I)                          <= '0'; -- Unused
        ToVote(IRQ_INTR_PRESENT_Pos'low + I)  <= '0'; -- no need to vote as it follows INTR
        ToVote(IRQ_INTERRUPT_Pos'low + I)     <= '0'; -- no need to vote as it is not used
      end generate Internal_Intr;

      CISR_CIER_TMR_No : if (C_TMR = 0) generate
      begin

        CISR_Reg : process (Clk) is
        begin  -- process CISR_Reg
          if Clk'event and Clk = '1' then  -- rising clock edge
            if Reset then                  -- synchronous reset (active high)
              cisr(I) <= '0';
            else
              if (intr_present(I) = '1') then
                cisr(I) <= '1';
              elsif (INTC_WRITE_CIAR = '1' and Write_Data(I) = '1') or (fast_ack(I) = '1') then
                cisr(I) <= '0';
              end if;
            end if;
          end if;
        end process CISR_Reg;

        CIER_Reg : process (Clk) is
        begin  -- process CIER_Reg
          if Clk'event and Clk = '1' then  -- rising clock edge
            if Reset then                  -- synchronous reset (active high)
              cier(I) <= '0';
            elsif (INTC_WRITE_CIER = '1') then
              cier(I) <= Write_Data(I);
            end if;
          end if;
        end process CIER_Reg;

        ToVote(IRQ_CISR_Pos'low + I) <= '0';
        ToVote(IRQ_CIER_Pos'low + I) <= '0';

      end generate CISR_CIER_TMR_No;

      CISR_CIER_TMR_Yes : if (C_TMR /= 0) generate
        signal cisr_d : std_logic;
        signal cier_d : std_logic;
      begin

        CISR_Logic : process (Reset, intr_present, INTC_WRITE_CIAR, Write_Data, fast_ack, cisr) is
        begin
          if Reset then
            cisr_d <= '0';
          else
            if (intr_present(I) = '1') then
              cisr_d <= '1';
            elsif (INTC_WRITE_CIAR = '1' and Write_Data(I) = '1') or (fast_ack(I) = '1') then
              cisr_d <= '0';
            else
              cisr_d <= cisr(I);
            end if;
          end if;
        end process CISR_Logic;

        ToVote(IRQ_CISR_Pos'low + I) <= cisr_d;

        CISR_DFF : process (Clk) is
        begin  -- process CISR_Reg
          if Clk'event and Clk = '1' then  -- rising clock edge
            cisr(I) <= vote(cisr_d, FromAVote(IRQ_CISR_Pos'low + I), FromBVote(IRQ_CISR_Pos'low + I), tmr_disable_b);
          end if;
        end process CISR_DFF;

        CIER_Reg : process (Reset, INTC_WRITE_CIER, Write_Data, cier) is
        begin
          if Reset then
            cier_d <= '0';
          elsif (INTC_WRITE_CIER = '1') then
            cier_d <= Write_Data(I);
          else
            cier_d <= cier(I);
          end if;
        end process CIER_Reg;

        ToVote(IRQ_CIER_Pos'low + I) <= cier_d;

        CIER_DFF : process (Clk) is
        begin  -- process CIER_Reg
          if Clk'event and Clk = '1' then  -- rising clock edge
            cier(I) <= vote(cier_d, FromAVote(IRQ_CIER_Pos'low + I), FromBVote(IRQ_CIER_Pos'low + I), tmr_disable_b);
          end if;
        end process CIER_DFF;

      end generate CISR_CIER_TMR_Yes;

      cipr(I) <= cisr(I) and cier(I);

    end generate Using_Intr;

    Not_Using_Intr : if (C_INTC_ENABLED(I) = '0') generate
    begin
      interrupt(I)                         <= '0';
      intr_present(I)                      <= '0';
      cier(I)                              <= '0';
      cisr(I)                              <= '0';
      cipr(I)                              <= '0';
      ToVote(IRQ_INTERRUPT_Pos'low    + I) <= '0';
      ToVote(IRQ_INTR_PRESENT_Pos'low + I) <= '0';
      ToVote(IRQ_CISR_Pos'low         + I) <= '0';
      ToVote(IRQ_CIER_Pos'low         + I) <= '0';
    end generate Not_Using_Intr;

    Using_CIMR : if (C_INTC_ENABLED(I) = '1') and (C_INTC_HAS_FAST = 1) generate
    begin

      CIMR_TMR_No : if (C_TMR = 0) generate
      begin

        CIMR_Reg : process (Clk) is
        begin  -- process CIMR_Reg
          if Clk'event and Clk = '1' then  -- rising clock edge
            if Reset then                  -- synchronous reset (active high)
              cimr(I) <= '0';
            elsif (INTC_WRITE_CIMR = '1') then
              cimr(I) <= Write_Data(I);
            end if;
          end if;
        end process CIMR_Reg;

        ToVote(IRQ_CIMR_Pos'low + I) <= '0';

      end generate CIMR_TMR_No;

      CIMR_TMR_Yes: if (C_TMR /= 0) generate
        signal cimr_d : std_logic;
      begin

        CIMR_Logic : process (Reset, cimr, INTC_WRITE_CIMR, Write_Data) is
        begin
          if Reset then
            cimr_d <= '0';
          elsif (INTC_WRITE_CIMR = '1') then
            cimr_d <= Write_Data(I);
          else
            cimr_d <= cimr(I);
          end if;
        end process CIMR_Logic;

        ToVote(IRQ_CIMR_Pos'low + I) <= cimr_d;

        CIMR_DFF : process (Clk) is
        begin  -- process CIMR_Reg
          if Clk'event and Clk = '1' then  -- rising clock edge
            cimr(I) <= vote(cimr_d, FromAVote(IRQ_CIMR_Pos'low + I), FromBVote(IRQ_CIMR_Pos'low + I), tmr_disable_b);
          end if;
        end process CIMR_DFF;

      end generate CIMR_TMR_Yes;

    end generate Using_CIMR;

    Not_Using_CIMR : if (C_INTC_ENABLED(I) = '0') or (C_INTC_HAS_FAST = 0) generate
    begin
      cimr(I) <= '0';
      ToVote(IRQ_CIMR_Pos'low + I) <= '0';
    end generate Not_Using_CIMR;

  end generate All_INTR_Bits;

  Using_Fast : if C_INTC_HAS_FAST = 1 and not C_ENABLED_NONE generate
    subtype byte_res_vec is std_logic_vector(2 downto 0);
    type byte_res_array is array (3 downto 0) of byte_res_vec;

    subtype mux_res_vec is std_logic_vector(4 downto 0);
    type mux_res_array is array (4 downto 0) of mux_res_vec;

    type civar_type is array (C_CIVAR_SIZE - 1 downto 0) of std_logic_vector(C_INTC_ADDR_WIDTH - 3 downto 0);

    constant Idle         : std_logic_vector(1 downto 0) := "00";
    constant Interrupting : std_logic_vector(1 downto 0) := "01";
    constant Handling     : std_logic_vector(1 downto 0) := "10";
    constant Acknowledge  : std_logic_vector(1 downto 0) := "11";

    signal byte_zeros       : std_logic_vector(3 downto 0);
    signal byte_res         : byte_res_array;
    signal mux_res          : mux_res_array;
    signal fast_state       : std_logic_vector(1 downto 0);
    signal do_fast_ack      : std_logic;
    signal civar            : civar_type := (others => C_DEFAULT_ADDR(C_INTC_ADDR_WIDTH - 1 downto 2));
    signal civar_read_addr  : std_logic_vector(C_CIVAR_WIDTH - 1 downto 0);
    signal civar_write_addr : std_logic_vector(C_CIVAR_WIDTH - 1 downto 0);
    signal intr_addr_i      : std_logic_vector(C_INTC_ADDR_WIDTH - 3 downto 0);
    signal early_ack        : std_logic;
    signal has_fast         : std_logic;

    attribute ram_style            : string;
    attribute ram_style of civar   : signal is "distributed";
    attribute ram_extract          : string;
    attribute ram_extract of civar : signal is C_USE_LUTRAM;

  begin

    -- Calculate first bit set to get highest priority interrupt number (civr)
    Calc_Byte_Res: for I in 0 to 3 generate
    begin
      byte_zeros(I)  <= '1' when cipr(8*I+7 downto 8*I) = "00000000" else '0';

      byte_res(I)(2) <= '1' when cipr(8*I+3 downto 8*I) = "0000" else '0';

      byte_res(I)(1) <= '0' when cipr(8*I+0) = '1' or cipr(8*I+1) = '1' else
                        '1' when cipr(8*I+2) = '1' or cipr(8*I+3) = '1' else
                        '0' when cipr(8*I+4) = '1' or cipr(8*I+5) = '1' else
                        '1';

      byte_res(I)(0) <= '0' when cipr(8*I+0)= '1' else
                        '1' when cipr(8*I+1)= '1' else
                        '0' when cipr(8*I+2)= '1' else
                        '1' when cipr(8*I+3)= '1' else
                        '0' when cipr(8*I+4)= '1' else
                        '1' when cipr(8*I+5)= '1' else
                        '0' when cipr(8*I+6)= '1' else
                        '1';
    end generate Calc_Byte_Res;

    mux_res(4) <= "00000";
    Mux_the_Results: for I in natural range 3 downto 0 generate
    begin
      mux_res(I) <= mux_res(I+1) when byte_zeros(I) = '1' else
                    std_logic_vector(to_unsigned(I,2)) & byte_res(I);
    end generate Mux_the_Results;

    Fast_FSM_TMR_No : if (C_TMR = 0) generate
    begin

      -- Handle interrupt occurrence and acknowledge
      Fast_FSM : process(Clk)
      begin
        if Clk'event and Clk = '1' then
          if Reset then                   -- synchronous reset (active high)
            fast_state  <= Idle;
            INTC_IRQ    <= '0';
            civr        <= (others => '0');
            do_fast_ack <= '0';
          else
            case fast_state is
              when Idle =>                -- wait for interrupt
                if byte_zeros /= "1111" then
                  fast_state <= Interrupting;
                end if;
                INTC_IRQ    <= '0';
                civr        <= mux_res(0);
                do_fast_ack <= '0';
              when Interrupting =>        -- wait for first ack
                if INTR_ACK = "01" then
                  fast_state  <= Handling;
                  INTC_IRQ    <= '0';
                  do_fast_ack <= early_ack and has_fast;
                else
                  INTC_IRQ    <= '1';
                  do_fast_ack <= '0';
                end if;
              when Handling =>            -- wait for second ack
                if INTR_ACK(1) = '1' then
                  fast_state  <= Acknowledge;
                  do_fast_ack <= not early_ack and has_fast;
                else
                  do_fast_ack <= '0';
                end if;
                INTC_IRQ <= '0';
              when Acknowledge =>         -- wait until acknowledged in cisr
                fast_state  <= Idle;
                do_fast_ack <= '0';
                INTC_IRQ    <= '0';
              when others =>
                null;
            end case;
          end if;
        end if;
      end process Fast_FSM;

      ToVote(IRQ_FAST_STATE_Pos)  <= (others => '0');
      ToVote(IRQ_INTC_IRQ_Pos)    <= '0';
      ToVote(IRQ_CIVR_Pos)        <= (others => '0');
      ToVote(IRQ_DO_FAST_ACK_Pos) <= '0';

    end generate Fast_FSM_TMR_No;

    Fast_FSM_TMR_Yes : if (C_TMR /= 0) generate
      signal fast_state_d  : std_logic_vector(1 downto 0);
      signal INTC_IRQ_d    : std_logic;
      signal civr_d        : std_logic_vector(4 downto 0);
      signal do_fast_ack_d : std_logic;
    begin
      -- Handle interrupt occurrence and acknowledge
      Fast_FSM_Logic : process(Reset, fast_state, civr, do_fast_ack,
                               byte_zeros, mux_res, early_ack, has_fast, INTR_ACK)
      begin
        if Reset then
          fast_state_d  <= Idle;
          INTC_IRQ_d    <= '0';
          civr_d        <= (others => '0');
          do_fast_ack_d <= '0';
        else
          case fast_state is
            when Idle =>                -- wait for interrupt
              if byte_zeros /= "1111" then
                fast_state_d <= Interrupting;
              else
                fast_state_d <= fast_state;
              end if;
              INTC_IRQ_d    <= '0';
              civr_d        <= mux_res(0);
              do_fast_ack_d <= '0';
            when Interrupting =>        -- wait for first ack
              if INTR_ACK = "01" then
                fast_state_d  <= Handling;
                INTC_IRQ_d    <= '0';
                do_fast_ack_d <= early_ack and has_fast;
              else
                fast_state_d  <= fast_state;
                INTC_IRQ_d    <= '1';
                do_fast_ack_d <= '0';
              end if;
              civr_d        <= civr;
            when Handling =>            -- wait for second ack
              if INTR_ACK(1) = '1' then
                fast_state_d  <= Acknowledge;
                do_fast_ack_d <= not early_ack and has_fast;
              else
                fast_state_d  <= fast_state;
                do_fast_ack_d <= '0';
              end if;
              civr_d     <= civr;
              INTC_IRQ_d <= '0';
            when Acknowledge =>         -- wait until acknowledged in cisr
              fast_state_d  <= Idle;
              do_fast_ack_d <= '0';
              INTC_IRQ_d    <= '0';
              civr_d        <= civr;
            when others =>
              null;
            end case;
        end if;
      end process Fast_FSM_Logic;

      ToVote(IRQ_FAST_STATE_Pos)  <= fast_state_d;
      ToVote(IRQ_INTC_IRQ_Pos)    <= INTC_IRQ_d;
      ToVote(IRQ_CIVR_Pos)        <= civr_d;
      ToVote(IRQ_DO_FAST_ACK_Pos) <= do_fast_ack_d;

      Fast_FSM_DFF : process(Clk)
      begin
        if Clk'event and Clk = '1' then
          fast_state  <= vote(fast_state_d,
                              FromAVote(IRQ_FAST_STATE_Pos),
                              FromBVote(IRQ_FAST_STATE_Pos), tmr_disable_b);
          INTC_IRQ    <= vote(INTC_IRQ_d,
                              FromAVote(IRQ_INTC_IRQ_Pos),
                              FromBVote(IRQ_INTC_IRQ_Pos), tmr_disable_b);
          civr        <= vote(civr_d,
                              FromAVote(IRQ_CIVR_Pos),
                              FromBVote(IRQ_CIVR_Pos), tmr_disable_b);
          do_fast_ack <= vote(do_fast_ack_d,
                              FromAVote(IRQ_DO_FAST_ACK_Pos),
                              FromBVote(IRQ_DO_FAST_ACK_Pos), tmr_disable_b);
        end if;
      end process Fast_FSM_DFF;

    end generate Fast_FSM_TMR_Yes;

    Fast_Ack_Assign : process(civr, do_fast_ack)
    begin
      fast_ack <= (others => '0');
      fast_ack(to_integer(unsigned(civr))) <= do_fast_ack;
    end process Fast_Ack_Assign;

    early_ack <= C_INTC_LEVEL_EDGE(to_integer(unsigned(civr)));
    has_fast  <= cimr(to_integer(unsigned(civr)));

    -- Vector address registers implemented as a LUTRAM
    civar_write_addr <= INTC_CIVAR_ADDR(C_CIVAR_WIDTH - 1 downto 0);
    civar_read_addr  <= civr(C_CIVAR_WIDTH - 1 downto 0);

    Using_EA: if C_INTC_ADDR_WIDTH > 32 generate
      signal write_data_hi : std_logic_vector(C_INTC_ADDR_WIDTH - 1 downto 32);
      signal write_data_lo : std_logic_vector(31 downto 2);
    begin
      write_data_hi  <= Write_Data(C_INTC_ADDR_WIDTH - 33 downto 0);
      write_data_lo  <= Write_Data(31 downto 2);

      Using_LUTRAM: if USE_LUTRAM generate
      begin

        civar_reg : process(Clk)
        begin
          if Clk'event and Clk = '1' then
            if (INTC_WRITE_CIVAR = '1') then
              civar(to_integer(unsigned(civar_write_addr)))(29 downto 0) <= write_data_lo;
            end if;
            if (INTC_WRITE_CIVEAR = '1') then
              civar(to_integer(unsigned(civar_write_addr)))(C_INTC_ADDR_WIDTH - 3 downto 30) <= write_data_hi;
            end if;
            intr_addr_i <= civar(to_integer(unsigned(civar_read_addr)));
          end if;
        end process civar_reg;

      end generate Using_LUTRAM;

      Not_Using_LUTRAM: if not USE_LUTRAM generate
      begin

        civar_reg : process(Clk)
        begin
          if Clk'event and Clk = '1' then
            if (Reset) then
              civar       <= (others => C_DEFAULT_ADDR(C_INTC_ADDR_WIDTH - 1 downto 2));
              intr_addr_i <= (others => '0');
            else
              if (INTC_WRITE_CIVAR = '1') then
                civar(to_integer(unsigned(civar_write_addr)))(29 downto 0) <= write_data_lo;
              end if;
              if (INTC_WRITE_CIVEAR = '1') then
                civar(to_integer(unsigned(civar_write_addr)))(C_INTC_ADDR_WIDTH - 3 downto 30) <= write_data_hi;
              end if;
              intr_addr_i <= civar(to_integer(unsigned(civar_read_addr)));
            end if;
          end if;
        end process civar_reg;

      end generate Not_Using_LUTRAM;
    end generate Using_EA;

    Not_Using_EA: if C_INTC_ADDR_WIDTH <= 32 generate
      signal write_data_i : std_logic_vector(C_INTC_ADDR_WIDTH - 3 downto 0);
    begin
      write_data_i <= Write_Data(C_INTC_ADDR_WIDTH - 1 downto 2);

      Using_LUTRAM: if USE_LUTRAM generate
      begin

        civar_reg : process(Clk)
        begin
          if Clk'event and Clk = '1' then
            if (INTC_WRITE_CIVAR = '1') then
              civar(to_integer(unsigned(civar_write_addr))) <= write_data_i;
              intr_addr_i <= civar(to_integer(unsigned(civar_read_addr)));
            else
              intr_addr_i <= civar(to_integer(unsigned(civar_read_addr)));
            end if;
          end if;
        end process civar_reg;

      end generate Using_LUTRAM;

      Not_Using_LUTRAM: if not USE_LUTRAM generate
      begin

        civar_reg : process(Clk)
        begin
          if Clk'event and Clk = '1' then
            if (Reset) then
              civar       <= (others => C_DEFAULT_ADDR(C_INTC_ADDR_WIDTH - 1 downto 2));
              intr_addr_i <= (others => '0');
            elsif (INTC_WRITE_CIVAR = '1') then
              civar(to_integer(unsigned(civar_write_addr))) <= write_data_i;
              intr_addr_i <= civar(to_integer(unsigned(civar_read_addr)));
            else
              intr_addr_i <= civar(to_integer(unsigned(civar_read_addr)));
            end if;
          end if;
        end process civar_reg;

      end generate Not_Using_LUTRAM;
    end generate Not_Using_EA;

    INTR_ADDR_Assign : process(intr_addr_i)
    begin
      INTR_ADDR <= C_DEFAULT_ADDR;
      INTR_ADDR(C_INTC_ADDR_WIDTH - 1 downto 2) <= intr_addr_i(C_INTC_ADDR_WIDTH - 3 downto 0);
    end process INTR_ADDR_Assign;

  end generate Using_Fast;

  Not_Using_Fast : if C_INTC_HAS_FAST = 0 or C_ENABLED_NONE generate
  begin
    civr                        <= (others => '0');
    fast_ack                    <= (others => '0');
    INTR_ADDR                   <= C_DEFAULT_ADDR;
    INTC_IRQ                    <= '1' when cipr /= X"00000000" else '0';
    ToVote(IRQ_FAST_STATE_Pos)  <= (others => '0');
    ToVote(IRQ_INTC_IRQ_Pos)    <= '0';
    ToVote(IRQ_CIVR_Pos)        <= (others => '0');
    ToVote(IRQ_DO_FAST_ACK_Pos) <= '0';
  end generate Not_Using_Fast;

  cisr_rd_dff : process (Clk) is
  begin  -- process cisr_rd_dff
    if Clk'event and Clk = '1' then   -- rising clock edge
      if (INTC_READ_CISR = '0' or Reset) then  -- synchronous reset (active high)
        INTC_CISR <= (others => '0');
      else
        INTC_CISR <= cisr;
      end if;
    end if;
  end process cisr_rd_dff;

  rst_cipr_rd <= not(INTC_READ_CIPR);

  ToVote(IRQ_RST_CIPR_RD_Pos) <= rst_cipr_rd;

  rst_cipr_rd_voted <= vote(rst_cipr_rd,
                            FromAVote(IRQ_RST_CIPR_RD_Pos),
                            FromBVote(IRQ_RST_CIPR_RD_Pos), tmr_disable_b, C_TMR);

  cipr_rd_dff_all : for I in 0 to 31 generate
  begin
    fdr_i : MB_FDR
      generic map (
        C_TARGET => C_TARGET)
      port map (
        Q => INTC_CIPR(I),
        C => Clk,
        D => cipr(I),
        R => rst_cipr_rd_voted);
  end generate cipr_rd_dff_all;

end architecture IMP;


-------------------------------------------------------------------------------
-- pit_module.vhd - Entity and architecture
-------------------------------------------------------------------------------
--
-- (c) Copyright 2011,2016-2017 Xilinx, Inc. All rights reserved.
--
-- This file contains confidential and proprietary information
-- of Xilinx, Inc. and is protected under U.S. and
-- international copyright and other intellectual property
-- laws.
--
-- DISCLAIMER
-- This disclaimer is not a license and does not grant any
-- rights to the materials distributed herewith. Except as
-- otherwise provided in a valid license issued to you by
-- Xilinx, and to the maximum extent permitted by applicable
-- law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND
-- WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES
-- AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
-- BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-
-- INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and
-- (2) Xilinx shall not be liable (whether in contract or tort,
-- including negligence, or under any other theory of
-- liability) for any loss or damage of any kind or nature
-- related to, arising under or in connection with these
-- materials, including for any direct, or any indirect,
-- special, incidental, or consequential loss or damage
-- (including loss of data, profits, goodwill, or any type of
-- loss or damage suffered as a result of any action brought
-- by a third party) even if such damage or loss was
-- reasonably foreseeable or Xilinx had been advised of the
-- possibility of the same.
--
-- CRITICAL APPLICATIONS
-- Xilinx products are not designed or intended to be fail-
-- safe, or for use in any application requiring fail-safe
-- performance, such as life-support or safety devices or
-- systems, Class III medical devices, nuclear facilities,
-- applications related to the deployment of airbags, or any
-- other applications that could lead to death, personal
-- injury, or severe property or environmental damage
-- (individually and collectively, "Critical
-- Applications"). Customer assumes the sole risk and
-- liability of any use of Xilinx products in Critical
-- Applications, subject only to applicable laws and
-- regulations governing limitations on product liability.
--
-- THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
-- PART OF THIS FILE AT ALL TIMES.
--
-------------------------------------------------------------------------------
-- Filename:        pit_module.vhd
--
-- Description:
--
-- VHDL-Standard:   VHDL'93/02
-------------------------------------------------------------------------------
-- Structure:
--              pit_module.vhd
--
-------------------------------------------------------------------------------
-- Author:          goran
--
-- History:
--   goran  2007-12-19    First Version
--
-------------------------------------------------------------------------------
-- Naming Conventions:
--      active low signals:                     "*_n"
--      clock signals:                          "clk", "clk_div#", "clk_#x"
--      reset signals:                          "rst", "rst_n"
--      generics:                               "C_*"
--      user defined types:                     "*_TYPE"
--      state machine next state:               "*_ns"
--      state machine current state:            "*_cs"
--      combinatorial signals:                  "*_com"
--      pipelined or register delay signals:    "*_d#"
--      counter signals:                        "*cnt*"
--      clock enable signals:                   "*_ce"
--      internal version of output port         "*_i"
--      device pins:                            "*_pin"
--      ports:                                  - Names begin with Uppercase
--      processes:                              "*_PROCESS"
--      component instantiations:               "<ENTITY_>I_<#|FUNC>
-------------------------------------------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_funcs.all;

entity PIT_Module is
  generic (
    C_TARGET          : TARGET_FAMILY_TYPE;
    C_TMR             : integer := 0;
    C_USE_TMR_DISABLE : integer := 0;
    C_VOTE_SIZE       : integer := 0;
    C_USE_PIT         : integer := 0;
    C_PIT_SIZE        : integer := 16;
    C_PIT_READABLE    : integer := 1
    );
  port (
    Clk               : in  std_logic;
    Reset             : in  boolean;
    Config_Reset      : in  std_logic;
    TMR_Disable       : in std_logic;
    FromAVote         : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
    FromBVote         : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
    ToVote            : out std_logic_vector(C_VOTE_SIZE-1 downto 0);
    PIT_Count_En      : in  std_logic;
    PIT_Write_Preload : in  std_logic;
    PIT_Write_Ctrl    : in  std_logic;
    PIT_Read          : in  std_logic;
    Write_Data        : in  std_logic_vector(31 downto 0);
    PIT_Data          : out std_logic_vector(C_PIT_SIZE-1 downto 0);
    PIT_Toggle        : out std_logic;
    PIT_Interrupt     : out std_logic);
end entity PIT_Module;

library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_vote_pkg.all;

architecture IMP of PIT_Module is

  component MB_MUXCY_XORCY is
    generic (
      C_TARGET : TARGET_FAMILY_TYPE);
    port (
      O  : out std_logic;
      LO : out std_logic;
      CI : in  std_logic;
      DI : in  std_logic;
      S  : in  std_logic);
  end component MB_MUXCY_XORCY;

  component MB_LUT3 is
    generic (
      C_TARGET : TARGET_FAMILY_TYPE;
      INIT     : bit_vector := X"00");
    port (
      O  : out std_logic;
      I0 : in  std_logic;
      I1 : in  std_logic;
      I2 : in  std_logic);
  end component MB_LUT3;

  component MB_MULT_AND is
    generic (
      C_TARGET : TARGET_FAMILY_TYPE;
      INIT : bit := '0');
    port(
      LO : out std_logic;
      I0 : in  std_logic;
      I1 : in  std_logic);
  end component MB_MULT_AND;

begin  -- architecture IMP

  Using_PIT : if (C_USE_PIT /= 0) generate
    signal preload_value   : std_logic_vector(C_PIT_SIZE-1 downto 0);
    signal preload_written : std_logic;

    signal reload   : std_logic;
    signal count_en : std_logic;

    signal count_enabled : std_logic;

    -- Counter signals
    signal count_load_n    : std_logic;
    signal cnt             : std_logic_vector(C_PIT_SIZE-1 downto 0);
    signal carry           : std_logic_vector(C_PIT_SIZE   downto 0);
    signal count           : std_logic_vector(C_PIT_SIZE-1 downto 0);
    signal count_wrap      : std_logic;
    signal pit_interrupt_i : std_logic;
    signal pit_toggle_i    : std_logic;
  begin

    TMR_No : if (C_TMR = 0) generate
    begin
      --------------------------------------------------------------------------------------------------
      -- Preload register
      --------------------------------------------------------------------------------------------------
      PreLoad_Handler : process (Clk) is
      begin  -- process PreLoad_Handler
        if Clk'event and Clk = '1' then   -- rising clock edge
          if Reset then                   -- synchronous reset (active high)
            preload_value <= (others => '0');
          elsif (PIT_Write_Preload = '1') then
            preload_value <= Write_Data(C_PIT_SIZE-1 downto 0);
          end if;
        end if;
      end process PreLoad_Handler;

      --------------------------------------------------------------------------------------------------
      -- Control Register
      --------------------------------------------------------------------------------------------------
      Ctrl_Handler : process (Clk) is
      begin  -- process Ctrl_Handler
        if Clk'event and Clk = '1' then   -- rising clock edge
          if Reset then                   -- synchronous reset (active high)
            reload          <= '0';
            count_en        <= '0';
            count_load_n    <= '1';
            preload_written <= '0';
          else
            preload_written <= PIT_Write_Preload;
            if (PIT_Write_Ctrl = '1') then
              reload   <= Write_Data(1);
              count_en <= Write_Data(0);
            end if;
            if ((count_wrap = '1') and (reload = '0') and (count_enabled = '1') and (count_load_n = '1')) then
              count_en <= '0';
            end if;

            -- reach -1 and will load_counter with preload value
            if ((count_wrap = '1') and (reload = '1') and (count_enabled = '1')) then
              count_load_n <= '0';
            end if;

            -- if we have written to preload register, load counter with preload value next time we count
            if (preload_written = '1') then
              count_load_n <= '0';
            end if;

            -- Counter is now loaded so remove count_load_n
            if (count_load_n = '0') and (count_enabled = '1') then
              count_load_n <= '1';
            end if;

          end if;
        end if;
      end process Ctrl_Handler;

      count_enabled <= count_en and PIT_Count_En;

      Counter : process (Clk) is
      begin  -- process Counter
        if Clk'event and Clk = '1' then   -- rising clock edge
          if Reset then
            count <= (others => '0');
          elsif (count_enabled = '1') then
            count <= cnt;
          end if;
        end if;
      end process Counter;

      Interrupt_Handler : process (Clk) is
      begin  -- process Interrupt_Handler
        if Clk'event and Clk = '1' then   -- rising clock edge
          if Reset then                   -- synchronous reset (active high)
            pit_interrupt_i <= '0';
          else
            pit_interrupt_i <= count_wrap and count_enabled and count_load_n;
          end if;
        end if;
      end process Interrupt_Handler;

      Toggle_Handler : process (Clk) is
      begin
        if Clk'event and Clk = '1' then
          if Reset then
            pit_toggle_i <= '0';
          elsif pit_interrupt_i = '1' then
            pit_toggle_i <= not pit_toggle_i;
          end if;
        end if;
      end process Toggle_Handler;

      ToVote <= (others => '0');

    end generate TMR_No;

    --------------------------------------------------------------------------------------------------
    -- Counter
    --------------------------------------------------------------------------------------------------
    Using_FPGA: if (C_TARGET /= RTL) generate
      signal new_cnt    : std_logic_vector(C_PIT_SIZE-1 downto 0);
      signal new_cnt_di : std_logic_vector(C_PIT_SIZE-1 downto 0);
    begin
    
      carry(0) <= '0';

      All_Bits : for I in 0 to C_PIT_SIZE - 1 generate
      begin

        Count_LUT : MB_LUT3
          generic map(
            C_TARGET => C_TARGET,
            INIT => X"72"
            )
          port map (
            O  => new_cnt(I),             -- [out]
            I0 => count_load_n,           -- [in]
            I1 => count(I),               -- [in]
            I2 => preload_value(I));      -- [in]

        MULT_AND_I : MB_MULT_AND
          generic map(
            C_TARGET => C_TARGET)
          port map (
            I0 => count_load_n,           -- [in]
            I1 => count(I),               -- [in]
            LO => new_cnt_di(I));         -- [out]

        MUXCY_XORCY_L_I1 : MB_MUXCY_XORCY
          generic map(
            C_TARGET => C_TARGET)
          port map (
            DI => new_cnt_di(I),          -- [in  std_logic S = 0]
            CI => carry(I),               -- [in  std_logic S = 1]
            S  => new_cnt(I),             -- [in  std_logic (Select)]
            LO => carry(I+1),             -- [out std_logic]
            O  => cnt(I));                -- [out std_logic]

      end generate All_Bits;

      count_wrap <= not carry(C_PIT_SIZE);

    end generate Using_FPGA;

    Using_RTL : if (C_TARGET = RTL) generate
      signal count_extra : std_logic_vector(C_PIT_SIZE downto 0);
    begin
      count_extra <= '0' & count;
      carry       <= std_logic_vector(unsigned(count_extra) - 1);
      cnt         <= carry(C_PIT_SIZE-1 downto 0) when count_load_n = '1' else
                     preload_value;
      count_wrap  <= carry(C_PIT_SIZE);
    end generate Using_RTL;

    TMR_Yes : if (C_TMR /= 0) generate
      signal tmr_disable_b     : boolean;
      signal preload_value_d   : std_logic_vector(C_PIT_SIZE-1 downto 0);
      signal count_d           : std_logic_vector(C_PIT_SIZE-1 downto 0);
      signal reload_d          : std_logic;
      signal count_en_d        : std_logic;
      signal count_load_n_d    : std_logic;
      signal preload_written_d : std_logic;
      signal pit_interrupt_i_d : std_logic;
      signal pit_toggle_i_d    : std_logic;
    begin

      tmr_disable_b <= TMR_Disable = '1' and C_USE_TMR_DISABLE = 1;

      --------------------------------------------------------------------------------------------------
      -- Preload register
      -------------------------------------------------------------------------------------------------
      PreLoad_Handler_Logic : process (Reset, PIT_Write_Preload, Write_Data, preload_value) is
      begin
        if Reset then
          preload_value_d <= (others => '0');
        elsif (PIT_Write_Preload = '1') then
          preload_value_d <= Write_Data(C_PIT_SIZE-1 downto 0);
        else
          preload_value_d <= preload_value;
        end if;
      end process PreLoad_Handler_Logic;

      ToVote(PIT_PRELOAD_VALUE_Pos'low + C_PIT_SIZE - 1 downto PIT_PRELOAD_VALUE_Pos'low) <= preload_value_d;
      -- Work around spyglass bug report on null range to the left but not to the right
      spy1_g: if C_PIT_SIZE < 32 generate
      begin
        ToVote(PIT_PRELOAD_VALUE_Pos'high downto PIT_PRELOAD_VALUE_Pos'low + C_PIT_SIZE) <= (others => '0');
      end generate spy1_g;

      PreLoad_Handler_DFF : process (Clk) is
      begin
        if Clk'event and Clk = '1' then
          preload_value <= vote(preload_value_d,
                                FromAVote(PIT_PRELOAD_VALUE_Pos),
                                FromBVote(PIT_PRELOAD_VALUE_Pos), tmr_disable_b);
        end if;
      end process PreLoad_Handler_DFF;

      --------------------------------------------------------------------------------------------------
      -- Control Register
      --------------------------------------------------------------------------------------------------
      Ctrl_Handler_Logic : process (Reset, PIT_Write_Preload, PIT_Write_Ctrl, Write_Data, count_wrap,
                                    reload, count_en, count_enabled, count_load_n, preload_written) is
      begin
        if Reset then
          reload_d          <= '0';
          count_en_d        <= '0';
          count_load_n_d    <= '1';
          preload_written_d <= '0';
        else
          if (PIT_Write_Ctrl = '1') then
            reload_d <= Write_Data(1);
          else
            reload_d <= reload;
          end if;

          if ((count_wrap = '1') and (reload = '0') and (count_enabled = '1') and (count_load_n = '1')) then
            count_en_d <= '0';
          elsif (PIT_Write_Ctrl = '1') then
            count_en_d <= Write_Data(0);
          else
            count_en_d <= count_en;
          end if;

          -- Counter is now loaded so remove count_load_n
          if (count_load_n = '0') and (count_enabled = '1') then
            count_load_n_d <= '1';
          -- if we have written to preload register, load counter with preload value next time we count
          elsif (preload_written = '1') then
            count_load_n_d <= '0';
          -- reach -1 and will load_counter with preload value
          elsif ((count_wrap = '1') and (reload = '1') and (count_enabled = '1')) then
            count_load_n_d <= '0';
          else
            count_load_n_d <= count_load_n;
          end if;

          preload_written_d <= PIT_Write_Preload;

        end if;
      end process Ctrl_Handler_Logic;

      ToVote(PIT_RELOAD_Pos)          <= reload_d;
      ToVote(PIT_COUNT_EN_Pos)        <= count_en_d;
      ToVote(PIT_COUNT_LOAD_N_Pos)    <= count_load_n_d;
      ToVote(PIT_PRELOAD_WRITTEN_Pos) <= preload_written_d;

      Ctrl_Handler_DFF : process (Clk) is
      begin
        if Clk'event and Clk = '1' then
          reload          <= vote(reload_d,
                                  FromAVote(PIT_RELOAD_Pos),
                                  FromBVote(PIT_RELOAD_Pos), tmr_disable_b);
          count_en        <= vote(count_en_d,
                                  FromAVote(PIT_COUNT_EN_Pos),
                                  FromBVote(PIT_COUNT_EN_Pos), tmr_disable_b);
          count_load_n    <= vote(count_load_n_d,
                                  FromAVote(PIT_COUNT_LOAD_N_Pos),
                                  FromBVote(PIT_COUNT_LOAD_N_Pos), tmr_disable_b);
          preload_written <= vote(preload_written_d,
                                  FromAVote(PIT_PRELOAD_WRITTEN_Pos),
                                  FromBVote(PIT_PRELOAD_WRITTEN_Pos), tmr_disable_b);
        end if;
      end process Ctrl_Handler_DFF;

      count_enabled <= count_en and PIT_Count_En;

      Counter_Logic : process (Reset, count_enabled, count, cnt) is
      begin
        if Reset then
          count_d <= (others => '0');
        elsif (count_enabled = '1') then
          count_d <= cnt;
        else
          count_d <= count;
        end if;
      end process Counter_Logic;

      ToVote(PIT_COUNT_Pos'low + C_PIT_SIZE - 1 downto PIT_COUNT_Pos'low) <= count_d;
      -- Work around spyglass bug report on null range to the left but not to the right
      spy2_g: if C_PIT_SIZE < 32 generate
      begin
        ToVote(PIT_COUNT_Pos'high downto PIT_COUNT_Pos'low + C_PIT_SIZE)    <= (others => '0');
      end generate spy2_g;

      Counter_DFF : process (Clk) is
      begin
        if Clk'event and Clk = '1' then
          count <= vote(count_d, FromAVote(PIT_COUNT_Pos), FromBVote(PIT_COUNT_Pos),tmr_disable_b);
        end if;
      end process Counter_DFF;

      Interrupt_Handler_Logic : process (Reset, count_wrap, count_enabled, count_load_n) is
      begin
        if Reset then
          pit_interrupt_i_d <= '0';
        else
          pit_interrupt_i_d <= count_wrap and count_enabled and count_load_n;
        end if;
      end process Interrupt_Handler_Logic;

      ToVote(PIT_INTERRUPT_I_Pos) <= pit_interrupt_i_d;

      Interrupt_Handler_DFF : process (Clk) is
      begin
        if Clk'event and Clk = '1' then
          pit_interrupt_i <= vote(pit_interrupt_i_d,
                                  FromAVote(PIT_INTERRUPT_I_Pos),
                                  FromBVote(PIT_INTERRUPT_I_Pos), tmr_disable_b);
        end if;
      end process Interrupt_Handler_DFF;

      Toggle_Handler_Logic : process (Reset, pit_toggle_i, pit_interrupt_i) is
      begin
        if Reset then
          pit_toggle_i_d <= '0';
        elsif pit_interrupt_i = '1' then
          pit_toggle_i_d <= not pit_toggle_i;
        else
          pit_toggle_i_d <= pit_toggle_i;
        end if;
      end process Toggle_Handler_Logic;

      ToVote(PIT_TOGGLE_I_Pos) <= pit_toggle_i_d;

      Toggle_Handler_DFF : process (Clk) is
      begin
        if Clk'event and Clk = '1' then
          pit_toggle_i <= vote(pit_toggle_i_d,
                               FromAVote(PIT_TOGGLE_I_Pos),
                               FromBVote(PIT_TOGGLE_I_Pos), tmr_disable_b);
        end if;
      end process Toggle_Handler_DFF;

    end generate TMR_Yes;

    PIT_Interrupt <= pit_interrupt_i;
    PIT_Toggle <= pit_toggle_i;

    --------------------------------------------------------------------------------------------------
    -- Read register
    --------------------------------------------------------------------------------------------------
    Readable_Counter : if (C_PIT_READABLE /= 0) generate
    begin

      PIT_Read_Handler : process (Clk) is
      begin  -- process PIT_Read_Handler
        if Clk'event and Clk = '1' then  -- rising clock edge
          if PIT_Read = '0' or Config_Reset = '1' then
            PIT_Data <= (others => '0');
          else
            PIT_Data                        <= (others => '0');
            PIT_Data(C_PIT_SIZE-1 downto 0) <= count;
          end if;
        end if;
      end process PIT_Read_Handler;

    end generate Readable_Counter;

    Dont_Read_Counter: if (C_PIT_READABLE = 0) generate
    begin
      PIT_Data <= (others => '0');
    end generate Dont_Read_Counter;

  end generate Using_PIT;

  Not_Using_Pit : if (C_USE_PIT = 0) generate
  begin
    PIT_Data      <= (others => '0');
    PIT_Interrupt <= '0';
    PIT_Toggle    <= '0';
    ToVote        <= (others => '0');
  end generate Not_Using_Pit;

end architecture IMP;


-------------------------------------------------------------------------------
-- uart_control_status.vhd - Entity and architecture
-------------------------------------------------------------------------------
--
-- (c) Copyright 2011,2016 Xilinx, Inc. All rights reserved.
--
-- This file contains confidential and proprietary information
-- of Xilinx, Inc. and is protected under U.S. and
-- international copyright and other intellectual property
-- laws.
--
-- DISCLAIMER
-- This disclaimer is not a license and does not grant any
-- rights to the materials distributed herewith. Except as
-- otherwise provided in a valid license issued to you by
-- Xilinx, and to the maximum extent permitted by applicable
-- law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND
-- WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES
-- AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
-- BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-
-- INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and
-- (2) Xilinx shall not be liable (whether in contract or tort,
-- including negligence, or under any other theory of
-- liability) for any loss or damage of any kind or nature
-- related to, arising under or in connection with these
-- materials, including for any direct, or any indirect,
-- special, incidental, or consequential loss or damage
-- (including loss of data, profits, goodwill, or any type of
-- loss or damage suffered as a result of any action brought
-- by a third party) even if such damage or loss was
-- reasonably foreseeable or Xilinx had been advised of the
-- possibility of the same.
--
-- CRITICAL APPLICATIONS
-- Xilinx products are not designed or intended to be fail-
-- safe, or for use in any application requiring fail-safe
-- performance, such as life-support or safety devices or
-- systems, Class III medical devices, nuclear facilities,
-- applications related to the deployment of airbags, or any
-- other applications that could lead to death, personal
-- injury, or severe property or environmental damage
-- (individually and collectively, "Critical
-- Applications"). Customer assumes the sole risk and
-- liability of any use of Xilinx products in Critical
-- Applications, subject only to applicable laws and
-- regulations governing limitations on product liability.
--
-- THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
-- PART OF THIS FILE AT ALL TIMES.
--
-------------------------------------------------------------------------------
-- Filename:        uart_control_status.vhd
--
-- Description:
--
-- VHDL-Standard:   VHDL'93/02
-------------------------------------------------------------------------------
-- Structure:
--              uart_control_status.vhd
--
-------------------------------------------------------------------------------
-- Author:          goran
--
-- History:
--   goran  2007-12-18    First Version
--
-------------------------------------------------------------------------------
-- Naming Conventions:
--      active low signals:                     "*_n"
--      clock signals:                          "clk", "clk_div#", "clk_#x"
--      reset signals:                          "rst", "rst_n"
--      generics:                               "C_*"
--      user defined types:                     "*_TYPE"
--      state machine next state:               "*_ns"
--      state machine current state:            "*_cs"
--      combinatorial signals:                  "*_com"
--      pipelined or register delay signals:    "*_d#"
--      counter signals:                        "*cnt*"
--      clock enable signals:                   "*_ce"
--      internal version of output port         "*_i"
--      device pins:                            "*_pin"
--      ports:                                  - Names begin with Uppercase
--      processes:                              "*_PROCESS"
--      component instantiations:               "<ENTITY_>I_<#|FUNC>
-------------------------------------------------------------------------------

library IEEE;
use IEEE.std_logic_1164.all;

library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_vote_pkg.all;

entity Uart_Control_Status is
  generic (
    C_TMR             : integer              := 0;
    C_USE_TMR_DISABLE : integer              := 0;
    C_VOTE_SIZE       : integer              := 0;
    C_USE_UART_RX     : integer              := 1;
    C_USE_UART_TX     : integer              := 1;
    C_UART_DATA_BITS  : integer range 5 to 8 := 8;
    C_UART_USE_PARITY : integer              := 0;
    C_UART_ODD_PARITY : integer              := 0
    );
  port (
    CLK   : in std_logic;
    Reset : in boolean;
    Config_Reset        : in std_logic;

    TMR_Disable         : in std_logic;
    FromAVote           : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
    FromBVote           : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
    ToVote              : out std_logic_vector(C_VOTE_SIZE-1 downto 0);

    TX_Data_Transmitted : in std_logic;
    TX_Buffer_Empty     : in std_logic;
    RX_Data_Received    : in std_logic;
    RX_Data_Exists      : in std_logic;
    RX_Frame_Error      : in std_logic;
    RX_Overrun_Error    : in std_logic;
    RX_Parity_Error     : in std_logic;

    UART_Status_Read     : in  std_logic;
    UART_Status          : out std_logic_vector(7 downto 0);
    UART_Interrupt       : out std_logic;
    UART_Rx_Interrupt    : out std_logic;
    UART_Tx_Interrupt    : out std_logic;
    UART_Error_Interrupt : out std_logic
    );

end entity Uart_Control_Status;

architecture IMP of Uart_Control_Status is

  signal parity_error  : std_logic;
  signal frame_error   : std_logic;
  signal overrun_error : std_logic;

  signal error_interrupt : std_logic;

begin  -- architecture IMP

  --------------------------------------------------------------------------------------------------
  -- Status register
  --------------------------------------------------------------------------------------------------
  UART_Status_DFF: process (Clk) is
  begin  -- process UART_Status_DFF
    if Clk'event and Clk = '1' then     -- rising clock edge
      if (UART_Status_Read = '0' or Config_Reset = '1') then  -- synchronous reset (active high)
        UART_Status <= (others => '0');
      else
        if ((C_USE_UART_RX = 0) or (C_UART_USE_PARITY = 0)) then
          UART_Status(7) <= '0';
        else
          UART_Status(7) <= parity_error;
        end if;
        if (C_USE_UART_RX = 0) then
          UART_Status(6) <= '0';
          UART_Status(5) <= '0';
        else
          UART_Status(6) <= frame_error;
          UART_Status(5) <= overrun_error;
        end if;
        UART_Status(4) <= '0';
        if (C_USE_UART_TX = 0) then
          UART_Status(3) <= '0';
        else
          UART_Status(3) <= not TX_Buffer_Empty;
        end if;
        UART_Status(2) <= '0';
        UART_Status(1) <= '0';
        if (C_USE_UART_RX = 0) then
          UART_Status(0) <= '0';
        else
          UART_Status(0) <= RX_Data_Exists;
        end if;
      end if;
    end if;
  end process UART_Status_DFF;

  --------------------------------------------------------------------------------------------------
  -- Keep track of errors
  --------------------------------------------------------------------------------------------------

  TMR_No : if (C_TMR = 0) generate
  begin

    Error_Flags : process (Clk) is
    begin  -- process Error_Flags
      if Clk'event and Clk = '1' then     -- rising clock edge
        if Reset then                     -- synchronous reset (active high)
          parity_error    <= '0';
          frame_error     <= '0';
          overrun_error   <= '0';
          error_interrupt <= '0';
        else
          error_interrupt <= '0';
          if ((C_USE_UART_RX = 0) or (UART_Status_Read = '1')) then
            parity_error  <= '0';
            frame_error   <= '0';
            overrun_error <= '0';
          end if;
          if ((C_USE_UART_RX /= 0) and (RX_Frame_Error = '1')) then
            frame_error     <= '1';
            error_interrupt <= '1';
          end if;
          if ((C_USE_UART_RX /= 0) and (RX_Overrun_Error = '1')) then
            overrun_error   <= '1';
            error_interrupt <= '1';
          end if;
          if ((C_USE_UART_RX /= 0) and (C_UART_USE_PARITY /= 0) and (RX_Parity_Error = '1')) then
            parity_error    <= '1';
            error_interrupt <= '1';
          end if;
        end if;
      end if;
    end process Error_Flags;

    ToVote <= (others => '0');

  end generate TMR_No;

  TMR_Yes : if (C_TMR /= 0) generate
    signal tmr_disable_b     : boolean;
    signal parity_error_d    : std_logic;
    signal frame_error_d     : std_logic;
    signal overrun_error_d   : std_logic;
    signal error_interrupt_d : std_logic;
  begin

    tmr_disable_b <= TMR_Disable = '1' and C_USE_TMR_DISABLE = 1;

    Error_Flags_Logic : process (Reset, parity_error, UART_Status_Read, RX_Frame_Error, RX_Overrun_Error,
                                 RX_Parity_Error, frame_error, overrun_error) is
    begin
      if Reset then
        parity_error_d    <= '0';
        frame_error_d     <= '0';
        overrun_error_d   <= '0';
        error_interrupt_d <= '0';
      else
        if ((C_USE_UART_RX /= 0) and (RX_Frame_Error = '1')) or
           ((C_USE_UART_RX /= 0) and (RX_Overrun_Error = '1')) or
           ((C_USE_UART_RX /= 0) and (C_UART_USE_PARITY /= 0) and (RX_Parity_Error = '1')) then
          error_interrupt_d <= '1';
        else
          error_interrupt_d <= '0';
        end if;

        if ((C_USE_UART_RX /= 0) and (C_UART_USE_PARITY /= 0) and (RX_Parity_Error = '1')) then
          parity_error_d <= '1';
        elsif ((C_USE_UART_RX = 0) or (UART_Status_Read = '1')) then
          parity_error_d <= '0';
        else
          parity_error_d <= parity_error;
        end if;

        if ((C_USE_UART_RX /= 0) and (RX_Frame_Error = '1')) then
          frame_error_d <= '1';
        elsif ((C_USE_UART_RX = 0) or (UART_Status_Read = '1')) then
          frame_error_d <= '0';
        else
          frame_error_d <= frame_error;
        end if;

        if ((C_USE_UART_RX /= 0) and (RX_Overrun_Error = '1')) then
          overrun_error_d <= '1';
        elsif ((C_USE_UART_RX = 0) or (UART_Status_Read = '1')) then
          overrun_error_d <= '0';
        else
          overrun_error_d <= overrun_error;
        end if;

      end if;
    end process Error_Flags_Logic;

    ToVote(PARITY_ERROR_Pos)    <= parity_error_d;
    ToVote(FRAME_ERROR_Pos)     <= frame_error_d;
    ToVote(OVERRUN_ERROR_Pos)   <= overrun_error_d;
    ToVote(ERROR_INTERRUPT_Pos) <= error_interrupt_d;

    Error_Flags_DFF : process (Clk) is
    begin  -- process Error_Flags
      if Clk'event and Clk = '1' then     -- rising clock edge
        parity_error    <= vote(parity_error_d,
                                FromAVote(PARITY_ERROR_Pos),
                                FromBVote(PARITY_ERROR_Pos), tmr_disable_b);
        frame_error     <= vote(frame_error_d,
                                FromAVote(FRAME_ERROR_Pos),
                                FromBVote(FRAME_ERROR_Pos), tmr_disable_b);
        overrun_error   <= vote(overrun_error_d,
                                FromAVote(OVERRUN_ERROR_Pos),
                                FromBVote(OVERRUN_ERROR_Pos), tmr_disable_b);
        error_interrupt <= vote(error_interrupt_d,
                                FromAVote(ERROR_INTERRUPT_Pos),
                                FromBVote(ERROR_INTERRUPT_Pos), tmr_disable_b);
      end if;
    end process Error_Flags_DFF;

  end generate TMR_Yes;

  --------------------------------------------------------------------------------------------------
  -- Interrupt generation
  --------------------------------------------------------------------------------------------------
  UART_Error_Interrupt <= error_interrupt;
  UART_Rx_Interrupt    <= RX_Data_Received;
  UART_Tx_Interrupt    <= TX_Data_Transmitted;
  UART_Interrupt       <= error_interrupt or RX_Data_Received or TX_Data_Transmitted;
end architecture IMP;


-------------------------------------------------------------------------------
-- uart_receive.vhd - Entity and architecture
-------------------------------------------------------------------------------
--
-- (c) Copyright 2011,2016 Xilinx, Inc. All rights reserved.
--
-- This file contains confidential and proprietary information
-- of Xilinx, Inc. and is protected under U.S. and
-- international copyright and other intellectual property
-- laws.
--
-- DISCLAIMER
-- This disclaimer is not a license and does not grant any
-- rights to the materials distributed herewith. Except as
-- otherwise provided in a valid license issued to you by
-- Xilinx, and to the maximum extent permitted by applicable
-- law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND
-- WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES
-- AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
-- BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-
-- INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and
-- (2) Xilinx shall not be liable (whether in contract or tort,
-- including negligence, or under any other theory of
-- liability) for any loss or damage of any kind or nature
-- related to, arising under or in connection with these
-- materials, including for any direct, or any indirect,
-- special, incidental, or consequential loss or damage
-- (including loss of data, profits, goodwill, or any type of
-- loss or damage suffered as a result of any action brought
-- by a third party) even if such damage or loss was
-- reasonably foreseeable or Xilinx had been advised of the
-- possibility of the same.
--
-- CRITICAL APPLICATIONS
-- Xilinx products are not designed or intended to be fail-
-- safe, or for use in any application requiring fail-safe
-- performance, such as life-support or safety devices or
-- systems, Class III medical devices, nuclear facilities,
-- applications related to the deployment of airbags, or any
-- other applications that could lead to death, personal
-- injury, or severe property or environmental damage
-- (individually and collectively, "Critical
-- Applications"). Customer assumes the sole risk and
-- liability of any use of Xilinx products in Critical
-- Applications, subject only to applicable laws and
-- regulations governing limitations on product liability.
--
-- THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
-- PART OF THIS FILE AT ALL TIMES.
--
-------------------------------------------------------------------------------
-- Filename:        uart_receive.vhd
--
-- Description:
--
-- VHDL-Standard:   VHDL'93/02
-------------------------------------------------------------------------------
-- Structure:
--              uart_receive.vhd
--
-------------------------------------------------------------------------------
-- Author:          goran
--
-- History:
--   goran  2007-12-18    First Version
--
-------------------------------------------------------------------------------
-- Naming Conventions:
--      active low signals:                     "*_n"
--      clock signals:                          "clk", "clk_div#", "clk_#x"
--      reset signals:                          "rst", "rst_n"
--      generics:                               "C_*"
--      user defined types:                     "*_TYPE"
--      state machine next state:               "*_ns"
--      state machine current state:            "*_cs"
--      combinatorial signals:                  "*_com"
--      pipelined or register delay signals:    "*_d#"
--      counter signals:                        "*cnt*"
--      clock enable signals:                   "*_ce"
--      internal version of output port         "*_i"
--      device pins:                            "*_pin"
--      ports:                                  - Names begin with Uppercase
--      processes:                              "*_PROCESS"
--      component instantiations:               "<ENTITY_>I_<#|FUNC>
-------------------------------------------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;
library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_funcs.all;

entity UART_Receive is
  generic (
    C_TARGET          : TARGET_FAMILY_TYPE;
    C_TMR             : integer := 0;
    C_USE_TMR_DISABLE : integer := 0;
    C_VOTE_SIZE       : integer := 0;
    C_USE_SRL16       : string;
    C_DATA_BITS       : integer range 5 to 8 := 8;
    C_USE_PARITY      : integer              := 0;
    C_ODD_PARITY      : integer              := 1
    );
  port (
    Config_Reset : in std_logic;
    Clk          : in std_logic;
    Reset        : in boolean;
    EN_16x_Baud  : in std_logic;

    RX               : in  std_logic;
    Read_RX_Data     : in  std_logic;
    TMR_Disable      : in  std_logic;
    FromAVote        : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
    FromBVote        : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
    ToVote           : out std_logic_vector(C_VOTE_SIZE-1 downto 0);
    RX_Data          : out std_logic_vector(C_DATA_BITS-1 downto 0);
    RX_Data_Received : out std_logic;
    RX_Data_Exists   : out std_logic;
    RX_Frame_Error   : out std_logic;
    RX_Overrun_Error : out std_logic;
    RX_Parity_Error  : out std_logic
    );

end entity UART_Receive;

library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_vote_pkg.all;

architecture IMP of UART_Receive is

  component XIL_SRL16E is
    generic (
      C_TARGET    : TARGET_FAMILY_TYPE;
      C_USE_SRL16 : string;
      C_STATIC    : boolean := false;
      INIT        : bit_vector);
    port (
      Config_Reset : in  std_logic;
      Q            : out std_logic;
      A0           : in  std_logic;
      A1           : in  std_logic;
      A2           : in  std_logic;
      A3           : in  std_logic;
      CE           : in  std_logic;
      CLK          : in  std_logic;
      D            : in  std_logic);
  end component XIL_SRL16E;

  component MB_FDSE is
  generic (
    C_TARGET : TARGET_FAMILY_TYPE;
    INIT : bit := '1');
  port(
    Q  : out std_logic;
    C  : in  std_logic;
    CE : in  std_logic;
    D  : in  std_logic;
    S  : in  std_logic);
  end component MB_FDSE;

  component MB_FDRE is
  generic (
    C_TARGET : TARGET_FAMILY_TYPE;
    INIT : bit := '0');
  port(
    Q  : out std_logic;
    C  : in  std_logic;
    CE : in  std_logic;
    D  : in  std_logic;
    R  : in  std_logic);
  end component MB_FDRE;

  signal previous_RX             : std_logic;
  signal start_Edge_Detected_Bit : std_logic;
  signal mid_Start_Bit           : std_logic;
  signal recycle                 : std_logic;
  signal sample_Point            : std_logic;
  signal stop_Bit_Position       : std_logic;

  function Calc_Length return integer is
  begin  -- function Calc_Length
    if (C_USE_PARITY = 1) then
      return 1 + C_DATA_BITS;
    else
      return C_DATA_BITS;
    end if;
  end function Calc_Length;

  constant SERIAL_TO_PAR_LENGTH : integer := Calc_Length;
  constant STOP_BIT_POS         : integer := SERIAL_TO_PAR_LENGTH;
  constant DATA_LSB_POS         : integer := SERIAL_TO_PAR_LENGTH;
  constant CALC_PAR_POS         : integer := SERIAL_TO_PAR_LENGTH;

  signal new_rx_data_write  : std_logic;
  signal new_rx_data        : std_logic_vector(0 to SERIAL_TO_PAR_LENGTH);
  signal serial_to_parallel : std_logic_vector(1 to SERIAL_TO_PAR_LENGTH);
  signal rx_data_exists_i   : std_logic;
  signal rx_frame_error_i   : std_logic;
  signal rx_parity_error_i  : std_logic;

  signal rx_1 : std_logic;
  signal rx_2 : std_logic;

  signal rx_data_i : std_logic_vector(C_DATA_BITS-1 downto 0);

  -- Preserve signals after synthesis for simulation UART support
  attribute KEEP : string;
  attribute KEEP of rx_frame_error_i  : signal is "TRUE";
  attribute KEEP of rx_parity_error_i : signal is "TRUE";
  attribute KEEP of new_rx_data_write : signal is "TRUE";
  attribute KEEP of new_rx_data       : signal is "TRUE";

begin  -- architecture IMP

  TMR_No : if (C_TMR = 0) generate
    signal start_Edge_Detected : boolean;
    signal running             : boolean;
    signal mid_Start_Bit_or_Config_Reset : std_logic;
  begin

    ToVote <= (others => '0');

    -----------------------------------------------------------------------------
    -- Double sample to avoid meta-stability
    -----------------------------------------------------------------------------
    RX_Sampling : process (Clk)
    begin  -- process RX_Sampling
      if Clk'event and Clk = '1' then     -- rising clock edge
        if Reset then                     -- asynchronous reset (active high)
          rx_1 <= '1';
          rx_2 <= '1';
        else
          rx_1 <= RX;
          rx_2 <= rx_1;
        end if;
      end if;
    end process RX_Sampling;

    -----------------------------------------------------------------------------
    -- Detect a falling edge on RX and start a new receiption if idle
    -----------------------------------------------------------------------------
    Prev_RX_DFF : process (Clk) is
    begin  -- process Prev_RX_DFF
      if Clk'event and Clk = '1' then     -- rising clock edge
        if Reset then
          previous_RX <= '0';
        else
          if (EN_16x_Baud = '1') then
            previous_RX <= rx_2;
          end if;
        end if;
      end if;
    end process Prev_RX_DFF;

    Start_Edge_DFF : process (Clk) is
    begin  -- process Start_Edge_DFF
      if Clk'event and Clk = '1' then     -- rising clock edge
        if Reset then
          start_Edge_Detected <= false;
        else
          if (EN_16x_Baud = '1') then
            start_Edge_Detected <= not running and (previous_RX = '1') and (rx_2 = '0');
          end if;
        end if;
      end if;
    end process Start_Edge_DFF;

    -----------------------------------------------------------------------------
    -- Running is '1' during a receiption
    -----------------------------------------------------------------------------
    Running_DFF : process (Clk) is
    begin  -- process Running_DFF
      if Clk'event and Clk = '1' then     -- rising clock edge
        if Reset then                     -- asynchronous reset (active high)
          running <= false;
        else
          if (EN_16x_Baud = '1') then
            if (start_Edge_Detected) then
              running <= true;
            elsif ((sample_Point = '1') and (stop_Bit_Position = '1')) then
              running <= false;
            end if;
          end if;
        end if;
      end if;
    end process Running_DFF;

    -----------------------------------------------------------------------------
    -- Delay start_Edge_Detected 7 clocks to get the mid-point in a bit
    -- The address needs to be 6 "0110" to get a delay of 7.
    -----------------------------------------------------------------------------

    start_Edge_Detected_Bit <= '1' when start_Edge_Detected else '0';

    Mid_Start_Bit_SRL16 : XIL_SRL16E
      generic map (
        C_TARGET    => C_TARGET,
        C_USE_SRL16 => C_USE_SRL16,               -- [string]
        INIT        => X"0000")
      port map (
        Config_Reset => Config_Reset,             -- [in  std_logic]
        CE           => EN_16x_Baud,              -- [in  std_logic]
        D            => start_Edge_Detected_Bit,  -- [in  std_logic]
        Clk          => Clk,                      -- [in  std_logic]
        A0           => '0',                      -- [in  std_logic]
        A1           => '1',                      -- [in  std_logic]
        A2           => '1',                      -- [in  std_logic]
        A3           => '0',                      -- [in  std_logic]
        Q            => mid_Start_Bit);           -- [out std_logic]

    mid_Start_Bit_or_Config_Reset <= mid_Start_Bit or Config_Reset;

    -- Keep regenerating new values into the 16 clock delay
    -- Starting with the first mid_Start_Bit and for every new sample_points
    -- until stop_Bit_Position is reached
    recycle <= not (stop_Bit_Position) and (mid_Start_Bit or sample_Point);

    Delay_16 : XIL_SRL16E
      generic map (
        C_TARGET    => C_TARGET,
        C_USE_SRL16 => C_USE_SRL16,       -- [string]
        INIT        => X"0000")
      port map (
        Config_Reset => Config_Reset,     -- [in  std_logic]
        CE           => EN_16x_Baud,      -- [in  std_logic]
        D            => recycle,          -- [in  std_logic]
        Clk          => Clk,              -- [in  std_logic]
        A0           => '1',              -- [in  std_logic]
        A1           => '1',              -- [in  std_logic]
        A2           => '1',              -- [in  std_logic]
        A3           => '1',              -- [in  std_logic]
        Q            => sample_Point);    -- [out std_logic]

    -----------------------------------------------------------------------------
    -- Detect when the stop bit is received
    -----------------------------------------------------------------------------
    Stop_Bit_Handler : process (Clk) is
    begin  -- process Stop_Bit_Handler
      if Clk'event and Clk = '1' then     -- rising clock edge
        if Reset then                     -- asynchronous reset (active high)
          stop_Bit_Position <= '0';
        else
          if (EN_16x_Baud = '1') then
            if (stop_Bit_Position = '0') then
              -- Start bit has reached the end of the shift register (Stop bit position)
              stop_Bit_Position <= sample_Point and new_rx_data(STOP_BIT_POS);
            elsif (sample_Point = '1') then
              -- if stop_Bit_Position = '1', then clear it at the next sample_Point
              stop_Bit_Position <= '0';
            end if;
          end if;
        end if;
      end if;
    end process Stop_Bit_Handler;

    -----------------------------------------------------------------------------
    -- Parity handling
    -----------------------------------------------------------------------------
    Using_Parity : if (C_USE_PARITY = 1) generate
      signal calc_Parity : std_logic;
      signal parity      : std_logic;
    begin

      Using_Odd_Parity : if (C_ODD_PARITY = 1) generate
      begin
        Parity_Bit : MB_FDSE
          generic map (
            C_TARGET => C_TARGET,
            INIT     => '1')                  -- [bit]
          port map (
            Q  => Parity,                 -- [out std_logic]
            C  => Clk,                    -- [in  std_logic]
            CE => EN_16x_Baud,            -- [in  std_logic]
            D  => calc_Parity,            -- [in  std_logic]
            S  => mid_Start_Bit_or_Config_Reset); -- [in std_logic]
      end generate Using_Odd_Parity;

      Using_Even_Parity : if (C_ODD_PARITY = 0) generate
      begin
        Parity_Bit : MB_FDRE
          generic map (
            C_TARGET => C_TARGET,
            INIT     => '0')                  -- [bit]
          port map (
            Q  => Parity,                 -- [out std_logic]
            C  => Clk,                    -- [in  std_logic]
            CE => EN_16x_Baud,            -- [in  std_logic]
            D  => calc_Parity,            -- [in  std_logic]
            R  => mid_Start_Bit_or_Config_Reset);  -- [in std_logic]
      end generate Using_Even_Parity;

      calc_Parity <= parity when (stop_Bit_Position or not sample_Point) = '1'
                     else parity xor rx_2;

      rx_parity_error_i <= (EN_16x_Baud and sample_Point) and (new_rx_data(CALC_PAR_POS)) and
                           not stop_Bit_Position
                           when running and (rx_2 /= Parity) else '0';
    end generate Using_Parity;

    Not_Using_Parity : if (C_USE_PARITY = 0) generate
    begin
      rx_parity_error_i <= '0';
    end generate Not_Using_Parity;


    -----------------------------------------------------------------------------
    -- Data part
    -----------------------------------------------------------------------------

    new_rx_data(0) <= rx_2;

    Convert_Serial_To_Parallel : for I in 1 to serial_to_parallel'length generate
    begin

      serial_to_parallel(I) <= new_rx_data(I) when (stop_Bit_Position or not sample_Point) = '1'
                               else new_rx_data(I-1);

      First_Bit : if (I = 1) generate
      begin
        First_Bit_I : MB_FDSE
          generic map (
            C_TARGET => C_TARGET,
            INIT     => '0')                  -- [bit]
          port map (
            Q  => new_rx_data(I),         -- [out std_logic]
            C  => Clk,                    -- [in  std_logic]
            CE => EN_16x_Baud,            -- [in  std_logic]
            D  => serial_to_parallel(I),  -- [in  std_logic]
            S  => mid_Start_Bit_or_Config_Reset); -- [in std_logic]
      end generate First_Bit;

      Rest_Bits : if (I /= 1) generate
      begin
        Others_I : MB_FDRE
          generic map (
            C_TARGET => C_TARGET,
            INIT     => '0')                  -- [bit]
          port map (
            Q  => new_rx_data(I),         -- [out std_logic]
            C  => Clk,                    -- [in  std_logic]
            CE => EN_16x_Baud,            -- [in  std_logic]
            D  => serial_to_parallel(I),  -- [in  std_logic]
            R  => mid_Start_Bit_or_Config_Reset); -- [in std_logic]
      end generate Rest_Bits;

    end generate Convert_Serial_To_Parallel;

    -----------------------------------------------------------------------------
    -- Write in the received word when the stop_bit has been received and it is a
    -- '1'
    -----------------------------------------------------------------------------
    NEW_RX_DATA_Write_DFF : process (Clk) is
    begin
      if Clk'event and Clk = '1' then
        if Reset then
          new_rx_data_write <= '0';
        else
          new_rx_data_Write <= stop_Bit_Position and rx_2 and sample_Point and EN_16x_Baud;
        end if;
      end if;
    end process NEW_RX_DATA_Write_DFF;

    Rx_Data_Exist_Handler : process (Clk) is
    begin
      if Clk'event and Clk = '1' then
        if Reset then
          rx_data_exists_i <= '0';
        else
          if (new_rx_data_write = '1') then
            rx_data_exists_i <= '1';
          end if;
          if (Read_RX_Data = '1') then
            rx_data_exists_i <= '0';
          end if;
        end if;
      end if;
    end process Rx_Data_Exist_Handler;

    Receive_Register : process (Clk) is
    begin
      if Clk'event and Clk = '1' then
        if Reset then
          rx_data_i <= (others => '0');
        elsif (NEW_RX_DATA_Write = '1') then
          rx_data_i <= new_rx_data(DATA_LSB_POS - C_DATA_BITS + 1 to DATA_LSB_POS);
        end if;
      end if;
    end process Receive_Register;

    UART_Read: process (Clk) is
    begin
      if Clk'event and Clk = '1' then
        if Read_RX_Data = '0' or Config_Reset = '1' then
          RX_Data <= (others => '0');
        else
          RX_Data <= rx_data_i;
        end if;
      end if;
    end process UART_Read;

  end generate TMR_No;

  RX_Data_Received <= new_rx_data_write;
  rx_frame_error_i <= stop_Bit_Position and sample_Point and EN_16x_Baud and not rx_2;
  RX_Overrun_Error <= rx_data_exists_i and new_rx_data_write;
  RX_Data_Exists   <= rx_data_exists_i;


  TMR_Yes : if (C_TMR /= 0) generate
    signal start_Edge_Detected       : boolean;
    signal running                   : boolean;

    signal tmr_disable_b             : boolean;
    -- Voted signals before DFF
    signal previous_RX_d             : std_logic;
    signal start_Edge_Detected_d     : boolean;
    signal running_d                 : boolean;
    signal recycle_voted             : std_logic;
    signal stop_Bit_Position_d       : std_logic;
    signal mid_Start_Bit_voted       : std_logic;
    signal mid_Start_Bit_voted_or_Config_Reset : std_logic;
    signal serial_to_parallel_voted  : std_logic_vector(1 to SERIAL_TO_PAR_LENGTH);
    signal new_rx_data_write_d       : std_logic;
    signal rx_data_exists_i_d        : std_logic;
    signal rx_data_i_d               : std_logic_vector(C_DATA_BITS-1 downto 0);
    signal RX_Data_d                 : std_logic_vector(C_DATA_BITS-1 downto 0);
  begin

    tmr_disable_b <= TMR_Disable = '1' and C_USE_TMR_DISABLE = 1;

    -----------------------------------------------------------------------------
    -- Double sample to avoid meta-stability
    -----------------------------------------------------------------------------
    RX_Sampling : process (Clk)
    begin  -- process RX_Sampling
      if Clk'event and Clk = '1' then     -- rising clock edge
        if Reset then                     -- asynchronous reset (active high)
          rx_1 <= '1';
          rx_2 <= '1';
        else
          rx_1 <= RX;
          rx_2 <= rx_1;
        end if;
      end if;
    end process RX_Sampling;

    -----------------------------------------------------------------------------
    -- Detect a falling edge on RX and start a new receiption if idle
    -----------------------------------------------------------------------------
    Prev_RX_Logic : process (Reset, EN_16x_Baud, rx_2, previous_RX) is
    begin
      if Reset then
        previous_RX_d <= '0';
      else
        if (EN_16x_Baud = '1') then
          previous_RX_d <= rx_2;
        else
          previous_RX_d <= previous_RX;
        end if;
       end if;
    end process Prev_RX_Logic;

    ToVote(UART_RX_PREVIOUS_RX_Pos) <= previous_RX_d;

    Prev_RX_DFF : process (Clk) is
    begin  -- process Prev_RX_DFF
      if Clk'event and Clk = '1' then     -- rising clock edge
         previous_RX <= vote(previous_RX_d,
                             FromAVote(UART_RX_PREVIOUS_RX_Pos),
                             FromBVote(UART_RX_PREVIOUS_RX_Pos), tmr_disable_b);
      end if;
    end process Prev_RX_DFF;

    Start_Edge_Logic : process (Reset, EN_16x_Baud, running, previous_RX, rx_2, start_Edge_Detected) is
    begin
      if Reset then
        start_Edge_Detected_d <= false;
      else
        if (EN_16x_Baud = '1') then
          start_Edge_Detected_d <= not running and (previous_RX = '1') and (rx_2 = '0');
        else
          start_Edge_Detected_d <= start_Edge_Detected;
        end if;
      end if;
    end process Start_Edge_Logic;

    ToVote(UART_RX_START_EDGE_DETECTED_Pos) <= '1' when start_Edge_Detected_d else
                                               '0';

    Start_Edge_DFF : process (Clk) is
    begin  -- process Start_Edge_DFF
      if Clk'event and Clk = '1' then     -- rising clock edge
        start_Edge_Detected <= vote(start_Edge_Detected_d,
                                    FromAVote(UART_RX_START_EDGE_DETECTED_Pos),
                                    FromBVote(UART_RX_START_EDGE_DETECTED_Pos), tmr_disable_b);
      end if;
    end process Start_Edge_DFF;

    -----------------------------------------------------------------------------
    -- Running is '1' during a receiption
    -----------------------------------------------------------------------------
    Running_Logic : process (Reset, EN_16x_Baud, start_Edge_Detected, sample_Point, stop_Bit_Position, running) is
    begin
      if Reset then
        running_d <= false;
      else
        if (EN_16x_Baud = '1') then
          if (start_Edge_Detected) then
            running_d <= true;
          elsif ((sample_Point = '1') and (stop_Bit_Position = '1')) then
            running_d <= false;
          else
            running_d <= running;
          end if;
        else
          running_d <= running;
        end if;
      end if;
    end process Running_Logic;

    ToVote(UART_RX_RUNNING_Pos) <= '1' when running_d else
                                   '0';

    Running_DFF : process (Clk) is
    begin
      if Clk'event and Clk = '1' then
        running <= vote(running_d, FromAVote(UART_RX_RUNNING_Pos), FromBVote(UART_RX_RUNNING_Pos),tmr_disable_b);
      end if;
    end process Running_DFF;

    -----------------------------------------------------------------------------
    -- Delay start_Edge_Detected 7 clocks to get the mid-point in a bit
    -- The address needs to be 6 "0110" to get a delay of 7.
    -----------------------------------------------------------------------------

    start_Edge_Detected_Bit <= '1' when start_Edge_Detected else '0';

    Mid_Start_Bit_SRL16 : XIL_SRL16E
      generic map (
        C_TARGET    => C_TARGET,
        C_USE_SRL16 => C_USE_SRL16,               -- [string]
        INIT        => X"0000")
      port map (
        Config_Reset => Config_Reset,             -- [in  std_logic]
        CE           => EN_16x_Baud,              -- [in  std_logic]
        D            => start_Edge_Detected_Bit,  -- [in  std_logic]
        Clk          => Clk,                      -- [in  std_logic]
        A0           => '0',                      -- [in  std_logic]
        A1           => '1',                      -- [in  std_logic]
        A2           => '1',                      -- [in  std_logic]
        A3           => '0',                      -- [in  std_logic]
        Q            => mid_Start_Bit);           -- [out std_logic]

    ToVote(UART_RX_MID_START_BIT_Pos) <= mid_Start_Bit;

    mid_Start_Bit_voted <= vote(mid_Start_Bit,
                                FromAVote(UART_RX_MID_START_BIT_Pos),
                                FromBVote(UART_RX_MID_START_BIT_Pos), tmr_disable_b);

    mid_Start_Bit_voted_or_Config_Reset <= mid_Start_Bit_voted or Config_Reset;

    -- Keep regenerating new values into the 16 clock delay
    -- Starting with the first mid_Start_Bit and for every new sample_points
    -- until stop_Bit_Position is reached
    recycle  <= not (stop_Bit_Position) and (mid_Start_Bit or sample_Point);

    ToVote(UART_RX_RECYCLE_Pos) <= recycle;

    recycle_voted <= vote(recycle, FromAVote(UART_RX_RECYCLE_Pos), FromBVote(UART_RX_RECYCLE_Pos), tmr_disable_b);

    Delay_16 : XIL_SRL16E
      generic map (
        C_TARGET    => C_TARGET,
        C_USE_SRL16 => C_USE_SRL16,       -- [string]
        INIT        => X"0000")
      port map (
        Config_Reset => Config_Reset,     -- [in  std_logic]
        CE           => EN_16x_Baud,      -- [in  std_logic]
        D            => recycle_voted,    -- [in  std_logic]
        Clk          => Clk,              -- [in  std_logic]
        A0           => '1',              -- [in  std_logic]
        A1           => '1',              -- [in  std_logic]
        A2           => '1',              -- [in  std_logic]
        A3           => '1',              -- [in  std_logic]
        Q            => sample_Point);    -- [out std_logic]

    -----------------------------------------------------------------------------
    -- Detect when the stop bit is received
    -----------------------------------------------------------------------------
    Stop_Bit_Handler_Logic : process (Reset, EN_16x_Baud, stop_Bit_Position, sample_Point, new_rx_data) is
    begin
      if Reset then
        stop_Bit_Position_d <= '0';
      else
        if (EN_16x_Baud = '1') then
          if (stop_Bit_Position = '0') then
            -- Start bit has reached the end of the shift register (Stop bit position)
            stop_Bit_Position_d <= sample_Point and new_rx_data(STOP_BIT_POS);
          elsif (sample_Point = '1') then
            -- if stop_Bit_Position = '1', then clear it at the next sample_Point
            stop_Bit_Position_d <= '0';
          else
            stop_Bit_Position_d <= stop_Bit_Position;
          end if;
        else
          stop_Bit_Position_d <= stop_Bit_Position;
        end if;
      end if;
    end process Stop_Bit_Handler_Logic;

    ToVote(UART_RX_STOP_BIT_POSITION_Pos) <= stop_Bit_Position_d;

    Stop_Bit_Handler_DFF : process (Clk) is
    begin
      if Clk'event and Clk = '1' then
        stop_Bit_Position <= vote(stop_Bit_Position_d,
                                  FromAVote(UART_RX_STOP_BIT_POSITION_Pos),
                                  FromBVote(UART_RX_STOP_BIT_POSITION_Pos), tmr_disable_b);
      end if;
    end process Stop_Bit_Handler_DFF;

    -----------------------------------------------------------------------------
    -- Parity handling
    -----------------------------------------------------------------------------
    Using_Parity : if (C_USE_PARITY = 1) generate
      signal calc_Parity       : std_logic;
      signal parity            : std_logic;
      signal calc_Parity_voted : std_logic;
    begin

      Using_Odd_Parity : if (C_ODD_PARITY = 1) generate
      begin
        Parity_Bit : MB_FDSE
          generic map (
            C_TARGET => C_TARGET,
            INIT => '1')                  -- [bit]
          port map (
            Q  => Parity,                 -- [out std_logic]
            C  => Clk,                    -- [in  std_logic]
            CE => EN_16x_Baud,            -- [in  std_logic]
            D  => calc_Parity_Voted,      -- [in  std_logic]
            S  => mid_Start_Bit_Voted_or_Config_Reset);   -- [in std_logic]
      end generate Using_Odd_Parity;

      Using_Even_Parity : if (C_ODD_PARITY = 0) generate
      begin
        Parity_Bit : MB_FDRE
          generic map (
            C_TARGET => C_TARGET,
            INIT => '0')                  -- [bit]
          port map (
            Q  => Parity,                 -- [out std_logic]
            C  => Clk,                    -- [in  std_logic]
            CE => EN_16x_Baud,            -- [in  std_logic]
            D  => calc_Parity_Voted,      -- [in  std_logic]
            R  => mid_Start_Bit_Voted_or_Config_Reset);   -- [in std_logic]
      end generate Using_Even_Parity;

      calc_Parity <= parity when (stop_Bit_Position or not sample_Point) = '1'
                     else parity xor rx_2;

      ToVote(UART_RX_CALC_PARITY_Pos) <= calc_Parity;

      calc_Parity_Voted <= vote(calc_Parity,
                                FromAVote(UART_RX_CALC_PARITY_Pos),
                                FromBVote(UART_RX_CALC_PARITY_Pos), tmr_disable_b);

      rx_parity_error_i <= (EN_16x_Baud and sample_Point) and (new_rx_data(CALC_PAR_POS)) and
                           not stop_Bit_Position
                           when running and (rx_2 /= Parity) else '0';


    end generate Using_Parity;

    Not_Using_Parity : if (C_USE_PARITY = 0) generate
    begin
      rx_parity_error_i <= '0';
      ToVote(UART_RX_CALC_PARITY_Pos) <= '0';
    end generate Not_Using_Parity;


    -----------------------------------------------------------------------------
    -- Data part
    -----------------------------------------------------------------------------

    new_rx_data(0) <= rx_2;

    ToVote(UART_RX_SERIAL_TO_PARALLEL_Pos'low + serial_to_parallel'length -1 downto
           UART_RX_SERIAL_TO_PARALLEL_Pos'low) <= serial_to_parallel;
    ToVote(UART_RX_SERIAL_TO_PARALLEL_Pos'high downto
           UART_RX_SERIAL_TO_PARALLEL_Pos'low + serial_to_parallel'length) <= (others => '0');

    serial_to_parallel_voted <=
      vote(serial_to_parallel,
           FromAVote(UART_RX_SERIAL_TO_PARALLEL_Pos'low + serial_to_parallel'length - 1 downto
                     UART_RX_SERIAL_TO_PARALLEL_Pos'low),
           FromBVote(UART_RX_SERIAL_TO_PARALLEL_Pos'low + serial_to_parallel'length - 1 downto
                     UART_RX_SERIAL_TO_PARALLEL_Pos'low), tmr_disable_b);

    Convert_Serial_To_Parallel : for I in 1 to serial_to_parallel'length generate
    begin

      serial_to_parallel(I) <= new_rx_data(I) when (stop_Bit_Position or not sample_Point) = '1'
                               else new_rx_data(I-1);

      First_Bit : if (I = 1) generate
      begin
        First_Bit_I : MB_FDSE
          generic map (
            C_TARGET => C_TARGET,
            INIT => '0')                        -- [bit]
          port map (
            Q  => new_rx_data(I),               -- [out std_logic]
            C  => Clk,                          -- [in  std_logic]
            CE => EN_16x_Baud,                  -- [in  std_logic]
            D  => serial_to_parallel_voted(I),  -- [in  std_logic]
            S  => mid_Start_Bit_Voted_or_Config_Reset);  -- [in std_logic]
      end generate First_Bit;

      Rest_Bits : if (I /= 1) generate
      begin
        Others_I : MB_FDRE
          generic map (
            C_TARGET => C_TARGET,
            INIT => '0')                        -- [bit]
          port map (
            Q  => new_rx_data(I),               -- [out std_logic]
            C  => Clk,                          -- [in  std_logic]
            CE => EN_16x_Baud,                  -- [in  std_logic]
            D  => serial_to_parallel_voted(I),  -- [in  std_logic]
            R  => mid_Start_Bit_Voted_or_Config_Reset);  -- [in std_logic]
      end generate Rest_Bits;

    end generate Convert_Serial_To_Parallel;

    -----------------------------------------------------------------------------
    -- Write in the received word when the stop_bit has been received and it is a
    -- '1'
    -----------------------------------------------------------------------------
    NEW_RX_DATA_Write_Logic : process (Reset, stop_Bit_Position, rx_2, sample_Point, EN_16x_Baud) is
    begin
      if Reset then
        new_rx_data_write_d <= '0';
      else
        new_rx_data_Write_d <= stop_Bit_Position and rx_2 and sample_Point and EN_16x_Baud;
      end if;
    end process NEW_RX_DATA_Write_Logic;

    ToVote(UART_RX_NEW_RX_DATA_WRITE_Pos) <= new_rx_data_write_d;

    NEW_RX_DATA_Write_DFF : process (Clk) is
    begin
      if Clk'event and Clk = '1' then
        new_rx_data_write <= vote(new_rx_data_write_d,
                                  FromAVote(UART_RX_NEW_RX_DATA_WRITE_Pos),
                                  FromBVote(UART_RX_NEW_RX_DATA_WRITE_Pos), tmr_disable_b);
      end if;
    end process NEW_RX_DATA_Write_DFF;

    Rx_Data_Exist_Handler_Logic : process (Reset, Read_RX_Data, new_rx_data_write, rx_data_exists_i) is
    begin
      if Reset then
        rx_data_exists_i_d <= '0';
      else
        if (Read_RX_Data = '1') then
          rx_data_exists_i_d <= '0';
        elsif (new_rx_data_write = '1') then
          rx_data_exists_i_d <= '1';
        else
          rx_data_exists_i_d <= rx_data_exists_i;
        end if;
      end if;
    end process Rx_Data_Exist_Handler_Logic;

    ToVote(UART_RX_DATA_EXISTS_Pos) <= rx_data_exists_i_d;

    Rx_Data_Exist_Handler_DFF : process (Clk) is
    begin
      if Clk'event and Clk = '1' then
        rx_data_exists_i <= vote(rx_data_exists_i_d,
                                 FromAVote(UART_RX_DATA_EXISTS_Pos),
                                 FromBVote(UART_RX_DATA_EXISTS_Pos), tmr_disable_b);
      end if;
    end process Rx_Data_Exist_Handler_DFF;

    Receive_Register_Logic : process (Reset, new_rx_data_write, new_rx_data, rx_data_i) is
    begin
      if Reset then
        rx_data_i_d <= (others => '0');
      elsif (new_rx_data_write = '1') then
        rx_data_i_d <= new_rx_data(DATA_LSB_POS - C_DATA_BITS + 1 to DATA_LSB_POS);
      else
        rx_data_i_d <= rx_data_i;
      end if;
    end process Receive_Register_Logic;

    ToVote(UART_RX_DATA_I_Pos'low+rx_data_i_d'length-1 downto UART_RX_DATA_I_Pos'low) <= rx_data_i_d;

    -- Work around spyglass bug report on null range to the left but not to the right
    spy1_g: if C_DATA_BITS < 8 generate
    begin
      ToVote(UART_RX_DATA_I_Pos'high downto UART_RX_DATA_I_Pos'low+rx_data_i_d'length)  <= (others => '0');
    end generate spy1_g;

    Receive_Register_DFF : process (Clk) is
    begin
      if Clk'event and Clk = '1' then
        rx_data_i <= vote(rx_data_i_d,
                          FromAVote(UART_RX_DATA_I_Pos'low+rx_data_i_d'length-1 downto UART_RX_DATA_I_Pos'low),
                          FromBVote(UART_RX_DATA_I_Pos'low+rx_data_i_d'length-1 downto UART_RX_DATA_I_Pos'low),
                          tmr_disable_b);
      end if;
    end process Receive_Register_DFF;

    UART_Read_Logic: process (Read_RX_Data, rx_data_i, Config_Reset) is
    begin
      if Read_RX_Data = '0' or Config_Reset = '1' then
        RX_Data_d <= (others => '0');
      else
        RX_Data_d <= rx_data_i;
      end if;
    end process UART_Read_Logic;

    ToVote(UART_RX_DATA_Pos'low+RX_Data_d'length-1 downto UART_RX_DATA_Pos'low)  <= RX_Data_d;

    -- Work around spyglass bug report on null range to the left but not to the right
    spy2_g: if C_DATA_BITS < 8 generate
    begin
      ToVote(UART_RX_DATA_Pos'high downto UART_RX_DATA_Pos'low+RX_Data_d'length) <= (others => '0');
    end generate spy2_g;

    UART_Read: process (Clk) is
    begin
      if Clk'event and Clk = '1' then
        RX_Data <= vote(RX_Data_d,
                        FromAVote(UART_RX_DATA_Pos'low+RX_Data_d'length-1 downto UART_RX_DATA_Pos'low),
                        FromBVote(UART_RX_DATA_Pos'low+RX_Data_d'length-1 downto UART_RX_DATA_Pos'low),
                        tmr_disable_b);
      end if;
    end process UART_Read;

  end generate TMR_Yes;

  RX_Frame_Error  <= rx_frame_error_i;
  RX_Parity_Error <= rx_parity_error_i;

end architecture IMP;


-------------------------------------------------------------------------------
-- uart_transmit.vhd - Entity and architecture
-------------------------------------------------------------------------------
--
-- (c) Copyright 2011,2016 Xilinx, Inc. All rights reserved.
--
-- This file contains confidential and proprietary information
-- of Xilinx, Inc. and is protected under U.S. and
-- international copyright and other intellectual property
-- laws.
--
-- DISCLAIMER
-- This disclaimer is not a license and does not grant any
-- rights to the materials distributed herewith. Except as
-- otherwise provided in a valid license issued to you by
-- Xilinx, and to the maximum extent permitted by applicable
-- law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND
-- WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES
-- AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
-- BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-
-- INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and
-- (2) Xilinx shall not be liable (whether in contract or tort,
-- including negligence, or under any other theory of
-- liability) for any loss or damage of any kind or nature
-- related to, arising under or in connection with these
-- materials, including for any direct, or any indirect,
-- special, incidental, or consequential loss or damage
-- (including loss of data, profits, goodwill, or any type of
-- loss or damage suffered as a result of any action brought
-- by a third party) even if such damage or loss was
-- reasonably foreseeable or Xilinx had been advised of the
-- possibility of the same.
--
-- CRITICAL APPLICATIONS
-- Xilinx products are not designed or intended to be fail-
-- safe, or for use in any application requiring fail-safe
-- performance, such as life-support or safety devices or
-- systems, Class III medical devices, nuclear facilities,
-- applications related to the deployment of airbags, or any
-- other applications that could lead to death, personal
-- injury, or severe property or environmental damage
-- (individually and collectively, "Critical
-- Applications"). Customer assumes the sole risk and
-- liability of any use of Xilinx products in Critical
-- Applications, subject only to applicable laws and
-- regulations governing limitations on product liability.
--
-- THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
-- PART OF THIS FILE AT ALL TIMES.
--
-------------------------------------------------------------------------------
-- Filename:        uart_transmit.vhd
--
-- Description:
--
-- VHDL-Standard:   VHDL'93/02
-------------------------------------------------------------------------------
-- Structure:
--              uart_transmit.vhd
--
-------------------------------------------------------------------------------
-- Author:          goran
--
-- History:
--   goran  2007-12-18    First Version
--
-------------------------------------------------------------------------------
-- Naming Conventions:
--      active low signals:                     "*_n"
--      clock signals:                          "clk", "clk_div#", "clk_#x"
--      reset signals:                          "rst", "rst_n"
--      generics:                               "C_*"
--      user defined types:                     "*_TYPE"
--      state machine next state:               "*_ns"
--      state machine current state:            "*_cs"
--      combinatorial signals:                  "*_com"
--      pipelined or register delay signals:    "*_d#"
--      counter signals:                        "*cnt*"
--      clock enable signals:                   "*_ce"
--      internal version of output port         "*_i"
--      device pins:                            "*_pin"
--      ports:                                  - Names begin with Uppercase
--      processes:                              "*_PROCESS"
--      component instantiations:               "<ENTITY_>I_<#|FUNC>
-------------------------------------------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;
library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_funcs.all;

entity UART_Transmit is
  generic (
    C_TARGET          : TARGET_FAMILY_TYPE;
    C_TMR             : integer := 0;
    C_USE_TMR_DISABLE : integer := 0;
    C_VOTE_SIZE       : integer := 0;
    C_USE_SRL16       : string;
    C_DATA_BITS       : integer range 5 to 8 := 8;
    C_USE_PARITY      : integer              := 0;
    C_ODD_PARITY      : integer              := 1
    );
  port (
    Config_Reset : in std_logic;
    Clk          : in std_logic;
    Reset        : in boolean;
    EN_16x_Baud  : in std_logic;

    TMR_Disable         : in std_logic;
    FromAVote           : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
    FromBVote           : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
    ToVote              : out std_logic_vector(C_VOTE_SIZE-1 downto 0);
    TX                  : out std_logic;
    Write_TX_Data       : in  std_logic;
    TX_Data             : in  std_logic_vector(C_DATA_BITS-1 downto 0);
    TX_Data_Transmitted : out std_logic;
    TX_Buffer_Empty     : out std_logic
    );

end entity UART_Transmit;

library ieee;
use ieee.numeric_std.all;
library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_vote_pkg.all;

architecture IMP of UART_Transmit is

  component XIL_SRL16E is
    generic (
      C_TARGET    : TARGET_FAMILY_TYPE;
      C_USE_SRL16 : string;
      C_STATIC    : boolean := false;
      INIT        : bit_vector);
    port (
      Config_Reset : in  std_logic;
      Q            : out std_logic;
      A0           : in  std_logic;
      A1           : in  std_logic;
      A2           : in  std_logic;
      A3           : in  std_logic;
      CE           : in  std_logic;
      CLK          : in  std_logic;
      D            : in  std_logic);
  end component XIL_SRL16E;

  component MB_MUXCY is
    generic (
      C_TARGET : TARGET_FAMILY_TYPE);
    port (
      LO : out std_logic;
      CI : in  std_logic;
      DI : in  std_logic;
      S  : in  std_logic);
  end component MB_MUXCY;

  component MB_XORCY is
    generic (
      C_TARGET : TARGET_FAMILY_TYPE);
    port (
      O  : out std_logic;
      CI : in  std_logic;
      LI : in  std_logic);
  end component MB_XORCY;

  component MB_FDSE is
  generic (
    C_TARGET : TARGET_FAMILY_TYPE;
    INIT : bit := '1');
  port(
    Q  : out std_logic;
    C  : in  std_logic;
    CE : in  std_logic;
    D  : in  std_logic;
    S  : in  std_logic);
  end component MB_FDSE;

  component MB_FDRE is
  generic (
    C_TARGET : TARGET_FAMILY_TYPE;
    INIT : bit := '0');
  port(
    Q  : out std_logic;
    C  : in  std_logic;
    CE : in  std_logic;
    D  : in  std_logic;
    R  : in  std_logic);
  end component MB_FDRE;

  component MB_MUXF5 is
  generic (
    C_TARGET : TARGET_FAMILY_TYPE);
  port (
    O  : out std_logic;
    I0 : in  std_logic;
    I1 : in  std_logic;
    S  : in  std_logic);
  end component MB_MUXF5;

  component MB_MUXF6 is
  generic (
    C_TARGET : TARGET_FAMILY_TYPE);
  port (
    O  : out std_logic;
    I0 : in  std_logic;
    I1 : in  std_logic;
    S  : in  std_logic);
  end component MB_MUXF6;

  -- signals for parity
  signal parity           : std_logic;
  signal calc_Parity      : std_logic;
  signal tx_Run1          : std_logic;
  signal select_Parity    : std_logic;
  signal data_to_transfer : std_logic_vector(0 to C_DATA_BITS-1);

  signal div16          : std_logic;
  signal tx_Data_Enable : std_logic;
  signal tx_Start       : std_logic;
  signal tx_DataBits    : std_logic;
  signal tx_Run         : std_logic;

  signal cnt_cy          : std_logic_vector(1 to 3);
  signal h_Cnt           : std_logic_vector(0 to 2);
  signal sum_cnt         : std_logic_vector(0 to 2);
  signal mux_sel         : std_logic_vector(0 to 2);
  signal mux_sel_is_zero : std_logic;

  constant mux_sel_init : std_logic_vector(0 to 2) :=
    std_logic_vector(to_unsigned(C_DATA_BITS-1, 3));

  signal mux_01      : std_logic;
  signal mux_23      : std_logic;
  signal mux_45      : std_logic;
  signal mux_67      : std_logic;
  signal mux_0123    : std_logic;
  signal mux_4567    : std_logic;
  signal mux_Out     : std_logic;
  signal serial_Data : std_logic;

  signal data_is_sent      : std_logic;
  signal tx_buffer_empty_i : std_logic;
  signal tx_i              : std_logic;
  signal fifo_DOut         : std_logic_vector(0 to C_DATA_BITS-1);

  -- Preserve signals after synthesis for simulation UART support
  attribute KEEP : string;
  attribute KEEP of tx_buffer_empty_i : signal is "TRUE";
  attribute KEEP of tx_i              : signal is "TRUE";

begin  -- architecture IMP

  TMR_No : if (C_TMR = 0) generate
    signal tx_Data_Enable_or_Config_Reset : std_logic;
    signal tx_Start_or_Config_Reset       : std_logic;
  begin

    ToVote <= (others => '0');

    -----------------------------------------------------------------------------
    -- Divide the EN_16x_Baud by 16 to get the correct baudrate
    -----------------------------------------------------------------------------
    DIV16_SRL16E : XIL_SRL16E
      generic map (
        C_TARGET    => C_TARGET,
        C_USE_SRL16 => C_USE_SRL16,       -- [string]
        INIT        => X"0001")
      port map (
        Config_Reset => Config_Reset,     -- [in  std_logic]
        CE           => EN_16x_Baud,      -- [in  std_logic]
        D            => div16,            -- [in  std_logic]
        Clk          => Clk,              -- [in  std_logic]
        A0           => '1',              -- [in  std_logic]
        A1           => '1',              -- [in  std_logic]
        A2           => '1',              -- [in  std_logic]
        A3           => '1',              -- [in  std_logic]
        Q            => div16);           -- [out std_logic]

    FDRE_I : MB_FDRE
      generic map (
        C_TARGET    => C_TARGET,
        INIT => '0')                      -- [bit]
      port map (
        Q  => tx_Data_Enable,             -- [out std_logic]
        C  => Clk,                        -- [in  std_logic]
        CE => EN_16x_Baud,                -- [in  std_logic]
        D  => div16,                      -- [in  std_logic]
        R  => tx_Data_Enable_or_Config_Reset);  -- [in std_logic]

    tx_Data_Enable_or_Config_Reset <= tx_Data_Enable or Config_Reset;
    
    -----------------------------------------------------------------------------
    -- tx_start is '1' for the start bit in a transmission
    -----------------------------------------------------------------------------
    TX_Start_DFF : process (Clk) is
    begin  -- process TX_Start_DFF
      if Clk'event and Clk = '1' then  -- rising clock edge
        if Reset then                 -- asynchronous reset (active high)
          tx_Start <= '0';
        else
          tx_Start <= not(tx_Run) and (tx_Start or (not(tx_buffer_empty_i) and tx_Data_Enable));
        end if;
      end if;
    end process TX_Start_DFF;

    tx_Start_or_Config_Reset <= tx_Start or Config_Reset;
    
    -----------------------------------------------------------------------------
    -- tx_DataBits is '1' during all databits transmission
    -----------------------------------------------------------------------------
    TX_Data_DFF : process (Clk) is
    begin  -- process TX_Data_DFF
      if Clk'event and Clk = '1' then  -- rising clock edge
        if Reset then                  -- asynchronous reset (active high)
          tx_DataBits <= '0';
        else
          tx_DataBits <= not(data_is_sent) and (tx_DataBits or (tx_Start and tx_Data_Enable));
        end if;
      end if;
    end process TX_Data_DFF;

    -- only decrement during data bits transfer
    cnt_cy(3) <= not tx_DataBits;

    Counter : for I in 2 downto 0 generate
    begin

      ---------------------------------------------------------------------------
      -- If mux_sel is zero then reload with the init value else decrement
      ---------------------------------------------------------------------------
      h_Cnt(I) <= mux_sel_init(I) when mux_sel_is_zero = '1' else not mux_sel(I);

      -- Don't need the last muxcy, cnt_cy(0) is not used anywhere
      Used_MuxCY: if I> 0 generate
      begin
        MUXCY_L_I : MB_MUXCY
          generic map(
            C_TARGET => C_TARGET)
          port map (
            DI => mux_sel(I),               -- [in  std_logic]
            CI => cnt_cy(I+1),              -- [in  std_logic]
            S  => h_cnt(I),                 -- [in  std_logic]
            LO => cnt_cy(I));               -- [out std_logic]
      end generate Used_MuxCY;

      XORCY_I : MB_XORCY
        generic map(
          C_TARGET => C_TARGET)
        port map (
          LI => h_cnt(I),                 -- [in  std_logic]
          CI => cnt_cy(I+1),              -- [in  std_logic]
          O  => sum_cnt(I));              -- [out std_logic]
    end generate Counter;

    Mux_Addr_DFF : process (Clk) is
    begin  -- process Mux_Addr_DFF
      if Clk'event and Clk = '1' then  -- rising clock edge
        if Reset then                 -- asynchronous reset (active high)
          mux_sel <= std_logic_vector(to_unsigned(C_DATA_BITS-1, mux_sel'length));
        else
          if (tx_Data_Enable = '1') then
            mux_sel <= sum_cnt;
          end if;
        end if;
      end if;
    end process Mux_Addr_DFF;

    -- Detecting when mux_sel is zero ie. all data bits is transfered
    mux_sel_is_zero <= '1' when mux_sel = "000" else '0';

    -- Read out the next data from the transmit fifo when the data has been
    -- transmitted
    Data_is_Sent_DFF : process (Clk) is
    begin  -- process Data_is_Sent_DFF
      if Clk'event and Clk = '1' then  -- rising clock edge
        if Reset then                 -- asynchronous reset (active high)
          data_is_sent <= '0';
        else
          data_is_sent <= tx_Data_Enable and mux_sel_is_zero;
        end if;
      end if;
    end process Data_is_Sent_DFF;

    TX_Data_Transmitted <= data_is_sent;

    -----------------------------------------------------------------------------
    -- Select which bit within the data word to transmit
    -----------------------------------------------------------------------------

    -- Need special treatment for inserting the parity bit because of parity generation
    Parity_Bit_Insertion : process (parity, select_Parity, fifo_DOut) is
    begin  -- process Parity_Bit_Insertion
      data_to_transfer <= fifo_DOut;
      if (select_Parity = '1') then
        data_to_transfer(C_DATA_BITS-1) <= parity;
      end if;
    end process Parity_Bit_Insertion;

    mux_01 <= data_to_transfer(1) when mux_sel(2) = '1' else data_to_transfer(0);
    mux_23 <= data_to_transfer(3) when mux_sel(2) = '1' else data_to_transfer(2);

    Data_Bits_Is_5 : if (C_DATA_BITS = 5) generate
    begin
      mux_45 <= data_to_transfer(4);
      mux_67 <= '0';
    end generate Data_Bits_Is_5;

    Data_Bits_Is_6 : if (C_DATA_BITS = 6) generate
    begin
      mux_45 <= data_to_transfer(5) when mux_sel(2) = '1' else data_to_transfer(4);
      mux_67 <= '0';
    end generate Data_Bits_Is_6;

    Data_Bits_Is_7 : if (C_DATA_BITS = 7) generate
    begin
      mux_45 <= data_to_transfer(5) when mux_sel(2) = '1' else data_to_transfer(4);
      mux_67 <= data_to_transfer(6);
    end generate Data_Bits_Is_7;

    Data_Bits_Is_8 : if (C_DATA_BITS = 8) generate
    begin
      mux_45 <= data_to_transfer(5) when mux_sel(2) = '1' else data_to_transfer(4);
      mux_67 <= data_to_transfer(7) when mux_sel(2) = '1' else data_to_transfer(6);
    end generate Data_Bits_Is_8;

    MUX_F5_1 : MB_MUXF5
      generic map(
        C_TARGET => C_TARGET)
      port map (
        O  => mux_0123,                   -- [out std_logic]
        I0 => mux_01,                     -- [in  std_logic]
        I1 => mux_23,                     -- [in  std_logic]
        S  => mux_sel(1));                -- [in std_logic]

    MUX_F5_2 : MB_MUXF5
      generic map(
        C_TARGET => C_TARGET)
      port map (
        O  => mux_4567,                   -- [out std_logic]
        I0 => mux_45,                     -- [in  std_logic]
        I1 => mux_67,                     -- [in  std_logic]
        S  => mux_sel(1));                -- [in std_logic]

    MUXF6_I : MB_MUXF6
      generic map(
        C_TARGET => C_TARGET)
      port map (
        O  => mux_out,                    -- [out std_logic]
        I0 => mux_0123,                   -- [in  std_logic]
        I1 => mux_4567,                   -- [in  std_logic]
        S  => mux_sel(0));                -- [in std_logic]

    Serial_Data_DFF : process (Clk) is
    begin  -- process Serial_Data_DFF
      if Clk'event and Clk = '1' then  -- rising clock edge
        if Reset then                 -- asynchronous reset (active high)
          serial_Data <= '0';
        else
          serial_Data <= mux_Out;
        end if;
      end if;
    end process Serial_Data_DFF;

    -----------------------------------------------------------------------------
    -- Force a '0' when tx_start is '1', Start_bit
    -- Force a '1' when tx_run is '0',   Idle
    -- otherwise put out the serial_data
    -----------------------------------------------------------------------------
    Serial_Out_DFF : process (Clk) is
    begin  -- process Serial_Out_DFF
      if Clk'event and Clk = '1' then  -- rising clock edge
        if Reset then                 -- asynchronous reset (active high)
          tx_i <= '1';
        else
          tx_i <= (not(tx_run) or serial_Data) and not(tx_Start);
        end if;
      end if;
    end process Serial_Out_DFF;

    -----------------------------------------------------------------------------
    -- Parity handling
    -----------------------------------------------------------------------------
    Using_Parity : if (C_USE_PARITY = 1) generate
    begin

      Using_Odd_Parity : if (C_ODD_PARITY = 1) generate
      begin
        Parity_Bit : MB_FDSE
          generic map(
            C_TARGET => C_TARGET,
            INIT => '1')                  -- [bit]
          port map (
            Q  => Parity,                 -- [out std_logic]
            C  => Clk,                    -- [in  std_logic]
            CE => tx_Data_Enable,         -- [in  std_logic]
            D  => calc_Parity,            -- [in  std_logic]
            S  => tx_Start_or_Config_Reset);  -- [in std_logic]
      end generate Using_Odd_Parity;

      Using_Even_Parity : if (C_ODD_PARITY = 0) generate
      begin
        Parity_Bit : MB_FDRE
          generic map (
            C_TARGET => C_TARGET,
            INIT => '0')                  -- [bit]
          port map (
            Q  => Parity,                 -- [out std_logic]
            C  => Clk,                    -- [in  std_logic]
            CE => tx_Data_Enable,         -- [in  std_logic]
            D  => calc_Parity,            -- [in  std_logic]
            R  => tx_Start_or_Config_Reset);  -- [in std_logic]
      end generate Using_Even_Parity;

      calc_Parity <= parity xor serial_data;

      tx_Run_DFF : process (Clk) is
      begin  -- process tx_Run_DFF
        if Clk'event and Clk = '1' then  -- rising clock edge
          if Reset then                 -- asynchronous reset (active high)
            tx_Run1 <= '0';
          else
            if (tx_Data_Enable = '1') then
              tx_Run1 <= tx_DataBits;
            end if;
          end if;
        end if;
      end process tx_Run_DFF;

      tx_Run <= tx_Run1 or tx_DataBits;

      Select_Parity_DFF : process (Clk) is
      begin  -- process Select_Parity_DFF
        if Clk'event and Clk = '1' then  -- rising clock edge
          if Reset then                 -- asynchronous reset (active high)
            select_Parity <= '0';
          else
            if (tx_Data_Enable = '1') then
              select_Parity <= mux_sel_is_zero;
            end if;
          end if;
        end if;
      end process Select_Parity_DFF;
    end generate Using_Parity;

    No_Parity : if (C_USE_PARITY = 0) generate
    begin
      tx_Run1       <= '0';
      calc_Parity   <= '0';
      parity        <= '0';
      tx_Run        <= tx_DataBits;
      select_Parity <= '0';
    end generate No_Parity;

    Data_To_Transmit: process (Clk) is
    begin
      if Clk'event and Clk = '1' then
        if Reset then
          fifo_DOut <= (others => '0');
        elsif (Write_TX_Data = '1') then
          fifo_DOut <= TX_Data;
        end if;
      end if;
    end process Data_To_Transmit;

    TX_Reg_Status: process (Clk) is
    begin
      if Clk'event and Clk = '1' then
        if Reset then
          tx_buffer_empty_i <= '1';
        else
          if Write_TX_Data = '1' then
            tx_buffer_empty_i <= '0';
          end if;
          if (data_is_sent = '1') then
            tx_buffer_empty_i <= '1';
          end if;
        end if;
      end if;
    end process TX_Reg_Status;

    TX_Buffer_Empty <= tx_buffer_empty_i;

  end generate TMR_No;

  TMR_Yes : if (C_TMR /= 0) generate
    signal tmr_disable_b        : boolean;
    signal div16_voted          : std_logic;
    signal tx_Data_Enable_voted : std_logic;
    signal tx_Data_Enable_voted_or_Config_Reset : std_logic;
    signal tx_Start_d           : std_logic;
    signal tx_DataBits_d        : std_logic;
    signal mux_sel_d            : std_logic_vector(0 to 2);
    signal data_is_sent_d       : std_logic;
    signal TX_d                 : std_logic;
    signal select_Parity_d      : std_logic;
    signal fifo_DOut_d          : std_logic_vector(0 to C_DATA_BITS-1);
    signal tx_buffer_empty_i_d  : std_logic;
    signal serial_Data_d        : std_logic;
  begin

    tmr_disable_b <= TMR_Disable = '1' and C_USE_TMR_DISABLE = 1;

    -----------------------------------------------------------------------------
    -- Divide the EN_16x_Baud by 16 to get the correct baudrate
    -----------------------------------------------------------------------------
    DIV16_SRL16E : XIL_SRL16E
      generic map (
        C_TARGET    => C_TARGET,
        C_USE_SRL16 => C_USE_SRL16,       -- [string]
        INIT        => X"0001")
      port map (
        Config_Reset => Config_Reset,     -- [in  std_logic]
        CE           => EN_16x_Baud,      -- [in  std_logic]
        D            => div16_voted,      -- [in  std_logic]
        Clk          => Clk,              -- [in  std_logic]
        A0           => '1',              -- [in  std_logic]
        A1           => '1',              -- [in  std_logic]
        A2           => '1',              -- [in  std_logic]
        A3           => '1',              -- [in  std_logic]
        Q            => div16);           -- [out std_logic]

    ToVote(UART_TX_DIV16_Pos) <= div16;

    div16_voted <= vote(div16, FromAVote(UART_TX_DIV16_Pos), FromBVote(UART_TX_DIV16_Pos), tmr_disable_b);

    FDRE_I : MB_FDRE
      generic map (
        C_TARGET    => C_TARGET,
        INIT => '0')                      -- [bit]
      port map (
        Q  => tx_Data_Enable,             -- [out std_logic]
        C  => Clk,                        -- [in  std_logic]
        CE => EN_16x_Baud,                -- [in  std_logic]
        D  => div16_voted,                -- [in  std_logic]
        R  => tx_Data_Enable_voted_or_Config_Reset); -- [in std_logic]

    ToVote(UART_TX_DATA_ENABLE_Pos) <= tx_Data_Enable;

    tx_Data_Enable_voted <= vote(tx_Data_Enable,
                                 FromAVote(UART_TX_DATA_ENABLE_Pos),
                                 FromBVote(UART_TX_DATA_ENABLE_Pos), tmr_disable_b);

    tx_Data_Enable_voted_or_Config_Reset <= tx_Data_Enable_voted or Config_Reset;

    -----------------------------------------------------------------------------
    -- tx_start is '1' for the start bit in a transmission
    -----------------------------------------------------------------------------
    TX_Start_Logic : process (Reset, tx_Run, tx_Start, tx_buffer_empty_i, tx_Data_Enable) is
    begin
      if Reset then
        tx_Start_d <= '0';
      else
        tx_Start_d <= not(tx_Run) and (tx_Start or (not(tx_buffer_empty_i) and tx_Data_Enable));
      end if;
    end process TX_Start_Logic;

    ToVote(UART_TX_START_Pos) <= tx_Start_d;

    TX_Start_DFF : process (Clk) is
    begin
      if Clk'event and Clk = '1' then  -- rising clock edge
        tx_Start <= vote(tx_Start_d, FromAVote(UART_TX_START_Pos), FromBVote(UART_TX_START_Pos), tmr_disable_b);
      end if;
    end process TX_Start_DFF;

    -----------------------------------------------------------------------------
    -- tx_DataBits is '1' during all databits transmission
    -----------------------------------------------------------------------------
    TX_Data_Logic : process (Reset, data_is_sent, tx_DataBits, tx_Start, tx_Data_Enable) is
    begin
      if Reset then
        tx_DataBits_d <= '0';
      else
        tx_DataBits_d <= not(data_is_sent) and (tx_DataBits or (tx_Start and tx_Data_Enable));
      end if;
    end process TX_Data_Logic;

    ToVote(UART_TX_DATABITS_Pos) <= tx_DataBits_d;

    TX_Data_DFF : process (Clk) is
    begin
      if Clk'event and Clk = '1' then
        tx_DataBits <= vote(tx_DataBits_d,
                            FromAVote(UART_TX_DATABITS_Pos),
                            FromBVote(UART_TX_DATABITS_Pos), tmr_disable_b);
      end if;
    end process TX_Data_DFF;

    -- only decrement during data bits transfer
    cnt_cy(3) <= not tx_DataBits;

    Counter : for I in 2 downto 0 generate
    begin

      ---------------------------------------------------------------------------
      -- If mux_sel is zero then reload with the init value else decrement
      ---------------------------------------------------------------------------
      h_Cnt(I) <= mux_sel_init(I) when mux_sel_is_zero = '1' else not mux_sel(I);

      -- Don't need the last muxcy, cnt_cy(0) is not used anywhere
      Used_MuxCY: if I> 0 generate
      begin
        MUXCY_L_I : MB_MUXCY
          generic map(
            C_TARGET => C_TARGET)
          port map (
            DI => mux_sel(I),               -- [in  std_logic]
            CI => cnt_cy(I+1),              -- [in  std_logic]
            S  => h_cnt(I),                 -- [in  std_logic]
            LO => cnt_cy(I));               -- [out std_logic]
      end generate Used_MuxCY;

      XORCY_I : MB_XORCY
        generic map(
          C_TARGET => C_TARGET)
        port map (
          LI => h_cnt(I),                 -- [in  std_logic]
          CI => cnt_cy(I+1),              -- [in  std_logic]
          O  => sum_cnt(I));              -- [out std_logic]
    end generate Counter;

    Mux_Addr_Logic : process (Reset,tx_Data_Enable, sum_cnt, mux_sel) is
    begin  -- process Mux_Addr_DFF
      if Reset then                 -- asynchronous reset (active high)
        mux_sel_d <= std_logic_vector(to_unsigned(C_DATA_BITS-1, mux_sel'length));
      else
        if (tx_Data_Enable = '1') then
          mux_sel_d <= sum_cnt;
        else
          mux_sel_d <= mux_sel;
        end if;
      end if;
    end process Mux_Addr_Logic;

    ToVote(UART_TX_MUX_SEL_Pos) <= mux_sel_d;

    Mux_Addr_DFF : process (Clk) is
    begin
      if Clk'event and Clk = '1' then
        mux_sel <= vote(mux_sel_d, FromAVote(UART_TX_MUX_SEL_Pos), FromBVote(UART_TX_MUX_SEL_Pos), tmr_disable_b);
      end if;
    end process Mux_Addr_DFF;

    -- Detecting when mux_sel is zero ie. all data bits is transfered
    mux_sel_is_zero <= '1' when mux_sel = "000" else '0';

    -- Read out the next data from the transmit fifo when the data has been
    -- transmitted
    Data_is_Sent_Logic : process (Reset, tx_Data_Enable, mux_sel_is_zero) is
    begin
      if Reset then
        data_is_sent_d <= '0';
      else
        data_is_sent_d <= tx_Data_Enable and mux_sel_is_zero;
      end if;
    end process Data_is_Sent_Logic;

    ToVote(UART_TX_DATA_IS_SENT_Pos) <= data_is_sent_d;

    Data_is_Sent_DFF : process (Clk) is
    begin
      if Clk'event and Clk = '1' then
        data_is_sent <= vote(data_is_sent_d,
                             FromAVote(UART_TX_DATA_IS_SENT_Pos),
                             FromBVote(UART_TX_DATA_IS_SENT_Pos), tmr_disable_b);
      end if;
    end process Data_is_Sent_DFF;

    TX_Data_Transmitted <= data_is_sent;

    -----------------------------------------------------------------------------
    -- Select which bit within the data word to transmit
    -----------------------------------------------------------------------------

    -- Need special treatment for inserting the parity bit because of parity generation
    Parity_Bit_Insertion : process (parity, select_Parity, fifo_DOut) is
    begin  -- process Parity_Bit_Insertion
      data_to_transfer <= fifo_DOut;
      if (select_Parity = '1') then
        data_to_transfer(C_DATA_BITS-1) <= parity;
      end if;
    end process Parity_Bit_Insertion;

    mux_01 <= data_to_transfer(1) when mux_sel(2) = '1' else data_to_transfer(0);
    mux_23 <= data_to_transfer(3) when mux_sel(2) = '1' else data_to_transfer(2);

    Data_Bits_Is_5 : if (C_DATA_BITS = 5) generate
    begin
      mux_45 <= data_to_transfer(4);
      mux_67 <= '0';
    end generate Data_Bits_Is_5;

    Data_Bits_Is_6 : if (C_DATA_BITS = 6) generate
    begin
      mux_45 <= data_to_transfer(5) when mux_sel(2) = '1' else data_to_transfer(4);
      mux_67 <= '0';
    end generate Data_Bits_Is_6;

    Data_Bits_Is_7 : if (C_DATA_BITS = 7) generate
    begin
      mux_45 <= data_to_transfer(5) when mux_sel(2) = '1' else data_to_transfer(4);
      mux_67 <= data_to_transfer(6);
    end generate Data_Bits_Is_7;

    Data_Bits_Is_8 : if (C_DATA_BITS = 8) generate
    begin
      mux_45 <= data_to_transfer(5) when mux_sel(2) = '1' else data_to_transfer(4);
      mux_67 <= data_to_transfer(7) when mux_sel(2) = '1' else data_to_transfer(6);
    end generate Data_Bits_Is_8;

    MUX_F5_1 : MB_MUXF5
      generic map(
        C_TARGET => C_TARGET)
      port map (
        O  => mux_0123,                   -- [out std_logic]
        I0 => mux_01,                     -- [in  std_logic]
        I1 => mux_23,                     -- [in  std_logic]
        S  => mux_sel(1));                -- [in std_logic]

    MUX_F5_2 : MB_MUXF5
      generic map(
        C_TARGET => C_TARGET)
      port map (
        O  => mux_4567,                   -- [out std_logic]
        I0 => mux_45,                     -- [in  std_logic]
        I1 => mux_67,                     -- [in  std_logic]
        S  => mux_sel(1));                -- [in std_logic]

    MUXF6_I : MB_MUXF6
      generic map(
        C_TARGET => C_TARGET)
      port map (
        O  => mux_out,                    -- [out std_logic]
        I0 => mux_0123,                   -- [in  std_logic]
        I1 => mux_4567,                   -- [in  std_logic]
        S  => mux_sel(0));                -- [in std_logic]

    Serial_Data_Logic : process (Reset, mux_Out) is
    begin
      if Reset then
        serial_Data_d <= '0';
      else
        serial_Data_d <= mux_Out;
      end if;
    end process Serial_Data_Logic;

    ToVote(UART_TX_SERIAL_DATA_Pos) <= serial_Data_d;

    Serial_Data_DFF : process (Clk) is
    begin
      if Clk'event and Clk = '1' then
        serial_Data <= vote(serial_Data_d,
                            FromAVote(UART_TX_SERIAL_DATA_Pos),
                            FromBVote(UART_TX_SERIAL_DATA_Pos), tmr_disable_b);
      end if;
    end process Serial_Data_DFF;

    -----------------------------------------------------------------------------
    -- Force a '0' when tx_start is '1', Start_bit
    -- Force a '1' when tx_run is '0',   Idle
    -- otherwise put out the serial_data
    -----------------------------------------------------------------------------
    Serial_Out_Logic : process (Reset, tx_run, serial_Data, tx_Start) is
    begin
      if Reset then
        TX_d <= '1';
      else
        TX_d <= (not(tx_run) or serial_Data) and not(tx_Start);
      end if;
    end process Serial_Out_Logic;

    ToVote(UART_TX_Pos) <= TX_d;

    Serial_Out_DFF : process (Clk) is
    begin
      if Clk'event and Clk = '1' then
        tx_i <= vote(TX_d, FromAVote(UART_TX_Pos), FromBVote(UART_TX_Pos), tmr_disable_b);
      end if;
    end process Serial_Out_DFF;

    -----------------------------------------------------------------------------
    -- Parity handling
    -----------------------------------------------------------------------------
    Using_Parity : if (C_USE_PARITY = 1) generate
      signal calc_Parity_voted    : std_logic;
      signal tx_Run1_d            : std_logic;
      signal serial_Data_d        : std_logic;
    begin

      Using_Odd_Parity : if (C_ODD_PARITY = 1) generate
      begin
        Parity_Bit : MB_FDSE
          generic map(
            C_TARGET => C_TARGET,
            INIT => '1')                  -- [bit]
          port map (
            Q  => Parity,                 -- [out std_logic]
            C  => Clk,                    -- [in  std_logic]
            CE => tx_Data_Enable_voted,   -- [in  std_logic]
            D  => calc_Parity_voted,      -- [in  std_logic]
            S  => tx_Start_d);            -- [in std_logic]
      end generate Using_Odd_Parity;

      Using_Even_Parity : if (C_ODD_PARITY = 0) generate
      begin
        Parity_Bit : MB_FDRE
          generic map (
            C_TARGET => C_TARGET,
            INIT => '0')                  -- [bit]
          port map (
            Q  => Parity,                 -- [out std_logic]
            C  => Clk,                    -- [in  std_logic]
            CE => tx_Data_Enable_voted,   -- [in  std_logic]
            D  => calc_Parity_voted,      -- [in  std_logic]
            R  => tx_Start_d);            -- [in std_logic]
      end generate Using_Even_Parity;

      calc_Parity <= parity xor serial_data;

      calc_Parity_voted <= vote(calc_Parity,
                                FromAVote(UART_TX_CALC_PARITY_Pos),
                                FromBVote(UART_TX_CALC_PARITY_Pos), tmr_disable_b);

      ToVote(UART_TX_CALC_PARITY_Pos) <= calc_Parity;

      tx_Run_Logic : process (Reset, tx_Data_Enable, tx_DataBits, tx_Run1) is
      begin
        if Reset then
          tx_Run1_d <= '0';
        else
          if (tx_Data_Enable = '1') then
            tx_Run1_d <= tx_DataBits;
          else
            tx_Run1_d <= tx_Run1;
          end if;
        end if;
      end process tx_Run_Logic;

      ToVote(UART_TX_RUN_Pos) <= tx_Run1_d;

      tx_Run_DFF : process (Clk) is
      begin
        if Clk'event and Clk = '1' then
          tx_Run1 <= vote(tx_Run1_d, FromAVote(UART_TX_RUN_Pos), FromBVote(UART_TX_RUN_Pos), tmr_disable_b);
        end if;
      end process tx_Run_DFF;

      tx_Run <= tx_Run1 or tx_DataBits;

      Select_Parity_Logic: process (Reset, tx_Data_Enable, mux_sel_is_zero, select_Parity) is
      begin
        if Reset then
          select_Parity_d <= '0';
        else
          if (tx_Data_Enable = '1') then
            select_Parity_d <= mux_sel_is_zero;
          else
            select_Parity_d <= select_Parity;
          end if;
        end if;
      end process Select_Parity_Logic;

      ToVote(UART_TX_SEL_PARITY_Pos) <= select_Parity_d;

      Select_Parity_DFF : process (Clk) is
      begin
        if Clk'event and Clk = '1' then
          select_Parity <= vote(select_Parity_d,
                                FromAVote(UART_TX_SEL_PARITY_Pos),
                                FromBVote(UART_TX_SEL_PARITY_Pos), tmr_disable_b);
        end if;
      end process Select_Parity_DFF;

    end generate Using_Parity;

    No_Parity : if (C_USE_PARITY = 0) generate
    begin
      tx_Run1                         <= '0';
      calc_Parity                     <= '0';
      parity                          <= '0';
      tx_Run                          <= tx_DataBits;
      select_Parity                   <= '0';
      select_Parity_d                 <= '0';
      ToVote(UART_TX_CALC_PARITY_Pos) <= '0';
      ToVote(UART_TX_RUN_Pos)         <= '0';
      ToVote(UART_TX_SEL_PARITY_Pos)  <= '0';
    end generate No_Parity;

    Data_To_Transmit_Logic: process (Reset, Write_TX_Data, TX_Data, fifo_DOut) is
    begin
      if Reset then
        fifo_DOut_d <= (others => '0');
      elsif (Write_TX_Data = '1') then
        fifo_DOut_d <= TX_Data;
      else
        fifo_DOut_d <= fifo_DOut;
      end if;
    end process Data_To_Transmit_Logic;

    ToVote(UART_TX_FIFO_DOUT_Pos'low+fifo_DOut'length-1 downto UART_TX_FIFO_DOUT_Pos'low) <= fifo_DOut_d;

    -- Work around spyglass bug report on null range to the left but not to the right
    spy_g: if C_DATA_BITS < 8 generate
    begin
      ToVote(UART_TX_FIFO_DOUT_Pos'high downto UART_TX_FIFO_DOUT_Pos'low+fifo_DOut'length)  <= (others => '0');
    end generate spy_g;

    Data_To_Transmit_DFF: process (Clk) is
    begin
      if Clk'event and Clk = '1' then
        fifo_DOut <= vote(fifo_DOut_d,
                          FromAVote(UART_TX_FIFO_DOUT_Pos'low+fifo_DOut'length-1 downto UART_TX_FIFO_DOUT_Pos'low),
                          FromBVote(UART_TX_FIFO_DOUT_Pos'low+fifo_DOut'length-1 downto UART_TX_FIFO_DOUT_Pos'low),
                          tmr_disable_b);
      end if;
    end process Data_To_Transmit_DFF;

    TX_Reg_Status_Logic: process (Reset, Write_TX_Data, data_is_sent, tx_buffer_empty_i) is
    begin
      if Reset then
        tx_buffer_empty_i_d <= '1';
      else
        if (data_is_sent = '1') then
          tx_buffer_empty_i_d <= '1';
        elsif Write_TX_Data = '1' then
          tx_buffer_empty_i_d <= '0';
        else
          tx_buffer_empty_i_d <= tx_buffer_empty_i;
        end if;
      end if;
    end process TX_Reg_Status_Logic;

    ToVote(UART_TX_BUFFER_EMPTY_Pos) <= tx_buffer_empty_i_d;

    TX_Reg_Status_DFF: process (Clk) is
    begin
      if Clk'event and Clk = '1' then
        tx_buffer_empty_i <= vote(tx_buffer_empty_i_d,
                                  FromAVote(UART_TX_BUFFER_EMPTY_Pos),
                                  FromBVote(UART_TX_BUFFER_EMPTY_Pos), tmr_disable_b);
      end if;
    end process TX_Reg_Status_DFF;

    TX_Buffer_Empty <= tx_buffer_empty_i;

  end generate TMR_Yes;

  TX <= tx_i;

end architecture IMP;


-------------------------------------------------------------------------------
-- iomodule_core.vhd - Entity and architecture
-------------------------------------------------------------------------------
--
-- (c) Copyright 2011-2012,2016,2018 Xilinx, Inc. All rights reserved.
--
-- This file contains confidential and proprietary information
-- of Xilinx, Inc. and is protected under U.S. and
-- international copyright and other intellectual property
-- laws.
--
-- DISCLAIMER
-- This disclaimer is not a license and does not grant any
-- rights to the materials distributed herewith. Except as
-- otherwise provided in a valid license issued to you by
-- Xilinx, and to the maximum extent permitted by applicable
-- law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND
-- WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES
-- AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
-- BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-
-- INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and
-- (2) Xilinx shall not be liable (whether in contract or tort,
-- including negligence, or under any other theory of
-- liability) for any loss or damage of any kind or nature
-- related to, arising under or in connection with these
-- materials, including for any direct, or any indirect,
-- special, incidental, or consequential loss or damage
-- (including loss of data, profits, goodwill, or any type of
-- loss or damage suffered as a result of any action brought
-- by a third party) even if such damage or loss was
-- reasonably foreseeable or Xilinx had been advised of the
-- possibility of the same.
--
-- CRITICAL APPLICATIONS
-- Xilinx products are not designed or intended to be fail-
-- safe, or for use in any application requiring fail-safe
-- performance, such as life-support or safety devices or
-- systems, Class III medical devices, nuclear facilities,
-- applications related to the deployment of airbags, or any
-- other applications that could lead to death, personal
-- injury, or severe property or environmental damage
-- (individually and collectively, "Critical
-- Applications"). Customer assumes the sole risk and
-- liability of any use of Xilinx products in Critical
-- Applications, subject only to applicable laws and
-- regulations governing limitations on product liability.
--
-- THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
-- PART OF THIS FILE AT ALL TIMES.
--
-------------------------------------------------------------------------------
-- Filename:        iomodule_core.vhd
--
-- Description:
--
-- VHDL-Standard:   VHDL'93/02
-------------------------------------------------------------------------------
-- Structure:
--              iomodule_core.vhd
--
-------------------------------------------------------------------------------
-- Author:          goran
--
-- History:
--   goran   2007-12-18    First Version
--   stefana 2012-03-20    Added GPI interrupt
--
-------------------------------------------------------------------------------
-- Naming Conventions:
--      active low signals:                     "*_n"
--      clock signals:                          "clk", "clk_div#", "clk_#x"
--      reset signals:                          "rst", "rst_n"
--      generics:                               "C_*"
--      user defined types:                     "*_TYPE"
--      state machine next state:               "*_ns"
--      state machine current state:            "*_cs"
--      combinatorial signals:                  "*_com"
--      pipelined or register delay signals:    "*_d#" cx
--      counter signals:                        "*cnt*"
--      clock enable signals:                   "*_ce"
--      internal version of output port         "*_i"
--      device pins:                            "*_pin"
--      ports:                                  - Names begin with Uppercase
--      processes:                              "*_PROCESS"
--      component instantiations:               "<ENTITY_>I_<#|FUNC>
-------------------------------------------------------------------------------

library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_funcs.all;

entity Iomodule_core is

  generic (
    C_TARGET               : TARGET_FAMILY_TYPE;
    C_FREQ                 : integer               := 100000000;
    C_USE_CONFIG_RESET     : integer               := 0;
    C_AVOID_PRIMITIVES     : integer               := 0;
    C_TMR                  : integer               := 0;
    C_USE_TMR_DISABLE      : integer               := 0;

    -- UART generics
    C_USE_UART_RX          : integer               := 0;
    C_USE_UART_TX          : integer               := 0;
    C_UART_BAUDRATE        : integer               := 9600;
    C_UART_DATA_BITS       : integer range 5 to 8  := 8;
    C_UART_USE_PARITY      : integer               := 0;
    C_UART_ODD_PARITY      : integer               := 0;
    C_UART_RX_INTERRUPT    : integer               := 0;
    C_UART_TX_INTERRUPT    : integer               := 0;
    C_UART_ERROR_INTERRUPT : integer               := 0;
    C_UART_PROG_BAUDRATE   : integer               := 0;

    -- FIT generics
    C_USE_FIT1             : integer               := 0;
    C_FIT1_No_CLOCKS       : integer               := 1000;
    C_FIT1_INTERRUPT       : integer               := 0;
    C_USE_FIT2             : integer               := 0;
    C_FIT2_No_CLOCKS       : integer               := 1000;
    C_FIT2_INTERRUPT       : integer               := 0;
    C_USE_FIT3             : integer               := 0;
    C_FIT3_No_CLOCKS       : integer               := 1000;
    C_FIT3_INTERRUPT       : integer               := 0;
    C_USE_FIT4             : integer               := 0;
    C_FIT4_No_CLOCKS       : integer               := 1000;
    C_FIT4_INTERRUPT       : integer               := 0;

    -- PIT generics
    C_USE_PIT1       : integer               := 0;
    C_PIT1_SIZE      : integer               := 32;
    C_PIT1_READABLE  : integer               := 1;
    C_PIT1_PRESCALER : integer range 0 to 9  := 0;
    C_PIT1_INTERRUPT : integer               := 0;
    C_USE_PIT2       : integer               := 0;
    C_PIT2_SIZE      : integer               := 32;
    C_PIT2_READABLE  : integer               := 1;
    C_PIT2_PRESCALER : integer range 0 to 9  := 0;
    C_PIT2_INTERRUPT : integer               := 0;
    C_USE_PIT3       : integer               := 0;
    C_PIT3_SIZE      : integer               := 32;
    C_PIT3_READABLE  : integer               := 1;
    C_PIT3_PRESCALER : integer range 0 to 9  := 0;
    C_PIT3_INTERRUPT : integer               := 0;
    C_USE_PIT4       : integer               := 0;
    C_PIT4_SIZE      : integer               := 32;
    C_PIT4_READABLE  : integer               := 1;
    C_PIT4_PRESCALER : integer range 0 to 9  := 0;
    C_PIT4_INTERRUPT : integer               := 0;

    -- GPO Generics
    C_USE_GPO1  : integer := 0;
    C_GPO1_SIZE : integer range 1 to 32 := 32;
    C_GPO1_INIT : std_logic_vector(31 downto 0) := (others => '0');
    C_USE_GPO2  : integer := 0;
    C_GPO2_SIZE : integer range 1 to 32 := 32;
    C_GPO2_INIT : std_logic_vector(31 downto 0) := (others => '0');
    C_USE_GPO3  : integer := 0;
    C_GPO3_SIZE : integer range 1 to 32 := 32;
    C_GPO3_INIT : std_logic_vector(31 downto 0) := (others => '0');
    C_USE_GPO4  : integer := 0;
    C_GPO4_SIZE : integer range 1 to 32 := 32;
    C_GPO4_INIT : std_logic_vector(31 downto 0) := (others => '0');

    -- GPI Generics
    C_USE_GPI1       : integer := 0;
    C_GPI1_SIZE      : integer range 1 to 32 := 32;
    C_GPI1_INTERRUPT : integer               := 0;
    C_USE_GPI2       : integer := 0;
    C_GPI2_SIZE      : integer range 1 to 32 := 32;
    C_GPI2_INTERRUPT : integer               := 0;
    C_USE_GPI3       : integer := 0;
    C_GPI3_SIZE      : integer range 1 to 32 := 32;
    C_GPI3_INTERRUPT : integer               := 0;
    C_USE_GPI4       : integer := 0;
    C_GPI4_SIZE      : integer range 1 to 32 := 32;
    C_GPI4_INTERRUPT : integer               := 0;

    -- Interrupt Handler Generics
    C_ADDR_WIDTH        : integer range 32 to 64    := 32;
    C_INTC_USE_EXT_INTR : integer                   := 0;
    C_INTC_INTR_SIZE    : integer range 1 to 16     := 1;
    C_INTC_LEVEL_EDGE   : std_logic_vector(15 downto 0) := X"0000";
    C_INTC_POSITIVE     : std_logic_vector(15 downto 0) := X"0000";
    C_INTC_HAS_FAST     : integer range 0 to 1      := 0;
    C_INTC_ADDR_WIDTH   : integer range 5 to 64     := 32;
    C_INTC_BASE_VECTORS : std_logic_vector(63 downto 0) := X"0000000000000000";
    C_INTC_ASYNC_INTR   : std_logic_vector(15 downto 0) := X"FFFF";
    C_INTC_NUM_SYNC_FF  : integer range 0 to 7      := 2
    );

  port (
    Config_Reset : in std_logic := '0';
    CLK          : in std_logic;
    Rst          : in std_logic;
    TMR_Rst      : in std_logic;
    TMR_Disable  : in std_logic;

    -- TMR voting inbetween redundant IO Modules
    ToVote       : out std_logic_vector(1023 downto 0);
    FromAVote    : in  std_logic_vector(1023 downto 0);
    FromBVote    : in  std_logic_vector(1023 downto 0);

    -- UART I/O
    UART_Rx        : in  std_logic;
    UART_Tx        : out std_logic;
    UART_Interrupt : out std_logic;

    -- FIT I/O
    FIT1_Interrupt : out std_logic;
    FIT1_Toggle    : out std_logic;
    FIT2_Interrupt : out std_logic;
    FIT2_Toggle    : out std_logic;
    FIT3_Interrupt : out std_logic;
    FIT3_Toggle    : out std_logic;
    FIT4_Interrupt : out std_logic;
    FIT4_Toggle    : out std_logic;

    -- PIT I/O
    PIT1_Enable    : in  std_logic;
    PIT1_Interrupt : out std_logic;
    PIT1_Toggle    : out std_logic;
    PIT2_Enable    : in  std_logic;
    PIT2_Interrupt : out std_logic;
    PIT2_Toggle    : out std_logic;
    PIT3_Enable    : in  std_logic;
    PIT3_Interrupt : out std_logic;
    PIT3_Toggle    : out std_logic;
    PIT4_Enable    : in  std_logic;
    PIT4_Interrupt : out std_logic;
    PIT4_Toggle    : out std_logic;

    -- GPO IO
    GPO1 : out std_logic_vector(C_GPO1_SIZE-1 downto 0);
    GPO2 : out std_logic_vector(C_GPO2_SIZE-1 downto 0);
    GPO3 : out std_logic_vector(C_GPO3_SIZE-1 downto 0);
    GPO4 : out std_logic_vector(C_GPO4_SIZE-1 downto 0);

    -- GPI IO
    GPI1           : in  std_logic_vector(C_GPI1_SIZE-1 downto 0);
    GPI1_Interrupt : out std_logic;
    GPI2           : in  std_logic_vector(C_GPI2_SIZE-1 downto 0);
    GPI2_Interrupt : out std_logic;
    GPI3           : in  std_logic_vector(C_GPI3_SIZE-1 downto 0);
    GPI3_Interrupt : out std_logic;
    GPI4           : in  std_logic_vector(C_GPI4_SIZE-1 downto 0);
    GPI4_Interrupt : out std_logic;

    -- Interrupt IO
    INTC_Interrupt         : in  std_logic_vector(C_INTC_INTR_SIZE-1 downto 0);
    INTC_IRQ               : out std_logic;
    INTC_Processor_Ack     : in  std_logic_vector(1 downto 0);
    INTC_Interrupt_Address : out std_logic_vector(C_ADDR_WIDTH-1 downto 0);

    -- Register access
    PIT1_Read          : in  std_logic;
    PIT1_Write_Preload : in  std_logic;
    PIT1_Write_Ctrl    : in  std_logic;
    PIT2_Read          : in  std_logic;
    PIT2_Write_Preload : in  std_logic;
    PIT2_Write_Ctrl    : in  std_logic;
    PIT3_Read          : in  std_logic;
    PIT3_Write_Preload : in  std_logic;
    PIT3_Write_Ctrl    : in  std_logic;
    PIT4_Read          : in  std_logic;
    PIT4_Write_Preload : in  std_logic;
    PIT4_Write_Ctrl    : in  std_logic;
    GPI1_Read          : in  std_logic;
    GPI2_Read          : in  std_logic;
    GPI3_Read          : in  std_logic;
    GPI4_Read          : in  std_logic;
    UART_TX_Write      : in  std_logic;
    UART_Baud_Write    : in  std_logic;
    GPO1_Write         : in  std_logic;
    GPO2_Write         : in  std_logic;
    GPO3_Write         : in  std_logic;
    GPO4_Write         : in  std_logic;
    UART_Status_Read   : in  std_logic;
    UART_Rx_Read       : in  std_logic;
    INTC_WRITE_CIAR    : in  std_logic;
    INTC_WRITE_CIER    : in  std_logic;
    INTC_WRITE_CIMR    : in  std_logic;
    INTC_WRITE_CIVAR   : in  std_logic;
    INTC_WRITE_CIVEAR  : in  std_logic;
    INTC_CIVAR_ADDR    : in  std_logic_vector(4 downto 0);
    INTC_READ_CISR     : in  std_logic;
    INTC_READ_CIPR     : in  std_logic;
    Write_Data         : in  std_logic_vector(31 downto 0);
    Read_Data          : out std_logic_vector(31 downto 0)
    );

end entity Iomodule_core;

library iomodule_v3_1_5;
use iomodule_v3_1_5.all;
use iomodule_v3_1_5.iomodule_vote_pkg.all;

architecture IMP of iomodule_core is

  component UART_Transmit is
    generic (
      C_TARGET          : TARGET_FAMILY_TYPE;
      C_TMR             : integer := 0;
      C_USE_TMR_DISABLE : integer := 0;
      C_VOTE_SIZE       : integer := 0;
      C_USE_SRL16       : string;
      C_DATA_BITS       : integer range 5 to 8;
      C_USE_PARITY      : integer;
      C_ODD_PARITY      : integer);
    port (
      Config_Reset : in std_logic;
      Clk          : in std_logic;
      Reset        : in boolean;
      EN_16x_Baud  : in std_logic;

      TMR_Disable         : in std_logic;
      FromAVote           : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
      FromBVote           : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
      ToVote              : out std_logic_vector(C_VOTE_SIZE-1 downto 0);
      TX                  : out std_logic;
      Write_TX_Data       : in  std_logic;
      TX_Data             : in  std_logic_vector(C_DATA_BITS-1 downto 0);
      TX_Data_Transmitted : out std_logic;
      TX_Buffer_Empty     : out std_logic);
  end component UART_Transmit;

  component UART_Receive is
    generic (
      C_TMR             : integer := 0;
      C_USE_TMR_DISABLE : integer := 0;
      C_VOTE_SIZE       : integer := 0;
      C_TARGET          : TARGET_FAMILY_TYPE;
      C_USE_SRL16       : string;
      C_DATA_BITS       : integer range 5 to 8;
      C_USE_PARITY      : integer;
      C_ODD_PARITY      : integer);
    port (
      Config_Reset : in std_logic;
      Clk          : in std_logic;
      Reset        : in boolean;
      EN_16x_Baud  : in std_logic;

      RX               : in  std_logic;
      Read_RX_Data     : in  std_logic;
      TMR_Disable      : in std_logic;
      FromAVote        : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
      FromBVote        : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
      ToVote           : out std_logic_vector(C_VOTE_SIZE-1 downto 0);
      RX_Data          : out std_logic_vector(C_DATA_BITS-1 downto 0);
      RX_Data_Received : out std_logic;
      RX_Data_Exists   : out std_logic;
      RX_Frame_Error   : out std_logic;
      RX_Overrun_Error : out std_logic;
      RX_Parity_Error  : out std_logic);
  end component UART_Receive;

  component Uart_Control_Status is
    generic (
      C_TMR             : integer := 0;
      C_USE_TMR_DISABLE : integer := 0;
      C_VOTE_SIZE       : integer := 0;
      C_USE_UART_RX     : integer;
      C_USE_UART_TX     : integer;
      C_UART_DATA_BITS  : integer range 5 to 8;
      C_UART_USE_PARITY : integer;
      C_UART_ODD_PARITY : integer);
    port (
      CLK   : in std_logic;
      Reset : in boolean;
      Config_Reset : in std_logic;      

      TMR_Disable : in std_logic;
      FromAVote   : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
      FromBVote   : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
      ToVote      : out std_logic_vector(C_VOTE_SIZE-1 downto 0);

      TX_Data_Transmitted : in std_logic;
      TX_Buffer_Empty     : in std_logic;
      RX_Data_Received    : in std_logic;
      RX_Data_Exists      : in std_logic;
      RX_Frame_Error      : in std_logic;
      RX_Overrun_Error    : in std_logic;
      RX_Parity_Error     : in std_logic;

      UART_Status_Read     : in  std_logic;
      UART_Status          : out std_logic_vector(7 downto 0);
      UART_Interrupt       : out std_logic;
      UART_Rx_Interrupt    : out std_logic;
      UART_Tx_Interrupt    : out std_logic;
      UART_Error_Interrupt : out std_logic);
  end component Uart_Control_Status;

  component FIT_Module is
    generic (
      C_TARGET          : TARGET_FAMILY_TYPE;
      C_TMR             : integer := 0;
      C_USE_TMR_DISABLE : integer := 0;
      C_VOTE_SIZE       : integer := 0;
      C_USE_SRL16       : string;
      C_USE_FIT         : integer;
      C_NO_CLOCKS       : integer;      -- The number of clocks between each interrupt
      C_INACCURACY      : integer);     -- The maximum inaccuracy of the number
    port (
      Config_Reset : in  std_logic;
      Clk          : in  std_logic;
      Reset        : in  boolean;
      TMR_Disable  : in std_logic;
      FromAVote    : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
      FromBVote    : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
      ToVote       : out std_logic_vector(C_VOTE_SIZE-1 downto 0);
      Toggle       : out std_logic;
      Interrupt    : out std_logic);
  end component FIT_Module;

  component PIT_Module is
    generic (
      C_TARGET          : TARGET_FAMILY_TYPE;
      C_TMR             : integer := 0;
      C_USE_TMR_DISABLE : integer := 0;
      C_VOTE_SIZE       : integer := 0;
      C_USE_PIT         : integer;
      C_PIT_SIZE        : integer;
      C_PIT_READABLE    : integer);
    port (
      Clk               : in  std_logic;
      Reset             : in  boolean;
      Config_Reset      : in  std_logic;
      TMR_Disable       : in std_logic;
      FromAVote         : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
      FromBVote         : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
      ToVote            : out std_logic_vector(C_VOTE_SIZE-1 downto 0);
      PIT_Count_En      : in  std_logic;
      PIT_Write_Preload : in  std_logic;
      PIT_Write_Ctrl    : in  std_logic;
      PIT_Read          : in  std_logic;
      Write_Data        : in  std_logic_vector(31 downto 0);
      PIT_Data          : out std_logic_vector(C_PIT_SIZE-1 downto 0);
      PIT_Toggle        : out std_logic;
      PIT_Interrupt     : out std_logic);
  end component PIT_Module;

  component GPO_Module is
    generic (
      C_TMR             : integer := 0;
      C_USE_TMR_DISABLE : integer := 0;
      C_VOTE_SIZE       : integer := 0;
      C_USE_GPO         : integer := 1;
      C_GPO_SIZE        : integer range 1 to 32 := 32;
      C_GPO_INIT        : std_logic_vector(31 downto 0) := (others => '0'));
    port (
      Clk         : in  std_logic;
      Reset       : in  boolean;
      GPO_Write   : in  std_logic;
      Write_Data  : in  std_logic_vector(31 downto 0);
      TMR_Disable : in std_logic;
      FromAVote   : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
      FromBVote   : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
      ToVote      : out std_logic_vector(C_VOTE_SIZE-1 downto 0);
      GPO         : out std_logic_vector(C_GPO_SIZE-1 downto 0));
  end component GPO_Module;

  component GPI_Module is
    generic (
      C_USE_GPI       : integer;
      C_GPI_SIZE      : integer;
      C_GPI_INTERRUPT : integer);
    port (
      Clk           : in  std_logic;
      Reset         : in  boolean;
      Config_Reset  : in  std_logic;
      GPI_Read      : in  std_logic;
      GPI           : in  std_logic_vector(C_GPI_SIZE-1 downto 0);
      GPI_In        : out std_logic_vector(C_GPI_SIZE-1 downto 0);
      GPI_Interrupt : out std_logic);
  end component GPI_Module;

  component intr_ctrl is
    generic (
      C_TARGET            : TARGET_FAMILY_TYPE;
      C_TMR               : integer := 0;
      C_USE_TMR_DISABLE   : integer := 0;
      C_VOTE_SIZE         : integer := 0;
      C_ADDR_WIDTH        : integer range 32 to 64 := 32;
      C_INTC_ENABLED      : std_logic_vector(31 downto 0);
      C_INTC_LEVEL_EDGE   : std_logic_vector(31 downto 0);
      C_INTC_POSITIVE     : std_logic_vector(31 downto 0);
      C_INTC_ASYNC_INTR   : std_logic_vector(31 downto 0);
      C_INTC_HAS_FAST     : integer range 0 to 1;
      C_INTC_ADDR_WIDTH   : integer range 5 to 64;
      C_INTC_NUM_SYNC_FF  : integer range 0 to 7;
      C_INTC_BASE_VECTORS : std_logic_vector(63 downto 0);
      C_USE_LUTRAM        : string);
    port (
      Clk               : in  std_logic;
      Reset             : in  boolean;
      TMR_Disable       : in  std_logic;
      FromAVote         : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
      FromBVote         : in  std_logic_vector(C_VOTE_SIZE-1 downto 0);
      ToVote            : out std_logic_vector(C_VOTE_SIZE-1 downto 0);
      INTR              : in  std_logic_vector(31 downto 0);
      INTR_ACK          : in  std_logic_vector(1 downto 0);
      INTR_ADDR         : out std_logic_vector(C_ADDR_WIDTH-1 downto 0);
      INTC_WRITE_CIAR   : in  std_logic;
      INTC_WRITE_CIER   : in  std_logic;
      INTC_WRITE_CIMR   : in  std_logic;
      INTC_WRITE_CIVAR  : in  std_logic;
      INTC_WRITE_CIVEAR : in  std_logic;
      INTC_CIVAR_ADDR   : in  std_logic_vector(4 downto 0);
      Write_Data        : in  std_logic_vector(31 downto 0);
      INTC_READ_CISR    : in  std_logic;
      INTC_READ_CIPR    : in  std_logic;
      INTC_IRQ          : out std_logic;
      INTC_CISR         : out std_logic_vector(31 downto 0);
      INTC_CIPR         : out std_logic_vector(31 downto 0));
  end component intr_ctrl;

  --------------------------------------------------------------------------------------------------
  -- Interrupt functions and constant calculation
  --------------------------------------------------------------------------------------------------
  function int2std (i : integer) return std_logic is
  begin  -- function int2std
    if (i = 0) then
      return '0';
    else
      return '1';
    end if;
  end function int2std;

  function INTR_IMPLEMENTED return std_logic_vector is
    variable t : std_logic_vector(31 downto 0);
  begin
    t(31 downto 16) := (others => '0');
    if (C_INTC_USE_EXT_INTR /= 0) then
      t(C_INTC_INTR_SIZE+15 downto 16) := (others => '1');
    end if;
    t(15) := '0';
    t(14) := int2std(C_GPI4_INTERRUPT);
    t(13) := int2std(C_GPI3_INTERRUPT);
    t(12) := int2std(C_GPI2_INTERRUPT);
    t(11) := int2std(C_GPI1_INTERRUPT);
    t(10) := int2std(C_FIT4_INTERRUPT);
    t(9)  := int2std(C_FIT3_INTERRUPT);
    t(8)  := int2std(C_FIT2_INTERRUPT);
    t(7)  := int2std(C_FIT1_INTERRUPT);
    t(6)  := int2std(C_PIT4_INTERRUPT);
    t(5)  := int2std(C_PIT3_INTERRUPT);
    t(4)  := int2std(C_PIT2_INTERRUPT);
    t(3)  := int2std(C_PIT1_INTERRUPT);
    t(2)  := int2std(C_UART_RX_INTERRUPT);
    t(1)  := int2std(C_UART_TX_INTERRUPT);
    t(0)  := int2std(C_UART_ERROR_INTERRUPT);
    return t;
  end function INTR_IMPLEMENTED;

  constant C_INTR_IMPL : std_logic_vector(31 downto 0) := INTR_IMPLEMENTED;

  -----------------------------------------------------------------------------
  -- bool_to_string conversion for C_AVOID_PRIMITIVES
  -----------------------------------------------------------------------------
  function bool_to_string (b : boolean) return string is
  begin  -- function bool_to_string
    if (b) then
      return "yes";
    else
      return "no";
    end if;
  end function bool_to_string;

  constant C_USE_SRL16  : string := bool_to_string(((C_AVOID_PRIMITIVES = 0) or (C_AVOID_PRIMITIVES = 2)) and (C_TARGET /= RTL));
  constant C_USE_LUTRAM : string := bool_to_string(((C_AVOID_PRIMITIVES = 0) or (C_AVOID_PRIMITIVES = 1)) and (C_TARGET /= RTL));

  signal reset   : boolean;
  --------------------------------------------------------------------------------------------------
  -- GPI signal
  --------------------------------------------------------------------------------------------------
  signal gpi1_in          : std_logic_vector(C_GPI1_SIZE-1 downto 0);
  signal gpi1_interrupt_i : std_logic;
  signal gpi2_in          : std_logic_vector(C_GPI2_SIZE-1 downto 0);
  signal gpi2_interrupt_i : std_logic;
  signal gpi3_in          : std_logic_vector(C_GPI3_SIZE-1 downto 0);
  signal gpi3_interrupt_i : std_logic;
  signal gpi4_in          : std_logic_vector(C_GPI4_SIZE-1 downto 0);
  signal gpi4_interrupt_i : std_logic;

  --------------------------------------------------------------------------------------------------
  -- Calculate FIT period for generating 16*C_UART_BAUDRATE
  --------------------------------------------------------------------------------------------------
  function FIT_PERIOD (FREQ : integer; BAUDRATE : integer ) return natural is
    constant C_BAUDRATE_16_BY_2 : integer := (16 * BAUDRATE) / 2;
    constant C_REMAINDER        : integer := FREQ rem (16 * BAUDRATE);
    constant C_RATIO            : integer := FREQ / (16 * BAUDRATE);
  begin
    if (C_BAUDRATE_16_BY_2 < C_REMAINDER) then
      return (C_RATIO + 1);
    else
      return C_RATIO;
    end if;
  end function FIT_PERIOD;

  --------------------------------------------------------------------------------------------------
  -- UART signals
  --------------------------------------------------------------------------------------------------
  signal en_16x_baud          : std_logic;
  signal tx_data_transmitted  : std_logic;
  signal tx_buffer_empty      : std_logic;
  signal uart_rx_data         : std_logic_vector(C_UART_DATA_BITS-1 downto 0);
  signal rx_data_received     : std_logic;
  signal rx_data_exists       : std_logic;
  signal rx_frame_error       : std_logic;
  signal rx_overrun_error     : std_logic;
  signal rx_parity_error      : std_logic;
  signal uart_status          : std_logic_vector(7 downto 0);
  signal uart_rx_interrupt    : std_logic;
  signal uart_tx_interrupt    : std_logic;
  signal uart_error_interrupt : std_logic;

  --------------------------------------------------------------------------------------------------
  -- FIT signals
  --------------------------------------------------------------------------------------------------
  signal fit1_interrupt_i : std_logic;
  signal fit2_interrupt_i : std_logic;
  signal fit3_interrupt_i : std_logic;
  signal fit4_interrupt_i : std_logic;

  --------------------------------------------------------------------------------------------------
  -- PIT signals
  --------------------------------------------------------------------------------------------------
  signal pit1_interrupt_i : std_logic;
  signal pit1_count_en    : std_logic;
  signal pit1_data        : std_logic_vector(C_PIT1_SIZE-1 downto 0);
  signal pit2_interrupt_i : std_logic;
  signal pit2_count_en    : std_logic;
  signal pit2_data        : std_logic_vector(C_PIT2_SIZE-1 downto 0);
  signal pit3_interrupt_i : std_logic;
  signal pit3_count_en    : std_logic;
  signal pit3_data        : std_logic_vector(C_PIT3_SIZE-1 downto 0);
  signal pit4_interrupt_i : std_logic;
  signal pit4_count_en    : std_logic;
  signal pit4_data        : std_logic_vector(C_PIT4_SIZE-1 downto 0);

  --------------------------------------------------------------------------------------------------
  -- INTC signals
  --------------------------------------------------------------------------------------------------
  signal intr      : std_logic_vector(31 downto 0);
  signal intc_cisr : std_logic_vector(31 downto 0);
  signal intc_cipr : std_logic_vector(31 downto 0);


  signal config_reset_i : std_logic;

  --------------------------------------------------------------------------------------------------
  -- TMR
  --------------------------------------------------------------------------------------------------
  signal ToVote_i      : std_logic_vector(VOTE_SIZE-1 downto 0);
  signal FromAVote_i   : std_logic_vector(VOTE_SIZE-1 downto 0);
  signal FromBVote_i   : std_logic_vector(VOTE_SIZE-1 downto 0);

begin  -- architecture IMP

  TMR_Yes : if (C_TMR /= 0) generate
  begin
    ToVote(ToVote_i'range)                     <= ToVote_i;
    ToVote(ToVote'high downto ToVote_i'high+1) <= (others => '0');
    FromAVote_i                                <= FromAVote(FromAVote_i'range);
    FromBVote_i                                <= FromBVote(FromBVote_i'range);
  end generate TMR_Yes;

  TMR_No : if (C_TMR = 0) generate
  begin
    ToVote      <= (others => '0');
    FromAVote_i <= (others => '0');
    FromBVote_i <= (others => '0');
  end generate TMR_No;

  Reset <= (Rst = '1');

  config_reset_i <= Config_Reset when C_USE_CONFIG_RESET /= 0 else '0';

  --------------------------------------------------------------------------------------------------
  -- UART Section
  --------------------------------------------------------------------------------------------------
  Using_UART_TX : if (C_USE_UART_TX /= 0) generate
  begin

    UART_TX_I1 : UART_Transmit
      generic map (
        C_TARGET          => C_TARGET,
        C_TMR             => C_TMR,
        C_USE_TMR_DISABLE => C_USE_TMR_DISABLE,
        C_VOTE_SIZE       => UART_TRANSMIT_Pos'high-UART_TRANSMIT_Pos'low+1,
        C_USE_SRL16       => C_USE_SRL16,
        C_DATA_BITS       => C_UART_DATA_BITS,
        C_USE_PARITY      => C_UART_USE_PARITY,
        C_ODD_PARITY      => C_UART_ODD_PARITY)
      port map (
        Config_Reset => config_reset_i,
        Clk          => Clk,
        Reset        => Reset,
        EN_16x_Baud  => en_16x_baud,

        TMR_Disable         => TMR_Disable,
        FromAVote           => FromAVote_i(UART_TRANSMIT_Pos),
        FromBVote           => FromBVote_i(UART_TRANSMIT_Pos),
        ToVote              => ToVote_i(UART_TRANSMIT_Pos),
        TX                  => UART_Tx,
        Write_TX_Data       => UART_TX_Write,
        TX_Data             => Write_Data(C_UART_DATA_BITS-1 downto 0),
        TX_Data_Transmitted => tx_data_transmitted,
        TX_Buffer_Empty     => tx_buffer_empty);
  end generate Using_UART_TX;

  No_UART_TX : if (C_USE_UART_TX = 0) generate
  begin
    tx_buffer_empty             <= '0';
    tx_data_transmitted         <= '0';
    UART_Tx                     <= '0';
    ToVote_i(UART_TRANSMIT_Pos) <= (others => '0');
  end generate No_UART_TX;

  Using_UART_RX : if (C_USE_UART_RX /= 0) generate
  begin

    UART_RX_I1 : UART_Receive
      generic map (
        C_TARGET          => C_TARGET,
        C_TMR             => C_TMR,
        C_USE_TMR_DISABLE => C_USE_TMR_DISABLE,
        C_VOTE_SIZE       => UART_RECEIVE_Pos'high-UART_RECEIVE_Pos'low+1,
        C_USE_SRL16       => C_USE_SRL16,
        C_DATA_BITS       => C_UART_DATA_BITS,
        C_USE_PARITY      => C_UART_USE_PARITY,
        C_ODD_PARITY      => C_UART_ODD_PARITY)
      port map (
        Config_Reset => config_reset_i,
        Clk          => Clk,
        Reset        => Reset,
        EN_16x_Baud  => en_16x_baud,

        RX               => UART_RX,
        Read_RX_Data     => UART_Rx_Read,
        TMR_Disable      => TMR_Disable,
        FromAVote        => FromAVote_i(UART_RECEIVE_Pos),
        FromBVote        => FromBVote_i(UART_RECEIVE_Pos),
        ToVote           => ToVote_i(UART_RECEIVE_Pos),
        RX_Data          => uart_rx_data,
        RX_Data_Received => rx_data_received,
        RX_Data_Exists   => rx_data_exists,
        RX_Frame_Error   => rx_frame_error,
        RX_Overrun_Error => rx_overrun_error,
        RX_Parity_Error  => rx_parity_error);
  end generate Using_UART_RX;

  No_UART_RX : if (C_USE_UART_RX = 0) generate
  begin
    uart_rx_data               <= (others => '0');
    rx_data_received           <= '0';
    rx_data_exists             <= '0';
    rx_frame_error             <= '0';
    rx_overrun_error           <= '0';
    rx_parity_error            <= '0';
    ToVote_i(UART_RECEIVE_Pos) <= (others => '0');
  end generate No_UART_RX;

  Using_UART : if ((C_USE_UART_RX /= 0) or (C_USE_UART_TX /= 0)) generate
  begin

    No_Dynamic_BaudRate: if C_UART_PROG_BAUDRATE = 0 generate
    begin
      UART_FIT_I : FIT_Module
        generic map (
          C_TARGET          => C_TARGET,
          C_TMR             => C_TMR,
          C_USE_TMR_DISABLE => C_USE_TMR_DISABLE,
          C_VOTE_SIZE       => UART_BAUD_FIT_Pos'high-UART_BAUD_FIT_Pos'low+1,
          C_USE_SRL16       => C_USE_SRL16,
          C_USE_FIT         => 1,
          C_NO_CLOCKS       => FIT_PERIOD(C_FREQ, C_UART_BAUDRATE),
          C_INACCURACY      => 0)
        port map (
          Config_Reset => config_reset_i,
          Clk          => Clk,
          Reset        => Reset,
          TMR_Disable  => TMR_Disable,
          FromAVote    => FromAVote_i(UART_BAUD_FIT_Pos),
          FromBVote    => FromBVote_i(UART_BAUD_FIT_Pos),
          ToVote       => ToVote_i(UART_BAUD_FIT_Pos),
          Toggle       => open,
          Interrupt    => en_16x_baud);

      ToVote_i(UART_BAUD_REG_Pos)  <= (others => '0');
      ToVote_i(UART_BAUD_CNT_Pos)  <= (others => '0');
      ToVote_i(UART_BAUD_EN16_Pos) <= '0';

    end generate No_Dynamic_BaudRate;

    Programmable_BaudRate_TMR_No: if C_UART_PROG_BAUDRATE /= 0 and C_TMR = 0 generate
      signal baudrate_cnt : std_logic_vector(C_UART_PROG_CNT_SIZE-1 downto 0);
      signal baudrate_reg : std_logic_vector(C_UART_PROG_CNT_SIZE-1 downto 0);
    begin

      ToVote_i(UART_BAUD_FIT_Pos) <= (others => '0');

      BaudRate_Counter : process (Clk)
      begin  -- process BaudRate_Counter
        if Clk'event and Clk = '1' then  -- rising clock edge
          if Reset then                  -- synchronous reset (active high)
            baudrate_reg <=
              std_logic_vector(to_unsigned(FIT_PERIOD(C_FREQ, C_UART_BAUDRATE) - 1, baudrate_reg'length));
            baudrate_cnt <= (others => '0');
            en_16x_baud  <= '0';
          else
            en_16x_baud <= '0';
            if baudrate_cnt = "00000000000000000000" then
              baudrate_cnt <= baudrate_reg;
              en_16x_baud  <= '1';
            else
              baudrate_cnt <= std_logic_vector(unsigned(baudrate_cnt) - 1);
            end if;
            if UART_Baud_Write = '1' then
              baudrate_reg <= Write_Data(baudrate_reg'range);
              baudrate_cnt <= (others => '0');
            end if;
          end if;
        end if;
      end process BaudRate_Counter;

      ToVote_i(UART_BAUD_REG_Pos)  <= (others => '0');
      ToVote_i(UART_BAUD_CNT_Pos)  <= (others => '0');
      ToVote_i(UART_BAUD_EN16_Pos) <= '0';

    end generate Programmable_BaudRate_TMR_No;

    Programmable_BaudRate_TMR_Yes: if C_UART_PROG_BAUDRATE /= 0 and C_TMR = 1 generate
      signal baudrate_cnt   : std_logic_vector(C_UART_PROG_CNT_SIZE-1 downto 0);
      signal baudrate_reg   : std_logic_vector(C_UART_PROG_CNT_SIZE-1 downto 0);
      signal baudrate_cnt_d : std_logic_vector(C_UART_PROG_CNT_SIZE-1 downto 0);
      signal baudrate_reg_d : std_logic_vector(C_UART_PROG_CNT_SIZE-1 downto 0);
      signal en_16x_baud_d  : std_logic;
      signal tmr_disable_b  : boolean;
    begin

      ToVote_i(UART_BAUD_FIT_Pos) <= (others => '0');

      tmr_disable_b <= TMR_Disable = '1' and C_USE_TMR_DISABLE = 1;

      BaudRate_Counter_Logic : process (Reset, baudrate_reg, baudrate_cnt, UART_Baud_Write, Write_Data)
      begin
        if Reset then
          baudrate_reg_d <=
            std_logic_vector(to_unsigned(FIT_PERIOD(C_FREQ, C_UART_BAUDRATE) - 1, baudrate_reg'length));
          baudrate_cnt_d <= (others => '0');
          en_16x_baud_d  <= '0';
        else
          if baudrate_cnt = "00000000000000000000" then
            baudrate_cnt_d <= baudrate_reg;
            en_16x_baud_d  <= '1';
          else
            baudrate_cnt_d <= std_logic_vector(unsigned(baudrate_cnt) - 1);
            en_16x_baud_d  <= '0';
          end if;
          if UART_Baud_Write = '1' then
            baudrate_reg_d <= Write_Data(baudrate_reg'range);
            baudrate_cnt_d <= (others => '0');
          else
            baudrate_reg_d <= baudrate_reg;
          end if;
        end if;
      end process BaudRate_Counter_Logic;

      ToVote_i(UART_BAUD_REG_Pos)  <= baudrate_reg_d;
      ToVote_i(UART_BAUD_CNT_Pos)  <= baudrate_cnt_d;
      ToVote_i(UART_BAUD_EN16_Pos) <= en_16x_baud_d;

      BaudRate_Counter_DFF : process (Clk)
      begin
        if Clk'event and Clk = '1' then
          baudrate_reg <= vote(baudrate_reg_d,
                               FromAVote_i(UART_BAUD_REG_Pos),
                               FromBVote_i(UART_BAUD_REG_Pos), tmr_disable_b);
          baudrate_cnt <= vote(baudrate_cnt_d,
                               FromAVote_i(UART_BAUD_CNT_Pos),
                               FromBVote_i(UART_BAUD_CNT_Pos), tmr_disable_b);
          en_16x_baud  <= vote(en_16x_baud_d,
                               FromAVote_i(UART_BAUD_EN16_Pos),
                               FromBVote_i(UART_BAUD_EN16_Pos), tmr_disable_b);
        end if;
      end process BaudRate_Counter_DFF;

    end generate Programmable_BaudRate_TMR_Yes;

    Uart_Control_Status_I1 : Uart_Control_Status
      generic map (
        C_TMR             => C_TMR,
        C_USE_TMR_DISABLE => C_USE_TMR_DISABLE,
        C_VOTE_SIZE       => UART_CONTROL_Pos'high-UART_CONTROL_Pos'low+1,
        C_USE_UART_RX     => C_USE_UART_RX,
        C_USE_UART_TX     => C_USE_UART_TX,
        C_UART_DATA_BITS  => C_UART_DATA_BITS,
        C_UART_USE_PARITY => C_UART_USE_PARITY,
        C_UART_ODD_PARITY => C_UART_ODD_PARITY)
      port map (
        CLK                  => CLK,
        Reset                => Reset,
        Config_Reset         => config_reset_i,
        TMR_Disable          => TMR_Disable,
        FromAVote            => FromAVote_i(UART_CONTROL_Pos),
        FromBVote            => FromBVote_i(UART_CONTROL_Pos),
        ToVote               => ToVote_i(UART_CONTROL_Pos),
        TX_Data_Transmitted  => tx_data_transmitted,
        TX_Buffer_Empty      => tx_buffer_empty,
        RX_Data_Received     => rx_data_received,
        RX_Data_Exists       => rx_data_exists,
        RX_Frame_Error       => rx_frame_error,
        RX_Overrun_Error     => rx_overrun_error,
        RX_Parity_Error      => rx_parity_error,
        UART_Status_Read     => UART_Status_Read,
        UART_Status          => uart_status,
        UART_Interrupt       => UART_Interrupt,
        UART_Rx_Interrupt    => uart_rx_interrupt,
        UART_Tx_Interrupt    => uart_tx_interrupt,
        UART_Error_Interrupt => uart_error_interrupt);
  end generate Using_UART;

  No_UART : if ((C_USE_UART_RX = 0) and (C_USE_UART_TX = 0)) generate
  begin
    uart_status                  <= (others => '0');
    UART_Interrupt               <= '0';
    uart_rx_interrupt            <= '0';
    uart_tx_interrupt            <= '0';
    uart_error_interrupt         <= '0';
    ToVote_i(UART_BAUD_FIT_Pos)  <= (others => '0');
    ToVote_i(UART_BAUD_REG_Pos)  <= (others => '0');
    ToVote_i(UART_BAUD_CNT_Pos)  <= (others => '0');
    ToVote_i(UART_BAUD_EN16_Pos) <= '0';
    ToVote_i(UART_CONTROL_Pos)   <= (others => '0');
  end generate No_UART;

  --------------------------------------------------------------------------------------------------
  -- FIT Section
  --------------------------------------------------------------------------------------------------
  FIT_I1 : FIT_Module
    generic map (
      C_TARGET          => C_TARGET,
      C_TMR             => C_TMR,
      C_USE_TMR_DISABLE => C_USE_TMR_DISABLE,
      C_VOTE_SIZE       => FIT1_Pos'high-FIT1_Pos'low+1,
      C_USE_SRL16       => C_USE_SRL16,
      C_USE_FIT         => C_USE_FIT1,
      C_NO_CLOCKS       => C_FIT1_NO_CLOCKS,
      C_INACCURACY      => 0)
    port map (
      Config_Reset => config_reset_i,
      Clk          => Clk,
      Reset        => Reset,
      TMR_Disable  => TMR_Disable,
      FromAVote    => FromAVote_i(FIT1_Pos),
      FromBVote    => FromBVote_i(FIT1_Pos),
      ToVote       => ToVote_i(FIT1_Pos),
      Toggle       => FIT1_Toggle,
      Interrupt    => fit1_interrupt_i);

  FIT1_Interrupt <= fit1_interrupt_i;

  FIT_I2 : FIT_Module
    generic map (
      C_TARGET          => C_TARGET,
      C_TMR             => C_TMR,
      C_USE_TMR_DISABLE => C_USE_TMR_DISABLE,
      C_VOTE_SIZE       => FIT2_Pos'high-FIT2_Pos'low+1,
      C_USE_SRL16       => C_USE_SRL16,
      C_USE_FIT         => C_USE_FIT2,
      C_NO_CLOCKS       => C_FIT2_NO_CLOCKS,
      C_INACCURACY      => 0)
    port map (
      Config_Reset => config_reset_i,
      Clk          => Clk,
      Reset        => Reset,
      TMR_Disable  => TMR_Disable,
      FromAVote    => FromAVote_i(FIT2_Pos),
      FromBVote    => FromBVote_i(FIT2_Pos),
      ToVote       => ToVote_i(FIT2_Pos),
      Toggle       => FIT2_Toggle,
      Interrupt    => fit2_interrupt_i);

  FIT2_Interrupt <= fit2_interrupt_i;

  FIT_I3 : FIT_Module
    generic map (
      C_TARGET          => C_TARGET,
      C_TMR             => C_TMR,
      C_USE_TMR_DISABLE => C_USE_TMR_DISABLE,
      C_VOTE_SIZE       => FIT3_Pos'high-FIT3_Pos'low+1,
      C_USE_SRL16       => C_USE_SRL16,
      C_USE_FIT         => C_USE_FIT3,
      C_NO_CLOCKS       => C_FIT3_NO_CLOCKS,
      C_INACCURACY      => 0)
    port map (
      Config_Reset => config_reset_i,
      Clk          => Clk,
      Reset        => Reset,
      TMR_Disable  => TMR_Disable,
      FromAVote    => FromAVote_i(FIT3_Pos),
      FromBVote    => FromBVote_i(FIT3_Pos),
      ToVote       => ToVote_i(FIT3_Pos),
      Toggle       => FIT3_Toggle,
      Interrupt    => fit3_interrupt_i);

  FIT3_Interrupt <= fit3_interrupt_i;

  FIT_I4 : FIT_Module
    generic map (
      C_TARGET          => C_TARGET,
      C_TMR             => C_TMR,
      C_USE_TMR_DISABLE => C_USE_TMR_DISABLE,
      C_VOTE_SIZE       => FIT4_Pos'high-FIT4_Pos'low+1,
      C_USE_SRL16       => C_USE_SRL16,
      C_USE_FIT         => C_USE_FIT4,
      C_NO_CLOCKS       => C_FIT4_NO_CLOCKS,
      C_INACCURACY      => 0)
    port map (
      Config_Reset => config_reset_i,
      Clk          => Clk,
      Reset        => Reset,
      TMR_Disable  => TMR_Disable,
      FromAVote    => FromAVote_i(FIT4_Pos),
      FromBVote    => FromBVote_i(FIT4_Pos),
      ToVote       => ToVote_i(FIT4_Pos),
      Toggle       => FIT4_Toggle,
      Interrupt    => fit4_interrupt_i);

  FIT4_Interrupt <= fit4_interrupt_i;

  --------------------------------------------------------------------------------------------------
  -- PIT Section
  --------------------------------------------------------------------------------------------------

  pit1_count_en <= '1'              when C_PIT1_PRESCALER = 0 else
                   fit1_interrupt_i when C_PIT1_PRESCALER = 1 else
                   fit2_interrupt_i when C_PIT1_PRESCALER = 2 else
                   fit3_interrupt_i when C_PIT1_PRESCALER = 3 else
                   fit4_interrupt_i when C_PIT1_PRESCALER = 4 else
                   pit2_interrupt_i when C_PIT1_PRESCALER = 6 else
                   pit3_interrupt_i when C_PIT1_PRESCALER = 7 else
                   pit4_interrupt_i when C_PIT1_PRESCALER = 8 else
                   PIT1_Enable      when C_PIT1_PRESCALER = 9 else
                   '0';

  PIT_I1 : PIT_Module
    generic map (
      C_TARGET          => C_TARGET,
      C_TMR             => C_TMR,
      C_USE_TMR_DISABLE => C_USE_TMR_DISABLE,
      C_VOTE_SIZE       => PIT1_Pos'high-PIT1_Pos'low+1,
      C_USE_PIT         => C_USE_PIT1,
      C_PIT_SIZE        => C_PIT1_SIZE,
      C_PIT_READABLE    => C_PIT1_READABLE)
    port map (
      Clk               => Clk,
      Reset             => Reset,
      Config_Reset      => config_reset_i,
      TMR_Disable       => TMR_Disable,
      FromAVote         => FromAVote_i(PIT1_Pos),
      FromBVote         => FromBVote_i(PIT1_Pos),
      ToVote            => ToVote_i(PIT1_Pos),
      PIT_Count_En      => pit1_count_en,
      PIT_Write_Preload => PIT1_Write_Preload,
      PIT_Write_Ctrl    => PIT1_Write_Ctrl,
      PIT_Read          => PIT1_Read,
      Write_Data        => Write_Data,
      PIT_Data          => pit1_data,
      PIT_Toggle        => PIT1_Toggle,
      PIT_Interrupt     => pit1_interrupt_i);

  PIT1_Interrupt <= pit1_interrupt_i;

  pit2_count_en <= '1'              when C_PIT2_PRESCALER = 0 else
                   fit1_interrupt_i when C_PIT2_PRESCALER = 1 else
                   fit2_interrupt_i when C_PIT2_PRESCALER = 2 else
                   fit3_interrupt_i when C_PIT2_PRESCALER = 3 else
                   fit4_interrupt_i when C_PIT2_PRESCALER = 4 else
                   pit1_interrupt_i when C_PIT2_PRESCALER = 5 else
                   pit3_interrupt_i when C_PIT2_PRESCALER = 7 else
                   pit4_interrupt_i when C_PIT2_PRESCALER = 8 else
                   PIT2_Enable      when C_PIT2_PRESCALER = 9 else
                   '0';

  PIT_I2 : PIT_Module
    generic map (
      C_TARGET          => C_TARGET,
      C_TMR             => C_TMR,
      C_USE_TMR_DISABLE => C_USE_TMR_DISABLE,
      C_VOTE_SIZE       => PIT2_Pos'high-PIT2_Pos'low+1,
      C_USE_PIT         => C_USE_PIT2,
      C_PIT_SIZE        => C_PIT2_SIZE,
      C_PIT_READABLE    => C_PIT2_READABLE)
    port map (
      Clk               => Clk,
      Reset             => Reset,
      Config_Reset      => config_reset_i,
      TMR_Disable       => TMR_Disable,
      FromAVote         => FromAVote_i(PIT2_Pos),
      FromBVote         => FromBVote_i(PIT2_Pos),
      ToVote            => ToVote_i(PIT2_Pos),
      PIT_Count_En      => pit2_count_en,
      PIT_Write_Preload => PIT2_Write_Preload,
      PIT_Write_Ctrl    => PIT2_Write_Ctrl,
      PIT_Read          => PIT2_Read,
      Write_Data        => Write_Data,
      PIT_Data          => pit2_data,
      PIT_Toggle        => PIT2_Toggle,
      PIT_Interrupt     => pit2_interrupt_i);

  PIT2_Interrupt <= pit2_interrupt_i;

  pit3_count_en <= '1'              when C_PIT3_PRESCALER = 0 else
                   fit1_interrupt_i when C_PIT3_PRESCALER = 1 else
                   fit2_interrupt_i when C_PIT3_PRESCALER = 2 else
                   fit3_interrupt_i when C_PIT3_PRESCALER = 3 else
                   fit4_interrupt_i when C_PIT3_PRESCALER = 4 else
                   pit1_interrupt_i when C_PIT3_PRESCALER = 5 else
                   pit2_interrupt_i when C_PIT3_PRESCALER = 6 else
                   pit4_interrupt_i when C_PIT3_PRESCALER = 8 else
                   PIT3_Enable      when C_PIT3_PRESCALER = 9 else
                   '0';

  PIT_I3 : PIT_Module
    generic map (
      C_TARGET          => C_TARGET,
      C_TMR             => C_TMR,
      C_USE_TMR_DISABLE => C_USE_TMR_DISABLE,
      C_VOTE_SIZE       => PIT3_Pos'high-PIT3_Pos'low+1,
      C_USE_PIT         => C_USE_PIT3,
      C_PIT_SIZE        => C_PIT3_SIZE,
      C_PIT_READABLE    => C_PIT3_READABLE)
    port map (
      Clk               => Clk,
      Reset             => Reset,
      Config_Reset      => config_reset_i,
      TMR_Disable       => TMR_Disable,
      FromAVote         => FromAVote_i(PIT3_Pos),
      FromBVote         => FromBVote_i(PIT3_Pos),
      ToVote            => ToVote_i(PIT3_Pos),
      PIT_Count_En      => pit3_count_en,
      PIT_Write_Preload => PIT3_Write_Preload,
      PIT_Write_Ctrl    => PIT3_Write_Ctrl,
      PIT_Read          => PIT3_Read,
      Write_Data        => Write_Data,
      PIT_Data          => pit3_data,
      PIT_Toggle        => PIT3_Toggle,
      PIT_Interrupt     => pit3_interrupt_i);

  PIT3_Interrupt <= pit3_interrupt_i;

  pit4_count_en <= '1'              when C_PIT4_PRESCALER = 0 else
                   fit1_interrupt_i when C_PIT4_PRESCALER = 1 else
                   fit2_interrupt_i when C_PIT4_PRESCALER = 2 else
                   fit3_interrupt_i when C_PIT4_PRESCALER = 3 else
                   fit4_interrupt_i when C_PIT4_PRESCALER = 4 else
                   pit1_interrupt_i when C_PIT4_PRESCALER = 5 else
                   pit2_interrupt_i when C_PIT4_PRESCALER = 6 else
                   pit3_interrupt_i when C_PIT4_PRESCALER = 7 else
                   PIT4_Enable      when C_PIT4_PRESCALER = 9 else
                   '0';

  PIT_I4 : PIT_Module
    generic map (
      C_TARGET          => C_TARGET,
      C_TMR             => C_TMR,
      C_USE_TMR_DISABLE => C_USE_TMR_DISABLE,
      C_VOTE_SIZE       => PIT4_Pos'high-PIT4_Pos'low+1,
      C_USE_PIT         => C_USE_PIT4,
      C_PIT_SIZE        => C_PIT4_SIZE,
      C_PIT_READABLE    => C_PIT4_READABLE)
    port map (
      Clk               => Clk,
      Reset             => Reset,
      Config_Reset      => config_reset_i,
      TMR_Disable       => TMR_Disable,
      FromAVote         => FromAVote_i(PIT4_Pos),
      FromBVote         => FromBVote_i(PIT4_Pos),
      ToVote            => ToVote_i(PIT4_Pos),
      PIT_Count_En      => pit4_count_en,
      PIT_Write_Preload => PIT4_Write_Preload,
      PIT_Write_Ctrl    => PIT4_Write_Ctrl,
      PIT_Read          => PIT4_Read,
      Write_Data        => Write_Data,
      PIT_Data          => pit4_data,
      PIT_Toggle        => PIT4_Toggle,
      PIT_Interrupt     => pit4_interrupt_i);

  PIT4_Interrupt <= pit4_interrupt_i;

  GPO_I1 : GPO_Module
    generic map (
      C_TMR             => C_TMR,
      C_USE_TMR_DISABLE => C_USE_TMR_DISABLE,
      C_VOTE_SIZE       => GPO1_Pos'high-GPO1_Pos'low+1,
      C_USE_GPO         => C_USE_GPO1,
      C_GPO_SIZE        => C_GPO1_SIZE,
      C_GPO_INIT        => C_GPO1_INIT)
    port map (
      Clk         => Clk,
      Reset       => Reset,
      GPO_Write   => GPO1_Write,
      Write_Data  => Write_Data,
      TMR_Disable => TMR_Disable,
      FromAVote   => FromAVote_i(GPO1_Pos),
      FromBVote   => FromBVote_i(GPO1_Pos),
      ToVote      => ToVote_i(GPO1_Pos),
      GPO         => GPO1);

  GPO_I2 : GPO_Module
    generic map (
      C_TMR             => C_TMR,
      C_USE_TMR_DISABLE => C_USE_TMR_DISABLE,
      C_VOTE_SIZE       => GPO2_Pos'high-GPO2_Pos'low+1,
      C_USE_GPO         => C_USE_GPO2,
      C_GPO_SIZE        => C_GPO2_SIZE,
      C_GPO_INIT        => C_GPO2_INIT)
    port map (
      Clk         => Clk,
      Reset       => Reset,
      GPO_Write   => GPO2_Write,
      Write_Data  => Write_Data,
      TMR_Disable => TMR_Disable,
      FromAVote   => FromAVote_i(GPO2_Pos),
      FromBVote   => FromBVote_i(GPO2_Pos),
      ToVote      => ToVote_i(GPO2_Pos),
      GPO         => GPO2);

  GPO_I3 : GPO_Module
    generic map (
      C_TMR             => C_TMR,
      C_USE_TMR_DISABLE => C_USE_TMR_DISABLE,
      C_VOTE_SIZE       => GPO3_Pos'high-GPO3_Pos'low+1,
      C_USE_GPO         => C_USE_GPO3,
      C_GPO_SIZE        => C_GPO3_SIZE,
      C_GPO_INIT        => C_GPO3_INIT)
    port map (
      Clk         => Clk,
      Reset       => Reset,
      GPO_Write   => GPO3_Write,
      Write_Data  => Write_Data,
      TMR_Disable => TMR_Disable,
      FromAVote   => FromAVote_i(GPO3_Pos),
      FromBVote   => FromBVote_i(GPO3_Pos),
      ToVote      => ToVote_i(GPO3_Pos),
      GPO         => GPO3);

  GPO_I4 : GPO_Module
    generic map (
      C_TMR             => C_TMR,
      C_USE_TMR_DISABLE => C_USE_TMR_DISABLE,
      C_VOTE_SIZE       => GPO4_Pos'high-GPO4_Pos'low+1,
      C_USE_GPO         => C_USE_GPO4,
      C_GPO_SIZE        => C_GPO4_SIZE,
      C_GPO_INIT        => C_GPO4_INIT)
    port map (
      Clk         => Clk,
      Reset       => Reset,
      GPO_Write   => GPO4_Write,
      Write_Data  => Write_Data,
      TMR_Disable => TMR_Disable,
      FromAVote   => FromAVote_i(GPO4_Pos),
      FromBVote   => FromBVote_i(GPO4_Pos),
      ToVote      => ToVote_i(GPO4_Pos),
      GPO         => GPO4);

  --------------------------------------------------------------------------------------------------
  -- GPI Section
  --------------------------------------------------------------------------------------------------
  GPI_I1 : GPI_Module
    generic map (
      C_USE_GPI       => C_USE_GPI1,
      C_GPI_SIZE      => C_GPI1_SIZE,
      C_GPI_INTERRUPT => C_GPI1_INTERRUPT)
    port map (
      Clk           => Clk,
      Reset         => Reset,
      Config_Reset  => config_reset_i,
      GPI_Read      => GPI1_Read,
      GPI           => GPI1,
      gpi_in        => gpi1_in,
      GPI_Interrupt => gpi1_interrupt_i);

  GPI1_Interrupt <= gpi1_interrupt_i;

  GPI_I2 : GPI_Module
    generic map (
      C_USE_GPI       => C_USE_GPI2,
      C_GPI_SIZE      => C_GPI2_SIZE,
      C_GPI_INTERRUPT => C_GPI2_INTERRUPT)
    port map (
      Clk           => Clk,
      Reset         => Reset,
      Config_Reset  => config_reset_i,
      GPI_Read      => GPI2_Read,
      GPI           => GPI2,
      gpi_in        => gpi2_in,
      GPI_Interrupt => gpi2_interrupt_i);

  GPI2_Interrupt <= gpi2_interrupt_i;

  GPI_I3 : GPI_Module
    generic map (
      C_USE_GPI       => C_USE_GPI3,
      C_GPI_SIZE      => C_GPI3_SIZE,
      C_GPI_INTERRUPT => C_GPI3_INTERRUPT)
    port map (
      Clk           => Clk,
      Reset         => Reset,
      Config_Reset  => config_reset_i,
      GPI_Read      => GPI3_Read,
      GPI           => GPI3,
      gpi_in        => gpi3_in,
      GPI_Interrupt => gpi3_interrupt_i);

  GPI3_Interrupt <= gpi3_interrupt_i;

  GPI_I4 : GPI_Module
    generic map (
      C_USE_GPI       => C_USE_GPI4,
      C_GPI_SIZE      => C_GPI4_SIZE,
      C_GPI_INTERRUPT => C_GPI4_INTERRUPT)
    port map (
      Clk           => Clk,
      Reset         => Reset,
      Config_Reset  => config_reset_i,
      GPI_Read      => GPI4_Read,
      GPI           => GPI4,
      gpi_in        => gpi4_in,
      GPI_Interrupt => gpi4_interrupt_i);

  GPI4_Interrupt <= gpi4_interrupt_i;

  --------------------------------------------------------------------------------------------------
  -- Interrupt Handler section
  --------------------------------------------------------------------------------------------------
  Intr_Assign : if C_INTC_INTR_SIZE < 16 generate
  begin
    intr(31 downto C_INTC_INTR_SIZE+16) <= (others => '0');
  end generate Intr_Assign;

  intr(C_INTC_INTR_SIZE+15 downto 16) <= INTC_Interrupt;
  intr(15)                            <= '0';
  intr(14)                            <= gpi4_interrupt_i;
  intr(13)                            <= gpi3_interrupt_i;
  intr(12)                            <= gpi2_interrupt_i;
  intr(11)                            <= gpi1_interrupt_i;
  intr(10)                            <= fit4_interrupt_i;
  intr(9)                             <= fit3_interrupt_i;
  intr(8)                             <= fit2_interrupt_i;
  intr(7)                             <= fit1_interrupt_i;
  intr(6)                             <= pit4_interrupt_i;
  intr(5)                             <= pit3_interrupt_i;
  intr(4)                             <= pit2_interrupt_i;
  intr(3)                             <= pit1_interrupt_i;
  intr(2)                             <= uart_rx_interrupt;
  intr(1)                             <= uart_tx_interrupt;
  intr(0)                             <= uart_error_interrupt;

  intr_ctrl_I1 : intr_ctrl
    generic map (
      C_TARGET            => C_TARGET,
      C_TMR               => C_TMR,
      C_USE_TMR_DISABLE   => C_USE_TMR_DISABLE,
      C_VOTE_SIZE         => IRQ_Pos'high-IRQ_Pos'low+1,
      C_ADDR_WIDTH        => C_ADDR_WIDTH,
      C_INTC_LEVEL_EDGE   => C_INTC_LEVEL_EDGE & X"FFFF",
      C_INTC_POSITIVE     => C_INTC_POSITIVE & X"FFFF",
      C_INTC_ASYNC_INTR   => C_INTC_ASYNC_INTR & X"0000",
      C_INTC_ENABLED      => C_INTR_IMPL,
      C_INTC_HAS_FAST     => C_INTC_HAS_FAST,
      C_INTC_ADDR_WIDTH   => C_INTC_ADDR_WIDTH,
      C_INTC_NUM_SYNC_FF  => C_INTC_NUM_SYNC_FF,
      C_INTC_BASE_VECTORS => C_INTC_BASE_VECTORS,
      C_USE_LUTRAM        => C_USE_LUTRAM)
    port map (
      Clk               => Clk,
      Reset             => Reset,
      TMR_Disable       => TMR_Disable,
      FromAVote         => FromAVote_i(IRQ_Pos),
      FromBVote         => FromBVote_i(IRQ_Pos),
      ToVote            => ToVote_i(IRQ_Pos),
      INTR              => intr,
      INTR_ACK          => INTC_Processor_Ack,
      INTR_ADDR         => INTC_Interrupt_Address,
      INTC_WRITE_CIAR   => INTC_WRITE_CIAR,
      INTC_WRITE_CIER   => INTC_WRITE_CIER,
      INTC_WRITE_CIMR   => INTC_WRITE_CIMR,
      INTC_WRITE_CIVAR  => INTC_WRITE_CIVAR,
      INTC_WRITE_CIVEAR => INTC_WRITE_CIVEAR,
      INTC_CIVAR_ADDR   => INTC_CIVAR_ADDR,
      Write_Data        => Write_Data,
      INTC_READ_CISR    => INTC_READ_CISR,
      INTC_READ_CIPR    => INTC_READ_CIPR,
      INTC_IRQ          => INTC_IRQ,
      INTC_CISR         => intc_cisr,
      INTC_CIPR         => intc_cipr);

  --------------------------------------------------------------------------------------------------
  -- Read MUX section
  --------------------------------------------------------------------------------------------------
  Simple_OR_Mux : process (gpi1_in, gpi2_in, gpi3_in, gpi4_in, intc_cipr, intc_cisr, pit1_data,
                           pit2_data, pit3_data, pit4_data, uart_rx_data, uart_status) is
    variable u1  : std_logic_vector(31 downto 0);
    variable u2  : std_logic_vector(31 downto 0);
    variable gi1 : std_logic_vector(31 downto 0);
    variable gi2 : std_logic_vector(31 downto 0);
    variable gi3 : std_logic_vector(31 downto 0);
    variable gi4 : std_logic_vector(31 downto 0);
    variable pi1 : std_logic_vector(31 downto 0);
    variable pi2 : std_logic_vector(31 downto 0);
    variable pi3 : std_logic_vector(31 downto 0);
    variable pi4 : std_logic_vector(31 downto 0);
  begin  -- process Simple_OR_Mux
    u1  := (others => '0');
    u2  := (others => '0');
    gi1 := (others => '0');
    gi2 := (others => '0');
    gi3 := (others => '0');
    gi4 := (others => '0');
    pi1 := (others => '0');
    pi2 := (others => '0');
    pi3 := (others => '0');
    pi4 := (others => '0');

    if (C_USE_UART_RX /= 0) then
      u1(uart_rx_data'range) := uart_rx_data;
    end if;

    if ((C_USE_UART_TX /= 0) or (C_USE_UART_RX /= 0)) then
      u2(uart_status'range) := uart_status;
    end if;

    if (C_USE_GPI1 /= 0) then
      gi1(gpi1_in'range) := gpi1_in;
    end if;

    if (C_USE_GPI2 /= 0) then
      gi2(gpi2_in'range) := gpi2_in;
    end if;

    if (C_USE_GPI3 /= 0) then
      gi3(gpi3_in'range) := gpi3_in;
    end if;

    if (C_USE_GPI4 /= 0) then
      gi4(gpi4_in'range) := gpi4_in;
    end if;

    if (C_USE_PIT1 /= 0) then
      pi1(pit1_data'range) := pit1_data;
    end if;

    if (C_USE_PIT2 /= 0) then
      pi2(pit2_data'range) := pit2_data;
    end if;

    if (C_USE_PIT3 /= 0) then
      pi3(pit3_data'range) := pit3_data;
    end if;

    if (C_USE_PIT4 /= 0) then
      pi4(pit4_data'range) := pit4_data;
    end if;

    Read_Data <= u1 or u2 or gi1 or gi2 or gi3 or gi4 or pi1 or pi2 or pi3 or pi4 or intc_cisr or intc_cipr;

  end process Simple_OR_Mux;

end architecture IMP;


-------------------------------------------------------------------------------
-- pselect_mask.vhd - Entity and architecture
-------------------------------------------------------------------------------
--
-- (c) Copyright 2011,2015 Xilinx, Inc. All rights reserved.
-- 
-- This file contains confidential and proprietary information
-- of Xilinx, Inc. and is protected under U.S. and 
-- international copyright and other intellectual property
-- laws.
-- 
-- DISCLAIMER
-- This disclaimer is not a license and does not grant any
-- rights to the materials distributed herewith. Except as
-- otherwise provided in a valid license issued to you by
-- Xilinx, and to the maximum extent permitted by applicable
-- law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND
-- WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES
-- AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
-- BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-
-- INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and
-- (2) Xilinx shall not be liable (whether in contract or tort,
-- including negligence, or under any other theory of
-- liability) for any loss or damage of any kind or nature
-- related to, arising under or in connection with these
-- materials, including for any direct, or any indirect,
-- special, incidental, or consequential loss or damage
-- (including loss of data, profits, goodwill, or any type of
-- loss or damage suffered as a result of any action brought
-- by a third party) even if such damage or loss was
-- reasonably foreseeable or Xilinx had been advised of the
-- possibility of the same.
-- 
-- CRITICAL APPLICATIONS
-- Xilinx products are not designed or intended to be fail-
-- safe, or for use in any application requiring fail-safe
-- performance, such as life-support or safety devices or
-- systems, Class III medical devices, nuclear facilities,
-- applications related to the deployment of airbags, or any
-- other applications that could lead to death, personal
-- injury, or severe property or environmental damage
-- (individually and collectively, "Critical
-- Applications"). Customer assumes the sole risk and
-- liability of any use of Xilinx products in Critical
-- Applications, subject only to applicable laws and
-- regulations governing limitations on product liability.
-- 
-- THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
-- PART OF THIS FILE AT ALL TIMES
--
------------------------------------------------------------------------------
-- Filename:        pselect_mask.vhd
--
-- Description:     
--                  
-- VHDL-Standard:   VHDL'93
-------------------------------------------------------------------------------
-- Structure:   
--              pselect_mask.vhd
--
-------------------------------------------------------------------------------
-- Author:          goran
-------------------------------------------------------------------------------
-- Naming Conventions:
--      active low signals:                     "*_n"
--      clock signals:                          "clk", "clk_div#", "clk_#x" 
--      reset signals:                          "rst", "rst_n" 
--      generics:                               "C_*" 
--      user defined types:                     "*_TYPE" 
--      state machine next state:               "*_ns" 
--      state machine current state:            "*_cs" 
--      combinatorial signals:                  "*_com" 
--      pipelined or register delay signals:    "*_d#" 
--      counter signals:                        "*cnt*"
--      clock enable signals:                   "*_ce" 
--      internal version of output port         "*_i"
--      device pins:                            "*_pin" 
--      ports:                                  - Names begin with Uppercase 
--      processes:                              "*_PROCESS" 
--      component instantiations:               "<ENTITY_>I_<#|FUNC>
-------------------------------------------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;
use IEEE.std_logic_unsigned.all;

entity pselect_mask is

  generic (
    C_AW   : integer                   := 32;
    C_BAR  : std_logic_vector(0 to 63) := X"0000000000020000";
    C_MASK : std_logic_vector(0 to 63) := X"000000000007C000"
    );
  port (
    A     : in  std_logic_vector(0 to C_AW-1);
    Valid : in  std_logic;
    CS    : out std_logic
    );

end entity pselect_mask;

architecture imp of pselect_mask is

  function Nr_Of_Ones (S : std_logic_vector) return natural is
    variable tmp : natural := 0;
  begin  -- function Nr_Of_Ones
    for I in S'range loop
      if (S(I) = '1') then
        tmp := tmp + 1;
      end if;
    end loop;  -- I
    return tmp;
  end function Nr_Of_Ones;

  function fix_AB (B : boolean; I : integer) return integer is
  begin  -- function fix_AB
    if (not B) then
      return I + 1;
    else
      return I;
    end if;
  end function fix_AB;

  constant Nr      : integer := Nr_Of_Ones(C_MASK(64 - C_AW to 63));
  constant Use_CIN : boolean := ((Nr mod 4) = 0);
  constant AB      : integer := fix_AB(Use_CIN, Nr);
  
  signal A_Bus : std_logic_vector(0 to AB);
  signal BAR   : std_logic_vector(0 to AB);

-------------------------------------------------------------------------------
-- Begin architecture section
-------------------------------------------------------------------------------
begin  -- VHDL_RTL

  Make_Busses : process (A,Valid) is
    variable tmp : natural;
  begin  -- process Make_Busses
    tmp   := 0;
    A_Bus <= (others => '0');
    BAR   <= (others => '0');
    for I in 0 to C_AW - 1 loop
      if (C_MASK(64 - C_AW + I) = '1') then
        A_Bus(tmp) <= A(I);
        BAR(tmp)   <= C_BAR(64 - C_AW + I);
        tmp        := tmp + 1;
      end if;
    end loop;  -- I
    if (not Use_CIN) then
      BAR(tmp) <= '1';
      A_Bus(tmp) <= Valid;
    end if;
  end process Make_Busses;

  CS <= Valid when A_Bus=BAR else '0';

end imp;


-------------------------------------------------------------------------------
-- iomodule.vhd - Entity and architecture
-------------------------------------------------------------------------------
--
-- (c) Copyright 2011-2015,2018 Xilinx, Inc. All rights reserved.
--
-- This file contains confidential and proprietary information
-- of Xilinx, Inc. and is protected under U.S. and 
-- international copyright and other intellectual property
-- laws.
--
-- DISCLAIMER
-- This disclaimer is not a license and does not grant any
-- rights to the materials distributed herewith. Except as
-- otherwise provided in a valid license issued to you by
-- Xilinx, and to the maximum extent permitted by applicable
-- law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND
-- WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES
-- AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
-- BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-
-- INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and
-- (2) Xilinx shall not be liable (whether in contract or tort,
-- including negligence, or under any other theory of
-- liability) for any loss or damage of any kind or nature
-- related to, arising under or in connection with these
-- materials, including for any direct, or any indirect,
-- special, incidental, or consequential loss or damage
-- (including loss of data, profits, goodwill, or any type of
-- loss or damage suffered as a result of any action brought
-- by a third party) even if such damage or loss was
-- reasonably foreseeable or Xilinx had been advised of the
-- possibility of the same.
--
-- CRITICAL APPLICATIONS
-- Xilinx products are not designed or intended to be fail-
-- safe, or for use in any application requiring fail-safe
-- performance, such as life-support or safety devices or
-- systems, Class III medical devices, nuclear facilities,
-- applications related to the deployment of airbags, or any
-- other applications that could lead to death, personal
-- injury, or severe property or environmental damage
-- (individually and collectively, "Critical
-- Applications"). Customer assumes the sole risk and
-- liability of any use of Xilinx products in Critical
-- Applications, subject only to applicable laws and
-- regulations governing limitations on product liability.
--
-- THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
-- PART OF THIS FILE AT ALL TIMES.
--
-------------------------------------------------------------------------------
-- Filename:        iomodule.vhd
--
-- Description:     
--                  
-- VHDL-Standard:   VHDL'93/02
-------------------------------------------------------------------------------
-- Structure:   
--              iomodule.vhd
--
-------------------------------------------------------------------------------
-- Author:          goran
--
-- History:
--   goran   2008-01-08    First Version
--   stefana 2012-03-20    Added GPI interrupt
--
-------------------------------------------------------------------------------
-- Naming Conventions:
--      active low signals:                     "*_n"
--      clock signals:                          "clk", "clk_div#", "clk_#x" 
--      reset signals:                          "rst", "rst_n" 
--      generics:                               "C_*" 
--      user defined types:                     "*_TYPE" 
--      state machine next state:               "*_ns" 
--      state machine current state:            "*_cs" 
--      combinatorial signals:                  "*_com" 
--      pipelined or register delay signals:    "*_d#" 
--      counter signals:                        "*cnt*"
--      clock enable signals:                   "*_ce" 
--      internal version of output port         "*_i"
--      device pins:                            "*_pin" 
--      ports:                                  - Names begin with Uppercase 
--      processes:                              "*_PROCESS" 
--      component instantiations:               "<ENTITY_>I_<#|FUNC>
-------------------------------------------------------------------------------
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;

library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_core;
use iomodule_v3_1_5.pselect_mask;

entity iomodule is
  generic (
    C_FAMILY                   : string                    := "Virtex7";
    C_FREQ                     : integer                   := 100000000;
    C_INSTANCE                 : string                    := "iomodule";
    C_USE_CONFIG_RESET         : integer                   := 0;
    C_AVOID_PRIMITIVES         : integer                   := 0;
    C_TMR                      : integer                   := 0;
    C_USE_TMR_DISABLE          : integer                   := 0;

    -- Local Memory Bus generics
    C_HIGHADDR                 : std_logic_vector(0 to 63) := X"0000000000000000";
    C_BASEADDR                 : std_logic_vector(0 to 63) := X"FFFFFFFFFFFFFFFF";
    C_MASK                     : std_logic_vector(0 to 63) := X"FFFFFFFFFFFFFFFF";
    C_IO_HIGHADDR              : std_logic_vector(0 to 63) := X"0000000000000000";
    C_IO_BASEADDR              : std_logic_vector(0 to 63) := X"FFFFFFFFFFFFFFFF";
    C_IO_MASK                  : std_logic_vector(0 to 63) := X"FFFFFFFFFFFFFFFF";
    C_LMB_AWIDTH               : integer                   := 32;
    C_LMB_DWIDTH               : integer                   := 32;
    C_LMB_PROTOCOL             : integer                   := 0;

    -- IO Bus
    C_USE_IO_BUS           : integer               := 0;
    
    -- UART generics
    C_USE_UART_RX          : integer               := 0;
    C_USE_UART_TX          : integer               := 0;
    C_UART_BAUDRATE        : integer               := 9600;
    C_UART_DATA_BITS       : integer range 5 to 8  := 8;
    C_UART_USE_PARITY      : integer               := 0;
    C_UART_ODD_PARITY      : integer               := 0;
    C_UART_RX_INTERRUPT    : integer               := 0;
    C_UART_TX_INTERRUPT    : integer               := 0;
    C_UART_ERROR_INTERRUPT : integer               := 0;
    C_UART_PROG_BAUDRATE   : integer               := 0;
    
    -- FIT generics
    C_USE_FIT1        : integer               := 0;
    C_FIT1_No_CLOCKS  : integer               := 6216;
    C_FIT1_INTERRUPT  : integer               := 0;
    C_USE_FIT2        : integer               := 0;
    C_FIT2_No_CLOCKS  : integer               := 6216;
    C_FIT2_INTERRUPT  : integer               := 0;
    C_USE_FIT3        : integer               := 0;
    C_FIT3_No_CLOCKS  : integer               := 6216;
    C_FIT3_INTERRUPT  : integer               := 0;
    C_USE_FIT4        : integer               := 0;
    C_FIT4_No_CLOCKS  : integer               := 6216;
    C_FIT4_INTERRUPT  : integer               := 0;

    -- PIT generics
    C_USE_PIT1       : integer               := 0;
    C_PIT1_SIZE      : integer range 1 to 32 := 32;
    C_PIT1_READABLE  : integer               := 1;
    C_PIT1_PRESCALER : integer range 0 to 9  := 0;
    C_PIT1_INTERRUPT : integer               := 0;
    C_USE_PIT2       : integer               := 0;
    C_PIT2_SIZE      : integer range 1 to 32 := 32;
    C_PIT2_READABLE  : integer               := 1;
    C_PIT2_PRESCALER : integer range 0 to 9  := 0;
    C_PIT2_INTERRUPT : integer               := 0;
    C_USE_PIT3       : integer               := 0;
    C_PIT3_SIZE      : integer range 1 to 32 := 32;
    C_PIT3_READABLE  : integer               := 1;
    C_PIT3_PRESCALER : integer range 0 to 9  := 0;
    C_PIT3_INTERRUPT : integer               := 0;
    C_USE_PIT4       : integer               := 0;
    C_PIT4_SIZE      : integer range 1 to 32 := 32;
    C_PIT4_READABLE  : integer               := 1;
    C_PIT4_PRESCALER : integer range 0 to 9  := 0;
    C_PIT4_INTERRUPT : integer               := 0;

    -- GPO Generics
    C_USE_GPO1  : integer := 0;
    C_GPO1_SIZE : integer range 1 to 32 := 32;
    C_GPO1_INIT : std_logic_vector(31 downto 0) := (others => '0');
    C_USE_GPO2  : integer := 0;
    C_GPO2_SIZE : integer range 1 to 32 := 32;
    C_GPO2_INIT : std_logic_vector(31 downto 0) := (others => '0');
    C_USE_GPO3  : integer := 0;
    C_GPO3_SIZE : integer range 1 to 32 := 32;
    C_GPO3_INIT : std_logic_vector(31 downto 0) := (others => '0');
    C_USE_GPO4  : integer := 0;
    C_GPO4_SIZE : integer range 1 to 32 := 32;
    C_GPO4_INIT : std_logic_vector(31 downto 0) := (others => '0');

    -- GPI Generics
    C_USE_GPI1       : integer := 0;
    C_GPI1_SIZE      : integer range 1 to 32 := 32;
    C_GPI1_INTERRUPT : integer               := 0;
    C_USE_GPI2       : integer := 0;
    C_GPI2_SIZE      : integer range 1 to 32 := 32;
    C_GPI2_INTERRUPT : integer               := 0;
    C_USE_GPI3       : integer := 0;
    C_GPI3_SIZE      : integer range 1 to 32 := 32;
    C_GPI3_INTERRUPT : integer               := 0;
    C_USE_GPI4       : integer := 0;
    C_GPI4_SIZE      : integer range 1 to 32 := 32;
    C_GPI4_INTERRUPT : integer               := 0;

    -- Interrupt Handler Generics
    C_INTC_USE_EXT_INTR : integer                       := 0;
    C_INTC_INTR_SIZE    : integer range 1 to 16         := 1;
    C_INTC_LEVEL_EDGE   : std_logic_vector(15 downto 0) := X"0000";
    C_INTC_POSITIVE     : std_logic_vector(15 downto 0) := X"FFFF";
    C_INTC_HAS_FAST     : integer range 0 to 1          := 0;
    C_INTC_ADDR_WIDTH   : integer range 5 to 64         := 32;
    C_INTC_BASE_VECTORS : std_logic_vector(63 downto 0) := X"0000000000000000";
    C_INTC_ASYNC_INTR   : std_logic_vector(15 downto 0) := X"FFFF";
    C_INTC_NUM_SYNC_FF  : integer range 0 to 7          := 2
    );
  port (
    Clk          : in std_logic;
    Rst          : in std_logic;
    Config_Reset : in std_logic := '0';
    TMR_Rst      : in std_logic;
    TMR_Disable  : in std_logic;
    
    -- TMR voting inbetween redundant IO Modules
    ToVote       : out std_logic_vector(1023 downto 0);
    FromAVote    : in  std_logic_vector(1023 downto 0);
    FromBVote    : in  std_logic_vector(1023 downto 0);

    -- IO Interface
    IO_Addr_Strobe  : out std_logic;
    IO_Read_Strobe  : out std_logic;
    IO_Write_Strobe : out std_logic;
    IO_Address      : out std_logic_vector(C_LMB_AWIDTH-1 downto 0);
    IO_Byte_Enable  : out std_logic_vector((C_LMB_DWIDTH/8 - 1) downto 0);
    IO_Write_Data   : out std_logic_vector(C_LMB_DWIDTH-1 downto 0);
    IO_Read_Data    : in  std_logic_vector(C_LMB_DWIDTH-1 downto 0);
    IO_Ready        : in  std_logic;

    -- UART I/O
    UART_Rx        : in  std_logic;
    UART_Tx        : out std_logic;
    UART_Interrupt : out std_logic;

    -- FIT I/O
    FIT1_Interrupt : out std_logic;
    FIT1_Toggle    : out std_logic;
    FIT2_Interrupt : out std_logic;
    FIT2_Toggle    : out std_logic;
    FIT3_Interrupt : out std_logic;
    FIT3_Toggle    : out std_logic;
    FIT4_Interrupt : out std_logic;
    FIT4_Toggle    : out std_logic;

    -- PIT I/O
    PIT1_Enable    : in  std_logic;
    PIT1_Interrupt : out std_logic;
    PIT1_Toggle    : out std_logic;
    PIT2_Enable    : in  std_logic;
    PIT2_Interrupt : out std_logic;
    PIT2_Toggle    : out std_logic;
    PIT3_Enable    : in  std_logic;
    PIT3_Interrupt : out std_logic;
    PIT3_Toggle    : out std_logic;
    PIT4_Enable    : in  std_logic;
    PIT4_Interrupt : out std_logic;
    PIT4_Toggle    : out std_logic;

    -- GPO IO
    GPO1 : out std_logic_vector(C_GPO1_SIZE-1 downto 0);
    GPO2 : out std_logic_vector(C_GPO2_SIZE-1 downto 0);
    GPO3 : out std_logic_vector(C_GPO3_SIZE-1 downto 0);
    GPO4 : out std_logic_vector(C_GPO4_SIZE-1 downto 0);

    -- GPI IO
    GPI1           : in  std_logic_vector(C_GPI1_SIZE-1 downto 0);
    GPI1_Interrupt : out std_logic;
    GPI2           : in  std_logic_vector(C_GPI2_SIZE-1 downto 0);
    GPI2_Interrupt : out std_logic;
    GPI3           : in  std_logic_vector(C_GPI3_SIZE-1 downto 0);
    GPI3_Interrupt : out std_logic;
    GPI4           : in  std_logic_vector(C_GPI4_SIZE-1 downto 0);
    GPI4_Interrupt : out std_logic;

    -- Interrupt IO
    INTC_Interrupt         : in  std_logic_vector(C_INTC_INTR_SIZE-1 downto 0);
    INTC_IRQ               : out std_logic;
    INTC_Processor_Ack     : in  std_logic_vector(1 downto 0);
    INTC_Interrupt_Address : out std_logic_vector(((C_INTC_ADDR_WIDTH+31) / 64) * (C_INTC_ADDR_WIDTH-32) + 31 downto 0);
    INTC_IRQ_OUT           : out std_logic;

    -- Local Memory Bus, LMB
    LMB_ABus        : in  std_logic_vector(0 to C_LMB_AWIDTH-1);
    LMB_WriteDBus   : in  std_logic_vector(0 to C_LMB_DWIDTH-1);
    LMB_AddrStrobe  : in  std_logic;
    LMB_ReadStrobe  : in  std_logic;
    LMB_WriteStrobe : in  std_logic;
    LMB_BE          : in  std_logic_vector(0 to (C_LMB_DWIDTH/8 - 1));
    Sl_DBus         : out std_logic_vector(0 to C_LMB_DWIDTH-1);
    Sl_Ready        : out std_logic;
    Sl_Wait         : out std_logic;
    Sl_UE           : out std_logic;
    Sl_CE           : out std_logic
    );

end entity iomodule;

library iomodule_v3_1_5;
use iomodule_v3_1_5.iomodule_funcs.all;

architecture IMP of iomodule is

  component iomodule_core is
    generic (
      C_TARGET           : TARGET_FAMILY_TYPE;
      C_FREQ             : integer := 100000000;
      C_USE_CONFIG_RESET : integer := 0;
      C_AVOID_PRIMITIVES : integer := 0;
      C_TMR              : integer := 0;
      C_USE_TMR_DISABLE  : integer := 0;

      -- UART generics
      C_USE_UART_RX          : integer;
      C_USE_UART_TX          : integer;
      C_UART_BAUDRATE        : integer;
      C_UART_DATA_BITS       : integer range 5 to 8;
      C_UART_USE_PARITY      : integer;
      C_UART_ODD_PARITY      : integer;
      C_UART_RX_INTERRUPT    : integer;
      C_UART_TX_INTERRUPT    : integer;
      C_UART_ERROR_INTERRUPT : integer;
      C_UART_PROG_BAUDRATE   : integer;

      -- FIT generics
      C_USE_FIT1       : integer;
      C_FIT1_No_CLOCKS : integer;
      C_FIT1_INTERRUPT : integer;
      C_USE_FIT2       : integer;
      C_FIT2_No_CLOCKS : integer;
      C_FIT2_INTERRUPT : integer;
      C_USE_FIT3       : integer;
      C_FIT3_No_CLOCKS : integer;
      C_FIT3_INTERRUPT : integer;
      C_USE_FIT4       : integer;
      C_FIT4_No_CLOCKS : integer;
      C_FIT4_INTERRUPT : integer;

      -- PIT generics
      C_USE_PIT1       : integer;
      C_PIT1_SIZE      : integer;
      C_PIT1_READABLE  : integer;
      C_PIT1_PRESCALER : integer range 0 to 9;
      C_PIT1_INTERRUPT : integer;
      C_USE_PIT2       : integer;
      C_PIT2_SIZE      : integer;
      C_PIT2_READABLE  : integer;
      C_PIT2_PRESCALER : integer range 0 to 9;
      C_PIT2_INTERRUPT : integer;
      C_USE_PIT3       : integer;
      C_PIT3_SIZE      : integer;
      C_PIT3_READABLE  : integer;
      C_PIT3_PRESCALER : integer range 0 to 9;
      C_PIT3_INTERRUPT : integer;
      C_USE_PIT4       : integer;
      C_PIT4_SIZE      : integer;
      C_PIT4_READABLE  : integer;
      C_PIT4_PRESCALER : integer range 0 to 9;
      C_PIT4_INTERRUPT : integer;

      -- GPO Generics
      C_USE_GPO1  : integer                       := 0;
      C_GPO1_SIZE : integer range 1 to 32         := 32;
      C_GPO1_INIT : std_logic_vector(31 downto 0) := (others => '0');
      C_USE_GPO2  : integer                       := 0;
      C_GPO2_SIZE : integer range 1 to 32         := 32;
      C_GPO2_INIT : std_logic_vector(31 downto 0) := (others => '0');
      C_USE_GPO3  : integer                       := 0;
      C_GPO3_SIZE : integer range 1 to 32         := 32;
      C_GPO3_INIT : std_logic_vector(31 downto 0) := (others => '0');
      C_USE_GPO4  : integer                       := 0;
      C_GPO4_SIZE : integer range 1 to 32         := 32;
      C_GPO4_INIT : std_logic_vector(31 downto 0) := (others => '0');

      -- GPI Generics
      C_USE_GPI1       : integer               := 0;
      C_GPI1_SIZE      : integer range 1 to 32 := 32;
      C_GPI1_INTERRUPT : integer               := 0;
      C_USE_GPI2       : integer               := 0;
      C_GPI2_SIZE      : integer range 1 to 32 := 32;
      C_GPI2_INTERRUPT : integer               := 0;
      C_USE_GPI3       : integer               := 0;
      C_GPI3_SIZE      : integer range 1 to 32 := 32;
      C_GPI3_INTERRUPT : integer               := 0;
      C_USE_GPI4       : integer               := 0;
      C_GPI4_SIZE      : integer range 1 to 32 := 32;
      C_GPI4_INTERRUPT : integer               := 0;

      -- Interrupt Handler Generics
      C_ADDR_WIDTH        : integer range 32 to 64;
      C_INTC_USE_EXT_INTR : integer;
      C_INTC_INTR_SIZE    : integer range 1 to 16;
      C_INTC_LEVEL_EDGE   : std_logic_vector(15 downto 0);
      C_INTC_POSITIVE     : std_logic_vector(15 downto 0);
      C_INTC_HAS_FAST     : integer range 0 to 1;
      C_INTC_ADDR_WIDTH   : integer range 5 to 64;
      C_INTC_BASE_VECTORS : std_logic_vector(63 downto 0);
      C_INTC_ASYNC_INTR   : std_logic_vector(15 downto 0) := X"FFFF";
      C_INTC_NUM_SYNC_FF  : integer range 0 to 7);
    port (
      Config_Reset : in std_logic := '0';
      CLK          : in std_logic;
      Rst          : in std_logic;
      TMR_Rst      : in std_logic;
      TMR_Disable  : in std_logic;

      -- TMR voting inbetween redundant IO Modules
      ToVote       : out std_logic_vector(1023 downto 0);
      FromAVote    : in  std_logic_vector(1023 downto 0);
      FromBVote    : in  std_logic_vector(1023 downto 0);

      -- UART I/O
      UART_Rx        : in  std_logic;
      UART_Tx        : out std_logic;
      UART_Interrupt : out std_logic;

      -- FIT I/O
      FIT1_Interrupt : out std_logic;
      FIT1_Toggle    : out std_logic;
      FIT2_Interrupt : out std_logic;
      FIT2_Toggle    : out std_logic;
      FIT3_Interrupt : out std_logic;
      FIT3_Toggle    : out std_logic;
      FIT4_Interrupt : out std_logic;
      FIT4_Toggle    : out std_logic;

      -- PIT I/O
      PIT1_Enable    : in  std_logic;
      PIT1_Interrupt : out std_logic;
      PIT1_Toggle    : out std_logic;
      PIT2_Enable    : in  std_logic;
      PIT2_Interrupt : out std_logic;
      PIT2_Toggle    : out std_logic;
      PIT3_Enable    : in  std_logic;
      PIT3_Interrupt : out std_logic;
      PIT3_Toggle    : out std_logic;
      PIT4_Enable    : in  std_logic;
      PIT4_Interrupt : out std_logic;
      PIT4_Toggle    : out std_logic;

      -- GPO IO
      GPO1 : out std_logic_vector(C_GPO1_SIZE-1 downto 0);
      GPO2 : out std_logic_vector(C_GPO2_SIZE-1 downto 0);
      GPO3 : out std_logic_vector(C_GPO3_SIZE-1 downto 0);
      GPO4 : out std_logic_vector(C_GPO4_SIZE-1 downto 0);

      -- GPI IO
      GPI1           : in  std_logic_vector(C_GPI1_SIZE-1 downto 0);
      GPI1_Interrupt : out std_logic;
      GPI2           : in  std_logic_vector(C_GPI2_SIZE-1 downto 0);
      GPI2_Interrupt : out std_logic;
      GPI3           : in  std_logic_vector(C_GPI3_SIZE-1 downto 0);
      GPI3_Interrupt : out std_logic;
      GPI4           : in  std_logic_vector(C_GPI4_SIZE-1 downto 0);
      GPI4_Interrupt : out std_logic;

      -- Interrupt IO
      INTC_Interrupt         : in  std_logic_vector(C_INTC_INTR_SIZE-1 downto 0);
      INTC_IRQ               : out std_logic;
      INTC_Processor_Ack     : in  std_logic_vector(1 downto 0);
      INTC_Interrupt_Address : out std_logic_vector(C_ADDR_WIDTH-1 downto 0);

      -- Register access
      PIT1_Read          : in  std_logic;
      PIT1_Write_Preload : in  std_logic;
      PIT1_Write_Ctrl    : in  std_logic;
      PIT2_Read          : in  std_logic;
      PIT2_Write_Preload : in  std_logic;
      PIT2_Write_Ctrl    : in  std_logic;
      PIT3_Read          : in  std_logic;
      PIT3_Write_Preload : in  std_logic;
      PIT3_Write_Ctrl    : in  std_logic;
      PIT4_Read          : in  std_logic;
      PIT4_Write_Preload : in  std_logic;
      PIT4_Write_Ctrl    : in  std_logic;
      GPI1_Read          : in  std_logic;
      GPI2_Read          : in  std_logic;
      GPI3_Read          : in  std_logic;
      GPI4_Read          : in  std_logic;
      UART_TX_Write      : in  std_logic;
      UART_Baud_Write    : in  std_logic;
      GPO1_Write         : in  std_logic;
      GPO2_Write         : in  std_logic;
      GPO3_Write         : in  std_logic;
      GPO4_Write         : in  std_logic;
      UART_Status_Read   : in  std_logic;
      UART_Rx_Read       : in  std_logic;
      INTC_WRITE_CIAR    : in  std_logic;
      INTC_WRITE_CIER    : in  std_logic;
      INTC_WRITE_CIMR    : in  std_logic;
      INTC_WRITE_CIVAR   : in  std_logic;
      INTC_WRITE_CIVEAR  : in  std_logic;
      INTC_CIVAR_ADDR    : in  std_logic_vector(4 downto 0);
      INTC_READ_CISR     : in  std_logic;
      INTC_READ_CIPR     : in  std_logic;
      Write_Data         : in  std_logic_vector(31 downto 0);
      Read_Data          : out std_logic_vector(31 downto 0));
  end component iomodule_core;

  component pselect_mask
    generic (
      C_AW   : integer                   := 32;
      C_BAR  : std_logic_vector(0 to 63) := X"0000000000000000";
      C_MASK : std_logic_vector(0 to 63) := X"0000000000800000");
    port (
      A     : in  std_logic_vector(0 to C_AW - 1);
      CS    : out std_logic;
      Valid : in  std_logic);
  end component;

  function c_use(size : integer) return integer is
  begin
    if size = 0 then
      return 0;
    else
      return 1;
    end if;
  end function c_use;

  -- Interrupt controller extended address
  constant C_INTC_HAS_AE : integer := Boolean'Pos(C_INTC_ADDR_WIDTH > 32);
  constant C_ADDR_WIDTH  : natural := ((C_INTC_ADDR_WIDTH + 31) / 64) * (C_INTC_ADDR_WIDTH - 32) + 32;

  -- Target
  constant C_TARGET : TARGET_FAMILY_TYPE := String_To_Family(C_FAMILY, false);
  
  -- Register Address Map
  constant C_UART_RX      : std_logic_vector(0 to 4) := "00000";
  constant C_UART_TX      : std_logic_vector(0 to 4) := "00001";
  constant C_UART_STATUS  : std_logic_vector(0 to 4) := "00010";
  constant C_IRQ_MODE     : std_logic_vector(0 to 4) := "00011";
  constant C_GPO1         : std_logic_vector(0 to 4) := "00100";
  constant C_GPO2         : std_logic_vector(0 to 4) := "00101";
  constant C_GPO3         : std_logic_vector(0 to 4) := "00110";
  constant C_GPO4         : std_logic_vector(0 to 4) := "00111";
  constant C_GPI1         : std_logic_vector(0 to 4) := "01000";
  constant C_GPI2         : std_logic_vector(0 to 4) := "01001";
  constant C_GPI3         : std_logic_vector(0 to 4) := "01010";
  constant C_GPI4         : std_logic_vector(0 to 4) := "01011";
  constant C_IRQ_STATUS   : std_logic_vector(0 to 4) := "01100";
  constant C_IRQ_PENDING  : std_logic_vector(0 to 4) := "01101";
  constant C_IRQ_ENABLE   : std_logic_vector(0 to 4) := "01110";
  constant C_IRQ_ACK      : std_logic_vector(0 to 4) := "01111";
  constant C_PIT1_PRELOAD : std_logic_vector(0 to 4) := "10000";
  constant C_PIT1_COUNTER : std_logic_vector(0 to 4) := "10001";
  constant C_PIT1_CONTROL : std_logic_vector(0 to 4) := "10010";
  constant C_UART_BAUD    : std_logic_vector(0 to 4) := "10011";
  constant C_PIT2_PRELOAD : std_logic_vector(0 to 4) := "10100";
  constant C_PIT2_COUNTER : std_logic_vector(0 to 4) := "10101";
  constant C_PIT2_CONTROL : std_logic_vector(0 to 4) := "10110";
  constant C_SPARE2       : std_logic_vector(0 to 4) := "10111";
  constant C_PIT3_PRELOAD : std_logic_vector(0 to 4) := "11000";
  constant C_PIT3_COUNTER : std_logic_vector(0 to 4) := "11001";
  constant C_PIT3_CONTROL : std_logic_vector(0 to 4) := "11010";
  constant C_SPARE3       : std_logic_vector(0 to 4) := "11011";
  constant C_PIT4_PRELOAD : std_logic_vector(0 to 4) := "11100";
  constant C_PIT4_COUNTER : std_logic_vector(0 to 4) := "11101";
  constant C_PIT4_CONTROL : std_logic_vector(0 to 4) := "11110";
  constant C_SPARE4       : std_logic_vector(0 to 4) := "11111";

  signal lmb_reg_select     : std_logic;
  signal lmb_io_select      : std_logic;
  signal lmb_io_select_keep : std_logic;
  signal lmb_abus_Q         : std_logic_vector(2 - C_INTC_HAS_FAST - C_INTC_HAS_AE to 6);
  signal lmb_reg_read       : std_logic;
  signal lmb_reg_read_Q     : std_logic;
  signal lmb_reg_write      : std_logic;
  signal io_ready_Q         : std_logic;
  signal io_bus_read_data   : std_logic_vector(C_LMB_DWIDTH-1 downto 0);
  
  -- Register access
  signal wen                : std_logic;
  signal regaddr            : std_logic_vector(0 to 4);
  signal pit1_read          : std_logic;
  signal pit1_write_preload : std_logic;
  signal pit1_write_ctrl    : std_logic;
  signal pit2_read          : std_logic;
  signal pit2_write_preload : std_logic;
  signal pit2_write_ctrl    : std_logic;
  signal pit3_read          : std_logic;
  signal pit3_write_preload : std_logic;
  signal pit3_write_ctrl    : std_logic;
  signal pit4_read          : std_logic;
  signal pit4_write_preload : std_logic;
  signal pit4_write_ctrl    : std_logic;
  signal gpi1_read          : std_logic;
  signal gpi2_read          : std_logic;
  signal gpi3_read          : std_logic;
  signal gpi4_read          : std_logic;
  signal uart_tx_write      : std_logic;
  signal gpo1_write         : std_logic;
  signal gpo2_write         : std_logic;
  signal gpo3_write         : std_logic;
  signal gpo4_write         : std_logic;
  signal uart_status_read   : std_logic;
  signal uart_rx_read       : std_logic;
  signal uart_baud_write    : std_logic;
  signal intc_write_ciar    : std_logic;
  signal intc_write_cier    : std_logic;
  signal intc_write_cimr    : std_logic;
  signal intc_write_civar   : std_logic;
  signal intc_write_civear  : std_logic;
  signal intc_read_cisr     : std_logic;
  signal intc_read_cipr     : std_logic;
  signal write_data         : std_logic_vector(31 downto 0);
  signal io_reg_read_data   : std_logic_vector(31 downto 0);

  signal fit1_interrupt_i : std_logic;
  signal fit2_interrupt_i : std_logic;
  signal fit3_interrupt_i : std_logic;
  signal fit4_interrupt_i : std_logic;
  signal pit1_interrupt_i : std_logic;
  signal pit2_interrupt_i : std_logic;
  signal pit3_interrupt_i : std_logic;
  signal pit4_interrupt_i : std_logic;

  signal intc_irq_i       : std_logic;

  signal One : std_logic;               -- tied to '1';

  -- Preserve signals after synthesis for simulation UART support
  attribute KEEP : string;
  attribute KEEP of uart_tx_write : signal is "TRUE";
  attribute KEEP of write_data    : signal is "TRUE";

  signal config_reset_i : std_logic;

begin  -- architecture IMP

  config_reset_i <= Config_Reset when C_USE_CONFIG_RESET /= 0 else '0';

  -----------------------------------------------------------------------------
  -- Do the LMB address decoding
  -----------------------------------------------------------------------------
  One <= '1';
  -- Detect if IO Module register is accessed
  pselect_mask_reg : pselect_mask
    generic map (
      C_AW   => LMB_ABus'length,
      C_BAR  => C_BASEADDR,
      C_MASK => C_MASK)
    port map (
      A     => LMB_ABus,
      CS    => lmb_reg_select,
      Valid => One);

  -- Remember address, read, write and write data
  AccessReg : process(Clk) is
  begin
    if (Clk'event and Clk = '1') then
      if config_reset_i = '1' then
        lmb_abus_Q <= (others => '0');

        lmb_reg_read_Q <= '0';
        lmb_reg_read   <= '0';
        lmb_reg_write  <= '0';

        Write_Data(31 downto 0) <= (others => '0');
      else
        lmb_abus_Q <= LMB_ABus(LMB_ABus'high-LMB_ABus_Q'length+1-2 to LMB_ABus'high-2);

        lmb_reg_read_Q <= lmb_reg_read;
        lmb_reg_read   <= LMB_ReadStrobe  and lmb_reg_select and LMB_AddrStrobe;
        lmb_reg_write  <= LMB_WriteStrobe and lmb_reg_select and LMB_AddrStrobe;

        Write_Data(31 downto 0) <= LMB_WriteDBus(0 to 31);  -- Data to write to IO module
      end if;
    end if;
  end process AccessReg;

  Using_IO_Bus : if (C_USE_IO_BUS /= 0) generate

    signal io_read_keep   : std_logic;

  begin
    
    -- Detect if IO Module IO extension is accessed
    pselect_mask_io : pselect_mask
      generic map (
        C_AW   => LMB_ABus'length,
        C_BAR  => C_IO_BASEADDR,
        C_MASK => C_IO_MASK)
      port map (
        A     => LMB_ABus,
        CS    => lmb_io_select,
        Valid => One);

    AccessIO : process(Clk) is
    begin
      if (Clk'event and Clk = '1') then
        if Rst = '1' or TMR_Rst = '1' then
          IO_Addr_Strobe     <= '0';
          IO_Read_Strobe     <= '0';
          IO_Write_Strobe    <= '0';
          IO_Address         <= (others => '0');
          IO_Byte_Enable     <= (others => '0');
          IO_Write_Data      <= (others => '0');
          lmb_io_select_keep <= '0';
          io_read_keep       <= '0';
        elsif lmb_io_select = '1' and LMB_AddrStrobe = '1' then
          IO_Addr_Strobe     <= '1';
          IO_Read_Strobe     <= LMB_ReadStrobe;
          IO_Write_Strobe    <= LMB_WriteStrobe;
          IO_Address         <= LMB_ABus;
          IO_Byte_Enable     <= LMB_BE;
          IO_Write_Data      <= LMB_WriteDBus;
          lmb_io_select_keep <= '1';
          io_read_keep       <= LMB_ReadStrobe;
        else
          if IO_Ready = '1' then
            lmb_io_select_keep <= '0';
            io_read_keep       <= '0';
          end if;
          IO_Addr_Strobe  <= '0';
          IO_Read_Strobe  <= '0';
          IO_Write_Strobe <= '0';
        end if;
      end if;
    end process AccessIO;

    ReadyReg : process(Clk) is
    begin
      if (Clk'event and Clk = '1') then
        if config_reset_i = '1' then
          io_ready_Q <= '0';
        else
          io_ready_Q <= lmb_io_select_keep and IO_Ready;
        end if;
      end if;
    end process ReadyReg;

    ReadDataReg : process(Clk) is
    begin
      if (Clk'event and Clk = '1') then
        if config_reset_i = '1' then
          io_bus_read_data <= (others => '0');
        else
          if IO_Ready = '1' and io_read_keep = '1' then
            io_bus_read_data <= IO_Read_Data;
          else
            io_bus_read_data <= (others => '0');
          end if;
        end if;
      end if;
    end process ReadDataReg;
    
  end generate Using_IO_Bus;
  
  Not_Using_IO_Bus : if (C_USE_IO_BUS = 0) generate
    io_ready_Q         <= '0';
    io_bus_read_data   <= (others => '0');
    lmb_io_select      <= '0';
    lmb_io_select_keep <= '0';
    IO_Addr_Strobe     <= '0';
    IO_Read_Strobe     <= '0';
    IO_Write_Strobe    <= '0';
    IO_Address         <= (others => '0');
    IO_Byte_Enable     <= (others => '0');
    IO_Write_Data      <= (others => '0');
  end generate Not_Using_IO_Bus;
  
  -- Data read from IO module

  -- Standard LMB protocol with RAM data read combinatorial through LMB controller 
  Sl_DBus_LMB_Protocol_0 : if (C_LMB_PROTOCOL = 0) generate
  begin
    Sl_DBus(0 to 31) <= io_reg_read_data(31 downto 0) or io_bus_read_data(31 downto 0);
  end generate Sl_DBus_LMB_Protocol_0;

  -- Timing optimized LMB protocol with RAM data read clocked in LMB controller
  -- Sl_DBus needs to be clocked once to match clocked timing of RAM data read clocked
  -- Only works with MicroBlaze 8-stage pipe and C_LMB_PROTOCOL set to 1 on MicroBlaze
  Sl_DBus_LMB_Protocol_1 : if (C_LMB_PROTOCOL = 1) generate
  begin
    Sl_DBus_DFF : process(Clk) is
    begin
      if (Clk'event and Clk = '1') then
        if config_reset_i = '1' then
          Sl_DBus <= (others => '0');
        else        
          Sl_DBus(0 to 31) <= io_reg_read_data(31 downto 0) or io_bus_read_data(31 downto 0);
        end if;
      end if;
    end process Sl_DBus_DFF;
  end generate Sl_DBus_LMB_Protocol_1;
  
  Sl_Wait <= lmb_reg_read or lmb_io_select_keep;

  Sl_UE   <= '0'; -- No Uncorrectable Errors
  Sl_CE   <= '0'; -- No Correctable Errors

  Sl_Ready <= '1' when lmb_reg_write  = '1' or lmb_reg_read_Q = '1' or io_ready_Q = '1' else
              '0';

  uart_rx_read       <= '1' when regaddr = C_UART_RX      and lmb_reg_read  = '1' else '0'; 
  uart_tx_write      <= wen when regaddr = C_UART_TX      and lmb_reg_write = '1' else '0';
  uart_status_read   <= '1' when regaddr = C_UART_STATUS  and lmb_reg_read  = '1' else '0';
  uart_baud_write    <= wen when regaddr = C_UART_BAUD    and lmb_reg_write = '1' else '0';
  intc_write_cimr    <= wen when regaddr = C_IRQ_MODE     and lmb_reg_write = '1' else '0';
  gpo1_write         <= wen when regaddr = C_GPO1         and lmb_reg_write = '1' else '0';
  gpo2_write         <= wen when regaddr = C_GPO2         and lmb_reg_write = '1' else '0';
  gpo3_write         <= wen when regaddr = C_GPO3         and lmb_reg_write = '1' else '0';
  gpo4_write         <= wen when regaddr = C_GPO4         and lmb_reg_write = '1' else '0';
  gpi1_read          <= '1' when regaddr = C_GPI1         and lmb_reg_read  = '1' else '0';
  gpi2_read          <= '1' when regaddr = C_GPI2         and lmb_reg_read  = '1' else '0';
  gpi3_read          <= '1' when regaddr = C_GPI3         and lmb_reg_read  = '1' else '0';
  gpi4_read          <= '1' when regaddr = C_GPI4         and lmb_reg_read  = '1' else '0';
  intc_write_ciar    <= wen when regaddr = C_IRQ_ACK      and lmb_reg_write = '1' else '0';
  intc_write_cier    <= wen when regaddr = C_IRQ_ENABLE   and lmb_reg_write = '1' else '0';
  intc_read_cisr     <= '1' when regaddr = C_IRQ_STATUS   and lmb_reg_read  = '1' else '0';
  intc_read_cipr     <= '1' when regaddr = C_IRQ_PENDING  and lmb_reg_read  = '1' else '0';
  pit1_read          <= '1' when regaddr = C_PIT1_COUNTER and lmb_reg_read  = '1' else '0';
  pit1_write_preload <= wen when regaddr = C_PIT1_PRELOAD and lmb_reg_write = '1' else '0';
  pit1_write_ctrl    <= wen when regaddr = C_PIT1_CONTROL and lmb_reg_write = '1' else '0';
  pit2_read          <= '1' when regaddr = C_PIT2_COUNTER and lmb_reg_read  = '1' else '0';
  pit2_write_preload <= wen when regaddr = C_PIT2_PRELOAD and lmb_reg_write = '1' else '0';
  pit2_write_ctrl    <= wen when regaddr = C_PIT2_CONTROL and lmb_reg_write = '1' else '0';
  pit3_read          <= '1' when regaddr = C_PIT3_COUNTER and lmb_reg_read  = '1' else '0';
  pit3_write_preload <= wen when regaddr = C_PIT3_PRELOAD and lmb_reg_write = '1' else '0';
  pit3_write_ctrl    <= wen when regaddr = C_PIT3_CONTROL and lmb_reg_write = '1' else '0';
  pit4_read          <= '1' when regaddr = C_PIT4_COUNTER and lmb_reg_read  = '1' else '0';
  pit4_write_preload <= wen when regaddr = C_PIT4_PRELOAD and lmb_reg_write = '1' else '0';
  pit4_write_ctrl    <= wen when regaddr = C_PIT4_CONTROL and lmb_reg_write = '1' else '0';

  Using_Fast : if C_INTC_HAS_FAST = 1 and C_INTC_HAS_AE = 0 generate
  begin
    intc_write_civar  <= lmb_reg_write when lmb_abus_Q(1) = '1' else '0';
    intc_write_civear <= '0';
    wen <= not lmb_abus_Q(1);
    regaddr <= lmb_abus_Q(2 to 6);
  end generate Using_Fast;

  Using_Fast_AE : if C_INTC_HAS_FAST = 1 and C_INTC_HAS_AE = 1 generate
  begin
    intc_write_civar  <= lmb_reg_write when (lmb_abus_Q(0) = '0' and lmb_abus_Q(1) = '1') or
                                            (lmb_abus_Q(0) = '1' and lmb_abus_Q(6) = '0') else '0';
    intc_write_civear <= lmb_reg_write when (lmb_abus_Q(0) = '1' and lmb_abus_Q(6) = '1') else '0';
    wen <= not (lmb_abus_Q(0) or lmb_abus_Q(1));
    regaddr <= lmb_abus_Q(2 to 6) when lmb_abus_Q(0) = '0' else
               lmb_abus_Q(1 to 5);
  end generate Using_Fast_AE;

  Not_Using_Fast : if C_INTC_HAS_FAST = 0 generate
  begin
    intc_write_civar  <= '0';
    intc_write_civear <= '0';
    wen <= '1';
    regaddr <= lmb_abus_Q(2 to 6);
  end generate Not_Using_Fast;


  IOModule_Core_I1: iomodule_core
    generic map (
      C_TARGET               => C_TARGET,
      C_FREQ                 => C_FREQ,
      C_USE_CONFIG_RESET     => C_USE_CONFIG_RESET,
      C_AVOID_PRIMITIVES     => C_AVOID_PRIMITIVES,
      C_TMR                  => C_TMR,
      C_USE_TMR_DISABLE      => C_USE_TMR_DISABLE,

      -- UART generics
      C_USE_UART_RX          => C_USE_UART_RX,           -- [integer]
      C_USE_UART_TX          => C_USE_UART_TX,           -- [integer]
      C_UART_BAUDRATE        => C_UART_BAUDRATE,         -- [integer]
      C_UART_DATA_BITS       => C_UART_DATA_BITS,        -- [integer range 5 to 8]
      C_UART_USE_PARITY      => C_UART_USE_PARITY,       -- [integer]
      C_UART_ODD_PARITY      => C_UART_ODD_PARITY,       -- [integer]
      C_UART_RX_INTERRUPT    => C_UART_RX_INTERRUPT,     -- [integer]
      C_UART_TX_INTERRUPT    => C_UART_TX_INTERRUPT,     -- [integer]
      C_UART_ERROR_INTERRUPT => C_UART_ERROR_INTERRUPT,  -- [integer]
      C_UART_PROG_BAUDRATE   => C_UART_PROG_BAUDRATE,    -- [integer]

      -- FIT generics
      C_USE_FIT1        => C_USE_FIT1,         -- [integer]
      C_FIT1_No_CLOCKS  => C_FIT1_No_CLOCKS,   -- [integer]
      C_FIT1_INTERRUPT  => C_FIT1_INTERRUPT,   -- [integer]
      C_USE_FIT2        => C_USE_FIT2,         -- [integer]
      C_FIT2_No_CLOCKS  => C_FIT2_No_CLOCKS,   -- [integer]
      C_FIT2_INTERRUPT  => C_FIT2_INTERRUPT,   -- [integer]
      C_USE_FIT3        => C_USE_FIT3,         -- [integer]
      C_FIT3_No_CLOCKS  => C_FIT3_No_CLOCKS,   -- [integer]
      C_FIT3_INTERRUPT  => C_FIT3_INTERRUPT,   -- [integer]
      C_USE_FIT4        => C_USE_FIT4,         -- [integer]
      C_FIT4_No_CLOCKS  => C_FIT4_No_CLOCKS,   -- [integer]
      C_FIT4_INTERRUPT  => C_FIT4_INTERRUPT,   -- [integer]

      -- PIT generics
      C_USE_PIT1           => C_USE_PIT1,            -- [integer]
      C_PIT1_SIZE          => C_PIT1_SIZE,           -- [integer]
      C_PIT1_READABLE      => C_PIT1_READABLE,       -- [integer]
      C_PIT1_PRESCALER     => C_PIT1_PRESCALER,      -- [integer range 0 to 9]
      C_PIT1_INTERRUPT     => C_PIT1_INTERRUPT,      -- [integer]
      C_USE_PIT2           => C_USE_PIT2,            -- [integer]
      C_PIT2_SIZE          => C_PIT2_SIZE,           -- [integer]
      C_PIT2_READABLE      => C_PIT2_READABLE,       -- [integer]
      C_PIT2_PRESCALER     => C_PIT2_PRESCALER,      -- [integer range 0 to 9]
      C_PIT2_INTERRUPT     => C_PIT2_INTERRUPT,      -- [integer]
      C_USE_PIT3           => C_USE_PIT3,            -- [integer]
      C_PIT3_SIZE          => C_PIT3_SIZE,           -- [integer]
      C_PIT3_READABLE      => C_PIT3_READABLE,       -- [integer]
      C_PIT3_PRESCALER     => C_PIT3_PRESCALER,      -- [integer range 0 to 9]
      C_PIT3_INTERRUPT     => C_PIT3_INTERRUPT,      -- [integer]
      C_USE_PIT4           => C_USE_PIT4,            -- [integer]
      C_PIT4_SIZE          => C_PIT4_SIZE,           -- [integer]
      C_PIT4_READABLE      => C_PIT4_READABLE,       -- [integer]
      C_PIT4_PRESCALER     => C_PIT4_PRESCALER,      -- [integer range 0 to 9]
      C_PIT4_INTERRUPT     => C_PIT4_INTERRUPT,      -- [integer]

      -- GPO Generics
      C_USE_GPO1  => C_USE_GPO1,
      C_GPO1_SIZE => C_GPO1_SIZE,
      C_GPO1_INIT => C_GPO1_INIT,
      C_USE_GPO2  => C_USE_GPO2,
      C_GPO2_SIZE => C_GPO2_SIZE,
      C_GPO2_INIT => C_GPO2_INIT,
      C_USE_GPO3  => C_USE_GPO3,
      C_GPO3_SIZE => C_GPO3_SIZE,
      C_GPO3_INIT => C_GPO3_INIT,
      C_USE_GPO4  => C_USE_GPO4,
      C_GPO4_SIZE => C_GPO4_SIZE,
      C_GPO4_INIT => C_GPO4_INIT,

      -- GPI Generics
      C_USE_GPI1       => C_USE_GPI1,
      C_GPI1_SIZE      => C_GPI1_SIZE,
      C_GPI1_INTERRUPT => C_GPI1_INTERRUPT,
      C_USE_GPI2       => C_USE_GPI2, 
      C_GPI2_SIZE      => C_GPI2_SIZE,
      C_GPI2_INTERRUPT => C_GPI2_INTERRUPT,
      C_USE_GPI3       => C_USE_GPI3, 
      C_GPI3_SIZE      => C_GPI3_SIZE,
      C_GPI3_INTERRUPT => C_GPI3_INTERRUPT,
      C_USE_GPI4       => C_USE_GPI4, 
      C_GPI4_SIZE      => C_GPI4_SIZE,
      C_GPI4_INTERRUPT => C_GPI4_INTERRUPT,

      -- Interrupt Handler Generics
      C_ADDR_WIDTH        => C_ADDR_WIDTH,
      C_INTC_USE_EXT_INTR => C_INTC_USE_EXT_INTR,
      C_INTC_INTR_SIZE    => C_INTC_INTR_SIZE,
      C_INTC_LEVEL_EDGE   => C_INTC_LEVEL_EDGE,
      C_INTC_POSITIVE     => C_INTC_POSITIVE,
      C_INTC_HAS_FAST     => C_INTC_HAS_FAST,
      C_INTC_ADDR_WIDTH   => C_INTC_ADDR_WIDTH,
      C_INTC_BASE_VECTORS => C_INTC_BASE_VECTORS,
      C_INTC_ASYNC_INTR   => C_INTC_ASYNC_INTR,
      C_INTC_NUM_SYNC_FF  => C_INTC_NUM_SYNC_FF)

    port map (
      Config_Reset => Config_Reset,
      CLK          => CLK,
      Rst          => Rst,
      TMR_Rst      => TMR_Rst,
      TMR_Disable  => TMR_Disable,

      -- TMR voting inbetween redundant IO Modules
      ToVote       => ToVote,
      FromAVote    => FromAVote,
      FromBVote    => FromBVote,

      -- UART I/O
      UART_Rx        => UART_Rx,
      UART_Tx        => UART_Tx,
      UART_Interrupt => UART_Interrupt,

      -- FIT I/O
      FIT1_Interrupt => fit1_interrupt_i,
      FIT1_Toggle    => FIT1_Toggle,
      FIT2_Interrupt => fit2_interrupt_i,
      FIT2_Toggle    => FIT2_Toggle,
      FIT3_Interrupt => fit3_interrupt_i,
      FIT3_Toggle    => FIT3_Toggle,
      FIT4_Interrupt => fit4_interrupt_i,
      FIT4_Toggle    => FIT4_Toggle,

      -- PIT I/O
      PIT1_Enable    => PIT1_Enable,
      PIT1_Interrupt => pit1_interrupt_i,
      PIT1_Toggle    => PIT1_Toggle,
      PIT2_Enable    => PIT2_Enable,
      PIT2_Interrupt => pit2_interrupt_i,
      PIT2_Toggle    => PIT2_Toggle,
      PIT3_Enable    => PIT3_Enable,
      PIT3_Interrupt => pit3_interrupt_i,
      PIT3_Toggle    => PIT3_Toggle,
      PIT4_Enable    => PIT4_Enable,
      PIT4_Interrupt => pit4_interrupt_i,
      PIT4_Toggle    => PIT4_Toggle,

      -- GPO IO
      GPO1 => GPO1,
      GPO2 => GPO2,
      GPO3 => GPO3,
      GPO4 => GPO4,

      -- GPI IO
      GPI1           => GPI1,
      GPI1_Interrupt => GPI1_Interrupt,
      GPI2           => GPI2,
      GPI2_Interrupt => GPI2_Interrupt,
      GPI3           => GPI3,
      GPI3_Interrupt => GPI3_Interrupt,
      GPI4           => GPI4,
      GPI4_Interrupt => GPI4_Interrupt,

      -- Interrupt IO
      INTC_Interrupt         => INTC_Interrupt,
      INTC_IRQ               => intc_irq_i,
      INTC_Processor_Ack     => INTC_Processor_Ack,
      INTC_Interrupt_Address => INTC_Interrupt_Address,

      -- Register access
      PIT1_Read          => pit1_read,
      PIT1_Write_Preload => pit1_write_preload,
      PIT1_Write_Ctrl    => pit1_write_ctrl,
      PIT2_Read          => pit2_read,
      PIT2_Write_Preload => pit2_write_preload,
      PIT2_Write_Ctrl    => pit2_write_ctrl,
      PIT3_Read          => pit3_read,
      PIT3_Write_Preload => pit3_write_preload,
      PIT3_Write_Ctrl    => pit3_write_ctrl,
      PIT4_Read          => pit4_read,
      PIT4_Write_Preload => pit4_write_preload,
      PIT4_Write_Ctrl    => pit4_write_ctrl,
      GPI1_Read          => gpi1_read,
      GPI2_Read          => gpi2_read,
      GPI3_Read          => gpi3_read,
      GPI4_Read          => gpi4_read,
      UART_TX_Write      => uart_tx_write,
      UART_Baud_Write    => uart_baud_write,
      GPO1_Write         => gpo1_write,
      GPO2_Write         => gpo2_write,
      GPO3_Write         => gpo3_write,
      GPO4_Write         => gpo4_write,
      UART_Status_Read   => uart_status_read,
      UART_Rx_Read       => uart_rx_read,
      INTC_WRITE_CIAR    => intc_write_ciar,
      INTC_WRITE_CIER    => intc_write_cier,
      INTC_WRITE_CIMR    => intc_write_cimr,
      INTC_WRITE_CIVAR   => intc_write_civar,
      INTC_WRITE_CIVEAR  => intc_write_civear,
      INTC_CIVAR_ADDR    => regaddr,
      INTC_READ_CISR     => intc_read_cisr,
      INTC_READ_CIPR     => intc_read_cipr,
      Write_Data         => write_data,
      Read_Data          => io_reg_read_data);

   INTC_IRQ     <= intc_irq_i;
   INTC_IRQ_OUT <= intc_irq_i;

   FIT1_Interrupt <= fit1_interrupt_i;
   FIT2_Interrupt <= fit2_interrupt_i;
   FIT3_Interrupt <= fit3_interrupt_i;
   FIT4_Interrupt <= fit4_interrupt_i;
   PIT1_Interrupt <= pit1_interrupt_i;
   PIT2_Interrupt <= pit2_interrupt_i;
   PIT3_Interrupt <= pit3_interrupt_i;
   PIT4_Interrupt <= pit4_interrupt_i;
  
end architecture IMP;