167754Smsmith/*
267754Smsmith * \file       trc_frame_decoder_impl.h
367754Smsmith * \brief      OpenCSD : Trace Deformatter implementation.
491116Smsmith *
567754Smsmith * \copyright  Copyright (c) 2015, ARM Limited. All Rights Reserved.
667754Smsmith */
767754Smsmith
867754Smsmith/*
967754Smsmith * Redistribution and use in source and binary forms, with or without modification,
1067754Smsmith * are permitted provided that the following conditions are met:
1167754Smsmith *
1291116Smsmith * 1. Redistributions of source code must retain the above copyright notice,
1370243Smsmith * this list of conditions and the following disclaimer.
1467754Smsmith *
1567754Smsmith * 2. Redistributions in binary form must reproduce the above copyright notice,
1667754Smsmith * this list of conditions and the following disclaimer in the documentation
1767754Smsmith * and/or other materials provided with the distribution.
1867754Smsmith *
1967754Smsmith * 3. Neither the name of the copyright holder nor the names of its contributors
2067754Smsmith * may be used to endorse or promote products derived from this software without
2167754Smsmith * specific prior written permission.
2267754Smsmith *
2367754Smsmith * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
2467754Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2567754Smsmith * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2667754Smsmith * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
2767754Smsmith * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2867754Smsmith * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2967754Smsmith * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
3067754Smsmith * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3167754Smsmith * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3267754Smsmith * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3367754Smsmith */
3467754Smsmith
3567754Smsmith#ifndef ARM_TRC_FRAME_DECODER_IMPL_H_INCLUDED
3667754Smsmith#define ARM_TRC_FRAME_DECODER_IMPL_H_INCLUDED
3767754Smsmith
3867754Smsmith#include "opencsd/ocsd_if_types.h"
3967754Smsmith#include "common/comp_attach_pt_t.h"
4067754Smsmith#include "interfaces/trc_data_raw_in_i.h"
4167754Smsmith#include "interfaces/trc_data_rawframe_in_i.h"
4267754Smsmith#include "interfaces/trc_indexer_src_i.h"
4367754Smsmith#include "common/trc_component.h"
4467754Smsmith
4567754Smsmith//! output data fragment from the current frame - collates bytes associated with an ID.
4667754Smsmithtypedef struct _out_chan_data {
4767754Smsmith    ocsd_trc_index_t index;      //!< trace source index for start of these bytes
4867754Smsmith    uint8_t id;             //!< Id for these bytes
4967754Smsmith    uint8_t data[15];       //!< frame data bytes for this ID
5067754Smsmith    uint32_t valid;         //!< Valid data bytes.
5167754Smsmith    uint32_t used;          //!< Data bytes output (used by attached processor).
5267754Smsmith} out_chan_data;
5367754Smsmith
5467754Smsmithclass TraceFmtDcdImpl : public TraceComponent, ITrcDataIn
5567754Smsmith{
5667754Smsmithprivate:
5767754Smsmith    TraceFmtDcdImpl();
5867754Smsmith    TraceFmtDcdImpl(int instNum);
5967754Smsmith    virtual ~TraceFmtDcdImpl();
6067754Smsmith
6167754Smsmith    /* the data input interface from the reader */
6267754Smsmith    virtual ocsd_datapath_resp_t TraceDataIn(  const ocsd_datapath_op_t op,
6367754Smsmith                                                const ocsd_trc_index_t index,
6467754Smsmith                                                const uint32_t dataBlockSize,
6567754Smsmith                                                const uint8_t *pDataBlock,
6667754Smsmith                                                uint32_t *numBytesProcessed);
6767754Smsmith
6867754Smsmith    /* enable / disable ID streams - default as all enabled */
6967754Smsmith    ocsd_err_t OutputFilterIDs(std::vector<uint8_t> &id_list, bool bEnable);
7067754Smsmith    ocsd_err_t OutputFilterAllIDs(bool bEnable);
7167754Smsmith
7267754Smsmith    /* decode control */
7367754Smsmith    ocsd_datapath_resp_t Reset();    /* reset the decode to the start state, drop partial data - propogate to attached components */
7467754Smsmith    ocsd_datapath_resp_t Flush();
7567754Smsmith    ocsd_err_t DecodeConfigure(uint32_t flags);
7667754Smsmith    ocsd_err_t SetForcedSyncIndex(ocsd_trc_index_t index, bool bSet);
7767754Smsmith
7867754Smsmith    void SetDemuxStatsBlock(ocsd_demux_stats_t *pStatsBlock) { m_pStatsBlock = pStatsBlock; };
7967754Smsmith
8067754Smsmithprivate:
8167754Smsmith    ocsd_datapath_resp_t executeNoneDataOpAllIDs(ocsd_datapath_op_t op, const ocsd_trc_index_t index = 0);
8267754Smsmith    ocsd_datapath_resp_t processTraceData(const ocsd_trc_index_t index,
8367754Smsmith                                                const uint32_t dataBlockSize,
8467754Smsmith                                                const uint8_t *pDataBlock,
8567754Smsmith                                                uint32_t *numBytesProcessed);
8667754Smsmith    // process phases
8767754Smsmith    bool checkForSync(); // find the sync point in the incoming block
8867754Smsmith    bool extractFrame(); // extract the frame data from incoming stream
8967754Smsmith    bool unpackFrame(); // process a complete frame.
9067754Smsmith    bool outputFrame(); // output data to channels.
9167754Smsmith
9267754Smsmith
9367754Smsmith    // managing data path responses.
9467754Smsmith    void InitCollateDataPathResp() { m_highestResp = OCSD_RESP_CONT; };
9567754Smsmith    void CollateDataPathResp(const ocsd_datapath_resp_t resp);
9667754Smsmith    const ocsd_datapath_resp_t highestDataPathResp() const { return m_highestResp; };
9767754Smsmith    const bool dataPathCont() const { return (bool)(m_highestResp < OCSD_RESP_WAIT); };
9867754Smsmith
9967754Smsmith    // deformat state
10067754Smsmith    void resetStateParams();
10167754Smsmith
10267754Smsmith    // synchronisation
10367754Smsmith    uint32_t findfirstFSync();
10467754Smsmith    void outputUnsyncedBytes(uint32_t num_bytes);  // output bytes as unsynced from current buffer position.
10567754Smsmith
10667754Smsmith    // output bytes to raw frame monitor
10767754Smsmith    void outputRawMonBytes(const ocsd_datapath_op_t op,
10867754Smsmith                           const ocsd_trc_index_t index,
10967754Smsmith                           const ocsd_rawframe_elem_t frame_element,
11067754Smsmith                           const int dataBlockSize,
11167754Smsmith                           const uint8_t *pDataBlock,
11267754Smsmith                           const uint8_t traceID);
11367754Smsmith
11467754Smsmith
11567754Smsmith    void setRawChanFilterAll(bool bEnable);
11667754Smsmith    const bool rawChanEnabled(const uint8_t id) const;
11767754Smsmith
11867754Smsmith	ocsd_err_t checkForResetFSyncPatterns(uint32_t &f_sync_bytes);
11967754Smsmith
12067754Smsmith    friend class TraceFormatterFrameDecoder;
12167754Smsmith
12267754Smsmith    // stats updates
12377424Smsmith    void addToIDStats(uint64_t val);
12491116Smsmith    void addToNoIDStats(uint64_t val);
12567754Smsmith    void addToFrameStats(uint64_t val);
12667754Smsmith    void addToUnknownIDStats(uint64_t val);
12767754Smsmith    void addToReservedIDStats(uint64_t val);
12867754Smsmith
12991116Smsmith    bool isReservedID(uint8_t ID) { return ((ID == 0) || (ID >= 0x70)); };
13067754Smsmith
13191116Smsmith    // attachment points
13291116Smsmith    componentAttachPt<ITrcDataIn> m_IDStreams[128];
13391116Smsmith    componentAttachPt<ITrcRawFrameIn> m_RawTraceFrame;
13467754Smsmith
13591116Smsmith    componentAttachPt<ITrcSrcIndexCreator> m_SrcIndexer;
13667754Smsmith
13791116Smsmith
13891116Smsmith    ocsd_datapath_resp_t m_highestResp;
13967754Smsmith
14067754Smsmith    /* static configuration */
14167754Smsmith    uint32_t m_cfgFlags;        /* configuration flags */
14267754Smsmith    ocsd_trc_index_t m_force_sync_idx;
14391116Smsmith    bool m_use_force_sync;
14491116Smsmith    uint32_t m_alignment;
14591116Smsmith
14691116Smsmith    /* dynamic state */
14791116Smsmith    ocsd_trc_index_t m_trc_curr_idx;    /* index of current trace data */
14867754Smsmith    bool m_frame_synced;
14967754Smsmith    bool m_first_data;
15091116Smsmith    uint8_t m_curr_src_ID;
15167754Smsmith
15267754Smsmith    // incoming frame buffer
15391116Smsmith    uint8_t m_ex_frm_data[OCSD_DFRMTR_FRAME_SIZE]; // buffer the current frame in case we have to stop part way through
15467754Smsmith    int m_ex_frm_n_bytes;   // number of valid bytes in the current frame (extraction)
15567754Smsmith    bool m_b_fsync_start_eob;  // flag to indicate that the end of the last buffer was a pair of bytes
15691116Smsmith                               // (0xffff) that could only validly be the start and FSYNC.
15767754Smsmith    ocsd_trc_index_t m_trc_curr_idx_sof; // trace source index at start of frame.
15891116Smsmith
15991116Smsmith    /* channel output data - can never be more than a frame of data for a single ID.
16091116Smsmith     * 8 possible ID changes per frame. Although the final one can have no associated data, a pathological
16167754Smsmith     * case exists with 7 ID changes, all data associated with a previous frame, except for last
16291116Smsmith     * ID / data byte which is data. Not possible with normal hardware but guard against corrupt input.
16367754Smsmith     */
16467754Smsmith    out_chan_data m_out_data[8]; // output data for a given ID
16591116Smsmith    int m_out_data_idx;          // number of out_chan_data frames used.
16667754Smsmith    int m_out_processed;         // number of complete out_chan_data frames output.
16791116Smsmith
16891116Smsmith    /* local copy of input buffer pointers*/
16991116Smsmith    const uint8_t *m_in_block_base;
17067754Smsmith    uint32_t m_in_block_size;
17191116Smsmith    uint32_t m_in_block_processed;
17267754Smsmith
17367754Smsmith    /* raw output options */
17491116Smsmith    bool m_b_output_packed_raw;
17583174Smsmith    bool m_b_output_unpacked_raw;
17691116Smsmith
17791116Smsmith    bool m_raw_chan_enable[128];
17878986Smsmith
17991116Smsmith    ocsd_demux_stats_t *m_pStatsBlock;
18067754Smsmith};
18167754Smsmith
18291116Smsmith
18391116Smsmith#endif // ARM_TRC_FRAME_DECODER_IMPL_H_INCLUDED
18478986Smsmith
18567754Smsmith/* End of File trc_frame_decoder_impl.h */
18667754Smsmith