aboutsummaryrefslogtreecommitdiff
path: root/impl/DMAppFpgaProg/DMAppFpgaProg/DirectC/G4Algo/dpG4alg.c
blob: 588097ef534a5d5f5dd9bfefbe55d6da724b0191 (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
/* ************ MICROSEMI SOC CORP. DIRECTC LICENSE AGREEMENT ************* */
/* ------------------------------------------------------------------------ 
PLEASE READ: BEFORE INSTALLING THIS SOFTWARE, CAREFULLY READ THE FOLLOWING 
MICROSEMI SOC CORP LICENSE AGREEMENT REGARDING THE USE OF THIS SOFTWARE. 
INSTALLING THIS SOFTWARE INDICATES THAT YOU ACCEPT AND UNDERSTAND THIS AGREEMENT 
AND WILL ABIDE BY IT. 

Note: This license agreement (“License”) only includes the following software: 
DirectC. DirectC is licensed under the following terms and conditions.

Hereinafter, Microsemi SoC Corp. shall be referred to as “Licensor” or “Author,” 
whereas the other party to this License shall be referred to as “Licensee.” Each 
party to this License shall be referred to, singularly, as a “Party,” or, 
collectively, as the “Parties.”

Permission to use, copy, modify, and/or distribute DirectC for any purpose, with
or without fee, is hereby granted by Licensor to Licensee, provided that the 
above Copyright notice and this permission notice appear in all copies, 
modifications and/or distributions of DirectC.

DIRECTC IS PROVIDED "AS IS" AND THE AUTHOR/LICENSOR DISCLAIMS ALL WARRANTIES 
WITH REGARD TO DIRECTC INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 
FITNESS. IN NO EVENT SHALL AUTHOR/LICENSOR BE LIABLE TO LICENSEE FOR ANY DAMAGES, 
INCLUDING SPECIAL, DIRECT,INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF 
CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 
WITH THE USE OR PERFORMANCE OF DIRECTC.

Export Control: Information furnished to Licensee may include United States 
origin technical data. Accordingly, Licensee is responsible for complying with, 
and warrants to Licensor that it will comply with, all U.S. export control laws 
and regulations, including the provisions of the Export Administration Act of 
1979 and the Export Administration Regulations promulgated thereunder, the Arms 
Export Control Act, and the sanctions laws administered by the Office of Foreign 
Assets Control including any other U.S. Government regulation applicable to the 
export, re-export, or disclosure of such controlled technical data (or the 
products thereof) to Foreign Nationals, whether within or without the U.S., 
including those employed by, or otherwise associated with, Licensee. Licensee 
shall obtain Licensor’s written consent prior to submitting any request for 
authority to export any such technical data.

ADR: Any dispute between the Parties arising from or related to this License or 
the subject matter hereof, including its validity, construction or performance 
thereunder, shall be exclusively resolved through arbitration by a mutually 
acceptable impartial and neutral arbitrator appointed by the Judicial 
Arbitration and Mediation Services (JAMS) in accordance with its rules and 
procedures. If the Parties are not able to agree on an arbitrator within 10 days 
of the date of request for mediation is served, then JAMS shall appoint an 
arbitrator. Notice of arbitration shall be served and filed with the JAMS main 
offices in Irvine, California. Each Party shall be responsible for all costs 
associated with the preparation and representation by attorneys, or any other 
persons retained thereby, to assist in connection with any such Arbitration. 
However, all costs charged by the mutually agreed upon Arbitration entity shall 
be equally shared by the Parties. The Party seeking Mediation and/or Arbitration 
as provided herein agrees that the venue for any such Mediation and Arbitration 
shall be selected by the other Party and that such venue must be Los Angeles, 
California; New York, New York; or Chicago, Illinois; whereby the applicable law 
and provisions of the Evidence Code of the State selected thereby shall be 
applicable and shall govern the validity, construction and performance of this 
License.

Governing Law: This license will be governed by the laws of the State of 
California, without regard to its conflict of law provisions.

Entire Agreement: This document constitutes the entire agreement between the 
Parties with respect to the subject matter herein and supersedes all other 
communications whether written or oral.                                     */

/* ************************************************************************ */
/*                                                                          */
/*  JTAG_DirectC    Copyright (C) Microsemi Corporation                     */
/*  Version 4.1     Release date January 29, 2018                           */
/*                                                                          */
/* ************************************************************************ */
/*                                                                          */
/*  Module:         dpG4alg.c                                               */
/*                                                                          */
/*  Description:    Contains initialization and data checking functions.    */
/*                                                                          */
/* ************************************************************************ */

#include "dpuser.h"
#ifdef ENABLE_G4_SUPPORT
#include "dputil.h"
#include "dpcom.h"
#include "dpalg.h"
#include "dpG4alg.h"
#include "dpjtag.h"

DPUCHAR pgmmode;
DPUCHAR pgmmode_flag;
DPUCHAR envm_only_flag;
DPUCHAR sec_ul;
DPUCHAR shared_buf[96]; // Maximum of 768
DPUCHAR poll_buf[17];
DPULONG poll_index;
DPUCHAR SYSREG_TEMP[4];
DPUCHAR component_type;

DPUCHAR security_queried;

/****************************************************************************
* Purpose: main entry function                                              
*  This function needs to be called from the main application function with 
*  the approppriate action code set to intiate the desired action.
****************************************************************************/
DPUCHAR dp_top_g4 (void) 
{
    dp_init_vars();
    dp_init_G4_vars();
    goto_jtag_state(JTAG_TEST_LOGIC_RESET,0u);
    dp_check_G4_action();
    if (error_code == DPE_SUCCESS)
    {
        dp_perform_G4_action();
    }
    
    return error_code;
}

void dp_init_G4_vars(void)
{
    envm_only_flag = FALSE;
    pgmmode = 0u;
    pgmmode_flag = FALSE;
    security_queried = FALSE;
    sec_ul = 0u;
    return;
}

void dp_check_G4_action(void)
{
    if ( (Action_code == DP_READ_IDCODE_ACTION_CODE) || (Action_code == DP_DEVICE_INFO_ACTION_CODE) )
    {
        #ifndef ENABLE_DISPLAY
        error_code = DPE_CODE_NOT_ENABLED;
        #endif 
    }
    else if (! (
    (Action_code == DP_ERASE_ACTION_CODE) ||
    (Action_code == DP_PROGRAM_ACTION_CODE) ||
    (Action_code == DP_VERIFY_ACTION_CODE) ||
    (Action_code == DP_ENC_DATA_AUTHENTICATION_ACTION_CODE) ||
    (Action_code == DP_VERIFY_DIGEST_ACTION_CODE)
    ))
    {
        error_code = DPE_ACTION_NOT_SUPPORTED;
        #ifdef ENABLE_DISPLAY
        dp_display_text("\r\nInvalid action.");
        #endif
    }
    return;
}

void dp_perform_G4_action (void)
{
    #ifdef ENABLE_DISPLAY
    if (Action_code == DP_READ_IDCODE_ACTION_CODE)
    {
        dp_read_idcode_action();
        Action_done = TRUE;
    }
    else if (Action_code == DP_DEVICE_INFO_ACTION_CODE)
    {
        dp_G4M_device_info_action();
        Action_done = TRUE;
    }
    #endif
    if (Action_done == FALSE)
    {
        dp_check_image_crc();
        if (error_code == DPE_SUCCESS)
        {
            dp_check_G4_device_ID();
            if (error_code == DPE_SUCCESS)
            {
                switch (Action_code) 
                {
                    case DP_ERASE_ACTION_CODE: 
                    dp_G4M_erase_action();
                    break;         
                    case DP_PROGRAM_ACTION_CODE: 
                    dp_G4M_program_action();
                    break;
                    case DP_VERIFY_ACTION_CODE: 
                    dp_G4M_verify_action();
                    break;
                    case DP_ENC_DATA_AUTHENTICATION_ACTION_CODE: 
                    dp_G4M_enc_data_authentication_action();
                    break;
                    case DP_VERIFY_DIGEST_ACTION_CODE: 
                    dp_G4M_verify_digest_action();
                    break;
                }
            }
        }
    }
    dp_G4M_exit();
    return;
}


void dp_G4M_erase_action(void)
{
    dp_G4M_initialize();
    if (error_code == DPE_SUCCESS)
    {
        dp_G4M_prepare_bitstream();
        pgmmode = 0x1u;
        dp_set_mode();
        
        if (error_code == DPE_SUCCESS)
        {
            
            /* Global unit1 is used to hold the number of components */
            global_uint1 = (DPUINT)dp_get_bytes(Header_ID,G4M_NUMOFCOMPONENT_OFFSET,G4M_NUMOFCOMPONENT_BYTE_LENGTH);
            global_uint2 = global_uint1 - ((DPUINT)dp_get_bytes(Header_ID,G4M_ERASEDATASIZE_OFFSET,G4M_DATASIZE_BYTE_LENGTH) - 1u);
            
            dp_G4M_process_data(G4M_erasedatastream_ID);
            
            if(error_code != DPE_SUCCESS)
            {
                error_code = DPE_ERASE_ERROR;
            }
            
        }
    }
    return;
}

void dp_G4M_program_action(void)
{
    dp_G4M_initialize();
    if (error_code == DPE_SUCCESS)
    {
        dp_G4M_prepare_bitstream();
    }
    
    
    if ((error_code == DPE_SUCCESS) && (sec_ul & 0x2u))
    {
        pgmmode = 0x1u;
        dp_set_mode();
        if (error_code == DPE_SUCCESS)
        {
            global_uint1 = (DPUINT)dp_get_bytes(Header_ID,G4M_NUMOFCOMPONENT_OFFSET,G4M_NUMOFCOMPONENT_BYTE_LENGTH);
            global_uint2 = global_uint1 - ((DPUINT)dp_get_bytes(Header_ID,G4M_ERASEDATASIZE_OFFSET,G4M_DATASIZE_BYTE_LENGTH) - 1u);
            dp_G4M_process_data(G4M_erasedatastream_ID);
            
            if(error_code != DPE_SUCCESS)
            {
                error_code = DPE_ERASE_ERROR;
            }
        }
    }
    if (error_code == DPE_SUCCESS)
    {
        /* Global unit1 is used to hold the number of components */
        global_uint1 = (DPUINT)dp_get_bytes(Header_ID,G4M_DATASIZE_OFFSET,G4M_DATASIZE_BYTE_LENGTH);
        global_uint2 = 1u;
        dp_G4M_setup_eNVM(G4M_datastream_ID);
        
        if (error_code == DPE_SUCCESS)
        {
            pgmmode = 0x1u;
            dp_set_mode();
            if (error_code == DPE_SUCCESS)
            {
                global_uint1 = (DPUINT)dp_get_bytes(Header_ID,G4M_DATASIZE_OFFSET,G4M_DATASIZE_BYTE_LENGTH);
                global_uint2 = 1u;
                dp_G4M_process_data(G4M_datastream_ID);
                if(error_code != DPE_SUCCESS)
                {
                    error_code = DPE_CORE_PROGRAM_ERROR;
                }
                else
                {
                    dp_G4M_post_setup_eNVM();
                }
            }
        }
    }
    
    
    return;
}

void dp_G4M_verify_action(void)
{
    dp_G4M_initialize();
    if (error_code == DPE_SUCCESS)
    {
        dp_G4M_prepare_bitstream();
    }
    
    
    global_uint2 = 1u;
    global_uint1 = (DPUINT)dp_get_bytes(Header_ID,G4M_DATASIZE_OFFSET,G4M_DATASIZE_BYTE_LENGTH);
    dp_G4M_setup_eNVM(G4M_datastream_ID);
    
    if (error_code == DPE_SUCCESS)
    {
        pgmmode = 0x2u;
        dp_set_mode();
        
        if (error_code == DPE_SUCCESS)
        {
            /* Global unit1 is used to hold the number of components */
            if ((DPUINT)dp_get_bytes(Header_ID,G4M_VERIFYDATASIZE_OFFSET,G4M_DATASIZE_BYTE_LENGTH) != 0u)
            {
                /* Both global_unit1 and global_unit2 have DATASIZE, ENVMDATASIZE and ENVMVERIFYDATASIZE; */
                global_uint1 = (DPUINT)dp_get_bytes(Header_ID,G4M_DATASIZE_OFFSET,G4M_DATASIZE_BYTE_LENGTH) +
                (DPUINT)dp_get_bytes(Header_ID,G4M_ENVMDATASIZE_OFFSET,G4M_DATASIZE_BYTE_LENGTH) +
                (DPUINT)dp_get_bytes(Header_ID,G4M_ENVMVERIFYDATASIZE_OFFSET,G4M_DATASIZE_BYTE_LENGTH);
                global_uint2 = global_uint1 + 1u;
                global_uint1 += (DPUINT)dp_get_bytes(Header_ID,G4M_VERIFYDATASIZE_OFFSET,G4M_DATASIZE_BYTE_LENGTH);
                dp_G4M_process_data(G4M_VerifyDataStream_ID);
            }
            else
            {
                global_uint2 = 1u;
                global_uint1 = (DPUINT)dp_get_bytes(Header_ID,G4M_DATASIZE_OFFSET,G4M_DATASIZE_BYTE_LENGTH);
                dp_G4M_process_data(G4M_datastream_ID);
            }
            
            if(error_code != DPE_SUCCESS)
            {
                error_code = DPE_CORE_VERIFY_ERROR;
            }
            else
            {
                dp_G4M_post_setup_eNVM();
            }
        }
    }
    
    return;
}

void dp_G4M_enc_data_authentication_action(void)
{
    dp_G4M_initialize();
    if (error_code == DPE_SUCCESS)
    {
        pgmmode = 0x0u;
        dp_set_mode();
        
        if (error_code == DPE_SUCCESS)
        {
            /* Global unit1 is used to hold the number of components */
            global_uint1 = (DPUINT)dp_get_bytes(Header_ID,G4M_DATASIZE_OFFSET,G4M_DATASIZE_BYTE_LENGTH);
            global_uint2 = 1u;
            
            dp_G4M_process_data(G4M_datastream_ID);
            
            
            if(error_code != DPE_SUCCESS)
            {
                error_code = DPE_AUTHENTICATION_FAILURE;
            }
            
        }
    }
    return;
}

void dp_G4M_verify_digest_action(void)
{
    dp_G4M_initialize();
    if (error_code == DPE_SUCCESS)
    {
        
        dp_flush_global_buf1();
        
        DataIndex = 0u;
        global_uint1 = (DPUINT)dp_get_bytes(Header_ID,G4M_DATASIZE_OFFSET,G4M_DATASIZE_BYTE_LENGTH);
        for (global_uint2 = 1u; global_uint2 <= global_uint1; global_uint2++)
        {
            /* get the number of blocks */
            /* Global ulong1 is used to hold the number of blocks within the components */
            global_ulong1 = dp_get_bytes(G4M_NUMBER_OF_BLOCKS_ID,(DPULONG)(((global_uint2 - 1u) * 22u) / 8u),4u);
            global_ulong1 >>= ((global_uint2 - 1U)* 22u) % 8u;
            global_ulong1 &= 0x3FFFFFu;
            
            component_type = (DPUCHAR) dp_get_bytes(G4M_datastream_ID,COMPONENT_TYPE_IN_HEADER_BYTE+DataIndex/8,1);
            if (component_type == G4M_FPGA)
            {
                global_buf1[0] |= 0x1u;
            }
            else if (component_type == G4M_ENVM)
            {
                /* Component type is no longer needed since we know it is eNVM.  We need to know which module */
                component_type = (DPUCHAR) dp_get_bytes(G4M_datastream_ID,COMPONENT_TYPE_IN_HEADER_BYTE+ENVM_MODULE_ID_IN_HEADER_BYTE+DataIndex/8,1);
                if (component_type == 0u)
                {
                    global_buf1[0] |= 0x2u;
                }
                else if (component_type == 1u)
                {
                    global_buf1[0] |= 0x4u;
                }
            }
            DataIndex += G4M_FRAME_BIT_LENGTH * global_ulong1;
        }
        global_ulong1 = dp_get_bytes(Header_ID,G4M_ID_MASK_OFFSET,4U);
        if ( ( (device_ID & global_ulong1) == M2S090_ID) || ( (device_ID & global_ulong1) == M2S150_ID) )
        {
            global_buf1[0] |= 0xcu;
        }
        global_buf1[1] |= 0x3cu;
        
        opcode = G4M_CHECK_DIGESTS;
        IRSCAN_in();
        DRSCAN_in(0u, G4M_SECURITY_STATUS_REGISTER_BIT_LENGTH, global_buf1);
        goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
        dp_delay(G4M_STANDARD_DELAY);
        
        opcode = G4M_CHECK_DIGESTS;
        dp_G4M_device_poll(16u, 15u);
        if (error_code == DPE_SUCCESS)
        {
            if (( poll_buf[1] & 0x3c0u) == 0x3cu)
            {
                #ifdef ENABLE_DISPLAY
                dp_display_text("\r\nDigest request from SPI/JTAG is protected by user pass key 1.");
                #endif
                error_code = DPE_VERIFY_DIGEST_ERROR;
            }
            else if (( poll_buf[1] & 0x40u) == 0x40u)
            {
                #ifdef ENABLE_DISPLAY
                dp_display_text("\r\nFailed to verify digest.");
                #endif
                error_code = DPE_VERIFY_DIGEST_ERROR;
            }
            if ((global_buf1[0] & 0x1u) == 0x1u)
            {
                if (( poll_buf[0] & 0x1u) == 0x1u)
                {
                    #ifdef ENABLE_DISPLAY
                    dp_display_text("\r\nFPGA Fabric digest verification: PASS");
                    #endif
                }
                else
                {
                    #ifdef ENABLE_DISPLAY
                    dp_display_text("\r\nFPGA Fabric digest verification: FAIL");
                    #endif
                    error_code = DPE_VERIFY_DIGEST_ERROR;
                }
                if (( poll_buf[0] & 0x80u) == 0x80u)
                {
                    #ifdef ENABLE_DISPLAY
                    dp_display_text("\r\nFabric configuration segment digest verification: PASS");
                    #endif
                }
                else
                {
                    #ifdef ENABLE_DISPLAY
                    dp_display_text("\r\nFabric configuration segment digest verification: FAIL");
                    #endif
                    error_code = DPE_VERIFY_DIGEST_ERROR;
                }
            }
            if ((global_buf1[0] & 0x2u) == 0x2u)
            {
                if (( poll_buf[0] & 0x2u) == 0x2u)
                {
                    #ifdef ENABLE_DISPLAY
                    dp_display_text("\r\neNVM_0 digest verification: PASS");
                    #endif
                }
                else
                {
                    #ifdef ENABLE_DISPLAY
                    dp_display_text("\r\neNVM_0 digest verification: FAIL");
                    #endif
                    error_code = DPE_VERIFY_DIGEST_ERROR;
                }
            }
            if ((global_buf1[0] & 0x4u) == 0x4u)
            {
                if (( poll_buf[0] & 0x4u) == 0x4u)
                {
                    #ifdef ENABLE_DISPLAY
                    dp_display_text("\r\neNVM_1 digest verification: PASS");
                    #endif
                }
                else
                {
                    #ifdef ENABLE_DISPLAY
                    dp_display_text("\r\neNVM_1 digest verification: FAIL");
                    #endif
                    error_code = DPE_VERIFY_DIGEST_ERROR;
                }
            }
            if ((sec_ul & 0x2u) == 0x2u)
            {
                if (( poll_buf[0] & 0x8u) == 0x8u)
                {
                    #ifdef ENABLE_DISPLAY
                    dp_display_text("\r\nUser security policies segment digest verification: PASS");
                    #endif
                }
                else
                {
                    #ifdef ENABLE_DISPLAY
                    dp_display_text("\r\nUser security policies segment digest verification: FAIL");
                    #endif
                    error_code = DPE_VERIFY_DIGEST_ERROR;
                }
            }
            dp_get_data(G4M_UPK1_ID,0u);
            if (return_bytes != 0u)
            {
                if (( poll_buf[0] & 0x10u) == 0x10u)
                {
                    #ifdef ENABLE_DISPLAY
                    dp_display_text("\r\nUser key set 1 segment digest verification: PASS");
                    #endif
                }
                else
                {
                    #ifdef ENABLE_DISPLAY
                    dp_display_text("\r\nUser key set 1 segment digest verification: FAIL");
                    #endif
                    error_code = DPE_VERIFY_DIGEST_ERROR;
                }
            }
            dp_get_data(G4M_UPK2_ID,0u);
            if (return_bytes != 0u)
            {
                if (( poll_buf[0] & 0x20u) == 0x20u)
                {
                    #ifdef ENABLE_DISPLAY
                    dp_display_text("\r\nUser key set 2 segment digest verification: PASS");
                    #endif
                }
                else
                {
                    #ifdef ENABLE_DISPLAY
                    dp_display_text("\r\nUser key set 2 segment digest verification: FAIL");
                    #endif
                    error_code = DPE_VERIFY_DIGEST_ERROR;
                }
            }
            if (( poll_buf[0] & 0x40u) == 0x0u)
            {
                #ifdef ENABLE_DISPLAY
                dp_display_text("\r\nFactory row and factory key segment digest verification: FAIL");
                #endif
                error_code = DPE_VERIFY_DIGEST_ERROR;
            }
        }
    }
    return;
}

void dp_G4M_check_core_status(void)
{
    
    opcode = G4M_ISC_NOOP;
    IRSCAN_out(&global_uchar1);
    goto_jtag_state(JTAG_RUN_TEST_IDLE,1u);
    
    #ifdef ENABLE_DISPLAY
    if ((global_uchar1 & 0x4u) == 0x4u)
    {
        dp_display_text("\r\nFPGA Array is programmed and enabled.");
    }
    else
    {
        dp_display_text("\r\nFPGA Array is not enabled.");
    }
    #endif
    
    return;
}


#ifdef ENABLE_DISPLAY
void dp_G4M_device_info_action(void)
{
    dp_G4M_check_core_status();
    if (error_code == DPE_SUCCESS)
    {
        dp_G4M_read_design_info();
        if (error_code == DPE_SUCCESS)
        {
            dp_G4M_read_prog_info();
            if (error_code == DPE_SUCCESS)
            {
                dp_G4M_read_bitstream_digest();
                if (error_code == DPE_SUCCESS)
                {
                    dp_G4M_read_fsn();
                    if (error_code == DPE_SUCCESS)
                    {
                        dp_G4M_read_security();
                    }
                }
            }
        }
    }
    
    return;
}

void dp_G4M_read_design_info(void)
{
    opcode = G4M_READ_DESIGN_INFO;
    IRSCAN_in();
    DRSCAN_in(0u, G4M_STATUS_REGISTER_BIT_LENGTH, (DPUCHAR*)(DPUCHAR*)DPNULL);
    goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
    dp_delay(G4M_STANDARD_DELAY);
    opcode = G4M_READ_DESIGN_INFO;
    dp_G4M_device_poll(8u, 7u);
    if (error_code == DPE_SUCCESS)
    {
        dp_read_shared_buffer(3u);
        if (error_code == DPE_SUCCESS)
        {
            dp_display_text("\r\nDesign Name: ");
            
            
            for (global_uchar1 = 2u; global_uchar1 < 32u; global_uchar1++)
            {
                dp_display_value(shared_buf[global_uchar1],CHR);
            }
            
            dp_display_text("\r\nChecksum: ");
            dp_display_array(shared_buf,2u,HEX);
            dp_display_text("\r\nDesign Info: ");
            dp_display_array(shared_buf,34u,HEX);
            dp_display_text("\r\nDESIGNVER: ");
            dp_display_array(&shared_buf[32],2u,HEX);
            dp_display_text("\r\nBACKLEVEL: ");
            dp_display_array(&shared_buf[34],2u,HEX);
        }
    }
    
    return;
}

void dp_G4M_read_prog_info(void)
{
    opcode = G4M_READ_PROG_INFO;
    IRSCAN_in();
    DRSCAN_in(0u, G4M_STATUS_REGISTER_BIT_LENGTH, (DPUCHAR*)(DPUCHAR*)DPNULL);
    goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
    dp_delay(G4M_STANDARD_DELAY);
    
    opcode = G4M_READ_PROG_INFO;
    dp_G4M_device_poll(128u, 127u);
    if (error_code == DPE_SUCCESS)
    {
        dp_display_text("\r\nPROG_INFO: ");
        dp_display_array(poll_buf,16u,HEX);
        if ((poll_buf[6] & 0x1) == 0u)
        {
            dp_display_text("\r\nVCC was programmed at 1.2V");
        }
        else
        {
            dp_display_text("\r\nVCC was programmed at 1.0v");
        }
        if  ( ((poll_buf[8] & 0x3f) != 0u) && ((poll_buf[8] & 0x3f) != 0x3fu) )
        {
            dp_display_text("\r\nAlgorithm Version: ");
            dp_display_value((poll_buf[8] & 0x3f),DEC);
        }
        
        
        global_uchar1 = ((poll_buf[8] >> 6) | (poll_buf[9] << 2)) & 0xfu;
        dp_display_text("\r\nProgrammer code: ");
        dp_display_value(global_uchar1, DEC);
        
        global_uchar1 = ((poll_buf[10] >> 1)) & 0x3fu;
        dp_display_text("\r\nSoftware version code: ");
        dp_display_value(global_uchar1, DEC);
        
        global_uchar1 = ((poll_buf[10] >> 7) | (poll_buf[11] << 1)) & 0x7u;
        dp_display_text("\r\nProgramming Software code: ");
        dp_display_value(global_uchar1, DEC);
        
        global_uchar1 = ((poll_buf[11] >> 2)) & 0x7u;
        dp_display_text("\r\nProgramming Interface Protocol code: ");
        dp_display_value(global_uchar1, DEC);
        
        global_uchar1 = ((poll_buf[11] >> 5)) & 0x7u;
        dp_display_text("\r\nProgramming File Type code: ");
        dp_display_value(global_uchar1, DEC);
        
    }
    
    return;
}

void dp_G4M_read_fsn(void)
{
    opcode = G4M_READ_FSN;
    IRSCAN_in();
    DRSCAN_in(0u, G4M_STATUS_REGISTER_BIT_LENGTH, (DPUCHAR*)(DPUCHAR*)DPNULL);
    goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
    opcode = G4M_READ_FSN;
    dp_G4M_device_poll(129u, 128u);
    dp_display_text("\r\n=====================================================================");
    dp_display_text("\r\nDSN: ");
    dp_display_array(poll_buf, 16u, HEX);
    dp_display_text("\r\n=====================================================================");
    
    return;
}

void dp_G4M_read_bitstream_digest(void)
{
    opcode = G4M_READ_DIGESTS;
    IRSCAN_in();
    DRSCAN_in(0u, G4M_STATUS_REGISTER_BIT_LENGTH, (DPUCHAR*)(DPUCHAR*)DPNULL);
    goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
    dp_delay(G4M_STANDARD_DELAY);
    opcode = G4M_READ_DIGESTS;
    dp_G4M_device_poll(8u, 7u);
    if (error_code == DPE_SUCCESS)
    {
        dp_read_shared_buffer(6u);
        if (error_code == DPE_SUCCESS)
        {
            dp_display_text("\r\nBitstream Fabric Digest: ");
            dp_display_array(shared_buf,32u,HEX);
            dp_display_text("\r\nBitstream eNVM0 Digest: ");
            dp_display_array(&shared_buf[32],32u,HEX);
            dp_display_text("\r\nBitstream eNVM1 Digest: ");
            dp_display_array(&shared_buf[64],32u,HEX);
        }
    }
    return;
}

#endif

/* Checking device ID function.  ID is already read in dpalg.c */
void dp_check_G4_device_ID (void)
{
    /* DataIndex is a variable used for loading the array data but not used now.  
    * Therefore, it can be used to store the Data file ID for */
    DataIndex = dp_get_bytes(Header_ID,G4M_ID_OFFSET,G4M_ID_BYTE_LENGTH);
    
    
    global_ulong1 = dp_get_bytes(Header_ID,G4M_ID_MASK_OFFSET,4U);
    
    device_ID &= global_ulong1;
    DataIndex &= global_ulong1;
    
    
    /* Identifying target device and setting its parms */
    if ( (DataIndex & 0xfff) == MICROSEMI_ID)
    {
        if (device_ID == DataIndex )
        {
            #ifdef ENABLE_DISPLAY
            dp_display_text("\r\nActID = ");
            dp_display_value(device_ID,HEX);
            dp_display_text(" ExpID = ");
            dp_display_value(DataIndex,HEX);
            dp_display_text("\r\nDevice Rev = ");
            dp_display_value(device_rev,HEX);
            #endif
            device_family = (DPUCHAR) dp_get_bytes(Header_ID,G4M_DEVICE_FAMILY_OFFSET,G4M_DEVICE_FAMILY_BYTE_LENGTH);
        }
        else
        {
            #ifdef ENABLE_DISPLAY
            dp_display_text(" ExpID = ");
            dp_display_value(DataIndex,HEX);
            #endif
            error_code = DPE_IDCODE_ERROR;
        }
    }
    else
    {
        error_code = DPE_IDCODE_ERROR;
    }
    
    return;
}

/* Check if system controller is ready to enter programming mode */
void dp_G4M_device_poll(DPUCHAR bits_to_shift, DPUCHAR Busy_bit)
{
    for ( poll_index = 0U; poll_index <= G4M_MAX_CONTROLLER_POLL; poll_index++ )
    {
        IRSCAN_in();
        DRSCAN_out(bits_to_shift, (DPUCHAR*)DPNULL, poll_buf);
        dp_delay(G4M_STANDARD_DELAY);
        if ( ((poll_buf[Busy_bit/8] & (1 << (Busy_bit % 8))) == 0x0u))
        {
            break;
        }
    }
    if(poll_index > G4M_MAX_CONTROLLER_POLL)
    {
        #ifdef ENABLE_DISPLAY
        dp_display_text("\r\nDevice polling failed.");
        #endif
        error_code = DPE_POLL_ERROR;
    }
    
    return;
}

void dp_G4M_device_shift_and_poll(DPUCHAR bits_to_shift, DPUCHAR Busy_bit,DPUCHAR Variable_ID,DPULONG start_bit_index)
{
    for ( poll_index = 0U; poll_index <= G4M_MAX_CONTROLLER_POLL; poll_index++ )
    {
        IRSCAN_in();
        dp_get_and_DRSCAN_in_out(Variable_ID, bits_to_shift, start_bit_index, poll_buf);
        //DRSCAN_out(bits_to_shift, (DPUCHAR*)DPNULL, poll_buf);
        dp_delay(G4M_STANDARD_DELAY);
        if ( ((poll_buf[Busy_bit/8] & (1 << (Busy_bit % 8))) == 0x0u))
        {
            break;
        }
    }
    if(poll_index > G4M_MAX_CONTROLLER_POLL)
    {
        #ifdef ENABLE_DISPLAY
        dp_display_text("\r\nDevice polling failed.");
        #endif
        error_code = DPE_POLL_ERROR;
    }
    
    return;
}

void dp_read_shared_buffer(DPUCHAR ucNumOfBlocks)
{
    
    dp_flush_global_buf1();
    for (global_uchar1 = 0u; global_uchar1 < ucNumOfBlocks; global_uchar1++)
    {
        global_buf1[0] = (global_uchar1 << 1u);
        opcode = G4M_READ_BUFFER;
        IRSCAN_in();
        DRSCAN_in(0u, G4M_FRAME_STATUS_BIT_LENGTH, global_buf1);
        goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
        dp_delay(G4M_STANDARD_DELAY);
        opcode = G4M_READ_BUFFER;
        dp_G4M_device_poll(129u, 128u);
        for (global_uchar2 = 0;global_uchar2 < 16u; global_uchar2++)
        {
            shared_buf[global_uchar1*16u + global_uchar2] = poll_buf[global_uchar2];
        }
    }
    
    return;
}

void dp_G4M_poll_device_ready(void)
{
    opcode = G4M_ISC_NOOP;
    for ( poll_index = 0U; poll_index <= G4M_MAX_CONTROLLER_POLL; poll_index++ )
    {
        IRSCAN_in();
        goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
        dp_delay(G4M_STANDARD_DELAY);
        DRSCAN_out(8u, (DPUCHAR*)DPNULL, poll_buf);
        
        if ((poll_buf[0] & 0x80u) == 0x0u)
        {
            break;
        }
    }
    if(poll_index > G4M_MAX_CONTROLLER_POLL)
    {
        #ifdef ENABLE_DISPLAY
        dp_display_text("\r\nDevice polling failed.");
        #endif
        error_code = DPE_POLL_ERROR;
    }
    
    return;
}

void dp_set_pgm_mode(void)
{
    opcode = G4M_MODE;
    IRSCAN_in();
    DRSCAN_in(0u, G4M_STATUS_REGISTER_BIT_LENGTH, (DPUCHAR*)(DPUCHAR*)DPNULL);
    goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
    dp_delay(G4M_STANDARD_DELAY);
    opcode = G4M_MODE;
    dp_G4M_device_poll(8u, 7u);
    
    return;
}

/****************************************************************************
* Purpose: Loads the BSR regsiter with data specified in the data file.  
*   If BSR_SAMPLE is enabled, the data is not loaded.  Instead, the last known
*   State of the IOs is maintained by stepping through DRCapture JTAG state.
****************************************************************************/
void dp_G4M_load_bsr(void)
{
    dp_G4M_check_core_status();
    
    
    global_uint1 = (DPUINT) dp_get_bytes(G4M_Header_ID,G4M_NUMOFBSRBITS_OFFSET,G4M_NUMOFBSRBITS_BYTE_LENGTH);
    opcode = ISC_SAMPLE;
    IRSCAN_in();
    
    #ifdef BSR_SAMPLE
    /* Capturing the last known state of the IOs is only valid if the core
    was programmed.  Otherwise, load the BSR with what is in the data file. */
    if ((global_uchar1 & 0x4u) != 0x4u)
    {
        dp_get_bytes(G4M_BsrPattern_ID,0u,1u);
        if (return_bytes)
        {
            #ifdef ENABLE_DISPLAY
            dp_display_text("\r\nWarning: FPGA array is not programmed. Loading BSR register...");
            #endif
            dp_get_and_DRSCAN_in(G4M_BsrPattern_ID, global_uint1, 0u);
            goto_jtag_state(JTAG_RUN_TEST_IDLE,0u);
        }
    }
    else 
    {
        #ifdef ENABLE_DISPLAY
        dp_display_text("\r\nMaintaining last known IO states...");
        #endif
        goto_jtag_state(JTAG_CAPTURE_DR,0u);
        goto_jtag_state(JTAG_RUN_TEST_IDLE,0u);
    }
    #else
    dp_get_bytes(G4M_BsrPattern_ID,0u,1u);
    if (return_bytes)
    {
        #ifdef ENABLE_DISPLAY
        dp_display_text("\r\nLoading BSR...");
        #endif
        dp_get_and_DRSCAN_in(G4M_BsrPattern_ID, global_uint1, 0u);
        goto_jtag_state(JTAG_RUN_TEST_IDLE,0u);
    }
    #endif
    
    
    return;
}

void dp_G4M_perform_isc_enable(void)
{
    
    pgmmode_flag = TRUE;
    dp_flush_global_buf1();
    global_buf1[0] |= (G4M_ALGO_VERSION & 0x3fu);
    global_buf1[2] |= (G4M_DIRECTC_VERSION & 0x3fu) << 1u;
    global_buf1[2] |= (DIRECTC_PROGRAMMING & 0x7u) << 7u;
    global_buf1[3] |= (DIRECTC_PROGRAMMING & 0x7u) >> 1u;
    global_buf1[3] |= (JTAG_PROGRAMMING_PROTOCOL & 0x7u) << 2u;
    
    opcode = ISC_ENABLE;
    IRSCAN_in();
    DRSCAN_in(0u, ISC_STATUS_REGISTER_BIT_LENGTH, global_buf1);
    goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
    dp_delay(G4M_STANDARD_DELAY);
    
    opcode = ISC_ENABLE;
    dp_G4M_device_poll(32u, 31u);
    
    
    if ( (error_code != DPE_SUCCESS) || 
    ((poll_buf[0] & 0x1u) == 1u) || 
    ( (poll_buf[0] == 0) && (poll_buf[1] == 0) && (poll_buf[2] == 0) && (poll_buf[3] == 0)))
    {
        #ifdef ENABLE_DISPLAY
        dp_display_text("\r\nFailed to enter programming mode.");
        #endif
        error_code = DPE_INIT_FAILURE;
    }
    
    #ifdef ENABLE_DISPLAY
    dp_display_text("\r\nISC_ENABLE_RESULT: ");
    dp_display_array(poll_buf,4u,HEX);
    
    /* Display CRCERR */
    global_uchar1 = poll_buf[0] & 0x1u;
    dp_display_text("\r\nCRCERR: ");
    dp_display_value(global_uchar1,HEX);
    
    /* Display EDCERR */
    global_uchar1 = (poll_buf[0] & 0x2u) >> 1u;
    dp_display_text("\r\nEDCERR: ");
    dp_display_value(global_uchar1,HEX);
    
    /* Display TEMPRANGE */
    global_uchar1 = (poll_buf[0] >> 2) & 0x7u;
    dp_display_text("\r\nTEMPRANGE:");
    dp_display_value(global_uchar1,HEX);
    if (global_uchar1 == 0u)
    {
        dp_display_text("\r\nTEMPRANGE: COLD");
    }
    else if (global_uchar1 == 1u)
    {
        dp_display_text("\r\nTEMPRANGE: ROOM");
    }
    if (global_uchar1 == 2u)
    {
        dp_display_text("\r\nTEMPRANGE: HOT");
    }
    
    
    /* Display VPPRANGE */
    global_uchar1 = (poll_buf[0] >> 5) & 0x7u;
    dp_display_text("\r\nVPPRANGE:");
    dp_display_value(global_uchar1,HEX);
    if (global_uchar1 == 0u)
    {
        dp_display_text("\r\nVPPRANGE: LOW");
    }
    else if (global_uchar1 == 1u)
    {
        dp_display_text("\r\nVPPRANGE: NOMINAL");
    }
    if (global_uchar1 == 2u)
    {
        dp_display_text("\r\nVPPRANGE: HIGH");
    }
    
    /* Display TEMP */
    dp_display_text("\r\nTEMP:");
    dp_display_value(poll_buf[1],HEX);
    
    /* Display VPP */
    dp_display_text("\r\nVPP:");
    dp_display_value(poll_buf[2],HEX);
    #endif
    
    
    return;
}

/* Enter programming mode */
void dp_G4M_initialize(void)
{
    dp_G4M_poll_device_ready();
    if (error_code == DPE_SUCCESS)
    {
        dp_set_pgm_mode();
        if (error_code == DPE_SUCCESS)
        {
            dp_G4M_read_security();
            if ((error_code == DPE_SUCCESS) && (sec_ul & 0x2))
            {
                dp_G4M_unlock_upk1();
                if (error_code == DPE_SUCCESS)
                {
                    dp_G4M_unlock_upk2();
                }
            }
        }
        dp_G4M_load_bsr();
        if (error_code == DPE_SUCCESS)
        {
            dp_G4M_perform_isc_enable();
        }
    }
    
    return;
}


/* Function is used to exit programming mode */
void dp_G4M_exit(void)
{
    
    if (pgmmode_flag == TRUE)
    {
        opcode = ISC_DISABLE;
        IRSCAN_in();
        goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
        dp_delay(G4M_STANDARD_DELAY);
        
        opcode = ISC_DISABLE;
        dp_G4M_device_poll(32u, 31u);
        #ifdef ENABLE_DISPLAY
        if (error_code != DPE_SUCCESS)
        {
            dp_display_text("\r\nFailed to disable programming mode.");
        }
        #endif
    }
    #ifdef ENABLE_DISPLAY
    dp_G4M_read_fsn();
    #endif
    goto_jtag_state(JTAG_TEST_LOGIC_RESET,0u);
    return;
}

void dp_set_mode(void)
{
    
    opcode = G4M_FRAME_INIT;
    IRSCAN_in();
    DRSCAN_in(0u, G4M_STATUS_REGISTER_BIT_LENGTH, &pgmmode);
    goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);	
    dp_delay(G4M_STANDARD_DELAY);
    dp_G4M_device_poll(8u, 7u);
    #ifdef ENABLE_DISPLAY
    if (error_code != DPE_SUCCESS)
    {
        dp_display_text("r\nFailed to set programming mode.");
    }
    #endif
    
    return;
}

void dp_G4M_setup_eNVM(DPUCHAR BlockID)
{
    envm_only_flag = FALSE;
    
    DataIndex = 0u;
    for (; global_uint2 <= global_uint1; global_uint2++)
    {
        /* get the number of blocks */
        /* Global ulong1 is used to hold the number of blocks within the components */
        global_ulong1 = dp_get_bytes(G4M_NUMBER_OF_BLOCKS_ID,(DPULONG)(((global_uint2 - 1u) * 22u) / 8u),4u);
        global_ulong1 >>= ((global_uint2 - 1U)* 22u) % 8u;
        global_ulong1 &= 0x3FFFFFu;
        
        /* Determine component types in bitstream */
        global_uchar1 = (DPUCHAR) dp_get_bytes(BlockID,COMPONENT_TYPE_IN_HEADER_BYTE + DataIndex/8u,1u);
        if ( (global_uchar1 == G4M_FPGA) || (global_uchar1 == G4M_KEYS) )
        {
            envm_only_flag = FALSE;
            break;
        }
        else if(global_uchar1 == G4M_ENVM)
        {
            envm_only_flag = TRUE;
        }
        DataIndex += (G4M_FRAME_BIT_LENGTH * global_ulong1);
    }
    
    
    if (envm_only_flag == TRUE)
    {
        dp_MSS_ADDR_CONFIG();
        if (error_code == DPE_SUCCESS)
        {
            dp_MSS_RD_DATA_SETUP();
            if (error_code == DPE_SUCCESS)
            {
                dp_MSS_ADDR_CONFIG();
                if (error_code == DPE_SUCCESS)
                {
                    dp_MSS_WR_DATA_SETUP();
                }
            }
        }
    }
    return;
}

void dp_G4M_post_setup_eNVM(void)
{
    
    if (envm_only_flag == TRUE)
    {
        dp_MSS_ADDR_CONFIG();
        if (error_code == DPE_SUCCESS)
        {
            dp_MSS_WR_DATA();
        }
    }
    return;
}

void dp_MSS_ADDR_CONFIG(void)
{
    dp_flush_global_buf1();
    
    global_buf1[0] = 0x0cu;
    global_buf1[1] = 0x80u;
    global_buf1[2] = 0x03u;
    global_buf1[3] = 0x40u;
    global_buf1[4] = 0x02u;
    global_buf1[5] = 0x00u;
    global_buf1[6] = 0x00u;
    global_buf1[7] = 0x00u;
    
    opcode = G4M_MSSADDR;
    IRSCAN_in();
    DRSCAN_in(0u, G4M_MSSADDR_BIT_LENGTH, global_buf1);
    goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
    dp_delay(G4M_STANDARD_DELAY);
    
    
    opcode = G4M_MSSADDR;
    dp_G4M_device_poll(64u, 63u);
    
    return;
}
void dp_MSS_RD_DATA_SETUP(void)
{
    
    dp_flush_global_buf1();
    global_buf1[0] = 0x04u;
    
    opcode = G4M_MSSRD;
    IRSCAN_in();
    DRSCAN_in(0u, G4M_MSSRD_BIT_LENGTH, global_buf1);
    goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
    dp_delay(G4M_STANDARD_DELAY);
    
    
    opcode = G4M_MSSRD;
    dp_G4M_device_poll(16u, 15u);
    #ifdef ENABLE_DISPLAY
    if ( (poll_buf[0] & 0x2u) == 1)
    {
        dp_display_text("\r\nMSS_RD_DATA_SETUP: msserr");
    }
    if ( (poll_buf[0] & 0x1u) == 1)
    {
        dp_display_text("\r\nMSS_RD_DATA_SETUP: secerr");
    }
    #endif
    dp_read_shared_buffer(1u);
    
    for (global_uchar1 = 0u; global_uchar1 < 4u; global_uchar1++)
    {
        SYSREG_TEMP[global_uchar1] = shared_buf[global_uchar1];
    }
    
    return;
}

void dp_MSS_WR_DATA_SETUP(void)
{
    
    dp_flush_global_buf1();
    for(global_uchar1 = 0u; global_uchar1 < 4u; global_uchar1++)
    {
        global_buf1[global_uchar1] = SYSREG_TEMP[global_uchar1];
    }
    global_buf1[0] |= 0xe0u;
    global_buf1[1] |= 0x1fu;
    
    opcode = G4M_MSSWR;
    IRSCAN_in();
    DRSCAN_in(0u, G4M_MSSWR_BIT_LENGTH, global_buf1);
    goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
    dp_delay(G4M_STANDARD_DELAY);
    
    
    opcode = G4M_MSSWR;
    dp_G4M_device_poll(32u, 31u);
    #ifdef ENABLE_DISPLAY
    if ( (poll_buf[0] & 0x2u) == 1)
    {
        dp_display_text("\r\nMSS_WR_DATA_SETUP: msserr");
    }
    if ( (poll_buf[0] & 0x1u) == 1)
    {
        dp_display_text("\r\nMSS_WR_DATA_SETUP: secerr");
    }
    #endif
    
    return;
}

void dp_MSS_WR_DATA(void)
{
    #ifdef ENABLE_DISPLAY
    dp_display_text("\r\nRestore user FREQRNG setting ");
    dp_display_array(SYSREG_TEMP,4u,HEX);
    dp_display_text("\r\n");
    #endif
    
    opcode = G4M_MSSWR;
    IRSCAN_in();
    DRSCAN_in(0u, G4M_MSSWR_BIT_LENGTH, SYSREG_TEMP);
    goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
    dp_delay(G4M_STANDARD_DELAY);
    
    opcode = G4M_MSSWR;
    dp_G4M_device_poll(32u, 31u);
    #ifdef ENABLE_DISPLAY
    if ( (poll_buf[0] & 0x2u) == 1)
    {
        dp_display_text("\r\nMSS_WR_DATA: msserr");
    }
    if ( (poll_buf[0] & 0x1u) == 1)
    {
        dp_display_text("\r\nMSS_WR_DATA: secerr");
    }
    #endif
    
    
    return;
}
/* global_uint2 is iCurComponentNo
global_uint1 is iCurNumOfComponents 
*/
void dp_G4M_prepare_bitstream(void)
{
    pgmmode = 0x2u;
    dp_set_mode();
    
    if ((error_code == DPE_SUCCESS) && (sec_ul & 0x2u))
    {
        if ((DPUINT)dp_get_bytes(Header_ID,G4M_VERIFYDATASIZE_OFFSET,G4M_DATASIZE_BYTE_LENGTH) != 0u)
        {
            /* Both global_unit1 and global_unit2 have DATASIZE, ENVMDATASIZE and ENVMVERIFYDATASIZE; */
            global_uint2 = (DPUINT)dp_get_bytes(Header_ID,G4M_DATASIZE_OFFSET,G4M_DATASIZE_BYTE_LENGTH) +
            (DPUINT)dp_get_bytes(Header_ID,G4M_ENVMDATASIZE_OFFSET,G4M_DATASIZE_BYTE_LENGTH) +
            (DPUINT)dp_get_bytes(Header_ID,G4M_ENVMVERIFYDATASIZE_OFFSET,G4M_DATASIZE_BYTE_LENGTH) + 1u;
            global_uint1 = global_uint2 + 1u;
            dp_G4M_process_predata(G4M_VerifyDataStream_ID);
        }
        else
        {
            global_uint2 = 1u;
            global_uint1 = 2u;
            dp_G4M_process_predata(G4M_datastream_ID);
        }
    }
    else
    {
        global_uint2 = 1u;
        global_uint1 = 2u;
        dp_G4M_process_predata(G4M_datastream_ID);
    }
    return;
}

void dp_G4M_process_predata(DPUCHAR BlockID)
{
    DataIndex = 0u;  
    /* Global unit1 is used to hold the number of components */
    /* Loop through the number of components */
    #ifdef ENABLE_DISPLAY
    dp_display_text("\r\n");
    #endif
    
    
    for (; global_uint2 <= global_uint1; global_uint2++)
    {
        /* get the number of blocks */
        /* Global ulong1 is used to hold the number of blocks within the components */
        if (global_uint2 != global_uint1)
        {
            global_ulong1 = dp_get_bytes(G4M_NUMBER_OF_BLOCKS_ID,(DPULONG)(((global_uint2 - 1u) * 22u) / 8u),4u);
            global_ulong1 >>= ((global_uint2 - 1U)* 22u) % 8u;
            global_ulong1 &= 0x3FFFFFu;
        }
        else
        {
            global_ulong1 = 91u;
        }
        
        
        opcode = G4M_FRAME_DATA;
        IRSCAN_in();
        dp_get_and_DRSCAN_in(BlockID, G4M_FRAME_BIT_LENGTH, DataIndex);
        
        for (global_ulong2 = 1u; global_ulong2 <= global_ulong1; global_ulong2++)
        {
            goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
            dp_delay(G4M_STANDARD_DELAY);
            
            opcode = G4M_FRAME_DATA;
            if (global_ulong2 == global_ulong1)
            {
                dp_G4M_device_poll(128u, 127u);
            }
            else
            {
                dp_G4M_device_shift_and_poll(128u, 127u, BlockID, DataIndex + G4M_FRAME_BIT_LENGTH);
            }
            if (error_code != DPE_SUCCESS)
            {
                global_uint2 = global_uint1;
                break;
            }
            else if ((poll_buf[0] & 0x18u) != 0u)
            {
                dp_G4M_get_data_status();
                if (error_code != DPE_SUCCESS)
                {
                    global_uint2 = global_uint1;
                    break;
                }
                else if ((poll_buf[0] & 0x4u) != 0u)
                {
                    global_uint2 = global_uint1;
                    break;
                }
            }
            DataIndex += G4M_FRAME_BIT_LENGTH;
        }
    }
    
    return;
}


void dp_G4M_process_data(DPUCHAR BlockID)
{
    DataIndex = 0u;  
    /* Global unit1 is used to hold the number of components */
    /* Loop through the number of components */
    #ifdef ENABLE_DISPLAY
    dp_display_text("\r\n");
    #endif
    
    
    for (; global_uint2 <= global_uint1; global_uint2++)
    {
        /* get the number of blocks */
        /* Global ulong1 is used to hold the number of blocks within the components */
        global_ulong1 = dp_get_bytes(G4M_NUMBER_OF_BLOCKS_ID,(DPULONG)(((global_uint2 - 1u) * 22u) / 8u),4u);
        global_ulong1 >>= ((global_uint2 - 1U)* 22u) % 8u;
        global_ulong1 &= 0x3FFFFFu;
        
        #ifdef ENABLE_DISPLAY
        dp_display_text("\r\nProcessing component ");
        dp_display_value(global_uint2,DEC);
        dp_display_text(". Please wait...");
        #endif
        
        opcode = G4M_FRAME_DATA;
        IRSCAN_in();
        dp_get_and_DRSCAN_in(BlockID, G4M_FRAME_BIT_LENGTH, DataIndex);
        
        #ifdef ENABLE_DISPLAY
        old_progress = 0;
        #endif
        for (global_ulong2 = 1u; global_ulong2 <= global_ulong1; global_ulong2++)
        {
            #ifdef ENABLE_DISPLAY
            new_progress = (global_ulong2 *100 / global_ulong1);
            if (new_progress != old_progress)
            {
                #ifdef ENABLE_EMBEDDED_SUPPORT
                dp_report_progress(new_progress);
                #endif
                old_progress = new_progress;
            }
            #endif
            
            goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
            dp_delay(G4M_STANDARD_DELAY);
            
            opcode = G4M_FRAME_DATA;
            if (global_ulong2 == global_ulong1)
            {
                dp_G4M_device_poll(128u, 127u);
            }
            else
            {
                dp_G4M_device_shift_and_poll(128u, 127u, BlockID, DataIndex + G4M_FRAME_BIT_LENGTH);
            }
            if (error_code != DPE_SUCCESS)
            {
                #ifdef ENABLE_DISPLAY
                dp_display_text("\r\nInstruction timed out.");
                dp_display_text("\r\ncomponentNo: ");
                dp_display_value(global_uint2, DEC);
                dp_display_text("\r\nblockNo: ");
                dp_display_value(global_ulong2, DEC);
                #endif
                error_code = DPE_PROCESS_DATA_ERROR;
                global_uint2 = global_uint1;
                break;
            }
            else if ((poll_buf[0] & 0x18u) != 0u)
            {
                dp_G4M_get_data_status();
                if (error_code != DPE_SUCCESS)
                {
                    #ifdef ENABLE_DISPLAY
                    dp_display_text("\r\nInstruction timed out.");
                    dp_display_text("\r\ncomponentNo: ");
                    dp_display_value(global_uint2, DEC);
                    dp_display_text("\r\nblockNo: ");
                    dp_display_value(global_ulong2, DEC);
                    #endif
                    error_code = DPE_PROCESS_DATA_ERROR;
                    global_uint2 = global_uint1;
                    break;
                }
                else if ((poll_buf[0] & 0x4u) != 0u)
                {
                    #ifdef ENABLE_DISPLAY
                    dp_display_text("\r\ncomponentNo: ");
                    dp_display_value(global_uint2, DEC);
                    dp_display_text("\r\nblockNo: ");
                    dp_display_value(global_ulong2, DEC);
                    dp_display_text("\r\nDATA_STATUS_RESULT: ");
                    dp_display_array(poll_buf,4u,HEX);
                    dp_display_text("\r\nERRORCODE: ");
                    dp_display_value((poll_buf[0]>>3u) & 0x1fu,HEX);
                    dp_display_text("\r\nAUTHERRCODE: ");
                    dp_display_value(poll_buf[1],HEX);
                    #endif
                    error_code = DPE_PROCESS_DATA_ERROR;
                    global_uint2 = global_uint1;
                    break;
                }
            }
            
            
            DataIndex += G4M_FRAME_BIT_LENGTH;
        }
    }
    
    return;
}

void dp_G4M_get_data_status(void)
{
    opcode = G4M_FRAME_STATUS;
    IRSCAN_in();
    DRSCAN_in(0u, ISC_STATUS_REGISTER_BIT_LENGTH, (DPUCHAR*)(DPUCHAR*)DPNULL);
    goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
    dp_delay(G4M_STANDARD_DELAY);
    
    opcode = G4M_FRAME_STATUS;
    dp_G4M_device_poll(32u, 31u);
    
    return;
}

void dp_G4M_read_security(void)
{
    dp_G4M_query_security();
    if ((error_code == DPE_SUCCESS) && (security_queried == FALSE) )
    {
        dp_G4M_unlock_dpk();
        if (error_code == DPE_SUCCESS)
        {
            dp_G4M_query_security();
            if ((error_code == DPE_SUCCESS) && (security_queried == FALSE) )
            {
                #ifdef ENABLE_DISPLAY
                dp_display_text("\r\nWarning: Security cannot be read even after unlocking debug pass key.");	
                #endif
            }
        }
    }
    sec_ul = shared_buf[4] >> 2u;
    return;
}
void dp_G4M_query_security(void)
{
    opcode = G4M_QUERY_SECURITY;
    IRSCAN_in();
    DRSCAN_in(0u, G4M_SECURITY_STATUS_REGISTER_BIT_LENGTH, (DPUCHAR*)(DPUCHAR*)DPNULL);
    goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
    opcode = G4M_QUERY_SECURITY;
    dp_G4M_device_poll(16u, 15u);
    if (error_code != DPE_SUCCESS)
    {
        #ifdef ENABLE_DISPLAY
        dp_display_text("\r\nFailed to query security information.");	
        #endif
    }
    else
    {
        dp_read_shared_buffer(3u);
        for (poll_index = 0u;poll_index < 40u; poll_index++)
        {
            if (shared_buf[poll_index] != 0u)
            {
                security_queried = TRUE;
                break;
            }
        }
        
        #ifdef ENABLE_DISPLAY
        if (security_queried == TRUE)
        {
            dp_display_text("\r\n--- Security locks and configuration settings ---\r\n");	
            dp_display_array(shared_buf,42u,HEX);
        }
        #endif
    }
    return;
}

void dp_G4M_unlock_dpk(void)
{
    dp_get_data(G4M_DPK_ID,0u);
    if (return_bytes == 0u)
    {
        #ifdef ENABLE_DISPLAY
        dp_display_text("\r\nWarning: DPK data is missing.");
        #endif
    }
    else
    {
        dp_G4M_load_dpk();
        if (error_code == DPE_SUCCESS)
        {
            opcode = G4M_UNLOCK_DEBUG_PASSCODE;
            IRSCAN_in();
            DRSCAN_in(0u, G4M_STATUS_REGISTER_BIT_LENGTH, (DPUCHAR*)(DPUCHAR*)DPNULL);
            goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
            dp_delay(G4M_STANDARD_DELAY);
        }
        dp_G4M_device_poll(8u, 7u);
        if ((error_code != DPE_SUCCESS) || ((poll_buf[0] & 0x3u) != 0x1u) )
        {
            #ifdef ENABLE_DISPLAY
            dp_display_text("\r\nFailed to unlock debug pass key.");
            #endif
            error_code = DPE_MATCH_ERROR;
        }
        else
        {
            #ifdef ENABLE_DISPLAY
            dp_display_text("\r\nDebug security (DPK) is unlocked.");
            #endif
        }
    }
    return;
}

void dp_G4M_unlock_upk1(void)
{
    dp_get_data(G4M_UPK1_ID,0u);
    if (return_bytes == 0u)
    {
        #ifdef ENABLE_DISPLAY
        dp_display_text("\r\nWarning: UPK1 data is missing.");
        #endif
    }
    else
    {
        dp_G4M_load_upk1();
        if (error_code == DPE_SUCCESS)
        {
            opcode = G4M_UNLOCK_USER_PASSCODE;
            IRSCAN_in();
            DRSCAN_in(0u, G4M_STATUS_REGISTER_BIT_LENGTH, (DPUCHAR*)(DPUCHAR*)DPNULL);
            goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
            dp_delay(G4M_STANDARD_DELAY);
        }
        dp_G4M_device_poll(8u, 7u);
        if ((error_code != DPE_SUCCESS) || ((poll_buf[0] & 0x3u) != 0x1u) )
        {
            #ifdef ENABLE_DISPLAY
            dp_display_text("\r\nFailed to unlock user pass key 1.");
            #endif
            error_code = DPE_MATCH_ERROR;
        }
        else
        {
            #ifdef ENABLE_DISPLAY
            dp_display_text("\r\nUser security (DPK1) is unlocked.");
            #endif
        }
    }
    return;
}

void dp_G4M_unlock_upk2(void)
{
    dp_get_data(G4M_UPK2_ID,0u);
    if (return_bytes == 0u)
    {
        #ifdef ENABLE_DISPLAY
        dp_display_text("\r\nWarning: UPK2 data is missing.");
        #endif
    }
    else
    {
        dp_G4M_load_upk2();
        if (error_code == DPE_SUCCESS)
        {
            opcode = G4M_UNLOCK_VENDOR_PASSCODE;
            IRSCAN_in();
            DRSCAN_in(0u, G4M_STATUS_REGISTER_BIT_LENGTH, (DPUCHAR*)(DPUCHAR*)DPNULL);
            goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
            dp_delay(G4M_STANDARD_DELAY);
        }
        dp_G4M_device_poll(8u, 7u);
        if ((error_code != DPE_SUCCESS) || ((poll_buf[0] & 0x3u) != 0x1u) )
        {
            #ifdef ENABLE_DISPLAY
            dp_display_text("\r\nFailed to unlock user pass key 2.");
            #endif
            error_code = DPE_MATCH_ERROR;
        }
        else
        {
            #ifdef ENABLE_DISPLAY
            dp_display_text("\r\nUser security (DPK2) is unlocked.");
            #endif
        }
    }
    return;
}

void dp_G4M_load_dpk(void)
{
    opcode = G4M_KEYLO;
    IRSCAN_in();
    dp_get_and_DRSCAN_in(G4M_DPK_ID, G4M_FRAME_BIT_LENGTH, 0u);
    goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
    dp_delay(G4M_STANDARD_DELAY);
    dp_G4M_device_poll(128u, 127u);
    if (error_code != DPE_SUCCESS)
    {
        #ifdef ENABLE_DISPLAY
        dp_display_text("\r\nFailed to load keylo. \r\nkeylo_result: ");
        dp_display_array(poll_buf,G4M_FRAME_BYTE_LENGTH,HEX);
        #endif
        error_code = DPE_MATCH_ERROR;
    }
    else
    {
        opcode = G4M_KEYHI;
        IRSCAN_in();
        dp_get_and_DRSCAN_in(G4M_DPK_ID, G4M_FRAME_BIT_LENGTH, G4M_FRAME_BIT_LENGTH);
        goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
        dp_delay(G4M_STANDARD_DELAY);
        dp_G4M_device_poll(128u, 127u);
        if (error_code != DPE_SUCCESS)
        {
            #ifdef ENABLE_DISPLAY
            dp_display_text("\r\nFailed to load keyhi. \r\nkeyhi_result: ");
            dp_display_array(poll_buf,G4M_FRAME_BYTE_LENGTH,HEX);
            #endif
            error_code = DPE_MATCH_ERROR;
        }
    }
    
    return;
}

void dp_G4M_load_upk1(void)
{
    opcode = G4M_KEYLO;
    IRSCAN_in();
    dp_get_and_DRSCAN_in(G4M_UPK1_ID, G4M_FRAME_BIT_LENGTH, 0u);
    goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
    dp_delay(G4M_STANDARD_DELAY);
    dp_G4M_device_poll(128u, 127u);
    if (error_code != DPE_SUCCESS)
    {
        #ifdef ENABLE_DISPLAY
        dp_display_text("\r\nFailed to load keylo. \r\nkeylo_result: ");
        dp_display_array(poll_buf,G4M_FRAME_BYTE_LENGTH,HEX);
        #endif
        error_code = DPE_MATCH_ERROR;
    }
    else
    {
        opcode = G4M_KEYHI;
        IRSCAN_in();
        dp_get_and_DRSCAN_in(G4M_UPK1_ID, G4M_FRAME_BIT_LENGTH, G4M_FRAME_BIT_LENGTH);
        goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
        dp_delay(G4M_STANDARD_DELAY);
        dp_G4M_device_poll(128u, 127u);
        if (error_code != DPE_SUCCESS)
        {
            #ifdef ENABLE_DISPLAY
            dp_display_text("\r\nFailed to load keyhi. \r\nkeyhi_result: ");
            dp_display_array(poll_buf,G4M_FRAME_BYTE_LENGTH,HEX);
            #endif
            error_code = DPE_MATCH_ERROR;
        }
    }
    
    return;
}

void dp_G4M_load_upk2(void)
{
    opcode = G4M_KEYLO;
    IRSCAN_in();
    dp_get_and_DRSCAN_in(G4M_UPK2_ID, G4M_FRAME_BIT_LENGTH, 0u);
    goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
    dp_delay(G4M_STANDARD_DELAY);
    dp_G4M_device_poll(128u, 127u);
    if (error_code != DPE_SUCCESS)
    {
        #ifdef ENABLE_DISPLAY
        dp_display_text("\r\nFailed to load keylo. \r\nkeylo_result: ");
        dp_display_array(poll_buf,G4M_FRAME_BYTE_LENGTH,HEX);
        #endif
        error_code = DPE_MATCH_ERROR;
    }
    else
    {
        opcode = G4M_KEYHI;
        IRSCAN_in();
        dp_get_and_DRSCAN_in(G4M_UPK2_ID, G4M_FRAME_BIT_LENGTH, G4M_FRAME_BIT_LENGTH);
        goto_jtag_state(JTAG_RUN_TEST_IDLE,G4M_STANDARD_CYCLES);
        dp_delay(G4M_STANDARD_DELAY);
        dp_G4M_device_poll(128u, 127u);
        if (error_code != DPE_SUCCESS)
        {
            #ifdef ENABLE_DISPLAY
            dp_display_text("\r\nFailed to load keyhi. \r\nkeyhi_result: ");
            dp_display_array(poll_buf,G4M_FRAME_BYTE_LENGTH,HEX);
            #endif
            error_code = DPE_MATCH_ERROR;
        }
    }
    
    return;
}


#endif /* ENABLE_G4_SUPPORT */

/* *************** End of File *************** */