aboutsummaryrefslogtreecommitdiffstats
path: root/testsuite/gna/bug17309/polyamplib.vhdl
blob: 4793cd56068639f6604f075dba5632799c7d8137 (plain)
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
-----------------------------------------------------------------------------
-- Polyamp vhdl function library
-----------------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

package polyamplib is

  component interface_ads1271
    port (
      fastclk  : in  std_logic;         -- Fast clock
      adclk    : in  std_logic;         -- A/D converter clock
      resync   : in  std_logic;         -- Clock change; resync
      ser_data : in  std_logic;         -- Serial data in
      n_drdy   : in  std_logic;         -- Data is ready, active low
      data_out : out std_logic_vector(23 downto 0);  -- Parallell data out
      nsync    : out std_logic;         -- Synchronize (restart) A/D conv.
      busy     : out std_logic;         -- Busy reading serial data
      g_sclk   : out std_logic);        -- Gated SPI clock, FOR SIMULATION ONLY
  end component;

  component priority_resolver2 is
    port (
      inp    : in  std_logic_vector(11 downto 0);
      prio   : out unsigned(3 downto 0);  -- Output highest signal
      active : out std_logic);            -- High when any input is active
  end component;

  component one_of_n_encoder is
    port (
      number     : in  unsigned(3 downto 0);
      enable     : in  std_logic;
      single_out : out std_logic_vector(11 downto 0));  -- Only a single wire is active at a time
  end component;

  component active_input is
    port (
      fastclk    : in  std_logic;       -- Main clock
      enable     : in  std_logic;       -- Enable signal
      busy_n     : in  std_logic;       -- Busy input signal, active low
      clear      : in  std_logic;       -- Clear the corresponding flip-flop
      active_out : out std_logic);      -- Active output
  end component;

  component master_state_machine is
    port (
      fastclk    : in    std_logic := '0';               -- Main clock
      enable_lo8 : in    std_logic;     -- Enable the 8 low inputs
      busy_n_vec : in    std_logic_vector(11 downto 0);  -- Busy inputs
      id_code    : inout unsigned(3 downto 0);           -- Id code output
      fifo_write : out   std_logic);    -- Save to fifo
  end component;

  component fifo_memory is
    generic (
      fifo_size : natural := 100);
    port (
      clock   : in  std_logic;
      sclr    : in  std_logic;
      datain  : in  std_logic_vector(31 downto 0);
      wrreq   : in  std_logic;
      rdreq   : in  std_logic;
      dataout : out std_logic_vector(31 downto 0);
      full    : out std_logic;
      empty   : out std_logic;
      meter   : out unsigned(7 downto 0));
  end component;

  component fifo_ft_memory is
    generic (
      fifo_size : natural := 100);
    port (
      clock   : in  std_logic;
      sclr    : in  std_logic;
      datain  : in  std_logic_vector(31 downto 0);
      wrreq   : in  std_logic;
      rdreq   : in  std_logic;
      dataout : out std_logic_vector(31 downto 0);
      full    : out std_logic;
      empty   : out std_logic;
      meter   : out unsigned(7 downto 0));
  end component;

  component ads1271_model is
    port (
      analog_in : in    signed(23 downto 0);  -- Analog input signal
      clk       : in    std_logic;            -- Conversion clock
      sclk      : in    std_logic;            -- SPI clock
      n_sync    : in    std_logic;            -- Input sync signal, active low
      din       : in    std_logic;            -- Serial data in
      dout      : out   std_logic;            -- Serial data out
      n_drdy    : out   std_logic);           -- Data ready, active low
  end component;

  component sr_sipo is
    generic (
      LENGTH : positive := 8;           --! Shift register length
      EDGE   : natural  := 1;           --! Active edge; 1=positive, 0=negative
      DIR    : natural  := 1);          --! Direction; 1=left, 0=right
    port (
      clk     : in  std_logic;          --! Clock input
      ser_in  : in  std_logic;          --! Serial input data
      par_out : out std_logic_vector(1 to LENGTH));  --! Parallel output data
  end component;

  component sr_piso is
    generic (
      LENGTH : positive := 8;           --! Shift register length
      EDGE   : natural  := 1;           --! Active edge; 1=positive, 0=negative
      DIR    : natural  := 1);          --! Direction; 1=left, 0=right
    port (
      clk     : in  std_logic;          --! Clock input
      load    : in  std_logic;          --! Load on next clock
      par_in  : in  std_logic_vector(1 to LENGTH);  --! Parallel input data
      ser_in  : in  std_logic;          --! Serial input data
      ser_out : out std_logic);         --! Serial output data
  end component;

  component sr_piso_s is
    generic (
      LENGTH : positive := 8;           --! Shift register length
      DIR    : natural  := 1);          --! Direction; 1=left, 0=right
    port (
      fastclk : in  std_logic;          --! Synchronous clock
      clk_en  : in  std_logic;          --! Clock enable input
      load_en : in  std_logic;          --! Load enable input
      par_in  : in  std_logic_vector(1 to LENGTH);  --! Parallel input data
      ser_in  : in  std_logic;          --! Serial input data
      ser_out : out std_logic);         --! Serial output data
  end component;

  component spi_slave is
    port (
      -- Main controls
      fastclk : in  std_logic;
      spi_tx  : in  std_logic_vector(31 downto 0);  -- Data to be sent
      spi_rx  : out std_logic_vector(31 downto 0);  -- Data to be received
      spi_op  : out std_logic;          -- Read/Write status/data/command

      -- Slave port, connected to CPU
      mosi    : in  std_logic;
      miso    : out std_logic;
      sck     : in  std_logic;          -- SPI clock
      en_adc  : in  std_logic;          -- Active low, enable ADC
      en_incl : in  std_logic;          -- Active low, enable inclinometer

      -- Master port, connected to inclinometer
      incl_miso : in  std_logic;
      incl_mosi : out std_logic;
      incl_sck  : out std_logic;
      incl_ena  : out std_logic);       -- Active low, enable inclinometer
  end component;

  component command_decoder is
    port (
      addr_data  : in  std_logic_vector(31 downto 0);  -- Input address/data
      decode     : in  std_logic;       -- Single cycle decode pulse
      fastclk    : in  std_logic;       -- Master clock (not used for now)
      sel_nulcmd : out std_logic;       -- NULL command (no operation)
      sel_adclk0 : out std_logic;       -- Select sampling clock, ad0.
      sel_adclk1 : out std_logic;       -- Select sampling clock, ad1.
      sel_adclk2 : out std_logic;       -- Select sampling clock, ad2.
      sel_adclk3 : out std_logic;       -- Select sampling clock, ad3.
      sel_adclk4 : out std_logic;       -- Select sampling clock, ad4.
      sel_adclk5 : out std_logic;       -- Select sampling clock, ad5.
      sel_adclk6 : out std_logic;       -- Select sampling clock, ad6.
      sel_adclk7 : out std_logic;       -- Select sampling clock, ad7.
      resync_adc : out std_logic;       -- Resynchronize all ADC's
      write_ctrl : out std_logic;       -- Write to control-signal register
      start_adcs : out std_logic;       -- Start AD-conversion
      stop_adcs  : out std_logic);      -- Stop AD-conversion
  end component;

  component clockmux is
    port (
      clk24M  : in  std_logic;             -- Input clocks, 24 576 000
      clk4M   : in  std_logic;             -- 4 096 000
      clk2M   : in  std_logic;             -- 2 048 000
      clk1M   : in  std_logic;             -- 1 024 000
      clk512k : in  std_logic;             -- 512 000
      clk256k : in  std_logic;             -- 256 000
      clk128k : in  std_logic;             -- 128 000
      sel     : in  unsigned(2 downto 0);  -- Mux select input
      clkout  : out std_logic);            -- Output clock
  end component;

  component spi_master is
    port (
      -- Hardware ports
      miso     : in    std_logic;
      mosi     : out   std_logic;
      sck      : inout std_logic;
      en_adval : out   std_logic := '1';
      en_incl  : out   std_logic := '1';

      -- Simulation ports
      data_to_spi   : in  std_logic_vector(31 downto 0);
      data_from_spi : out std_logic_vector(31 downto 0);
      start         : in  std_logic;
      busy          : out std_logic := '0';
      running       : in  std_logic);
  end component;

  component spi_slave_burst is
    port (
      -- Main controls
      fastclk   : in  std_logic;
      spi_tx    : in  std_logic_vector(31 downto 0);  -- Data to be sent
      spi_rx    : out std_logic_vector(31 downto 0);  -- Data to be received
      exec_cmd  : out std_logic;                      -- Write command/data
      fifo_read : out std_logic;                      -- Read status/data

      -- Slave port, connected to CPU
      mosi    : in  std_logic;
      miso    : out std_logic;
      sck     : in  std_logic;          -- SPI clock
      en_adc  : in  std_logic;          -- Active low, enable ADC
      en_incl : in  std_logic;          -- Active low, enable inclinometer

      -- Master port, connected to inclinometer
      incl_miso : in  std_logic;
      incl_mosi : out std_logic;
      incl_sck  : out std_logic;
      incl_ena  : out std_logic);       -- Active low, enable inclinometer
  end component;

  component sync_logic_2 is
    port (
      start_adcs       : in  std_logic;   -- Start command
      stop_adcs        : in  std_logic;   -- Stop command
      reset            : in  std_logic;   -- Active high
      hwsync           : in  std_logic;   -- Hardware sync
      fastclk          : in  std_logic;   -- Master clock
      enable_adcvalues : out std_logic);  -- Enable reception of values
  end component;

end polyamplib;


-----------------------------------------------------------------------------
-- Shift register for adc_interface, spi_interface etc.
-----------------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;


--! General serial input, parallell output shift register

--! The shift register length is generic, from 2 and up. It can trigger on
--! positive or negative edges, and the shift direction can be choosen as well.
entity sr_sipo is
  generic (
    LENGTH : positive := 8;             --! Shift register length
    EDGE   : natural  := 1;             --! Active edge; 1=positive, 0=negative
    DIR    : natural  := 1);            --! Direction; 1=left, 0=right
  port (
    clk     : in  std_logic;            --! Clock input
    ser_in  : in  std_logic;            --! Serial input data
    par_out : out std_logic_vector(1 to LENGTH));  --! Parallel output data
end sr_sipo;

architecture sr_arch of sr_sipo is

  signal my_sr : std_logic_vector(1 to LENGTH);

begin  -- sr_arch
  
  posedge_right : if (EDGE = 1) and (DIR = 1) generate
    shift : process(clk)

    begin
      if clk'event and clk = '1' then   -- Positive clock edge
        my_sr(1 to LENGTH) <= (my_sr(2 to LENGTH) & ser_in);
      end if;
    end process shift;
  end generate posedge_right;

  posedge_left : if (EDGE = 1) and (DIR /= 1) generate
    shift : process(clk)

    begin
      if clk'event and clk = '1' then   -- Positive clock edge
        my_sr(1 to LENGTH) <= (ser_in & my_sr(1 to LENGTH-1));
      end if;
    end process shift;
  end generate posedge_left;

  negedge_right : if (EDGE /= 1) and (DIR = 1) generate
    shift : process(clk)

    begin
      if clk'event and clk = '0' then   -- Negative clock edge
        my_sr(1 to LENGTH) <= (my_sr(2 to LENGTH) & ser_in);
      end if;
    end process shift;
  end generate negedge_right;

  negedge_left : if (EDGE /= 1) and (DIR /= 1) generate
    shift : process(clk)

    begin
      if clk'event and clk = '0' then   -- Negative clock edge
        my_sr(1 to LENGTH) <= (ser_in & my_sr(1 to LENGTH-1));
      end if;
    end process shift;
  end generate negedge_left;

  par_out <= my_sr(1 to LENGTH) after 2 ns;

end sr_arch;





-----------------------------------------------------------------------------
-- Shift register for spi_interface
-----------------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;


--! General parallell input, serial output shift register

--! The shift register length is generic, from 2 and up. It can trigger on
--! positive or negative edges, and the shift direction can be choosen as well.

entity sr_piso is
  generic (
    LENGTH : positive := 8;             --! Shift register length
    EDGE   : natural  := 1;             --! Active edge; 1=positive, 0=negative
    DIR    : natural  := 1);            --! Direction; 1=left, 0=right
  port (
    clk     : in  std_logic;            --! Clock input
    load    : in  std_logic;            --! Load on next clock
    par_in  : in  std_logic_vector(1 to LENGTH);  --! Parallel input data
    ser_in  : in  std_logic;            --! Serial input data
    ser_out : out std_logic);           --! Serial output data
end sr_piso;

architecture sr_arch of sr_piso is

  signal my_sr : std_logic_vector(1 to LENGTH);

begin  -- sr_arch
  
  shift_left : if DIR = 1 generate
    ser_out <= my_sr(1) after 2 ns;
  end generate shift_left;

  shift_right : if DIR /= 1 generate
    ser_out <= my_sr(LENGTH) after 2 ns;
  end generate shift_right;

  posedge_left : if (EDGE = 1) and (DIR = 1) generate
    shift : process(clk)

    begin
      if clk'event and clk = '1' then   -- Positive clock edge
        if load = '1' then
          my_sr <= par_in;
        else
          my_sr(1 to (LENGTH-1)) <= my_sr(2 to LENGTH);
          my_sr(LENGTH)          <= ser_in;
        end if;
      end if;
    end process shift;
  end generate posedge_left;

  posedge_right : if (EDGE = 1) and (DIR /= 1) generate
    shift : process(clk)

    begin
      if clk'event and clk = '1' then   -- Positive clock edge
        if load = '1' then
          my_sr <= par_in;
        else
          my_sr(2 to LENGTH) <= my_sr(1 to LENGTH-1);
          my_sr(1)           <= ser_in;
        end if;
      end if;
    end process shift;
  end generate posedge_right;

  negedge_left : if (EDGE /= 1) and (DIR = 1) generate
    shift : process(clk)

    begin
      if clk'event and clk = '0' then   -- Negative clock edge
        if load = '1' then
          my_sr <= par_in;
        else
          my_sr(1 to (LENGTH-1)) <= my_sr(2 to LENGTH);
          my_sr(LENGTH)          <= ser_in;
        end if;
      end if;
    end process shift;
  end generate negedge_left;

  negedge_right : if (EDGE /= 1) and (DIR /= 1) generate
    shift : process(clk)

    begin
      if clk'event and clk = '0' then   -- Negative clock edge
        if load = '1' then
          my_sr <= par_in;
        else
          my_sr(2 to LENGTH) <= my_sr(1 to LENGTH-1);
          my_sr(1)           <= ser_in;
        end if;
      end if;
    end process shift;
  end generate negedge_right;

end sr_arch;




-----------------------------------------------------------------------------
-- Synchronously clocked shift register for spi_interface
-----------------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;


--! Synchronously clocked parallell input, serial output shift register

--! The shift register length is generic, from 2 and up.
--! The shift direction can be choosen.

entity sr_piso_s is
  generic (
    LENGTH : positive := 8;             --! Shift register length
    DIR    : natural  := 1);            --! Direction; 1=left, 0=right
  port (
    fastclk : in  std_logic;            --! Synchronous clock
    clk_en  : in  std_logic;            --! Clock enable input
    load_en : in  std_logic;            --! Load enable input
    par_in  : in  std_logic_vector(1 to LENGTH);  --! Parallel input data
    ser_in  : in  std_logic;            --! Serial input data
    ser_out : out std_logic);           --! Serial output data
end sr_piso_s;


architecture ar_piso_s_arch of sr_piso_s is

  signal my_sr : std_logic_vector(1 to LENGTH);

begin  -- ar_piso_s_arch

  shift_left_out : if DIR = 1 generate
    ser_out <= my_sr(1) after 2 ns;
  end generate shift_left_out;

  shift_right_out : if DIR /= 1 generate
    ser_out <= my_sr(LENGTH) after 2 ns;
  end generate shift_right_out;

  shift_left : if (DIR = 1) generate
    shift : process(fastclk)

    begin
      if rising_edge(fastclk) then      -- Positive clock edge
        if load_en = '1' then
          my_sr <= par_in;
        elsif clk_en = '1' then
          my_sr(1 to (LENGTH-1)) <= my_sr(2 to LENGTH);
          my_sr(LENGTH)          <= ser_in;
        end if;
      end if;
    end process shift;
  end generate shift_left;

  shift_right : if (DIR /= 1) generate
    shift : process(fastclk)

    begin
      if rising_edge(fastclk) then      -- Positive clock edge
        if load_en = '1' then
          my_sr <= par_in;
        elsif clk_en = '1' then
          my_sr(2 to LENGTH) <= my_sr(1 to LENGTH-1);
          my_sr(1)           <= ser_in;
        end if;
      end if;
    end process shift;
  end generate shift_right;
  

end ar_piso_s_arch;






-----------------------------------------------------------------------------
-- The adc_interface
-----------------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity interface_ads1271 is
  
  port (
    fastclk  : in  std_logic;           -- Fast clock
    adclk    : in  std_logic;           -- A/D converter clock
    resync   : in  std_logic;           -- Clock change; resync
    ser_data : in  std_logic;           -- Serial data in
    n_drdy   : in  std_logic;           -- Data is ready, active low
    data_out : out std_logic_vector(23 downto 0);  -- Parallell data out
    nsync    : out std_logic := '1';    -- Synchronize (restart) A/D conv.
    busy     : out std_logic;           -- Busy reading serial data
    g_sclk   : out std_logic);          -- Gated SPI clock, FOR SIMULATION ONLY

end interface_ads1271;


architecture interface_ads1271_arch of interface_ads1271 is

  type   state_type is (idle, counting, freeze);
  signal state         : state_type                    := idle;  -- Controlling state machine
  signal unlatch_data  : std_logic_vector(23 downto 0) := (others => '0');  -- From shift register
  signal bitcounter    : unsigned(4 downto 0)          := to_unsigned(0, 5);  -- Range 0..31
  signal g_sclk_enable : std_logic                     := '0';

  type   rsync_sm_type is (rs_idle, rs_clo1, rs_chi, rs_clo2);
  signal rsync_sm : rsync_sm_type := rs_idle;

  component sr_sipo is
    generic (
      LENGTH : positive;                --! Shift register length
      EDGE   : natural;                 --! Active edge; 1=positive, 0=negative
      DIR    : natural);                --! Direction; 1=left, 0=right
    port (
      clk     : in  std_logic;          --! Clock input
      ser_in  : in  std_logic;          --! Serial input data
      par_out : out std_logic_vector(1 to LENGTH));  --! Parallel output data
  end component;

  
begin  -- interface_ads1271_arch

  sr1 : sr_sipo generic map (
    LENGTH => 24,
    EDGE   => 1,
    DIR    => 1)
    port map (
      clk     => adclk,
      ser_in  => ser_data,
      par_out => unlatch_data);


-- Static interconnects
  g_sclk <= g_sclk_enable and adclk;



-- purpose: Freeze output data 
-- type   : sequential
-- inputs : adclk, n_drdy

  freeze_proc : process (adclk)
  begin  -- process freeze_proc
    if rising_edge(adclk) then          -- rising clock edge
      case state is
        when idle => if n_drdy = '0' then
                       busy          <= '1';
                       state         <= counting;
                       bitcounter    <= to_unsigned(23, 5);
                       g_sclk_enable <= '1';
                     else
                       busy <= '0';
                     end if;
        when counting => busy <= '1';
                         if bitcounter > 0 then
                           bitcounter <= bitcounter - 1;
                         else
                           state         <= freeze;
                           data_out      <= unlatch_data;
                           g_sclk_enable <= '0';
                         end if;
        when freeze => busy <= '0';
                       if n_drdy = '1' then
                         state <= idle;
                       end if;
        when others => state <= idle; busy <= '0';
      end case;
    end if;
  end process freeze_proc;



-- purpose: Handle resyncronization of A/D converters
--          Keep nsync low for at least one adclk cycle
-- type   : sequential
-- inputs : fastclk, adclk, resync
-- outputs: nsync
  resync_proc : process (fastclk)
  begin  -- process resync_proc
    if rising_edge(fastclk) then        -- rising clock edge
      nsync <= '0';
      case rsync_sm is
        when rs_idle => if resync = '1' then
                          rsync_sm <= rs_clo1;
                        else
                          nsync <= '1';
                        end if;
        when rs_clo1 => if adclk = '0' then
                          rsync_sm <= rs_chi;
                        end if;
        when rs_chi => if adclk = '1' then
                         rsync_sm <= rs_clo2;
                       end if;
    when rs_clo2 => if adclk = '0' then
                      rsync_sm <= rs_idle;
                    end if;
    when others => rsync_sm <= rs_idle;
    nsync                   <= '1';
  end case;
end if;
end process resync_proc;

end interface_ads1271_arch;




-----------------------------------------------------------------------------
-- The priority_resolver2
-----------------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity priority_resolver2 is
  port (
    inp    : in  std_logic_vector(11 downto 0);  -- Active low inputs
    prio   : out unsigned(3 downto 0);  -- Output highest signal
    active : out std_logic);            -- High when any input is active
end priority_resolver2;


architecture priority_resolver2_arch of priority_resolver2 is

begin  -- priority_resolver2_arch

  -- Any input active ???
  active <= inp(0) or inp(1) or inp(2) or inp(3) or inp(4) or inp(5) or
            inp(6) or inp(7) or inp(8) or inp(9) or inp(10) or inp(11) after 1 ns;

  -- purpose: Priority resolver
  -- type   : combinational
  -- inputs : inp
  -- outputs: prio, active
  mainloop : process (inp)
  begin  -- process mainloop
    if inp(11) = '1' then
      prio <= to_unsigned(11, 4) after 1 ns;
    elsif inp(10) = '1' then
      prio <= to_unsigned(10, 4) after 1 ns;
    elsif inp(9) = '1' then
      prio <= to_unsigned(9, 4) after 1 ns;
    elsif inp(8) = '1' then
      prio <= to_unsigned(8, 4) after 1 ns;
    elsif inp(7) = '1' then
      prio <= to_unsigned(7, 4) after 1 ns;
    elsif inp(6) = '1' then
      prio <= to_unsigned(6, 4) after 1 ns;
    elsif inp(5) = '1' then
      prio <= to_unsigned(5, 4) after 1 ns;
    elsif inp(4) = '1' then
      prio <= to_unsigned(4, 4) after 1 ns;
    elsif inp(3) = '1' then
      prio <= to_unsigned(3, 4) after 1 ns;
    elsif inp(2) = '1' then
      prio <= to_unsigned(2, 4) after 1 ns;
    elsif inp(1) = '1' then
      prio <= to_unsigned(1, 4) after 1 ns;
    else
      prio <= to_unsigned(0, 4) after 1 ns;
    end if;
  end process mainloop;
  
end priority_resolver2_arch;




-----------------------------------------------------------------------------
-- The active_input state machine
-----------------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity active_input is
  port (
    fastclk    : in  std_logic;         -- Main clock
    enable     : in  std_logic;         -- Enable signal
    busy_n     : in  std_logic;         -- Busy input signal, active low
    clear      : in  std_logic;         -- Clear the corresponding flip-flop
    active_out : out std_logic);        -- Active output
end active_input;


architecture active_input_arch of active_input is

  type   input_state_type is (i_idle, i_active, i_decay);
  signal i_state         : input_state_type := i_idle;
  signal temp_active_out : std_logic        := '0';  -- Temporary signal'


begin  -- active_input_arch

  -- Static delayed connex
  active_out <= temp_active_out after 1 ns;


  -- purpose: Input state machine
  -- type   : sequential
  -- inputs : fastclk, fastclk, busy_n, clear
  -- outputs: temp_active_out
  input_active : process (fastclk)

  begin  -- process input_active
    if rising_edge(fastclk) then        -- rising clock edge
      temp_active_out <= '0';
      case i_state is
        when i_idle => if (busy_n = '0') and (enable = '1') then
                         temp_active_out <= '1';
                         i_state         <= i_active;
                       end if;

        when i_active => if clear = '0' then
                           temp_active_out <= '1';
                         else
                           i_state <= i_decay;
                         end if;

        when i_decay => if busy_n = '1' then
                          i_state <= i_idle;
                        end if;
                        
        when others => i_state <= i_idle;
      end case;

    end if;
  end process input_active;
end active_input_arch;






-----------------------------------------------------------------------------
-- The one_of_n_encoder
-----------------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity one_of_n_encoder is
  
  port (
    number     : in  unsigned(3 downto 0);
    enable     : in  std_logic;
    single_out : out std_logic_vector(11 downto 0));  -- Only a single wire is active at a time

end one_of_n_encoder;



architecture one_of_n_encoder_arch of one_of_n_encoder is

  type     out_table_type is array (0 to 15) of std_logic_vector(11 downto 0);
  constant out_table : out_table_type :=
    ("000000000001",
     "000000000010",
     "000000000100",
     "000000001000",
     "000000010000",
     "000000100000",
     "000001000000",
     "000010000000",
     "000100000000",
     "001000000000",
     "010000000000",
     "100000000000",
     "000000000000",
     "000000000000",
     "000000000000",
     "000000000000");

begin  -- one_of_n_encoder_arch

  -- purpose: Translate unsigned number into a signle output
  -- type   : sequential
  -- inputs : number, number, enable
  -- outputs: single_out
  xlate : process (number, enable)
  begin  -- process xlate
    if enable = '1' then
      single_out <= out_table(to_integer(number)) after 1 ns;
    else
      single_out <= "000000000000" after 1 ns;
    end if;
    
  end process xlate;

end one_of_n_encoder_arch;








-----------------------------------------------------------------------------
-- The master_state_machine
-----------------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.polyamplib.all;

entity master_state_machine is
  
  port (

    fastclk    : in    std_logic := '0';               -- Main clock
    enable_lo8 : in    std_logic;       -- Enable the 8 low inputs
    busy_n_vec : in    std_logic_vector(11 downto 0);  -- Busy inputs
    id_code    : inout unsigned(3 downto 0);           -- Id code output
    fifo_write : out   std_logic);      -- Save to fifo

end master_state_machine;



architecture master_state_machine_arch of master_state_machine is

  signal active_data   : std_logic_vector(11 downto 0);
  signal clear_vec     : std_logic_vector(11 downto 0);
  signal any_active    : std_logic := '0';  -- Is any input active ???
  signal clear_enable  : std_logic := '0';  -- Clear the current active input flip-flop
  signal temp_clear_en : std_logic := '0';
  signal priority      : unsigned(3 downto 0);

  type   master_state_type is (m_idle, m_latch, m_clear, m_decay);
  signal m_state : master_state_type := m_idle;
  

  
begin  -- master_state_machine_arch

  -- The first 8 are controlled by the enable_lo8 signal
  gen1 : for i in 0 to 7 generate
    inp_sm : active_input port map (
      fastclk    => fastclk,
      enable     => enable_lo8,
      busy_n     => busy_n_vec(i),
      clear      => clear_vec(i),
      active_out => active_data(i));
  end generate gen1;

  -- The other inputs are always enabled
  gen2 : for i in 8 to 11 generate
    inp_sm : active_input port map (
      fastclk    => fastclk,
      enable     => '1',
      busy_n     => busy_n_vec(i),
      clear      => clear_vec(i),
      active_out => active_data(i));
  end generate gen2;

  resolve : priority_resolver2 port map (
    inp    => active_data,
    prio   => priority,
    active => any_active);

  encode : one_of_n_encoder port map (
    number     => id_code,
    enable     => clear_enable,
    single_out => clear_vec);


  clear_enable <= temp_clear_en after 1 ns;  -- After some delay
  fifo_write   <= clear_enable;              -- Same-same


  -- purpose: Master state machine
  -- type   : sequential
  -- inputs : fastclk, s0..s11,c0..c11
  -- outputs: fifo_write, data_id
  master_loop : process (fastclk)

  begin  -- process master_loop

    if rising_edge(fastclk) then        -- rising clock edge
      temp_clear_en <= '0';
      case m_state is
        when m_idle => if any_active = '1' then
                         id_code <= priority after 1 ns;
                         m_state <= m_latch;
                       end if;

        when m_latch =>
          temp_clear_en <= '1';
          m_state       <= m_clear;

        when m_clear =>
          m_state <= m_decay;

        when m_decay =>
          if any_active = '1' then
            id_code <= priority after 1 ns;
            m_state <= m_latch;
          else
            m_state <= m_idle;
          end if;

        when others => m_state <= m_idle;
      end case;
    end if;

  end process master_loop;
  
end master_state_machine_arch;





-----------------------------------------------------------------------------
-- The fifo_memory
-----------------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
--use work.polyamplib.all;

entity fifo_memory is
  generic (
    fifo_size : natural := 100);
  port (
    clock   : in  std_logic;
    sclr    : in  std_logic;
    datain  : in  std_logic_vector(31 downto 0);
    wrreq   : in  std_logic;
    rdreq   : in  std_logic;
    dataout : out std_logic_vector(31 downto 0);
    full    : out std_logic;
    empty   : out std_logic;
    meter   : out unsigned(7 downto 0));
end fifo_memory;



architecture fifo_memory_arch of fifo_memory is

begin  -- fifo_memory_arch

  -- purpose: Fifo memory simulator
  -- type   : sequential
  -- inputs : clock, datain, wrreq, rdreq, clock
  -- outputs: dataout, full, empty
  fifo_mem : process (clock)

    subtype  word is std_logic_vector(31 downto 0);
    type     memory_area is array(0 to fifo_size) of word;
    variable fifo : memory_area;        -- The fifo memory area

    variable head_ix : natural := 0;    -- Write data to this address
    variable tail_ix : natural := 0;    -- Read data from this address
    variable test_ix : natural := 0;    -- Used for test of overflow/underflow
    
  begin  -- process fifo
    if rising_edge(clock) then          -- rising clock edge

      -- Synchronous clear
      if sclr = '1' then
        head_ix := 0;
        tail_ix := 0;
      elsif wrreq = '1' then            -- Fifo write op's
        test_ix := head_ix + 1;
        if test_ix > fifo_size then
          test_ix := 0;
        end if;
        -- Writing to full fifo discards the last value
        if test_ix /= tail_ix then      -- NOT full
          fifo(head_ix) := datain;      -- Write data
          head_ix       := test_ix;     -- And adjust the pointer
        end if;
      end if;

      -- Reading empty fifo returns the last value
      if (rdreq = '1') and (head_ix /= tail_ix) then
        dataout <= fifo(tail_ix) after 1 ns;
        tail_ix := tail_ix + 1;
        if tail_ix > fifo_size then
          tail_ix := 0;
        end if;
      end if;

      -- Fifo empty signal
      if head_ix = tail_ix then
        empty <= '1' after 1 ns;
      else
        empty <= '0' after 1 ns;
      end if;

      -- Fifo full signal
      if (tail_ix = (head_ix+1)) or ((tail_ix = 0) and (head_ix = fifo_size)) then
        full <= '1' after 1 ns;
      else
        full <= '0' after 1 ns;
      end if;

      -- Fifo fill meter operations
      if head_ix >= tail_ix then
        meter <= to_unsigned(head_ix - tail_ix, 8);
      else
        meter <= to_unsigned(fifo_size + 1 + head_ix - tail_ix, 8);
      end if;
      
    end if;
  end process fifo_mem;

end fifo_memory_arch;






-----------------------------------------------------------------------------
-- The fifo_ft_memory
-----------------------------------------------------------------------------

-- Fallthrough fifo memory model


library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
--use work.polyamplib.all;

entity fifo_ft_memory is
  generic (
    fifo_size : natural := 100);
  port (
    clock   : in  std_logic;
    sclr    : in  std_logic;
    datain  : in  std_logic_vector(31 downto 0);
    wrreq   : in  std_logic;
    rdreq   : in  std_logic;
    dataout : out std_logic_vector(31 downto 0);
    full    : out std_logic;
    empty   : out std_logic;
    meter   : out unsigned(7 downto 0));
end fifo_ft_memory;



architecture fifo_ft_memory_arch of fifo_ft_memory is

begin  -- fifo_ft_memory_arch

  -- purpose: Fifo memory simulator
  -- type   : sequential
  -- inputs : clock, datain, wrreq, rdreq, clock
  -- outputs: dataout, full, empty
  fifo_mem : process (clock)

    subtype  word is std_logic_vector(31 downto 0);
    type     memory_area is array(0 to fifo_size) of word;
    variable fifo : memory_area;        -- The fifo memory area

    variable head_ix : natural := 0;    -- Write data to this address
    variable tail_ix : natural := 0;    -- Read data from this address
    variable test_ix : natural := 0;    -- Used for test of overflow/underflow

  begin  -- process fifo
    if rising_edge(clock) then          -- rising clock edge

      -- Synchronous clear
      if sclr = '1' then
        head_ix := 0;
        tail_ix := 0;
      else
        -- Write to fifo
        if wrreq = '1' then             -- Fifo write op's
          test_ix := head_ix + 1;
          if test_ix > fifo_size then
            test_ix := 0;
          end if;

          -- Fifo is empty
          if head_ix = tail_ix then
            dataout       <= datain;     -- Let data fall-through
            fifo(head_ix) := datain;     -- Write data
            head_ix       := test_ix;    -- And adjust the pointer
          elsif test_ix /= tail_ix then  -- NOT full
            fifo(head_ix) := datain;     -- Write data
            head_ix       := test_ix;    -- And adjust the pointer
          end if;
        end if;

        -- Reading empty fifo returns the last value
        if (rdreq = '1') and (head_ix /= tail_ix) then
          tail_ix := tail_ix + 1;
          if tail_ix > fifo_size then
            tail_ix := 0;
          end if;
          if tail_ix /= head_ix then
            dataout <= fifo(tail_ix) after 1 ns;
          end if;
        end if;
      end if;


      -- Fifo empty signal
      if head_ix = tail_ix then
        empty <= '1' after 1 ns;
      else
        empty <= '0' after 1 ns;
      end if;

      -- Fifo full signal
      if (tail_ix = (head_ix+1)) or ((tail_ix = 0) and (head_ix = fifo_size)) then
        full <= '1' after 1 ns;
      else
        full <= '0' after 1 ns;
      end if;

      -- Fifo fill meter operations
      if head_ix >= tail_ix then
        meter <= to_unsigned(head_ix - tail_ix, 8);
      else
        meter <= to_unsigned(fifo_size + 1 + head_ix - tail_ix, 8);
      end if;
      
    end if;
  end process fifo_mem;

end fifo_ft_memory_arch;






-----------------------------------------------------------------------------
-- VHDL model of A/D-converter Texas ADS1271
-----------------------------------------------------------------------------

-- NOTE! This is a model for simulation only. It is not
--       coded to be synthezised.

-- SPI interface format, 512 clock cycles/conversion

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;


entity ads1271_model is
  port (
    analog_in : in  signed(23 downto 0);  -- Analog input signal
    clk       : in  std_logic;            -- Conversion clock
    sclk      : in  std_logic;            -- SPI clock
    n_sync    : in  std_logic;            -- Input sync signal, active low
    din       : in  std_logic;            -- Serial data in
    dout      : out std_logic;            -- Serial data out
    n_drdy    : out std_logic := '1');    -- Data ready, active low
end ads1271_model;



architecture ads1271_model_arch of ads1271_model is

  signal analog_data            : signed(23 downto 0) := to_signed(0, 24);
  shared variable conv_timer    : integer             := 2;  -- Data conversion rate
  shared variable sdata_counter : integer             := -1;  -- Controls dout bits
  signal din_mem                : std_logic           := '0';  -- 'din' memory
  signal drdy_ff                : std_logic;
  signal clk_del                : std_logic           := '0';  -- Delayed clk signal
  signal sclk_del               : std_logic           := '0';  -- Delayed sclk signal
  signal ct_is_one              : std_logic;  -- Locally generated sync
  signal ct_is_zero             : std_logic;  -- Locally generated sync

  
begin  -- ads1271_model_arch

  n_drdy   <= drdy_ff or ct_is_one after 8 ns;
  sclk_del <= sclk                 after 2 ns;
  clk_del  <= clk                  after 1 ns;


  -- purpose: Data conversion circuitry
  -- type   : sequential
  -- inputs : clk_del
  -- outputs: dout, ct_is_one
  convert : process (clk_del)
  begin  -- process convert
    if falling_edge(clk_del) then       -- falling clock edge
      ct_is_one  <= '0';
      ct_is_zero <= '0';
      if n_sync = '0' then
        conv_timer := (128*512);        -- Reloading the FIR takes a while
        ct_is_one  <= '1';
      else
        conv_timer    := conv_timer - 1;
        if conv_timer <= 0 then         -- Conversion rate number
          conv_timer    := 512;
          sdata_counter := 23;          -- MSBit comes first
          ct_is_zero    <= '1';
        end if;  -- conv_timer <= 0
        if conv_timer = 1 then
          ct_is_one <= '1';
        end if;  -- conv_timer = 1
        if conv_timer = 512 then
          analog_data <= analog_in;  -- Data sampling, data av. after 512 clock's
        end if;  -- conv_timer = 512
      end if;  -- n_sync = '0'
    end if;  -- falling_edge(clk_del)

  end process convert;



  -- purpose: Serial data interface control
  -- type   : sequential
  -- inputs : sclk_del
  -- outputs: dout
  dataout : process (sclk_del)

  begin  -- process dataout
    if sclk_del'event then
      if sclk_del = '0' then            -- falling clock edge
        if sdata_counter >= 0 then
          dout          <= 'X'                        after 5 ns;
          dout          <= analog_data(sdata_counter) after 12 ns;
          sdata_counter := sdata_counter - 1;
        else
          dout <= 'X'     after 5 ns;
          dout <= din_mem after 12 ns;
        end if;
      else
        -- rising clock edge samples din
        din_mem <= din;
      end if;
    end if;
  end process dataout;



  -- purpose: Controls the drdy_ff signal
  -- type   : sequential
  -- inputs : sclk_del, ct_is_zero
  -- outputs: drdy_ff
  readyctl : process (sclk_del, ct_is_zero)
  begin  -- process readyctl
    if ct_is_zero = '1' then            -- asynchronous reset (active high)
      drdy_ff <= '0';
    elsif falling_edge(sclk_del) then   -- falling clock edge
      drdy_ff <= '1';
    end if;
  end process readyctl;

end ads1271_model_arch;





-----------------------------------------------------------------------------
-- SPI slave with secondary spi master port 
-----------------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.polyamplib.all;


entity spi_slave is
  
  port (
    -- Main controls
    fastclk : in  std_logic;
    spi_tx  : in  std_logic_vector(31 downto 0);  -- Data to be sent
    spi_rx  : out std_logic_vector(31 downto 0);  -- Data to be received
    spi_op  : out std_logic;            -- Read/Write status/data/command

    -- Slave port, connected to CPU
    mosi    : in  std_logic;
    miso    : out std_logic;
    sck     : in  std_logic;            -- SPI clock
    en_adc  : in  std_logic;            -- Active low, enable ADC
    en_incl : in  std_logic;            -- Active low, enable inclinometer

    -- Master port, connected to inclinometer
    incl_miso : in  std_logic;
    incl_mosi : out std_logic;
    incl_sck  : out std_logic;
    incl_ena  : out std_logic);         -- Active low, enable inclinometer
end spi_slave;



architecture spi_slave_arch of spi_slave is

  type   state_type is (idle, load, shift, relax);
  signal state : state_type := idle;    -- Controlling state machine

  signal enable_inclinometer : std_logic := '0';
  signal enable_adc          : std_logic := '0';
  signal adc_miso            : std_logic;
  signal adc_load            : std_logic := '0';
--  signal g_miso              : std_logic register := '0';  -- guarded miso signal
  signal adc_sck             : std_logic;
  signal tx_sck              : std_logic;
  signal delayed_adc_sck     : std_logic;  -- Delayed, for edge detection
  

begin  -- spi_slave_arch

  -- Transmit shift register
  tx_sr : sr_piso_s generic map (
    LENGTH => 32,
    DIR    => 1)                        -- Shift left => MSB first
    port map (
      fastclk => fastclk,
      clk_en  => tx_sck,
      load_en => adc_load,
      par_in  => spi_tx,
      ser_in  => '0',
      ser_out => adc_miso);

  -- Receive shift register
  rx_sr : sr_sipo generic map (
    LENGTH => 32,
    EDGE   => 1,                        -- Positive edge
    DIR    => 1)                        -- Shift left => MSB first
    port map (
      clk     => adc_sck,
      ser_in  => mosi,
      par_out => spi_rx);


  -- Passthru signals
  incl_sck  <= sck                     after 1 ns;
  incl_mosi <= mosi                    after 1 ns;
  incl_ena  <= not enable_inclinometer after 1 ns;  -- Active low output

  -- Enable and clock
  enable_inclinometer <= not en_incl and en_adc            after 1 ns;
  enable_adc          <= not en_adc                        after 1 ns;
  adc_sck             <= sck or en_adc                     after 1 ns;  -- Clock is driven high at inactive state
  tx_sck              <= (not adc_sck) and delayed_adc_sck after 1 ns;  -- sck negative edge trigger

  -- Output data
--  miso <= g_miso;


  -- purpose: ADC controls miso
--  talkto_adc : block (enable_adc = '1')
--  begin  -- block talkto_adc
--    g_miso <= guarded adc_miso;
--  end block talkto_adc;


  -- purpose: Inclinometer controls miso
--  talkto_incl : block (enable_inclinometer = '1')
--  begin  -- block talkto_incl
--    g_miso <= guarded incl_miso;
--  end block talkto_incl;


  -- Instead of the guarded assignments above
  miso <= adc_miso when enable_inclinometer = '0' else incl_miso;


  -- purpose: SPI slave controller state machine
  -- type   : sequential
  -- inputs : fastclk, enable_adc
  -- outputs: adc_load
  input_active : process (fastclk)

  begin  -- process input_active
    if rising_edge(fastclk) then        -- rising clock edge
      delayed_adc_sck <= adc_sck;
      adc_load        <= '1' after 1 ns;
      spi_op          <= '0' after 1 ns;
      case state is
        when idle => if enable_adc = '1' then
                       state <= load;
                     end if;

        when load => state <= shift;

        when shift => adc_load <= '0' after 1 ns;
                      if enable_adc = '0' then
                        state  <= relax;
                        spi_op <= '1' after 1 ns;
                      end if;

        when relax => state <= idle;

        when others => state <= idle;
      end case;
    end if;
  end process input_active;
  
end spi_slave_arch;




-----------------------------------------------------------------------------
-- SPI slave with burst capabilities and with secondary spi master port 
-- NOTE! Spi mode 2
-----------------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.polyamplib.all;


entity spi_slave_burst is
  
  port (
    -- Main controls
    fastclk   : in  std_logic;
    spi_tx    : in  std_logic_vector(31 downto 0);  -- Data to be sent
    spi_rx    : out std_logic_vector(31 downto 0);  -- Data to be received
    exec_cmd  : out std_logic;                      -- Write command/data
    fifo_read : out std_logic;                      -- Read status/data

    -- Slave port, connected to CPU
    mosi    : in  std_logic;
    miso    : out std_logic;
    sck     : in  std_logic;            -- SPI clock
    en_adc  : in  std_logic;            -- Active low, enable ADC
    en_incl : in  std_logic;            -- Active low, enable inclinometer

    -- Master port, connected to inclinometer
    incl_miso : in  std_logic;
    incl_mosi : out std_logic;
    incl_sck  : out std_logic;
    incl_ena  : out std_logic);         -- Active low, enable inclinometer
end spi_slave_burst;



architecture spi_slave_burst_arch of spi_slave_burst is

--  type   state_type is (idle, load, shift, relax);
--  signal state : state_type := idle;    -- Controlling state machine

  signal bitcounter          : unsigned(4 downto 0) := to_unsigned(0, 5);  -- Range 0..31
  signal enable_inclinometer : std_logic            := '0';
  signal enable_adc          : std_logic            := '0';
  signal adc_miso            : std_logic;
  signal clk_tx_sr           : std_logic            := '0';
  signal load_tx_sr          : std_logic            := '0';

  signal sck_r1             : std_logic;  -- Delayed, for edge detection
  signal sck_r2             : std_logic;  -- Delayed, for edge detection
  signal sck_negedge        : std_logic;  -- Negative edge
  signal sck_posedge        : std_logic;  -- Positive edge
  signal enable_adc_r1      : std_logic;  -- Delayed, for edge detection
  signal enable_adc_r2      : std_logic;  -- Delayed, for edge detection
  signal enable_adc_negedge : std_logic;  -- Negative edge

  signal bitcounter_zero : std_logic;
  signal bitcounter_one  : std_logic;
  signal exec_ff         : std_logic := '0';  -- Enable the execute signal
  signal load_ff         : std_logic := '0';  -- Enable the load signal
  
  
begin  -- spi_slave_burst_arch

  -- Transmit shift register
  tx_sr : sr_piso_s generic map (
    LENGTH => 32,
    DIR    => 1)                        -- Shift left => MSB first
    port map (
      fastclk => fastclk,
      clk_en  => clk_tx_sr,
      load_en => load_tx_sr,
      par_in  => spi_tx,
      ser_in  => '0',
      ser_out => adc_miso);

  -- Receive shift register
  rx_sr : sr_sipo generic map (
    LENGTH => 32,
    EDGE   => 1,                        -- Positive edge
    DIR    => 1)                        -- Shift left => MSB first
    port map (
      clk     => sck_negedge,
      ser_in  => mosi,
      par_out => spi_rx);


  -- Passthru signals
  incl_sck  <= sck                     after 1 ns;
  incl_mosi <= mosi                    after 1 ns;
  incl_ena  <= not enable_inclinometer after 1 ns;  -- Active low output

  -- Enable and clock
  enable_inclinometer <= not en_incl and en_adc after 1 ns;
  enable_adc          <= not en_adc             after 1 ns;

  sck_posedge <= sck_r1 and (not sck_r2) after 1 ns;  -- sck Positive edge
  sck_negedge <= sck_r2 and (not sck_r1) after 1 ns;  -- sck Negative edge

  enable_adc_negedge <= enable_adc_r1 and (not enable_adc_r2) after 1 ns;
  fifo_read          <= sck_posedge and bitcounter_one        after 1 ns;
  load_tx_sr         <= enable_adc_negedge or
                        (sck_negedge and bitcounter_zero and (not load_ff)) after 1 ns;
  exec_cmd  <= bitcounter_zero and exec_ff and sck_posedge after 1 ns;
  clk_tx_sr <= sck_posedge or enable_adc_negedge           after 1 ns;

  -- Instead of the guarded assignments above
  miso <= adc_miso when enable_inclinometer = '0' else incl_miso after 1 ns;


  -- purpose: SPI slave controller state machine
  -- type   : sequential
  -- inputs : fastclk, sck, enable_adc, bitcounter, load_tx_sr
  -- outputs: sck_r1, sck_r2, enable_adc_r1, enable_adc_r2, bitcounter
  input_active : process (fastclk)

  begin  -- process input_active
    if rising_edge(fastclk) then        -- rising clock edge
      sck_r2 <= sck_r1 after 1 ns;
      sck_r1 <= sck    after 1 ns;

      enable_adc_r2 <= enable_adc_r1 after 1 ns;
      enable_adc_r1 <= enable_adc    after 1 ns;

      if enable_adc_negedge = '1' then
        bitcounter <= to_unsigned(0, 5) after 1 ns;
      else if sck_negedge = '1' then
             bitcounter <= bitcounter + 1 after 1 ns;
           end if;
      end if;

      if bitcounter = 0 then
        bitcounter_zero <= '1' after 1 ns;
      else
        bitcounter_zero <= '0' after 1 ns;
      end if;

      if bitcounter = 1 then
        bitcounter_one <= '1' after 1 ns;
      else
        bitcounter_one <= '0' after 1 ns;
      end if;

      if enable_adc = '0' then
        exec_ff <= '0' after 1 ns;
      elsif bitcounter = 2 then
        exec_ff <= '1' after 1 ns;
      end if;

      if load_tx_sr = '1' then
        load_ff <= '1' after 1 ns;
      elsif bitcounter = 2 then
        load_ff <= '0' after 1 ns;
      end if;

    end if;
  end process input_active;

end spi_slave_burst_arch;





----------------------------------------------------------------------
-- Command decoder function
----------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;


entity command_decoder is
  port (
    addr_data  : in  std_logic_vector(31 downto 0);  -- Input address/data
    decode     : in  std_logic;         -- Single cycle decode pulse
    fastclk    : in  std_logic;         -- Master clock (not used for now)
    sel_nulcmd : out std_logic;         -- NULL command (no operation)
    sel_adclk0 : out std_logic;         -- Select sampling clock, ad0.
    sel_adclk1 : out std_logic;         -- Select sampling clock, ad1.
    sel_adclk2 : out std_logic;         -- Select sampling clock, ad2.
    sel_adclk3 : out std_logic;         -- Select sampling clock, ad3.
    sel_adclk4 : out std_logic;         -- Select sampling clock, ad4.
    sel_adclk5 : out std_logic;         -- Select sampling clock, ad5.
    sel_adclk6 : out std_logic;         -- Select sampling clock, ad6.
    sel_adclk7 : out std_logic;         -- Select sampling clock, ad7.
    resync_adc : out std_logic;         -- Resynchronize all ADC's
    write_ctrl : out std_logic;         -- Write to control-signal register
    start_adcs : out std_logic;         -- Start AD-conversion
    stop_adcs  : out std_logic);        -- Stop AD-conversion
end command_decoder;


architecture command_decoder_arch of command_decoder is

begin  -- command_decoder_arch

  sel_nulcmd <= '1' when (addr_data(27 downto 24) = "0000") and decode = '1' else '0';
  sel_adclk0 <= '1' when (addr_data(27 downto 24) = "0001") and decode = '1' else '0';
  sel_adclk1 <= '1' when (addr_data(27 downto 24) = "0010") and decode = '1' else '0';
  sel_adclk2 <= '1' when (addr_data(27 downto 24) = "0011") and decode = '1' else '0';
  sel_adclk3 <= '1' when (addr_data(27 downto 24) = "0100") and decode = '1' else '0';
  sel_adclk4 <= '1' when (addr_data(27 downto 24) = "0101") and decode = '1' else '0';
  sel_adclk5 <= '1' when (addr_data(27 downto 24) = "0110") and decode = '1' else '0';
  sel_adclk6 <= '1' when (addr_data(27 downto 24) = "0111") and decode = '1' else '0';
  sel_adclk7 <= '1' when (addr_data(27 downto 24) = "1000") and decode = '1' else '0';
  resync_adc <= '1' when (addr_data(27 downto 24) = "1001") and decode = '1' else '0';
  write_ctrl <= '1' when (addr_data(27 downto 24) = "1010") and decode = '1' else '0';
  start_adcs <= '1' when (addr_data(27 downto 24) = "1011") and decode = '1' else '0';
  stop_adcs  <= '1' when (addr_data(27 downto 24) = "1100") and decode = '1' else '0';

end command_decoder_arch;




----------------------------------------------------------------------
-- ADC clock multiplexer function
----------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity clockmux is
  port (
    clk24M  : in  std_logic;             -- Input clocks, 24 576 000
    clk4M   : in  std_logic;             -- 4 096 000
    clk2M   : in  std_logic;             -- 2 048 000
    clk1M   : in  std_logic;             -- 1 024 000
    clk512k : in  std_logic;             -- 512 000
    clk256k : in  std_logic;             -- 256 000
    clk128k : in  std_logic;             -- 128 000
    sel     : in  unsigned(2 downto 0);  -- Mux select input
    clkout  : out std_logic);            -- Output clock
end clockmux;


architecture clockmux_arch of clockmux is

begin  -- clockmux_arch

  with sel select
    clkout <=
    clk128k when "000",
    clk256k when "001",
    clk512k when "010",
    clk1M   when "011",
    clk2M   when "100",
    clk4M   when "101",
    clk24M  when "110",
    clk24M  when others;

end clockmux_arch;




-------------------------------------------------------------------------------
-- SPI master for simulation and test (no synth.)
-------------------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;


entity spi_master is
  port (
    -- Hardware ports
    miso     : in    std_logic;
    mosi     : out   std_logic;
    sck      : inout std_logic := '0';
    en_adval : out   std_logic := '1';
    en_incl  : out   std_logic := '1';

    -- Simulation ports
    data_to_spi   : in  std_logic_vector(31 downto 0);
    data_from_spi : out std_logic_vector(31 downto 0);
    start         : in  std_logic;
    busy          : out std_logic := '0';
    running       : in  std_logic);
end spi_master;


architecture spi_master_arch of spi_master is

  type   state_type is (idle, load, shift);
  signal state     : state_type := idle;
  signal start_mem : std_logic  := '0';
  
  
begin  -- spi_master_arch


  -----------------------------------------------------------------------------

  -- Assume that data_to_spi is defined before this process is triggered
  spi_rxtx : process (sck)

    variable bit_index : integer := 0;
    
  begin  -- process spi_rxtx
    if sck'event then
      if sck = '1' then                 -- rising clock edge
        start_mem <= start;
        case state is
          when idle => if start = '1' and start_mem = '0' then  -- Start rising
                                                                -- edge
                         en_adval  <= '0' after 15 ns;
                         busy      <= '1' after 2 ns;
                         state     <= load;
                         bit_index := 31;
                       end if;

          when load => data_from_spi(bit_index) <= miso;
                       state <= shift;

          when shift => bit_index := bit_index - 1;
                        if bit_index < 0 then
                          state    <= idle;
                          en_adval <= '1' after 15 ns;
                          busy     <= '0' after 2 ns;
                        else
                          data_from_spi(bit_index) <= miso;
                        end if;
          when others => state <= idle;
        end case;
      end if;  -- sck = '1' else ...

      if sck = '0' and (state = load or state = shift) then
        mosi <= data_to_spi(bit_index) after 1 ns;
      end if;

    end if;  -- sck'event
  end process spi_rxtx;

  -----------------------------------------------------------------------------

  spi_clk : process
  begin  -- process spi_clk
    while running = '1' loop
      sck <= not sck;
      wait for 50 ns;
    end loop;
    sck <= not sck;
    wait for 50 ns;
    sck <= not sck;
    wait for 50 ns;
    sck <= not sck;
    wait for 50 ns;
    sck <= not sck;
    wait for 50 ns;
    wait;
  end process spi_clk;

end spi_master_arch;



----------------------------------------------------------------------
-- Synchronization logic
----------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity sync_logic_2 is
  
  port (
    start_adcs       : in  std_logic;   -- Start command
    stop_adcs        : in  std_logic;   -- Stop command
    reset            : in  std_logic;   -- Active high
    hwsync           : in  std_logic;   -- Hardware sync
    fastclk          : in  std_logic;   -- Master clock
    enable_adcvalues : out std_logic);  -- Enable reception of values

end sync_logic_2;



architecture sync_logic_2_arch of sync_logic_2 is

  signal del_sync_1 : std_logic;         -- 1'st delay
  signal del_sync_2 : std_logic;         -- 2'nd delay
  signal sr_set     : std_logic;         -- Set input to SR-latch
  signal sr         : std_logic := '0';  -- sr Q output, entity output

  
begin  -- sync_logic_2_arch

  enable_adcvalues <= sr;
  sr_set           <= (del_sync_1 and (not del_sync_2)) xor start_adcs;


  -- purpose: Two D-latches and one SR-latch is controlled
  -- type   : sequential
  -- inputs : fastclk, reset, start_adcs, stop_adcs, hwsync
  -- outputs: sr
  registered_logic : process (fastclk, reset)
  begin  -- process registered_logic
    if reset = '1' then                 -- asynchronous reset
      del_sync_1 <= '0' after 2 ns;
      del_sync_2 <= '0' after 2 ns;
      sr         <= '0' after 2 ns;
    elsif rising_edge(fastclk) then     -- rising clock edge
      del_sync_1 <= hwsync     after 2 ns;
      del_sync_2 <= del_sync_1 after 2 ns;
      if stop_adcs = '1' then
        sr <= '0' after 2 ns;
      elsif sr_set = '1' then
        sr <= '1' after 2 ns;
      end if;

    end if;
  end process registered_logic;

end sync_logic_2_arch;