aboutsummaryrefslogtreecommitdiffstats
path: root/decoder/source/etmv3/trc_pkt_decode_etmv3.cpp
blob: 0a15a33c42fb66b9febe02dc8f4f89ddfa117759 (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
/*!
 * \file       trc_pkt_decode_etmv3.cpp
 * \brief      OpenCSD : ETMv3 trace packet decode.
 * 
 * \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 "opencsd/etmv3/trc_pkt_decode_etmv3.h"

#define DCD_NAME "DCD_ETMV3"

TrcPktDecodeEtmV3::TrcPktDecodeEtmV3() : 
    TrcPktDecodeBase(DCD_NAME)
{
    initDecoder();
}

TrcPktDecodeEtmV3::TrcPktDecodeEtmV3(int instIDNum) :
    TrcPktDecodeBase(DCD_NAME, instIDNum)
{
    initDecoder();
}

TrcPktDecodeEtmV3::~TrcPktDecodeEtmV3()
{
}


/* implementation packet decoding interface */
ocsd_datapath_resp_t TrcPktDecodeEtmV3::processPacket()
{
    ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
    bool bPktDone = false;

    if(!m_config)
        return OCSD_RESP_FATAL_NOT_INIT;

    // iterate round the state machine, waiting for sync, then decoding packets.
    while(!bPktDone)
    {
        switch(m_curr_state)
        {
        case NO_SYNC:
            // output the initial not synced packet to the sink
            resp = sendUnsyncPacket();  
            m_curr_state = WAIT_ASYNC;  // immediate wait for ASync and actually check out the packet
            break;

        case WAIT_ASYNC:
            // if async, wait for ISync, but this packet done.
            if(m_curr_packet_in->getType() == ETM3_PKT_A_SYNC)
                m_curr_state = WAIT_ISYNC;
            bPktDone = true;
            break;

        case WAIT_ISYNC:
            m_bWaitISync = true;    // we are waiting for ISync
            if((m_curr_packet_in->getType() == ETM3_PKT_I_SYNC) || 
                (m_curr_packet_in->getType() == ETM3_PKT_I_SYNC_CYCLE))
            {
                // process the ISync immediately as the first ISync seen.
                resp = processISync((m_curr_packet_in->getType() == ETM3_PKT_I_SYNC_CYCLE),true);
                m_curr_state = SEND_PKTS;
                m_bWaitISync = false;
            }
            // something like TS, CC, PHDR+CC, which after ASYNC may be valid prior to ISync
            else if(preISyncValid(m_curr_packet_in->getType()))
            {
                // decode anything that might be valid - send will be set automatically 
                resp = decodePacket(bPktDone);
            }
            else
                bPktDone = true; 
            break;

        case DECODE_PKTS:
            resp = decodePacket(bPktDone);
            break;

        case SEND_PKTS:
            resp = m_outputElemList.sendElements();
            if(OCSD_DATA_RESP_IS_CONT(resp))
                m_curr_state = m_bWaitISync ? WAIT_ISYNC : DECODE_PKTS;
            bPktDone = true;
            break;  

        default:
            bPktDone = true;
            LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_FAIL,m_index_curr_pkt,"Unknown Decoder State"));
            resetDecoder(); // mark decoder as unsynced - dump any current state.
            resp = OCSD_RESP_FATAL_SYS_ERR;
            break;
        }
    }

    return resp;
}

ocsd_datapath_resp_t TrcPktDecodeEtmV3::onEOT()
{
    ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
    OcsdTraceElement *pElem = 0;
    try {
        pElem = GetNextOpElem(resp);
        pElem->setType(OCSD_GEN_TRC_ELEM_EO_TRACE);
        m_outputElemList.commitAllPendElem();
        m_curr_state = SEND_PKTS;
        resp = m_outputElemList.sendElements();
        if(OCSD_DATA_RESP_IS_CONT(resp))
            m_curr_state = DECODE_PKTS;
    }
    catch(ocsdError &err)
    {
        LogError(err);
        resetDecoder(); // mark decoder as unsynced - dump any current state.
    }    
    return resp;
}

ocsd_datapath_resp_t TrcPktDecodeEtmV3::onReset()
{
    ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
    resetDecoder();
    return resp;
}

ocsd_datapath_resp_t TrcPktDecodeEtmV3::onFlush()
{
    ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
    if(m_curr_state == SEND_PKTS)
    {
        resp = m_outputElemList.sendElements();
        if(OCSD_DATA_RESP_IS_CONT(resp))
            m_curr_state = m_bWaitISync ? WAIT_ISYNC : DECODE_PKTS;
    }
    return resp;
}

ocsd_err_t TrcPktDecodeEtmV3::onProtocolConfig()
{
    ocsd_err_t err = OCSD_OK;
    if(m_config)
    {
        // set some static config elements
        m_CSID = m_config->getTraceID();

        // check config compatible with current decoder support level.
        // at present no data trace;
        if(m_config->GetTraceMode() != EtmV3Config::TM_INSTR_ONLY)
        {
            err = OCSD_ERR_HW_CFG_UNSUPP;
            LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_HW_CFG_UNSUPP,"ETMv3 trace decoder : data trace decode not yet supported"));
        }

        // need to set up core profile info in follower
        ocsd_arch_profile_t arch_profile;
        arch_profile.arch = m_config->getArchVersion();
        arch_profile.profile = m_config->getCoreProfile();
        m_code_follower.setArchProfile(arch_profile);
        m_code_follower.setMemSpaceCSID(m_CSID);
        m_outputElemList.initCSID(m_CSID);
    }
    else
        err = OCSD_ERR_NOT_INIT;
    return err;
}

/* local decode methods */

// initialise on creation
void TrcPktDecodeEtmV3::initDecoder()
{
    m_CSID = 0;
    resetDecoder();
    m_code_follower.initInterfaces(getMemoryAccessAttachPt(),getInstrDecodeAttachPt());
    m_outputElemList.initSendIf(getTraceElemOutAttachPt());
}

// reset for first use / re-use.
void TrcPktDecodeEtmV3::resetDecoder()
{
    m_curr_state = NO_SYNC; // mark as not synced
    m_bNeedAddr = true;
    m_bSentUnknown = false;
    m_bWaitISync = false;
    m_outputElemList.reset();
}

OcsdTraceElement *TrcPktDecodeEtmV3::GetNextOpElem(ocsd_datapath_resp_t &resp)
{
    OcsdTraceElement *pElem = m_outputElemList.getNextElem(m_index_curr_pkt);
    if(pElem == 0)
    {
        resp = OCSD_RESP_FATAL_NOT_INIT;
        throw ocsdError(OCSD_ERR_SEV_ERROR, OCSD_ERR_MEM,m_index_curr_pkt,m_CSID,"Memory Allocation Error - fatal");
    }
    return pElem;
}

bool TrcPktDecodeEtmV3::preISyncValid(ocsd_etmv3_pkt_type pkt_type)
{
    bool bValid = false;
    // its a timestamp
    if((pkt_type == ETM3_PKT_TIMESTAMP) || 
        // or we are cycleacc and its a packet that can have CC in it
        (m_config->isCycleAcc() && ((pkt_type == ETM3_PKT_CYCLE_COUNT) || (pkt_type == ETM3_PKT_P_HDR)))
        )
        bValid = true;
    return bValid;
}

// simple packet transforms handled here, more complex processing passed on to specific routines.
ocsd_datapath_resp_t TrcPktDecodeEtmV3::decodePacket(bool &pktDone)
{
    ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
    bool bISyncHasCC = false;
    OcsdTraceElement *pElem = 0;
    pktDone = false;

    // there may be pended packets that can now be committed.
    // only the branch address with exception and cancel element can cancel
    // if not one of those, commit immediately, otherwise defer to branch address handler.
    if(m_curr_packet_in->getType() != ETM3_PKT_BRANCH_ADDRESS)
        m_outputElemList.commitAllPendElem();

    try {

        switch(m_curr_packet_in->getType())
        {

        case ETM3_PKT_NOTSYNC:
            // mark as not synced - must have lost sync in the packet processor somehow
            throw ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_BAD_PACKET_SEQ,m_index_curr_pkt,m_CSID,"Trace Packet Synchronisation Lost");
            break;

            // no action for these packets - ignore and continue
        case ETM3_PKT_INCOMPLETE_EOT:
        case ETM3_PKT_A_SYNC:
        case ETM3_PKT_IGNORE:
            break;

    // markers for valid packets
        case ETM3_PKT_CYCLE_COUNT:
            pElem = GetNextOpElem(resp);
            pElem->setType(OCSD_GEN_TRC_ELEM_CYCLE_COUNT);
            pElem->setCycleCount(m_curr_packet_in->getCycleCount());
            break;

        case ETM3_PKT_TRIGGER:
            pElem = GetNextOpElem(resp);
            pElem->setType(OCSD_GEN_TRC_ELEM_EVENT);
            pElem->setEvent(EVENT_TRIGGER,0);
            break;

        case ETM3_PKT_BRANCH_ADDRESS:
            resp = processBranchAddr();
            break;

        case ETM3_PKT_I_SYNC_CYCLE:
            bISyncHasCC = true;
        case ETM3_PKT_I_SYNC:
            resp = processISync(bISyncHasCC);
            break;

        case ETM3_PKT_P_HDR:
            resp = processPHdr();
            break;

        case ETM3_PKT_CONTEXT_ID:
            pElem = GetNextOpElem(resp);
            pElem->setType(OCSD_GEN_TRC_ELEM_PE_CONTEXT);
            m_PeContext.setCtxtID(m_curr_packet_in->getCtxtID());
            pElem->setContext(m_PeContext);
            break;

        case ETM3_PKT_VMID:
            pElem = GetNextOpElem(resp);
            pElem->setType(OCSD_GEN_TRC_ELEM_PE_CONTEXT);
            m_PeContext.setVMID(m_curr_packet_in->getVMID());
            pElem->setContext(m_PeContext);
            break;

        case ETM3_PKT_EXCEPTION_ENTRY:
            pElem = GetNextOpElem(resp);
            pElem->setType(OCSD_GEN_TRC_ELEM_EXCEPTION);
            pElem->setExcepMarker(); // exception entries are always v7M data markers in ETMv3 trace.
            break;

        case ETM3_PKT_EXCEPTION_EXIT:
            pElem = GetNextOpElem(resp);
            pElem->setType(OCSD_GEN_TRC_ELEM_EXCEPTION_RET);
            pendExceptionReturn();
            break;
        
        case ETM3_PKT_TIMESTAMP:
            pElem = GetNextOpElem(resp);
            pElem->setType(OCSD_GEN_TRC_ELEM_TIMESTAMP);
            pElem->setTS(m_curr_packet_in->getTS());
            break;

            // data packets - data trace not supported at present
        case ETM3_PKT_STORE_FAIL:
        case ETM3_PKT_OOO_DATA:
        case ETM3_PKT_OOO_ADDR_PLC:
        case ETM3_PKT_NORM_DATA:
        case ETM3_PKT_DATA_SUPPRESSED:
        case ETM3_PKT_VAL_NOT_TRACED:
        case ETM3_PKT_BAD_TRACEMODE:
            resp = OCSD_RESP_FATAL_INVALID_DATA;        
            throw ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_HW_CFG_UNSUPP,m_index_curr_pkt,m_CSID,"Invalid packet type : Data Tracing decode not supported.");
            break;

    // packet errors 
        case ETM3_PKT_BAD_SEQUENCE:
            resp = OCSD_RESP_FATAL_INVALID_DATA;
            throw ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_BAD_PACKET_SEQ,m_index_curr_pkt,m_CSID,"Bad Packet sequence.");
            break;

        default:
        case ETM3_PKT_RESERVED:
            resp = OCSD_RESP_FATAL_INVALID_DATA;
            throw ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_BAD_PACKET_SEQ,m_index_curr_pkt,m_CSID,"Reserved or unknown packet ID.");
            break;
        }    
        m_curr_state = m_outputElemList.elemToSend() ? SEND_PKTS : DECODE_PKTS;
        pktDone = !m_outputElemList.elemToSend();
    }
    catch(ocsdError &err)
    {
        LogError(err);
        resetDecoder(); // mark decoder as unsynced - dump any current state.
        pktDone = true;
    }
    catch(...)
    {
        LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_FAIL,m_index_curr_pkt,m_CSID,"Bad Packet sequence."));
        resp = OCSD_RESP_FATAL_SYS_ERR;
        resetDecoder(); // mark decoder as unsynced - dump any current state.
        pktDone = true;
    }
    return resp;
}
   
ocsd_datapath_resp_t TrcPktDecodeEtmV3::sendUnsyncPacket()
{
    ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
    OcsdTraceElement *pElem = 0;
    try {
        pElem = GetNextOpElem(resp);
        pElem->setType(OCSD_GEN_TRC_ELEM_NO_SYNC);
        resp = m_outputElemList.sendElements();
    }
    catch(ocsdError &err)
    {
        LogError(err);
        resetDecoder(); // mark decoder as unsynced - dump any current state.
    }
    return resp;
}

void TrcPktDecodeEtmV3::setNeedAddr(bool bNeedAddr)
{
    m_bNeedAddr = bNeedAddr;
    m_bSentUnknown = false;    
}

ocsd_datapath_resp_t TrcPktDecodeEtmV3::processISync(const bool withCC, const bool firstSync /* = false */)
{
    // map ISync reason to generic reason codes.
    static trace_on_reason_t on_map[] = { TRACE_ON_NORMAL, TRACE_ON_NORMAL,
        TRACE_ON_OVERFLOW, TRACE_ON_EX_DEBUG };

    ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
    bool ctxtUpdate = m_curr_packet_in->isCtxtUpdated();   
    OcsdTraceElement *pElem = 0;

    try {

        pElem = GetNextOpElem(resp);

        if(firstSync || (m_curr_packet_in->getISyncReason() != iSync_Periodic))
        {
            pElem->setType(OCSD_GEN_TRC_ELEM_TRACE_ON);
            pElem->setTraceOnReason(on_map[(int)m_curr_packet_in->getISyncReason()]);
            pElem =  GetNextOpElem(resp);           
        }

        // look for context changes....
        if(ctxtUpdate || firstSync)
        {
            // if not first time out, read existing context in output element,
            // otherwise we are setting it new.
            if(firstSync)  
                m_PeContext.resetCtxt();

            if(m_curr_packet_in->isCtxtIDUpdated())
                m_PeContext.setCtxtID(m_curr_packet_in->getCtxtID());
            if(m_curr_packet_in->isVMIDUpdated())
                m_PeContext.setVMID(m_curr_packet_in->getVMID());
            if(m_curr_packet_in->isCtxtFlagsUpdated())
            {
                m_PeContext.setEL(m_curr_packet_in->isHyp() ? ocsd_EL2 : ocsd_EL_unknown);
                m_PeContext.setSecLevel(m_curr_packet_in->isNS() ? ocsd_sec_nonsecure : ocsd_sec_secure);
            }

            // prepare the context packet
            pElem->setType(OCSD_GEN_TRC_ELEM_PE_CONTEXT);
            pElem->setContext(m_PeContext);
            pElem->setISA(m_curr_packet_in->ISA());

            // with cycle count...
            if(m_curr_packet_in->getISyncHasCC())
                pElem->setCycleCount(m_curr_packet_in->getCycleCount());

        }

        // set ISync address - if it is a valid I address
        if(!m_curr_packet_in->getISyncNoAddr())
        {
            if(m_curr_packet_in->getISyncIsLSiPAddr())
            {
                // TBD: handle extra data processing instruction for data trace
                // need to output E atom relating to the data instruction
                // rare - on start-up case.

                // main instruction address saved in data address for this packet type.
                m_IAddr = m_curr_packet_in->getDataAddr();
            }
            else
            {
                m_IAddr = m_curr_packet_in->getAddr();
            }
            setNeedAddr(false);    // ready to process atoms.
        }
        m_curr_state = m_outputElemList.elemToSend() ? SEND_PKTS : DECODE_PKTS;
    }
    catch(ocsdError &err)
    {
        LogError(err);
        resetDecoder(); // mark decoder as unsynced - dump any current state.
    }
    return resp;
}

ocsd_datapath_resp_t TrcPktDecodeEtmV3::processBranchAddr()
{
    ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
    OcsdTraceElement *pElem = 0;
    bool bUpdatePEContext = false;

    // might need to cancel something ... if the last output was an instruction range or excep return
    if(m_curr_packet_in->isExcepCancel())
        m_outputElemList.cancelPendElem();
    else         
        m_outputElemList.commitAllPendElem(); // otherwise commit any pending elements.

    // record the address
    m_IAddr = m_curr_packet_in->getAddr();
    setNeedAddr(false);    // no longer need an address.
   
    // exception packet - may need additional output
    if(m_curr_packet_in->isExcepPkt())
    {
        // exeception packet may have exception, context change, or both.
        // check for context change
        if(m_curr_packet_in->isCtxtUpdated())
        {
            
            ocsd_sec_level sec = m_curr_packet_in->isNS() ? ocsd_sec_nonsecure : ocsd_sec_secure;
            if(sec != m_PeContext.getSecLevel())
            {
                m_PeContext.setSecLevel(sec);
                bUpdatePEContext = true;
            }
            ocsd_ex_level pkt_el = m_curr_packet_in->isHyp() ?  ocsd_EL2 : ocsd_EL_unknown;
            if(pkt_el != m_PeContext.getEL())
            {
                m_PeContext.setEL(pkt_el);
                bUpdatePEContext = true;
            }
        }

        // now decide if we need to send any packets out.
        try {

            if(bUpdatePEContext)
            {
                pElem = GetNextOpElem(resp);
                pElem->setType(OCSD_GEN_TRC_ELEM_PE_CONTEXT);
                pElem->setContext(m_PeContext);
            }

            // check for exception
            if(m_curr_packet_in->excepNum() != 0)
            {
                pElem = GetNextOpElem(resp);
                pElem->setType(OCSD_GEN_TRC_ELEM_EXCEPTION);
                pElem->setExceptionNum(m_curr_packet_in->excepNum());                        
            }

            // finally - do we have anything to send yet?
            m_curr_state = m_outputElemList.elemToSend() ? SEND_PKTS : DECODE_PKTS;
        }
        catch(ocsdError &err)
        {
            LogError(err);
            resetDecoder(); // mark decoder as unsynced - dump any current state.
        }
    }       
    return resp;
}


ocsd_datapath_resp_t TrcPktDecodeEtmV3::processPHdr()
{
    ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
    OcsdTraceElement *pElem = 0;
    ocsd_isa isa;
    Etmv3Atoms atoms(m_config->isCycleAcc());

    atoms.initAtomPkt(m_curr_packet_in,m_index_curr_pkt);
    isa = m_curr_packet_in->ISA();
    m_code_follower.setMemSpaceAccess((m_PeContext.getSecLevel() ==  ocsd_sec_secure) ? OCSD_MEM_SPACE_S : OCSD_MEM_SPACE_N);

    try
    {
        do 
        {
            // if we do not have a valid address then send any cycle count elements 
            // and stop processing
            if(m_bNeedAddr)
            {
                // output unknown address packet or a cycle count packet
                if(!m_bSentUnknown || m_config->isCycleAcc())
                {
                    pElem = GetNextOpElem(resp);
                    if(m_bSentUnknown || !atoms.numAtoms())
                        pElem->setType(OCSD_GEN_TRC_ELEM_CYCLE_COUNT);
                    else
                        pElem->setType(OCSD_GEN_TRC_ELEM_ADDR_UNKNOWN);
                    if(m_config->isCycleAcc())
                        pElem->setCycleCount(atoms.getRemainCC());
                    m_bSentUnknown = true;
                }
                atoms.clearAll();   // skip remaining atoms
            }
            else    // have an address, can process atoms
            {
                pElem = GetNextOpElem(resp);
                pElem->setType(OCSD_GEN_TRC_ELEM_INSTR_RANGE);
    
                // cycle accurate may have a cycle count to use
                if(m_config->isCycleAcc())
                {
                    // note: it is possible to have a CC only atom packet.
                    if(!atoms.numAtoms())   // override type if CC only
                         pElem->setType(OCSD_GEN_TRC_ELEM_CYCLE_COUNT);
                    // set cycle count
                    pElem->setCycleCount(atoms.getAtomCC());
                }

                // now process the atom 
                if(atoms.numAtoms())
                {
                    m_code_follower.setISA(isa);
                    m_code_follower.followSingleAtom(m_IAddr,atoms.getCurrAtomVal());

                    // valid code range
                    if(m_code_follower.hasRange())
                    {
                        pElem->setAddrRange(m_IAddr,m_code_follower.getRangeEn());
                        pElem->setLastInstrInfo(atoms.getCurrAtomVal() == ATOM_E, 
                                    m_code_follower.getInstrType(),
                                    m_code_follower.getInstrSubType(),m_code_follower.getInstrSize());
                        pElem->setLastInstrCond(m_code_follower.isCondInstr());
                        pElem->setISA(isa);
                        if(m_code_follower.hasNextAddr())
                            m_IAddr = m_code_follower.getNextAddr();
                        else
                            setNeedAddr(true);
                    }

                    // next address has new ISA?
                    if(m_code_follower.ISAChanged())
                        isa = m_code_follower.nextISA();
                    
                    // there is a nacc
                    if(m_code_follower.isNacc())
                    {
                        if(m_code_follower.hasRange())
                        {
                            pElem = GetNextOpElem(resp);
                            pElem->setType(OCSD_GEN_TRC_ELEM_ADDR_NACC);
                        }
                        else
                            pElem->updateType(OCSD_GEN_TRC_ELEM_ADDR_NACC);
                        pElem->setAddrStart(m_code_follower.getNaccAddr());
                        setNeedAddr(true);
                        m_code_follower.clearNacc(); // we have generated some code for the nacc.
                    }
                }

                atoms.clearAtom();  // next atom
            }
        }
        while(atoms.numAtoms());

        // is tha last element an atom?
        int numElem = m_outputElemList.getNumElem();
        if(numElem >= 1)
        {
            // if the last thing is an instruction range, pend it - could be cancelled later.
            if(m_outputElemList.getElemType(numElem-1) == OCSD_GEN_TRC_ELEM_INSTR_RANGE)
                m_outputElemList.pendLastNElem(1);
        }

        // finally - do we have anything to send yet?
        m_curr_state = m_outputElemList.elemToSend() ? SEND_PKTS : DECODE_PKTS;
    }
    catch(ocsdError &err)
    {
        LogError(err);
        resetDecoder(); // mark decoder as unsynced - dump any current state.
    }
    return resp;
}

// if v7M -> pend only ERET, if V7A/R pend ERET and prev instr.
void TrcPktDecodeEtmV3::pendExceptionReturn()
{
    int pendElem = 1;
    if(m_config->getCoreProfile() != profile_CortexM)
    {
        int nElem = m_outputElemList.getNumElem();
        if(nElem > 1)
        {
           if(m_outputElemList.getElemType(nElem - 2) == OCSD_GEN_TRC_ELEM_INSTR_RANGE)
               pendElem = 2;    // need to pend instr+eret for A/R
        }
    }
    m_outputElemList.pendLastNElem(pendElem);
}

/* End of File trc_pkt_decode_etmv3.cpp */