Jump to content

Recommended Posts

Posted

Hi everyone,
I'm met some problem. In box below you can see my code.

struct Some_stuct {
	int		data;
	float	data_f;
};

class payload_t {
public:
	unsigned char	*data;
	unsigned		lenght;

	inline friend ostream& operator << ( ostream& os,  payload_t const & v ) {
		os << "(" << v.data << "," << std::boolalpha << "," << v.lenght << ")";
       return os;
   }
};

SC_MODULE(tx) {
	sc_port<sc_fifo_out_if<payload_t> > out_port;

	void process() {
		Some_stuct data;
		payload_t out_data;

		while(true) {		

			data.data	= 5;
			data.data_f	= 6;

			out_data.data	=  reinterpret_cast<unsigned char*>(&data);
			out_data.lenght =  sizeof(Some_stuct);	
						
			out_port->write(out_data);

			data.data++;
			data.data_f++;
			wait(1, SC_NS);
		}
	}

	SC_CTOR(tx) {
		SC_THREAD(process);
	}
};

SC_MODULE(rx) {
	sc_port<sc_fifo_in_if<payload_t> > 	in_port;
	
	void process() {
		int realData = 0;
		payload_t in_data;

		while(true) {
			wait(in_port->data_written_event());			
			wait(171, SC_NS);
			in_data = in_port->read();			
			
			Some_stuct *val = reinterpret_cast<Some_stuct*>(in_data.data);
			cout << sc_time_stamp() << " " << val->data << " "  << val->data_f << endl;
		}
	}

	SC_CTOR(rx) {
		SC_THREAD(process);
	}
};


int sc_main(int argc, char* argv[]) {

	rx rx_i("rx");
	tx tx_i("tx");

	sc_fifo<payload_t> buf(8);
	tx_i.out_port(buf);
	rx_i.in_port(buf);
  
	sc_start();

	getchar();
	return 0;
}

I'm have two blocks: transmitter and receiver. When we call method write to fifo from transmitter, actually we will send address of our local data. And when i will read from buffer, I will receive address of transmitter local data. But if transmitter works more faster than receiver, my rx block will receive "New" data and sended data will loss. How to solve this problem? I should receive correct data

Posted

I'm not sure if I understand your question.

 

But if you send a pointer through your fifo, and you expect to keep the data at the receiver, then you need to take a copy of the data immediately when you receive  it.

 

regards

Alan

Posted

Hi everyone,

I'm met some problem. In box below you can see my code.

struct Some_stuct {
	int		data;
	float	data_f;
};

class payload_t {
public:
	unsigned char	*data;
	unsigned		lenght;

	inline friend ostream& operator << ( ostream& os,  payload_t const & v ) {
		os << "(" << v.data << "," << std::boolalpha << "," << v.lenght << ")";
       return os;
   }
};

SC_MODULE(tx) {
	sc_port<sc_fifo_out_if<payload_t> > out_port;

	void process() {
		Some_stuct data;
		payload_t out_data;

		while(true) {		

			data.data	= 5;
			data.data_f	= 6;

			out_data.data	=  reinterpret_cast<unsigned char*>(&data);
			out_data.lenght =  sizeof(Some_stuct);	
						
			out_port->write(out_data);

			data.data++;
			data.data_f++;
			wait(1, SC_NS);
		}
	}

	SC_CTOR(tx) {
		SC_THREAD(process);
	}
};

SC_MODULE(rx) {
	sc_port<sc_fifo_in_if<payload_t> > 	in_port;
	
	void process() {
		int realData = 0;
		payload_t in_data;

		while(true) {
			wait(in_port->data_written_event());			
			wait(171, SC_NS);
			in_data = in_port->read();			
			
			Some_stuct *val = reinterpret_cast<Some_stuct*>(in_data.data);
			cout << sc_time_stamp() << " " << val->data << " "  << val->data_f << endl;
		}
	}

	SC_CTOR(rx) {
		SC_THREAD(process);
	}
};


int sc_main(int argc, char* argv[]) {

	rx rx_i("rx");
	tx tx_i("tx");

	sc_fifo<payload_t> buf(8);
	tx_i.out_port(buf);
	rx_i.in_port(buf);
  
	sc_start();

	getchar();
	return 0;
}

I'm have two blocks: transmitter and receiver. When we call method write to fifo from transmitter, actually we will send address of our local data. And when i will read from buffer, I will receive address of transmitter local data. But if transmitter works more faster than receiver, my rx block will receive "New" data and sended data will loss. How to solve this problem? I should receive correct data

 

Hello Sir,

It appears that you are trying to analyse some network protocol(or something 

like that) and it is not working. There are a few issues with your modules, as 

listed below:

1. For both your transmitter and receiver, what are the triggering events for

your threads ? A clock ? A change in data coming over the port ? In short,

what events are your threads "waiting" on ?

2. Most importantly, as mentioned with reference to another poster on this

newsgroup, the reason sc_fifo works with the built-in data types is that at

the time of elaboration, the space required to store a built-in data type is

already known - 'x' bytes for int, 'y' bytes for float, etc., Here you have your

own data type, "Some_struct" and to transfer your own data type between

the transmitter and receiver,  your need your custom channel and port. To

achieve this:

A. You would have to declare a custom interface that is a sub-class of 

sc_core::sc_interface

B. You would have to create a class that implements the custom interface -

the actual channel

C. Create a custom port to read/write data from/to your custom channel

 

There is no other option - using syntactic sugar as reinterpret_cast would

not work. You have run into a core limitation of C++ templates. Hope that

helps.

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...