📄 simple_target_socket.h
字号:
/***************************************************************************** The following code is derived, directly or indirectly, from the SystemC source code Copyright (c) 1996-2008 by all Contributors. All Rights reserved. The contents of this file are subject to the restrictions and limitations set forth in the SystemC Open Source License Version 3.0 (the "License"); You may not use this file except in compliance with such restrictions and limitations. You may obtain instructions on how to receive a copy of the License at http://www.systemc.org/. Software distributed by Contributors under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. *****************************************************************************/#ifndef __SIMPLE_TARGET_SOCKET_H__#define __SIMPLE_TARGET_SOCKET_H__#include "tlm.h"#include "peq_with_get.h"namespace tlm_utils {template <typename MODULE, unsigned int BUSWIDTH = 32, typename TYPES = tlm::tlm_base_protocol_types>class simple_target_socket : public tlm::tlm_target_socket<BUSWIDTH, TYPES>{ friend class fw_process; friend class bw_process;public: typedef typename TYPES::tlm_payload_type transaction_type; typedef typename TYPES::tlm_phase_type phase_type; typedef tlm::tlm_sync_enum sync_enum_type; typedef tlm::tlm_fw_transport_if<TYPES> fw_interface_type; typedef tlm::tlm_bw_transport_if<TYPES> bw_interface_type; typedef tlm::tlm_target_socket<BUSWIDTH, TYPES> base_type;public: explicit simple_target_socket(const char* n = "simple_target_socket") : base_type(sc_core::sc_gen_unique_name(n)), m_fw_process(this), m_bw_process(this) { bind(m_fw_process); } // bw transport must come thru us. tlm::tlm_bw_transport_if<TYPES> * operator ->() {return &m_bw_process;} // REGISTER_XXX void register_nb_transport_fw(MODULE* mod, sync_enum_type (MODULE::*cb)(transaction_type&, phase_type&, sc_core::sc_time&)) { assert(!sc_core::sc_get_curr_simcontext()->elaboration_done()); m_fw_process.set_nb_transport_ptr(mod, cb); } void register_b_transport(MODULE* mod, void (MODULE::*cb)(transaction_type&, sc_core::sc_time&)) { assert(!sc_core::sc_get_curr_simcontext()->elaboration_done()); m_fw_process.set_b_transport_ptr(mod, cb); } void register_transport_dbg(MODULE* mod, unsigned int (MODULE::*cb)(transaction_type&)) { assert(!sc_core::sc_get_curr_simcontext()->elaboration_done()); m_fw_process.set_transport_dbg_ptr(mod, cb); } void register_get_direct_mem_ptr(MODULE* mod, bool (MODULE::*cb)(transaction_type&, tlm::tlm_dmi&)) { assert(!sc_core::sc_get_curr_simcontext()->elaboration_done()); m_fw_process.set_get_direct_mem_ptr(mod, cb); }private: //make call on bw path. sync_enum_type bw_nb_transport(transaction_type &trans, phase_type &phase, sc_core::sc_time &t) { return base_type::operator ->()->nb_transport_bw(trans, phase, t); } void bw_invalidate_direct_mem_ptr(sc_dt::uint64 s,sc_dt::uint64 e) { base_type::operator ->()->invalidate_direct_mem_ptr(s, e); } //Helper class to handle bw path calls // Needed to detect transaction end when called from b_transport. class bw_process : public tlm::tlm_bw_transport_if<TYPES> { public: bw_process(simple_target_socket *p_own) : m_owner(p_own) { } sync_enum_type nb_transport_bw(transaction_type &trans, phase_type &phase, sc_core::sc_time &t) { typename std::map<transaction_type*, sc_core::sc_event *>::iterator it; it = m_owner->m_pending_trans.find(&trans); if(it == m_owner->m_pending_trans.end()) { // Not a blocking call, forward. return m_owner->bw_nb_transport(trans, phase, t); } else { if (phase == tlm::END_REQ) { m_owner->m_end_request.notify(sc_core::SC_ZERO_TIME); return tlm::TLM_ACCEPTED; } else if (phase == tlm::BEGIN_RESP) { if (m_owner->m_current_transaction == &trans) { m_owner->m_end_request.notify(sc_core::SC_ZERO_TIME); } //TODO: add response-accept delay? it->second->notify(t); m_owner->m_pending_trans.erase(it); return tlm::TLM_COMPLETED; } else { assert(0); exit(1); }// return tlm::TLM_COMPLETED; //Should not reach here } } void invalidate_direct_mem_ptr(sc_dt::uint64 s,sc_dt::uint64 e) { return m_owner->bw_invalidate_direct_mem_ptr(s, e); } private: simple_target_socket *m_owner; }; class fw_process : public tlm::tlm_fw_transport_if<TYPES>, public tlm::tlm_mm_interface { public: typedef sync_enum_type (MODULE::*NBTransportPtr)(transaction_type&, tlm::tlm_phase&, sc_core::sc_time&); typedef void (MODULE::*BTransportPtr)(transaction_type&, sc_core::sc_time&); typedef unsigned int (MODULE::*TransportDbgPtr)(transaction_type&); typedef bool (MODULE::*GetDirectMemPtr)(transaction_type&, tlm::tlm_dmi&); fw_process(simple_target_socket *p_own) : m_name(p_own->name()), m_owner(p_own), m_mod(0), m_nb_transport_ptr(0), m_b_transport_ptr(0), m_transport_dbg_ptr(0), m_get_direct_mem_ptr(0), m_peq(sc_core::sc_gen_unique_name("m_peq")), m_response_in_progress(false) { sc_core::sc_spawn_options opts; opts.set_sensitivity(&m_peq.get_event()); sc_core::sc_spawn(sc_bind(&fw_process::b2nb_thread, this), sc_core::sc_gen_unique_name("b2nb_thread"), &opts); } void set_nb_transport_ptr(MODULE* mod, NBTransportPtr p) { if (m_nb_transport_ptr) { std::cerr << m_name << ": non-blocking callback allready registered" << std::endl; } else { assert(!m_mod || m_mod == mod); m_mod = mod; m_nb_transport_ptr = p; } } void set_b_transport_ptr(MODULE* mod, BTransportPtr p) { if (m_b_transport_ptr) { std::cerr << m_name << ": non-blocking callback allready registered" << std::endl; } else { assert(!m_mod || m_mod == mod); m_mod = mod; m_b_transport_ptr = p; } } void set_transport_dbg_ptr(MODULE* mod, TransportDbgPtr p) { if (m_transport_dbg_ptr) { std::cerr << m_name << ": debug callback allready registered" << std::endl; } else { assert(!m_mod || m_mod == mod); m_mod = mod; m_transport_dbg_ptr = p; } } void set_get_direct_mem_ptr(MODULE* mod, GetDirectMemPtr p) { if (m_get_direct_mem_ptr) { std::cerr << m_name << ": get DMI pointer callback allready registered" << std::endl; } else { assert(!m_mod || m_mod == mod); m_mod = mod; m_get_direct_mem_ptr = p; } }// Interface implementation sync_enum_type nb_transport_fw(transaction_type& trans, phase_type& phase, sc_core::sc_time& t) { if (m_nb_transport_ptr) { // forward call assert(m_mod); return (m_mod->*m_nb_transport_ptr)(trans, phase, t); } else if (m_b_transport_ptr) { if (phase == tlm::BEGIN_REQ) { // create thread to do blocking call sc_core::sc_spawn_options opts; opts.dont_initialize(); sc_core::sc_event *e = new sc_core::sc_event; opts.set_sensitivity(e); // sc_core::sc_spawn(sc_bind(&fw_process::nb2b_thread, this, sc_ref(trans), e), // sc_core::sc_gen_unique_name("nb2b_thread"), &opts); process_handle_class * ph = m_process_handle.get_handle(&trans,e); if (!ph) { // create new dynamic process ph = new process_handle_class(&trans,e); m_process_handle.put_handle(ph); sc_core::sc_spawn(sc_bind(&fw_process::nb2b_thread,this, ph, sc_ref(trans), e), sc_core::sc_gen_unique_name("nb2b_thread"), &opts); } else { // reuse existing dynamic process and resume it ph->m_wakeup.notify(); // immidiate notification } e->notify(t); return tlm::TLM_ACCEPTED; } else if (phase == tlm::END_RESP) { m_response_in_progress = false; m_end_response.notify(t); return tlm::TLM_COMPLETED; } else { assert(0); exit(1);// return tlm::TLM_COMPLETED; ///< unreachable code } } else { std::cerr << m_name << ": no transport callback registered" << std::endl; assert(0); exit(1);// return tlm::TLM_COMPLETED; ///< unreachable code } } void b_transport(transaction_type& trans, sc_core::sc_time& t) { if (m_b_transport_ptr) { // forward call assert(m_mod); (m_mod->*m_b_transport_ptr)(trans, t); return; } else if (m_nb_transport_ptr) { m_peq.notify(trans, t); t = sc_core::SC_ZERO_TIME; mm_end_event_ext mm_ext; const bool mm_added = !trans.has_mm(); if (mm_added) { trans.set_mm(this); trans.set_auto_extension(&mm_ext); trans.acquire(); } // wait until transaction is finished sc_core::sc_event end_event; m_owner->m_pending_trans[&trans] = &end_event; sc_core::wait(end_event); if (mm_added) { // release will not delete the transaction, it will notify mm_ext.done trans.release(); if (trans.get_ref_count()) { sc_core::wait(mm_ext.done); } trans.set_mm(0); } } else { std::cerr << m_name << ": no transport callback registered" << std::endl; assert(0); exit(1);// return tlm::TLM_COMPLETED; ///< unreachable code } } unsigned int transport_dbg(transaction_type& trans) { if (m_transport_dbg_ptr) { // forward call assert(m_mod); return (m_mod->*m_transport_dbg_ptr)(trans); } else { // No debug support return 0; } } bool get_direct_mem_ptr(transaction_type& trans, tlm::tlm_dmi& dmi_data) { if (m_get_direct_mem_ptr) { // forward call assert(m_mod); return (m_mod->*m_get_direct_mem_ptr)(trans, dmi_data); } else { // No DMI support dmi_data.allow_read_write(); dmi_data.set_start_address(0x0); dmi_data.set_end_address((sc_dt::uint64)-1); return false; } } private:// dynamic process handler for nb2b conversion class process_handle_class { public: process_handle_class(transaction_type * trans, sc_core::sc_event* e): m_trans(trans),m_e(e),m_suspend(false){} transaction_type* m_trans;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -