Jump to content

[SOLVED] How to send float value with LT TLM2.0 and read it back


Recommended Posts

Hello everyone,

so I'm learning systemC and currently I'm researching tlm2.0. I know how to send int values with tlm2.0 but I really can't send any float value (e.g. 12.53) and read it back. I'm using generator module and memory module, where generator module is generating transactions and memory module is receiving them.

I focused on just how to insert float values inside unsigned char array. As I tested my solution with simple test.cpp to insert float value inside unsigned char array and read it back, I tried to implement that without success inside systemc tlm2.0.

I tried with direct memory access:


	// Init first 20 locations to FF with DMI access
	tlm_dmi dmi;
	dmi_valid = isoc->get_direct_mem_ptr(pl, dmi); 
	if (dmi_valid)
	{
		dmi_mem = dmi.get_dmi_ptr(); //dmi_mem is pointer to ram[] array in memory.h
		for (int i = 0; i != 20; ++i)
			fl_ptr[i] = 12.7;
    //dmi_mem[i] = 0xFF;
	}

but I couldn't read correct values back with:

	isoc->transport_dbg(dbg_pl);
		msg = " RAM at time " + sc_time_stamp().to_string();
		msg += "\n";
    unsigned char *moj_p;
    moj_p = dbg_pl.get_data_ptr();
    float* moj_p_fl = reinterpret_cast<float*>(moj_p);
    std::cout << "it was written: "<<std::endl;

		for (int i = 0; i != 200; ++i)
		{
			msg += std::to_string(moj_p_fl[i]);
			msg += " ";
		}
		SC_REPORT_INFO("generator", msg.c_str());

Or should I say, there are values I inserted into array but not on the right places.

Any help is much appreciated 🙂

Tom

 

generator.cpp generator.hpp main.cpp memory.cpp memory.hpp test.cpp

Link to comment
Share on other sites

You are initailaizing fl_ptr during consturction, not during execution. In generator.hpp you have:

 float* fl_ptr = reinterpret_cast<float*>(dmi_mem); //ovo sam ja pisao

This never updates fl_ptr to the actual value of dmi_ptr.

Actually your access should look like:

if (dmi_valid)
{
	dmi_mem = dmi.get_dmi_ptr(); //dmi_mem is pointer to ram[] array in memory.h
    float* fl_ptr = reinterpret_cast<float*>(dmi_mem);
	for (int i = 0; i != 20; ++i)
		fl_ptr[i] = 12.7;
}

 

Link to comment
Share on other sites

Thanks @Eyck, that did the trick. For future reference, I will put here my whole solution for LT TLM2.0 for writing and reading floating values.

Normal TLM2.0 transport - write :

	// Normal TLM transport interface.
	for (unsigned int i = 0; i != 24; ++i)
	{
		unsigned int data_length = 4;
		unsigned int addr = rand() % 200;
		tlm_command cmd = i < 20 ? TLM_WRITE_COMMAND : TLM_READ_COMMAND;
		std::string msg = cmd == TLM_WRITE_COMMAND ? "Write " : "Read  ";

    //OK, so whene you you want to write with regular mode into array, you should know that float value is 4 byte
    //and unsigned char is 1 byte. To be able to write float to unsigned char array, you must cast unsigned char array
    //to char array.
    float* buf_fl = reinterpret_cast<float*>(buf);

    buf_fl[0]=555.23;

    
		pl.set_command         ( TLM_WRITE_COMMAND       );
		pl.set_address         ( 8                     );//targeting ram[2]
		pl.set_data_ptr        (buf );
		pl.set_data_length     ( 4                       );
		pl.set_response_status ( TLM_INCOMPLETE_RESPONSE );


		 isoc->b_transport(pl, offset);


     buf_fl[0]=777.23;
     pl.set_command         ( TLM_WRITE_COMMAND       );
     pl.set_address         ( 20                      );//targeting ram[5]
     pl.set_data_ptr        (buf );
     pl.set_data_length     ( 4                       );
     pl.set_response_status ( TLM_INCOMPLETE_RESPONSE );
		 isoc->b_transport(pl, offset);


     buf_fl[0]=654.321;
     pl.set_command         ( TLM_WRITE_COMMAND       );
     pl.set_address         ( 796                      );//targeting ram[199]
     pl.set_data_ptr        (buf );
     pl.set_data_length     ( 4                       );
     pl.set_response_status ( TLM_INCOMPLETE_RESPONSE );
		 isoc->b_transport(pl, offset);

Normal TLM2.0 transport - read:

		isoc->transport_dbg(dbg_pl);
		msg = " RAM at time " + sc_time_stamp().to_string();
		msg += "\n";
    unsigned char *moj_p;
    moj_p = dbg_pl.get_data_ptr();
    float* moj_p_fl = reinterpret_cast<float*>(moj_p);
    std::cout << "it was written: "<<std::endl;

    std::cout<<"teting location 2 and 199: "<<moj_p_fl[2]<<"  "<<moj_p_fl[199]<<std::endl;

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...