aboutsummaryrefslogtreecommitdiffstats
path: root/decoder/source/etmv4/trc_pkt_proc_etmv4i_impl.cpp
blob: 0607c192f8799fdf12a61733e44d5f2f14200b28 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
/*
 * \file       trc_pkt_proc_etmv4i_impl.cpp
 * \brief      OpenCSD : 
 * 
 * \copyright  Copyright (c) 2015, ARM Limited. All Rights Reserved.
 */

/* 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, 
 * this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution. 
 * 
 * 3. Neither the name of the copyright holder nor the names of its contributors 
 * may be used to endorse or promote products derived from this software without 
 * specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */ 

#include "trc_pkt_proc_etmv4i_impl.h"

/* Trace raw input buffer class */
TraceRawBuffer::TraceRawBuffer()
{
    m_bufSize = 0;
    m_bufProcessed = 0;
    m_pBuffer = 0;
    pkt = 0;
}

// init the buffer
void TraceRawBuffer::init(const uint32_t size, const uint8_t *rawtrace, std::vector<uint8_t> *out_packet)
{
    m_bufSize = size;
    m_bufProcessed = 0;
    m_pBuffer = rawtrace;
    pkt = out_packet;
}

void TraceRawBuffer::copyByteToPkt()
{
    if (!empty()) {
        pkt->push_back(m_pBuffer[m_bufProcessed]);
        m_bufProcessed++;
    }
}
uint8_t TraceRawBuffer::peekNextByte()
{
    uint8_t val = 0;
    if (!empty())
        val = m_pBuffer[m_bufProcessed];
    return val;
}

/* trace etmv4 packet processing class */
EtmV4IPktProcImpl::EtmV4IPktProcImpl() :
    m_isInit(false),
    m_interface(0),
    m_first_trace_info(false)
{
    
}

EtmV4IPktProcImpl::~EtmV4IPktProcImpl()
{
}

void EtmV4IPktProcImpl::Initialise(TrcPktProcEtmV4I *p_interface)
{
     if(p_interface)
     {
         m_interface = p_interface;
         m_isInit = true;
     }
     InitProcessorState();
}

ocsd_err_t EtmV4IPktProcImpl::Configure(const EtmV4Config *p_config)
{
    ocsd_err_t err = OCSD_OK;
    if(p_config != 0)
    {
        m_config = *p_config;
        BuildIPacketTable();    // packet table based on config
    }
    else
    {
        err = OCSD_ERR_INVALID_PARAM_VAL;
        if(m_isInit)
            m_interface->LogError(ocsdError(OCSD_ERR_SEV_ERROR,err));
    }
    return err;
}

ocsd_datapath_resp_t EtmV4IPktProcImpl::processData(  const ocsd_trc_index_t index,
                                    const uint32_t dataBlockSize,
                                    const uint8_t *pDataBlock,
                                    uint32_t *numBytesProcessed)
{
    ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
    m_trcIn.init(dataBlockSize, pDataBlock, &m_currPacketData);
    m_blockIndex = index;
    bool done = false;
    uint8_t nextByte;

    do
    {
        try 
        {
           /* while (((m_blockBytesProcessed < dataBlockSize) ||
                ((m_blockBytesProcessed == dataBlockSize) && (m_process_state == SEND_PKT))) &&
                OCSD_DATA_RESP_IS_CONT(resp))*/
            while ( (!m_trcIn.empty() || (m_process_state == SEND_PKT)) &&
                    OCSD_DATA_RESP_IS_CONT(resp)
                )
            {
                switch (m_process_state)
                {
                case PROC_HDR:
                    m_packet_index = m_blockIndex + m_trcIn.processed();
                    if (m_is_sync)
                    {
                        nextByte = m_trcIn.peekNextByte();
                        m_pIPktFn = m_i_table[nextByte].pptkFn;
                        m_curr_packet.type = m_i_table[nextByte].pkt_type;
                    }
                    else
                    {
                        // unsynced - process data until we see a sync point
                        m_pIPktFn = &EtmV4IPktProcImpl::iNotSync;
                        m_curr_packet.type = ETM4_PKT_I_NOTSYNC;
                    }
                    m_process_state = PROC_DATA;

                case PROC_DATA:
                    // loop till full packet or no more data...
                    while (!m_trcIn.empty() && (m_process_state == PROC_DATA))
                    {
                        nextByte = m_trcIn.peekNextByte();
                        m_trcIn.copyByteToPkt();  // move next byte into the packet
    //                    m_currPacketData.push_back(pDataBlock[m_blockBytesProcessed]);
    //                    m_blockBytesProcessed++;
                        (this->*m_pIPktFn)(nextByte);
                    }
                    break;

                case SEND_PKT:
                    resp = outputPacket();
                    InitPacketState();
                    m_process_state = PROC_HDR;
                    break;

                case SEND_UNSYNCED:
                    resp = outputUnsyncedRawPacket();
                    if (m_update_on_unsync_packet_index != 0)
                    {
                        m_packet_index = m_update_on_unsync_packet_index;
                        m_update_on_unsync_packet_index = 0;
                    }
                    m_process_state = PROC_DATA;        // after dumping unsynced data, still in data mode.
                    break;
                }
            }
            done = true;
        }
        catch(ocsdError &err)
        {
            done = true;
            m_interface->LogError(err);
            if( (err.getErrorCode() == OCSD_ERR_BAD_PACKET_SEQ) ||
                (err.getErrorCode() == OCSD_ERR_INVALID_PCKT_HDR))
            {
                // send invalid packets up the pipe to let the next stage decide what to do.
                m_process_state = SEND_PKT; 
                done = false;
            }
            else
            {
                // bail out on any other error.
                resp = OCSD_RESP_FATAL_INVALID_DATA;
            }
        }
        catch(...)
        {
            done = true;
            /// vv bad at this point.
            resp = OCSD_RESP_FATAL_SYS_ERR;
            const ocsdError &fatal = ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_FAIL,m_packet_index,m_config.getTraceID(),"Unknown System Error decoding trace.");
            m_interface->LogError(fatal);
        }
    } while (!done);

    *numBytesProcessed = m_trcIn.processed();
    return resp;
}

ocsd_datapath_resp_t EtmV4IPktProcImpl::onEOT()
{
    ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
    // if we have a partial packet then send to attached sinks
    if(m_currPacketData.size() != 0)
    {
        m_curr_packet.updateErrType(ETM4_PKT_I_INCOMPLETE_EOT);
        resp = outputPacket();
        InitPacketState();
    }
    return resp;
}

ocsd_datapath_resp_t EtmV4IPktProcImpl::onReset()
{
    // prepare for new decoding session
    InitProcessorState();
    return OCSD_RESP_CONT;
}

ocsd_datapath_resp_t EtmV4IPktProcImpl::onFlush()
{
    // packet processor never holds on to flushable data (may have partial packet, 
    // but any full packets are immediately sent)
    return OCSD_RESP_CONT;
}

void EtmV4IPktProcImpl::InitPacketState()
{
    m_currPacketData.clear();
    m_curr_packet.initNextPacket(); // clear for next packet.
    m_update_on_unsync_packet_index = 0;
}

void EtmV4IPktProcImpl::InitProcessorState()
{
    InitPacketState();
    m_pIPktFn = &EtmV4IPktProcImpl::iNotSync;
    m_packet_index = 0;
    m_is_sync = false;
    m_first_trace_info = false;
    m_sent_notsync_packet = false;
    m_process_state = PROC_HDR;
    m_curr_packet.initStartState();
}

ocsd_datapath_resp_t EtmV4IPktProcImpl::outputPacket()
{
    ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
    resp = m_interface->outputOnAllInterfaces(m_packet_index,&m_curr_packet,&m_curr_packet.type,m_currPacketData);
    return resp;
}

ocsd_datapath_resp_t EtmV4IPktProcImpl::outputUnsyncedRawPacket()
{
    ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
    

    m_interface->outputRawPacketToMonitor(m_packet_index,&m_curr_packet,m_dump_unsynced_bytes,&m_currPacketData[0]);
        
    if(!m_sent_notsync_packet)
    {        
        resp = m_interface->outputDecodedPacket(m_packet_index,&m_curr_packet);
        m_sent_notsync_packet = true;
    }
    
    if(m_currPacketData.size() <= m_dump_unsynced_bytes)
        m_currPacketData.clear();
    else
        m_currPacketData.erase(m_currPacketData.begin(),m_currPacketData.begin()+m_dump_unsynced_bytes);

    return resp;
}

void EtmV4IPktProcImpl::iNotSync(const uint8_t lastByte)
{
    // is it an extension byte? 
    if (lastByte == 0x00) // TBD : add check for forced sync in here?
    {
        if (m_currPacketData.size() > 1)
        {
            m_dump_unsynced_bytes = m_currPacketData.size() - 1;
            m_process_state = SEND_UNSYNCED;
            // outputting some data then update packet index after so output indexes accurate
            m_update_on_unsync_packet_index = m_blockIndex + m_trcIn.processed() - 1;
        }
        else
            m_packet_index = m_blockIndex + m_trcIn.processed() - 1;  // set it up now otherwise.

        m_pIPktFn = m_i_table[lastByte].pptkFn;
    }
    else if (m_currPacketData.size() >= 8)
    {
        m_dump_unsynced_bytes = m_currPacketData.size();
        m_process_state = SEND_UNSYNCED;
        // outputting some data then update packet index after so output indexes accurate
        m_update_on_unsync_packet_index = m_blockIndex + m_trcIn.processed();
    }
}

void EtmV4IPktProcImpl::iPktNoPayload(const uint8_t lastByte)
{
    // some expansion may be required...
    switch(m_curr_packet.type)
    {
    case ETM4_PKT_I_ADDR_MATCH:
        m_curr_packet.setAddressExactMatch(lastByte & 0x3);
        break;

    case ETM4_PKT_I_EVENT:
        m_curr_packet.setEvent(lastByte & 0xF);
        break;

    case ETM4_PKT_I_NUM_DS_MKR:
    case ETM4_PKT_I_UNNUM_DS_MKR:
        m_curr_packet.setDataSyncMarker(lastByte & 0x7);
        break;

    // these just need the packet type - no processing required.
    case ETM4_PKT_I_COND_FLUSH:
    case ETM4_PKT_I_EXCEPT_RTN:
    case ETM4_PKT_I_TRACE_ON:
    case ETM4_PKT_I_FUNC_RET:
    case ETM4_PKT_I_IGNORE:
    default: break;
    }
    m_process_state = SEND_PKT; // now just send it....
}

void EtmV4IPktProcImpl::iPktReserved(const uint8_t lastByte)
{
    m_curr_packet.updateErrType(ETM4_PKT_I_RESERVED, lastByte);   // swap type for err type
    throw ocsdError(OCSD_ERR_SEV_ERROR, OCSD_ERR_INVALID_PCKT_HDR,m_packet_index,m_config.getTraceID());
}

void EtmV4IPktProcImpl::iPktInvalidCfg(const uint8_t lastByte)
{
    m_curr_packet.updateErrType(ETM4_PKT_I_RESERVED_CFG, lastByte);   // swap type for err type
    throw ocsdError(OCSD_ERR_SEV_ERROR, OCSD_ERR_INVALID_PCKT_HDR, m_packet_index, m_config.getTraceID());
}

void EtmV4IPktProcImpl::iPktExtension(const uint8_t lastByte)
{
    if(m_currPacketData.size() == 2)
    {
        // not sync and not next by 0x00 - not sync sequence
        if(!m_is_sync && (lastByte != 0x00))
        {
            m_pIPktFn = &EtmV4IPktProcImpl::iNotSync;
            m_curr_packet.type = ETM4_PKT_I_NOTSYNC;
            return;
        }

        switch(lastByte)
        {
        case 0x03: // discard packet.
            m_curr_packet.type = ETM4_PKT_I_DISCARD;
            m_process_state = SEND_PKT;
            break;

        case 0x05:
            m_curr_packet.type = ETM4_PKT_I_OVERFLOW;
            m_process_state = SEND_PKT;
            break;

        case 0x00:
            m_curr_packet.type = ETM4_PKT_I_ASYNC;
            m_pIPktFn = &EtmV4IPktProcImpl::iPktASync;  // handle subsequent bytes as async
            break;

        default:
            m_curr_packet.err_type = m_curr_packet.type;
            m_curr_packet.type = ETM4_PKT_I_BAD_SEQUENCE;
            m_process_state = SEND_PKT;
            break;
        }
    }
}

void EtmV4IPktProcImpl::iPktASync(const uint8_t lastByte)
{
    if(lastByte != 0x00)
    {
        // not sync and not next by 0x00 - not sync sequence if < 12
        if(!m_is_sync && m_currPacketData.size() != 12)
        {
            m_pIPktFn = &EtmV4IPktProcImpl::iNotSync;
            m_curr_packet.type = ETM4_PKT_I_NOTSYNC;
            return;
        }

        // 12 bytes and not valid sync sequence - not possible even if not synced
        m_process_state = SEND_PKT;
        if((m_currPacketData.size() != 12) || (lastByte != 0x80))
        {
            m_curr_packet.type = ETM4_PKT_I_BAD_SEQUENCE;
            m_curr_packet.err_type = ETM4_PKT_I_ASYNC;
        }
        else
             m_is_sync = true;  // found a sync packet, mark decoder as synchronised.
    }
    else if(m_currPacketData.size() == 12)
    {
        if(!m_is_sync)
        {
            // if we are not yet synced then ignore extra leading 0x00.
            m_dump_unsynced_bytes = 1;
            m_process_state = SEND_UNSYNCED;
        }
        else
        {
            // bad periodic ASYNC sequence.
            m_curr_packet.type = ETM4_PKT_I_BAD_SEQUENCE;
            m_curr_packet.err_type = ETM4_PKT_I_ASYNC;
            m_process_state = SEND_PKT;
        }
    }
}

void EtmV4IPktProcImpl::iPktTraceInfo(const uint8_t lastByte)
{
    if(m_currPacketData.size() == 1)    // header
    {
        //clear flags
        m_tinfo_sections.sectFlags = 0; // mark all sections as incomplete.
        m_tinfo_sections.ctrlBytes = 1; // assume only a single control section byte for now
        
    }
    else if(m_currPacketData.size() == 2) // first payload control byte
    {
        // figure out which sections are absent and set to true - opposite of bitfeild in byte;
        m_tinfo_sections.sectFlags = (~lastByte) & TINFO_ALL_SECT;

        // see if there is an extended control section, otherwise this byte is it.
        if((lastByte & 0x80) == 0x0)
            m_tinfo_sections.sectFlags |= TINFO_CTRL;
 
    }
    else
    {
        if(!(m_tinfo_sections.sectFlags & TINFO_CTRL))
        {
            m_tinfo_sections.sectFlags |= (lastByte & 0x80) ? 0 : TINFO_CTRL;
            m_tinfo_sections.ctrlBytes++;
        }
        else if(!(m_tinfo_sections.sectFlags & TINFO_INFO_SECT))
            m_tinfo_sections.sectFlags |= (lastByte & 0x80) ? 0 : TINFO_INFO_SECT;
        else if(!(m_tinfo_sections.sectFlags & TINFO_KEY_SECT))
            m_tinfo_sections.sectFlags |= (lastByte & 0x80) ? 0 : TINFO_KEY_SECT;
        else if(!(m_tinfo_sections.sectFlags & TINFO_SPEC_SECT))
            m_tinfo_sections.sectFlags |= (lastByte & 0x80) ? 0 : TINFO_SPEC_SECT;
        else if(!(m_tinfo_sections.sectFlags & TINFO_CYCT_SECT))
            m_tinfo_sections.sectFlags |= (lastByte & 0x80) ? 0 : TINFO_CYCT_SECT;
    }

    // all sections accounted for?
    if(m_tinfo_sections.sectFlags == TINFO_ALL)
    {
        // index of first section is number of payload control bytes + 1 for header byte
        unsigned idx = m_tinfo_sections.ctrlBytes + 1;
        uint32_t fieldVal = 0;
        uint8_t presSect = m_currPacketData[1] & TINFO_ALL_SECT;  // first payload control byte

        m_curr_packet.clearTraceInfo();

        if((presSect & TINFO_INFO_SECT) && (idx < m_currPacketData.size()))
        {
            idx += extractContField(m_currPacketData,idx,fieldVal);
            m_curr_packet.setTraceInfo(fieldVal);
        }
        if((presSect & TINFO_KEY_SECT) && (idx < m_currPacketData.size()))
        {
            idx += extractContField(m_currPacketData,idx,fieldVal);
            m_curr_packet.setTraceInfoKey(fieldVal);
        }
        if((presSect & TINFO_SPEC_SECT) && (idx < m_currPacketData.size()))
        {
            idx += extractContField(m_currPacketData,idx,fieldVal);
            m_curr_packet.setTraceInfoSpec(fieldVal);
        }
        if((presSect & TINFO_CYCT_SECT) && (idx < m_currPacketData.size()))
        {
            idx += extractContField(m_currPacketData,idx,fieldVal);
            m_curr_packet.setTraceInfoCyct(fieldVal);
        }
        m_process_state = SEND_PKT;
        m_first_trace_info = true;
    }

}

void EtmV4IPktProcImpl::iPktTimestamp(const uint8_t lastByte)
{
    // process the header byte
    if(m_currPacketData.size() == 1)
    {
        m_ccount_done = (bool)((lastByte & 0x1) == 0); // 0 = not present
        m_ts_done = false;
        m_ts_bytes = 0;
    }
    else
    {        
        if(!m_ts_done)
        {
            m_ts_bytes++;
            m_ts_done = (m_ts_bytes == 9) || ((lastByte & 0x80) == 0);
        }
        else if(!m_ccount_done)
        {
            m_ccount_done = (bool)((lastByte & 0x80) == 0);
            // TBD: check for oorange ccount - bad packet.
        }
    }

    if(m_ts_done && m_ccount_done)
    {        
        int idx = 1;
        uint64_t tsVal;
        int ts_bytes = extractContField64(m_currPacketData, idx, tsVal);
        int ts_bits = ts_bytes < 7 ? ts_bytes * 7 : 64;

        if(!m_curr_packet.pkt_valid.bits.ts_valid && m_first_trace_info)
            ts_bits = 64;   // after trace info, missing bits are all 0.

        m_curr_packet.setTS(tsVal,(uint8_t)ts_bits);

        if((m_currPacketData[0] & 0x1) == 0x1)
        {
            uint32_t countVal, countMask;
            
            idx += ts_bytes;           
            extractContField(m_currPacketData, idx, countVal, 3);    // only 3 possible count bytes.
            countMask = (((uint32_t)1UL << m_config.ccSize()) - 1); // mask of the CC size
            countVal &= countMask;
            m_curr_packet.setCycleCount(countVal);
        }

        m_process_state = SEND_PKT;
    }
}

void EtmV4IPktProcImpl::iPktException(const uint8_t lastByte)
{
    uint16_t excep_type = 0;

    switch(m_currPacketData.size())
    {
    case 1: m_excep_size = 3; break;
    case 2: if((lastByte & 0x80) == 0x00)
                m_excep_size = 2; 
            break;
    }

    if(m_currPacketData.size() ==  (unsigned)m_excep_size)
    {
        excep_type =  (m_currPacketData[1] >> 1) & 0x1F;
        uint8_t addr_interp = (m_currPacketData[1] & 0x40) >> 5 | (m_currPacketData[1] & 0x1);
        uint8_t m_fault_pending = 0;        
        uint8_t m_type = (m_config.coreProfile() == profile_CortexM) ? 1 : 0;

        // extended exception packet (probably M class);
        if(m_currPacketData[1] & 0x80)
        {
            excep_type |= ((uint16_t)m_currPacketData[2] & 0x1F) << 5;
            m_fault_pending = (m_currPacketData[2] >> 5)  & 0x1;
        }
        m_curr_packet.setExceptionInfo(excep_type,addr_interp,m_fault_pending, m_type);
        m_process_state = SEND_PKT;

        // allow the standard address packet handlers to process the address packet field for the exception.
    }
}

void EtmV4IPktProcImpl::iPktCycleCntF123(const uint8_t lastByte)
{
    ocsd_etmv4_i_pkt_type format = m_curr_packet.type;

    if( m_currPacketData.size() == 1)
    {
        m_count_done = m_commit_done = false; 
        m_has_count = true;

        if(format == ETM4_PKT_I_CCNT_F3)
        {
            // no commit section for TRCIDR0.COMMOPT == 1
            if(!m_config.commitOpt1())
            {
                m_curr_packet.setCommitElements(((lastByte >> 2) & 0x3) + 1);
            }
            // TBD: warning of non-valid CC threshold here?
            m_curr_packet.setCycleCount(m_curr_packet.getCCThreshold() + (lastByte & 0x3));
            m_process_state = SEND_PKT;
        }
        else if(format == ETM4_PKT_I_CCNT_F1) 
        {
            if((lastByte & 0x1) == 0x1)
            {
                m_has_count = false;
                m_count_done = true;
            }

            // no commit section for TRCIDR0.COMMOPT == 1
            if(m_config.commitOpt1())
                m_commit_done = true;
        }
    }
    else if((format == ETM4_PKT_I_CCNT_F2) && ( m_currPacketData.size() == 2))
    {
        int commit_offset = ((lastByte & 0x1) == 0x1) ? ((int)m_config.MaxSpecDepth() - 15) : 1;
        int commit_elements = ((lastByte >> 4) & 0xF);
        commit_elements += commit_offset;

        // TBD: warning if commit elements < 0?

        m_curr_packet.setCycleCount(m_curr_packet.getCCThreshold() + (lastByte & 0xF));
        m_curr_packet.setCommitElements(commit_elements);
        m_process_state = SEND_PKT;
    }
    else
    {
        // F1 and size 2 or more
        if(!m_commit_done)
            m_commit_done = ((lastByte & 0x80) == 0x00);
        else if(!m_count_done)
            m_count_done = ((lastByte & 0x80) == 0x00);
    }

    if((format == ETM4_PKT_I_CCNT_F1) && m_commit_done && m_count_done)
    {        
        int idx = 1; // index into buffer for payload data.
        uint32_t field_value = 0;
        // no commit section for TRCIDR0.COMMOPT == 1
        if(!m_config.commitOpt1())
        {
            idx += extractContField(m_currPacketData,idx,field_value);
            m_curr_packet.setCommitElements(field_value);
        }
		if (m_has_count)
		{
			extractContField(m_currPacketData, idx, field_value, 3);
			m_curr_packet.setCycleCount(field_value + m_curr_packet.getCCThreshold());
		}
		else
			m_curr_packet.setCycleCount(0);	/* unknown CC marked as 0 after overflow */
        m_process_state = SEND_PKT;
    }
}

void EtmV4IPktProcImpl::iPktSpeclRes(const uint8_t lastByte)
{    
    if(m_currPacketData.size() == 1)
    {
        switch(m_curr_packet.getType())
        {
        case ETM4_PKT_I_MISPREDICT:
        case ETM4_PKT_I_CANCEL_F2:
            switch(lastByte & 0x3)
            {
            case 0x1: m_curr_packet.setAtomPacket(ATOM_PATTERN, 0x1, 1); break; // E
            case 0x2: m_curr_packet.setAtomPacket(ATOM_PATTERN, 0x3, 2); break; // EE
            case 0x3: m_curr_packet.setAtomPacket(ATOM_PATTERN, 0x0, 1); break; // N
            }
            if(m_curr_packet.getType() == ETM4_PKT_I_CANCEL_F2)
                m_curr_packet.setCancelElements(1);
            m_process_state = SEND_PKT;
            break;

        case ETM4_PKT_I_CANCEL_F3:
            if(lastByte & 0x1)
                m_curr_packet.setAtomPacket(ATOM_PATTERN, 0x1, 1); // E
            m_curr_packet.setCancelElements(((lastByte >> 1) & 0x3) + 2);
            m_process_state = SEND_PKT;
            break;
        }        
    }
    else
    {
        if((lastByte & 0x80) == 0x00)
        {
            uint32_t field_val = 0;
            extractContField(m_currPacketData,1,field_val);
            if(m_curr_packet.getType() == ETM4_PKT_I_COMMIT)
                m_curr_packet.setCommitElements(field_val);
            else
                m_curr_packet.setCancelElements(field_val);
            // TBD: sanity check with max spec depth here?
            m_process_state = SEND_PKT;
        }
    }
}

void EtmV4IPktProcImpl::iPktCondInstr(const uint8_t lastByte)
{
    bool bF1Done = false;

    if(m_currPacketData.size() == 1)    
    {
        if(m_curr_packet.getType() == ETM4_PKT_I_COND_I_F2)
        {
            m_curr_packet.setCondIF2(lastByte & 0x3);
            m_process_state = SEND_PKT;
        }

    }
    else if(m_currPacketData.size() == 2)  
    {
        if(m_curr_packet.getType() == ETM4_PKT_I_COND_I_F3)   // f3 two bytes long
        {
          uint8_t num_c_elem = ((lastByte >> 1) & 0x3F) + (lastByte & 0x1);
            m_curr_packet.setCondIF3(num_c_elem,(bool)((lastByte & 0x1) == 0x1));
            // TBD: check for 0 num_c_elem in here.
            m_process_state = SEND_PKT;
        }
        else
        {
            bF1Done = ((lastByte & 0x80) == 0x00);
        }
    }
    else
    {
        bF1Done = ((lastByte & 0x80) == 0x00);
    }

    if(bF1Done)
    {
        uint32_t cond_key = 0;
        extractContField(m_currPacketData, 1, cond_key);       
        m_process_state = SEND_PKT;        
    }
}

void EtmV4IPktProcImpl::iPktCondResult(const uint8_t lastByte)
{
    if(m_currPacketData.size() == 1)    
    {
        m_F1P1_done = false;  // F1 payload 1 done
        m_F1P2_done = false;  // F1 payload 2 done
        m_F1has_P2 = false;   // F1 has a payload 2
                
        switch(m_curr_packet.getType())
        {
        case ETM4_PKT_I_COND_RES_F1:            

            m_F1has_P2 = true;
            if((lastByte & 0xFC) == 0x6C)// only one payload set
            {
                m_F1P2_done = true;
                m_F1has_P2 = false;
            }
            break;

        case ETM4_PKT_I_COND_RES_F2:
            m_curr_packet.setCondRF2((lastByte & 0x4) ? 2 : 1, lastByte & 0x3);
            m_process_state = SEND_PKT;
            break;

        case ETM4_PKT_I_COND_RES_F3:
            break;

        case ETM4_PKT_I_COND_RES_F4:
            m_curr_packet.setCondRF4(lastByte & 0x3);
            m_process_state = SEND_PKT;
            break;
        }        
    }
    else if((m_curr_packet.getType() == ETM4_PKT_I_COND_RES_F3) && (m_currPacketData.size() == 2)) 
    {
        // 2nd F3 packet
        uint16_t f3_tokens = 0;
        f3_tokens = (uint16_t)m_currPacketData[1];
        f3_tokens |= ((uint16_t)m_currPacketData[0] & 0xf) << 8;
        m_curr_packet.setCondRF3(f3_tokens);
        m_process_state = SEND_PKT;
    }
    else  // !first packet  - F1
    {
        if(!m_F1P1_done)
            m_F1P1_done = ((lastByte & 0x80) == 0x00);
        else if(!m_F1P2_done)
            m_F1P2_done = ((lastByte & 0x80) == 0x00);

        if(m_F1P1_done && m_F1P2_done)
        {
            int st_idx = 1;
            uint32_t key[2];
            uint8_t result[2];
            uint8_t CI[2];

            st_idx+= extractCondResult(m_currPacketData,st_idx,key[0],result[0]);
            CI[0] = m_currPacketData[0] & 0x1;
            if(m_F1has_P2) // 2nd payload?
            {
                extractCondResult(m_currPacketData,st_idx,key[1],result[1]);
                CI[1] = (m_currPacketData[0] >> 1) & 0x1;
            }
            m_curr_packet.setCondRF1(key,result,CI,m_F1has_P2);
            m_process_state = SEND_PKT;
        }
    }
}

void EtmV4IPktProcImpl::iPktContext(const uint8_t lastByte)
{
    bool bSendPacket = false;
    
    if(m_currPacketData.size() == 1) 
    {
        if((lastByte & 0x1) == 0)
        {
            m_curr_packet.setContextInfo(false);    // no update context packet (ctxt same as last time).
            m_process_state = SEND_PKT;
        }
    }
    else if(m_currPacketData.size() == 2) 
    {
        if((lastByte & 0xC0) == 0) // no VMID or CID
        {
            bSendPacket = true;
        }
        else
        {
            m_vmidBytes = ((lastByte & 0x40) == 0x40) ? (m_config.vmidSize()/8) : 0;
            m_ctxtidBytes = ((lastByte & 0x80) == 0x80) ? (m_config.cidSize()/8) : 0;
        }
    }
    else    // 3rd byte onwards
    {
        if(m_vmidBytes > 0)
            m_vmidBytes--;
        else if(m_ctxtidBytes > 0)
            m_ctxtidBytes--;

        if((m_ctxtidBytes == 0) && (m_vmidBytes == 0))
            bSendPacket = true;        
    }

    if(bSendPacket)
    {
        extractAndSetContextInfo(m_currPacketData,1);
        m_process_state = SEND_PKT;
    }
}

void EtmV4IPktProcImpl::extractAndSetContextInfo(const std::vector<uint8_t> &buffer, const int st_idx)
{
    // on input, buffer index points at the info byte - always present
    uint8_t infoByte = m_currPacketData[st_idx];
    
    m_curr_packet.setContextInfo(true, (infoByte & 0x3), (infoByte >> 5) & 0x1, (infoByte >> 4) & 0x1);    

    // see if there are VMID and CID bytes, and how many.
    int nVMID_bytes = ((infoByte & 0x40) == 0x40) ? (m_config.vmidSize()/8) : 0;
    int nCtxtID_bytes = ((infoByte & 0x80) == 0x80) ? (m_config.cidSize()/8) : 0;

    // extract any VMID and CID
    int payload_idx = st_idx+1;
    if(nVMID_bytes)
    {
        uint32_t VMID = 0; 
        for(int i = 0; i < nVMID_bytes; i++)
        {
            VMID |= ((uint32_t)m_currPacketData[i+payload_idx] << i*8);
        }
        payload_idx += nVMID_bytes;
        m_curr_packet.setContextVMID(VMID);
    }

    if(nCtxtID_bytes)
    {
        uint32_t CID = 0; 
        for(int i = 0; i < nCtxtID_bytes; i++)
        {
            CID |= ((uint32_t)m_currPacketData[i+payload_idx] << i*8);
        }        
        m_curr_packet.setContextCID(CID);
    }
}

void EtmV4IPktProcImpl::iPktAddrCtxt(const uint8_t lastByte)
{
    if( m_currPacketData.size() == 1)    
    {        
        m_addrIS = 0;
        m_addrBytes = 4;
        m_bAddr64bit = false;
        m_vmidBytes = 0;
        m_ctxtidBytes = 0;
        m_bCtxtInfoDone = false;

        switch(m_curr_packet.type)
        {
        case ETM4_PKT_I_ADDR_CTXT_L_32IS1:
            m_addrIS = 1;
        case ETM4_PKT_I_ADDR_CTXT_L_32IS0:
            break;

        case ETM4_PKT_I_ADDR_CTXT_L_64IS1:
            m_addrIS = 1;
        case ETM4_PKT_I_ADDR_CTXT_L_64IS0:
            m_addrBytes = 8;
            m_bAddr64bit = true;
            break;
        }
    }
    else
    {
        if(m_addrBytes == 0)
        {
            if(m_bCtxtInfoDone == false)
            {
                m_bCtxtInfoDone = true;
                m_vmidBytes = ((lastByte & 0x40) == 0x40) ? (m_config.vmidSize()/8) : 0;
                m_ctxtidBytes = ((lastByte & 0x80) == 0x80) ? (m_config.cidSize()/8) : 0;
            }
            else
            {
                if( m_vmidBytes > 0) 
                     m_vmidBytes--;
                else if(m_ctxtidBytes > 0)
                    m_ctxtidBytes--;
            }
        }
        else
            m_addrBytes--;

        if((m_addrBytes == 0) && m_bCtxtInfoDone && (m_vmidBytes == 0) && (m_ctxtidBytes == 0))
        {
            int st_idx = 1;
            if(m_bAddr64bit)
            {
                uint64_t val64;
                st_idx+=extract64BitLongAddr(m_currPacketData,st_idx,m_addrIS,val64);
                m_curr_packet.set64BitAddress(val64,m_addrIS);
            }
            else
            {
                uint32_t val32;
                st_idx+=extract32BitLongAddr(m_currPacketData,st_idx,m_addrIS,val32);
                m_curr_packet.set32BitAddress(val32,m_addrIS);
            }
            extractAndSetContextInfo(m_currPacketData,st_idx);
            m_process_state = SEND_PKT;
        }
    }
}

void EtmV4IPktProcImpl::iPktShortAddr(const uint8_t lastByte)
{
    if (m_currPacketData.size() == 1)
    {
        m_addr_done = false;
        m_addrIS = 0;
        if (lastByte == ETM4_PKT_I_ADDR_S_IS1)
            m_addrIS = 1;
    }
    else if(!m_addr_done)
    {
        m_addr_done = (m_currPacketData.size() == 3) || ((lastByte & 0x80) == 0x00);
    }

    if(m_addr_done)
    {
        uint32_t addr_val = 0;
        int bits = 0;

        extractShortAddr(m_currPacketData,1,m_addrIS,addr_val,bits);
        m_curr_packet.updateShortAddress(addr_val,m_addrIS,(uint8_t)bits);
        m_process_state = SEND_PKT;
    }
}

int EtmV4IPktProcImpl::extractShortAddr(const std::vector<uint8_t> &buffer, const int st_idx, const uint8_t IS, uint32_t &value, int &bits)
{
    int IS_shift = (IS == 0) ? 2 : 1;
    int idx = 0;

    bits = 7;   // at least 7 bits
    value = 0;
    value |= ((uint32_t)(buffer[st_idx+idx] & 0x7F)) << IS_shift;
    
    if(m_currPacketData[st_idx+idx] & 0x80)
    {
        idx++;
        value |= ((uint32_t)m_currPacketData[st_idx+idx]) <<  (7 + IS_shift);
        bits += 8;
    }
    idx++;
    bits += IS_shift;
    return idx;
}

void EtmV4IPktProcImpl::iPktLongAddr(const uint8_t lastByte)
{
    if(m_currPacketData.size() == 1)    
    {
        // init the intra-byte data
        m_addrIS = 0;
        m_bAddr64bit = false;
        m_addrBytes = 4;

        switch(m_curr_packet.type)
        {
        case ETM4_PKT_I_ADDR_L_32IS1:
            m_addrIS = 1;
        case ETM4_PKT_I_ADDR_L_32IS0:
            m_addrBytes = 4;
            break;

        case ETM4_PKT_I_ADDR_L_64IS1:
            m_addrIS = 1;
        case ETM4_PKT_I_ADDR_L_64IS0:
            m_addrBytes = 8;
            m_bAddr64bit = true;
            break;
        }
    }
    if(m_currPacketData.size() == (unsigned)(1+m_addrBytes))
    {
        int st_idx = 1;
        if(m_bAddr64bit)
        {
            uint64_t val64;
            st_idx+=extract64BitLongAddr(m_currPacketData,st_idx,m_addrIS,val64);
            m_curr_packet.set64BitAddress(val64,m_addrIS);
        }
        else
        {
            uint32_t val32;
            st_idx+=extract32BitLongAddr(m_currPacketData,st_idx,m_addrIS,val32);
            m_curr_packet.set32BitAddress(val32,m_addrIS);
        }
        m_process_state = SEND_PKT;
    }
}

void EtmV4IPktProcImpl::iPktQ(const uint8_t lastByte)
{
    if(m_currPacketData.size() == 1)
    {
        m_Q_type = lastByte & 0xF;

        m_addrBytes = 0;
        m_count_done = false;
        m_has_addr = false;
        m_addr_short = true;
        m_addr_match = false;
        m_addrIS = 1;
        m_QE = 0;

        switch(m_Q_type)
        {
            // count only - implied address.
        case 0x0:
        case 0x1:
        case 0x2:
            m_addr_match = true;
            m_has_addr = true;
            m_QE = m_Q_type & 0x3;
        case 0xC:
            break;

            // count + short address 
        case 0x5:
            m_addrIS = 0;
        case 0x6:
            m_has_addr = true;            
            m_addrBytes = 2;  // short IS0/1
            break;

            // count + long address
        case 0xA:
            m_addrIS = 0;
        case 0xB:
            m_has_addr = true;
            m_addr_short = false;
            m_addrBytes = 4; // long IS0/1
            break;

            // no count, no address
        case 0xF:
            m_count_done = true;
            break;

            // reserved values 0x3, 0x4, 0x7, 0x8, 0x9, 0xD, 0xE
        default:
            m_curr_packet.err_type =  m_curr_packet.type;
            m_curr_packet.type = ETM4_PKT_I_BAD_SEQUENCE;
            //SendBadIPacket( PKT_BAD_SEQUENCE, "ERROR: Bad Q packet type", PKT_Q );
            break;
        }
    }
    else
    {
        if(m_addrBytes > 0)
        {
            if(m_addr_short && m_addrBytes == 2)  // short
            {
                if((lastByte & 0x80) == 0x00)
                    m_addrBytes--;        // short version can have just single byte.
            }
            m_addrBytes--;
        }
        else if(!m_count_done)
        {
            m_count_done = ((lastByte & 0x80) == 0x00);
        }
    }

    if(((m_addrBytes == 0) && m_count_done))
    {
        int idx = 1; // move past the header
        int bits = 0;
        uint32_t q_addr;
        uint32_t q_count;

        if(m_has_addr)
        {
            if(m_addr_match)
            {
                m_curr_packet.setAddressExactMatch(m_QE);
            }
            else if(m_addr_short)
            {
                idx+=extractShortAddr(m_currPacketData,idx,m_addrIS,q_addr,bits);
                m_curr_packet.updateShortAddress(q_addr,m_addrIS,(uint8_t)bits);
            }
            else
            {
                idx+=extract32BitLongAddr(m_currPacketData,idx,m_addrIS,q_addr);
                m_curr_packet.set32BitAddress(q_addr,m_addrIS);
            }
        }

        if(m_Q_type != 0xF)
        {
            extractContField(m_currPacketData,idx,q_count);
            m_curr_packet.setQType(true,q_count,m_has_addr,m_addr_match,m_Q_type);
        }
        else
        {
            m_curr_packet.setQType(false,0,false,false,0xF);
        }
        m_process_state = SEND_PKT;
    }

}

void EtmV4IPktProcImpl::iAtom(const uint8_t lastByte)
{
    // patterns lsbit = oldest atom, ms bit = newest.
    static const uint32_t f4_patterns[] = {
        0xE, // EEEN 
        0x0, // NNNN
        0xA, // ENEN
        0x5  // NENE
    };

    uint8_t pattIdx = 0, pattCount = 0;
    uint32_t pattern;

    // atom packets are single byte, no payload.
    switch(m_curr_packet.type)
    {
    case ETM4_PKT_I_ATOM_F1:
        m_curr_packet.setAtomPacket(ATOM_PATTERN,(lastByte & 0x1), 1); // 1xE or N
        break;

    case ETM4_PKT_I_ATOM_F2:
        m_curr_packet.setAtomPacket(ATOM_PATTERN,(lastByte & 0x3), 2); // 2x (E or N)
        break;

    case ETM4_PKT_I_ATOM_F3:
        m_curr_packet.setAtomPacket(ATOM_PATTERN,(lastByte & 0x7), 3); // 3x (E or N)
        break;

    case ETM4_PKT_I_ATOM_F4:
        m_curr_packet.setAtomPacket(ATOM_PATTERN,f4_patterns[(lastByte & 0x3)], 4); // 4 atom pattern
        break; 

    case ETM4_PKT_I_ATOM_F5:
        pattIdx = ((lastByte & 0x20) >> 3) | (lastByte & 0x3);
        switch(pattIdx)
        {
        case 5: // 0b101
            m_curr_packet.setAtomPacket(ATOM_PATTERN,0x1E, 5); // 5 atom pattern EEEEN
            break;

        case 1: // 0b001
            m_curr_packet.setAtomPacket(ATOM_PATTERN,0x00, 5); // 5 atom pattern NNNNN
            break;

        case 2: //0b010
            m_curr_packet.setAtomPacket(ATOM_PATTERN,0x0A, 5); // 5 atom pattern NENEN
            break;

        case 3: //0b011
            m_curr_packet.setAtomPacket(ATOM_PATTERN,0x15, 5); // 5 atom pattern ENENE
            break;

        default:
            // TBD: warn about invalid pattern in here.
            break;
        }
        break;

    case ETM4_PKT_I_ATOM_F6:
        pattCount = (lastByte & 0x1F) + 3;  // count of E's
        // TBD: check 23 or less at this point? 
        pattern = ((uint32_t)0x1 << pattCount) - 1; // set pattern to string of E's
        if((lastByte & 0x20) == 0x00)   // last atom is E?
            pattern |= ((uint32_t)0x1 << pattCount); 
        m_curr_packet.setAtomPacket(ATOM_PATTERN,pattern, pattCount+1);
        break;
    }

    m_process_state = SEND_PKT;
}

// header byte processing is table driven.
void EtmV4IPktProcImpl::BuildIPacketTable()   
{
    // initialise everything as reserved.
    for(int i = 0; i < 256; i++)
    {
        m_i_table[i].pkt_type = ETM4_PKT_I_RESERVED;
        m_i_table[i].pptkFn = &EtmV4IPktProcImpl::iPktReserved;
    }

    // 0x00 - extension 
    m_i_table[0x00].pkt_type = ETM4_PKT_I_EXTENSION;
    m_i_table[0x00].pptkFn   = &EtmV4IPktProcImpl::iPktExtension;

    // 0x01 - Trace info
    m_i_table[0x01].pkt_type = ETM4_PKT_I_TRACE_INFO;
    m_i_table[0x01].pptkFn   = &EtmV4IPktProcImpl::iPktTraceInfo;

    // b0000001x - timestamp
    m_i_table[0x02].pkt_type = ETM4_PKT_I_TIMESTAMP;
    m_i_table[0x02].pptkFn   = &EtmV4IPktProcImpl::iPktTimestamp;
    m_i_table[0x03].pkt_type = ETM4_PKT_I_TIMESTAMP;
    m_i_table[0x03].pptkFn   = &EtmV4IPktProcImpl::iPktTimestamp;

    // b0000 0100 - trace on
    m_i_table[0x04].pkt_type = ETM4_PKT_I_TRACE_ON;
    m_i_table[0x04].pptkFn   = &EtmV4IPktProcImpl::iPktNoPayload;


    // b0000 0101 - Funct ret V8M
    m_i_table[0x05].pkt_type = ETM4_PKT_I_FUNC_RET;
    if ((m_config.coreProfile() == profile_CortexM) &&
        (OCSD_IS_V8_ARCH(m_config.archVersion())) &&
        (m_config.FullVersion() >= 0x42))
    {        
        m_i_table[0x05].pptkFn = &EtmV4IPktProcImpl::iPktNoPayload;
    }

    // b0000 0110 - exception 
    m_i_table[0x06].pkt_type = ETM4_PKT_I_EXCEPT;
    m_i_table[0x06].pptkFn   = &EtmV4IPktProcImpl::iPktException;

    // b0000 0111 - exception return 
    m_i_table[0x07].pkt_type = ETM4_PKT_I_EXCEPT_RTN;
    m_i_table[0x07].pptkFn = &EtmV4IPktProcImpl::iPktNoPayload;

    // b0000 110x - cycle count f2
    // b0000 111x - cycle count f1
    for(int i = 0; i < 4; i++)
    {
        m_i_table[0x0C+i].pkt_type = (i >= 2) ? ETM4_PKT_I_CCNT_F1 : ETM4_PKT_I_CCNT_F2;
        m_i_table[0x0C+i].pptkFn   = &EtmV4IPktProcImpl::iPktCycleCntF123;
    }

    // b0001 xxxx - cycle count f3
    for(int i = 0; i < 16; i++)
    {
        m_i_table[0x10+i].pkt_type = ETM4_PKT_I_CCNT_F3;
        m_i_table[0x10+i].pptkFn   = &EtmV4IPktProcImpl::iPktCycleCntF123;
    }

    // b0010 0xxx - NDSM
    for(int i = 0; i < 8; i++)
    {
        m_i_table[0x20 + i].pkt_type = ETM4_PKT_I_NUM_DS_MKR;
        if (m_config.enabledDataTrace())
            m_i_table[0x20+i].pptkFn = &EtmV4IPktProcImpl::iPktNoPayload;
        else
            m_i_table[0x20+i].pptkFn = &EtmV4IPktProcImpl::iPktInvalidCfg;
    }

    // b0010 10xx, b0010 1100 - UDSM
    for(int i = 0; i < 5; i++)
    {
        m_i_table[0x28+i].pkt_type = ETM4_PKT_I_UNNUM_DS_MKR;
        if (m_config.enabledDataTrace())
            m_i_table[0x28+i].pptkFn = &EtmV4IPktProcImpl::iPktNoPayload;
        else
            m_i_table[0x28+i].pptkFn = &EtmV4IPktProcImpl::iPktInvalidCfg;
    }

    // b0010 1101 - commit
    m_i_table[0x2D].pkt_type = ETM4_PKT_I_COMMIT;
    m_i_table[0x2D].pptkFn   = &EtmV4IPktProcImpl::iPktSpeclRes;

    // b0010 111x - cancel f1
    for(int i = 0; i < 2; i++)
    {
        // G++ doesn't understand [0x2E+i] so...
        int idx = i + 0x2E;
        m_i_table[idx].pkt_type = ETM4_PKT_I_CANCEL_F1;
        m_i_table[idx].pptkFn   = &EtmV4IPktProcImpl::iPktSpeclRes;
    }

    // b0011 00xx - mis predict
    for(int i = 0; i < 4; i++)
    {
        m_i_table[0x30+i].pkt_type = ETM4_PKT_I_MISPREDICT;
        m_i_table[0x30+i].pptkFn   =  &EtmV4IPktProcImpl::iPktSpeclRes;
    }

    // b0011 01xx - cancel f2
    for(int i = 0; i < 4; i++)
    {
        m_i_table[0x34+i].pkt_type = ETM4_PKT_I_CANCEL_F2;
        m_i_table[0x34+i].pptkFn   =  &EtmV4IPktProcImpl::iPktSpeclRes;
    }

    // b0011 1xxx - cancel f3
    for(int i = 0; i < 8; i++)
    {
        m_i_table[0x38+i].pkt_type = ETM4_PKT_I_CANCEL_F3;
        m_i_table[0x38+i].pptkFn   =  &EtmV4IPktProcImpl::iPktSpeclRes;
    }

    bool bCondValid = m_config.hasCondTrace() && m_config.enabledCondITrace();

    // b0100 000x, b0100 0010 - cond I f2
    for (int i = 0; i < 3; i++)
    {
        m_i_table[0x40 + i].pkt_type = ETM4_PKT_I_COND_I_F2;
        if (bCondValid)
            m_i_table[0x40 + i].pptkFn = &EtmV4IPktProcImpl::iPktCondInstr;
        else
            m_i_table[0x40 + i].pptkFn = &EtmV4IPktProcImpl::iPktInvalidCfg;
    }

    // b0100 0011 - cond flush
    m_i_table[0x43].pkt_type = ETM4_PKT_I_COND_FLUSH;
    if (bCondValid)
        m_i_table[0x43].pptkFn = &EtmV4IPktProcImpl::iPktNoPayload;
    else
        m_i_table[0x43].pptkFn = &EtmV4IPktProcImpl::iPktInvalidCfg;

    // b0100 010x, b0100 0110 - cond res f4
    for (int i = 0; i < 3; i++)
    {
        m_i_table[0x44 + i].pkt_type = ETM4_PKT_I_COND_RES_F4;
        if (bCondValid)
            m_i_table[0x44 + i].pptkFn = &EtmV4IPktProcImpl::iPktCondResult;
        else
            m_i_table[0x44 + i].pptkFn = &EtmV4IPktProcImpl::iPktInvalidCfg;
    }

    // b0100 100x, b0100 0110 - cond res f2
    // b0100 110x, b0100 1110 - cond res f2
    for (int i = 0; i < 3; i++)
    {
        m_i_table[0x48 + i].pkt_type = ETM4_PKT_I_COND_RES_F2;
        if (bCondValid)
            m_i_table[0x48 + i].pptkFn = &EtmV4IPktProcImpl::iPktCondResult;
        else
            m_i_table[0x48 + i].pptkFn = &EtmV4IPktProcImpl::iPktInvalidCfg;
    }
    for (int i = 0; i < 3; i++)
    {
        m_i_table[0x4C + i].pkt_type = ETM4_PKT_I_COND_RES_F2;
        if (bCondValid)
            m_i_table[0x4C + i].pptkFn = &EtmV4IPktProcImpl::iPktCondResult;
        else
            m_i_table[0x4C + i].pptkFn = &EtmV4IPktProcImpl::iPktInvalidCfg;
    }

    // b0101xxxx - cond res f3
    for (int i = 0; i < 16; i++)
    {
        m_i_table[0x50 + i].pkt_type = ETM4_PKT_I_COND_RES_F3;
        if (bCondValid)
            m_i_table[0x50 + i].pptkFn = &EtmV4IPktProcImpl::iPktCondResult;
        else
            m_i_table[0x50 + i].pptkFn = &EtmV4IPktProcImpl::iPktInvalidCfg;
    }

    // b011010xx - cond res f1
    for (int i = 0; i < 4; i++)
    {
        m_i_table[0x68 + i].pkt_type = ETM4_PKT_I_COND_RES_F1;
        if (bCondValid)
            m_i_table[0x68 + i].pptkFn = &EtmV4IPktProcImpl::iPktCondResult;
        else
            m_i_table[0x68 + i].pptkFn = &EtmV4IPktProcImpl::iPktInvalidCfg;
    }

    // b0110 1100 - cond instr f1
    m_i_table[0x6C].pkt_type = ETM4_PKT_I_COND_I_F1;
    if (bCondValid)
        m_i_table[0x6C].pptkFn = &EtmV4IPktProcImpl::iPktCondInstr;
    else
        m_i_table[0x6C].pptkFn = &EtmV4IPktProcImpl::iPktInvalidCfg;

    // b0110 1101 - cond instr f3
    m_i_table[0x6D].pkt_type = ETM4_PKT_I_COND_I_F3;
    if (bCondValid)
        m_i_table[0x6D].pptkFn = &EtmV4IPktProcImpl::iPktCondInstr;
    else
        m_i_table[0x6D].pptkFn = &EtmV4IPktProcImpl::iPktInvalidCfg;

    // b0110111x - cond res f1
    for (int i = 0; i < 2; i++)
    {
        // G++ cannot understand [0x6E+i] so change these round
        m_i_table[i + 0x6E].pkt_type = ETM4_PKT_I_COND_RES_F1;
        if (bCondValid)
            m_i_table[i + 0x6E].pptkFn = &EtmV4IPktProcImpl::iPktCondResult;
        else
            m_i_table[i + 0x6E].pptkFn = &EtmV4IPktProcImpl::iPktInvalidCfg;
    }

    // ETM 4.3 introduces ignore packets
    if (m_config.FullVersion() >= 0x43)
    {
        m_i_table[0x70].pkt_type = ETM4_PKT_I_IGNORE;
        m_i_table[0x70].pptkFn = &EtmV4IPktProcImpl::iPktNoPayload;
    }

    // b01110001 - b01111111 - event trace
    for(int i = 0; i < 15; i++)
    {
        m_i_table[0x71+i].pkt_type = ETM4_PKT_I_EVENT;
        m_i_table[0x71+i].pptkFn   = &EtmV4IPktProcImpl::iPktNoPayload;
    }
    
    // 0b1000 000x - context 
    for(int i = 0; i < 2; i++)
    {
        m_i_table[0x80+i].pkt_type = ETM4_PKT_I_CTXT;
        m_i_table[0x80+i].pptkFn   = &EtmV4IPktProcImpl::iPktContext;
    }

    // 0b1000 0010 to b1000 0011 - addr with ctxt
    // 0b1000 0101 to b1000 0110 - addr with ctxt
    for(int i = 0; i < 2; i++)
    {
        m_i_table[0x82+i].pkt_type =  (i == 0) ? ETM4_PKT_I_ADDR_CTXT_L_32IS0 : ETM4_PKT_I_ADDR_CTXT_L_32IS1;
        m_i_table[0x82+i].pptkFn   = &EtmV4IPktProcImpl::iPktAddrCtxt;
    }

    for(int i = 0; i < 2; i++)
    {
        m_i_table[0x85+i].pkt_type = (i == 0) ? ETM4_PKT_I_ADDR_CTXT_L_64IS0 : ETM4_PKT_I_ADDR_CTXT_L_64IS1;
        m_i_table[0x85+i].pptkFn   = &EtmV4IPktProcImpl::iPktAddrCtxt;
    }

    // 0b1001 0000 to b1001 0010 - exact match addr
    for(int i = 0; i < 3; i++)
    {
        m_i_table[0x90+i].pkt_type = ETM4_PKT_I_ADDR_MATCH;
        m_i_table[0x90+i].pptkFn   = &EtmV4IPktProcImpl::iPktNoPayload;
    }

    // b1001 0101 - b1001 0110 - addr short address
    for(int i = 0; i < 2; i++)
    {
        m_i_table[0x95+i].pkt_type =  (i == 0) ? ETM4_PKT_I_ADDR_S_IS0 : ETM4_PKT_I_ADDR_S_IS1;
        m_i_table[0x95+i].pptkFn   = &EtmV4IPktProcImpl::iPktShortAddr;
    }

    // b10011010 - b10011011 - addr long address 
    // b10011101 - b10011110 - addr long address 
    for(int i = 0; i < 2; i++)
    {
        m_i_table[0x9A+i].pkt_type =  (i == 0) ? ETM4_PKT_I_ADDR_L_32IS0 : ETM4_PKT_I_ADDR_L_32IS1;
        m_i_table[0x9A+i].pptkFn   = &EtmV4IPktProcImpl::iPktLongAddr;
    }
    for(int i = 0; i < 2; i++)
    {
        m_i_table[0x9D+i].pkt_type =  (i == 0) ? ETM4_PKT_I_ADDR_L_64IS0 : ETM4_PKT_I_ADDR_L_64IS1;
        m_i_table[0x9D+i].pptkFn   = &EtmV4IPktProcImpl::iPktLongAddr;
    }
    
    // b1010xxxx - Q packet
    for (int i = 0; i < 16; i++)
    {
        m_i_table[0xA0 + i].pkt_type = ETM4_PKT_I_Q;
        // certain Q type codes are reserved.
        switch (i) {
        case 0x3:
        case 0x4:
        case 0x7:
        case 0x8:
        case 0x9:
        case 0xD:
        case 0xE:
            // don't update pkt fn - leave at default reserved.
            break;
        default:
            // if this config supports Q elem - otherwise reserved again.
            if (m_config.hasQElem())
                m_i_table[0xA0 + i].pptkFn = &EtmV4IPktProcImpl::iPktQ;
        }
    }

    // Atom Packets - all no payload but have specific pattern generation fn
    for(int i = 0xC0; i <= 0xD4; i++)   // atom f6
    {
        m_i_table[i].pkt_type = ETM4_PKT_I_ATOM_F6;
        m_i_table[i].pptkFn   = &EtmV4IPktProcImpl::iAtom;
    }
    for(int i = 0xD5; i <= 0xD7; i++)  // atom f5
    {
        m_i_table[i].pkt_type = ETM4_PKT_I_ATOM_F5;
        m_i_table[i].pptkFn   = &EtmV4IPktProcImpl::iAtom;
    }
    for(int i = 0xD8; i <= 0xDB; i++)  // atom f2
    {
        m_i_table[i].pkt_type = ETM4_PKT_I_ATOM_F2;
        m_i_table[i].pptkFn   = &EtmV4IPktProcImpl::iAtom;
    }
    for(int i = 0xDC; i <= 0xDF; i++)  // atom f4
    {
        m_i_table[i].pkt_type = ETM4_PKT_I_ATOM_F4;
        m_i_table[i].pptkFn   = &EtmV4IPktProcImpl::iAtom;
    }
    for(int i = 0xE0; i <= 0xF4; i++)  // atom f6
    {
        m_i_table[i].pkt_type = ETM4_PKT_I_ATOM_F6;
        m_i_table[i].pptkFn   = &EtmV4IPktProcImpl::iAtom;
    }
    
    // atom f5
    m_i_table[0xF5].pkt_type = ETM4_PKT_I_ATOM_F5;
    m_i_table[0xF5].pptkFn   = &EtmV4IPktProcImpl::iAtom;

    for(int i = 0xF6; i <= 0xF7; i++)  // atom f1
    {
        m_i_table[i].pkt_type = ETM4_PKT_I_ATOM_F1;
        m_i_table[i].pptkFn   = &EtmV4IPktProcImpl::iAtom;
    }
    for(int i = 0xF8; i <= 0xFF; i++)  // atom f3
    {
        m_i_table[i].pkt_type = ETM4_PKT_I_ATOM_F3;
        m_i_table[i].pptkFn   = &EtmV4IPktProcImpl::iAtom;
    }
}

 unsigned EtmV4IPktProcImpl::extractContField(const std::vector<uint8_t> &buffer, const unsigned st_idx, uint32_t &value, const unsigned byte_limit /*= 5*/)
{
    unsigned idx = 0;
    bool lastByte = false;
    uint8_t byteVal;
    value = 0;
    while(!lastByte && (idx < byte_limit))   // max 5 bytes for 32 bit value;
    {
        if(buffer.size() > (st_idx + idx))
        {
            // each byte has seven bits + cont bit
            byteVal = buffer[(st_idx + idx)];
            lastByte = (byteVal & 0x80) != 0x80;
            value |= ((uint32_t)(byteVal & 0x7F)) << (idx * 7);
            idx++;
        }
        else
        {
            throwBadSequenceError("Invalid 32 bit continuation fields in packet");
        }
    }
    return idx;
}

unsigned EtmV4IPktProcImpl::extractContField64(const std::vector<uint8_t> &buffer, const unsigned st_idx, uint64_t &value, const unsigned byte_limit /*= 9*/)
{
    unsigned idx = 0;
    bool lastByte = false;
    uint8_t byteVal;
    value = 0;
    while(!lastByte && (idx < byte_limit))   // max 9 bytes for 64 bit value;
    {
        if(buffer.size() > (st_idx + idx))
        {
            // each byte has seven bits + cont bit
            byteVal = buffer[(st_idx + idx)];
            lastByte = (byteVal & 0x80) != 0x80;
            value |= ((uint64_t)(byteVal & 0x7F)) << (idx * 7);
            idx++;
        }
        else
        {
            throwBadSequenceError("Invalid 64 bit continuation fields in packet");
        }
    }
    return idx;
}

 unsigned EtmV4IPktProcImpl::extractCondResult(const std::vector<uint8_t> &buffer, const unsigned st_idx, uint32_t& key, uint8_t &result)
{
    unsigned idx = 0;
    bool lastByte = false;
    int incr = 0;

    key = 0;

    while(!lastByte && (idx < 6)) // cannot be more than 6 bytes for res + 32 bit key
    {
        if(buffer.size() > (st_idx + idx))
        {
            if(idx == 0)
            {
                result = buffer[st_idx+idx];
                key = (buffer[st_idx+idx] >> 4) & 0x7;
                incr+=3;
            }
            else
            {
                key |= ((uint32_t)(buffer[st_idx+idx] & 0x7F)) << incr;
                incr+=7;
            }
            lastByte = (bool)((buffer[st_idx+idx] & 0x80) == 0); 
            idx++;
        }
        else
        {
            throwBadSequenceError("Invalid continuation fields in packet");
        }
    }    
    return idx;
}

int EtmV4IPktProcImpl::extract64BitLongAddr(const std::vector<uint8_t> &buffer, const int st_idx, const uint8_t IS, uint64_t &value)
{
    value = 0;
    if(IS == 0)
    {
        value |= ((uint64_t)(buffer[st_idx+0] & 0x7F)) << 2;
        value |= ((uint64_t)(buffer[st_idx+1] & 0x7F)) << 9;
    }
    else
    {
        value |= ((uint64_t)(buffer[st_idx+0] & 0x7F)) << 1;
        value |= ((uint64_t)buffer[st_idx+1]) << 8;
    }
    value |= ((uint64_t)buffer[st_idx+2]) << 16;
    value |= ((uint64_t)buffer[st_idx+3]) << 24;
    value |= ((uint64_t)buffer[st_idx+4]) << 32;
    value |= ((uint64_t)buffer[st_idx+5]) << 40;
    value |= ((uint64_t)buffer[st_idx+6]) << 48;
    value |= ((uint64_t)buffer[st_idx+7]) << 56;      
    return 8;    
}

int EtmV4IPktProcImpl::extract32BitLongAddr(const std::vector<uint8_t> &buffer, const int st_idx, const uint8_t IS, uint32_t &value)
{
    value = 0;
    if(IS == 0)
    {
        value |= ((uint32_t)(buffer[st_idx+0] & 0x7F)) << 2;
        value |= ((uint32_t)(buffer[st_idx+1] & 0x7F)) << 9;
    }
    else
    {
        value |= ((uint32_t)(buffer[st_idx+0] & 0x7F)) << 1;
        value |= ((uint32_t)buffer[st_idx+1]) << 8;
    }
    value |= ((uint32_t)buffer[st_idx+2]) << 16;
    value |= ((uint32_t)buffer[st_idx+3]) << 24;
    return 4;
}

void EtmV4IPktProcImpl::throwBadSequenceError(const char *pszExtMsg)
{
    m_curr_packet.updateErrType(ETM4_PKT_I_BAD_SEQUENCE);   // swap type for err type
    throw ocsdError(OCSD_ERR_SEV_ERROR, OCSD_ERR_BAD_PACKET_SEQ,m_packet_index,m_config.getTraceID(),pszExtMsg);
}


/* End of File trc_pkt_proc_etmv4i_impl.cpp */