|
| 1 | + |
| 2 | + |
| 3 | +#ifndef IMG_INITIATOR_CPP |
| 4 | +#define IMG_INITIATOR_CPP |
| 5 | + |
| 6 | +// #include "tlm_transaction.cpp" |
| 7 | +#include "transaction_memory_manager.cpp" |
| 8 | + |
| 9 | +#include "systemc.h" |
| 10 | +using namespace sc_core; |
| 11 | +using namespace sc_dt; |
| 12 | +using namespace std; |
| 13 | + |
| 14 | +#include "tlm.h" |
| 15 | +#include "tlm_utils/simple_initiator_socket.h" |
| 16 | +#include "tlm_utils/simple_target_socket.h" |
| 17 | +#include "tlm_utils/peq_with_cb_and_phase.h" |
| 18 | + |
| 19 | +//const char* tlm_enum_names[] = {"TLM_ACCEPTED", "TLM_UPDATED", "TLM_COMPLETED"}; |
| 20 | + |
| 21 | +// Initiator module generating generic payload transactions |
| 22 | +struct img_initiator: sc_module |
| 23 | +{ |
| 24 | + // TLM2.0 Socket |
| 25 | + tlm_utils::simple_initiator_socket<img_initiator> socket; |
| 26 | + |
| 27 | + //Memory Manager for transaction memory allocation |
| 28 | + mm memory_manager; |
| 29 | + |
| 30 | + //Address for this Initiator |
| 31 | + unsigned int address; |
| 32 | + unsigned char* data; |
| 33 | + unsigned int data_length; |
| 34 | + |
| 35 | + //Pointer to transaction in progress |
| 36 | + tlm::tlm_generic_payload* pending_transaction; |
| 37 | + |
| 38 | + //Payload event queue with callback and phase |
| 39 | + tlm_utils::peq_with_cb_and_phase<img_initiator> m_peq; |
| 40 | + |
| 41 | + //Events |
| 42 | + sc_event transaction_received_e; |
| 43 | + |
| 44 | + //Delay |
| 45 | + sc_time write_delay = sc_time(10, SC_NS); |
| 46 | + sc_time read_delay = sc_time(10,SC_NS); |
| 47 | + |
| 48 | + //Constructor |
| 49 | + SC_CTOR(img_initiator) |
| 50 | + : socket("socket"), pending_transaction(0), m_peq(this, &img_initiator::peq_cb) // Construct and name socket |
| 51 | + { |
| 52 | + // Register callbacks for incoming interface method calls |
| 53 | + socket.register_nb_transport_bw(this, &img_initiator::nb_transport_bw); |
| 54 | + } |
| 55 | + |
| 56 | + //Method to send_reading transaction and wait for response |
| 57 | + void read (int*& data, unsigned int address, unsigned int data_length){ |
| 58 | + |
| 59 | + //Create transaction and allocate it |
| 60 | + tlm::tlm_generic_payload* transaction = memory_manager.allocate(); |
| 61 | + |
| 62 | + //Set transaction fields |
| 63 | + transaction->set_command(tlm::TLM_READ_COMMAND); |
| 64 | + transaction->set_address(address); |
| 65 | + transaction->set_data_ptr(reinterpret_cast<unsigned char*>(data)); |
| 66 | + transaction->set_data_length(data_length); //In Bytes |
| 67 | + transaction->set_streaming_width(data_length); |
| 68 | + transaction->set_byte_enable_ptr(0); |
| 69 | + transaction->set_dmi_allowed(false); //Mandatory Initial Value |
| 70 | + transaction->set_response_status(tlm::TLM_INCOMPLETE_RESPONSE); //Mandatory Initial Value |
| 71 | + |
| 72 | + //Send transaction |
| 73 | + this->send_transaction(transaction); |
| 74 | + |
| 75 | + data = reinterpret_cast<int*>(this->data); |
| 76 | + //-----------DEBUG----------- |
| 77 | + printf("[DEBUG] Reading at Initiator: "); |
| 78 | + for (int i = 0; i < this->pending_transaction->get_data_length()/sizeof(int); ++i){ |
| 79 | + printf("%02x", *(reinterpret_cast<int*>(this->data)+i)); |
| 80 | + } |
| 81 | + printf("\n"); |
| 82 | + //-----------DEBUG----------- |
| 83 | + } |
| 84 | + |
| 85 | + void write (int*& data, unsigned int address, unsigned int data_length){ |
| 86 | + |
| 87 | + //Create transaction and allocate it |
| 88 | + tlm::tlm_generic_payload* transaction = memory_manager.allocate(); |
| 89 | + |
| 90 | + //Set transaction fields |
| 91 | + transaction->set_command(tlm::TLM_WRITE_COMMAND); |
| 92 | + transaction->set_address(address); |
| 93 | + transaction->set_data_ptr(reinterpret_cast<unsigned char*>(data)); |
| 94 | + transaction->set_data_length(data_length); //In Bytes |
| 95 | + transaction->set_streaming_width(data_length); |
| 96 | + transaction->set_byte_enable_ptr(0); |
| 97 | + transaction->set_dmi_allowed(false); //Mandatory Initial Value |
| 98 | + transaction->set_response_status(tlm::TLM_INCOMPLETE_RESPONSE); //Mandatory Initial Value |
| 99 | + |
| 100 | + //-----------DEBUG----------- |
| 101 | + printf("[DEBUG] Writing: "); |
| 102 | + for (int i = 0; i < data_length/sizeof(int); ++i){ |
| 103 | + printf("%02x", *(data+i)); |
| 104 | + } |
| 105 | + printf("\n"); |
| 106 | + //-----------DEBUG----------- |
| 107 | + |
| 108 | + //Set transaction |
| 109 | + this->send_transaction(transaction); |
| 110 | + } |
| 111 | + |
| 112 | + void send_transaction(tlm::tlm_generic_payload*& transaction) { |
| 113 | + |
| 114 | + //Transaction Management Variables |
| 115 | + tlm::tlm_phase phase; |
| 116 | + tlm::tlm_sync_enum status; |
| 117 | + |
| 118 | + //Begin Request |
| 119 | + phase = tlm::BEGIN_REQ; |
| 120 | + transaction->acquire(); |
| 121 | + cout << name() << " BEGIN_REQ SENT" << " TRANS ID " << 0 << " at time " << sc_time_stamp() << endl; |
| 122 | + pending_transaction = transaction; |
| 123 | + status = socket->nb_transport_fw(*transaction, phase, this->write_delay); // Non-blocking transport call |
| 124 | + |
| 125 | + // Check request status returned by target |
| 126 | + switch (status) { |
| 127 | + //Case 1: Transaction was accepted |
| 128 | + case tlm::TLM_ACCEPTED: { |
| 129 | + printf("%s:\t %s received -> Transaction ID %d at time %s\n", name(), "TLM_ACCEPTED", 0, sc_time_stamp()); |
| 130 | + //cout << name() << " TLM_ACCEPTED RECEIVED " << " TRANS ID " << transaction->transaction_id << " at time " << sc_time_stamp() << endl; |
| 131 | + check_transaction(*transaction); |
| 132 | + transaction->release(); |
| 133 | + pending_transaction = 0; |
| 134 | + //Initiator only cares about sending the transaction, doesnt need to wait for response (non-blocking) |
| 135 | + break; |
| 136 | + } |
| 137 | + |
| 138 | + //Not implementing Updated and Completed Status |
| 139 | + default: { |
| 140 | + printf("%s:\t [ERROR] Invalid status received at initiator", name()); |
| 141 | + break; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + //Wait for response transaction |
| 146 | + wait(transaction_received_e); |
| 147 | + //-----------DEBUG----------- |
| 148 | + printf("[DEBUG1] Reading at Initiator: "); |
| 149 | + for (int i = 0; i < this->data_length/sizeof(int); ++i){ |
| 150 | + printf("%02x", *(reinterpret_cast<int*>(this->data)+i)); |
| 151 | + } |
| 152 | + printf("\n"); |
| 153 | + //-----------DEBUG----------- |
| 154 | + |
| 155 | + //Increment transaction ID |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | + // TLM2 backward path non-blocking transport method |
| 160 | + virtual tlm::tlm_sync_enum nb_transport_bw(tlm::tlm_generic_payload& trans, |
| 161 | + tlm::tlm_phase& phase, sc_time& delay ) |
| 162 | + { |
| 163 | + //Call event queue |
| 164 | + m_peq.notify(trans, phase, delay); |
| 165 | + cout<<"HERE"<<endl; |
| 166 | + return tlm::TLM_ACCEPTED; |
| 167 | + } |
| 168 | + |
| 169 | + //Payload event and queue callback to handle transactions received from target |
| 170 | + void peq_cb(tlm::tlm_generic_payload& trans, const tlm::tlm_phase& phase) |
| 171 | + { |
| 172 | + |
| 173 | + //printf("%s:\t %s received -> Transaction ID %d from address %x at time %s\n", name(), phase, this->id_extension->transaction_id, trans.get_address(), sc_time_stamp()); |
| 174 | + //cout << name() << " " <<hex << trans.get_address() << " BEGIN_RESP RECEIVED at " << sc_time_stamp() << endl; |
| 175 | + switch (phase) { |
| 176 | + case tlm::BEGIN_RESP: { |
| 177 | + |
| 178 | + cout<<"HERE3"<<endl; |
| 179 | + |
| 180 | + trans.acquire(); |
| 181 | + this->data_length = trans.get_data_length(); |
| 182 | + this->data = new unsigned char[this->data_length]; |
| 183 | + memcpy(this->data, trans.get_data_ptr(), this->data_length); |
| 184 | + |
| 185 | + this->pending_transaction = &trans; //Set response transaction to return |
| 186 | + check_transaction(trans); |
| 187 | + |
| 188 | + //Initiator dont care about confirming resp transaction. So nothing else to do. |
| 189 | + |
| 190 | + //-----------DEBUG----------- |
| 191 | + printf("[DEBUG] Reading at Initiator: "); |
| 192 | + for (int i = 0; i < trans.get_data_length()/sizeof(int); ++i){ |
| 193 | + printf("%02x", *(reinterpret_cast<int*>(trans.get_data_ptr())+i)); |
| 194 | + } |
| 195 | + printf("\n"); |
| 196 | + //-----------DEBUG----------- |
| 197 | + |
| 198 | + cout<<"HERE3"<<endl; |
| 199 | + |
| 200 | + transaction_received_e.notify(); |
| 201 | + //-----------DEBUG----------- |
| 202 | + printf("[DEBUG] Reading at Initiator: "); |
| 203 | + for (int i = 0; i < this->data_length/sizeof(int); ++i){ |
| 204 | + printf("%02x", *(reinterpret_cast<int*>(this->data)+i)); |
| 205 | + } |
| 206 | + printf("\n"); |
| 207 | + //-----------DEBUG----------- |
| 208 | + cout<<"HERE10"<<endl; |
| 209 | + break; |
| 210 | + } |
| 211 | + default: { |
| 212 | + SC_REPORT_FATAL("TLM-2", "Illegal transaction phase received by initiator"); |
| 213 | + break; |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + //Function to check transaction integrity |
| 219 | + void check_transaction(tlm::tlm_generic_payload& trans) |
| 220 | + { |
| 221 | + //Check transaction if here |
| 222 | + |
| 223 | + // tlm::tlm_command command = trans.get_command(); |
| 224 | + // sc_dt::uint64 address = trans.get_address(); |
| 225 | + // unsigned char* data_ptr = reinterpret_cast<unsigned char*>(trans.get_data_ptr()); |
| 226 | + |
| 227 | + // Allow the memory manager to free the transaction object |
| 228 | + //trans.release(); |
| 229 | + } |
| 230 | +} ; |
| 231 | + |
| 232 | +#endif |
0 commit comments