#ifndef ARM_TRC_PKT_DECODE_BASE_H_INCLUDED
#define ARM_TRC_PKT_DECODE_BASE_H_INCLUDED
#include <new>
#include "trc_component.h"
#include "comp_attach_pt_t.h"
#include "interfaces/trc_pkt_in_i.h"
#include "interfaces/trc_gen_elem_in_i.h"
#include "interfaces/trc_tgt_mem_access_i.h"
#include "interfaces/trc_instr_decode_i.h"
class TrcPktDecodeI : public TraceComponent
{
public:
TrcPktDecodeI(const char *component_name);
TrcPktDecodeI(const char *component_name, int instIDNum);
virtual ~TrcPktDecodeI() {};
componentAttachPt<ITrcGenElemIn> *getTraceElemOutAttachPt() { return &m_trace_elem_out; };
componentAttachPt<ITargetMemAccess> *getMemoryAccessAttachPt() { return &m_mem_access; };
componentAttachPt<IInstrDecode> *getInstrDecodeAttachPt() { return &m_instr_decode; };
void setUsesMemAccess(bool bUsesMemaccess) { m_uses_memaccess = bUsesMemaccess; };
const bool getUsesMemAccess() const { return m_uses_memaccess; };
void setUsesIDecode(bool bUsesIDecode) { m_uses_idecode = bUsesIDecode; };
const bool getUsesIDecode() const { return m_uses_idecode; };
protected:
virtual ocsd_datapath_resp_t processPacket() = 0;
virtual ocsd_datapath_resp_t onEOT() = 0;
virtual ocsd_datapath_resp_t onReset() = 0;
virtual ocsd_datapath_resp_t onFlush() = 0;
virtual ocsd_err_t onProtocolConfig() = 0;
virtual const uint8_t getCoreSightTraceID() = 0;
const bool checkInit();
virtual void onFirstInitOK() {};
ocsd_datapath_resp_t outputTraceElement(const OcsdTraceElement &elem); ocsd_datapath_resp_t outputTraceElementIdx(ocsd_trc_index_t idx, const OcsdTraceElement &elem);
ocsd_err_t accessMemory(const ocsd_vaddr_t address, const ocsd_mem_space_acc_t mem_space, uint32_t *num_bytes, uint8_t *p_buffer);
ocsd_err_t invalidateMemAccCache();
ocsd_err_t instrDecode(ocsd_instr_info *instr_info);
componentAttachPt<ITrcGenElemIn> m_trace_elem_out;
componentAttachPt<ITargetMemAccess> m_mem_access;
componentAttachPt<IInstrDecode> m_instr_decode;
ocsd_trc_index_t m_index_curr_pkt;
bool m_decode_init_ok; bool m_config_init_ok;
std::string init_err_msg;
bool m_uses_memaccess;
bool m_uses_idecode;
};
inline TrcPktDecodeI::TrcPktDecodeI(const char *component_name) :
TraceComponent(component_name),
m_index_curr_pkt(0),
m_decode_init_ok(false),
m_config_init_ok(false),
m_uses_memaccess(true),
m_uses_idecode(true)
{
}
inline TrcPktDecodeI::TrcPktDecodeI(const char *component_name, int instIDNum) :
TraceComponent(component_name, instIDNum),
m_index_curr_pkt(0),
m_decode_init_ok(false),
m_config_init_ok(false),
m_uses_memaccess(true),
m_uses_idecode(true)
{
}
inline const bool TrcPktDecodeI::checkInit()
{
if(!m_decode_init_ok)
{
if(!m_config_init_ok)
init_err_msg = "No decoder configuration information";
else if(!m_trace_elem_out.hasAttachedAndEnabled())
init_err_msg = "No element output interface attached and enabled";
else if(m_uses_memaccess && !m_mem_access.hasAttachedAndEnabled())
init_err_msg = "No memory access interface attached and enabled";
else if(m_uses_idecode && !m_instr_decode.hasAttachedAndEnabled())
init_err_msg = "No instruction decoder interface attached and enabled";
else
m_decode_init_ok = true;
if (m_decode_init_ok)
onFirstInitOK();
}
return m_decode_init_ok;
}
inline ocsd_datapath_resp_t TrcPktDecodeI::outputTraceElement(const OcsdTraceElement &elem)
{
return m_trace_elem_out.first()->TraceElemIn(m_index_curr_pkt,getCoreSightTraceID(), elem);
}
inline ocsd_datapath_resp_t TrcPktDecodeI::outputTraceElementIdx(ocsd_trc_index_t idx, const OcsdTraceElement &elem)
{
return m_trace_elem_out.first()->TraceElemIn(idx, getCoreSightTraceID(), elem);
}
inline ocsd_err_t TrcPktDecodeI::instrDecode(ocsd_instr_info *instr_info)
{
if(m_uses_idecode)
return m_instr_decode.first()->DecodeInstruction(instr_info);
return OCSD_ERR_DCD_INTERFACE_UNUSED;
}
inline ocsd_err_t TrcPktDecodeI::accessMemory(const ocsd_vaddr_t address, const ocsd_mem_space_acc_t mem_space, uint32_t *num_bytes, uint8_t *p_buffer)
{
if(m_uses_memaccess)
return m_mem_access.first()->ReadTargetMemory(address,getCoreSightTraceID(),mem_space, num_bytes,p_buffer);
return OCSD_ERR_DCD_INTERFACE_UNUSED;
}
inline ocsd_err_t TrcPktDecodeI::invalidateMemAccCache()
{
if (!m_uses_memaccess)
return OCSD_ERR_DCD_INTERFACE_UNUSED;
m_mem_access.first()->InvalidateMemAccCache(getCoreSightTraceID());
return OCSD_OK;
}
template <class P, class Pc>
class TrcPktDecodeBase : public TrcPktDecodeI, public IPktDataIn<P>
{
public:
TrcPktDecodeBase(const char *component_name);
TrcPktDecodeBase(const char *component_name, int instIDNum);
virtual ~TrcPktDecodeBase();
virtual ocsd_datapath_resp_t PacketDataIn( const ocsd_datapath_op_t op,
const ocsd_trc_index_t index_sop,
const P *p_packet_in);
ocsd_err_t setProtocolConfig(const Pc *config);
const Pc * getProtocolConfig() const { return m_config; };
protected:
void ClearConfigObj();
Pc * m_config;
const P * m_curr_packet_in;
};
template <class P, class Pc> TrcPktDecodeBase<P, Pc>::TrcPktDecodeBase(const char *component_name) :
TrcPktDecodeI(component_name),
m_config(0)
{
}
template <class P, class Pc> TrcPktDecodeBase<P, Pc>::TrcPktDecodeBase(const char *component_name, int instIDNum) :
TrcPktDecodeI(component_name,instIDNum),
m_config(0)
{
}
template <class P, class Pc> TrcPktDecodeBase<P, Pc>::~TrcPktDecodeBase()
{
ClearConfigObj();
}
template <class P, class Pc> ocsd_datapath_resp_t TrcPktDecodeBase<P, Pc>::PacketDataIn( const ocsd_datapath_op_t op,
const ocsd_trc_index_t index_sop,
const P *p_packet_in)
{
ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
if(!checkInit())
{
LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_NOT_INIT,init_err_msg));
return OCSD_RESP_FATAL_NOT_INIT;
}
switch(op)
{
case OCSD_OP_DATA:
if(p_packet_in == 0)
{
LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_INVALID_PARAM_VAL));
resp = OCSD_RESP_FATAL_INVALID_PARAM;
}
else
{
m_curr_packet_in = p_packet_in;
m_index_curr_pkt = index_sop;
resp = processPacket();
}
break;
case OCSD_OP_EOT:
resp = onEOT();
break;
case OCSD_OP_FLUSH:
resp = onFlush();
break;
case OCSD_OP_RESET:
resp = onReset();
break;
default:
LogError(ocsdError(OCSD_ERR_SEV_ERROR,OCSD_ERR_INVALID_PARAM_VAL));
resp = OCSD_RESP_FATAL_INVALID_OP;
break;
}
return resp;
}
template <class P, class Pc> ocsd_err_t TrcPktDecodeBase<P, Pc>::setProtocolConfig(const Pc *config)
{
ocsd_err_t err = OCSD_ERR_INVALID_PARAM_VAL;
if(config != 0)
{
ClearConfigObj(); m_config = new (std::nothrow) Pc(*config); if(m_config != 0)
{
err = onProtocolConfig();
if(err == OCSD_OK)
m_config_init_ok = true;
}
else
err = OCSD_ERR_MEM;
}
return err;
}
template <class P, class Pc> void TrcPktDecodeBase<P, Pc>::ClearConfigObj()
{
if(m_config)
{
delete m_config;
m_config = 0;
}
}
#endif