{{{ #!html

Writing TLM2.0-compliant timed SystemC simulation models for SoCLib

}}} Authors : Alain Greiner, François PĂȘcheux, Aline Vieira de Mello [[PageOutline]] = A) Introduction = This document is still under development. It describes the modeling rules for writing TLM-T SystemC simulation models for SoCLib that are compliant with the new TLM2.0 OSCI standard. These rules enforce the PDES (Parallel Discrete Event Simulation) principles. In the TLM-T approach, we don't use the SystemC global time, as each PDES process involved in the simulation has its own local time. PDES processes (implemented as SC_THREADS) synchronize through messages piggybacked with time information. Models complying to these rules can be used with the "standard" OSCI simulation engine (SystemC 2.x) and the TLM2.0 library, but can also be used also with others simulation engines, especially distributed, parallelized simulation engines. The examples presented below use the VCI/OCP communication protocol selected by the SoCLib project, but the TLM-T approach described here is very flexible, and is not limited to the VCI/OCP communication protocol. The interested user should also look at the [WritingRules/General general SoCLib rules]. = B) VCI initiator and VCI target = Figure 1 presents a minimal system containing one single VCI initiator, '''my_initiator''' , and one single VCI target, '''my_target''' . The initiator behavior is modeled by the SC_THREAD '''execLoop()''', that contains an infinite loop. The interface function '''vci_rsp_received()''' is executed when a VCI response packet is received by the initiator module. [[Image(tlmt_figure_1.png, nolink)]] Unlike the initiator, the target module has a purely reactive behaviour and is therefore modeled as a simple interface function. In other words, there is no need to use a SC_THREAD for a target component: the target behaviour is entirely described by the interface function '''vci_cmd_received()''', that is executed when a VCI command packet is received by the target module. The VCI communication channel is a point-to-point bi-directional channel, encapsulating two separated uni-directional channels: one to transmit the VCI command packet, one to transmit the VCI response packet. = C) VCI Transaction in TLM-T = The TLM2.0 standard allows the user to redefine both the payload and the phases of the transactions. Two classes have been defined in '''soclib_vci_types''' : a '''tlmt_vci_transaction''' and a '''tlmt_phase". The payload of a '''tlmt_vci_transaction''' contains three groups of information: * TLM2.0 generic fields * TLM-T specific fields * VCI specific fields {{{ class tlmt_vci_transaction { ... private: // TLM2.0 related fields and common structure sc_dt::uint64 address; unsigned char* data; unsigned int length; tlm_response_status response_status; unsigned char* byte_enable; unsigned int streaming_width; // TLM-T related fields bool* activity_status_ptr; sc_core::sc_time* local_time_ptr; // VCI related fields vci_param::cmd_t cmd; vci_param::srcid_t srcid; vci_param::trdid_t trdid; vci_param::pktid_t pktid; }}} The TLM2.0 compliant accessors allow to set the TLM2.0 related fields, such as the transaction address, the data & byte enable arrays pointers and its associated size in bytes. Dedicated VCI accessors are used to define the VCI transaction type, that can either be '''set_read()''' (for VCI read command), '''set_write()''' (for VCI write command),'''set_locked_read()''' (for VCI atomic locked read), and '''set_store_cond()''' (for VCI atomic store conditional). The '''set_src_id()''', '''set_trd_id()''' and '''set_pkt_id()''' functions respectively set the VCI source, thread and packet identifiers. NB : The char array approach defined by TLM2.0 can degrade the simulation speed, as the existing SoCLib models use uint32_t arrays to model both the embedded memories and the caches... Experiments are currently in progress to evaluate the performance degradation incurred by this char <-> uint formatting. It is therefore possible that the types of the data and byte_enable fields of the '''tlmt_vci_transaction''' will be changed to '''uint32*''' . = D) VCI initiator Modeling = == D.1) Member variables & methods == In the proposed example, the initiator module is modeled by the '''my_initiator''' class. This class inherits from the standard SystemC '''sc_core::sc_module''' class, that acts as the root class for all TLM-T modules. The initiator local time is contained in a member variable named '''m_localTime''', of type '''sc_core::sc_time'''. The local time can be accessed with the following accessors: '''addLocalTime()''', '''setLocalTime()''' and '''getLocalTime()'''. {{{ sc_core::sc_time m_local_time; // the initiator local time ... void addLocalTime(sc_core::sc_time t); // add an increment to the local time void setLocalTime(sc_core::sc_time& t); // set the local time sc_core::sc_time getLocalTime(void); // get the local time }}} The boolean member variable '''m_activity_status''' indicates if the initiator is currently active. It is used by the arbitration threads contained in the '''vci_vgmn''' interconnect, as described in section F. The corresponding access functions are '''setActivity()''' and '''getActivity()'''. {{{ bool m_activity_status; ... void setActivity(bool t); // set the activity status (true if the component is active) bool getActivity(void); // get the activity state }}} The '''execLoop()''' method, describing the initiator behaviour must be declared as a member function. The '''my_initiator''' class contains a member variable '''p_vci_init''', of type '''tlmt_simple_initiator_socket''', representing the VCI initiator port. It must also define an interface function to handle the VCI response packets. == D.2) Sending a VCI command packet == To send a VCI command packet, the '''execLoop()''' method must use the '''nb_transport_fw()''' method, defined by TLM2.0, that is a member function of the '''p_vci_init''' port. The prototype of this method is the following: {{{ tlm::tlm_sync_enum nb_transport_fw ( soclib_vci_types::tlm_payload_type &payload, // payload soclib_vci_types::tlm_phase_type &phase, // transaction phase (TLMT_CMD) sc_core::sc_time &time); // local time }}} The first argument is a pointer to the payload, the second represents the phase, and the third argument contains the initiator local time. The return value is not used in this TLM-T implementation. The '''nb_transport_fw()''' function is non-blocking. To implement a blocking transaction (such as a cache line read, where the processor is stalled during the VCI transaction), the model designer must use the SystemC '''sc_core::wait(x)''' primitive ('''x''' being of type '''sc_core::sc_event'''): the '''execLoop()''' thread is then suspended, and will be reactivated when the response packet is actually received. == D.3) Receiving a VCI response packet == To receive a VCI response packet, an interface function must be defined as a member function of the class '''my_initiator'''. This function (named '''vci_rsp_received()''' in the example), must be linked to the '''p_vci_init''' port, and is executed each time a VCI response packet is received on the '''p_vci_init''' port. The function name is not constrained, but the arguments must respect the following prototype: {{{ tlm::tlm_sync_enum vci_rsp_received ( soclib_vci_types::tlm_payload_type &payload, // payload soclib_vci_types::tlm_phase_type &phase, // transaction phase (TLMT_RSP) sc_core::sc_time &time); // response time }}} The return value (type tlm::tlm_sync_enum) is not used in this TLM-T implementation, and must be sytematically set to tlm::TLM_COMPLETED. In the general case, the actions executed by the interface function depend on both the phase argument, and on the transaction type (defined in the payload). For sake of simplicity, the interface function proposed below does not make any distinction between transaction types. == D.4) Initiator Constructor == The constructor of the class '''my_initiator''' must initialize all the member variables, including the '''p_vci_init''' port. The '''vci_rsp_received()''' function being executed in the context of the thread sending the response packet, a link between the '''p_vci_init''' port and this interface function must be established. The constructor for the '''p_vci_init''' port must be called with the following arguments: {{{ p_vci_init.register_nb_transport_bw(this, &my_initiator::vci_rsp_received); }}} == D.5) Lookahead parameter == The SystemC simulation engine behaves as a cooperative, non-preemptive multi-tasks system. Any thread in the system must stop execution after at some point, in order to allow the other threads to execute. With the proposed approach, a TLM-T initiator will never stop if it does not execute blocking communication (such as a processor that has all code and data in the L1 caches). To solve this issue, it is necessary to define -for each initiator module- a '''lookahead''' parameter. This parameter defines the maximum number of cycles that can be executed by the thread before it is descheduled. The '''lookahead''' parameter allows the system designer to bound the de-synchronization time interval between threads. A small value for this parameter results in a better timing accuracy for the simulation, but implies a larger number of context switches, and a slower simulation speed. == D.6) VCI initiator example == {{{ ////////////////////////// my_initiator.h //////////////////////////////// #ifndef __MY_INITIATOR_H__ #define __MY_INITIATOR_H__ #include "tlm.h" // TLM headers #include "tlmt_transactions.h" // VCI headers #include "tlmt_simple_initiator_socket.h" // VCI socket #include "mapping_table.h" class my_initiator // my_initiator : public sc_core::sc_module // inherit from SC module base class { private: //Variables typedef soclib::tlmt::VciParams vci_param; sc_core::sc_event m_rspEvent; sc_core::sc_time m_localTime; bool m_activity; uint32_t m_initid; uint32_t m_counter; uint32_t m_lookahead; ///////////////////////////////////////////////////////////////////////////////////// // Functions ///////////////////////////////////////////////////////////////////////////////////// void execLoop(void); // initiator thread void addLocalTime(sc_core::sc_time t); // add a value to the local time void setLocalTime(sc_core::sc_time& t); // set the local time sc_core::sc_time getLocalTime(void); // get the local time void setActivity(bool t); // set the activity status (true if the component is active) bool getActivity(void); // get the activity state ///////////////////////////////////////////////////////////////////////////////////// // Virtual Fuctions tlm::tlm_bw_transport_if (VCI INITIATOR SOCKET) ///////////////////////////////////////////////////////////////////////////////////// /// Receive rsp from target tlm::tlm_sync_enum vci_rsp_received // for resp messages ( soclib_vci_types::tlm_payload_type &payload, // payload soclib_vci_types::tlm_phase_type &phase, // transaction phase sc_core::sc_time &time); // resp time protected: SC_HAS_PROCESS(my_initiator); public: tlmt_simple_initiator_socket p_vci_init; // VCI initiator port //constructor my_initiator( // constructor sc_core::sc_module_name name, // module name const soclib::common::IntTab &index, // VCI initiator index const soclib::common::MappingTable &mt, // mapping table uint32_t lookahead // lookahead ); }; #endif /* __MY_INITIATOR_H__ */ ////////////////////////// my_initiator.cpp //////////////////////////////// #include "my_initiator.h" // Our header #ifndef MY_INITIATOR_DEBUG #define MY_INITIATOR_DEBUG 1 #endif #define tmpl(x) x my_initiator ///Constructor tmpl (/**/)::my_initiator ( sc_core::sc_module_name name, // module name const soclib::common::IntTab &index, // index of mapping table const soclib::common::MappingTable &mt, // mapping table uint32_t lookahead // lookahead ) : sc_module(name) // init module name , p_vci_init("p_vci_init") // vci initiator socket name { //register callback function p_vci_init.register_nb_transport_bw(this, &my_initiator::vci_rsp_received); // initiator identification m_initid = mt.indexForId(index); //lookahead control m_counter = 0; m_lookahead = lookahead; //initialize the local time m_localTime= 0 * UNIT_TIME; // initialize the activity variable setActivity(true); // register thread process SC_THREAD(execLoop); } ///////////////////////////////////////////////////////////////////////////////////// // Functions ///////////////////////////////////////////////////////////////////////////////////// tmpl (sc_core::sc_time)::getLocalTime() { return m_localTime; } tmpl (bool)::getActivity() { return m_activity; } tmpl (void)::setLocalTime(sc_core::sc_time &t) { m_localTime=t; } tmpl (void)::addLocalTime(sc_core::sc_time t) { m_localTime= m_localTime + t; } tmpl (void)::setActivity(bool t) { m_activity =t; } tmpl (void)::execLoop(void) // initiator thread { soclib_vci_types::tlm_payload_type payload; soclib_vci_types::tlm_phase_type phase; sc_core::sc_time sendTime; unsigned char data[32]; unsigned char byte_enable[32]; int pktid = 0; int nbytes = 4; // 1 word of 32 bits uint32_t int_data = 12345678; std::ostringstream name; name << "" << int_data; std::cout << "NAME = " << std::dec << name << std::endl; for(int i=0; i