Yes, you can send a value of a variable of type std::array<sc_fixed<16, 9>, 64> using the void set_data_ptr(unsigned char*) member function of the tlm_generic_payload class. However, you need to be careful about how you handle the data type and ensure that the pointer you pass is correctly aligned and points to the correct memory location.
Here's a step-by-step approach to achieve this:
-
Ensure Proper Alignment and Byte Order:
Ensure that the data you are sending is properly aligned and that the byte order is consistent between the sender and the receiver.
-
Convert the Data to unsigned char*:
You need to cast the pointer of your std::array<sc_fixed<16, 9>, 64> to unsigned char*.
-
Set the Data Pointer in tlm_generic_payload:
Use the set_data_ptr method to set the pointer to your data.
-
Handle the Data in b_transport:
In the b_transport method, you will receive the tlm_generic_payload object and can extract the data pointer from it.
Here's an example to illustrate this:
#include <systemc.h>
#include <tlm.h>
#include <tlm_utils/simple_initiator_socket.h>
#include <tlm_utils/simple_target_socket.h>
// Define your custom data type
using my_data_type = std::array<sc_fixed<16, 9>, 64>;
// Initiator module
class Initiator : public sc_module {
public:
Initiator(sc_module_name name)
: socket("socket") {
socket.register_b_transport(this, &Initiator::b_transport);
}
void send_data() {
my_data_type data = { /* Initialize your data here */ };
// Create a tlm_generic_payload
tlm::tlm_generic_payload trans;
trans.set_data_ptr(reinterpret_cast<unsigned char*>(data.data()));
trans.set_data_length(data.size() * sizeof(sc_fixed<16, 9>));
trans.set_streaming_width(data.size() * sizeof(sc_fixed<16, 9>));
trans.set_command(tlm::TLM_WRITE_COMMAND);
trans.set_address(0); // Example address
sc_time delay;
socket->b_transport(trans, delay);
}
tlm::tlm_utils::simple_initiator_socket<Initiator> socket;
private:
void b_transport(tlm::tlm_generic_payload& trans, sc_time& delay) {
// This method will be called by the target
// You can handle the transport here if needed
}
};
// Target module
class Target : public sc_module {
public:
Target(sc_module_name name)
: socket("socket") {
}
void b_transport(tlm::tlm_generic_payload& trans, sc_time& delay) {
// Extract the data from the payload
unsigned char* data_ptr = trans.get_data_ptr();
uint64_t data_length = trans.get_data_length();
// Convert the data back to the original type
my_data_type received_data;
std::copy(data_ptr, data_ptr + data_length, received_data.begin());
// Process the received data
// ...
}
tlm::tlm_utils::simple_target_socket<Target> socket;
};
int sc_main(int argc, char* argv[]) {
Initiator initiator("initiator");
Target target("target");
initiator.socket.bind(target.socket);
initiator.send_data();
sc_start();
return 0;
}
Key Points:
- Data Alignment and Byte Order: Ensure that the data is correctly aligned and the byte order is consistent.
- Pointer Casting: Use
reinterpret_cast to convert the pointer to unsigned char*.
- Data Length: Set the data length appropriately to ensure the entire array is transferred.
This example demonstrates how to send and receive a custom data type using TLM. Adjust the data initialization and processing as needed for your specific application.